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