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