1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * 27 * usb_dev.c - An abstraction layer for creating devices under /dev/... 28 */ 29 30 #include <sys/stdint.h> 31 #include <sys/stddef.h> 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/bus.h> 38 #include <sys/linker_set.h> 39 #include <sys/module.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/condvar.h> 43 #include <sys/sysctl.h> 44 #include <sys/sx.h> 45 #include <sys/unistd.h> 46 #include <sys/callout.h> 47 #include <sys/malloc.h> 48 #include <sys/priv.h> 49 #include <sys/vnode.h> 50 #include <sys/conf.h> 51 #include <sys/fcntl.h> 52 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usb_ioctl.h> 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 58 #define USB_DEBUG_VAR usb_fifo_debug 59 60 #include <dev/usb/usb_core.h> 61 #include <dev/usb/usb_dev.h> 62 #include <dev/usb/usb_mbuf.h> 63 #include <dev/usb/usb_process.h> 64 #include <dev/usb/usb_device.h> 65 #include <dev/usb/usb_debug.h> 66 #include <dev/usb/usb_busdma.h> 67 #include <dev/usb/usb_generic.h> 68 #include <dev/usb/usb_dynamic.h> 69 #include <dev/usb/usb_util.h> 70 71 #include <dev/usb/usb_controller.h> 72 #include <dev/usb/usb_bus.h> 73 74 #include <sys/filio.h> 75 #include <sys/ttycom.h> 76 #include <sys/syscallsubr.h> 77 78 #include <machine/stdarg.h> 79 80 #if USB_HAVE_UGEN 81 82 #ifdef USB_DEBUG 83 static int usb_fifo_debug = 0; 84 85 SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device"); 86 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW, 87 &usb_fifo_debug, 0, "Debug Level"); 88 #endif 89 90 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \ 91 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000))) 92 #define USB_UCRED struct ucred *ucred, 93 #else 94 #define USB_UCRED 95 #endif 96 97 /* prototypes */ 98 99 static int usb_fifo_open(struct usb_cdev_privdata *, 100 struct usb_fifo *, int); 101 static void usb_fifo_close(struct usb_fifo *, int); 102 static void usb_dev_init(void *); 103 static void usb_dev_init_post(void *); 104 static void usb_dev_uninit(void *); 105 static int usb_fifo_uiomove(struct usb_fifo *, void *, int, 106 struct uio *); 107 static void usb_fifo_check_methods(struct usb_fifo_methods *); 108 static struct usb_fifo *usb_fifo_alloc(void); 109 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t, 110 uint8_t); 111 static void usb_loc_fill(struct usb_fs_privdata *, 112 struct usb_cdev_privdata *); 113 static void usb_close(void *); 114 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int); 115 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *); 116 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *); 117 118 static d_open_t usb_open; 119 static d_ioctl_t usb_ioctl; 120 static d_read_t usb_read; 121 static d_write_t usb_write; 122 static d_poll_t usb_poll; 123 124 static d_ioctl_t usb_static_ioctl; 125 126 static usb_fifo_open_t usb_fifo_dummy_open; 127 static usb_fifo_close_t usb_fifo_dummy_close; 128 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl; 129 static usb_fifo_cmd_t usb_fifo_dummy_cmd; 130 131 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */ 132 struct cdevsw usb_devsw = { 133 .d_version = D_VERSION, 134 .d_open = usb_open, 135 .d_ioctl = usb_ioctl, 136 .d_name = "usbdev", 137 .d_flags = D_TRACKCLOSE, 138 .d_read = usb_read, 139 .d_write = usb_write, 140 .d_poll = usb_poll 141 }; 142 143 static struct cdev* usb_dev = NULL; 144 145 /* character device structure used for /dev/usb */ 146 static struct cdevsw usb_static_devsw = { 147 .d_version = D_VERSION, 148 .d_ioctl = usb_static_ioctl, 149 .d_name = "usb" 150 }; 151 152 static TAILQ_HEAD(, usb_symlink) usb_sym_head; 153 static struct sx usb_sym_lock; 154 155 struct mtx usb_ref_lock; 156 157 /*------------------------------------------------------------------------* 158 * usb_loc_fill 159 * 160 * This is used to fill out a usb_cdev_privdata structure based on the 161 * device's address as contained in usb_fs_privdata. 162 *------------------------------------------------------------------------*/ 163 static void 164 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd) 165 { 166 cpd->bus_index = pd->bus_index; 167 cpd->dev_index = pd->dev_index; 168 cpd->ep_addr = pd->ep_addr; 169 cpd->fifo_index = pd->fifo_index; 170 } 171 172 /*------------------------------------------------------------------------* 173 * usb_ref_device 174 * 175 * This function is used to atomically refer an USB device by its 176 * device location. If this function returns success the USB device 177 * will not dissappear until the USB device is unreferenced. 178 * 179 * Return values: 180 * 0: Success, refcount incremented on the given USB device. 181 * Else: Failure. 182 *------------------------------------------------------------------------*/ 183 usb_error_t 184 usb_ref_device(struct usb_cdev_privdata *cpd, 185 struct usb_cdev_refdata *crd, int need_uref) 186 { 187 struct usb_fifo **ppf; 188 struct usb_fifo *f; 189 190 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref); 191 192 /* clear all refs */ 193 memset(crd, 0, sizeof(*crd)); 194 195 mtx_lock(&usb_ref_lock); 196 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index); 197 if (cpd->bus == NULL) { 198 DPRINTFN(2, "no bus at %u\n", cpd->bus_index); 199 goto error; 200 } 201 cpd->udev = cpd->bus->devices[cpd->dev_index]; 202 if (cpd->udev == NULL) { 203 DPRINTFN(2, "no device at %u\n", cpd->dev_index); 204 goto error; 205 } 206 if (cpd->udev->refcount == USB_DEV_REF_MAX) { 207 DPRINTFN(2, "no dev ref\n"); 208 goto error; 209 } 210 if (need_uref) { 211 DPRINTFN(2, "ref udev - needed\n"); 212 cpd->udev->refcount++; 213 214 mtx_unlock(&usb_ref_lock); 215 216 /* 217 * We need to grab the sx-lock before grabbing the 218 * FIFO refs to avoid deadlock at detach! 219 */ 220 sx_xlock(cpd->udev->default_sx + 1); 221 222 mtx_lock(&usb_ref_lock); 223 224 /* 225 * Set "is_uref" after grabbing the default SX lock 226 */ 227 crd->is_uref = 1; 228 } 229 230 /* check if we are doing an open */ 231 if (cpd->fflags == 0) { 232 /* use zero defaults */ 233 } else { 234 /* check for write */ 235 if (cpd->fflags & FWRITE) { 236 ppf = cpd->udev->fifo; 237 f = ppf[cpd->fifo_index + USB_FIFO_TX]; 238 crd->txfifo = f; 239 crd->is_write = 1; /* ref */ 240 if (f == NULL || f->refcount == USB_FIFO_REF_MAX) 241 goto error; 242 if (f->curr_cpd != cpd) 243 goto error; 244 /* check if USB-FS is active */ 245 if (f->fs_ep_max != 0) { 246 crd->is_usbfs = 1; 247 } 248 } 249 250 /* check for read */ 251 if (cpd->fflags & FREAD) { 252 ppf = cpd->udev->fifo; 253 f = ppf[cpd->fifo_index + USB_FIFO_RX]; 254 crd->rxfifo = f; 255 crd->is_read = 1; /* ref */ 256 if (f == NULL || f->refcount == USB_FIFO_REF_MAX) 257 goto error; 258 if (f->curr_cpd != cpd) 259 goto error; 260 /* check if USB-FS is active */ 261 if (f->fs_ep_max != 0) { 262 crd->is_usbfs = 1; 263 } 264 } 265 } 266 267 /* when everything is OK we increment the refcounts */ 268 if (crd->is_write) { 269 DPRINTFN(2, "ref write\n"); 270 crd->txfifo->refcount++; 271 } 272 if (crd->is_read) { 273 DPRINTFN(2, "ref read\n"); 274 crd->rxfifo->refcount++; 275 } 276 mtx_unlock(&usb_ref_lock); 277 278 if (crd->is_uref) { 279 mtx_lock(&Giant); /* XXX */ 280 } 281 return (0); 282 283 error: 284 if (crd->is_uref) { 285 sx_unlock(cpd->udev->default_sx + 1); 286 if (--(cpd->udev->refcount) == 0) { 287 cv_signal(cpd->udev->default_cv + 1); 288 } 289 } 290 mtx_unlock(&usb_ref_lock); 291 DPRINTFN(2, "fail\n"); 292 return (USB_ERR_INVAL); 293 } 294 295 /*------------------------------------------------------------------------* 296 * usb_usb_ref_device 297 * 298 * This function is used to upgrade an USB reference to include the 299 * USB device reference on a USB location. 300 * 301 * Return values: 302 * 0: Success, refcount incremented on the given USB device. 303 * Else: Failure. 304 *------------------------------------------------------------------------*/ 305 static usb_error_t 306 usb_usb_ref_device(struct usb_cdev_privdata *cpd, 307 struct usb_cdev_refdata *crd) 308 { 309 /* 310 * Check if we already got an USB reference on this location: 311 */ 312 if (crd->is_uref) 313 return (0); /* success */ 314 315 /* 316 * To avoid deadlock at detach we need to drop the FIFO ref 317 * and re-acquire a new ref! 318 */ 319 usb_unref_device(cpd, crd); 320 321 return (usb_ref_device(cpd, crd, 1 /* need uref */)); 322 } 323 324 /*------------------------------------------------------------------------* 325 * usb_unref_device 326 * 327 * This function will release the reference count by one unit for the 328 * given USB device. 329 *------------------------------------------------------------------------*/ 330 void 331 usb_unref_device(struct usb_cdev_privdata *cpd, 332 struct usb_cdev_refdata *crd) 333 { 334 335 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref); 336 337 if (crd->is_uref) { 338 mtx_unlock(&Giant); /* XXX */ 339 sx_unlock(cpd->udev->default_sx + 1); 340 } 341 mtx_lock(&usb_ref_lock); 342 if (crd->is_read) { 343 if (--(crd->rxfifo->refcount) == 0) { 344 cv_signal(&crd->rxfifo->cv_drain); 345 } 346 crd->is_read = 0; 347 } 348 if (crd->is_write) { 349 if (--(crd->txfifo->refcount) == 0) { 350 cv_signal(&crd->txfifo->cv_drain); 351 } 352 crd->is_write = 0; 353 } 354 if (crd->is_uref) { 355 if (--(cpd->udev->refcount) == 0) { 356 cv_signal(cpd->udev->default_cv + 1); 357 } 358 crd->is_uref = 0; 359 } 360 mtx_unlock(&usb_ref_lock); 361 } 362 363 static struct usb_fifo * 364 usb_fifo_alloc(void) 365 { 366 struct usb_fifo *f; 367 368 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO); 369 if (f) { 370 cv_init(&f->cv_io, "FIFO-IO"); 371 cv_init(&f->cv_drain, "FIFO-DRAIN"); 372 f->refcount = 1; 373 } 374 return (f); 375 } 376 377 /*------------------------------------------------------------------------* 378 * usb_fifo_create 379 *------------------------------------------------------------------------*/ 380 static int 381 usb_fifo_create(struct usb_cdev_privdata *cpd, 382 struct usb_cdev_refdata *crd) 383 { 384 struct usb_device *udev = cpd->udev; 385 struct usb_fifo *f; 386 struct usb_endpoint *ep; 387 uint8_t n; 388 uint8_t is_tx; 389 uint8_t is_rx; 390 uint8_t no_null; 391 uint8_t is_busy; 392 int e = cpd->ep_addr; 393 394 is_tx = (cpd->fflags & FWRITE) ? 1 : 0; 395 is_rx = (cpd->fflags & FREAD) ? 1 : 0; 396 no_null = 1; 397 is_busy = 0; 398 399 /* Preallocated FIFO */ 400 if (e < 0) { 401 DPRINTFN(5, "Preallocated FIFO\n"); 402 if (is_tx) { 403 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX]; 404 if (f == NULL) 405 return (EINVAL); 406 crd->txfifo = f; 407 } 408 if (is_rx) { 409 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX]; 410 if (f == NULL) 411 return (EINVAL); 412 crd->rxfifo = f; 413 } 414 return (0); 415 } 416 417 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e)); 418 419 /* search for a free FIFO slot */ 420 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e); 421 for (n = 0;; n += 2) { 422 423 if (n == USB_FIFO_MAX) { 424 if (no_null) { 425 no_null = 0; 426 n = 0; 427 } else { 428 /* end of FIFOs reached */ 429 DPRINTFN(5, "out of FIFOs\n"); 430 return (ENOMEM); 431 } 432 } 433 /* Check for TX FIFO */ 434 if (is_tx) { 435 f = udev->fifo[n + USB_FIFO_TX]; 436 if (f != NULL) { 437 if (f->dev_ep_index != e) { 438 /* wrong endpoint index */ 439 continue; 440 } 441 if (f->curr_cpd != NULL) { 442 /* FIFO is opened */ 443 is_busy = 1; 444 continue; 445 } 446 } else if (no_null) { 447 continue; 448 } 449 } 450 /* Check for RX FIFO */ 451 if (is_rx) { 452 f = udev->fifo[n + USB_FIFO_RX]; 453 if (f != NULL) { 454 if (f->dev_ep_index != e) { 455 /* wrong endpoint index */ 456 continue; 457 } 458 if (f->curr_cpd != NULL) { 459 /* FIFO is opened */ 460 is_busy = 1; 461 continue; 462 } 463 } else if (no_null) { 464 continue; 465 } 466 } 467 break; 468 } 469 470 if (no_null == 0) { 471 if (e >= (USB_EP_MAX / 2)) { 472 /* we don't create any endpoints in this range */ 473 DPRINTFN(5, "ep out of range\n"); 474 return (is_busy ? EBUSY : EINVAL); 475 } 476 } 477 478 if ((e != 0) && is_busy) { 479 /* 480 * Only the default control endpoint is allowed to be 481 * opened multiple times! 482 */ 483 DPRINTFN(5, "busy\n"); 484 return (EBUSY); 485 } 486 487 /* Check TX FIFO */ 488 if (is_tx && 489 (udev->fifo[n + USB_FIFO_TX] == NULL)) { 490 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX); 491 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX); 492 if (ep == NULL) { 493 DPRINTFN(5, "dev_get_endpoint returned NULL\n"); 494 return (EINVAL); 495 } 496 f = usb_fifo_alloc(); 497 if (f == NULL) { 498 DPRINTFN(5, "could not alloc tx fifo\n"); 499 return (ENOMEM); 500 } 501 /* update some fields */ 502 f->fifo_index = n + USB_FIFO_TX; 503 f->dev_ep_index = e; 504 f->priv_mtx = udev->default_mtx; 505 f->priv_sc0 = ep; 506 f->methods = &usb_ugen_methods; 507 f->iface_index = ep->iface_index; 508 f->udev = udev; 509 mtx_lock(&usb_ref_lock); 510 udev->fifo[n + USB_FIFO_TX] = f; 511 mtx_unlock(&usb_ref_lock); 512 } 513 /* Check RX FIFO */ 514 if (is_rx && 515 (udev->fifo[n + USB_FIFO_RX] == NULL)) { 516 517 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX); 518 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX); 519 if (ep == NULL) { 520 DPRINTFN(5, "dev_get_endpoint returned NULL\n"); 521 return (EINVAL); 522 } 523 f = usb_fifo_alloc(); 524 if (f == NULL) { 525 DPRINTFN(5, "could not alloc rx fifo\n"); 526 return (ENOMEM); 527 } 528 /* update some fields */ 529 f->fifo_index = n + USB_FIFO_RX; 530 f->dev_ep_index = e; 531 f->priv_mtx = udev->default_mtx; 532 f->priv_sc0 = ep; 533 f->methods = &usb_ugen_methods; 534 f->iface_index = ep->iface_index; 535 f->udev = udev; 536 mtx_lock(&usb_ref_lock); 537 udev->fifo[n + USB_FIFO_RX] = f; 538 mtx_unlock(&usb_ref_lock); 539 } 540 if (is_tx) { 541 crd->txfifo = udev->fifo[n + USB_FIFO_TX]; 542 } 543 if (is_rx) { 544 crd->rxfifo = udev->fifo[n + USB_FIFO_RX]; 545 } 546 /* fill out fifo index */ 547 DPRINTFN(5, "fifo index = %d\n", n); 548 cpd->fifo_index = n; 549 550 /* complete */ 551 552 return (0); 553 } 554 555 void 556 usb_fifo_free(struct usb_fifo *f) 557 { 558 uint8_t n; 559 560 if (f == NULL) { 561 /* be NULL safe */ 562 return; 563 } 564 /* destroy symlink devices, if any */ 565 for (n = 0; n != 2; n++) { 566 if (f->symlink[n]) { 567 usb_free_symlink(f->symlink[n]); 568 f->symlink[n] = NULL; 569 } 570 } 571 mtx_lock(&usb_ref_lock); 572 573 /* delink ourselves to stop calls from userland */ 574 if ((f->fifo_index < USB_FIFO_MAX) && 575 (f->udev != NULL) && 576 (f->udev->fifo[f->fifo_index] == f)) { 577 f->udev->fifo[f->fifo_index] = NULL; 578 } else { 579 DPRINTFN(0, "USB FIFO %p has not been linked!\n", f); 580 } 581 582 /* decrease refcount */ 583 f->refcount--; 584 /* prevent any write flush */ 585 f->flag_iserror = 1; 586 /* need to wait until all callers have exited */ 587 while (f->refcount != 0) { 588 mtx_unlock(&usb_ref_lock); /* avoid LOR */ 589 mtx_lock(f->priv_mtx); 590 /* get I/O thread out of any sleep state */ 591 if (f->flag_sleeping) { 592 f->flag_sleeping = 0; 593 cv_broadcast(&f->cv_io); 594 } 595 mtx_unlock(f->priv_mtx); 596 mtx_lock(&usb_ref_lock); 597 598 /* wait for sync */ 599 cv_wait(&f->cv_drain, &usb_ref_lock); 600 } 601 mtx_unlock(&usb_ref_lock); 602 603 /* take care of closing the device here, if any */ 604 usb_fifo_close(f, 0); 605 606 cv_destroy(&f->cv_io); 607 cv_destroy(&f->cv_drain); 608 609 free(f, M_USBDEV); 610 } 611 612 static struct usb_endpoint * 613 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir) 614 { 615 struct usb_endpoint *ep; 616 uint8_t ep_dir; 617 618 if (ep_index == 0) { 619 ep = &udev->default_ep; 620 } else { 621 if (dir == USB_FIFO_RX) { 622 if (udev->flags.usb_mode == USB_MODE_HOST) { 623 ep_dir = UE_DIR_IN; 624 } else { 625 ep_dir = UE_DIR_OUT; 626 } 627 } else { 628 if (udev->flags.usb_mode == USB_MODE_HOST) { 629 ep_dir = UE_DIR_OUT; 630 } else { 631 ep_dir = UE_DIR_IN; 632 } 633 } 634 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir); 635 } 636 637 if (ep == NULL) { 638 /* if the endpoint does not exist then return */ 639 return (NULL); 640 } 641 if (ep->edesc == NULL) { 642 /* invalid endpoint */ 643 return (NULL); 644 } 645 return (ep); /* success */ 646 } 647 648 /*------------------------------------------------------------------------* 649 * usb_fifo_open 650 * 651 * Returns: 652 * 0: Success 653 * Else: Failure 654 *------------------------------------------------------------------------*/ 655 static int 656 usb_fifo_open(struct usb_cdev_privdata *cpd, 657 struct usb_fifo *f, int fflags) 658 { 659 int err; 660 661 if (f == NULL) { 662 /* no FIFO there */ 663 DPRINTFN(2, "no FIFO\n"); 664 return (ENXIO); 665 } 666 /* remove FWRITE and FREAD flags */ 667 fflags &= ~(FWRITE | FREAD); 668 669 /* set correct file flags */ 670 if ((f->fifo_index & 1) == USB_FIFO_TX) { 671 fflags |= FWRITE; 672 } else { 673 fflags |= FREAD; 674 } 675 676 /* check if we are already opened */ 677 /* we don't need any locks when checking this variable */ 678 if (f->curr_cpd != NULL) { 679 err = EBUSY; 680 goto done; 681 } 682 683 /* reset short flag before open */ 684 f->flag_short = 0; 685 686 /* call open method */ 687 err = (f->methods->f_open) (f, fflags); 688 if (err) { 689 goto done; 690 } 691 mtx_lock(f->priv_mtx); 692 693 /* reset sleep flag */ 694 f->flag_sleeping = 0; 695 696 /* reset error flag */ 697 f->flag_iserror = 0; 698 699 /* reset complete flag */ 700 f->flag_iscomplete = 0; 701 702 /* reset select flag */ 703 f->flag_isselect = 0; 704 705 /* reset flushing flag */ 706 f->flag_flushing = 0; 707 708 /* reset ASYNC proc flag */ 709 f->async_p = NULL; 710 711 mtx_lock(&usb_ref_lock); 712 /* flag the fifo as opened to prevent others */ 713 f->curr_cpd = cpd; 714 mtx_unlock(&usb_ref_lock); 715 716 /* reset queue */ 717 usb_fifo_reset(f); 718 719 mtx_unlock(f->priv_mtx); 720 done: 721 return (err); 722 } 723 724 /*------------------------------------------------------------------------* 725 * usb_fifo_reset 726 *------------------------------------------------------------------------*/ 727 void 728 usb_fifo_reset(struct usb_fifo *f) 729 { 730 struct usb_mbuf *m; 731 732 if (f == NULL) { 733 return; 734 } 735 while (1) { 736 USB_IF_DEQUEUE(&f->used_q, m); 737 if (m) { 738 USB_IF_ENQUEUE(&f->free_q, m); 739 } else { 740 break; 741 } 742 } 743 /* reset have fragment flag */ 744 f->flag_have_fragment = 0; 745 } 746 747 /*------------------------------------------------------------------------* 748 * usb_fifo_close 749 *------------------------------------------------------------------------*/ 750 static void 751 usb_fifo_close(struct usb_fifo *f, int fflags) 752 { 753 int err; 754 755 /* check if we are not opened */ 756 if (f->curr_cpd == NULL) { 757 /* nothing to do - already closed */ 758 return; 759 } 760 mtx_lock(f->priv_mtx); 761 762 /* clear current cdev private data pointer */ 763 f->curr_cpd = NULL; 764 765 /* check if we are selected */ 766 if (f->flag_isselect) { 767 selwakeup(&f->selinfo); 768 f->flag_isselect = 0; 769 } 770 /* check if a thread wants SIGIO */ 771 if (f->async_p != NULL) { 772 PROC_LOCK(f->async_p); 773 psignal(f->async_p, SIGIO); 774 PROC_UNLOCK(f->async_p); 775 f->async_p = NULL; 776 } 777 /* remove FWRITE and FREAD flags */ 778 fflags &= ~(FWRITE | FREAD); 779 780 /* flush written data, if any */ 781 if ((f->fifo_index & 1) == USB_FIFO_TX) { 782 783 if (!f->flag_iserror) { 784 785 /* set flushing flag */ 786 f->flag_flushing = 1; 787 788 /* get the last packet in */ 789 if (f->flag_have_fragment) { 790 struct usb_mbuf *m; 791 f->flag_have_fragment = 0; 792 USB_IF_DEQUEUE(&f->free_q, m); 793 if (m) { 794 USB_IF_ENQUEUE(&f->used_q, m); 795 } 796 } 797 798 /* start write transfer, if not already started */ 799 (f->methods->f_start_write) (f); 800 801 /* check if flushed already */ 802 while (f->flag_flushing && 803 (!f->flag_iserror)) { 804 /* wait until all data has been written */ 805 f->flag_sleeping = 1; 806 err = cv_wait_sig(&f->cv_io, f->priv_mtx); 807 if (err) { 808 DPRINTF("signal received\n"); 809 break; 810 } 811 } 812 } 813 fflags |= FWRITE; 814 815 /* stop write transfer, if not already stopped */ 816 (f->methods->f_stop_write) (f); 817 } else { 818 fflags |= FREAD; 819 820 /* stop write transfer, if not already stopped */ 821 (f->methods->f_stop_read) (f); 822 } 823 824 /* check if we are sleeping */ 825 if (f->flag_sleeping) { 826 DPRINTFN(2, "Sleeping at close!\n"); 827 } 828 mtx_unlock(f->priv_mtx); 829 830 /* call close method */ 831 (f->methods->f_close) (f, fflags); 832 833 DPRINTF("closed\n"); 834 } 835 836 /*------------------------------------------------------------------------* 837 * usb_open - cdev callback 838 *------------------------------------------------------------------------*/ 839 static int 840 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td) 841 { 842 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1; 843 struct usb_cdev_refdata refs; 844 struct usb_cdev_privdata *cpd; 845 int err, ep; 846 847 DPRINTFN(2, "%s fflags=0x%08x\n", dev->si_name, fflags); 848 849 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags")); 850 if (((fflags & FREAD) && !(pd->mode & FREAD)) || 851 ((fflags & FWRITE) && !(pd->mode & FWRITE))) { 852 DPRINTFN(2, "access mode not supported\n"); 853 return (EPERM); 854 } 855 856 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO); 857 ep = cpd->ep_addr = pd->ep_addr; 858 859 usb_loc_fill(pd, cpd); 860 err = usb_ref_device(cpd, &refs, 1); 861 if (err) { 862 DPRINTFN(2, "cannot ref device\n"); 863 free(cpd, M_USBDEV); 864 return (ENXIO); 865 } 866 cpd->fflags = fflags; /* access mode for open lifetime */ 867 868 /* create FIFOs, if any */ 869 err = usb_fifo_create(cpd, &refs); 870 /* check for error */ 871 if (err) { 872 DPRINTFN(2, "cannot create fifo\n"); 873 usb_unref_device(cpd, &refs); 874 free(cpd, M_USBDEV); 875 return (err); 876 } 877 if (fflags & FREAD) { 878 err = usb_fifo_open(cpd, refs.rxfifo, fflags); 879 if (err) { 880 DPRINTFN(2, "read open failed\n"); 881 usb_unref_device(cpd, &refs); 882 free(cpd, M_USBDEV); 883 return (err); 884 } 885 } 886 if (fflags & FWRITE) { 887 err = usb_fifo_open(cpd, refs.txfifo, fflags); 888 if (err) { 889 DPRINTFN(2, "write open failed\n"); 890 if (fflags & FREAD) { 891 usb_fifo_close(refs.rxfifo, fflags); 892 } 893 usb_unref_device(cpd, &refs); 894 free(cpd, M_USBDEV); 895 return (err); 896 } 897 } 898 usb_unref_device(cpd, &refs); 899 devfs_set_cdevpriv(cpd, usb_close); 900 901 return (0); 902 } 903 904 /*------------------------------------------------------------------------* 905 * usb_close - cdev callback 906 *------------------------------------------------------------------------*/ 907 static void 908 usb_close(void *arg) 909 { 910 struct usb_cdev_refdata refs; 911 struct usb_cdev_privdata *cpd = arg; 912 int err; 913 914 DPRINTFN(2, "cpd=%p\n", cpd); 915 916 err = usb_ref_device(cpd, &refs, 1); 917 if (err) { 918 free(cpd, M_USBDEV); 919 return; 920 } 921 if (cpd->fflags & FREAD) { 922 usb_fifo_close(refs.rxfifo, cpd->fflags); 923 } 924 if (cpd->fflags & FWRITE) { 925 usb_fifo_close(refs.txfifo, cpd->fflags); 926 } 927 928 usb_unref_device(cpd, &refs); 929 free(cpd, M_USBDEV); 930 return; 931 } 932 933 static void 934 usb_dev_init(void *arg) 935 { 936 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF); 937 sx_init(&usb_sym_lock, "USB sym mutex"); 938 TAILQ_INIT(&usb_sym_head); 939 940 /* check the UGEN methods */ 941 usb_fifo_check_methods(&usb_ugen_methods); 942 } 943 944 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL); 945 946 static void 947 usb_dev_init_post(void *arg) 948 { 949 /* 950 * Create /dev/usb - this is needed for usbconfig(8), which 951 * needs a well-known device name to access. 952 */ 953 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR, 954 0644, USB_DEVICE_NAME); 955 if (usb_dev == NULL) { 956 DPRINTFN(0, "Could not create usb bus device!\n"); 957 } 958 } 959 960 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL); 961 962 static void 963 usb_dev_uninit(void *arg) 964 { 965 if (usb_dev != NULL) { 966 destroy_dev(usb_dev); 967 usb_dev = NULL; 968 969 } 970 mtx_destroy(&usb_ref_lock); 971 sx_destroy(&usb_sym_lock); 972 } 973 974 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL); 975 976 static int 977 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr, 978 struct thread *td) 979 { 980 int error = 0; 981 982 switch (cmd) { 983 case FIODTYPE: 984 *(int *)addr = 0; /* character device */ 985 break; 986 987 case FIONBIO: 988 /* handled by upper FS layer */ 989 break; 990 991 case FIOASYNC: 992 if (*(int *)addr) { 993 if (f->async_p != NULL) { 994 error = EBUSY; 995 break; 996 } 997 f->async_p = USB_TD_GET_PROC(td); 998 } else { 999 f->async_p = NULL; 1000 } 1001 break; 1002 1003 /* XXX this is not the most general solution */ 1004 case TIOCSPGRP: 1005 if (f->async_p == NULL) { 1006 error = EINVAL; 1007 break; 1008 } 1009 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) { 1010 error = EPERM; 1011 break; 1012 } 1013 break; 1014 default: 1015 return (ENOIOCTL); 1016 } 1017 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error); 1018 return (error); 1019 } 1020 1021 /*------------------------------------------------------------------------* 1022 * usb_ioctl - cdev callback 1023 *------------------------------------------------------------------------*/ 1024 static int 1025 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td) 1026 { 1027 struct usb_cdev_refdata refs; 1028 struct usb_cdev_privdata* cpd; 1029 struct usb_fifo *f; 1030 int fflags; 1031 int err; 1032 1033 DPRINTFN(2, "cmd=0x%lx\n", cmd); 1034 1035 err = devfs_get_cdevpriv((void **)&cpd); 1036 if (err != 0) 1037 return (err); 1038 1039 /* 1040 * Performance optimisation: We try to check for IOCTL's that 1041 * don't need the USB reference first. Then we grab the USB 1042 * reference if we need it! 1043 */ 1044 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1045 if (err) { 1046 return (ENXIO); 1047 } 1048 fflags = cpd->fflags; 1049 1050 f = NULL; /* set default value */ 1051 err = ENOIOCTL; /* set default value */ 1052 1053 if (fflags & FWRITE) { 1054 f = refs.txfifo; 1055 err = usb_ioctl_f_sub(f, cmd, addr, td); 1056 } 1057 if (fflags & FREAD) { 1058 f = refs.rxfifo; 1059 err = usb_ioctl_f_sub(f, cmd, addr, td); 1060 } 1061 KASSERT(f != NULL, ("fifo not found")); 1062 if (err == ENOIOCTL) { 1063 err = (f->methods->f_ioctl) (f, cmd, addr, fflags); 1064 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err); 1065 if (err == ENOIOCTL) { 1066 if (usb_usb_ref_device(cpd, &refs)) { 1067 err = ENXIO; 1068 goto done; 1069 } 1070 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags); 1071 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err); 1072 } 1073 } 1074 if (err == ENOIOCTL) { 1075 err = ENOTTY; 1076 } 1077 done: 1078 usb_unref_device(cpd, &refs); 1079 return (err); 1080 } 1081 1082 /* ARGSUSED */ 1083 static int 1084 usb_poll(struct cdev* dev, int events, struct thread* td) 1085 { 1086 struct usb_cdev_refdata refs; 1087 struct usb_cdev_privdata* cpd; 1088 struct usb_fifo *f; 1089 struct usb_mbuf *m; 1090 int fflags, revents; 1091 1092 if (devfs_get_cdevpriv((void **)&cpd) != 0 || 1093 usb_ref_device(cpd, &refs, 0) != 0) 1094 return (events & 1095 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 1096 1097 fflags = cpd->fflags; 1098 1099 /* Figure out who needs service */ 1100 revents = 0; 1101 if ((events & (POLLOUT | POLLWRNORM)) && 1102 (fflags & FWRITE)) { 1103 1104 f = refs.txfifo; 1105 1106 mtx_lock(f->priv_mtx); 1107 1108 if (!refs.is_usbfs) { 1109 if (f->flag_iserror) { 1110 /* we got an error */ 1111 m = (void *)1; 1112 } else { 1113 if (f->queue_data == NULL) { 1114 /* 1115 * start write transfer, if not 1116 * already started 1117 */ 1118 (f->methods->f_start_write) (f); 1119 } 1120 /* check if any packets are available */ 1121 USB_IF_POLL(&f->free_q, m); 1122 } 1123 } else { 1124 if (f->flag_iscomplete) { 1125 m = (void *)1; 1126 } else { 1127 m = NULL; 1128 } 1129 } 1130 1131 if (m) { 1132 revents |= events & (POLLOUT | POLLWRNORM); 1133 } else { 1134 f->flag_isselect = 1; 1135 selrecord(td, &f->selinfo); 1136 } 1137 1138 mtx_unlock(f->priv_mtx); 1139 } 1140 if ((events & (POLLIN | POLLRDNORM)) && 1141 (fflags & FREAD)) { 1142 1143 f = refs.rxfifo; 1144 1145 mtx_lock(f->priv_mtx); 1146 1147 if (!refs.is_usbfs) { 1148 if (f->flag_iserror) { 1149 /* we have and error */ 1150 m = (void *)1; 1151 } else { 1152 if (f->queue_data == NULL) { 1153 /* 1154 * start read transfer, if not 1155 * already started 1156 */ 1157 (f->methods->f_start_read) (f); 1158 } 1159 /* check if any packets are available */ 1160 USB_IF_POLL(&f->used_q, m); 1161 } 1162 } else { 1163 if (f->flag_iscomplete) { 1164 m = (void *)1; 1165 } else { 1166 m = NULL; 1167 } 1168 } 1169 1170 if (m) { 1171 revents |= events & (POLLIN | POLLRDNORM); 1172 } else { 1173 f->flag_isselect = 1; 1174 selrecord(td, &f->selinfo); 1175 1176 if (!refs.is_usbfs) { 1177 /* start reading data */ 1178 (f->methods->f_start_read) (f); 1179 } 1180 } 1181 1182 mtx_unlock(f->priv_mtx); 1183 } 1184 usb_unref_device(cpd, &refs); 1185 return (revents); 1186 } 1187 1188 static int 1189 usb_read(struct cdev *dev, struct uio *uio, int ioflag) 1190 { 1191 struct usb_cdev_refdata refs; 1192 struct usb_cdev_privdata* cpd; 1193 struct usb_fifo *f; 1194 struct usb_mbuf *m; 1195 int fflags; 1196 int resid; 1197 int io_len; 1198 int err; 1199 uint8_t tr_data = 0; 1200 1201 err = devfs_get_cdevpriv((void **)&cpd); 1202 if (err != 0) 1203 return (err); 1204 1205 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1206 if (err) { 1207 return (ENXIO); 1208 } 1209 fflags = cpd->fflags; 1210 1211 f = refs.rxfifo; 1212 if (f == NULL) { 1213 /* should not happen */ 1214 usb_unref_device(cpd, &refs); 1215 return (EPERM); 1216 } 1217 1218 resid = uio->uio_resid; 1219 1220 mtx_lock(f->priv_mtx); 1221 1222 /* check for permanent read error */ 1223 if (f->flag_iserror) { 1224 err = EIO; 1225 goto done; 1226 } 1227 /* check if USB-FS interface is active */ 1228 if (refs.is_usbfs) { 1229 /* 1230 * The queue is used for events that should be 1231 * retrieved using the "USB_FS_COMPLETE" ioctl. 1232 */ 1233 err = EINVAL; 1234 goto done; 1235 } 1236 while (uio->uio_resid > 0) { 1237 1238 USB_IF_DEQUEUE(&f->used_q, m); 1239 1240 if (m == NULL) { 1241 1242 /* start read transfer, if not already started */ 1243 1244 (f->methods->f_start_read) (f); 1245 1246 if (ioflag & IO_NDELAY) { 1247 if (tr_data) { 1248 /* return length before error */ 1249 break; 1250 } 1251 err = EWOULDBLOCK; 1252 break; 1253 } 1254 DPRINTF("sleeping\n"); 1255 1256 err = usb_fifo_wait(f); 1257 if (err) { 1258 break; 1259 } 1260 continue; 1261 } 1262 if (f->methods->f_filter_read) { 1263 /* 1264 * Sometimes it is convenient to process data at the 1265 * expense of a userland process instead of a kernel 1266 * process. 1267 */ 1268 (f->methods->f_filter_read) (f, m); 1269 } 1270 tr_data = 1; 1271 1272 io_len = MIN(m->cur_data_len, uio->uio_resid); 1273 1274 DPRINTFN(2, "transfer %d bytes from %p\n", 1275 io_len, m->cur_data_ptr); 1276 1277 err = usb_fifo_uiomove(f, 1278 m->cur_data_ptr, io_len, uio); 1279 1280 m->cur_data_len -= io_len; 1281 m->cur_data_ptr += io_len; 1282 1283 if (m->cur_data_len == 0) { 1284 1285 uint8_t last_packet; 1286 1287 last_packet = m->last_packet; 1288 1289 USB_IF_ENQUEUE(&f->free_q, m); 1290 1291 if (last_packet) { 1292 /* keep framing */ 1293 break; 1294 } 1295 } else { 1296 USB_IF_PREPEND(&f->used_q, m); 1297 } 1298 1299 if (err) { 1300 break; 1301 } 1302 } 1303 done: 1304 mtx_unlock(f->priv_mtx); 1305 1306 usb_unref_device(cpd, &refs); 1307 1308 return (err); 1309 } 1310 1311 static int 1312 usb_write(struct cdev *dev, struct uio *uio, int ioflag) 1313 { 1314 struct usb_cdev_refdata refs; 1315 struct usb_cdev_privdata* cpd; 1316 struct usb_fifo *f; 1317 struct usb_mbuf *m; 1318 uint8_t *pdata; 1319 int fflags; 1320 int resid; 1321 int io_len; 1322 int err; 1323 uint8_t tr_data = 0; 1324 1325 DPRINTFN(2, "\n"); 1326 1327 err = devfs_get_cdevpriv((void **)&cpd); 1328 if (err != 0) 1329 return (err); 1330 1331 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1332 if (err) { 1333 return (ENXIO); 1334 } 1335 fflags = cpd->fflags; 1336 1337 f = refs.txfifo; 1338 if (f == NULL) { 1339 /* should not happen */ 1340 usb_unref_device(cpd, &refs); 1341 return (EPERM); 1342 } 1343 resid = uio->uio_resid; 1344 1345 mtx_lock(f->priv_mtx); 1346 1347 /* check for permanent write error */ 1348 if (f->flag_iserror) { 1349 err = EIO; 1350 goto done; 1351 } 1352 /* check if USB-FS interface is active */ 1353 if (refs.is_usbfs) { 1354 /* 1355 * The queue is used for events that should be 1356 * retrieved using the "USB_FS_COMPLETE" ioctl. 1357 */ 1358 err = EINVAL; 1359 goto done; 1360 } 1361 if (f->queue_data == NULL) { 1362 /* start write transfer, if not already started */ 1363 (f->methods->f_start_write) (f); 1364 } 1365 /* we allow writing zero length data */ 1366 do { 1367 USB_IF_DEQUEUE(&f->free_q, m); 1368 1369 if (m == NULL) { 1370 1371 if (ioflag & IO_NDELAY) { 1372 if (tr_data) { 1373 /* return length before error */ 1374 break; 1375 } 1376 err = EWOULDBLOCK; 1377 break; 1378 } 1379 DPRINTF("sleeping\n"); 1380 1381 err = usb_fifo_wait(f); 1382 if (err) { 1383 break; 1384 } 1385 continue; 1386 } 1387 tr_data = 1; 1388 1389 if (f->flag_have_fragment == 0) { 1390 USB_MBUF_RESET(m); 1391 io_len = m->cur_data_len; 1392 pdata = m->cur_data_ptr; 1393 if (io_len > uio->uio_resid) 1394 io_len = uio->uio_resid; 1395 m->cur_data_len = io_len; 1396 } else { 1397 io_len = m->max_data_len - m->cur_data_len; 1398 pdata = m->cur_data_ptr + m->cur_data_len; 1399 if (io_len > uio->uio_resid) 1400 io_len = uio->uio_resid; 1401 m->cur_data_len += io_len; 1402 } 1403 1404 DPRINTFN(2, "transfer %d bytes to %p\n", 1405 io_len, pdata); 1406 1407 err = usb_fifo_uiomove(f, pdata, io_len, uio); 1408 1409 if (err) { 1410 f->flag_have_fragment = 0; 1411 USB_IF_ENQUEUE(&f->free_q, m); 1412 break; 1413 } 1414 1415 /* check if the buffer is ready to be transmitted */ 1416 1417 if ((f->flag_write_defrag == 0) || 1418 (m->cur_data_len == m->max_data_len)) { 1419 f->flag_have_fragment = 0; 1420 1421 /* 1422 * Check for write filter: 1423 * 1424 * Sometimes it is convenient to process data 1425 * at the expense of a userland process 1426 * instead of a kernel process. 1427 */ 1428 if (f->methods->f_filter_write) { 1429 (f->methods->f_filter_write) (f, m); 1430 } 1431 1432 /* Put USB mbuf in the used queue */ 1433 USB_IF_ENQUEUE(&f->used_q, m); 1434 1435 /* Start writing data, if not already started */ 1436 (f->methods->f_start_write) (f); 1437 } else { 1438 /* Wait for more data or close */ 1439 f->flag_have_fragment = 1; 1440 USB_IF_PREPEND(&f->free_q, m); 1441 } 1442 1443 } while (uio->uio_resid > 0); 1444 done: 1445 mtx_unlock(f->priv_mtx); 1446 1447 usb_unref_device(cpd, &refs); 1448 1449 return (err); 1450 } 1451 1452 int 1453 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 1454 struct thread *td) 1455 { 1456 union { 1457 struct usb_read_dir *urd; 1458 void* data; 1459 } u; 1460 int err = ENOTTY; 1461 1462 u.data = data; 1463 switch (cmd) { 1464 case USB_READ_DIR: 1465 err = usb_read_symlink(u.urd->urd_data, 1466 u.urd->urd_startentry, u.urd->urd_maxlen); 1467 break; 1468 case USB_DEV_QUIRK_GET: 1469 case USB_QUIRK_NAME_GET: 1470 case USB_DEV_QUIRK_ADD: 1471 case USB_DEV_QUIRK_REMOVE: 1472 err = usb_quirk_ioctl_p(cmd, data, fflag, td); 1473 break; 1474 case USB_GET_TEMPLATE: 1475 *(int *)data = usb_template; 1476 break; 1477 case USB_SET_TEMPLATE: 1478 err = priv_check(curthread, PRIV_DRIVER); 1479 if (err) 1480 break; 1481 usb_template = *(int *)data; 1482 break; 1483 } 1484 return (err); 1485 } 1486 1487 static int 1488 usb_fifo_uiomove(struct usb_fifo *f, void *cp, 1489 int n, struct uio *uio) 1490 { 1491 int error; 1492 1493 mtx_unlock(f->priv_mtx); 1494 1495 /* 1496 * "uiomove()" can sleep so one needs to make a wrapper, 1497 * exiting the mutex and checking things: 1498 */ 1499 error = uiomove(cp, n, uio); 1500 1501 mtx_lock(f->priv_mtx); 1502 1503 return (error); 1504 } 1505 1506 int 1507 usb_fifo_wait(struct usb_fifo *f) 1508 { 1509 int err; 1510 1511 mtx_assert(f->priv_mtx, MA_OWNED); 1512 1513 if (f->flag_iserror) { 1514 /* we are gone */ 1515 return (EIO); 1516 } 1517 f->flag_sleeping = 1; 1518 1519 err = cv_wait_sig(&f->cv_io, f->priv_mtx); 1520 1521 if (f->flag_iserror) { 1522 /* we are gone */ 1523 err = EIO; 1524 } 1525 return (err); 1526 } 1527 1528 void 1529 usb_fifo_signal(struct usb_fifo *f) 1530 { 1531 if (f->flag_sleeping) { 1532 f->flag_sleeping = 0; 1533 cv_broadcast(&f->cv_io); 1534 } 1535 } 1536 1537 void 1538 usb_fifo_wakeup(struct usb_fifo *f) 1539 { 1540 usb_fifo_signal(f); 1541 1542 if (f->flag_isselect) { 1543 selwakeup(&f->selinfo); 1544 f->flag_isselect = 0; 1545 } 1546 if (f->async_p != NULL) { 1547 PROC_LOCK(f->async_p); 1548 psignal(f->async_p, SIGIO); 1549 PROC_UNLOCK(f->async_p); 1550 } 1551 } 1552 1553 static int 1554 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags) 1555 { 1556 return (0); 1557 } 1558 1559 static void 1560 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags) 1561 { 1562 return; 1563 } 1564 1565 static int 1566 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 1567 { 1568 return (ENOIOCTL); 1569 } 1570 1571 static void 1572 usb_fifo_dummy_cmd(struct usb_fifo *fifo) 1573 { 1574 fifo->flag_flushing = 0; /* not flushing */ 1575 } 1576 1577 static void 1578 usb_fifo_check_methods(struct usb_fifo_methods *pm) 1579 { 1580 /* check that all callback functions are OK */ 1581 1582 if (pm->f_open == NULL) 1583 pm->f_open = &usb_fifo_dummy_open; 1584 1585 if (pm->f_close == NULL) 1586 pm->f_close = &usb_fifo_dummy_close; 1587 1588 if (pm->f_ioctl == NULL) 1589 pm->f_ioctl = &usb_fifo_dummy_ioctl; 1590 1591 if (pm->f_ioctl_post == NULL) 1592 pm->f_ioctl_post = &usb_fifo_dummy_ioctl; 1593 1594 if (pm->f_start_read == NULL) 1595 pm->f_start_read = &usb_fifo_dummy_cmd; 1596 1597 if (pm->f_stop_read == NULL) 1598 pm->f_stop_read = &usb_fifo_dummy_cmd; 1599 1600 if (pm->f_start_write == NULL) 1601 pm->f_start_write = &usb_fifo_dummy_cmd; 1602 1603 if (pm->f_stop_write == NULL) 1604 pm->f_stop_write = &usb_fifo_dummy_cmd; 1605 } 1606 1607 /*------------------------------------------------------------------------* 1608 * usb_fifo_attach 1609 * 1610 * The following function will create a duplex FIFO. 1611 * 1612 * Return values: 1613 * 0: Success. 1614 * Else: Failure. 1615 *------------------------------------------------------------------------*/ 1616 int 1617 usb_fifo_attach(struct usb_device *udev, void *priv_sc, 1618 struct mtx *priv_mtx, struct usb_fifo_methods *pm, 1619 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit, 1620 uint8_t iface_index, uid_t uid, gid_t gid, int mode) 1621 { 1622 struct usb_fifo *f_tx; 1623 struct usb_fifo *f_rx; 1624 char devname[32]; 1625 uint8_t n; 1626 struct usb_fs_privdata* pd; 1627 1628 f_sc->fp[USB_FIFO_TX] = NULL; 1629 f_sc->fp[USB_FIFO_RX] = NULL; 1630 1631 if (pm == NULL) 1632 return (EINVAL); 1633 1634 /* check the methods */ 1635 usb_fifo_check_methods(pm); 1636 1637 if (priv_mtx == NULL) 1638 priv_mtx = &Giant; 1639 1640 /* search for a free FIFO slot */ 1641 for (n = 0;; n += 2) { 1642 1643 if (n == USB_FIFO_MAX) { 1644 /* end of FIFOs reached */ 1645 return (ENOMEM); 1646 } 1647 /* Check for TX FIFO */ 1648 if (udev->fifo[n + USB_FIFO_TX] != NULL) { 1649 continue; 1650 } 1651 /* Check for RX FIFO */ 1652 if (udev->fifo[n + USB_FIFO_RX] != NULL) { 1653 continue; 1654 } 1655 break; 1656 } 1657 1658 f_tx = usb_fifo_alloc(); 1659 f_rx = usb_fifo_alloc(); 1660 1661 if ((f_tx == NULL) || (f_rx == NULL)) { 1662 usb_fifo_free(f_tx); 1663 usb_fifo_free(f_rx); 1664 return (ENOMEM); 1665 } 1666 /* initialise FIFO structures */ 1667 1668 f_tx->fifo_index = n + USB_FIFO_TX; 1669 f_tx->dev_ep_index = -1; 1670 f_tx->priv_mtx = priv_mtx; 1671 f_tx->priv_sc0 = priv_sc; 1672 f_tx->methods = pm; 1673 f_tx->iface_index = iface_index; 1674 f_tx->udev = udev; 1675 1676 f_rx->fifo_index = n + USB_FIFO_RX; 1677 f_rx->dev_ep_index = -1; 1678 f_rx->priv_mtx = priv_mtx; 1679 f_rx->priv_sc0 = priv_sc; 1680 f_rx->methods = pm; 1681 f_rx->iface_index = iface_index; 1682 f_rx->udev = udev; 1683 1684 f_sc->fp[USB_FIFO_TX] = f_tx; 1685 f_sc->fp[USB_FIFO_RX] = f_rx; 1686 1687 mtx_lock(&usb_ref_lock); 1688 udev->fifo[f_tx->fifo_index] = f_tx; 1689 udev->fifo[f_rx->fifo_index] = f_rx; 1690 mtx_unlock(&usb_ref_lock); 1691 1692 for (n = 0; n != 4; n++) { 1693 1694 if (pm->basename[n] == NULL) { 1695 continue; 1696 } 1697 if (subunit == 0xFFFF) { 1698 if (snprintf(devname, sizeof(devname), 1699 "%s%u%s", pm->basename[n], 1700 unit, pm->postfix[n] ? 1701 pm->postfix[n] : "")) { 1702 /* ignore */ 1703 } 1704 } else { 1705 if (snprintf(devname, sizeof(devname), 1706 "%s%u.%u%s", pm->basename[n], 1707 unit, subunit, pm->postfix[n] ? 1708 pm->postfix[n] : "")) { 1709 /* ignore */ 1710 } 1711 } 1712 1713 /* 1714 * Distribute the symbolic links into two FIFO structures: 1715 */ 1716 if (n & 1) { 1717 f_rx->symlink[n / 2] = 1718 usb_alloc_symlink(devname); 1719 } else { 1720 f_tx->symlink[n / 2] = 1721 usb_alloc_symlink(devname); 1722 } 1723 1724 /* 1725 * Initialize device private data - this is used to find the 1726 * actual USB device itself. 1727 */ 1728 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO); 1729 pd->bus_index = device_get_unit(udev->bus->bdev); 1730 pd->dev_index = udev->device_index; 1731 pd->ep_addr = -1; /* not an endpoint */ 1732 pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index; 1733 pd->mode = FREAD|FWRITE; 1734 1735 /* Now, create the device itself */ 1736 f_sc->dev = make_dev(&usb_devsw, 0, uid, gid, mode, 1737 devname); 1738 /* XXX setting si_drv1 and creating the device is not atomic! */ 1739 f_sc->dev->si_drv1 = pd; 1740 } 1741 1742 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx); 1743 return (0); 1744 } 1745 1746 /*------------------------------------------------------------------------* 1747 * usb_fifo_alloc_buffer 1748 * 1749 * Return values: 1750 * 0: Success 1751 * Else failure 1752 *------------------------------------------------------------------------*/ 1753 int 1754 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize, 1755 uint16_t nbuf) 1756 { 1757 usb_fifo_free_buffer(f); 1758 1759 /* allocate an endpoint */ 1760 f->free_q.ifq_maxlen = nbuf; 1761 f->used_q.ifq_maxlen = nbuf; 1762 1763 f->queue_data = usb_alloc_mbufs( 1764 M_USBDEV, &f->free_q, bufsize, nbuf); 1765 1766 if ((f->queue_data == NULL) && bufsize && nbuf) { 1767 return (ENOMEM); 1768 } 1769 return (0); /* success */ 1770 } 1771 1772 /*------------------------------------------------------------------------* 1773 * usb_fifo_free_buffer 1774 * 1775 * This function will free the buffers associated with a FIFO. This 1776 * function can be called multiple times in a row. 1777 *------------------------------------------------------------------------*/ 1778 void 1779 usb_fifo_free_buffer(struct usb_fifo *f) 1780 { 1781 if (f->queue_data) { 1782 /* free old buffer */ 1783 free(f->queue_data, M_USBDEV); 1784 f->queue_data = NULL; 1785 } 1786 /* reset queues */ 1787 1788 bzero(&f->free_q, sizeof(f->free_q)); 1789 bzero(&f->used_q, sizeof(f->used_q)); 1790 } 1791 1792 static void 1793 usb_fifo_cleanup(void* ptr) 1794 { 1795 free(ptr, M_USBDEV); 1796 } 1797 1798 void 1799 usb_fifo_detach(struct usb_fifo_sc *f_sc) 1800 { 1801 if (f_sc == NULL) { 1802 return; 1803 } 1804 usb_fifo_free(f_sc->fp[USB_FIFO_TX]); 1805 usb_fifo_free(f_sc->fp[USB_FIFO_RX]); 1806 1807 f_sc->fp[USB_FIFO_TX] = NULL; 1808 f_sc->fp[USB_FIFO_RX] = NULL; 1809 1810 if (f_sc->dev != NULL) { 1811 destroy_dev_sched_cb(f_sc->dev, 1812 usb_fifo_cleanup, f_sc->dev->si_drv1); 1813 f_sc->dev = NULL; 1814 } 1815 1816 DPRINTFN(2, "detached %p\n", f_sc); 1817 } 1818 1819 usb_size_t 1820 usb_fifo_put_bytes_max(struct usb_fifo *f) 1821 { 1822 struct usb_mbuf *m; 1823 usb_size_t len; 1824 1825 USB_IF_POLL(&f->free_q, m); 1826 1827 if (m) { 1828 len = m->max_data_len; 1829 } else { 1830 len = 0; 1831 } 1832 return (len); 1833 } 1834 1835 /*------------------------------------------------------------------------* 1836 * usb_fifo_put_data 1837 * 1838 * what: 1839 * 0 - normal operation 1840 * 1 - set last packet flag to enforce framing 1841 *------------------------------------------------------------------------*/ 1842 void 1843 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc, 1844 usb_frlength_t offset, usb_frlength_t len, uint8_t what) 1845 { 1846 struct usb_mbuf *m; 1847 usb_frlength_t io_len; 1848 1849 while (len || (what == 1)) { 1850 1851 USB_IF_DEQUEUE(&f->free_q, m); 1852 1853 if (m) { 1854 USB_MBUF_RESET(m); 1855 1856 io_len = MIN(len, m->cur_data_len); 1857 1858 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len); 1859 1860 m->cur_data_len = io_len; 1861 offset += io_len; 1862 len -= io_len; 1863 1864 if ((len == 0) && (what == 1)) { 1865 m->last_packet = 1; 1866 } 1867 USB_IF_ENQUEUE(&f->used_q, m); 1868 1869 usb_fifo_wakeup(f); 1870 1871 if ((len == 0) || (what == 1)) { 1872 break; 1873 } 1874 } else { 1875 break; 1876 } 1877 } 1878 } 1879 1880 void 1881 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr, 1882 usb_size_t len, uint8_t what) 1883 { 1884 struct usb_mbuf *m; 1885 usb_size_t io_len; 1886 1887 while (len || (what == 1)) { 1888 1889 USB_IF_DEQUEUE(&f->free_q, m); 1890 1891 if (m) { 1892 USB_MBUF_RESET(m); 1893 1894 io_len = MIN(len, m->cur_data_len); 1895 1896 bcopy(ptr, m->cur_data_ptr, io_len); 1897 1898 m->cur_data_len = io_len; 1899 ptr = USB_ADD_BYTES(ptr, io_len); 1900 len -= io_len; 1901 1902 if ((len == 0) && (what == 1)) { 1903 m->last_packet = 1; 1904 } 1905 USB_IF_ENQUEUE(&f->used_q, m); 1906 1907 usb_fifo_wakeup(f); 1908 1909 if ((len == 0) || (what == 1)) { 1910 break; 1911 } 1912 } else { 1913 break; 1914 } 1915 } 1916 } 1917 1918 uint8_t 1919 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len) 1920 { 1921 struct usb_mbuf *m; 1922 1923 USB_IF_DEQUEUE(&f->free_q, m); 1924 1925 if (m) { 1926 m->cur_data_len = len; 1927 m->cur_data_ptr = ptr; 1928 USB_IF_ENQUEUE(&f->used_q, m); 1929 usb_fifo_wakeup(f); 1930 return (1); 1931 } 1932 return (0); 1933 } 1934 1935 void 1936 usb_fifo_put_data_error(struct usb_fifo *f) 1937 { 1938 f->flag_iserror = 1; 1939 usb_fifo_wakeup(f); 1940 } 1941 1942 /*------------------------------------------------------------------------* 1943 * usb_fifo_get_data 1944 * 1945 * what: 1946 * 0 - normal operation 1947 * 1 - only get one "usb_mbuf" 1948 * 1949 * returns: 1950 * 0 - no more data 1951 * 1 - data in buffer 1952 *------------------------------------------------------------------------*/ 1953 uint8_t 1954 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc, 1955 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen, 1956 uint8_t what) 1957 { 1958 struct usb_mbuf *m; 1959 usb_frlength_t io_len; 1960 uint8_t tr_data = 0; 1961 1962 actlen[0] = 0; 1963 1964 while (1) { 1965 1966 USB_IF_DEQUEUE(&f->used_q, m); 1967 1968 if (m) { 1969 1970 tr_data = 1; 1971 1972 io_len = MIN(len, m->cur_data_len); 1973 1974 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len); 1975 1976 len -= io_len; 1977 offset += io_len; 1978 actlen[0] += io_len; 1979 m->cur_data_ptr += io_len; 1980 m->cur_data_len -= io_len; 1981 1982 if ((m->cur_data_len == 0) || (what == 1)) { 1983 USB_IF_ENQUEUE(&f->free_q, m); 1984 1985 usb_fifo_wakeup(f); 1986 1987 if (what == 1) { 1988 break; 1989 } 1990 } else { 1991 USB_IF_PREPEND(&f->used_q, m); 1992 } 1993 } else { 1994 1995 if (tr_data) { 1996 /* wait for data to be written out */ 1997 break; 1998 } 1999 if (f->flag_flushing) { 2000 /* check if we should send a short packet */ 2001 if (f->flag_short != 0) { 2002 f->flag_short = 0; 2003 tr_data = 1; 2004 break; 2005 } 2006 /* flushing complete */ 2007 f->flag_flushing = 0; 2008 usb_fifo_wakeup(f); 2009 } 2010 break; 2011 } 2012 if (len == 0) { 2013 break; 2014 } 2015 } 2016 return (tr_data); 2017 } 2018 2019 uint8_t 2020 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr, 2021 usb_size_t len, usb_size_t *actlen, uint8_t what) 2022 { 2023 struct usb_mbuf *m; 2024 usb_size_t io_len; 2025 uint8_t tr_data = 0; 2026 2027 actlen[0] = 0; 2028 2029 while (1) { 2030 2031 USB_IF_DEQUEUE(&f->used_q, m); 2032 2033 if (m) { 2034 2035 tr_data = 1; 2036 2037 io_len = MIN(len, m->cur_data_len); 2038 2039 bcopy(m->cur_data_ptr, ptr, io_len); 2040 2041 len -= io_len; 2042 ptr = USB_ADD_BYTES(ptr, io_len); 2043 actlen[0] += io_len; 2044 m->cur_data_ptr += io_len; 2045 m->cur_data_len -= io_len; 2046 2047 if ((m->cur_data_len == 0) || (what == 1)) { 2048 USB_IF_ENQUEUE(&f->free_q, m); 2049 2050 usb_fifo_wakeup(f); 2051 2052 if (what == 1) { 2053 break; 2054 } 2055 } else { 2056 USB_IF_PREPEND(&f->used_q, m); 2057 } 2058 } else { 2059 2060 if (tr_data) { 2061 /* wait for data to be written out */ 2062 break; 2063 } 2064 if (f->flag_flushing) { 2065 /* check if we should send a short packet */ 2066 if (f->flag_short != 0) { 2067 f->flag_short = 0; 2068 tr_data = 1; 2069 break; 2070 } 2071 /* flushing complete */ 2072 f->flag_flushing = 0; 2073 usb_fifo_wakeup(f); 2074 } 2075 break; 2076 } 2077 if (len == 0) { 2078 break; 2079 } 2080 } 2081 return (tr_data); 2082 } 2083 2084 uint8_t 2085 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen) 2086 { 2087 struct usb_mbuf *m; 2088 2089 USB_IF_POLL(&f->used_q, m); 2090 2091 if (m) { 2092 *plen = m->cur_data_len; 2093 *pptr = m->cur_data_ptr; 2094 2095 return (1); 2096 } 2097 return (0); 2098 } 2099 2100 void 2101 usb_fifo_get_data_error(struct usb_fifo *f) 2102 { 2103 f->flag_iserror = 1; 2104 usb_fifo_wakeup(f); 2105 } 2106 2107 /*------------------------------------------------------------------------* 2108 * usb_alloc_symlink 2109 * 2110 * Return values: 2111 * NULL: Failure 2112 * Else: Pointer to symlink entry 2113 *------------------------------------------------------------------------*/ 2114 struct usb_symlink * 2115 usb_alloc_symlink(const char *target) 2116 { 2117 struct usb_symlink *ps; 2118 2119 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK); 2120 if (ps == NULL) { 2121 return (ps); 2122 } 2123 /* XXX no longer needed */ 2124 strlcpy(ps->src_path, target, sizeof(ps->src_path)); 2125 ps->src_len = strlen(ps->src_path); 2126 strlcpy(ps->dst_path, target, sizeof(ps->dst_path)); 2127 ps->dst_len = strlen(ps->dst_path); 2128 2129 sx_xlock(&usb_sym_lock); 2130 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry); 2131 sx_unlock(&usb_sym_lock); 2132 return (ps); 2133 } 2134 2135 /*------------------------------------------------------------------------* 2136 * usb_free_symlink 2137 *------------------------------------------------------------------------*/ 2138 void 2139 usb_free_symlink(struct usb_symlink *ps) 2140 { 2141 if (ps == NULL) { 2142 return; 2143 } 2144 sx_xlock(&usb_sym_lock); 2145 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry); 2146 sx_unlock(&usb_sym_lock); 2147 2148 free(ps, M_USBDEV); 2149 } 2150 2151 /*------------------------------------------------------------------------* 2152 * usb_read_symlink 2153 * 2154 * Return value: 2155 * 0: Success 2156 * Else: Failure 2157 *------------------------------------------------------------------------*/ 2158 int 2159 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len) 2160 { 2161 struct usb_symlink *ps; 2162 uint32_t temp; 2163 uint32_t delta = 0; 2164 uint8_t len; 2165 int error = 0; 2166 2167 sx_xlock(&usb_sym_lock); 2168 2169 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) { 2170 2171 /* 2172 * Compute total length of source and destination symlink 2173 * strings pluss one length byte and two NUL bytes: 2174 */ 2175 temp = ps->src_len + ps->dst_len + 3; 2176 2177 if (temp > 255) { 2178 /* 2179 * Skip entry because this length cannot fit 2180 * into one byte: 2181 */ 2182 continue; 2183 } 2184 if (startentry != 0) { 2185 /* decrement read offset */ 2186 startentry--; 2187 continue; 2188 } 2189 if (temp > user_len) { 2190 /* out of buffer space */ 2191 break; 2192 } 2193 len = temp; 2194 2195 /* copy out total length */ 2196 2197 error = copyout(&len, 2198 USB_ADD_BYTES(user_ptr, delta), 1); 2199 if (error) { 2200 break; 2201 } 2202 delta += 1; 2203 2204 /* copy out source string */ 2205 2206 error = copyout(ps->src_path, 2207 USB_ADD_BYTES(user_ptr, delta), ps->src_len); 2208 if (error) { 2209 break; 2210 } 2211 len = 0; 2212 delta += ps->src_len; 2213 error = copyout(&len, 2214 USB_ADD_BYTES(user_ptr, delta), 1); 2215 if (error) { 2216 break; 2217 } 2218 delta += 1; 2219 2220 /* copy out destination string */ 2221 2222 error = copyout(ps->dst_path, 2223 USB_ADD_BYTES(user_ptr, delta), ps->dst_len); 2224 if (error) { 2225 break; 2226 } 2227 len = 0; 2228 delta += ps->dst_len; 2229 error = copyout(&len, 2230 USB_ADD_BYTES(user_ptr, delta), 1); 2231 if (error) { 2232 break; 2233 } 2234 delta += 1; 2235 2236 user_len -= temp; 2237 } 2238 2239 /* a zero length entry indicates the end */ 2240 2241 if ((user_len != 0) && (error == 0)) { 2242 2243 len = 0; 2244 2245 error = copyout(&len, 2246 USB_ADD_BYTES(user_ptr, delta), 1); 2247 } 2248 sx_unlock(&usb_sym_lock); 2249 return (error); 2250 } 2251 2252 void 2253 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff) 2254 { 2255 if (f == NULL) 2256 return; 2257 2258 /* send a Zero Length Packet, ZLP, before close */ 2259 f->flag_short = onoff; 2260 } 2261 2262 void 2263 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff) 2264 { 2265 if (f == NULL) 2266 return; 2267 2268 /* defrag written data */ 2269 f->flag_write_defrag = onoff; 2270 /* reset defrag state */ 2271 f->flag_have_fragment = 0; 2272 } 2273 2274 void * 2275 usb_fifo_softc(struct usb_fifo *f) 2276 { 2277 return (f->priv_sc0); 2278 } 2279 #endif /* USB_HAVE_UGEN */ 2280