1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved. 4 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #ifdef USB_GLOBAL_INCLUDE_FILE 29 #include USB_GLOBAL_INCLUDE_FILE 30 #else 31 #include <sys/stdint.h> 32 #include <sys/stddef.h> 33 #include <sys/param.h> 34 #include <sys/queue.h> 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/bus.h> 39 #include <sys/module.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/condvar.h> 43 #include <sys/sysctl.h> 44 #include <sys/sx.h> 45 #include <sys/unistd.h> 46 #include <sys/callout.h> 47 #include <sys/malloc.h> 48 #include <sys/priv.h> 49 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usbdi.h> 52 #include <dev/usb/usbdi_util.h> 53 54 #define USB_DEBUG_VAR usb_debug 55 56 #include <dev/usb/usb_core.h> 57 #include <linux/usb.h> 58 #include <dev/usb/usb_process.h> 59 #include <dev/usb/usb_device.h> 60 #include <dev/usb/usb_util.h> 61 #include <dev/usb/usb_busdma.h> 62 #include <dev/usb/usb_transfer.h> 63 #include <dev/usb/usb_hub.h> 64 #include <dev/usb/usb_request.h> 65 #include <dev/usb/usb_debug.h> 66 #include <dev/usb/usb_dynamic.h> 67 #endif /* USB_GLOBAL_INCLUDE_FILE */ 68 69 struct usb_linux_softc { 70 LIST_ENTRY(usb_linux_softc) sc_attached_list; 71 72 device_t sc_fbsd_dev; 73 struct usb_device *sc_fbsd_udev; 74 struct usb_interface *sc_ui; 75 struct usb_driver *sc_udrv; 76 }; 77 78 /* prototypes */ 79 static device_probe_t usb_linux_probe; 80 static device_attach_t usb_linux_attach; 81 static device_detach_t usb_linux_detach; 82 static device_suspend_t usb_linux_suspend; 83 static device_resume_t usb_linux_resume; 84 85 static usb_callback_t usb_linux_isoc_callback; 86 static usb_callback_t usb_linux_non_isoc_callback; 87 88 static usb_complete_t usb_linux_wait_complete; 89 90 static uint16_t usb_max_isoc_frames(struct usb_device *); 91 static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *); 92 static const struct usb_device_id *usb_linux_lookup_id( 93 const struct usb_device_id *, struct usb_attach_arg *); 94 static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *); 95 static int usb_linux_create_usb_device(struct usb_device *, device_t); 96 static void usb_linux_cleanup_interface(struct usb_device *, 97 struct usb_interface *); 98 static void usb_linux_complete(struct usb_xfer *); 99 static int usb_unlink_urb_sub(struct urb *, uint8_t); 100 101 /*------------------------------------------------------------------------* 102 * FreeBSD USB interface 103 *------------------------------------------------------------------------*/ 104 105 static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list; 106 static LIST_HEAD(, usb_driver) usb_linux_driver_list; 107 108 static device_method_t usb_linux_methods[] = { 109 /* Device interface */ 110 DEVMETHOD(device_probe, usb_linux_probe), 111 DEVMETHOD(device_attach, usb_linux_attach), 112 DEVMETHOD(device_detach, usb_linux_detach), 113 DEVMETHOD(device_suspend, usb_linux_suspend), 114 DEVMETHOD(device_resume, usb_linux_resume), 115 116 DEVMETHOD_END 117 }; 118 119 static driver_t usb_linux_driver = { 120 .name = "usb_linux", 121 .methods = usb_linux_methods, 122 .size = sizeof(struct usb_linux_softc), 123 }; 124 125 DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, NULL, NULL); 126 MODULE_VERSION(usb_linux, 1); 127 128 /*------------------------------------------------------------------------* 129 * usb_linux_lookup_id 130 * 131 * This functions takes an array of "struct usb_device_id" and tries 132 * to match the entries with the information in "struct usb_attach_arg". 133 * If it finds a match the matching entry will be returned. 134 * Else "NULL" will be returned. 135 *------------------------------------------------------------------------*/ 136 static const struct usb_device_id * 137 usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa) 138 { 139 if (id == NULL) { 140 goto done; 141 } 142 /* 143 * Keep on matching array entries until we find one with 144 * "match_flags" equal to zero, which indicates the end of the 145 * array: 146 */ 147 for (; id->match_flags; id++) { 148 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 149 (id->idVendor != uaa->info.idVendor)) { 150 continue; 151 } 152 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 153 (id->idProduct != uaa->info.idProduct)) { 154 continue; 155 } 156 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 157 (id->bcdDevice_lo > uaa->info.bcdDevice)) { 158 continue; 159 } 160 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 161 (id->bcdDevice_hi < uaa->info.bcdDevice)) { 162 continue; 163 } 164 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 165 (id->bDeviceClass != uaa->info.bDeviceClass)) { 166 continue; 167 } 168 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 169 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) { 170 continue; 171 } 172 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 173 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) { 174 continue; 175 } 176 if ((uaa->info.bDeviceClass == 0xFF) && 177 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 178 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 179 USB_DEVICE_ID_MATCH_INT_SUBCLASS | 180 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) { 181 continue; 182 } 183 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 184 (id->bInterfaceClass != uaa->info.bInterfaceClass)) { 185 continue; 186 } 187 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 188 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) { 189 continue; 190 } 191 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 192 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) { 193 continue; 194 } 195 /* we found a match! */ 196 return (id); 197 } 198 199 done: 200 return (NULL); 201 } 202 203 /*------------------------------------------------------------------------* 204 * usb_linux_probe 205 * 206 * This function is the FreeBSD probe callback. It is called from the 207 * FreeBSD USB stack through the "device_probe_and_attach()" function. 208 *------------------------------------------------------------------------*/ 209 static int 210 usb_linux_probe(device_t dev) 211 { 212 struct usb_attach_arg *uaa = device_get_ivars(dev); 213 struct usb_driver *udrv; 214 int err = ENXIO; 215 216 if (uaa->usb_mode != USB_MODE_HOST) { 217 return (ENXIO); 218 } 219 mtx_lock(&Giant); 220 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 221 if (usb_linux_lookup_id(udrv->id_table, uaa)) { 222 err = BUS_PROBE_DEFAULT; 223 break; 224 } 225 } 226 mtx_unlock(&Giant); 227 228 return (err); 229 } 230 231 /*------------------------------------------------------------------------* 232 * usb_linux_get_usb_driver 233 * 234 * This function returns the pointer to the "struct usb_driver" where 235 * the Linux USB device driver "struct usb_device_id" match was found. 236 * We apply a lock before reading out the pointer to avoid races. 237 *------------------------------------------------------------------------*/ 238 static struct usb_driver * 239 usb_linux_get_usb_driver(struct usb_linux_softc *sc) 240 { 241 struct usb_driver *udrv; 242 243 mtx_lock(&Giant); 244 udrv = sc->sc_udrv; 245 mtx_unlock(&Giant); 246 return (udrv); 247 } 248 249 /*------------------------------------------------------------------------* 250 * usb_linux_attach 251 * 252 * This function is the FreeBSD attach callback. It is called from the 253 * FreeBSD USB stack through the "device_probe_and_attach()" function. 254 * This function is called when "usb_linux_probe()" returns zero. 255 *------------------------------------------------------------------------*/ 256 static int 257 usb_linux_attach(device_t dev) 258 { 259 struct usb_attach_arg *uaa = device_get_ivars(dev); 260 struct usb_linux_softc *sc = device_get_softc(dev); 261 struct usb_driver *udrv; 262 const struct usb_device_id *id = NULL; 263 264 mtx_lock(&Giant); 265 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 266 id = usb_linux_lookup_id(udrv->id_table, uaa); 267 if (id) 268 break; 269 } 270 mtx_unlock(&Giant); 271 272 if (id == NULL) { 273 return (ENXIO); 274 } 275 if (usb_linux_create_usb_device(uaa->device, dev) != 0) 276 return (ENOMEM); 277 device_set_usb_desc(dev); 278 279 sc->sc_fbsd_udev = uaa->device; 280 sc->sc_fbsd_dev = dev; 281 sc->sc_udrv = udrv; 282 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum); 283 if (sc->sc_ui == NULL) { 284 return (EINVAL); 285 } 286 if (udrv->probe) { 287 if ((udrv->probe) (sc->sc_ui, id)) { 288 return (ENXIO); 289 } 290 } 291 mtx_lock(&Giant); 292 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list); 293 mtx_unlock(&Giant); 294 295 /* success */ 296 return (0); 297 } 298 299 /*------------------------------------------------------------------------* 300 * usb_linux_detach 301 * 302 * This function is the FreeBSD detach callback. It is called from the 303 * FreeBSD USB stack through the "device_detach()" function. 304 *------------------------------------------------------------------------*/ 305 static int 306 usb_linux_detach(device_t dev) 307 { 308 struct usb_linux_softc *sc = device_get_softc(dev); 309 struct usb_driver *udrv = NULL; 310 311 mtx_lock(&Giant); 312 if (sc->sc_attached_list.le_prev) { 313 LIST_REMOVE(sc, sc_attached_list); 314 sc->sc_attached_list.le_prev = NULL; 315 udrv = sc->sc_udrv; 316 sc->sc_udrv = NULL; 317 } 318 mtx_unlock(&Giant); 319 320 if (udrv && udrv->disconnect) { 321 (udrv->disconnect) (sc->sc_ui); 322 } 323 /* 324 * Make sure that we free all FreeBSD USB transfers belonging to 325 * this Linux "usb_interface", hence they will most likely not be 326 * needed any more. 327 */ 328 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui); 329 return (0); 330 } 331 332 /*------------------------------------------------------------------------* 333 * usb_linux_suspend 334 * 335 * This function is the FreeBSD suspend callback. Usually it does nothing. 336 *------------------------------------------------------------------------*/ 337 static int 338 usb_linux_suspend(device_t dev) 339 { 340 struct usb_linux_softc *sc = device_get_softc(dev); 341 struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 342 int err; 343 344 err = 0; 345 if (udrv && udrv->suspend) 346 err = (udrv->suspend) (sc->sc_ui, 0); 347 return (-err); 348 } 349 350 /*------------------------------------------------------------------------* 351 * usb_linux_resume 352 * 353 * This function is the FreeBSD resume callback. Usually it does nothing. 354 *------------------------------------------------------------------------*/ 355 static int 356 usb_linux_resume(device_t dev) 357 { 358 struct usb_linux_softc *sc = device_get_softc(dev); 359 struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 360 int err; 361 362 err = 0; 363 if (udrv && udrv->resume) 364 err = (udrv->resume) (sc->sc_ui); 365 return (-err); 366 } 367 368 /*------------------------------------------------------------------------* 369 * Linux emulation layer 370 *------------------------------------------------------------------------*/ 371 372 /*------------------------------------------------------------------------* 373 * usb_max_isoc_frames 374 * 375 * The following function returns the maximum number of isochronous 376 * frames that we support per URB. It is not part of the Linux USB API. 377 *------------------------------------------------------------------------*/ 378 static uint16_t 379 usb_max_isoc_frames(struct usb_device *dev) 380 { 381 ; /* indent fix */ 382 switch (usbd_get_speed(dev)) { 383 case USB_SPEED_LOW: 384 case USB_SPEED_FULL: 385 return (USB_MAX_FULL_SPEED_ISOC_FRAMES); 386 default: 387 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES); 388 } 389 } 390 391 /*------------------------------------------------------------------------* 392 * usb_submit_urb 393 * 394 * This function is used to queue an URB after that it has been 395 * initialized. If it returns non-zero, it means that the URB was not 396 * queued. 397 *------------------------------------------------------------------------*/ 398 int 399 usb_submit_urb(struct urb *urb, uint16_t mem_flags) 400 { 401 struct usb_host_endpoint *uhe; 402 uint8_t do_unlock; 403 int err; 404 405 if (urb == NULL) 406 return (-EINVAL); 407 408 do_unlock = mtx_owned(&Giant) ? 0 : 1; 409 if (do_unlock) 410 mtx_lock(&Giant); 411 412 if (urb->endpoint == NULL) { 413 err = -EINVAL; 414 goto done; 415 } 416 417 /* 418 * Check to see if the urb is in the process of being killed 419 * and stop a urb that is in the process of being killed from 420 * being re-submitted (e.g. from its completion callback 421 * function). 422 */ 423 if (urb->kill_count != 0) { 424 err = -EPERM; 425 goto done; 426 } 427 428 uhe = urb->endpoint; 429 430 /* 431 * Check that we have got a FreeBSD USB transfer that will dequeue 432 * the URB structure and do the real transfer. If there are no USB 433 * transfers, then we return an error. 434 */ 435 if (uhe->bsd_xfer[0] || 436 uhe->bsd_xfer[1]) { 437 /* we are ready! */ 438 439 TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list); 440 441 urb->status = -EINPROGRESS; 442 443 usbd_transfer_start(uhe->bsd_xfer[0]); 444 usbd_transfer_start(uhe->bsd_xfer[1]); 445 err = 0; 446 } else { 447 /* no pipes have been setup yet! */ 448 urb->status = -EINVAL; 449 err = -EINVAL; 450 } 451 done: 452 if (do_unlock) 453 mtx_unlock(&Giant); 454 return (err); 455 } 456 457 /*------------------------------------------------------------------------* 458 * usb_unlink_urb 459 * 460 * This function is used to stop an URB after that it is been 461 * submitted, but before the "complete" callback has been called. On 462 *------------------------------------------------------------------------*/ 463 int 464 usb_unlink_urb(struct urb *urb) 465 { 466 return (usb_unlink_urb_sub(urb, 0)); 467 } 468 469 static void 470 usb_unlink_bsd(struct usb_xfer *xfer, 471 struct urb *urb, uint8_t drain) 472 { 473 if (xfer == NULL) 474 return; 475 if (!usbd_transfer_pending(xfer)) 476 return; 477 if (xfer->priv_fifo == (void *)urb) { 478 if (drain) { 479 mtx_unlock(&Giant); 480 usbd_transfer_drain(xfer); 481 mtx_lock(&Giant); 482 } else { 483 usbd_transfer_stop(xfer); 484 } 485 usbd_transfer_start(xfer); 486 } 487 } 488 489 static int 490 usb_unlink_urb_sub(struct urb *urb, uint8_t drain) 491 { 492 struct usb_host_endpoint *uhe; 493 uint16_t x; 494 uint8_t do_unlock; 495 int err; 496 497 if (urb == NULL) 498 return (-EINVAL); 499 500 do_unlock = mtx_owned(&Giant) ? 0 : 1; 501 if (do_unlock) 502 mtx_lock(&Giant); 503 if (drain) 504 urb->kill_count++; 505 506 if (urb->endpoint == NULL) { 507 err = -EINVAL; 508 goto done; 509 } 510 uhe = urb->endpoint; 511 512 if (urb->bsd_urb_list.tqe_prev) { 513 /* not started yet, just remove it from the queue */ 514 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 515 urb->bsd_urb_list.tqe_prev = NULL; 516 urb->status = -ECONNRESET; 517 urb->actual_length = 0; 518 519 for (x = 0; x < urb->number_of_packets; x++) { 520 urb->iso_frame_desc[x].actual_length = 0; 521 } 522 523 if (urb->complete) { 524 (urb->complete) (urb); 525 } 526 } else { 527 /* 528 * If the URB is not on the URB list, then check if one of 529 * the FreeBSD USB transfer are processing the current URB. 530 * If so, re-start that transfer, which will lead to the 531 * termination of that URB: 532 */ 533 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain); 534 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain); 535 } 536 err = 0; 537 done: 538 if (drain) 539 urb->kill_count--; 540 if (do_unlock) 541 mtx_unlock(&Giant); 542 return (err); 543 } 544 545 /*------------------------------------------------------------------------* 546 * usb_clear_halt 547 * 548 * This function must always be used to clear the stall. Stall is when 549 * an USB endpoint returns a stall message to the USB host controller. 550 * Until the stall is cleared, no data can be transferred. 551 *------------------------------------------------------------------------*/ 552 int 553 usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe) 554 { 555 struct usb_config cfg[1]; 556 struct usb_endpoint *ep; 557 uint8_t type; 558 uint8_t addr; 559 560 if (uhe == NULL) 561 return (-EINVAL); 562 563 type = uhe->desc.bmAttributes & UE_XFERTYPE; 564 addr = uhe->desc.bEndpointAddress; 565 566 memset(cfg, 0, sizeof(cfg)); 567 568 cfg[0].type = type; 569 cfg[0].endpoint = addr & UE_ADDR; 570 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 571 572 ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg); 573 if (ep == NULL) 574 return (-EINVAL); 575 576 usbd_clear_data_toggle(dev, ep); 577 578 return (usb_control_msg(dev, &dev->ep0, 579 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT, 580 UF_ENDPOINT_HALT, addr, NULL, 0, 1000)); 581 } 582 583 /*------------------------------------------------------------------------* 584 * usb_start_wait_urb 585 * 586 * This is an internal function that is used to perform synchronous 587 * Linux USB transfers. 588 *------------------------------------------------------------------------*/ 589 static int 590 usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen) 591 { 592 int err; 593 uint8_t do_unlock; 594 595 /* you must have a timeout! */ 596 if (timeout == 0) { 597 timeout = 1; 598 } 599 urb->complete = &usb_linux_wait_complete; 600 urb->timeout = timeout; 601 urb->transfer_flags |= URB_WAIT_WAKEUP; 602 urb->transfer_flags &= ~URB_IS_SLEEPING; 603 604 do_unlock = mtx_owned(&Giant) ? 0 : 1; 605 if (do_unlock) 606 mtx_lock(&Giant); 607 err = usb_submit_urb(urb, 0); 608 if (err) 609 goto done; 610 611 /* 612 * the URB might have completed before we get here, so check that by 613 * using some flags! 614 */ 615 while (urb->transfer_flags & URB_WAIT_WAKEUP) { 616 urb->transfer_flags |= URB_IS_SLEEPING; 617 cv_wait(&urb->cv_wait, &Giant); 618 urb->transfer_flags &= ~URB_IS_SLEEPING; 619 } 620 621 err = urb->status; 622 623 done: 624 if (do_unlock) 625 mtx_unlock(&Giant); 626 if (p_actlen != NULL) { 627 if (err) 628 *p_actlen = 0; 629 else 630 *p_actlen = urb->actual_length; 631 } 632 return (err); 633 } 634 635 /*------------------------------------------------------------------------* 636 * usb_control_msg 637 * 638 * The following function performs a control transfer sequence one any 639 * control, bulk or interrupt endpoint, specified by "uhe". A control 640 * transfer means that you transfer an 8-byte header first followed by 641 * a data-phase as indicated by the 8-byte header. The "timeout" is 642 * given in milliseconds. 643 * 644 * Return values: 645 * 0: Success 646 * < 0: Failure 647 * > 0: Actual length 648 *------------------------------------------------------------------------*/ 649 int 650 usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe, 651 uint8_t request, uint8_t requesttype, 652 uint16_t value, uint16_t index, void *data, 653 uint16_t size, usb_timeout_t timeout) 654 { 655 struct usb_device_request req; 656 struct urb *urb; 657 int err; 658 uint16_t actlen; 659 uint8_t type; 660 uint8_t addr; 661 662 req.bmRequestType = requesttype; 663 req.bRequest = request; 664 USETW(req.wValue, value); 665 USETW(req.wIndex, index); 666 USETW(req.wLength, size); 667 668 if (uhe == NULL) { 669 return (-EINVAL); 670 } 671 type = (uhe->desc.bmAttributes & UE_XFERTYPE); 672 addr = (uhe->desc.bEndpointAddress & UE_ADDR); 673 674 if (type != UE_CONTROL) { 675 return (-EINVAL); 676 } 677 if (addr == 0) { 678 /* 679 * The FreeBSD USB stack supports standard control 680 * transfers on control endpoint zero: 681 */ 682 err = usbd_do_request_flags(dev, 683 NULL, &req, data, USB_SHORT_XFER_OK, 684 &actlen, timeout); 685 if (err) { 686 err = -EPIPE; 687 } else { 688 err = actlen; 689 } 690 return (err); 691 } 692 if (dev->flags.usb_mode != USB_MODE_HOST) { 693 /* not supported */ 694 return (-EINVAL); 695 } 696 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ ); 697 698 /* 699 * NOTE: we need to allocate real memory here so that we don't 700 * transfer data to/from the stack! 701 * 702 * 0xFFFF is a FreeBSD specific magic value. 703 */ 704 urb = usb_alloc_urb(0xFFFF, size); 705 706 urb->dev = dev; 707 urb->endpoint = uhe; 708 709 memcpy(urb->setup_packet, &req, sizeof(req)); 710 711 if (size && (!(req.bmRequestType & UT_READ))) { 712 /* move the data to a real buffer */ 713 memcpy(USB_ADD_BYTES(urb->setup_packet, sizeof(req)), 714 data, size); 715 } 716 err = usb_start_wait_urb(urb, timeout, &actlen); 717 718 if (req.bmRequestType & UT_READ) { 719 if (actlen) { 720 bcopy(USB_ADD_BYTES(urb->setup_packet, 721 sizeof(req)), data, actlen); 722 } 723 } 724 usb_free_urb(urb); 725 726 if (err == 0) { 727 err = actlen; 728 } 729 return (err); 730 } 731 732 /*------------------------------------------------------------------------* 733 * usb_set_interface 734 * 735 * The following function will select which alternate setting of an 736 * USB interface you plan to use. By default alternate setting with 737 * index zero is selected. Note that "iface_no" is not the interface 738 * index, but rather the value of "bInterfaceNumber". 739 *------------------------------------------------------------------------*/ 740 int 741 usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index) 742 { 743 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no); 744 int err; 745 746 if (p_ui == NULL) 747 return (-EINVAL); 748 if (alt_index >= p_ui->num_altsetting) 749 return (-EINVAL); 750 usb_linux_cleanup_interface(dev, p_ui); 751 err = -usbd_set_alt_interface_index(dev, 752 p_ui->bsd_iface_index, alt_index); 753 if (err == 0) { 754 p_ui->cur_altsetting = p_ui->altsetting + alt_index; 755 } 756 return (err); 757 } 758 759 /*------------------------------------------------------------------------* 760 * usb_setup_endpoint 761 * 762 * The following function is an extension to the Linux USB API that 763 * allows you to set a maximum buffer size for a given USB endpoint. 764 * The maximum buffer size is per URB. If you don't call this function 765 * to set a maximum buffer size, the endpoint will not be functional. 766 * Note that for isochronous endpoints the maximum buffer size must be 767 * a non-zero dummy, hence this function will base the maximum buffer 768 * size on "wMaxPacketSize". 769 *------------------------------------------------------------------------*/ 770 int 771 usb_setup_endpoint(struct usb_device *dev, 772 struct usb_host_endpoint *uhe, usb_size_t bufsize) 773 { 774 struct usb_config cfg[2]; 775 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE; 776 uint8_t addr = uhe->desc.bEndpointAddress; 777 778 if (uhe->fbsd_buf_size == bufsize) { 779 /* optimize */ 780 return (0); 781 } 782 usbd_transfer_unsetup(uhe->bsd_xfer, 2); 783 784 uhe->fbsd_buf_size = bufsize; 785 786 if (bufsize == 0) { 787 return (0); 788 } 789 memset(cfg, 0, sizeof(cfg)); 790 791 if (type == UE_ISOCHRONOUS) { 792 /* 793 * Isochronous transfers are special in that they don't fit 794 * into the BULK/INTR/CONTROL transfer model. 795 */ 796 797 cfg[0].type = type; 798 cfg[0].endpoint = addr & UE_ADDR; 799 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 800 cfg[0].callback = &usb_linux_isoc_callback; 801 cfg[0].bufsize = 0; /* use wMaxPacketSize */ 802 cfg[0].frames = usb_max_isoc_frames(dev); 803 cfg[0].flags.proxy_buffer = 1; 804 #if 0 805 /* 806 * The Linux USB API allows non back-to-back 807 * isochronous frames which we do not support. If the 808 * isochronous frames are not back-to-back we need to 809 * do a copy, and then we need a buffer for 810 * that. Enable this at your own risk. 811 */ 812 cfg[0].flags.ext_buffer = 1; 813 #endif 814 cfg[0].flags.short_xfer_ok = 1; 815 816 bcopy(cfg, cfg + 1, sizeof(*cfg)); 817 818 /* Allocate and setup two generic FreeBSD USB transfers */ 819 820 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 821 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) { 822 return (-EINVAL); 823 } 824 } else { 825 if (bufsize > (1 << 22)) { 826 /* limit buffer size */ 827 bufsize = (1 << 22); 828 } 829 /* Allocate and setup one generic FreeBSD USB transfer */ 830 831 cfg[0].type = type; 832 cfg[0].endpoint = addr & UE_ADDR; 833 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 834 cfg[0].callback = &usb_linux_non_isoc_callback; 835 cfg[0].bufsize = bufsize; 836 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */ 837 cfg[0].flags.proxy_buffer = 1; 838 cfg[0].flags.short_xfer_ok = 1; 839 840 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 841 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) { 842 return (-EINVAL); 843 } 844 } 845 return (0); 846 } 847 848 /*------------------------------------------------------------------------* 849 * usb_linux_create_usb_device 850 * 851 * The following function is used to build up a per USB device 852 * structure tree, that mimics the Linux one. The root structure 853 * is returned by this function. 854 *------------------------------------------------------------------------*/ 855 static int 856 usb_linux_create_usb_device(struct usb_device *udev, device_t dev) 857 { 858 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 859 struct usb_descriptor *desc; 860 struct usb_interface_descriptor *id; 861 struct usb_endpoint_descriptor *ed; 862 struct usb_interface *p_ui = NULL; 863 struct usb_host_interface *p_uhi = NULL; 864 struct usb_host_endpoint *p_uhe = NULL; 865 usb_size_t size; 866 uint16_t niface_total; 867 uint16_t nedesc; 868 uint16_t iface_no_curr; 869 uint16_t iface_index; 870 uint8_t pass; 871 uint8_t iface_no; 872 873 /* 874 * We do two passes. One pass for computing necessary memory size 875 * and one pass to initialize all the allocated memory structures. 876 */ 877 for (pass = 0; pass < 2; pass++) { 878 iface_no_curr = 0xFFFF; 879 niface_total = 0; 880 iface_index = 0; 881 nedesc = 0; 882 desc = NULL; 883 884 /* 885 * Iterate over all the USB descriptors. Use the USB config 886 * descriptor pointer provided by the FreeBSD USB stack. 887 */ 888 while ((desc = usb_desc_foreach(cd, desc))) { 889 /* 890 * Build up a tree according to the descriptors we 891 * find: 892 */ 893 switch (desc->bDescriptorType) { 894 case UDESC_DEVICE: 895 break; 896 897 case UDESC_ENDPOINT: 898 ed = (void *)desc; 899 if ((ed->bLength < sizeof(*ed)) || 900 (iface_index == 0)) 901 break; 902 if (p_uhe) { 903 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc)); 904 p_uhe->bsd_iface_index = iface_index - 1; 905 TAILQ_INIT(&p_uhe->bsd_urb_list); 906 p_uhe++; 907 } 908 if (p_uhi) { 909 (p_uhi - 1)->desc.bNumEndpoints++; 910 } 911 nedesc++; 912 break; 913 914 case UDESC_INTERFACE: 915 id = (void *)desc; 916 if (id->bLength < sizeof(*id)) 917 break; 918 if (p_uhi) { 919 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc)); 920 p_uhi->desc.bNumEndpoints = 0; 921 p_uhi->endpoint = p_uhe; 922 p_uhi->string = ""; 923 p_uhi->bsd_iface_index = iface_index; 924 p_uhi++; 925 } 926 iface_no = id->bInterfaceNumber; 927 niface_total++; 928 if (iface_no_curr != iface_no) { 929 if (p_ui) { 930 p_ui->altsetting = p_uhi - 1; 931 p_ui->cur_altsetting = p_uhi - 1; 932 p_ui->bsd_iface_index = iface_index; 933 p_ui->linux_udev = udev; 934 p_ui++; 935 } 936 iface_no_curr = iface_no; 937 iface_index++; 938 } 939 break; 940 941 default: 942 break; 943 } 944 } 945 946 if (pass == 0) { 947 size = (sizeof(*p_uhe) * nedesc) + 948 (sizeof(*p_ui) * iface_index) + 949 (sizeof(*p_uhi) * niface_total); 950 951 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 952 p_ui = (void *)(p_uhe + nedesc); 953 p_uhi = (void *)(p_ui + iface_index); 954 955 udev->linux_iface_start = p_ui; 956 udev->linux_iface_end = p_ui + iface_index; 957 udev->linux_endpoint_start = p_uhe; 958 udev->linux_endpoint_end = p_uhe + nedesc; 959 udev->devnum = device_get_unit(dev); 960 bcopy(&udev->ddesc, &udev->descriptor, 961 sizeof(udev->descriptor)); 962 bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc, 963 sizeof(udev->ep0.desc)); 964 } 965 } 966 return (0); 967 } 968 969 /*------------------------------------------------------------------------* 970 * usb_alloc_urb 971 * 972 * This function should always be used when you allocate an URB for 973 * use with the USB Linux stack. In case of an isochronous transfer 974 * you must specifiy the maximum number of "iso_packets" which you 975 * plan to transfer per URB. This function is always blocking, and 976 * "mem_flags" are not regarded like on Linux. 977 *------------------------------------------------------------------------*/ 978 struct urb * 979 usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags) 980 { 981 struct urb *urb; 982 usb_size_t size; 983 984 if (iso_packets == 0xFFFF) { 985 /* 986 * FreeBSD specific magic value to ask for control transfer 987 * memory allocation: 988 */ 989 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags; 990 } else { 991 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0])); 992 } 993 994 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 995 996 cv_init(&urb->cv_wait, "URBWAIT"); 997 if (iso_packets == 0xFFFF) { 998 urb->setup_packet = (void *)(urb + 1); 999 urb->transfer_buffer = (void *)(urb->setup_packet + 1000 sizeof(struct usb_device_request)); 1001 } else { 1002 urb->number_of_packets = iso_packets; 1003 } 1004 return (urb); 1005 } 1006 1007 /*------------------------------------------------------------------------* 1008 * usb_find_host_endpoint 1009 * 1010 * The following function will return the Linux USB host endpoint 1011 * structure that matches the given endpoint type and endpoint 1012 * value. If no match is found, NULL is returned. This function is not 1013 * part of the Linux USB API and is only used internally. 1014 *------------------------------------------------------------------------*/ 1015 struct usb_host_endpoint * 1016 usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep) 1017 { 1018 struct usb_host_endpoint *uhe; 1019 struct usb_host_endpoint *uhe_end; 1020 struct usb_host_interface *uhi; 1021 struct usb_interface *ui; 1022 uint8_t ea; 1023 uint8_t at; 1024 uint8_t mask; 1025 1026 if (dev == NULL) { 1027 return (NULL); 1028 } 1029 if (type == UE_CONTROL) { 1030 mask = UE_ADDR; 1031 } else { 1032 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR); 1033 } 1034 1035 ep &= mask; 1036 1037 /* 1038 * Iterate over all the interfaces searching the selected alternate 1039 * setting only, and all belonging endpoints. 1040 */ 1041 for (ui = dev->linux_iface_start; 1042 ui != dev->linux_iface_end; 1043 ui++) { 1044 uhi = ui->cur_altsetting; 1045 if (uhi) { 1046 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1047 for (uhe = uhi->endpoint; 1048 uhe != uhe_end; 1049 uhe++) { 1050 ea = uhe->desc.bEndpointAddress; 1051 at = uhe->desc.bmAttributes; 1052 1053 if (((ea & mask) == ep) && 1054 ((at & UE_XFERTYPE) == type)) { 1055 return (uhe); 1056 } 1057 } 1058 } 1059 } 1060 1061 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) { 1062 return (&dev->ep0); 1063 } 1064 return (NULL); 1065 } 1066 1067 /*------------------------------------------------------------------------* 1068 * usb_altnum_to_altsetting 1069 * 1070 * The following function returns a pointer to an alternate setting by 1071 * index given a "usb_interface" pointer. If the alternate setting by 1072 * index does not exist, NULL is returned. And alternate setting is a 1073 * variant of an interface, but usually with slightly different 1074 * characteristics. 1075 *------------------------------------------------------------------------*/ 1076 struct usb_host_interface * 1077 usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index) 1078 { 1079 if (alt_index >= intf->num_altsetting) { 1080 return (NULL); 1081 } 1082 return (intf->altsetting + alt_index); 1083 } 1084 1085 /*------------------------------------------------------------------------* 1086 * usb_ifnum_to_if 1087 * 1088 * The following function searches up an USB interface by 1089 * "bInterfaceNumber". If no match is found, NULL is returned. 1090 *------------------------------------------------------------------------*/ 1091 struct usb_interface * 1092 usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no) 1093 { 1094 struct usb_interface *p_ui; 1095 1096 for (p_ui = dev->linux_iface_start; 1097 p_ui != dev->linux_iface_end; 1098 p_ui++) { 1099 if ((p_ui->num_altsetting > 0) && 1100 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) { 1101 return (p_ui); 1102 } 1103 } 1104 return (NULL); 1105 } 1106 1107 /*------------------------------------------------------------------------* 1108 * usb_buffer_alloc 1109 *------------------------------------------------------------------------*/ 1110 void * 1111 usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr) 1112 { 1113 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO)); 1114 } 1115 1116 /*------------------------------------------------------------------------* 1117 * usbd_get_intfdata 1118 *------------------------------------------------------------------------*/ 1119 void * 1120 usbd_get_intfdata(struct usb_interface *intf) 1121 { 1122 return (intf->bsd_priv_sc); 1123 } 1124 1125 /*------------------------------------------------------------------------* 1126 * usb_linux_register 1127 * 1128 * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1129 * and is used to register a Linux USB driver, so that its 1130 * "usb_device_id" structures gets searched a probe time. This 1131 * function is not part of the Linux USB API, and is for internal use 1132 * only. 1133 *------------------------------------------------------------------------*/ 1134 void 1135 usb_linux_register(void *arg) 1136 { 1137 struct usb_driver *drv = arg; 1138 1139 mtx_lock(&Giant); 1140 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list); 1141 mtx_unlock(&Giant); 1142 1143 usb_needs_explore_all(); 1144 } 1145 1146 /*------------------------------------------------------------------------* 1147 * usb_linux_deregister 1148 * 1149 * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1150 * and is used to deregister a Linux USB driver. This function will 1151 * ensure that all driver instances belonging to the Linux USB device 1152 * driver in question, gets detached before the driver is 1153 * unloaded. This function is not part of the Linux USB API, and is 1154 * for internal use only. 1155 *------------------------------------------------------------------------*/ 1156 void 1157 usb_linux_deregister(void *arg) 1158 { 1159 struct usb_driver *drv = arg; 1160 struct usb_linux_softc *sc; 1161 1162 repeat: 1163 mtx_lock(&Giant); 1164 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) { 1165 if (sc->sc_udrv == drv) { 1166 mtx_unlock(&Giant); 1167 bus_topo_lock(); 1168 device_detach(sc->sc_fbsd_dev); 1169 bus_topo_unlock(); 1170 goto repeat; 1171 } 1172 } 1173 LIST_REMOVE(drv, linux_driver_list); 1174 mtx_unlock(&Giant); 1175 } 1176 1177 /*------------------------------------------------------------------------* 1178 * usb_linux_free_device 1179 * 1180 * The following function is only used by the FreeBSD USB stack, to 1181 * cleanup and free memory after that a Linux USB device was attached. 1182 *------------------------------------------------------------------------*/ 1183 void 1184 usb_linux_free_device(struct usb_device *dev) 1185 { 1186 struct usb_host_endpoint *uhe; 1187 struct usb_host_endpoint *uhe_end; 1188 1189 uhe = dev->linux_endpoint_start; 1190 uhe_end = dev->linux_endpoint_end; 1191 while (uhe != uhe_end) { 1192 usb_setup_endpoint(dev, uhe, 0); 1193 uhe++; 1194 } 1195 usb_setup_endpoint(dev, &dev->ep0, 0); 1196 free(dev->linux_endpoint_start, M_USBDEV); 1197 } 1198 1199 /*------------------------------------------------------------------------* 1200 * usb_buffer_free 1201 *------------------------------------------------------------------------*/ 1202 void 1203 usb_buffer_free(struct usb_device *dev, usb_size_t size, 1204 void *addr, uint8_t dma_addr) 1205 { 1206 free(addr, M_USBDEV); 1207 } 1208 1209 /*------------------------------------------------------------------------* 1210 * usb_free_urb 1211 *------------------------------------------------------------------------*/ 1212 void 1213 usb_free_urb(struct urb *urb) 1214 { 1215 if (urb == NULL) { 1216 return; 1217 } 1218 /* make sure that the current URB is not active */ 1219 usb_kill_urb(urb); 1220 1221 /* destroy condition variable */ 1222 cv_destroy(&urb->cv_wait); 1223 1224 /* just free it */ 1225 free(urb, M_USBDEV); 1226 } 1227 1228 /*------------------------------------------------------------------------* 1229 * usb_init_urb 1230 * 1231 * The following function can be used to initialize a custom URB. It 1232 * is not recommended to use this function. Use "usb_alloc_urb()" 1233 * instead. 1234 *------------------------------------------------------------------------*/ 1235 void 1236 usb_init_urb(struct urb *urb) 1237 { 1238 if (urb == NULL) { 1239 return; 1240 } 1241 memset(urb, 0, sizeof(*urb)); 1242 } 1243 1244 /*------------------------------------------------------------------------* 1245 * usb_kill_urb 1246 *------------------------------------------------------------------------*/ 1247 void 1248 usb_kill_urb(struct urb *urb) 1249 { 1250 usb_unlink_urb_sub(urb, 1); 1251 } 1252 1253 /*------------------------------------------------------------------------* 1254 * usb_set_intfdata 1255 * 1256 * The following function sets the per Linux USB interface private 1257 * data pointer. It is used by most Linux USB device drivers. 1258 *------------------------------------------------------------------------*/ 1259 void 1260 usb_set_intfdata(struct usb_interface *intf, void *data) 1261 { 1262 intf->bsd_priv_sc = data; 1263 } 1264 1265 /*------------------------------------------------------------------------* 1266 * usb_linux_cleanup_interface 1267 * 1268 * The following function will release all FreeBSD USB transfers 1269 * associated with a Linux USB interface. It is for internal use only. 1270 *------------------------------------------------------------------------*/ 1271 static void 1272 usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface) 1273 { 1274 struct usb_host_interface *uhi; 1275 struct usb_host_interface *uhi_end; 1276 struct usb_host_endpoint *uhe; 1277 struct usb_host_endpoint *uhe_end; 1278 1279 uhi = iface->altsetting; 1280 uhi_end = iface->altsetting + iface->num_altsetting; 1281 while (uhi != uhi_end) { 1282 uhe = uhi->endpoint; 1283 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1284 while (uhe != uhe_end) { 1285 usb_setup_endpoint(dev, uhe, 0); 1286 uhe++; 1287 } 1288 uhi++; 1289 } 1290 } 1291 1292 /*------------------------------------------------------------------------* 1293 * usb_linux_wait_complete 1294 * 1295 * The following function is used by "usb_start_wait_urb()" to wake it 1296 * up, when an USB transfer has finished. 1297 *------------------------------------------------------------------------*/ 1298 static void 1299 usb_linux_wait_complete(struct urb *urb) 1300 { 1301 if (urb->transfer_flags & URB_IS_SLEEPING) { 1302 cv_signal(&urb->cv_wait); 1303 } 1304 urb->transfer_flags &= ~URB_WAIT_WAKEUP; 1305 } 1306 1307 /*------------------------------------------------------------------------* 1308 * usb_linux_complete 1309 *------------------------------------------------------------------------*/ 1310 static void 1311 usb_linux_complete(struct usb_xfer *xfer) 1312 { 1313 struct urb *urb; 1314 1315 urb = usbd_xfer_get_priv(xfer); 1316 usbd_xfer_set_priv(xfer, NULL); 1317 if (urb->complete) { 1318 (urb->complete) (urb); 1319 } 1320 } 1321 1322 /*------------------------------------------------------------------------* 1323 * usb_linux_isoc_callback 1324 * 1325 * The following is the FreeBSD isochronous USB callback. Isochronous 1326 * frames are USB packets transferred 1000 or 8000 times per second, 1327 * depending on whether a full- or high- speed USB transfer is 1328 * used. 1329 *------------------------------------------------------------------------*/ 1330 static void 1331 usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1332 { 1333 usb_frlength_t max_frame = xfer->max_frame_size; 1334 usb_frlength_t offset; 1335 usb_frcount_t x; 1336 struct urb *urb = usbd_xfer_get_priv(xfer); 1337 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1338 struct usb_iso_packet_descriptor *uipd; 1339 1340 DPRINTF("\n"); 1341 1342 switch (USB_GET_STATE(xfer)) { 1343 case USB_ST_TRANSFERRED: 1344 1345 if (urb->bsd_isread) { 1346 /* copy in data with regard to the URB */ 1347 1348 offset = 0; 1349 1350 for (x = 0; x < urb->number_of_packets; x++) { 1351 uipd = urb->iso_frame_desc + x; 1352 if (uipd->length > xfer->frlengths[x]) { 1353 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1354 /* XXX should be EREMOTEIO */ 1355 uipd->status = -EPIPE; 1356 } else { 1357 uipd->status = 0; 1358 } 1359 } else { 1360 uipd->status = 0; 1361 } 1362 uipd->actual_length = xfer->frlengths[x]; 1363 if (!xfer->flags.ext_buffer) { 1364 usbd_copy_out(xfer->frbuffers, offset, 1365 USB_ADD_BYTES(urb->transfer_buffer, 1366 uipd->offset), uipd->actual_length); 1367 } 1368 offset += max_frame; 1369 } 1370 } else { 1371 for (x = 0; x < urb->number_of_packets; x++) { 1372 uipd = urb->iso_frame_desc + x; 1373 uipd->actual_length = xfer->frlengths[x]; 1374 uipd->status = 0; 1375 } 1376 } 1377 1378 urb->actual_length = xfer->actlen; 1379 1380 /* check for short transfer */ 1381 if (xfer->actlen < xfer->sumlen) { 1382 /* short transfer */ 1383 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1384 /* XXX should be EREMOTEIO */ 1385 urb->status = -EPIPE; 1386 } else { 1387 urb->status = 0; 1388 } 1389 } else { 1390 /* success */ 1391 urb->status = 0; 1392 } 1393 1394 /* call callback */ 1395 usb_linux_complete(xfer); 1396 1397 case USB_ST_SETUP: 1398 tr_setup: 1399 1400 if (xfer->priv_fifo == NULL) { 1401 /* get next transfer */ 1402 urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1403 if (urb == NULL) { 1404 /* nothing to do */ 1405 return; 1406 } 1407 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1408 urb->bsd_urb_list.tqe_prev = NULL; 1409 1410 x = xfer->max_frame_count; 1411 if (urb->number_of_packets > x) { 1412 /* XXX simply truncate the transfer */ 1413 urb->number_of_packets = x; 1414 } 1415 } else { 1416 DPRINTF("Already got a transfer\n"); 1417 1418 /* already got a transfer (should not happen) */ 1419 urb = usbd_xfer_get_priv(xfer); 1420 } 1421 1422 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0; 1423 1424 if (xfer->flags.ext_buffer) { 1425 /* set virtual address to load */ 1426 usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0); 1427 } 1428 if (!(urb->bsd_isread)) { 1429 /* copy out data with regard to the URB */ 1430 1431 offset = 0; 1432 1433 for (x = 0; x < urb->number_of_packets; x++) { 1434 uipd = urb->iso_frame_desc + x; 1435 usbd_xfer_set_frame_len(xfer, x, uipd->length); 1436 if (!xfer->flags.ext_buffer) { 1437 usbd_copy_in(xfer->frbuffers, offset, 1438 USB_ADD_BYTES(urb->transfer_buffer, 1439 uipd->offset), uipd->length); 1440 } 1441 offset += uipd->length; 1442 } 1443 } else { 1444 /* 1445 * compute the transfer length into the "offset" 1446 * variable 1447 */ 1448 1449 offset = urb->number_of_packets * max_frame; 1450 1451 /* setup "frlengths" array */ 1452 1453 for (x = 0; x < urb->number_of_packets; x++) { 1454 uipd = urb->iso_frame_desc + x; 1455 usbd_xfer_set_frame_len(xfer, x, max_frame); 1456 } 1457 } 1458 usbd_xfer_set_priv(xfer, urb); 1459 xfer->flags.force_short_xfer = 0; 1460 xfer->timeout = urb->timeout; 1461 xfer->nframes = urb->number_of_packets; 1462 usbd_transfer_submit(xfer); 1463 return; 1464 1465 default: /* Error */ 1466 if (xfer->error == USB_ERR_CANCELLED) { 1467 urb->status = -ECONNRESET; 1468 } else { 1469 urb->status = -EPIPE; /* stalled */ 1470 } 1471 1472 /* Set zero for "actual_length" */ 1473 urb->actual_length = 0; 1474 1475 /* Set zero for "actual_length" */ 1476 for (x = 0; x < urb->number_of_packets; x++) { 1477 urb->iso_frame_desc[x].actual_length = 0; 1478 urb->iso_frame_desc[x].status = urb->status; 1479 } 1480 1481 /* call callback */ 1482 usb_linux_complete(xfer); 1483 1484 if (xfer->error == USB_ERR_CANCELLED) { 1485 /* we need to return in this case */ 1486 return; 1487 } 1488 goto tr_setup; 1489 } 1490 } 1491 1492 /*------------------------------------------------------------------------* 1493 * usb_linux_non_isoc_callback 1494 * 1495 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB 1496 * callback. It dequeues Linux USB stack compatible URB's, transforms 1497 * the URB fields into a FreeBSD USB transfer, and defragments the USB 1498 * transfer as required. When the transfer is complete the "complete" 1499 * callback is called. 1500 *------------------------------------------------------------------------*/ 1501 static void 1502 usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1503 { 1504 enum { 1505 REQ_SIZE = sizeof(struct usb_device_request) 1506 }; 1507 struct urb *urb = usbd_xfer_get_priv(xfer); 1508 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1509 uint8_t *ptr; 1510 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 1511 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0; 1512 1513 DPRINTF("\n"); 1514 1515 switch (USB_GET_STATE(xfer)) { 1516 case USB_ST_TRANSFERRED: 1517 1518 if (xfer->flags_int.control_xfr) { 1519 /* don't transfer the setup packet again: */ 1520 1521 usbd_xfer_set_frame_len(xfer, 0, 0); 1522 } 1523 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) { 1524 /* copy in data with regard to the URB */ 1525 usbd_copy_out(xfer->frbuffers + data_frame, 0, 1526 urb->bsd_data_ptr, xfer->frlengths[data_frame]); 1527 } 1528 urb->bsd_length_rem -= xfer->frlengths[data_frame]; 1529 urb->bsd_data_ptr += xfer->frlengths[data_frame]; 1530 urb->actual_length += xfer->frlengths[data_frame]; 1531 1532 /* check for short transfer */ 1533 if (xfer->actlen < xfer->sumlen) { 1534 urb->bsd_length_rem = 0; 1535 1536 /* short transfer */ 1537 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1538 urb->status = -EPIPE; 1539 } else { 1540 urb->status = 0; 1541 } 1542 } else { 1543 /* check remainder */ 1544 if (urb->bsd_length_rem > 0) { 1545 goto setup_bulk; 1546 } 1547 /* success */ 1548 urb->status = 0; 1549 } 1550 1551 /* call callback */ 1552 usb_linux_complete(xfer); 1553 1554 case USB_ST_SETUP: 1555 tr_setup: 1556 /* get next transfer */ 1557 urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1558 if (urb == NULL) { 1559 /* nothing to do */ 1560 return; 1561 } 1562 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1563 urb->bsd_urb_list.tqe_prev = NULL; 1564 1565 usbd_xfer_set_priv(xfer, urb); 1566 xfer->flags.force_short_xfer = 0; 1567 xfer->timeout = urb->timeout; 1568 1569 if (xfer->flags_int.control_xfr) { 1570 /* 1571 * USB control transfers need special handling. 1572 * First copy in the header, then copy in data! 1573 */ 1574 if (!xfer->flags.ext_buffer) { 1575 usbd_copy_in(xfer->frbuffers, 0, 1576 urb->setup_packet, REQ_SIZE); 1577 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE); 1578 } else { 1579 /* set virtual address to load */ 1580 usbd_xfer_set_frame_data(xfer, 0, 1581 urb->setup_packet, REQ_SIZE); 1582 } 1583 1584 ptr = urb->setup_packet; 1585 1586 /* setup data transfer direction and length */ 1587 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0; 1588 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8); 1589 1590 } else { 1591 /* setup data transfer direction */ 1592 1593 urb->bsd_length_rem = urb->transfer_buffer_length; 1594 urb->bsd_isread = (uhe->desc.bEndpointAddress & 1595 UE_DIR_IN) ? 1 : 0; 1596 } 1597 1598 urb->bsd_data_ptr = urb->transfer_buffer; 1599 urb->actual_length = 0; 1600 1601 setup_bulk: 1602 if (max_bulk > urb->bsd_length_rem) { 1603 max_bulk = urb->bsd_length_rem; 1604 } 1605 /* check if we need to force a short transfer */ 1606 1607 if ((max_bulk == urb->bsd_length_rem) && 1608 (urb->transfer_flags & URB_ZERO_PACKET) && 1609 (!xfer->flags_int.control_xfr)) { 1610 xfer->flags.force_short_xfer = 1; 1611 } 1612 /* check if we need to copy in data */ 1613 1614 if (xfer->flags.ext_buffer) { 1615 /* set virtual address to load */ 1616 usbd_xfer_set_frame_data(xfer, data_frame, 1617 urb->bsd_data_ptr, max_bulk); 1618 } else if (!urb->bsd_isread) { 1619 /* copy out data with regard to the URB */ 1620 usbd_copy_in(xfer->frbuffers + data_frame, 0, 1621 urb->bsd_data_ptr, max_bulk); 1622 usbd_xfer_set_frame_len(xfer, data_frame, max_bulk); 1623 } 1624 if (xfer->flags_int.control_xfr) { 1625 if (max_bulk > 0) { 1626 xfer->nframes = 2; 1627 } else { 1628 xfer->nframes = 1; 1629 } 1630 } else { 1631 xfer->nframes = 1; 1632 } 1633 usbd_transfer_submit(xfer); 1634 return; 1635 1636 default: 1637 if (xfer->error == USB_ERR_CANCELLED) { 1638 urb->status = -ECONNRESET; 1639 } else { 1640 urb->status = -EPIPE; 1641 } 1642 1643 /* Set zero for "actual_length" */ 1644 urb->actual_length = 0; 1645 1646 /* call callback */ 1647 usb_linux_complete(xfer); 1648 1649 if (xfer->error == USB_ERR_CANCELLED) { 1650 /* we need to return in this case */ 1651 return; 1652 } 1653 goto tr_setup; 1654 } 1655 } 1656 1657 /*------------------------------------------------------------------------* 1658 * usb_fill_bulk_urb 1659 *------------------------------------------------------------------------*/ 1660 void 1661 usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev, 1662 struct usb_host_endpoint *uhe, void *buf, 1663 int length, usb_complete_t callback, void *arg) 1664 { 1665 urb->dev = udev; 1666 urb->endpoint = uhe; 1667 urb->transfer_buffer = buf; 1668 urb->transfer_buffer_length = length; 1669 urb->complete = callback; 1670 urb->context = arg; 1671 } 1672 1673 /*------------------------------------------------------------------------* 1674 * usb_bulk_msg 1675 * 1676 * NOTE: This function can also be used for interrupt endpoints! 1677 * 1678 * Return values: 1679 * 0: Success 1680 * Else: Failure 1681 *------------------------------------------------------------------------*/ 1682 int 1683 usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe, 1684 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout) 1685 { 1686 struct urb *urb; 1687 int err; 1688 1689 if (uhe == NULL) 1690 return (-EINVAL); 1691 if (len < 0) 1692 return (-EINVAL); 1693 1694 err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */); 1695 if (err) 1696 return (err); 1697 1698 urb = usb_alloc_urb(0, 0); 1699 1700 usb_fill_bulk_urb(urb, udev, uhe, data, len, 1701 usb_linux_wait_complete, NULL); 1702 1703 err = usb_start_wait_urb(urb, timeout, pactlen); 1704 1705 usb_free_urb(urb); 1706 1707 return (err); 1708 } 1709 MODULE_DEPEND(linuxkpi, usb, 1, 1, 1); 1710 1711 static void 1712 usb_linux_init(void *arg) 1713 { 1714 /* register our function */ 1715 usb_linux_free_device_p = &usb_linux_free_device; 1716 } 1717 SYSINIT(usb_linux_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_linux_init, NULL); 1718 SYSUNINIT(usb_linux_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_linux_unload, NULL); 1719