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