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