1/* $FreeBSD: releng/10.3/sys/dev/usb/usb_dev.c 284500 2015-06-17 07:36:00Z hselasky $ */ 2/*- 3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * 27 * usb_dev.c - An abstraction layer for creating devices under /dev/... 28 */ 29 30#ifdef USB_GLOBAL_INCLUDE_FILE 31#include USB_GLOBAL_INCLUDE_FILE 32#else 33#include <sys/stdint.h> 34#include <sys/stddef.h> 35#include <sys/param.h> 36#include <sys/queue.h> 37#include <sys/types.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/bus.h> 41#include <sys/module.h> 42#include <sys/lock.h> 43#include <sys/mutex.h> 44#include <sys/condvar.h> 45#include <sys/sysctl.h> 46#include <sys/sx.h> 47#include <sys/unistd.h> 48#include <sys/callout.h> 49#include <sys/malloc.h> 50#include <sys/priv.h> 51#include <sys/vnode.h> 52#include <sys/conf.h> 53#include <sys/fcntl.h> 54 55#include <dev/usb/usb.h> 56#include <dev/usb/usb_ioctl.h> 57#include <dev/usb/usbdi.h> 58#include <dev/usb/usbdi_util.h> 59 60#define USB_DEBUG_VAR usb_fifo_debug 61 62#include <dev/usb/usb_core.h> 63#include <dev/usb/usb_dev.h> 64#include <dev/usb/usb_mbuf.h> 65#include <dev/usb/usb_process.h> 66#include <dev/usb/usb_device.h> 67#include <dev/usb/usb_debug.h> 68#include <dev/usb/usb_busdma.h> 69#include <dev/usb/usb_generic.h> 70#include <dev/usb/usb_dynamic.h> 71#include <dev/usb/usb_util.h> 72 73#include <dev/usb/usb_controller.h> 74#include <dev/usb/usb_bus.h> 75 76#include <sys/filio.h> 77#include <sys/ttycom.h> 78#include <sys/syscallsubr.h> 79 80#include <machine/stdarg.h> 81#endif /* USB_GLOBAL_INCLUDE_FILE */ 82 83#if USB_HAVE_UGEN 84 85#ifdef USB_DEBUG 86static int usb_fifo_debug = 0; 87 88static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device"); 89SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, 90 &usb_fifo_debug, 0, "Debug Level"); 91TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug); 92#endif 93 94#if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \ 95 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000))) 96#define USB_UCRED struct ucred *ucred, 97#else 98#define USB_UCRED 99#endif 100 101/* prototypes */ 102 103static int usb_fifo_open(struct usb_cdev_privdata *, 104 struct usb_fifo *, int); 105static void usb_fifo_close(struct usb_fifo *, int); 106static void usb_dev_init(void *); 107static void usb_dev_init_post(void *); 108static void usb_dev_uninit(void *); 109static int usb_fifo_uiomove(struct usb_fifo *, void *, int, 110 struct uio *); 111static void usb_fifo_check_methods(struct usb_fifo_methods *); 112static struct usb_fifo *usb_fifo_alloc(struct mtx *); 113static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t, 114 uint8_t); 115static void usb_loc_fill(struct usb_fs_privdata *, 116 struct usb_cdev_privdata *); 117static void usb_close(void *); 118static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int); 119static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *); 120static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *); 121 122static d_open_t usb_open; 123static d_ioctl_t usb_ioctl; 124static d_read_t usb_read; 125static d_write_t usb_write; 126static d_poll_t usb_poll; 127static d_kqfilter_t usb_kqfilter; 128 129static d_ioctl_t usb_static_ioctl; 130 131static usb_fifo_open_t usb_fifo_dummy_open; 132static usb_fifo_close_t usb_fifo_dummy_close; 133static usb_fifo_ioctl_t usb_fifo_dummy_ioctl; 134static usb_fifo_cmd_t usb_fifo_dummy_cmd; 135 136/* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */ 137struct cdevsw usb_devsw = { 138 .d_version = D_VERSION, 139 .d_open = usb_open, 140 .d_ioctl = usb_ioctl, 141 .d_name = "usbdev", 142 .d_flags = D_TRACKCLOSE, 143 .d_read = usb_read, 144 .d_write = usb_write, 145 .d_poll = usb_poll, 146 .d_kqfilter = usb_kqfilter, 147}; 148 149static struct cdev* usb_dev = NULL; 150 151/* character device structure used for /dev/usb */ 152static struct cdevsw usb_static_devsw = { 153 .d_version = D_VERSION, 154 .d_ioctl = usb_static_ioctl, 155 .d_name = "usb" 156}; 157 158static TAILQ_HEAD(, usb_symlink) usb_sym_head; 159static struct sx usb_sym_lock; 160 161struct mtx usb_ref_lock; 162 163/*------------------------------------------------------------------------* 164 * usb_loc_fill 165 * 166 * This is used to fill out a usb_cdev_privdata structure based on the 167 * device's address as contained in usb_fs_privdata. 168 *------------------------------------------------------------------------*/ 169static void 170usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd) 171{ 172 cpd->bus_index = pd->bus_index; 173 cpd->dev_index = pd->dev_index; 174 cpd->ep_addr = pd->ep_addr; 175 cpd->fifo_index = pd->fifo_index; 176} 177 178/*------------------------------------------------------------------------* 179 * usb_ref_device 180 * 181 * This function is used to atomically refer an USB device by its 182 * device location. If this function returns success the USB device 183 * will not dissappear until the USB device is unreferenced. 184 * 185 * Return values: 186 * 0: Success, refcount incremented on the given USB device. 187 * Else: Failure. 188 *------------------------------------------------------------------------*/ 189static usb_error_t 190usb_ref_device(struct usb_cdev_privdata *cpd, 191 struct usb_cdev_refdata *crd, int need_uref) 192{ 193 struct usb_fifo **ppf; 194 struct usb_fifo *f; 195 196 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref); 197 198 /* clear all refs */ 199 memset(crd, 0, sizeof(*crd)); 200 201 mtx_lock(&usb_ref_lock); 202 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index); 203 if (cpd->bus == NULL) { 204 DPRINTFN(2, "no bus at %u\n", cpd->bus_index); 205 goto error; 206 } 207 cpd->udev = cpd->bus->devices[cpd->dev_index]; 208 if (cpd->udev == NULL) { 209 DPRINTFN(2, "no device at %u\n", cpd->dev_index); 210 goto error; 211 } 212 if (cpd->udev->state == USB_STATE_DETACHED && 213 (need_uref != 2)) { 214 DPRINTFN(2, "device is detached\n"); 215 goto error; 216 } 217 if (need_uref) { 218 DPRINTFN(2, "ref udev - needed\n"); 219 220 if (cpd->udev->refcount == USB_DEV_REF_MAX) { 221 DPRINTFN(2, "no dev ref\n"); 222 goto error; 223 } 224 cpd->udev->refcount++; 225 226 mtx_unlock(&usb_ref_lock); 227 228 /* 229 * We need to grab the enumeration SX-lock before 230 * grabbing the FIFO refs to avoid deadlock at detach! 231 */ 232 crd->do_unlock = usbd_enum_lock(cpd->udev); 233 234 mtx_lock(&usb_ref_lock); 235 236 /* 237 * Set "is_uref" after grabbing the default SX lock 238 */ 239 crd->is_uref = 1; 240 } 241 242 /* check if we are doing an open */ 243 if (cpd->fflags == 0) { 244 /* use zero defaults */ 245 } else { 246 /* check for write */ 247 if (cpd->fflags & FWRITE) { 248 ppf = cpd->udev->fifo; 249 f = ppf[cpd->fifo_index + USB_FIFO_TX]; 250 crd->txfifo = f; 251 crd->is_write = 1; /* ref */ 252 if (f == NULL || f->refcount == USB_FIFO_REF_MAX) 253 goto error; 254 if (f->curr_cpd != cpd) 255 goto error; 256 /* check if USB-FS is active */ 257 if (f->fs_ep_max != 0) { 258 crd->is_usbfs = 1; 259 } 260 } 261 262 /* check for read */ 263 if (cpd->fflags & FREAD) { 264 ppf = cpd->udev->fifo; 265 f = ppf[cpd->fifo_index + USB_FIFO_RX]; 266 crd->rxfifo = f; 267 crd->is_read = 1; /* ref */ 268 if (f == NULL || f->refcount == USB_FIFO_REF_MAX) 269 goto error; 270 if (f->curr_cpd != cpd) 271 goto error; 272 /* check if USB-FS is active */ 273 if (f->fs_ep_max != 0) { 274 crd->is_usbfs = 1; 275 } 276 } 277 } 278 279 /* when everything is OK we increment the refcounts */ 280 if (crd->is_write) { 281 DPRINTFN(2, "ref write\n"); 282 crd->txfifo->refcount++; 283 } 284 if (crd->is_read) { 285 DPRINTFN(2, "ref read\n"); 286 crd->rxfifo->refcount++; 287 } 288 mtx_unlock(&usb_ref_lock); 289 290 return (0); 291 292error: 293 if (crd->do_unlock) 294 usbd_enum_unlock(cpd->udev); 295 296 if (crd->is_uref) { 297 if (--(cpd->udev->refcount) == 0) 298 cv_broadcast(&cpd->udev->ref_cv); 299 } 300 mtx_unlock(&usb_ref_lock); 301 DPRINTFN(2, "fail\n"); 302 303 /* clear all refs */ 304 memset(crd, 0, sizeof(*crd)); 305 306 return (USB_ERR_INVAL); 307} 308 309/*------------------------------------------------------------------------* 310 * usb_usb_ref_device 311 * 312 * This function is used to upgrade an USB reference to include the 313 * USB device reference on a USB location. 314 * 315 * Return values: 316 * 0: Success, refcount incremented on the given USB device. 317 * Else: Failure. 318 *------------------------------------------------------------------------*/ 319static usb_error_t 320usb_usb_ref_device(struct usb_cdev_privdata *cpd, 321 struct usb_cdev_refdata *crd) 322{ 323 /* 324 * Check if we already got an USB reference on this location: 325 */ 326 if (crd->is_uref) 327 return (0); /* success */ 328 329 /* 330 * To avoid deadlock at detach we need to drop the FIFO ref 331 * and re-acquire a new ref! 332 */ 333 usb_unref_device(cpd, crd); 334 335 return (usb_ref_device(cpd, crd, 1 /* need uref */)); 336} 337 338/*------------------------------------------------------------------------* 339 * usb_unref_device 340 * 341 * This function will release the reference count by one unit for the 342 * given USB device. 343 *------------------------------------------------------------------------*/ 344static void 345usb_unref_device(struct usb_cdev_privdata *cpd, 346 struct usb_cdev_refdata *crd) 347{ 348 349 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref); 350 351 if (crd->do_unlock) 352 usbd_enum_unlock(cpd->udev); 353 354 mtx_lock(&usb_ref_lock); 355 if (crd->is_read) { 356 if (--(crd->rxfifo->refcount) == 0) { 357 cv_signal(&crd->rxfifo->cv_drain); 358 } 359 crd->is_read = 0; 360 } 361 if (crd->is_write) { 362 if (--(crd->txfifo->refcount) == 0) { 363 cv_signal(&crd->txfifo->cv_drain); 364 } 365 crd->is_write = 0; 366 } 367 if (crd->is_uref) { 368 crd->is_uref = 0; 369 if (--(cpd->udev->refcount) == 0) 370 cv_broadcast(&cpd->udev->ref_cv); 371 } 372 mtx_unlock(&usb_ref_lock); 373} 374 375static struct usb_fifo * 376usb_fifo_alloc(struct mtx *mtx) 377{ 378 struct usb_fifo *f; 379 380 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO); 381 if (f != NULL) { 382 cv_init(&f->cv_io, "FIFO-IO"); 383 cv_init(&f->cv_drain, "FIFO-DRAIN"); 384 f->priv_mtx = mtx; 385 f->refcount = 1; 386 knlist_init_mtx(&f->selinfo.si_note, mtx); 387 } 388 return (f); 389} 390 391/*------------------------------------------------------------------------* 392 * usb_fifo_create 393 *------------------------------------------------------------------------*/ 394static int 395usb_fifo_create(struct usb_cdev_privdata *cpd, 396 struct usb_cdev_refdata *crd) 397{ 398 struct usb_device *udev = cpd->udev; 399 struct usb_fifo *f; 400 struct usb_endpoint *ep; 401 uint8_t n; 402 uint8_t is_tx; 403 uint8_t is_rx; 404 uint8_t no_null; 405 uint8_t is_busy; 406 int e = cpd->ep_addr; 407 408 is_tx = (cpd->fflags & FWRITE) ? 1 : 0; 409 is_rx = (cpd->fflags & FREAD) ? 1 : 0; 410 no_null = 1; 411 is_busy = 0; 412 413 /* Preallocated FIFO */ 414 if (e < 0) { 415 DPRINTFN(5, "Preallocated FIFO\n"); 416 if (is_tx) { 417 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX]; 418 if (f == NULL) 419 return (EINVAL); 420 crd->txfifo = f; 421 } 422 if (is_rx) { 423 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX]; 424 if (f == NULL) 425 return (EINVAL); 426 crd->rxfifo = f; 427 } 428 return (0); 429 } 430 431 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e)); 432 433 /* search for a free FIFO slot */ 434 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e); 435 for (n = 0;; n += 2) { 436 437 if (n == USB_FIFO_MAX) { 438 if (no_null) { 439 no_null = 0; 440 n = 0; 441 } else { 442 /* end of FIFOs reached */ 443 DPRINTFN(5, "out of FIFOs\n"); 444 return (ENOMEM); 445 } 446 } 447 /* Check for TX FIFO */ 448 if (is_tx) { 449 f = udev->fifo[n + USB_FIFO_TX]; 450 if (f != NULL) { 451 if (f->dev_ep_index != e) { 452 /* wrong endpoint index */ 453 continue; 454 } 455 if (f->curr_cpd != NULL) { 456 /* FIFO is opened */ 457 is_busy = 1; 458 continue; 459 } 460 } else if (no_null) { 461 continue; 462 } 463 } 464 /* Check for RX FIFO */ 465 if (is_rx) { 466 f = udev->fifo[n + USB_FIFO_RX]; 467 if (f != NULL) { 468 if (f->dev_ep_index != e) { 469 /* wrong endpoint index */ 470 continue; 471 } 472 if (f->curr_cpd != NULL) { 473 /* FIFO is opened */ 474 is_busy = 1; 475 continue; 476 } 477 } else if (no_null) { 478 continue; 479 } 480 } 481 break; 482 } 483 484 if (no_null == 0) { 485 if (e >= (USB_EP_MAX / 2)) { 486 /* we don't create any endpoints in this range */ 487 DPRINTFN(5, "ep out of range\n"); 488 return (is_busy ? EBUSY : EINVAL); 489 } 490 } 491 492 if ((e != 0) && is_busy) { 493 /* 494 * Only the default control endpoint is allowed to be 495 * opened multiple times! 496 */ 497 DPRINTFN(5, "busy\n"); 498 return (EBUSY); 499 } 500 501 /* Check TX FIFO */ 502 if (is_tx && 503 (udev->fifo[n + USB_FIFO_TX] == NULL)) { 504 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX); 505 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX); 506 if (ep == NULL) { 507 DPRINTFN(5, "dev_get_endpoint returned NULL\n"); 508 return (EINVAL); 509 } 510 f = usb_fifo_alloc(&udev->device_mtx); 511 if (f == NULL) { 512 DPRINTFN(5, "could not alloc tx fifo\n"); 513 return (ENOMEM); 514 } 515 /* update some fields */ 516 f->fifo_index = n + USB_FIFO_TX; 517 f->dev_ep_index = e; 518 f->priv_sc0 = ep; 519 f->methods = &usb_ugen_methods; 520 f->iface_index = ep->iface_index; 521 f->udev = udev; 522 mtx_lock(&usb_ref_lock); 523 udev->fifo[n + USB_FIFO_TX] = f; 524 mtx_unlock(&usb_ref_lock); 525 } 526 /* Check RX FIFO */ 527 if (is_rx && 528 (udev->fifo[n + USB_FIFO_RX] == NULL)) { 529 530 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX); 531 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX); 532 if (ep == NULL) { 533 DPRINTFN(5, "dev_get_endpoint returned NULL\n"); 534 return (EINVAL); 535 } 536 f = usb_fifo_alloc(&udev->device_mtx); 537 if (f == NULL) { 538 DPRINTFN(5, "could not alloc rx fifo\n"); 539 return (ENOMEM); 540 } 541 /* update some fields */ 542 f->fifo_index = n + USB_FIFO_RX; 543 f->dev_ep_index = e; 544 f->priv_sc0 = ep; 545 f->methods = &usb_ugen_methods; 546 f->iface_index = ep->iface_index; 547 f->udev = udev; 548 mtx_lock(&usb_ref_lock); 549 udev->fifo[n + USB_FIFO_RX] = f; 550 mtx_unlock(&usb_ref_lock); 551 } 552 if (is_tx) { 553 crd->txfifo = udev->fifo[n + USB_FIFO_TX]; 554 } 555 if (is_rx) { 556 crd->rxfifo = udev->fifo[n + USB_FIFO_RX]; 557 } 558 /* fill out fifo index */ 559 DPRINTFN(5, "fifo index = %d\n", n); 560 cpd->fifo_index = n; 561 562 /* complete */ 563 564 return (0); 565} 566 567void 568usb_fifo_free(struct usb_fifo *f) 569{ 570 uint8_t n; 571 572 if (f == NULL) { 573 /* be NULL safe */ 574 return; 575 } 576 /* destroy symlink devices, if any */ 577 for (n = 0; n != 2; n++) { 578 if (f->symlink[n]) { 579 usb_free_symlink(f->symlink[n]); 580 f->symlink[n] = NULL; 581 } 582 } 583 mtx_lock(&usb_ref_lock); 584 585 /* delink ourselves to stop calls from userland */ 586 if ((f->fifo_index < USB_FIFO_MAX) && 587 (f->udev != NULL) && 588 (f->udev->fifo[f->fifo_index] == f)) { 589 f->udev->fifo[f->fifo_index] = NULL; 590 } else { 591 DPRINTFN(0, "USB FIFO %p has not been linked\n", f); 592 } 593 594 /* decrease refcount */ 595 f->refcount--; 596 /* need to wait until all callers have exited */ 597 while (f->refcount != 0) { 598 mtx_unlock(&usb_ref_lock); /* avoid LOR */ 599 mtx_lock(f->priv_mtx); 600 /* prevent write flush, if any */ 601 f->flag_iserror = 1; 602 /* get I/O thread out of any sleep state */ 603 if (f->flag_sleeping) { 604 f->flag_sleeping = 0; 605 cv_broadcast(&f->cv_io); 606 } 607 mtx_unlock(f->priv_mtx); 608 mtx_lock(&usb_ref_lock); 609 610 /* 611 * Check if the "f->refcount" variable reached zero 612 * during the unlocked time before entering wait: 613 */ 614 if (f->refcount == 0) 615 break; 616 617 /* wait for sync */ 618 cv_wait(&f->cv_drain, &usb_ref_lock); 619 } 620 mtx_unlock(&usb_ref_lock); 621 622 /* take care of closing the device here, if any */ 623 usb_fifo_close(f, 0); 624 625 cv_destroy(&f->cv_io); 626 cv_destroy(&f->cv_drain); 627 628 knlist_clear(&f->selinfo.si_note, 0); 629 seldrain(&f->selinfo); 630 knlist_destroy(&f->selinfo.si_note); 631 632 free(f, M_USBDEV); 633} 634 635static struct usb_endpoint * 636usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir) 637{ 638 struct usb_endpoint *ep; 639 uint8_t ep_dir; 640 641 if (ep_index == 0) { 642 ep = &udev->ctrl_ep; 643 } else { 644 if (dir == USB_FIFO_RX) { 645 if (udev->flags.usb_mode == USB_MODE_HOST) { 646 ep_dir = UE_DIR_IN; 647 } else { 648 ep_dir = UE_DIR_OUT; 649 } 650 } else { 651 if (udev->flags.usb_mode == USB_MODE_HOST) { 652 ep_dir = UE_DIR_OUT; 653 } else { 654 ep_dir = UE_DIR_IN; 655 } 656 } 657 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir); 658 } 659 660 if (ep == NULL) { 661 /* if the endpoint does not exist then return */ 662 return (NULL); 663 } 664 if (ep->edesc == NULL) { 665 /* invalid endpoint */ 666 return (NULL); 667 } 668 return (ep); /* success */ 669} 670 671/*------------------------------------------------------------------------* 672 * usb_fifo_open 673 * 674 * Returns: 675 * 0: Success 676 * Else: Failure 677 *------------------------------------------------------------------------*/ 678static int 679usb_fifo_open(struct usb_cdev_privdata *cpd, 680 struct usb_fifo *f, int fflags) 681{ 682 int err; 683 684 if (f == NULL) { 685 /* no FIFO there */ 686 DPRINTFN(2, "no FIFO\n"); 687 return (ENXIO); 688 } 689 /* remove FWRITE and FREAD flags */ 690 fflags &= ~(FWRITE | FREAD); 691 692 /* set correct file flags */ 693 if ((f->fifo_index & 1) == USB_FIFO_TX) { 694 fflags |= FWRITE; 695 } else { 696 fflags |= FREAD; 697 } 698 699 /* check if we are already opened */ 700 /* we don't need any locks when checking this variable */ 701 if (f->curr_cpd != NULL) { 702 err = EBUSY; 703 goto done; 704 } 705 706 /* reset short flag before open */ 707 f->flag_short = 0; 708 709 /* call open method */ 710 err = (f->methods->f_open) (f, fflags); 711 if (err) { 712 goto done; 713 } 714 mtx_lock(f->priv_mtx); 715 716 /* reset sleep flag */ 717 f->flag_sleeping = 0; 718 719 /* reset error flag */ 720 f->flag_iserror = 0; 721 722 /* reset complete flag */ 723 f->flag_iscomplete = 0; 724 725 /* reset select flag */ 726 f->flag_isselect = 0; 727 728 /* reset flushing flag */ 729 f->flag_flushing = 0; 730 731 /* reset ASYNC proc flag */ 732 f->async_p = NULL; 733 734 mtx_lock(&usb_ref_lock); 735 /* flag the fifo as opened to prevent others */ 736 f->curr_cpd = cpd; 737 mtx_unlock(&usb_ref_lock); 738 739 /* reset queue */ 740 usb_fifo_reset(f); 741 742 mtx_unlock(f->priv_mtx); 743done: 744 return (err); 745} 746 747/*------------------------------------------------------------------------* 748 * usb_fifo_reset 749 *------------------------------------------------------------------------*/ 750void 751usb_fifo_reset(struct usb_fifo *f) 752{ 753 struct usb_mbuf *m; 754 755 if (f == NULL) { 756 return; 757 } 758 while (1) { 759 USB_IF_DEQUEUE(&f->used_q, m); 760 if (m) { 761 USB_IF_ENQUEUE(&f->free_q, m); 762 } else { 763 break; 764 } 765 } 766 /* reset have fragment flag */ 767 f->flag_have_fragment = 0; 768} 769 770/*------------------------------------------------------------------------* 771 * usb_fifo_close 772 *------------------------------------------------------------------------*/ 773static void 774usb_fifo_close(struct usb_fifo *f, int fflags) 775{ 776 int err; 777 778 /* check if we are not opened */ 779 if (f->curr_cpd == NULL) { 780 /* nothing to do - already closed */ 781 return; 782 } 783 mtx_lock(f->priv_mtx); 784 785 /* clear current cdev private data pointer */ 786 mtx_lock(&usb_ref_lock); 787 f->curr_cpd = NULL; 788 mtx_unlock(&usb_ref_lock); 789 790 /* check if we are watched by kevent */ 791 KNOTE_LOCKED(&f->selinfo.si_note, 0); 792 793 /* check if we are selected */ 794 if (f->flag_isselect) { 795 selwakeup(&f->selinfo); 796 f->flag_isselect = 0; 797 } 798 /* check if a thread wants SIGIO */ 799 if (f->async_p != NULL) { 800 PROC_LOCK(f->async_p); 801 kern_psignal(f->async_p, SIGIO); 802 PROC_UNLOCK(f->async_p); 803 f->async_p = NULL; 804 } 805 /* remove FWRITE and FREAD flags */ 806 fflags &= ~(FWRITE | FREAD); 807 808 /* flush written data, if any */ 809 if ((f->fifo_index & 1) == USB_FIFO_TX) { 810 811 if (!f->flag_iserror) { 812 813 /* set flushing flag */ 814 f->flag_flushing = 1; 815 816 /* get the last packet in */ 817 if (f->flag_have_fragment) { 818 struct usb_mbuf *m; 819 f->flag_have_fragment = 0; 820 USB_IF_DEQUEUE(&f->free_q, m); 821 if (m) { 822 USB_IF_ENQUEUE(&f->used_q, m); 823 } 824 } 825 826 /* start write transfer, if not already started */ 827 (f->methods->f_start_write) (f); 828 829 /* check if flushed already */ 830 while (f->flag_flushing && 831 (!f->flag_iserror)) { 832 /* wait until all data has been written */ 833 f->flag_sleeping = 1; 834 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx, 835 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT)); 836 if (err) { 837 DPRINTF("signal received\n"); 838 break; 839 } 840 } 841 } 842 fflags |= FWRITE; 843 844 /* stop write transfer, if not already stopped */ 845 (f->methods->f_stop_write) (f); 846 } else { 847 fflags |= FREAD; 848 849 /* stop write transfer, if not already stopped */ 850 (f->methods->f_stop_read) (f); 851 } 852 853 /* check if we are sleeping */ 854 if (f->flag_sleeping) { 855 DPRINTFN(2, "Sleeping at close!\n"); 856 } 857 mtx_unlock(f->priv_mtx); 858 859 /* call close method */ 860 (f->methods->f_close) (f, fflags); 861 862 DPRINTF("closed\n"); 863} 864 865/*------------------------------------------------------------------------* 866 * usb_open - cdev callback 867 *------------------------------------------------------------------------*/ 868static int 869usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td) 870{ 871 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1; 872 struct usb_cdev_refdata refs; 873 struct usb_cdev_privdata *cpd; 874 int err, ep; 875 876 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags); 877 878 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags")); 879 if (((fflags & FREAD) && !(pd->mode & FREAD)) || 880 ((fflags & FWRITE) && !(pd->mode & FWRITE))) { 881 DPRINTFN(2, "access mode not supported\n"); 882 return (EPERM); 883 } 884 885 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO); 886 ep = cpd->ep_addr = pd->ep_addr; 887 888 usb_loc_fill(pd, cpd); 889 err = usb_ref_device(cpd, &refs, 1); 890 if (err) { 891 DPRINTFN(2, "cannot ref device\n"); 892 free(cpd, M_USBDEV); 893 return (ENXIO); 894 } 895 cpd->fflags = fflags; /* access mode for open lifetime */ 896 897 /* create FIFOs, if any */ 898 err = usb_fifo_create(cpd, &refs); 899 /* check for error */ 900 if (err) { 901 DPRINTFN(2, "cannot create fifo\n"); 902 usb_unref_device(cpd, &refs); 903 free(cpd, M_USBDEV); 904 return (err); 905 } 906 if (fflags & FREAD) { 907 err = usb_fifo_open(cpd, refs.rxfifo, fflags); 908 if (err) { 909 DPRINTFN(2, "read open failed\n"); 910 usb_unref_device(cpd, &refs); 911 free(cpd, M_USBDEV); 912 return (err); 913 } 914 } 915 if (fflags & FWRITE) { 916 err = usb_fifo_open(cpd, refs.txfifo, fflags); 917 if (err) { 918 DPRINTFN(2, "write open failed\n"); 919 if (fflags & FREAD) { 920 usb_fifo_close(refs.rxfifo, fflags); 921 } 922 usb_unref_device(cpd, &refs); 923 free(cpd, M_USBDEV); 924 return (err); 925 } 926 } 927 usb_unref_device(cpd, &refs); 928 devfs_set_cdevpriv(cpd, usb_close); 929 930 return (0); 931} 932 933/*------------------------------------------------------------------------* 934 * usb_close - cdev callback 935 *------------------------------------------------------------------------*/ 936static void 937usb_close(void *arg) 938{ 939 struct usb_cdev_refdata refs; 940 struct usb_cdev_privdata *cpd = arg; 941 int err; 942 943 DPRINTFN(2, "cpd=%p\n", cpd); 944 945 err = usb_ref_device(cpd, &refs, 946 2 /* uref and allow detached state */); 947 if (err) { 948 DPRINTFN(2, "Cannot grab USB reference when " 949 "closing USB file handle\n"); 950 goto done; 951 } 952 if (cpd->fflags & FREAD) { 953 usb_fifo_close(refs.rxfifo, cpd->fflags); 954 } 955 if (cpd->fflags & FWRITE) { 956 usb_fifo_close(refs.txfifo, cpd->fflags); 957 } 958 usb_unref_device(cpd, &refs); 959done: 960 free(cpd, M_USBDEV); 961} 962 963static void 964usb_dev_init(void *arg) 965{ 966 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF); 967 sx_init(&usb_sym_lock, "USB sym mutex"); 968 TAILQ_INIT(&usb_sym_head); 969 970 /* check the UGEN methods */ 971 usb_fifo_check_methods(&usb_ugen_methods); 972} 973 974SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL); 975 976static void 977usb_dev_init_post(void *arg) 978{ 979 /* 980 * Create /dev/usb - this is needed for usbconfig(8), which 981 * needs a well-known device name to access. 982 */ 983 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR, 984 0644, USB_DEVICE_NAME); 985 if (usb_dev == NULL) { 986 DPRINTFN(0, "Could not create usb bus device\n"); 987 } 988} 989 990SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL); 991 992static void 993usb_dev_uninit(void *arg) 994{ 995 if (usb_dev != NULL) { 996 destroy_dev(usb_dev); 997 usb_dev = NULL; 998 } 999 mtx_destroy(&usb_ref_lock); 1000 sx_destroy(&usb_sym_lock); 1001} 1002 1003SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL); 1004 1005static int 1006usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr, 1007 struct thread *td) 1008{ 1009 int error = 0; 1010 1011 switch (cmd) { 1012 case FIODTYPE: 1013 *(int *)addr = 0; /* character device */ 1014 break; 1015 1016 case FIONBIO: 1017 /* handled by upper FS layer */ 1018 break; 1019 1020 case FIOASYNC: 1021 if (*(int *)addr) { 1022 if (f->async_p != NULL) { 1023 error = EBUSY; 1024 break; 1025 } 1026 f->async_p = USB_TD_GET_PROC(td); 1027 } else { 1028 f->async_p = NULL; 1029 } 1030 break; 1031 1032 /* XXX this is not the most general solution */ 1033 case TIOCSPGRP: 1034 if (f->async_p == NULL) { 1035 error = EINVAL; 1036 break; 1037 } 1038 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) { 1039 error = EPERM; 1040 break; 1041 } 1042 break; 1043 default: 1044 return (ENOIOCTL); 1045 } 1046 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error); 1047 return (error); 1048} 1049 1050/*------------------------------------------------------------------------* 1051 * usb_ioctl - cdev callback 1052 *------------------------------------------------------------------------*/ 1053static int 1054usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td) 1055{ 1056 struct usb_cdev_refdata refs; 1057 struct usb_cdev_privdata* cpd; 1058 struct usb_fifo *f; 1059 int fflags; 1060 int err; 1061 1062 DPRINTFN(2, "cmd=0x%lx\n", cmd); 1063 1064 err = devfs_get_cdevpriv((void **)&cpd); 1065 if (err != 0) 1066 return (err); 1067 1068 /* 1069 * Performance optimisation: We try to check for IOCTL's that 1070 * don't need the USB reference first. Then we grab the USB 1071 * reference if we need it! 1072 */ 1073 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1074 if (err) 1075 return (ENXIO); 1076 1077 fflags = cpd->fflags; 1078 1079 f = NULL; /* set default value */ 1080 err = ENOIOCTL; /* set default value */ 1081 1082 if (fflags & FWRITE) { 1083 f = refs.txfifo; 1084 err = usb_ioctl_f_sub(f, cmd, addr, td); 1085 } 1086 if (fflags & FREAD) { 1087 f = refs.rxfifo; 1088 err = usb_ioctl_f_sub(f, cmd, addr, td); 1089 } 1090 KASSERT(f != NULL, ("fifo not found")); 1091 if (err != ENOIOCTL) 1092 goto done; 1093 1094 err = (f->methods->f_ioctl) (f, cmd, addr, fflags); 1095 1096 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err); 1097 1098 if (err != ENOIOCTL) 1099 goto done; 1100 1101 if (usb_usb_ref_device(cpd, &refs)) { 1102 /* we lost the reference */ 1103 return (ENXIO); 1104 } 1105 1106 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags); 1107 1108 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err); 1109 1110 if (err == ENOIOCTL) 1111 err = ENOTTY; 1112 1113 if (err) 1114 goto done; 1115 1116 /* Wait for re-enumeration, if any */ 1117 1118 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) { 1119 1120 usb_unref_device(cpd, &refs); 1121 1122 usb_pause_mtx(NULL, hz / 128); 1123 1124 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) { 1125 if (usb_ref_device(cpd, &refs, 0)) { 1126 /* device no longer exists */ 1127 return (ENXIO); 1128 } 1129 usb_unref_device(cpd, &refs); 1130 usb_pause_mtx(NULL, hz / 128); 1131 } 1132 } 1133 1134done: 1135 usb_unref_device(cpd, &refs); 1136 return (err); 1137} 1138 1139static void 1140usb_filter_detach(struct knote *kn) 1141{ 1142 struct usb_fifo *f = kn->kn_hook; 1143 knlist_remove(&f->selinfo.si_note, kn, 0); 1144} 1145 1146static int 1147usb_filter_write(struct knote *kn, long hint) 1148{ 1149 struct usb_cdev_privdata* cpd; 1150 struct usb_fifo *f; 1151 struct usb_mbuf *m; 1152 1153 DPRINTFN(2, "\n"); 1154 1155 f = kn->kn_hook; 1156 1157 mtx_assert(f->priv_mtx, MA_OWNED); 1158 1159 cpd = f->curr_cpd; 1160 if (cpd == NULL) { 1161 m = (void *)1; 1162 } else if (f->fs_ep_max == 0) { 1163 if (f->flag_iserror) { 1164 /* we got an error */ 1165 m = (void *)1; 1166 } else { 1167 if (f->queue_data == NULL) { 1168 /* 1169 * start write transfer, if not 1170 * already started 1171 */ 1172 (f->methods->f_start_write) (f); 1173 } 1174 /* check if any packets are available */ 1175 USB_IF_POLL(&f->free_q, m); 1176 } 1177 } else { 1178 if (f->flag_iscomplete) { 1179 m = (void *)1; 1180 } else { 1181 m = NULL; 1182 } 1183 } 1184 return (m ? 1 : 0); 1185} 1186 1187static int 1188usb_filter_read(struct knote *kn, long hint) 1189{ 1190 struct usb_cdev_privdata* cpd; 1191 struct usb_fifo *f; 1192 struct usb_mbuf *m; 1193 1194 DPRINTFN(2, "\n"); 1195 1196 f = kn->kn_hook; 1197 1198 mtx_assert(f->priv_mtx, MA_OWNED); 1199 1200 cpd = f->curr_cpd; 1201 if (cpd == NULL) { 1202 m = (void *)1; 1203 } else if (f->fs_ep_max == 0) { 1204 if (f->flag_iserror) { 1205 /* we have an error */ 1206 m = (void *)1; 1207 } else { 1208 if (f->queue_data == NULL) { 1209 /* 1210 * start read transfer, if not 1211 * already started 1212 */ 1213 (f->methods->f_start_read) (f); 1214 } 1215 /* check if any packets are available */ 1216 USB_IF_POLL(&f->used_q, m); 1217 1218 /* start reading data, if any */ 1219 if (m == NULL) 1220 (f->methods->f_start_read) (f); 1221 } 1222 } else { 1223 if (f->flag_iscomplete) { 1224 m = (void *)1; 1225 } else { 1226 m = NULL; 1227 } 1228 } 1229 return (m ? 1 : 0); 1230} 1231 1232static struct filterops usb_filtops_write = { 1233 .f_isfd = 1, 1234 .f_detach = usb_filter_detach, 1235 .f_event = usb_filter_write, 1236}; 1237 1238static struct filterops usb_filtops_read = { 1239 .f_isfd = 1, 1240 .f_detach = usb_filter_detach, 1241 .f_event = usb_filter_read, 1242}; 1243 1244 1245/* ARGSUSED */ 1246static int 1247usb_kqfilter(struct cdev* dev, struct knote *kn) 1248{ 1249 struct usb_cdev_refdata refs; 1250 struct usb_cdev_privdata* cpd; 1251 struct usb_fifo *f; 1252 int fflags; 1253 int err = EINVAL; 1254 1255 DPRINTFN(2, "\n"); 1256 1257 if (devfs_get_cdevpriv((void **)&cpd) != 0 || 1258 usb_ref_device(cpd, &refs, 0) != 0) 1259 return (ENXIO); 1260 1261 fflags = cpd->fflags; 1262 1263 /* Figure out who needs service */ 1264 switch (kn->kn_filter) { 1265 case EVFILT_WRITE: 1266 if (fflags & FWRITE) { 1267 f = refs.txfifo; 1268 kn->kn_fop = &usb_filtops_write; 1269 err = 0; 1270 } 1271 break; 1272 case EVFILT_READ: 1273 if (fflags & FREAD) { 1274 f = refs.rxfifo; 1275 kn->kn_fop = &usb_filtops_read; 1276 err = 0; 1277 } 1278 break; 1279 default: 1280 err = EOPNOTSUPP; 1281 break; 1282 } 1283 1284 if (err == 0) { 1285 kn->kn_hook = f; 1286 mtx_lock(f->priv_mtx); 1287 knlist_add(&f->selinfo.si_note, kn, 1); 1288 mtx_unlock(f->priv_mtx); 1289 } 1290 1291 usb_unref_device(cpd, &refs); 1292 return (err); 1293} 1294 1295/* ARGSUSED */ 1296static int 1297usb_poll(struct cdev* dev, int events, struct thread* td) 1298{ 1299 struct usb_cdev_refdata refs; 1300 struct usb_cdev_privdata* cpd; 1301 struct usb_fifo *f; 1302 struct usb_mbuf *m; 1303 int fflags, revents; 1304 1305 if (devfs_get_cdevpriv((void **)&cpd) != 0 || 1306 usb_ref_device(cpd, &refs, 0) != 0) 1307 return (events & 1308 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 1309 1310 fflags = cpd->fflags; 1311 1312 /* Figure out who needs service */ 1313 revents = 0; 1314 if ((events & (POLLOUT | POLLWRNORM)) && 1315 (fflags & FWRITE)) { 1316 1317 f = refs.txfifo; 1318 1319 mtx_lock(f->priv_mtx); 1320 1321 if (!refs.is_usbfs) { 1322 if (f->flag_iserror) { 1323 /* we got an error */ 1324 m = (void *)1; 1325 } else { 1326 if (f->queue_data == NULL) { 1327 /* 1328 * start write transfer, if not 1329 * already started 1330 */ 1331 (f->methods->f_start_write) (f); 1332 } 1333 /* check if any packets are available */ 1334 USB_IF_POLL(&f->free_q, m); 1335 } 1336 } else { 1337 if (f->flag_iscomplete) { 1338 m = (void *)1; 1339 } else { 1340 m = NULL; 1341 } 1342 } 1343 1344 if (m) { 1345 revents |= events & (POLLOUT | POLLWRNORM); 1346 } else { 1347 f->flag_isselect = 1; 1348 selrecord(td, &f->selinfo); 1349 } 1350 1351 mtx_unlock(f->priv_mtx); 1352 } 1353 if ((events & (POLLIN | POLLRDNORM)) && 1354 (fflags & FREAD)) { 1355 1356 f = refs.rxfifo; 1357 1358 mtx_lock(f->priv_mtx); 1359 1360 if (!refs.is_usbfs) { 1361 if (f->flag_iserror) { 1362 /* we have an error */ 1363 m = (void *)1; 1364 } else { 1365 if (f->queue_data == NULL) { 1366 /* 1367 * start read transfer, if not 1368 * already started 1369 */ 1370 (f->methods->f_start_read) (f); 1371 } 1372 /* check if any packets are available */ 1373 USB_IF_POLL(&f->used_q, m); 1374 } 1375 } else { 1376 if (f->flag_iscomplete) { 1377 m = (void *)1; 1378 } else { 1379 m = NULL; 1380 } 1381 } 1382 1383 if (m) { 1384 revents |= events & (POLLIN | POLLRDNORM); 1385 } else { 1386 f->flag_isselect = 1; 1387 selrecord(td, &f->selinfo); 1388 1389 if (!refs.is_usbfs) { 1390 /* start reading data */ 1391 (f->methods->f_start_read) (f); 1392 } 1393 } 1394 1395 mtx_unlock(f->priv_mtx); 1396 } 1397 usb_unref_device(cpd, &refs); 1398 return (revents); 1399} 1400 1401static int 1402usb_read(struct cdev *dev, struct uio *uio, int ioflag) 1403{ 1404 struct usb_cdev_refdata refs; 1405 struct usb_cdev_privdata* cpd; 1406 struct usb_fifo *f; 1407 struct usb_mbuf *m; 1408 int fflags; 1409 int resid; 1410 int io_len; 1411 int err; 1412 uint8_t tr_data = 0; 1413 1414 err = devfs_get_cdevpriv((void **)&cpd); 1415 if (err != 0) 1416 return (err); 1417 1418 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1419 if (err) 1420 return (ENXIO); 1421 1422 fflags = cpd->fflags; 1423 1424 f = refs.rxfifo; 1425 if (f == NULL) { 1426 /* should not happen */ 1427 usb_unref_device(cpd, &refs); 1428 return (EPERM); 1429 } 1430 1431 resid = uio->uio_resid; 1432 1433 mtx_lock(f->priv_mtx); 1434 1435 /* check for permanent read error */ 1436 if (f->flag_iserror) { 1437 err = EIO; 1438 goto done; 1439 } 1440 /* check if USB-FS interface is active */ 1441 if (refs.is_usbfs) { 1442 /* 1443 * The queue is used for events that should be 1444 * retrieved using the "USB_FS_COMPLETE" ioctl. 1445 */ 1446 err = EINVAL; 1447 goto done; 1448 } 1449 while (uio->uio_resid > 0) { 1450 1451 USB_IF_DEQUEUE(&f->used_q, m); 1452 1453 if (m == NULL) { 1454 1455 /* start read transfer, if not already started */ 1456 1457 (f->methods->f_start_read) (f); 1458 1459 if (ioflag & IO_NDELAY) { 1460 if (tr_data) { 1461 /* return length before error */ 1462 break; 1463 } 1464 err = EWOULDBLOCK; 1465 break; 1466 } 1467 DPRINTF("sleeping\n"); 1468 1469 err = usb_fifo_wait(f); 1470 if (err) { 1471 break; 1472 } 1473 continue; 1474 } 1475 if (f->methods->f_filter_read) { 1476 /* 1477 * Sometimes it is convenient to process data at the 1478 * expense of a userland process instead of a kernel 1479 * process. 1480 */ 1481 (f->methods->f_filter_read) (f, m); 1482 } 1483 tr_data = 1; 1484 1485 io_len = MIN(m->cur_data_len, uio->uio_resid); 1486 1487 DPRINTFN(2, "transfer %d bytes from %p\n", 1488 io_len, m->cur_data_ptr); 1489 1490 err = usb_fifo_uiomove(f, 1491 m->cur_data_ptr, io_len, uio); 1492 1493 m->cur_data_len -= io_len; 1494 m->cur_data_ptr += io_len; 1495 1496 if (m->cur_data_len == 0) { 1497 1498 uint8_t last_packet; 1499 1500 last_packet = m->last_packet; 1501 1502 USB_IF_ENQUEUE(&f->free_q, m); 1503 1504 if (last_packet) { 1505 /* keep framing */ 1506 break; 1507 } 1508 } else { 1509 USB_IF_PREPEND(&f->used_q, m); 1510 } 1511 1512 if (err) { 1513 break; 1514 } 1515 } 1516done: 1517 mtx_unlock(f->priv_mtx); 1518 1519 usb_unref_device(cpd, &refs); 1520 1521 return (err); 1522} 1523 1524static int 1525usb_write(struct cdev *dev, struct uio *uio, int ioflag) 1526{ 1527 struct usb_cdev_refdata refs; 1528 struct usb_cdev_privdata* cpd; 1529 struct usb_fifo *f; 1530 struct usb_mbuf *m; 1531 uint8_t *pdata; 1532 int fflags; 1533 int resid; 1534 int io_len; 1535 int err; 1536 uint8_t tr_data = 0; 1537 1538 DPRINTFN(2, "\n"); 1539 1540 err = devfs_get_cdevpriv((void **)&cpd); 1541 if (err != 0) 1542 return (err); 1543 1544 err = usb_ref_device(cpd, &refs, 0 /* no uref */ ); 1545 if (err) 1546 return (ENXIO); 1547 1548 fflags = cpd->fflags; 1549 1550 f = refs.txfifo; 1551 if (f == NULL) { 1552 /* should not happen */ 1553 usb_unref_device(cpd, &refs); 1554 return (EPERM); 1555 } 1556 resid = uio->uio_resid; 1557 1558 mtx_lock(f->priv_mtx); 1559 1560 /* check for permanent write error */ 1561 if (f->flag_iserror) { 1562 err = EIO; 1563 goto done; 1564 } 1565 /* check if USB-FS interface is active */ 1566 if (refs.is_usbfs) { 1567 /* 1568 * The queue is used for events that should be 1569 * retrieved using the "USB_FS_COMPLETE" ioctl. 1570 */ 1571 err = EINVAL; 1572 goto done; 1573 } 1574 if (f->queue_data == NULL) { 1575 /* start write transfer, if not already started */ 1576 (f->methods->f_start_write) (f); 1577 } 1578 /* we allow writing zero length data */ 1579 do { 1580 USB_IF_DEQUEUE(&f->free_q, m); 1581 1582 if (m == NULL) { 1583 1584 if (ioflag & IO_NDELAY) { 1585 if (tr_data) { 1586 /* return length before error */ 1587 break; 1588 } 1589 err = EWOULDBLOCK; 1590 break; 1591 } 1592 DPRINTF("sleeping\n"); 1593 1594 err = usb_fifo_wait(f); 1595 if (err) { 1596 break; 1597 } 1598 continue; 1599 } 1600 tr_data = 1; 1601 1602 if (f->flag_have_fragment == 0) { 1603 USB_MBUF_RESET(m); 1604 io_len = m->cur_data_len; 1605 pdata = m->cur_data_ptr; 1606 if (io_len > uio->uio_resid) 1607 io_len = uio->uio_resid; 1608 m->cur_data_len = io_len; 1609 } else { 1610 io_len = m->max_data_len - m->cur_data_len; 1611 pdata = m->cur_data_ptr + m->cur_data_len; 1612 if (io_len > uio->uio_resid) 1613 io_len = uio->uio_resid; 1614 m->cur_data_len += io_len; 1615 } 1616 1617 DPRINTFN(2, "transfer %d bytes to %p\n", 1618 io_len, pdata); 1619 1620 err = usb_fifo_uiomove(f, pdata, io_len, uio); 1621 1622 if (err) { 1623 f->flag_have_fragment = 0; 1624 USB_IF_ENQUEUE(&f->free_q, m); 1625 break; 1626 } 1627 1628 /* check if the buffer is ready to be transmitted */ 1629 1630 if ((f->flag_write_defrag == 0) || 1631 (m->cur_data_len == m->max_data_len)) { 1632 f->flag_have_fragment = 0; 1633 1634 /* 1635 * Check for write filter: 1636 * 1637 * Sometimes it is convenient to process data 1638 * at the expense of a userland process 1639 * instead of a kernel process. 1640 */ 1641 if (f->methods->f_filter_write) { 1642 (f->methods->f_filter_write) (f, m); 1643 } 1644 1645 /* Put USB mbuf in the used queue */ 1646 USB_IF_ENQUEUE(&f->used_q, m); 1647 1648 /* Start writing data, if not already started */ 1649 (f->methods->f_start_write) (f); 1650 } else { 1651 /* Wait for more data or close */ 1652 f->flag_have_fragment = 1; 1653 USB_IF_PREPEND(&f->free_q, m); 1654 } 1655 1656 } while (uio->uio_resid > 0); 1657done: 1658 mtx_unlock(f->priv_mtx); 1659 1660 usb_unref_device(cpd, &refs); 1661 1662 return (err); 1663} 1664 1665int 1666usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 1667 struct thread *td) 1668{ 1669 union { 1670 struct usb_read_dir *urd; 1671 void* data; 1672 } u; 1673 int err; 1674 1675 u.data = data; 1676 switch (cmd) { 1677 case USB_READ_DIR: 1678 err = usb_read_symlink(u.urd->urd_data, 1679 u.urd->urd_startentry, u.urd->urd_maxlen); 1680 break; 1681 case USB_DEV_QUIRK_GET: 1682 case USB_QUIRK_NAME_GET: 1683 case USB_DEV_QUIRK_ADD: 1684 case USB_DEV_QUIRK_REMOVE: 1685 err = usb_quirk_ioctl_p(cmd, data, fflag, td); 1686 break; 1687 case USB_GET_TEMPLATE: 1688 *(int *)data = usb_template; 1689 err = 0; 1690 break; 1691 case USB_SET_TEMPLATE: 1692 err = priv_check(curthread, PRIV_DRIVER); 1693 if (err) 1694 break; 1695 usb_template = *(int *)data; 1696 break; 1697 default: 1698 err = ENOTTY; 1699 break; 1700 } 1701 return (err); 1702} 1703 1704static int 1705usb_fifo_uiomove(struct usb_fifo *f, void *cp, 1706 int n, struct uio *uio) 1707{ 1708 int error; 1709 1710 mtx_unlock(f->priv_mtx); 1711 1712 /* 1713 * "uiomove()" can sleep so one needs to make a wrapper, 1714 * exiting the mutex and checking things: 1715 */ 1716 error = uiomove(cp, n, uio); 1717 1718 mtx_lock(f->priv_mtx); 1719 1720 return (error); 1721} 1722 1723int 1724usb_fifo_wait(struct usb_fifo *f) 1725{ 1726 int err; 1727 1728 mtx_assert(f->priv_mtx, MA_OWNED); 1729 1730 if (f->flag_iserror) { 1731 /* we are gone */ 1732 return (EIO); 1733 } 1734 f->flag_sleeping = 1; 1735 1736 err = cv_wait_sig(&f->cv_io, f->priv_mtx); 1737 1738 if (f->flag_iserror) { 1739 /* we are gone */ 1740 err = EIO; 1741 } 1742 return (err); 1743} 1744 1745void 1746usb_fifo_signal(struct usb_fifo *f) 1747{ 1748 if (f->flag_sleeping) { 1749 f->flag_sleeping = 0; 1750 cv_broadcast(&f->cv_io); 1751 } 1752} 1753 1754void 1755usb_fifo_wakeup(struct usb_fifo *f) 1756{ 1757 usb_fifo_signal(f); 1758 1759 KNOTE_LOCKED(&f->selinfo.si_note, 0); 1760 1761 if (f->flag_isselect) { 1762 selwakeup(&f->selinfo); 1763 f->flag_isselect = 0; 1764 } 1765 if (f->async_p != NULL) { 1766 PROC_LOCK(f->async_p); 1767 kern_psignal(f->async_p, SIGIO); 1768 PROC_UNLOCK(f->async_p); 1769 } 1770} 1771 1772static int 1773usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags) 1774{ 1775 return (0); 1776} 1777 1778static void 1779usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags) 1780{ 1781 return; 1782} 1783 1784static int 1785usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 1786{ 1787 return (ENOIOCTL); 1788} 1789 1790static void 1791usb_fifo_dummy_cmd(struct usb_fifo *fifo) 1792{ 1793 fifo->flag_flushing = 0; /* not flushing */ 1794} 1795 1796static void 1797usb_fifo_check_methods(struct usb_fifo_methods *pm) 1798{ 1799 /* check that all callback functions are OK */ 1800 1801 if (pm->f_open == NULL) 1802 pm->f_open = &usb_fifo_dummy_open; 1803 1804 if (pm->f_close == NULL) 1805 pm->f_close = &usb_fifo_dummy_close; 1806 1807 if (pm->f_ioctl == NULL) 1808 pm->f_ioctl = &usb_fifo_dummy_ioctl; 1809 1810 if (pm->f_ioctl_post == NULL) 1811 pm->f_ioctl_post = &usb_fifo_dummy_ioctl; 1812 1813 if (pm->f_start_read == NULL) 1814 pm->f_start_read = &usb_fifo_dummy_cmd; 1815 1816 if (pm->f_stop_read == NULL) 1817 pm->f_stop_read = &usb_fifo_dummy_cmd; 1818 1819 if (pm->f_start_write == NULL) 1820 pm->f_start_write = &usb_fifo_dummy_cmd; 1821 1822 if (pm->f_stop_write == NULL) 1823 pm->f_stop_write = &usb_fifo_dummy_cmd; 1824} 1825 1826/*------------------------------------------------------------------------* 1827 * usb_fifo_attach 1828 * 1829 * The following function will create a duplex FIFO. 1830 * 1831 * Return values: 1832 * 0: Success. 1833 * Else: Failure. 1834 *------------------------------------------------------------------------*/ 1835int 1836usb_fifo_attach(struct usb_device *udev, void *priv_sc, 1837 struct mtx *priv_mtx, struct usb_fifo_methods *pm, 1838 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit, 1839 uint8_t iface_index, uid_t uid, gid_t gid, int mode) 1840{ 1841 struct usb_fifo *f_tx; 1842 struct usb_fifo *f_rx; 1843 char devname[32]; 1844 uint8_t n; 1845 1846 f_sc->fp[USB_FIFO_TX] = NULL; 1847 f_sc->fp[USB_FIFO_RX] = NULL; 1848 1849 if (pm == NULL) 1850 return (EINVAL); 1851 1852 /* check the methods */ 1853 usb_fifo_check_methods(pm); 1854 1855 if (priv_mtx == NULL) 1856 priv_mtx = &Giant; 1857 1858 /* search for a free FIFO slot */ 1859 for (n = 0;; n += 2) { 1860 1861 if (n == USB_FIFO_MAX) { 1862 /* end of FIFOs reached */ 1863 return (ENOMEM); 1864 } 1865 /* Check for TX FIFO */ 1866 if (udev->fifo[n + USB_FIFO_TX] != NULL) { 1867 continue; 1868 } 1869 /* Check for RX FIFO */ 1870 if (udev->fifo[n + USB_FIFO_RX] != NULL) { 1871 continue; 1872 } 1873 break; 1874 } 1875 1876 f_tx = usb_fifo_alloc(priv_mtx); 1877 f_rx = usb_fifo_alloc(priv_mtx); 1878 1879 if ((f_tx == NULL) || (f_rx == NULL)) { 1880 usb_fifo_free(f_tx); 1881 usb_fifo_free(f_rx); 1882 return (ENOMEM); 1883 } 1884 /* initialise FIFO structures */ 1885 1886 f_tx->fifo_index = n + USB_FIFO_TX; 1887 f_tx->dev_ep_index = -1; 1888 f_tx->priv_sc0 = priv_sc; 1889 f_tx->methods = pm; 1890 f_tx->iface_index = iface_index; 1891 f_tx->udev = udev; 1892 1893 f_rx->fifo_index = n + USB_FIFO_RX; 1894 f_rx->dev_ep_index = -1; 1895 f_rx->priv_sc0 = priv_sc; 1896 f_rx->methods = pm; 1897 f_rx->iface_index = iface_index; 1898 f_rx->udev = udev; 1899 1900 f_sc->fp[USB_FIFO_TX] = f_tx; 1901 f_sc->fp[USB_FIFO_RX] = f_rx; 1902 1903 mtx_lock(&usb_ref_lock); 1904 udev->fifo[f_tx->fifo_index] = f_tx; 1905 udev->fifo[f_rx->fifo_index] = f_rx; 1906 mtx_unlock(&usb_ref_lock); 1907 1908 for (n = 0; n != 4; n++) { 1909 1910 if (pm->basename[n] == NULL) { 1911 continue; 1912 } 1913 if (subunit < 0) { 1914 if (snprintf(devname, sizeof(devname), 1915 "%s%u%s", pm->basename[n], 1916 unit, pm->postfix[n] ? 1917 pm->postfix[n] : "")) { 1918 /* ignore */ 1919 } 1920 } else { 1921 if (snprintf(devname, sizeof(devname), 1922 "%s%u.%d%s", pm->basename[n], 1923 unit, subunit, pm->postfix[n] ? 1924 pm->postfix[n] : "")) { 1925 /* ignore */ 1926 } 1927 } 1928 1929 /* 1930 * Distribute the symbolic links into two FIFO structures: 1931 */ 1932 if (n & 1) { 1933 f_rx->symlink[n / 2] = 1934 usb_alloc_symlink(devname); 1935 } else { 1936 f_tx->symlink[n / 2] = 1937 usb_alloc_symlink(devname); 1938 } 1939 1940 /* Create the device */ 1941 f_sc->dev = usb_make_dev(udev, devname, -1, 1942 f_tx->fifo_index & f_rx->fifo_index, 1943 FREAD|FWRITE, uid, gid, mode); 1944 } 1945 1946 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx); 1947 return (0); 1948} 1949 1950/*------------------------------------------------------------------------* 1951 * usb_fifo_alloc_buffer 1952 * 1953 * Return values: 1954 * 0: Success 1955 * Else failure 1956 *------------------------------------------------------------------------*/ 1957int 1958usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize, 1959 uint16_t nbuf) 1960{ 1961 usb_fifo_free_buffer(f); 1962 1963 /* allocate an endpoint */ 1964 f->free_q.ifq_maxlen = nbuf; 1965 f->used_q.ifq_maxlen = nbuf; 1966 1967 f->queue_data = usb_alloc_mbufs( 1968 M_USBDEV, &f->free_q, bufsize, nbuf); 1969 1970 if ((f->queue_data == NULL) && bufsize && nbuf) { 1971 return (ENOMEM); 1972 } 1973 return (0); /* success */ 1974} 1975 1976/*------------------------------------------------------------------------* 1977 * usb_fifo_free_buffer 1978 * 1979 * This function will free the buffers associated with a FIFO. This 1980 * function can be called multiple times in a row. 1981 *------------------------------------------------------------------------*/ 1982void 1983usb_fifo_free_buffer(struct usb_fifo *f) 1984{ 1985 if (f->queue_data) { 1986 /* free old buffer */ 1987 free(f->queue_data, M_USBDEV); 1988 f->queue_data = NULL; 1989 } 1990 /* reset queues */ 1991 1992 memset(&f->free_q, 0, sizeof(f->free_q)); 1993 memset(&f->used_q, 0, sizeof(f->used_q)); 1994} 1995 1996void 1997usb_fifo_detach(struct usb_fifo_sc *f_sc) 1998{ 1999 if (f_sc == NULL) { 2000 return; 2001 } 2002 usb_fifo_free(f_sc->fp[USB_FIFO_TX]); 2003 usb_fifo_free(f_sc->fp[USB_FIFO_RX]); 2004 2005 f_sc->fp[USB_FIFO_TX] = NULL; 2006 f_sc->fp[USB_FIFO_RX] = NULL; 2007 2008 usb_destroy_dev(f_sc->dev); 2009 2010 f_sc->dev = NULL; 2011 2012 DPRINTFN(2, "detached %p\n", f_sc); 2013} 2014 2015usb_size_t 2016usb_fifo_put_bytes_max(struct usb_fifo *f) 2017{ 2018 struct usb_mbuf *m; 2019 usb_size_t len; 2020 2021 USB_IF_POLL(&f->free_q, m); 2022 2023 if (m) { 2024 len = m->max_data_len; 2025 } else { 2026 len = 0; 2027 } 2028 return (len); 2029} 2030 2031/*------------------------------------------------------------------------* 2032 * usb_fifo_put_data 2033 * 2034 * what: 2035 * 0 - normal operation 2036 * 1 - set last packet flag to enforce framing 2037 *------------------------------------------------------------------------*/ 2038void 2039usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc, 2040 usb_frlength_t offset, usb_frlength_t len, uint8_t what) 2041{ 2042 struct usb_mbuf *m; 2043 usb_frlength_t io_len; 2044 2045 while (len || (what == 1)) { 2046 2047 USB_IF_DEQUEUE(&f->free_q, m); 2048 2049 if (m) { 2050 USB_MBUF_RESET(m); 2051 2052 io_len = MIN(len, m->cur_data_len); 2053 2054 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len); 2055 2056 m->cur_data_len = io_len; 2057 offset += io_len; 2058 len -= io_len; 2059 2060 if ((len == 0) && (what == 1)) { 2061 m->last_packet = 1; 2062 } 2063 USB_IF_ENQUEUE(&f->used_q, m); 2064 2065 usb_fifo_wakeup(f); 2066 2067 if ((len == 0) || (what == 1)) { 2068 break; 2069 } 2070 } else { 2071 break; 2072 } 2073 } 2074} 2075 2076void 2077usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr, 2078 usb_size_t len, uint8_t what) 2079{ 2080 struct usb_mbuf *m; 2081 usb_size_t io_len; 2082 2083 while (len || (what == 1)) { 2084 2085 USB_IF_DEQUEUE(&f->free_q, m); 2086 2087 if (m) { 2088 USB_MBUF_RESET(m); 2089 2090 io_len = MIN(len, m->cur_data_len); 2091 2092 memcpy(m->cur_data_ptr, ptr, io_len); 2093 2094 m->cur_data_len = io_len; 2095 ptr = USB_ADD_BYTES(ptr, io_len); 2096 len -= io_len; 2097 2098 if ((len == 0) && (what == 1)) { 2099 m->last_packet = 1; 2100 } 2101 USB_IF_ENQUEUE(&f->used_q, m); 2102 2103 usb_fifo_wakeup(f); 2104 2105 if ((len == 0) || (what == 1)) { 2106 break; 2107 } 2108 } else { 2109 break; 2110 } 2111 } 2112} 2113 2114uint8_t 2115usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len) 2116{ 2117 struct usb_mbuf *m; 2118 2119 USB_IF_DEQUEUE(&f->free_q, m); 2120 2121 if (m) { 2122 m->cur_data_len = len; 2123 m->cur_data_ptr = ptr; 2124 USB_IF_ENQUEUE(&f->used_q, m); 2125 usb_fifo_wakeup(f); 2126 return (1); 2127 } 2128 return (0); 2129} 2130 2131void 2132usb_fifo_put_data_error(struct usb_fifo *f) 2133{ 2134 f->flag_iserror = 1; 2135 usb_fifo_wakeup(f); 2136} 2137 2138/*------------------------------------------------------------------------* 2139 * usb_fifo_get_data 2140 * 2141 * what: 2142 * 0 - normal operation 2143 * 1 - only get one "usb_mbuf" 2144 * 2145 * returns: 2146 * 0 - no more data 2147 * 1 - data in buffer 2148 *------------------------------------------------------------------------*/ 2149uint8_t 2150usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc, 2151 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen, 2152 uint8_t what) 2153{ 2154 struct usb_mbuf *m; 2155 usb_frlength_t io_len; 2156 uint8_t tr_data = 0; 2157 2158 actlen[0] = 0; 2159 2160 while (1) { 2161 2162 USB_IF_DEQUEUE(&f->used_q, m); 2163 2164 if (m) { 2165 2166 tr_data = 1; 2167 2168 io_len = MIN(len, m->cur_data_len); 2169 2170 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len); 2171 2172 len -= io_len; 2173 offset += io_len; 2174 actlen[0] += io_len; 2175 m->cur_data_ptr += io_len; 2176 m->cur_data_len -= io_len; 2177 2178 if ((m->cur_data_len == 0) || (what == 1)) { 2179 USB_IF_ENQUEUE(&f->free_q, m); 2180 2181 usb_fifo_wakeup(f); 2182 2183 if (what == 1) { 2184 break; 2185 } 2186 } else { 2187 USB_IF_PREPEND(&f->used_q, m); 2188 } 2189 } else { 2190 2191 if (tr_data) { 2192 /* wait for data to be written out */ 2193 break; 2194 } 2195 if (f->flag_flushing) { 2196 /* check if we should send a short packet */ 2197 if (f->flag_short != 0) { 2198 f->flag_short = 0; 2199 tr_data = 1; 2200 break; 2201 } 2202 /* flushing complete */ 2203 f->flag_flushing = 0; 2204 usb_fifo_wakeup(f); 2205 } 2206 break; 2207 } 2208 if (len == 0) { 2209 break; 2210 } 2211 } 2212 return (tr_data); 2213} 2214 2215uint8_t 2216usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr, 2217 usb_size_t len, usb_size_t *actlen, uint8_t what) 2218{ 2219 struct usb_mbuf *m; 2220 usb_size_t io_len; 2221 uint8_t tr_data = 0; 2222 2223 actlen[0] = 0; 2224 2225 while (1) { 2226 2227 USB_IF_DEQUEUE(&f->used_q, m); 2228 2229 if (m) { 2230 2231 tr_data = 1; 2232 2233 io_len = MIN(len, m->cur_data_len); 2234 2235 memcpy(ptr, m->cur_data_ptr, io_len); 2236 2237 len -= io_len; 2238 ptr = USB_ADD_BYTES(ptr, io_len); 2239 actlen[0] += io_len; 2240 m->cur_data_ptr += io_len; 2241 m->cur_data_len -= io_len; 2242 2243 if ((m->cur_data_len == 0) || (what == 1)) { 2244 USB_IF_ENQUEUE(&f->free_q, m); 2245 2246 usb_fifo_wakeup(f); 2247 2248 if (what == 1) { 2249 break; 2250 } 2251 } else { 2252 USB_IF_PREPEND(&f->used_q, m); 2253 } 2254 } else { 2255 2256 if (tr_data) { 2257 /* wait for data to be written out */ 2258 break; 2259 } 2260 if (f->flag_flushing) { 2261 /* check if we should send a short packet */ 2262 if (f->flag_short != 0) { 2263 f->flag_short = 0; 2264 tr_data = 1; 2265 break; 2266 } 2267 /* flushing complete */ 2268 f->flag_flushing = 0; 2269 usb_fifo_wakeup(f); 2270 } 2271 break; 2272 } 2273 if (len == 0) { 2274 break; 2275 } 2276 } 2277 return (tr_data); 2278} 2279 2280uint8_t 2281usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen) 2282{ 2283 struct usb_mbuf *m; 2284 2285 USB_IF_POLL(&f->used_q, m); 2286 2287 if (m) { 2288 *plen = m->cur_data_len; 2289 *pptr = m->cur_data_ptr; 2290 2291 return (1); 2292 } 2293 return (0); 2294} 2295 2296void 2297usb_fifo_get_data_error(struct usb_fifo *f) 2298{ 2299 f->flag_iserror = 1; 2300 usb_fifo_wakeup(f); 2301} 2302 2303/*------------------------------------------------------------------------* 2304 * usb_alloc_symlink 2305 * 2306 * Return values: 2307 * NULL: Failure 2308 * Else: Pointer to symlink entry 2309 *------------------------------------------------------------------------*/ 2310struct usb_symlink * 2311usb_alloc_symlink(const char *target) 2312{ 2313 struct usb_symlink *ps; 2314 2315 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK); 2316 if (ps == NULL) { 2317 return (ps); 2318 } 2319 /* XXX no longer needed */ 2320 strlcpy(ps->src_path, target, sizeof(ps->src_path)); 2321 ps->src_len = strlen(ps->src_path); 2322 strlcpy(ps->dst_path, target, sizeof(ps->dst_path)); 2323 ps->dst_len = strlen(ps->dst_path); 2324 2325 sx_xlock(&usb_sym_lock); 2326 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry); 2327 sx_unlock(&usb_sym_lock); 2328 return (ps); 2329} 2330 2331/*------------------------------------------------------------------------* 2332 * usb_free_symlink 2333 *------------------------------------------------------------------------*/ 2334void 2335usb_free_symlink(struct usb_symlink *ps) 2336{ 2337 if (ps == NULL) { 2338 return; 2339 } 2340 sx_xlock(&usb_sym_lock); 2341 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry); 2342 sx_unlock(&usb_sym_lock); 2343 2344 free(ps, M_USBDEV); 2345} 2346 2347/*------------------------------------------------------------------------* 2348 * usb_read_symlink 2349 * 2350 * Return value: 2351 * 0: Success 2352 * Else: Failure 2353 *------------------------------------------------------------------------*/ 2354int 2355usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len) 2356{ 2357 struct usb_symlink *ps; 2358 uint32_t temp; 2359 uint32_t delta = 0; 2360 uint8_t len; 2361 int error = 0; 2362 2363 sx_xlock(&usb_sym_lock); 2364 2365 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) { 2366 2367 /* 2368 * Compute total length of source and destination symlink 2369 * strings pluss one length byte and two NUL bytes: 2370 */ 2371 temp = ps->src_len + ps->dst_len + 3; 2372 2373 if (temp > 255) { 2374 /* 2375 * Skip entry because this length cannot fit 2376 * into one byte: 2377 */ 2378 continue; 2379 } 2380 if (startentry != 0) { 2381 /* decrement read offset */ 2382 startentry--; 2383 continue; 2384 } 2385 if (temp > user_len) { 2386 /* out of buffer space */ 2387 break; 2388 } 2389 len = temp; 2390 2391 /* copy out total length */ 2392 2393 error = copyout(&len, 2394 USB_ADD_BYTES(user_ptr, delta), 1); 2395 if (error) { 2396 break; 2397 } 2398 delta += 1; 2399 2400 /* copy out source string */ 2401 2402 error = copyout(ps->src_path, 2403 USB_ADD_BYTES(user_ptr, delta), ps->src_len); 2404 if (error) { 2405 break; 2406 } 2407 len = 0; 2408 delta += ps->src_len; 2409 error = copyout(&len, 2410 USB_ADD_BYTES(user_ptr, delta), 1); 2411 if (error) { 2412 break; 2413 } 2414 delta += 1; 2415 2416 /* copy out destination string */ 2417 2418 error = copyout(ps->dst_path, 2419 USB_ADD_BYTES(user_ptr, delta), ps->dst_len); 2420 if (error) { 2421 break; 2422 } 2423 len = 0; 2424 delta += ps->dst_len; 2425 error = copyout(&len, 2426 USB_ADD_BYTES(user_ptr, delta), 1); 2427 if (error) { 2428 break; 2429 } 2430 delta += 1; 2431 2432 user_len -= temp; 2433 } 2434 2435 /* a zero length entry indicates the end */ 2436 2437 if ((user_len != 0) && (error == 0)) { 2438 2439 len = 0; 2440 2441 error = copyout(&len, 2442 USB_ADD_BYTES(user_ptr, delta), 1); 2443 } 2444 sx_unlock(&usb_sym_lock); 2445 return (error); 2446} 2447 2448void 2449usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff) 2450{ 2451 if (f == NULL) 2452 return; 2453 2454 /* send a Zero Length Packet, ZLP, before close */ 2455 f->flag_short = onoff; 2456} 2457 2458void 2459usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff) 2460{ 2461 if (f == NULL) 2462 return; 2463 2464 /* defrag written data */ 2465 f->flag_write_defrag = onoff; 2466 /* reset defrag state */ 2467 f->flag_have_fragment = 0; 2468} 2469 2470void * 2471usb_fifo_softc(struct usb_fifo *f) 2472{ 2473 return (f->priv_sc0); 2474} 2475#endif /* USB_HAVE_UGEN */ 2476