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