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