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