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