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 * Note that some ioctl_post handlers would need to run with the 1044 * newbus lock held. It cannot be acquired later because it can 1045 * introduce a LOR, so acquire it here. 1046 */ 1047 newbus_xlock(); 1048 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1049 if (err) { 1050 newbus_xunlock(); 1051 return (ENXIO); 1052 } 1053 fflags = cpd->fflags; 1054 1055 f = NULL; /* set default value */ 1056 err = ENOIOCTL; /* set default value */ 1057 1058 if (fflags & FWRITE) { 1059 f = refs.txfifo; 1060 err = usb_ioctl_f_sub(f, cmd, addr, td); 1061 } 1062 if (fflags & FREAD) { 1063 f = refs.rxfifo; 1064 err = usb_ioctl_f_sub(f, cmd, addr, td); 1065 } 1066 KASSERT(f != NULL, ("fifo not found")); 1067 if (err == ENOIOCTL) { 1068 err = (f->methods->f_ioctl) (f, cmd, addr, fflags); 1069 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err); 1070 if (err == ENOIOCTL) { 1071 if (usb_usb_ref_device(cpd, &refs)) { 1072 err = ENXIO; 1073 goto done; 1074 } 1075 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags); 1076 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err); 1077 } 1078 } 1079 if (err == ENOIOCTL) { 1080 err = ENOTTY; 1081 } 1082 done: 1083 usb_unref_device(cpd, &refs); 1084 newbus_xunlock(); 1085 return (err); 1086 } 1087 1088 /* ARGSUSED */ 1089 static int 1090 usb_poll(struct cdev* dev, int events, struct thread* td) 1091 { 1092 struct usb_cdev_refdata refs; 1093 struct usb_cdev_privdata* cpd; 1094 struct usb_fifo *f; 1095 struct usb_mbuf *m; 1096 int fflags, revents; 1097 1098 if (devfs_get_cdevpriv((void **)&cpd) != 0 || 1099 usb_ref_device(cpd, &refs, 0) != 0) 1100 return (events & 1101 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 1102 1103 fflags = cpd->fflags; 1104 1105 /* Figure out who needs service */ 1106 revents = 0; 1107 if ((events & (POLLOUT | POLLWRNORM)) && 1108 (fflags & FWRITE)) { 1109 1110 f = refs.txfifo; 1111 1112 mtx_lock(f->priv_mtx); 1113 1114 if (!refs.is_usbfs) { 1115 if (f->flag_iserror) { 1116 /* we got an error */ 1117 m = (void *)1; 1118 } else { 1119 if (f->queue_data == NULL) { 1120 /* 1121 * start write transfer, if not 1122 * already started 1123 */ 1124 (f->methods->f_start_write) (f); 1125 } 1126 /* check if any packets are available */ 1127 USB_IF_POLL(&f->free_q, m); 1128 } 1129 } else { 1130 if (f->flag_iscomplete) { 1131 m = (void *)1; 1132 } else { 1133 m = NULL; 1134 } 1135 } 1136 1137 if (m) { 1138 revents |= events & (POLLOUT | POLLWRNORM); 1139 } else { 1140 f->flag_isselect = 1; 1141 selrecord(td, &f->selinfo); 1142 } 1143 1144 mtx_unlock(f->priv_mtx); 1145 } 1146 if ((events & (POLLIN | POLLRDNORM)) && 1147 (fflags & FREAD)) { 1148 1149 f = refs.rxfifo; 1150 1151 mtx_lock(f->priv_mtx); 1152 1153 if (!refs.is_usbfs) { 1154 if (f->flag_iserror) { 1155 /* we have and error */ 1156 m = (void *)1; 1157 } else { 1158 if (f->queue_data == NULL) { 1159 /* 1160 * start read transfer, if not 1161 * already started 1162 */ 1163 (f->methods->f_start_read) (f); 1164 } 1165 /* check if any packets are available */ 1166 USB_IF_POLL(&f->used_q, m); 1167 } 1168 } else { 1169 if (f->flag_iscomplete) { 1170 m = (void *)1; 1171 } else { 1172 m = NULL; 1173 } 1174 } 1175 1176 if (m) { 1177 revents |= events & (POLLIN | POLLRDNORM); 1178 } else { 1179 f->flag_isselect = 1; 1180 selrecord(td, &f->selinfo); 1181 1182 if (!refs.is_usbfs) { 1183 /* start reading data */ 1184 (f->methods->f_start_read) (f); 1185 } 1186 } 1187 1188 mtx_unlock(f->priv_mtx); 1189 } 1190 usb_unref_device(cpd, &refs); 1191 return (revents); 1192 } 1193 1194 static int 1195 usb_read(struct cdev *dev, struct uio *uio, int ioflag) 1196 { 1197 struct usb_cdev_refdata refs; 1198 struct usb_cdev_privdata* cpd; 1199 struct usb_fifo *f; 1200 struct usb_mbuf *m; 1201 int fflags; 1202 int resid; 1203 int io_len; 1204 int err; 1205 uint8_t tr_data = 0; 1206 1207 err = devfs_get_cdevpriv((void **)&cpd); 1208 if (err != 0) 1209 return (err); 1210 1211 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1212 if (err) { 1213 return (ENXIO); 1214 } 1215 fflags = cpd->fflags; 1216 1217 f = refs.rxfifo; 1218 if (f == NULL) { 1219 /* should not happen */ 1220 usb_unref_device(cpd, &refs); 1221 return (EPERM); 1222 } 1223 1224 resid = uio->uio_resid; 1225 1226 mtx_lock(f->priv_mtx); 1227 1228 /* check for permanent read error */ 1229 if (f->flag_iserror) { 1230 err = EIO; 1231 goto done; 1232 } 1233 /* check if USB-FS interface is active */ 1234 if (refs.is_usbfs) { 1235 /* 1236 * The queue is used for events that should be 1237 * retrieved using the "USB_FS_COMPLETE" ioctl. 1238 */ 1239 err = EINVAL; 1240 goto done; 1241 } 1242 while (uio->uio_resid > 0) { 1243 1244 USB_IF_DEQUEUE(&f->used_q, m); 1245 1246 if (m == NULL) { 1247 1248 /* start read transfer, if not already started */ 1249 1250 (f->methods->f_start_read) (f); 1251 1252 if (ioflag & IO_NDELAY) { 1253 if (tr_data) { 1254 /* return length before error */ 1255 break; 1256 } 1257 err = EWOULDBLOCK; 1258 break; 1259 } 1260 DPRINTF("sleeping\n"); 1261 1262 err = usb_fifo_wait(f); 1263 if (err) { 1264 break; 1265 } 1266 continue; 1267 } 1268 if (f->methods->f_filter_read) { 1269 /* 1270 * Sometimes it is convenient to process data at the 1271 * expense of a userland process instead of a kernel 1272 * process. 1273 */ 1274 (f->methods->f_filter_read) (f, m); 1275 } 1276 tr_data = 1; 1277 1278 io_len = MIN(m->cur_data_len, uio->uio_resid); 1279 1280 DPRINTFN(2, "transfer %d bytes from %p\n", 1281 io_len, m->cur_data_ptr); 1282 1283 err = usb_fifo_uiomove(f, 1284 m->cur_data_ptr, io_len, uio); 1285 1286 m->cur_data_len -= io_len; 1287 m->cur_data_ptr += io_len; 1288 1289 if (m->cur_data_len == 0) { 1290 1291 uint8_t last_packet; 1292 1293 last_packet = m->last_packet; 1294 1295 USB_IF_ENQUEUE(&f->free_q, m); 1296 1297 if (last_packet) { 1298 /* keep framing */ 1299 break; 1300 } 1301 } else { 1302 USB_IF_PREPEND(&f->used_q, m); 1303 } 1304 1305 if (err) { 1306 break; 1307 } 1308 } 1309 done: 1310 mtx_unlock(f->priv_mtx); 1311 1312 usb_unref_device(cpd, &refs); 1313 1314 return (err); 1315 } 1316 1317 static int 1318 usb_write(struct cdev *dev, struct uio *uio, int ioflag) 1319 { 1320 struct usb_cdev_refdata refs; 1321 struct usb_cdev_privdata* cpd; 1322 struct usb_fifo *f; 1323 struct usb_mbuf *m; 1324 uint8_t *pdata; 1325 int fflags; 1326 int resid; 1327 int io_len; 1328 int err; 1329 uint8_t tr_data = 0; 1330 1331 DPRINTFN(2, "\n"); 1332 1333 err = devfs_get_cdevpriv((void **)&cpd); 1334 if (err != 0) 1335 return (err); 1336 1337 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1338 if (err) { 1339 return (ENXIO); 1340 } 1341 fflags = cpd->fflags; 1342 1343 f = refs.txfifo; 1344 if (f == NULL) { 1345 /* should not happen */ 1346 usb_unref_device(cpd, &refs); 1347 return (EPERM); 1348 } 1349 resid = uio->uio_resid; 1350 1351 mtx_lock(f->priv_mtx); 1352 1353 /* check for permanent write error */ 1354 if (f->flag_iserror) { 1355 err = EIO; 1356 goto done; 1357 } 1358 /* check if USB-FS interface is active */ 1359 if (refs.is_usbfs) { 1360 /* 1361 * The queue is used for events that should be 1362 * retrieved using the "USB_FS_COMPLETE" ioctl. 1363 */ 1364 err = EINVAL; 1365 goto done; 1366 } 1367 if (f->queue_data == NULL) { 1368 /* start write transfer, if not already started */ 1369 (f->methods->f_start_write) (f); 1370 } 1371 /* we allow writing zero length data */ 1372 do { 1373 USB_IF_DEQUEUE(&f->free_q, m); 1374 1375 if (m == NULL) { 1376 1377 if (ioflag & IO_NDELAY) { 1378 if (tr_data) { 1379 /* return length before error */ 1380 break; 1381 } 1382 err = EWOULDBLOCK; 1383 break; 1384 } 1385 DPRINTF("sleeping\n"); 1386 1387 err = usb_fifo_wait(f); 1388 if (err) { 1389 break; 1390 } 1391 continue; 1392 } 1393 tr_data = 1; 1394 1395 if (f->flag_have_fragment == 0) { 1396 USB_MBUF_RESET(m); 1397 io_len = m->cur_data_len; 1398 pdata = m->cur_data_ptr; 1399 if (io_len > uio->uio_resid) 1400 io_len = uio->uio_resid; 1401 m->cur_data_len = io_len; 1402 } else { 1403 io_len = m->max_data_len - m->cur_data_len; 1404 pdata = m->cur_data_ptr + m->cur_data_len; 1405 if (io_len > uio->uio_resid) 1406 io_len = uio->uio_resid; 1407 m->cur_data_len += io_len; 1408 } 1409 1410 DPRINTFN(2, "transfer %d bytes to %p\n", 1411 io_len, pdata); 1412 1413 err = usb_fifo_uiomove(f, pdata, io_len, uio); 1414 1415 if (err) { 1416 f->flag_have_fragment = 0; 1417 USB_IF_ENQUEUE(&f->free_q, m); 1418 break; 1419 } 1420 1421 /* check if the buffer is ready to be transmitted */ 1422 1423 if ((f->flag_write_defrag == 0) || 1424 (m->cur_data_len == m->max_data_len)) { 1425 f->flag_have_fragment = 0; 1426 1427 /* 1428 * Check for write filter: 1429 * 1430 * Sometimes it is convenient to process data 1431 * at the expense of a userland process 1432 * instead of a kernel process. 1433 */ 1434 if (f->methods->f_filter_write) { 1435 (f->methods->f_filter_write) (f, m); 1436 } 1437 1438 /* Put USB mbuf in the used queue */ 1439 USB_IF_ENQUEUE(&f->used_q, m); 1440 1441 /* Start writing data, if not already started */ 1442 (f->methods->f_start_write) (f); 1443 } else { 1444 /* Wait for more data or close */ 1445 f->flag_have_fragment = 1; 1446 USB_IF_PREPEND(&f->free_q, m); 1447 } 1448 1449 } while (uio->uio_resid > 0); 1450 done: 1451 mtx_unlock(f->priv_mtx); 1452 1453 usb_unref_device(cpd, &refs); 1454 1455 return (err); 1456 } 1457 1458 int 1459 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 1460 struct thread *td) 1461 { 1462 union { 1463 struct usb_read_dir *urd; 1464 void* data; 1465 } u; 1466 int err = ENOTTY; 1467 1468 u.data = data; 1469 switch (cmd) { 1470 case USB_READ_DIR: 1471 err = usb_read_symlink(u.urd->urd_data, 1472 u.urd->urd_startentry, u.urd->urd_maxlen); 1473 break; 1474 case USB_DEV_QUIRK_GET: 1475 case USB_QUIRK_NAME_GET: 1476 case USB_DEV_QUIRK_ADD: 1477 case USB_DEV_QUIRK_REMOVE: 1478 err = usb_quirk_ioctl_p(cmd, data, fflag, td); 1479 break; 1480 case USB_GET_TEMPLATE: 1481 *(int *)data = usb_template; 1482 break; 1483 case USB_SET_TEMPLATE: 1484 err = priv_check(curthread, PRIV_DRIVER); 1485 if (err) 1486 break; 1487 usb_template = *(int *)data; 1488 break; 1489 } 1490 return (err); 1491 } 1492 1493 static int 1494 usb_fifo_uiomove(struct usb_fifo *f, void *cp, 1495 int n, struct uio *uio) 1496 { 1497 int error; 1498 1499 mtx_unlock(f->priv_mtx); 1500 1501 /* 1502 * "uiomove()" can sleep so one needs to make a wrapper, 1503 * exiting the mutex and checking things: 1504 */ 1505 error = uiomove(cp, n, uio); 1506 1507 mtx_lock(f->priv_mtx); 1508 1509 return (error); 1510 } 1511 1512 int 1513 usb_fifo_wait(struct usb_fifo *f) 1514 { 1515 int err; 1516 1517 mtx_assert(f->priv_mtx, MA_OWNED); 1518 1519 if (f->flag_iserror) { 1520 /* we are gone */ 1521 return (EIO); 1522 } 1523 f->flag_sleeping = 1; 1524 1525 err = cv_wait_sig(&f->cv_io, f->priv_mtx); 1526 1527 if (f->flag_iserror) { 1528 /* we are gone */ 1529 err = EIO; 1530 } 1531 return (err); 1532 } 1533 1534 void 1535 usb_fifo_signal(struct usb_fifo *f) 1536 { 1537 if (f->flag_sleeping) { 1538 f->flag_sleeping = 0; 1539 cv_broadcast(&f->cv_io); 1540 } 1541 } 1542 1543 void 1544 usb_fifo_wakeup(struct usb_fifo *f) 1545 { 1546 usb_fifo_signal(f); 1547 1548 if (f->flag_isselect) { 1549 selwakeup(&f->selinfo); 1550 f->flag_isselect = 0; 1551 } 1552 if (f->async_p != NULL) { 1553 PROC_LOCK(f->async_p); 1554 psignal(f->async_p, SIGIO); 1555 PROC_UNLOCK(f->async_p); 1556 } 1557 } 1558 1559 static int 1560 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags) 1561 { 1562 return (0); 1563 } 1564 1565 static void 1566 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags) 1567 { 1568 return; 1569 } 1570 1571 static int 1572 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 1573 { 1574 return (ENOIOCTL); 1575 } 1576 1577 static void 1578 usb_fifo_dummy_cmd(struct usb_fifo *fifo) 1579 { 1580 fifo->flag_flushing = 0; /* not flushing */ 1581 } 1582 1583 static void 1584 usb_fifo_check_methods(struct usb_fifo_methods *pm) 1585 { 1586 /* check that all callback functions are OK */ 1587 1588 if (pm->f_open == NULL) 1589 pm->f_open = &usb_fifo_dummy_open; 1590 1591 if (pm->f_close == NULL) 1592 pm->f_close = &usb_fifo_dummy_close; 1593 1594 if (pm->f_ioctl == NULL) 1595 pm->f_ioctl = &usb_fifo_dummy_ioctl; 1596 1597 if (pm->f_ioctl_post == NULL) 1598 pm->f_ioctl_post = &usb_fifo_dummy_ioctl; 1599 1600 if (pm->f_start_read == NULL) 1601 pm->f_start_read = &usb_fifo_dummy_cmd; 1602 1603 if (pm->f_stop_read == NULL) 1604 pm->f_stop_read = &usb_fifo_dummy_cmd; 1605 1606 if (pm->f_start_write == NULL) 1607 pm->f_start_write = &usb_fifo_dummy_cmd; 1608 1609 if (pm->f_stop_write == NULL) 1610 pm->f_stop_write = &usb_fifo_dummy_cmd; 1611 } 1612 1613 /*------------------------------------------------------------------------* 1614 * usb_fifo_attach 1615 * 1616 * The following function will create a duplex FIFO. 1617 * 1618 * Return values: 1619 * 0: Success. 1620 * Else: Failure. 1621 *------------------------------------------------------------------------*/ 1622 int 1623 usb_fifo_attach(struct usb_device *udev, void *priv_sc, 1624 struct mtx *priv_mtx, struct usb_fifo_methods *pm, 1625 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit, 1626 uint8_t iface_index, uid_t uid, gid_t gid, int mode) 1627 { 1628 struct usb_fifo *f_tx; 1629 struct usb_fifo *f_rx; 1630 char devname[32]; 1631 uint8_t n; 1632 struct usb_fs_privdata* pd; 1633 1634 f_sc->fp[USB_FIFO_TX] = NULL; 1635 f_sc->fp[USB_FIFO_RX] = NULL; 1636 1637 if (pm == NULL) 1638 return (EINVAL); 1639 1640 /* check the methods */ 1641 usb_fifo_check_methods(pm); 1642 1643 if (priv_mtx == NULL) 1644 priv_mtx = &Giant; 1645 1646 /* search for a free FIFO slot */ 1647 for (n = 0;; n += 2) { 1648 1649 if (n == USB_FIFO_MAX) { 1650 /* end of FIFOs reached */ 1651 return (ENOMEM); 1652 } 1653 /* Check for TX FIFO */ 1654 if (udev->fifo[n + USB_FIFO_TX] != NULL) { 1655 continue; 1656 } 1657 /* Check for RX FIFO */ 1658 if (udev->fifo[n + USB_FIFO_RX] != NULL) { 1659 continue; 1660 } 1661 break; 1662 } 1663 1664 f_tx = usb_fifo_alloc(); 1665 f_rx = usb_fifo_alloc(); 1666 1667 if ((f_tx == NULL) || (f_rx == NULL)) { 1668 usb_fifo_free(f_tx); 1669 usb_fifo_free(f_rx); 1670 return (ENOMEM); 1671 } 1672 /* initialise FIFO structures */ 1673 1674 f_tx->fifo_index = n + USB_FIFO_TX; 1675 f_tx->dev_ep_index = -1; 1676 f_tx->priv_mtx = priv_mtx; 1677 f_tx->priv_sc0 = priv_sc; 1678 f_tx->methods = pm; 1679 f_tx->iface_index = iface_index; 1680 f_tx->udev = udev; 1681 1682 f_rx->fifo_index = n + USB_FIFO_RX; 1683 f_rx->dev_ep_index = -1; 1684 f_rx->priv_mtx = priv_mtx; 1685 f_rx->priv_sc0 = priv_sc; 1686 f_rx->methods = pm; 1687 f_rx->iface_index = iface_index; 1688 f_rx->udev = udev; 1689 1690 f_sc->fp[USB_FIFO_TX] = f_tx; 1691 f_sc->fp[USB_FIFO_RX] = f_rx; 1692 1693 mtx_lock(&usb_ref_lock); 1694 udev->fifo[f_tx->fifo_index] = f_tx; 1695 udev->fifo[f_rx->fifo_index] = f_rx; 1696 mtx_unlock(&usb_ref_lock); 1697 1698 for (n = 0; n != 4; n++) { 1699 1700 if (pm->basename[n] == NULL) { 1701 continue; 1702 } 1703 if (subunit == 0xFFFF) { 1704 if (snprintf(devname, sizeof(devname), 1705 "%s%u%s", pm->basename[n], 1706 unit, pm->postfix[n] ? 1707 pm->postfix[n] : "")) { 1708 /* ignore */ 1709 } 1710 } else { 1711 if (snprintf(devname, sizeof(devname), 1712 "%s%u.%u%s", pm->basename[n], 1713 unit, subunit, pm->postfix[n] ? 1714 pm->postfix[n] : "")) { 1715 /* ignore */ 1716 } 1717 } 1718 1719 /* 1720 * Distribute the symbolic links into two FIFO structures: 1721 */ 1722 if (n & 1) { 1723 f_rx->symlink[n / 2] = 1724 usb_alloc_symlink(devname); 1725 } else { 1726 f_tx->symlink[n / 2] = 1727 usb_alloc_symlink(devname); 1728 } 1729 1730 /* 1731 * Initialize device private data - this is used to find the 1732 * actual USB device itself. 1733 */ 1734 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO); 1735 pd->bus_index = device_get_unit(udev->bus->bdev); 1736 pd->dev_index = udev->device_index; 1737 pd->ep_addr = -1; /* not an endpoint */ 1738 pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index; 1739 pd->mode = FREAD|FWRITE; 1740 1741 /* Now, create the device itself */ 1742 f_sc->dev = make_dev(&usb_devsw, 0, uid, gid, mode, 1743 devname); 1744 /* XXX setting si_drv1 and creating the device is not atomic! */ 1745 f_sc->dev->si_drv1 = pd; 1746 } 1747 1748 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx); 1749 return (0); 1750 } 1751 1752 /*------------------------------------------------------------------------* 1753 * usb_fifo_alloc_buffer 1754 * 1755 * Return values: 1756 * 0: Success 1757 * Else failure 1758 *------------------------------------------------------------------------*/ 1759 int 1760 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize, 1761 uint16_t nbuf) 1762 { 1763 usb_fifo_free_buffer(f); 1764 1765 /* allocate an endpoint */ 1766 f->free_q.ifq_maxlen = nbuf; 1767 f->used_q.ifq_maxlen = nbuf; 1768 1769 f->queue_data = usb_alloc_mbufs( 1770 M_USBDEV, &f->free_q, bufsize, nbuf); 1771 1772 if ((f->queue_data == NULL) && bufsize && nbuf) { 1773 return (ENOMEM); 1774 } 1775 return (0); /* success */ 1776 } 1777 1778 /*------------------------------------------------------------------------* 1779 * usb_fifo_free_buffer 1780 * 1781 * This function will free the buffers associated with a FIFO. This 1782 * function can be called multiple times in a row. 1783 *------------------------------------------------------------------------*/ 1784 void 1785 usb_fifo_free_buffer(struct usb_fifo *f) 1786 { 1787 if (f->queue_data) { 1788 /* free old buffer */ 1789 free(f->queue_data, M_USBDEV); 1790 f->queue_data = NULL; 1791 } 1792 /* reset queues */ 1793 1794 bzero(&f->free_q, sizeof(f->free_q)); 1795 bzero(&f->used_q, sizeof(f->used_q)); 1796 } 1797 1798 static void 1799 usb_fifo_cleanup(void* ptr) 1800 { 1801 free(ptr, M_USBDEV); 1802 } 1803 1804 void 1805 usb_fifo_detach(struct usb_fifo_sc *f_sc) 1806 { 1807 if (f_sc == NULL) { 1808 return; 1809 } 1810 usb_fifo_free(f_sc->fp[USB_FIFO_TX]); 1811 usb_fifo_free(f_sc->fp[USB_FIFO_RX]); 1812 1813 f_sc->fp[USB_FIFO_TX] = NULL; 1814 f_sc->fp[USB_FIFO_RX] = NULL; 1815 1816 if (f_sc->dev != NULL) { 1817 destroy_dev_sched_cb(f_sc->dev, 1818 usb_fifo_cleanup, f_sc->dev->si_drv1); 1819 f_sc->dev = NULL; 1820 } 1821 1822 DPRINTFN(2, "detached %p\n", f_sc); 1823 } 1824 1825 usb_size_t 1826 usb_fifo_put_bytes_max(struct usb_fifo *f) 1827 { 1828 struct usb_mbuf *m; 1829 usb_size_t len; 1830 1831 USB_IF_POLL(&f->free_q, m); 1832 1833 if (m) { 1834 len = m->max_data_len; 1835 } else { 1836 len = 0; 1837 } 1838 return (len); 1839 } 1840 1841 /*------------------------------------------------------------------------* 1842 * usb_fifo_put_data 1843 * 1844 * what: 1845 * 0 - normal operation 1846 * 1 - set last packet flag to enforce framing 1847 *------------------------------------------------------------------------*/ 1848 void 1849 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc, 1850 usb_frlength_t offset, usb_frlength_t len, uint8_t what) 1851 { 1852 struct usb_mbuf *m; 1853 usb_frlength_t io_len; 1854 1855 while (len || (what == 1)) { 1856 1857 USB_IF_DEQUEUE(&f->free_q, m); 1858 1859 if (m) { 1860 USB_MBUF_RESET(m); 1861 1862 io_len = MIN(len, m->cur_data_len); 1863 1864 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len); 1865 1866 m->cur_data_len = io_len; 1867 offset += io_len; 1868 len -= io_len; 1869 1870 if ((len == 0) && (what == 1)) { 1871 m->last_packet = 1; 1872 } 1873 USB_IF_ENQUEUE(&f->used_q, m); 1874 1875 usb_fifo_wakeup(f); 1876 1877 if ((len == 0) || (what == 1)) { 1878 break; 1879 } 1880 } else { 1881 break; 1882 } 1883 } 1884 } 1885 1886 void 1887 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr, 1888 usb_size_t len, uint8_t what) 1889 { 1890 struct usb_mbuf *m; 1891 usb_size_t io_len; 1892 1893 while (len || (what == 1)) { 1894 1895 USB_IF_DEQUEUE(&f->free_q, m); 1896 1897 if (m) { 1898 USB_MBUF_RESET(m); 1899 1900 io_len = MIN(len, m->cur_data_len); 1901 1902 bcopy(ptr, m->cur_data_ptr, io_len); 1903 1904 m->cur_data_len = io_len; 1905 ptr = USB_ADD_BYTES(ptr, io_len); 1906 len -= io_len; 1907 1908 if ((len == 0) && (what == 1)) { 1909 m->last_packet = 1; 1910 } 1911 USB_IF_ENQUEUE(&f->used_q, m); 1912 1913 usb_fifo_wakeup(f); 1914 1915 if ((len == 0) || (what == 1)) { 1916 break; 1917 } 1918 } else { 1919 break; 1920 } 1921 } 1922 } 1923 1924 uint8_t 1925 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len) 1926 { 1927 struct usb_mbuf *m; 1928 1929 USB_IF_DEQUEUE(&f->free_q, m); 1930 1931 if (m) { 1932 m->cur_data_len = len; 1933 m->cur_data_ptr = ptr; 1934 USB_IF_ENQUEUE(&f->used_q, m); 1935 usb_fifo_wakeup(f); 1936 return (1); 1937 } 1938 return (0); 1939 } 1940 1941 void 1942 usb_fifo_put_data_error(struct usb_fifo *f) 1943 { 1944 f->flag_iserror = 1; 1945 usb_fifo_wakeup(f); 1946 } 1947 1948 /*------------------------------------------------------------------------* 1949 * usb_fifo_get_data 1950 * 1951 * what: 1952 * 0 - normal operation 1953 * 1 - only get one "usb_mbuf" 1954 * 1955 * returns: 1956 * 0 - no more data 1957 * 1 - data in buffer 1958 *------------------------------------------------------------------------*/ 1959 uint8_t 1960 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc, 1961 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen, 1962 uint8_t what) 1963 { 1964 struct usb_mbuf *m; 1965 usb_frlength_t io_len; 1966 uint8_t tr_data = 0; 1967 1968 actlen[0] = 0; 1969 1970 while (1) { 1971 1972 USB_IF_DEQUEUE(&f->used_q, m); 1973 1974 if (m) { 1975 1976 tr_data = 1; 1977 1978 io_len = MIN(len, m->cur_data_len); 1979 1980 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len); 1981 1982 len -= io_len; 1983 offset += io_len; 1984 actlen[0] += io_len; 1985 m->cur_data_ptr += io_len; 1986 m->cur_data_len -= io_len; 1987 1988 if ((m->cur_data_len == 0) || (what == 1)) { 1989 USB_IF_ENQUEUE(&f->free_q, m); 1990 1991 usb_fifo_wakeup(f); 1992 1993 if (what == 1) { 1994 break; 1995 } 1996 } else { 1997 USB_IF_PREPEND(&f->used_q, m); 1998 } 1999 } else { 2000 2001 if (tr_data) { 2002 /* wait for data to be written out */ 2003 break; 2004 } 2005 if (f->flag_flushing) { 2006 /* check if we should send a short packet */ 2007 if (f->flag_short != 0) { 2008 f->flag_short = 0; 2009 tr_data = 1; 2010 break; 2011 } 2012 /* flushing complete */ 2013 f->flag_flushing = 0; 2014 usb_fifo_wakeup(f); 2015 } 2016 break; 2017 } 2018 if (len == 0) { 2019 break; 2020 } 2021 } 2022 return (tr_data); 2023 } 2024 2025 uint8_t 2026 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr, 2027 usb_size_t len, usb_size_t *actlen, uint8_t what) 2028 { 2029 struct usb_mbuf *m; 2030 usb_size_t io_len; 2031 uint8_t tr_data = 0; 2032 2033 actlen[0] = 0; 2034 2035 while (1) { 2036 2037 USB_IF_DEQUEUE(&f->used_q, m); 2038 2039 if (m) { 2040 2041 tr_data = 1; 2042 2043 io_len = MIN(len, m->cur_data_len); 2044 2045 bcopy(m->cur_data_ptr, ptr, io_len); 2046 2047 len -= io_len; 2048 ptr = USB_ADD_BYTES(ptr, io_len); 2049 actlen[0] += io_len; 2050 m->cur_data_ptr += io_len; 2051 m->cur_data_len -= io_len; 2052 2053 if ((m->cur_data_len == 0) || (what == 1)) { 2054 USB_IF_ENQUEUE(&f->free_q, m); 2055 2056 usb_fifo_wakeup(f); 2057 2058 if (what == 1) { 2059 break; 2060 } 2061 } else { 2062 USB_IF_PREPEND(&f->used_q, m); 2063 } 2064 } else { 2065 2066 if (tr_data) { 2067 /* wait for data to be written out */ 2068 break; 2069 } 2070 if (f->flag_flushing) { 2071 /* check if we should send a short packet */ 2072 if (f->flag_short != 0) { 2073 f->flag_short = 0; 2074 tr_data = 1; 2075 break; 2076 } 2077 /* flushing complete */ 2078 f->flag_flushing = 0; 2079 usb_fifo_wakeup(f); 2080 } 2081 break; 2082 } 2083 if (len == 0) { 2084 break; 2085 } 2086 } 2087 return (tr_data); 2088 } 2089 2090 uint8_t 2091 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen) 2092 { 2093 struct usb_mbuf *m; 2094 2095 USB_IF_POLL(&f->used_q, m); 2096 2097 if (m) { 2098 *plen = m->cur_data_len; 2099 *pptr = m->cur_data_ptr; 2100 2101 return (1); 2102 } 2103 return (0); 2104 } 2105 2106 void 2107 usb_fifo_get_data_error(struct usb_fifo *f) 2108 { 2109 f->flag_iserror = 1; 2110 usb_fifo_wakeup(f); 2111 } 2112 2113 /*------------------------------------------------------------------------* 2114 * usb_alloc_symlink 2115 * 2116 * Return values: 2117 * NULL: Failure 2118 * Else: Pointer to symlink entry 2119 *------------------------------------------------------------------------*/ 2120 struct usb_symlink * 2121 usb_alloc_symlink(const char *target) 2122 { 2123 struct usb_symlink *ps; 2124 2125 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK); 2126 if (ps == NULL) { 2127 return (ps); 2128 } 2129 /* XXX no longer needed */ 2130 strlcpy(ps->src_path, target, sizeof(ps->src_path)); 2131 ps->src_len = strlen(ps->src_path); 2132 strlcpy(ps->dst_path, target, sizeof(ps->dst_path)); 2133 ps->dst_len = strlen(ps->dst_path); 2134 2135 sx_xlock(&usb_sym_lock); 2136 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry); 2137 sx_unlock(&usb_sym_lock); 2138 return (ps); 2139 } 2140 2141 /*------------------------------------------------------------------------* 2142 * usb_free_symlink 2143 *------------------------------------------------------------------------*/ 2144 void 2145 usb_free_symlink(struct usb_symlink *ps) 2146 { 2147 if (ps == NULL) { 2148 return; 2149 } 2150 sx_xlock(&usb_sym_lock); 2151 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry); 2152 sx_unlock(&usb_sym_lock); 2153 2154 free(ps, M_USBDEV); 2155 } 2156 2157 /*------------------------------------------------------------------------* 2158 * usb_read_symlink 2159 * 2160 * Return value: 2161 * 0: Success 2162 * Else: Failure 2163 *------------------------------------------------------------------------*/ 2164 int 2165 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len) 2166 { 2167 struct usb_symlink *ps; 2168 uint32_t temp; 2169 uint32_t delta = 0; 2170 uint8_t len; 2171 int error = 0; 2172 2173 sx_xlock(&usb_sym_lock); 2174 2175 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) { 2176 2177 /* 2178 * Compute total length of source and destination symlink 2179 * strings pluss one length byte and two NUL bytes: 2180 */ 2181 temp = ps->src_len + ps->dst_len + 3; 2182 2183 if (temp > 255) { 2184 /* 2185 * Skip entry because this length cannot fit 2186 * into one byte: 2187 */ 2188 continue; 2189 } 2190 if (startentry != 0) { 2191 /* decrement read offset */ 2192 startentry--; 2193 continue; 2194 } 2195 if (temp > user_len) { 2196 /* out of buffer space */ 2197 break; 2198 } 2199 len = temp; 2200 2201 /* copy out total length */ 2202 2203 error = copyout(&len, 2204 USB_ADD_BYTES(user_ptr, delta), 1); 2205 if (error) { 2206 break; 2207 } 2208 delta += 1; 2209 2210 /* copy out source string */ 2211 2212 error = copyout(ps->src_path, 2213 USB_ADD_BYTES(user_ptr, delta), ps->src_len); 2214 if (error) { 2215 break; 2216 } 2217 len = 0; 2218 delta += ps->src_len; 2219 error = copyout(&len, 2220 USB_ADD_BYTES(user_ptr, delta), 1); 2221 if (error) { 2222 break; 2223 } 2224 delta += 1; 2225 2226 /* copy out destination string */ 2227 2228 error = copyout(ps->dst_path, 2229 USB_ADD_BYTES(user_ptr, delta), ps->dst_len); 2230 if (error) { 2231 break; 2232 } 2233 len = 0; 2234 delta += ps->dst_len; 2235 error = copyout(&len, 2236 USB_ADD_BYTES(user_ptr, delta), 1); 2237 if (error) { 2238 break; 2239 } 2240 delta += 1; 2241 2242 user_len -= temp; 2243 } 2244 2245 /* a zero length entry indicates the end */ 2246 2247 if ((user_len != 0) && (error == 0)) { 2248 2249 len = 0; 2250 2251 error = copyout(&len, 2252 USB_ADD_BYTES(user_ptr, delta), 1); 2253 } 2254 sx_unlock(&usb_sym_lock); 2255 return (error); 2256 } 2257 2258 void 2259 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff) 2260 { 2261 if (f == NULL) 2262 return; 2263 2264 /* send a Zero Length Packet, ZLP, before close */ 2265 f->flag_short = onoff; 2266 } 2267 2268 void 2269 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff) 2270 { 2271 if (f == NULL) 2272 return; 2273 2274 /* defrag written data */ 2275 f->flag_write_defrag = onoff; 2276 /* reset defrag state */ 2277 f->flag_have_fragment = 0; 2278 } 2279 2280 void * 2281 usb_fifo_softc(struct usb_fifo *f) 2282 { 2283 return (f->priv_sc0); 2284 } 2285 #endif /* USB_HAVE_UGEN */ 2286