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