usb_device.c revision 184610
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_device.c 184610 2008-11-04 02:31:03Z alfred $ */ 2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <dev/usb2/include/usb2_defs.h> 28#include <dev/usb2/include/usb2_mfunc.h> 29#include <dev/usb2/include/usb2_error.h> 30#include <dev/usb2/include/usb2_standard.h> 31#include <dev/usb2/include/usb2_ioctl.h> 32#include <dev/usb2/include/usb2_devid.h> 33 34#define USB_DEBUG_VAR usb2_debug 35 36#include <dev/usb2/core/usb2_core.h> 37#include <dev/usb2/core/usb2_debug.h> 38#include <dev/usb2/core/usb2_process.h> 39#include <dev/usb2/core/usb2_device.h> 40#include <dev/usb2/core/usb2_busdma.h> 41#include <dev/usb2/core/usb2_transfer.h> 42#include <dev/usb2/core/usb2_parse.h> 43#include <dev/usb2/core/usb2_request.h> 44#include <dev/usb2/core/usb2_dynamic.h> 45#include <dev/usb2/core/usb2_hub.h> 46#include <dev/usb2/core/usb2_util.h> 47#include <dev/usb2/core/usb2_mbuf.h> 48#include <dev/usb2/core/usb2_dev.h> 49#include <dev/usb2/core/usb2_msctest.h> 50 51#include <dev/usb2/quirk/usb2_quirk.h> 52 53#include <dev/usb2/controller/usb2_controller.h> 54#include <dev/usb2/controller/usb2_bus.h> 55 56/* function prototypes */ 57 58static void usb2_fill_pipe_data(struct usb2_device *udev, uint8_t iface_index, struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe); 59static void usb2_free_pipe_data(struct usb2_device *udev, uint8_t iface_index, uint8_t iface_mask); 60static void usb2_free_iface_data(struct usb2_device *udev); 61static void usb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev, uint8_t free_subdev); 62static uint8_t usb2_probe_and_attach_sub(struct usb2_device *udev, struct usb2_attach_arg *uaa); 63static void usb2_init_attach_arg(struct usb2_device *udev, struct usb2_attach_arg *uaa); 64static void usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend); 65static void usb2_clear_stall_proc(struct usb2_proc_msg *_pm); 66static void usb2_check_strings(struct usb2_device *udev); 67static usb2_error_t usb2_fill_iface_data(struct usb2_device *udev, uint8_t iface_index, uint8_t alt_index); 68static void usb2_notify_addq(const char *type, struct usb2_device *udev); 69static void usb2_fifo_free_wrap(struct usb2_device *udev, uint8_t iface_index, uint8_t free_all); 70 71/* static structures */ 72 73static const uint8_t usb2_hub_speed_combs[USB_SPEED_MAX][USB_SPEED_MAX] = { 74 /* HUB *//* subdevice */ 75 [USB_SPEED_HIGH][USB_SPEED_HIGH] = 1, 76 [USB_SPEED_HIGH][USB_SPEED_FULL] = 1, 77 [USB_SPEED_HIGH][USB_SPEED_LOW] = 1, 78 [USB_SPEED_FULL][USB_SPEED_FULL] = 1, 79 [USB_SPEED_FULL][USB_SPEED_LOW] = 1, 80 [USB_SPEED_LOW][USB_SPEED_LOW] = 1, 81}; 82 83/* This variable is global to allow easy access to it: */ 84 85int usb2_template = 0; 86 87SYSCTL_INT(_hw_usb2, OID_AUTO, template, CTLFLAG_RW, 88 &usb2_template, 0, "Selected USB device side template"); 89 90 91/*------------------------------------------------------------------------* 92 * usb2_get_pipe_by_addr 93 * 94 * This function searches for an USB pipe by endpoint address and 95 * direction. 96 * 97 * Returns: 98 * NULL: Failure 99 * Else: Success 100 *------------------------------------------------------------------------*/ 101struct usb2_pipe * 102usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val) 103{ 104 struct usb2_pipe *pipe = udev->pipes; 105 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 106 enum { 107 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 108 }; 109 110 /* 111 * According to the USB specification not all bits are used 112 * for the endpoint address. Keep defined bits only: 113 */ 114 ea_val &= EA_MASK; 115 116 /* 117 * Iterate accross all the USB pipes searching for a match 118 * based on the endpoint address: 119 */ 120 for (; pipe != pipe_end; pipe++) { 121 122 if (pipe->edesc == NULL) { 123 continue; 124 } 125 /* do the mask and check the value */ 126 if ((pipe->edesc->bEndpointAddress & EA_MASK) == ea_val) { 127 goto found; 128 } 129 } 130 131 /* 132 * The default pipe is always present and is checked separately: 133 */ 134 if ((udev->default_pipe.edesc) && 135 ((udev->default_pipe.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 136 pipe = &udev->default_pipe; 137 goto found; 138 } 139 return (NULL); 140 141found: 142 return (pipe); 143} 144 145/*------------------------------------------------------------------------* 146 * usb2_get_pipe 147 * 148 * This function searches for an USB pipe based on the information 149 * given by the passed "struct usb2_config" pointer. 150 * 151 * Return values: 152 * NULL: No match. 153 * Else: Pointer to "struct usb2_pipe". 154 *------------------------------------------------------------------------*/ 155struct usb2_pipe * 156usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index, 157 const struct usb2_config *setup) 158{ 159 struct usb2_pipe *pipe = udev->pipes; 160 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 161 uint8_t index = setup->ep_index; 162 uint8_t ea_mask; 163 uint8_t ea_val; 164 uint8_t type_mask; 165 uint8_t type_val; 166 167 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 168 "type=0x%x dir=0x%x index=%d\n", 169 udev, iface_index, setup->endpoint, 170 setup->type, setup->direction, setup->ep_index); 171 172 /* setup expected endpoint direction mask and value */ 173 174 if (setup->direction == UE_DIR_ANY) { 175 /* match any endpoint direction */ 176 ea_mask = 0; 177 ea_val = 0; 178 } else { 179 /* match the given endpoint direction */ 180 ea_mask = (UE_DIR_IN | UE_DIR_OUT); 181 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 182 } 183 184 /* setup expected endpoint address */ 185 186 if (setup->endpoint == UE_ADDR_ANY) { 187 /* match any endpoint address */ 188 } else { 189 /* match the given endpoint address */ 190 ea_mask |= UE_ADDR; 191 ea_val |= (setup->endpoint & UE_ADDR); 192 } 193 194 /* setup expected endpoint type */ 195 196 if (setup->type == UE_BULK_INTR) { 197 /* this will match BULK and INTERRUPT endpoints */ 198 type_mask = 2; 199 type_val = 2; 200 } else if (setup->type == UE_TYPE_ANY) { 201 /* match any endpoint type */ 202 type_mask = 0; 203 type_val = 0; 204 } else { 205 /* match the given endpoint type */ 206 type_mask = UE_XFERTYPE; 207 type_val = (setup->type & UE_XFERTYPE); 208 } 209 210 /* 211 * Iterate accross all the USB pipes searching for a match 212 * based on the endpoint address. Note that we are searching 213 * the pipes from the beginning of the "udev->pipes" array. 214 */ 215 for (; pipe != pipe_end; pipe++) { 216 217 if ((pipe->edesc == NULL) || 218 (pipe->iface_index != iface_index)) { 219 continue; 220 } 221 /* do the masks and check the values */ 222 223 if (((pipe->edesc->bEndpointAddress & ea_mask) == ea_val) && 224 ((pipe->edesc->bmAttributes & type_mask) == type_val)) { 225 if (!index--) { 226 goto found; 227 } 228 } 229 } 230 231 /* 232 * Match against default pipe last, so that "any pipe", "any 233 * address" and "any direction" returns the first pipe of the 234 * interface. "iface_index" and "direction" is ignored: 235 */ 236 if ((udev->default_pipe.edesc) && 237 ((udev->default_pipe.edesc->bEndpointAddress & ea_mask) == ea_val) && 238 ((udev->default_pipe.edesc->bmAttributes & type_mask) == type_val) && 239 (!index)) { 240 pipe = &udev->default_pipe; 241 goto found; 242 } 243 return (NULL); 244 245found: 246 return (pipe); 247} 248 249/*------------------------------------------------------------------------* 250 * usb2_interface_count 251 * 252 * This function stores the number of USB interfaces excluding 253 * alternate settings, which the USB config descriptor reports into 254 * the unsigned 8-bit integer pointed to by "count". 255 * 256 * Returns: 257 * 0: Success 258 * Else: Failure 259 *------------------------------------------------------------------------*/ 260usb2_error_t 261usb2_interface_count(struct usb2_device *udev, uint8_t *count) 262{ 263 if (udev->cdesc == NULL) { 264 *count = 0; 265 return (USB_ERR_NOT_CONFIGURED); 266 } 267 *count = udev->cdesc->bNumInterface; 268 return (USB_ERR_NORMAL_COMPLETION); 269} 270 271 272/*------------------------------------------------------------------------* 273 * usb2_fill_pipe_data 274 * 275 * This function will initialise the USB pipe structure pointed to by 276 * the "pipe" argument. 277 *------------------------------------------------------------------------*/ 278static void 279usb2_fill_pipe_data(struct usb2_device *udev, uint8_t iface_index, 280 struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe) 281{ 282 bzero(pipe, sizeof(*pipe)); 283 284 (udev->bus->methods->pipe_init) (udev, edesc, pipe); 285 286 if (pipe->methods == NULL) { 287 /* the pipe is invalid: just return */ 288 return; 289 } 290 /* initialise USB pipe structure */ 291 pipe->edesc = edesc; 292 pipe->iface_index = iface_index; 293 TAILQ_INIT(&pipe->pipe_q.head); 294 pipe->pipe_q.command = &usb2_pipe_start; 295 296 /* clear stall, if any */ 297 if (udev->bus->methods->clear_stall) { 298 mtx_lock(&udev->bus->mtx); 299 (udev->bus->methods->clear_stall) (udev, pipe); 300 mtx_unlock(&udev->bus->mtx); 301 } 302 return; 303} 304 305/*------------------------------------------------------------------------* 306 * usb2_free_pipe_data 307 * 308 * This function will free USB pipe data for the given interface 309 * index. Hence we do not have any dynamic allocations we simply clear 310 * "pipe->edesc" to indicate that the USB pipe structure can be 311 * reused. The pipes belonging to the given interface should not be in 312 * use when this function is called and no check is performed to 313 * prevent this. 314 *------------------------------------------------------------------------*/ 315static void 316usb2_free_pipe_data(struct usb2_device *udev, 317 uint8_t iface_index, uint8_t iface_mask) 318{ 319 struct usb2_pipe *pipe = udev->pipes; 320 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 321 322 while (pipe != pipe_end) { 323 if ((pipe->iface_index & iface_mask) == iface_index) { 324 /* free pipe */ 325 pipe->edesc = NULL; 326 } 327 pipe++; 328 } 329 return; 330} 331 332/*------------------------------------------------------------------------* 333 * usb2_fill_iface_data 334 * 335 * This function will fill in interface data and allocate USB pipes 336 * for all the endpoints that belong to the given interface. This 337 * function is typically called when setting the configuration or when 338 * setting an alternate interface. 339 *------------------------------------------------------------------------*/ 340static usb2_error_t 341usb2_fill_iface_data(struct usb2_device *udev, 342 uint8_t iface_index, uint8_t alt_index) 343{ 344 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 345 struct usb2_pipe *pipe; 346 struct usb2_pipe *pipe_end; 347 struct usb2_interface_descriptor *id; 348 struct usb2_endpoint_descriptor *ed = NULL; 349 struct usb2_descriptor *desc; 350 uint8_t nendpt; 351 352 if (iface == NULL) { 353 return (USB_ERR_INVAL); 354 } 355 DPRINTFN(5, "iface_index=%d alt_index=%d\n", 356 iface_index, alt_index); 357 358 sx_assert(udev->default_sx + 1, SA_LOCKED); 359 360 pipe = udev->pipes; 361 pipe_end = udev->pipes + USB_EP_MAX; 362 363 /* 364 * Check if any USB pipes on the given USB interface are in 365 * use: 366 */ 367 while (pipe != pipe_end) { 368 if ((pipe->edesc != NULL) && 369 (pipe->iface_index == iface_index) && 370 (pipe->refcount != 0)) { 371 return (USB_ERR_IN_USE); 372 } 373 pipe++; 374 } 375 376 pipe = &udev->pipes[0]; 377 378 id = usb2_find_idesc(udev->cdesc, iface_index, alt_index); 379 if (id == NULL) { 380 return (USB_ERR_INVAL); 381 } 382 /* 383 * Free old pipes after we know that an interface descriptor exists, 384 * if any. 385 */ 386 usb2_free_pipe_data(udev, iface_index, 0 - 1); 387 388 /* Setup USB interface structure */ 389 iface->idesc = id; 390 iface->alt_index = alt_index; 391 iface->parent_iface_index = USB_IFACE_INDEX_ANY; 392 393 nendpt = id->bNumEndpoints; 394 DPRINTFN(5, "found idesc nendpt=%d\n", nendpt); 395 396 desc = (void *)id; 397 398 while (nendpt--) { 399 DPRINTFN(11, "endpt=%d\n", nendpt); 400 401 while ((desc = usb2_desc_foreach(udev->cdesc, desc))) { 402 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 403 (desc->bLength >= sizeof(*ed))) { 404 goto found; 405 } 406 if (desc->bDescriptorType == UDESC_INTERFACE) { 407 break; 408 } 409 } 410 goto error; 411 412found: 413 ed = (void *)desc; 414 415 /* find a free pipe */ 416 while (pipe != pipe_end) { 417 if (pipe->edesc == NULL) { 418 /* pipe is free */ 419 usb2_fill_pipe_data(udev, iface_index, ed, pipe); 420 break; 421 } 422 pipe++; 423 } 424 } 425 return (USB_ERR_NORMAL_COMPLETION); 426 427error: 428 /* passed end, or bad desc */ 429 DPRINTFN(0, "%s: bad descriptor(s), addr=%d!\n", 430 __FUNCTION__, udev->address); 431 432 /* free old pipes if any */ 433 usb2_free_pipe_data(udev, iface_index, 0 - 1); 434 return (USB_ERR_INVAL); 435} 436 437/*------------------------------------------------------------------------* 438 * usb2_free_iface_data 439 * 440 * This function will free all USB interfaces and USB pipes belonging 441 * to an USB device. 442 *------------------------------------------------------------------------*/ 443static void 444usb2_free_iface_data(struct usb2_device *udev) 445{ 446 struct usb2_interface *iface = udev->ifaces; 447 struct usb2_interface *iface_end = udev->ifaces + USB_IFACE_MAX; 448 449 /* mtx_assert() */ 450 451 /* free Linux compat device, if any */ 452 if (udev->linux_dev) { 453 usb_linux_free_device(udev->linux_dev); 454 udev->linux_dev = NULL; 455 } 456 /* free all pipes, if any */ 457 usb2_free_pipe_data(udev, 0, 0); 458 459 /* free all interfaces, if any */ 460 while (iface != iface_end) { 461 iface->idesc = NULL; 462 iface->alt_index = 0; 463 iface->parent_iface_index = USB_IFACE_INDEX_ANY; 464 iface->perm.mode = 0; /* disable permissions */ 465 iface++; 466 } 467 468 /* free "cdesc" after "ifaces", if any */ 469 if (udev->cdesc) { 470 free(udev->cdesc, M_USB); 471 udev->cdesc = NULL; 472 } 473 /* set unconfigured state */ 474 udev->curr_config_no = USB_UNCONFIG_NO; 475 udev->curr_config_index = USB_UNCONFIG_INDEX; 476 return; 477} 478 479/*------------------------------------------------------------------------* 480 * usb2_set_config_index 481 * 482 * This function selects configuration by index, independent of the 483 * actual configuration number. This function should not be used by 484 * USB drivers. 485 * 486 * Returns: 487 * 0: Success 488 * Else: Failure 489 *------------------------------------------------------------------------*/ 490usb2_error_t 491usb2_set_config_index(struct usb2_device *udev, uint8_t index) 492{ 493 struct usb2_status ds; 494 struct usb2_hub_descriptor hd; 495 struct usb2_config_descriptor *cdp; 496 uint16_t power; 497 uint16_t max_power; 498 uint8_t nifc; 499 uint8_t selfpowered; 500 uint8_t do_unlock; 501 usb2_error_t err; 502 503 DPRINTFN(6, "udev=%p index=%d\n", udev, index); 504 505 /* automatic locking */ 506 if (sx_xlocked(udev->default_sx + 1)) { 507 do_unlock = 0; 508 } else { 509 do_unlock = 1; 510 sx_xlock(udev->default_sx + 1); 511 } 512 513 /* detach all interface drivers */ 514 usb2_detach_device(udev, USB_IFACE_INDEX_ANY, 1); 515 516 /* free all FIFOs except control endpoint FIFOs */ 517 usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 0); 518 519 /* free all configuration data structures */ 520 usb2_free_iface_data(udev); 521 522 if (index == USB_UNCONFIG_INDEX) { 523 /* 524 * Leave unallocated when unconfiguring the 525 * device. "usb2_free_iface_data()" will also reset 526 * the current config number and index. 527 */ 528 err = usb2_req_set_config(udev, &Giant, USB_UNCONFIG_NO); 529 goto done; 530 } 531 /* get the full config descriptor */ 532 err = usb2_req_get_config_desc_full(udev, 533 &Giant, &cdp, M_USB, index); 534 if (err) { 535 goto done; 536 } 537 /* set the new config descriptor */ 538 539 udev->cdesc = cdp; 540 541 if (cdp->bNumInterface > USB_IFACE_MAX) { 542 DPRINTFN(0, "too many interfaces: %d\n", cdp->bNumInterface); 543 cdp->bNumInterface = USB_IFACE_MAX; 544 } 545 /* Figure out if the device is self or bus powered. */ 546 selfpowered = 0; 547 if ((!udev->flags.uq_bus_powered) && 548 (cdp->bmAttributes & UC_SELF_POWERED) && 549 (udev->flags.usb2_mode == USB_MODE_HOST)) { 550 /* May be self powered. */ 551 if (cdp->bmAttributes & UC_BUS_POWERED) { 552 /* Must ask device. */ 553 if (udev->flags.uq_power_claim) { 554 /* 555 * HUB claims to be self powered, but isn't. 556 * It seems that the power status can be 557 * determined by the HUB characteristics. 558 */ 559 err = usb2_req_get_hub_descriptor 560 (udev, &Giant, &hd, 1); 561 if (err) { 562 DPRINTFN(0, "could not read " 563 "HUB descriptor: %s\n", 564 usb2_errstr(err)); 565 566 } else if (UGETW(hd.wHubCharacteristics) & 567 UHD_PWR_INDIVIDUAL) { 568 selfpowered = 1; 569 } 570 DPRINTF("characteristics=0x%04x\n", 571 UGETW(hd.wHubCharacteristics)); 572 } else { 573 err = usb2_req_get_device_status 574 (udev, &Giant, &ds); 575 if (err) { 576 DPRINTFN(0, "could not read " 577 "device status: %s\n", 578 usb2_errstr(err)); 579 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 580 selfpowered = 1; 581 } 582 DPRINTF("status=0x%04x \n", 583 UGETW(ds.wStatus)); 584 } 585 } else 586 selfpowered = 1; 587 } 588 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 589 "selfpowered=%d, power=%d\n", 590 udev, cdp, 591 cdp->bConfigurationValue, udev->address, cdp->bmAttributes, 592 selfpowered, cdp->bMaxPower * 2); 593 594 /* Check if we have enough power. */ 595 power = cdp->bMaxPower * 2; 596 597 if (udev->parent_hub) { 598 max_power = udev->parent_hub->hub->portpower; 599 } else { 600 max_power = USB_MAX_POWER; 601 } 602 603 if (power > max_power) { 604 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 605 err = USB_ERR_NO_POWER; 606 goto done; 607 } 608 /* Only update "self_powered" in USB Host Mode */ 609 if (udev->flags.usb2_mode == USB_MODE_HOST) { 610 udev->flags.self_powered = selfpowered; 611 } 612 udev->power = power; 613 udev->curr_config_no = cdp->bConfigurationValue; 614 udev->curr_config_index = index; 615 616 /* Set the actual configuration value. */ 617 err = usb2_req_set_config(udev, &Giant, cdp->bConfigurationValue); 618 if (err) { 619 goto done; 620 } 621 /* Allocate and fill interface data. */ 622 nifc = cdp->bNumInterface; 623 while (nifc--) { 624 err = usb2_fill_iface_data(udev, nifc, 0); 625 if (err) { 626 goto done; 627 } 628 } 629 630done: 631 DPRINTF("error=%s\n", usb2_errstr(err)); 632 if (err) { 633 usb2_free_iface_data(udev); 634 } 635 if (do_unlock) { 636 sx_unlock(udev->default_sx + 1); 637 } 638 return (err); 639} 640 641/*------------------------------------------------------------------------* 642 * usb2_set_alt_interface_index 643 * 644 * This function will select an alternate interface index for the 645 * given interface index. The interface should not be in use when this 646 * function is called. That means there should be no open USB 647 * transfers. Else an error is returned. 648 * 649 * Returns: 650 * 0: Success 651 * Else: Failure 652 *------------------------------------------------------------------------*/ 653usb2_error_t 654usb2_set_alt_interface_index(struct usb2_device *udev, 655 uint8_t iface_index, uint8_t alt_index) 656{ 657 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 658 usb2_error_t err; 659 uint8_t do_unlock; 660 661 /* automatic locking */ 662 if (sx_xlocked(udev->default_sx + 1)) { 663 do_unlock = 0; 664 } else { 665 do_unlock = 1; 666 sx_xlock(udev->default_sx + 1); 667 } 668 if (iface == NULL) { 669 err = USB_ERR_INVAL; 670 goto done; 671 } 672 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 673 usb2_detach_device(udev, iface_index, 1); 674 } 675 /* free all FIFOs for this interface */ 676 usb2_fifo_free_wrap(udev, iface_index, 0); 677 678 err = usb2_fill_iface_data(udev, iface_index, alt_index); 679 if (err) { 680 goto done; 681 } 682 err = usb2_req_set_alt_interface_no 683 (udev, &Giant, iface_index, 684 iface->idesc->bAlternateSetting); 685 686done: 687 if (do_unlock) { 688 sx_unlock(udev->default_sx + 1); 689 } 690 return (err); 691} 692 693/*------------------------------------------------------------------------* 694 * usb2_set_endpoint_stall 695 * 696 * This function is used to make a BULK or INTERRUPT endpoint 697 * send STALL tokens. 698 * 699 * Returns: 700 * 0: Success 701 * Else: Failure 702 *------------------------------------------------------------------------*/ 703usb2_error_t 704usb2_set_endpoint_stall(struct usb2_device *udev, struct usb2_pipe *pipe, 705 uint8_t do_stall) 706{ 707 struct usb2_xfer *xfer; 708 uint8_t et; 709 uint8_t was_stalled; 710 711 if (pipe == NULL) { 712 /* nothing to do */ 713 DPRINTF("Cannot find endpoint\n"); 714 /* 715 * Pretend that the clear or set stall request is 716 * successful else some USB host stacks can do 717 * strange things, especially when a control endpoint 718 * stalls. 719 */ 720 return (0); 721 } 722 et = (pipe->edesc->bmAttributes & UE_XFERTYPE); 723 724 if ((et != UE_BULK) && 725 (et != UE_INTERRUPT)) { 726 /* 727 * Should not stall control 728 * nor isochronous endpoints. 729 */ 730 DPRINTF("Invalid endpoint\n"); 731 return (0); 732 } 733 mtx_lock(&udev->bus->mtx); 734 735 /* store current stall state */ 736 was_stalled = pipe->is_stalled; 737 738 /* check for no change */ 739 if (was_stalled && do_stall) { 740 /* if the pipe is already stalled do nothing */ 741 mtx_unlock(&udev->bus->mtx); 742 DPRINTF("No change\n"); 743 return (0); 744 } 745 /* set stalled state */ 746 pipe->is_stalled = 1; 747 748 if (do_stall || (!was_stalled)) { 749 if (!was_stalled) { 750 /* lookup the current USB transfer, if any */ 751 xfer = pipe->pipe_q.curr; 752 } else { 753 xfer = NULL; 754 } 755 756 /* 757 * If "xfer" is non-NULL the "set_stall" method will 758 * complete the USB transfer like in case of a timeout 759 * setting the error code "USB_ERR_STALLED". 760 */ 761 (udev->bus->methods->set_stall) (udev, xfer, pipe); 762 } 763 if (!do_stall) { 764 pipe->toggle_next = 0; /* reset data toggle */ 765 pipe->is_stalled = 0; /* clear stalled state */ 766 767 (udev->bus->methods->clear_stall) (udev, pipe); 768 769 /* start up the current or next transfer, if any */ 770 usb2_command_wrapper(&pipe->pipe_q, pipe->pipe_q.curr); 771 } 772 mtx_unlock(&udev->bus->mtx); 773 return (0); 774} 775 776/*------------------------------------------------------------------------* 777 * usb2_reset_iface_endpoints - used in USB device side mode 778 *------------------------------------------------------------------------*/ 779usb2_error_t 780usb2_reset_iface_endpoints(struct usb2_device *udev, uint8_t iface_index) 781{ 782 struct usb2_pipe *pipe; 783 struct usb2_pipe *pipe_end; 784 usb2_error_t err; 785 786 pipe = udev->pipes; 787 pipe_end = udev->pipes + USB_EP_MAX; 788 789 for (; pipe != pipe_end; pipe++) { 790 791 if ((pipe->edesc == NULL) || 792 (pipe->iface_index != iface_index)) { 793 continue; 794 } 795 /* simulate a clear stall from the peer */ 796 err = usb2_set_endpoint_stall(udev, pipe, 0); 797 if (err) { 798 /* just ignore */ 799 } 800 } 801 return (0); 802} 803 804/*------------------------------------------------------------------------* 805 * usb2_detach_device_sub 806 * 807 * This function will try to detach an USB device. If it fails a panic 808 * will result. 809 *------------------------------------------------------------------------*/ 810static void 811usb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev, 812 uint8_t free_subdev) 813{ 814 device_t dev; 815 int err; 816 817 if (!free_subdev) { 818 819 *ppdev = NULL; 820 821 } else if (*ppdev) { 822 823 /* 824 * NOTE: It is important to clear "*ppdev" before deleting 825 * the child due to some device methods being called late 826 * during the delete process ! 827 */ 828 dev = *ppdev; 829 *ppdev = NULL; 830 831 device_printf(dev, "at %s, port %d, addr %d " 832 "(disconnected)\n", 833 device_get_nameunit(udev->parent_dev), 834 udev->port_no, udev->address); 835 836 if (device_is_attached(dev)) { 837 if (udev->flags.suspended) { 838 err = DEVICE_RESUME(dev); 839 if (err) { 840 device_printf(dev, "Resume failed!\n"); 841 } 842 } 843 if (device_detach(dev)) { 844 goto error; 845 } 846 } 847 if (device_delete_child(udev->parent_dev, dev)) { 848 goto error; 849 } 850 } 851 return; 852 853error: 854 /* Detach is not allowed to fail in the USB world */ 855 panic("An USB driver would not detach!\n"); 856 return; 857} 858 859/*------------------------------------------------------------------------* 860 * usb2_detach_device 861 * 862 * The following function will detach the matching interfaces. 863 * This function is NULL safe. 864 *------------------------------------------------------------------------*/ 865void 866usb2_detach_device(struct usb2_device *udev, uint8_t iface_index, 867 uint8_t free_subdev) 868{ 869 struct usb2_interface *iface; 870 uint8_t i; 871 uint8_t do_unlock; 872 873 if (udev == NULL) { 874 /* nothing to do */ 875 return; 876 } 877 DPRINTFN(4, "udev=%p\n", udev); 878 879 /* automatic locking */ 880 if (sx_xlocked(udev->default_sx + 1)) { 881 do_unlock = 0; 882 } else { 883 do_unlock = 1; 884 sx_xlock(udev->default_sx + 1); 885 } 886 887 /* 888 * First detach the child to give the child's detach routine a 889 * chance to detach the sub-devices in the correct order. 890 * Then delete the child using "device_delete_child()" which 891 * will detach all sub-devices from the bottom and upwards! 892 */ 893 if (iface_index != USB_IFACE_INDEX_ANY) { 894 i = iface_index; 895 iface_index = i + 1; 896 } else { 897 i = 0; 898 iface_index = USB_IFACE_MAX; 899 } 900 901 /* do the detach */ 902 903 for (; i != iface_index; i++) { 904 905 iface = usb2_get_iface(udev, i); 906 if (iface == NULL) { 907 /* looks like the end of the USB interfaces */ 908 break; 909 } 910 usb2_detach_device_sub(udev, &iface->subdev, free_subdev); 911 } 912 913 if (do_unlock) { 914 sx_unlock(udev->default_sx + 1); 915 } 916 return; 917} 918 919/*------------------------------------------------------------------------* 920 * usb2_probe_and_attach_sub 921 * 922 * Returns: 923 * 0: Success 924 * Else: Failure 925 *------------------------------------------------------------------------*/ 926static uint8_t 927usb2_probe_and_attach_sub(struct usb2_device *udev, 928 struct usb2_attach_arg *uaa) 929{ 930 struct usb2_interface *iface; 931 device_t dev; 932 int err; 933 934 iface = uaa->iface; 935 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 936 /* leave interface alone */ 937 return (0); 938 } 939 dev = iface->subdev; 940 if (dev) { 941 942 /* clean up after module unload */ 943 944 if (device_is_attached(dev)) { 945 /* already a device there */ 946 return (0); 947 } 948 /* clear "iface->subdev" as early as possible */ 949 950 iface->subdev = NULL; 951 952 if (device_delete_child(udev->parent_dev, dev)) { 953 954 /* 955 * Panic here, else one can get a double call 956 * to device_detach(). USB devices should 957 * never fail on detach! 958 */ 959 panic("device_delete_child() failed!\n"); 960 } 961 } 962 if (uaa->temp_dev == NULL) { 963 964 /* create a new child */ 965 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 966 if (uaa->temp_dev == NULL) { 967 device_printf(udev->parent_dev, 968 "Device creation failed!\n"); 969 return (1); /* failure */ 970 } 971 device_set_ivars(uaa->temp_dev, uaa); 972 device_quiet(uaa->temp_dev); 973 } 974 /* 975 * Set "subdev" before probe and attach so that "devd" gets 976 * the information it needs. 977 */ 978 iface->subdev = uaa->temp_dev; 979 980 if (device_probe_and_attach(iface->subdev) == 0) { 981 /* 982 * The USB attach arguments are only available during probe 983 * and attach ! 984 */ 985 uaa->temp_dev = NULL; 986 device_set_ivars(iface->subdev, NULL); 987 988 if (udev->flags.suspended) { 989 err = DEVICE_SUSPEND(iface->subdev); 990 device_printf(iface->subdev, "Suspend failed\n"); 991 } 992 return (0); /* success */ 993 } else { 994 /* No USB driver found */ 995 iface->subdev = NULL; 996 } 997 return (1); /* failure */ 998} 999 1000/*------------------------------------------------------------------------* 1001 * usb2_set_parent_iface 1002 * 1003 * Using this function will lock the alternate interface setting on an 1004 * interface. It is typically used for multi interface drivers. In USB 1005 * device side mode it is assumed that the alternate interfaces all 1006 * have the same endpoint descriptors. The default parent index value 1007 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 1008 * locked. 1009 *------------------------------------------------------------------------*/ 1010void 1011usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index, 1012 uint8_t parent_index) 1013{ 1014 struct usb2_interface *iface; 1015 1016 iface = usb2_get_iface(udev, iface_index); 1017 if (iface) { 1018 iface->parent_iface_index = parent_index; 1019 } 1020 return; 1021} 1022 1023static void 1024usb2_init_attach_arg(struct usb2_device *udev, 1025 struct usb2_attach_arg *uaa) 1026{ 1027 bzero(uaa, sizeof(*uaa)); 1028 1029 uaa->device = udev; 1030 uaa->usb2_mode = udev->flags.usb2_mode; 1031 uaa->port = udev->port_no; 1032 1033 uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 1034 uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 1035 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 1036 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 1037 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 1038 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 1039 uaa->info.bConfigIndex = udev->curr_config_index; 1040 uaa->info.bConfigNum = udev->curr_config_no; 1041 1042 return; 1043} 1044 1045/*------------------------------------------------------------------------* 1046 * usb2_probe_and_attach 1047 * 1048 * This function is called from "uhub_explore_sub()", 1049 * "usb2_handle_set_config()" and "usb2_handle_request()". 1050 * 1051 * Returns: 1052 * 0: Success 1053 * Else: A control transfer failed 1054 *------------------------------------------------------------------------*/ 1055usb2_error_t 1056usb2_probe_and_attach(struct usb2_device *udev, uint8_t iface_index) 1057{ 1058 struct usb2_attach_arg uaa; 1059 struct usb2_interface *iface; 1060 uint8_t i; 1061 uint8_t j; 1062 uint8_t do_unlock; 1063 1064 if (udev == NULL) { 1065 DPRINTF("udev == NULL\n"); 1066 return (USB_ERR_INVAL); 1067 } 1068 /* automatic locking */ 1069 if (sx_xlocked(udev->default_sx + 1)) { 1070 do_unlock = 0; 1071 } else { 1072 do_unlock = 1; 1073 sx_xlock(udev->default_sx + 1); 1074 } 1075 1076 if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 1077 /* do nothing - no configuration has been set */ 1078 goto done; 1079 } 1080 /* setup USB attach arguments */ 1081 1082 usb2_init_attach_arg(udev, &uaa); 1083 1084 /* Check if only one interface should be probed: */ 1085 if (iface_index != USB_IFACE_INDEX_ANY) { 1086 i = iface_index; 1087 j = i + 1; 1088 } else { 1089 i = 0; 1090 j = USB_IFACE_MAX; 1091 } 1092 1093 /* Do the probe and attach */ 1094 for (; i != j; i++) { 1095 1096 iface = usb2_get_iface(udev, i); 1097 if (iface == NULL) { 1098 /* 1099 * Looks like the end of the USB 1100 * interfaces ! 1101 */ 1102 DPRINTFN(2, "end of interfaces " 1103 "at %u\n", i); 1104 break; 1105 } 1106 if (iface->idesc == NULL) { 1107 /* no interface descriptor */ 1108 continue; 1109 } 1110 uaa.iface = iface; 1111 1112 uaa.info.bInterfaceClass = 1113 iface->idesc->bInterfaceClass; 1114 uaa.info.bInterfaceSubClass = 1115 iface->idesc->bInterfaceSubClass; 1116 uaa.info.bInterfaceProtocol = 1117 iface->idesc->bInterfaceProtocol; 1118 uaa.info.bIfaceIndex = i; 1119 uaa.info.bIfaceNum = 1120 iface->idesc->bInterfaceNumber; 1121 uaa.use_generic = 0; 1122 1123 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 1124 uaa.info.bInterfaceClass, 1125 uaa.info.bInterfaceSubClass, 1126 uaa.info.bInterfaceProtocol, 1127 uaa.info.bIfaceIndex, 1128 uaa.info.bIfaceNum); 1129 1130 /* try specific interface drivers first */ 1131 1132 if (usb2_probe_and_attach_sub(udev, &uaa)) { 1133 /* ignore */ 1134 } 1135 /* try generic interface drivers last */ 1136 1137 uaa.use_generic = 1; 1138 1139 if (usb2_probe_and_attach_sub(udev, &uaa)) { 1140 /* ignore */ 1141 } 1142 } 1143 1144 if (uaa.temp_dev) { 1145 /* remove the last created child; it is unused */ 1146 1147 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) { 1148 DPRINTFN(0, "device delete child failed!\n"); 1149 } 1150 } 1151done: 1152 if (do_unlock) { 1153 sx_unlock(udev->default_sx + 1); 1154 } 1155 return (0); 1156} 1157 1158/*------------------------------------------------------------------------* 1159 * usb2_suspend_resume_sub 1160 * 1161 * This function is called when the suspend or resume methods should 1162 * be executed on an USB device. 1163 *------------------------------------------------------------------------*/ 1164static void 1165usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend) 1166{ 1167 int err; 1168 1169 if (dev == NULL) { 1170 return; 1171 } 1172 if (!device_is_attached(dev)) { 1173 return; 1174 } 1175 if (do_suspend) { 1176 err = DEVICE_SUSPEND(dev); 1177 } else { 1178 err = DEVICE_RESUME(dev); 1179 } 1180 if (err) { 1181 device_printf(dev, "%s failed!\n", 1182 do_suspend ? "Suspend" : "Resume"); 1183 } 1184 return; 1185} 1186 1187/*------------------------------------------------------------------------* 1188 * usb2_suspend_resume_device 1189 * 1190 * The following function will suspend or resume the USB device. 1191 * 1192 * Returns: 1193 * 0: Success 1194 * Else: Failure 1195 *------------------------------------------------------------------------*/ 1196usb2_error_t 1197usb2_suspend_resume(struct usb2_device *udev, uint8_t do_suspend) 1198{ 1199 struct usb2_interface *iface; 1200 uint8_t i; 1201 1202 if (udev == NULL) { 1203 /* nothing to do */ 1204 return (0); 1205 } 1206 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 1207 1208 sx_assert(udev->default_sx + 1, SA_LOCKED); 1209 1210 mtx_lock(&udev->bus->mtx); 1211 /* filter the suspend events */ 1212 if (udev->flags.suspended == do_suspend) { 1213 mtx_unlock(&udev->bus->mtx); 1214 /* nothing to do */ 1215 return (0); 1216 } 1217 udev->flags.suspended = do_suspend; 1218 mtx_unlock(&udev->bus->mtx); 1219 1220 /* do the suspend or resume */ 1221 1222 for (i = 0; i != USB_IFACE_MAX; i++) { 1223 1224 iface = usb2_get_iface(udev, i); 1225 if (iface == NULL) { 1226 /* looks like the end of the USB interfaces */ 1227 break; 1228 } 1229 usb2_suspend_resume_sub(udev, iface->subdev, do_suspend); 1230 } 1231 return (0); 1232} 1233 1234/*------------------------------------------------------------------------* 1235 * usb2_clear_stall_proc 1236 * 1237 * This function performs generic USB clear stall operations. 1238 *------------------------------------------------------------------------*/ 1239static void 1240usb2_clear_stall_proc(struct usb2_proc_msg *_pm) 1241{ 1242 struct usb2_clear_stall_msg *pm = (void *)_pm; 1243 struct usb2_device *udev = pm->udev; 1244 1245 /* Change lock */ 1246 mtx_unlock(&udev->bus->mtx); 1247 mtx_lock(udev->default_mtx); 1248 1249 /* Start clear stall callback */ 1250 usb2_transfer_start(udev->default_xfer[1]); 1251 1252 /* Change lock */ 1253 mtx_unlock(udev->default_mtx); 1254 mtx_lock(&udev->bus->mtx); 1255 return; 1256} 1257 1258/*------------------------------------------------------------------------* 1259 * usb2_alloc_device 1260 * 1261 * This function allocates a new USB device. This function is called 1262 * when a new device has been put in the powered state, but not yet in 1263 * the addressed state. Get initial descriptor, set the address, get 1264 * full descriptor and get strings. 1265 * 1266 * Return values: 1267 * 0: Failure 1268 * Else: Success 1269 *------------------------------------------------------------------------*/ 1270struct usb2_device * 1271usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus, 1272 struct usb2_device *parent_hub, uint8_t depth, 1273 uint8_t port_index, uint8_t port_no, uint8_t speed, uint8_t usb2_mode) 1274{ 1275 struct usb2_attach_arg uaa; 1276 struct usb2_device *udev; 1277 struct usb2_device *adev; 1278 struct usb2_device *hub; 1279 uint8_t *scratch_ptr; 1280 uint32_t scratch_size; 1281 usb2_error_t err; 1282 uint8_t device_index; 1283 1284 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1285 "port_index=%u, port_no=%u, speed=%u, usb2_mode=%u\n", 1286 parent_dev, bus, parent_hub, depth, port_index, port_no, 1287 speed, usb2_mode); 1288 1289 /* 1290 * Find an unused device index. In USB Host mode this is the 1291 * same as the device address. 1292 * 1293 * NOTE: Index 1 is reserved for the Root HUB. 1294 */ 1295 for (device_index = USB_ROOT_HUB_ADDR; device_index != 1296 USB_MAX_DEVICES; device_index++) { 1297 if (bus->devices[device_index] == NULL) 1298 break; 1299 } 1300 1301 if (device_index == USB_MAX_DEVICES) { 1302 device_printf(bus->bdev, 1303 "No free USB device index for new device!\n"); 1304 return (NULL); 1305 } 1306 if (depth > 0x10) { 1307 device_printf(bus->bdev, 1308 "Invalid device depth!\n"); 1309 return (NULL); 1310 } 1311 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1312 if (udev == NULL) { 1313 return (NULL); 1314 } 1315 /* initialise our SX-lock */ 1316 sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth); 1317 1318 /* initialise our SX-lock */ 1319 sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth); 1320 1321 usb2_cv_init(udev->default_cv, "WCTRL"); 1322 usb2_cv_init(udev->default_cv + 1, "UGONE"); 1323 1324 /* initialise our mutex */ 1325 mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF); 1326 1327 /* initialise generic clear stall */ 1328 udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc; 1329 udev->cs_msg[0].udev = udev; 1330 udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc; 1331 udev->cs_msg[1].udev = udev; 1332 1333 /* initialise some USB device fields */ 1334 udev->parent_hub = parent_hub; 1335 udev->parent_dev = parent_dev; 1336 udev->port_index = port_index; 1337 udev->port_no = port_no; 1338 udev->depth = depth; 1339 udev->bus = bus; 1340 udev->address = USB_START_ADDR; /* default value */ 1341 udev->plugtime = (uint32_t)ticks; 1342 udev->power_mode = USB_POWER_MODE_ON; 1343 1344 /* we are not ready yet */ 1345 udev->refcount = 1; 1346 1347 /* set up default endpoint descriptor */ 1348 udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc); 1349 udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1350 udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1351 udev->default_ep_desc.bmAttributes = UE_CONTROL; 1352 udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 1353 udev->default_ep_desc.wMaxPacketSize[1] = 0; 1354 udev->default_ep_desc.bInterval = 0; 1355 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1356 1357 udev->speed = speed; 1358 udev->flags.usb2_mode = usb2_mode; 1359 1360 /* check speed combination */ 1361 1362 hub = udev->parent_hub; 1363 if (hub) { 1364 if (usb2_hub_speed_combs[hub->speed][speed] == 0) { 1365#if USB_DEBUG 1366 printf("%s: the selected subdevice and HUB speed " 1367 "combination is not supported %d/%d.\n", 1368 __FUNCTION__, speed, hub->speed); 1369#endif 1370 /* reject this combination */ 1371 err = USB_ERR_INVAL; 1372 goto done; 1373 } 1374 } 1375 /* search for our High Speed USB HUB, if any */ 1376 1377 adev = udev; 1378 hub = udev->parent_hub; 1379 1380 while (hub) { 1381 if (hub->speed == USB_SPEED_HIGH) { 1382 udev->hs_hub_addr = hub->address; 1383 udev->hs_port_no = adev->port_no; 1384 break; 1385 } 1386 adev = hub; 1387 hub = hub->parent_hub; 1388 } 1389 1390 /* init the default pipe */ 1391 usb2_fill_pipe_data(udev, 0, 1392 &udev->default_ep_desc, 1393 &udev->default_pipe); 1394 1395 /* set device index */ 1396 udev->device_index = device_index; 1397 1398 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1399 1400 err = usb2_req_set_address(udev, &Giant, device_index); 1401 1402 /* This is the new USB device address from now on */ 1403 1404 udev->address = device_index; 1405 1406 /* 1407 * We ignore any set-address errors, hence there are 1408 * buggy USB devices out there that actually receive 1409 * the SETUP PID, but manage to set the address before 1410 * the STATUS stage is ACK'ed. If the device responds 1411 * to the subsequent get-descriptor at the new 1412 * address, then we know that the set-address command 1413 * was successful. 1414 */ 1415 if (err) { 1416 DPRINTFN(0, "set address %d failed " 1417 "(ignored)\n", udev->address); 1418 } 1419 /* allow device time to set new address */ 1420 usb2_pause_mtx(&Giant, USB_SET_ADDRESS_SETTLE); 1421 } else { 1422 /* We are not self powered */ 1423 udev->flags.self_powered = 0; 1424 1425 /* Set unconfigured state */ 1426 udev->curr_config_no = USB_UNCONFIG_NO; 1427 udev->curr_config_index = USB_UNCONFIG_INDEX; 1428 1429 /* Setup USB descriptors */ 1430 err = (usb2_temp_setup_by_index_p) (udev, usb2_template); 1431 if (err) { 1432 DPRINTFN(0, "setting up USB template failed maybe the USB " 1433 "template module has not been loaded\n"); 1434 goto done; 1435 } 1436 } 1437 1438 /* 1439 * Get the first 8 bytes of the device descriptor ! 1440 * 1441 * NOTE: "usb2_do_request" will check the device descriptor 1442 * next time we do a request to see if the maximum packet size 1443 * changed! The 8 first bytes of the device descriptor 1444 * contains the maximum packet size to use on control endpoint 1445 * 0. If this value is different from "USB_MAX_IPACKET" a new 1446 * USB control request will be setup! 1447 */ 1448 err = usb2_req_get_desc(udev, &Giant, &udev->ddesc, 1449 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1450 if (err) { 1451 DPRINTFN(0, "getting device descriptor " 1452 "at addr %d failed!\n", udev->address); 1453 goto done; 1454 } 1455 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1456 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1457 udev->address, UGETW(udev->ddesc.bcdUSB), 1458 udev->ddesc.bDeviceClass, 1459 udev->ddesc.bDeviceSubClass, 1460 udev->ddesc.bDeviceProtocol, 1461 udev->ddesc.bMaxPacketSize, 1462 udev->ddesc.bLength, 1463 udev->speed); 1464 1465 /* get the full device descriptor */ 1466 err = usb2_req_get_device_desc(udev, &Giant, &udev->ddesc); 1467 if (err) { 1468 DPRINTF("addr=%d, getting full desc failed\n", 1469 udev->address); 1470 goto done; 1471 } 1472 /* 1473 * Setup temporary USB attach args so that we can figure out some 1474 * basic quirks for this device. 1475 */ 1476 usb2_init_attach_arg(udev, &uaa); 1477 1478 if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) { 1479 udev->flags.uq_bus_powered = 1; 1480 } 1481 if (usb2_test_quirk(&uaa, UQ_POWER_CLAIM)) { 1482 udev->flags.uq_power_claim = 1; 1483 } 1484 if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) { 1485 udev->flags.no_strings = 1; 1486 } 1487 /* 1488 * Workaround for buggy USB devices. 1489 * 1490 * It appears that some string-less USB chips will crash and 1491 * disappear if any attempts are made to read any string 1492 * descriptors. 1493 * 1494 * Try to detect such chips by checking the strings in the USB 1495 * device descriptor. If no strings are present there we 1496 * simply disable all USB strings. 1497 */ 1498 scratch_ptr = udev->bus->scratch[0].data; 1499 scratch_size = sizeof(udev->bus->scratch[0].data); 1500 1501 if (udev->ddesc.iManufacturer || 1502 udev->ddesc.iProduct || 1503 udev->ddesc.iSerialNumber) { 1504 /* read out the language ID string */ 1505 err = usb2_req_get_string_desc(udev, &Giant, 1506 (char *)scratch_ptr, 4, scratch_size, 1507 USB_LANGUAGE_TABLE); 1508 } else { 1509 err = USB_ERR_INVAL; 1510 } 1511 1512 if (err || (scratch_ptr[0] < 4)) { 1513 udev->flags.no_strings = 1; 1514 } else { 1515 /* pick the first language as the default */ 1516 udev->langid = UGETW(scratch_ptr + 2); 1517 } 1518 1519 /* assume 100mA bus powered for now. Changed when configured. */ 1520 udev->power = USB_MIN_POWER; 1521 1522 /* get serial number string */ 1523 err = usb2_req_get_string_any 1524 (udev, &Giant, (char *)scratch_ptr, 1525 scratch_size, udev->ddesc.iSerialNumber); 1526 1527 strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial)); 1528 1529 /* get manufacturer string */ 1530 err = usb2_req_get_string_any 1531 (udev, &Giant, (char *)scratch_ptr, 1532 scratch_size, udev->ddesc.iManufacturer); 1533 1534 strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer)); 1535 1536 /* get product string */ 1537 err = usb2_req_get_string_any 1538 (udev, &Giant, (char *)scratch_ptr, 1539 scratch_size, udev->ddesc.iProduct); 1540 1541 strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product)); 1542 1543 /* finish up all the strings */ 1544 usb2_check_strings(udev); 1545 1546 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1547 uint8_t config_index; 1548 uint8_t config_quirk; 1549 1550 /* 1551 * Most USB devices should attach to config index 0 by 1552 * default 1553 */ 1554 if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1555 config_index = 0; 1556 config_quirk = 1; 1557 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1558 config_index = 1; 1559 config_quirk = 1; 1560 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1561 config_index = 2; 1562 config_quirk = 1; 1563 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1564 config_index = 3; 1565 config_quirk = 1; 1566 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1567 config_index = 4; 1568 config_quirk = 1; 1569 } else { 1570 config_index = 0; 1571 config_quirk = 0; 1572 } 1573 1574repeat_set_config: 1575 1576 DPRINTF("setting config %u\n", config_index); 1577 1578 /* get the USB device configured */ 1579 sx_xlock(udev->default_sx + 1); 1580 err = usb2_set_config_index(udev, config_index); 1581 sx_unlock(udev->default_sx + 1); 1582 if (err) { 1583 DPRINTFN(0, "Failure selecting " 1584 "configuration index %u: %s, port %u, addr %u\n", 1585 config_index, usb2_errstr(err), udev->port_no, 1586 udev->address); 1587 1588 } else if (config_quirk) { 1589 /* user quirk selects configuration index */ 1590 } else if ((config_index + 1) < udev->ddesc.bNumConfigurations) { 1591 1592 if ((udev->cdesc->bNumInterface < 2) && 1593 (usb2_get_no_endpoints(udev->cdesc) == 0)) { 1594 DPRINTFN(0, "Found no endpoints " 1595 "(trying next config)!\n"); 1596 config_index++; 1597 goto repeat_set_config; 1598 } 1599 if (config_index == 0) { 1600 /* 1601 * Try to figure out if we have an 1602 * auto-install disk there: 1603 */ 1604 if (usb2_test_autoinstall(udev, 0) == 0) { 1605 DPRINTFN(0, "Found possible auto-install " 1606 "disk (trying next config)\n"); 1607 config_index++; 1608 goto repeat_set_config; 1609 } 1610 } 1611 } else if (UGETW(udev->ddesc.idVendor) == USB_VENDOR_HUAWEI) { 1612 if (usb2_test_huawei(udev, 0) == 0) { 1613 DPRINTFN(0, "Found Huawei auto-install disk!\n"); 1614 err = USB_ERR_STALLED; /* fake an error */ 1615 } 1616 } 1617 } else { 1618 err = 0; /* set success */ 1619 } 1620 1621 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1622 udev->address, udev, udev->parent_hub); 1623 1624 /* register our device - we are ready */ 1625 usb2_bus_port_set_device(bus, parent_hub ? 1626 parent_hub->hub->ports + port_index : NULL, udev, device_index); 1627 1628 /* make a symlink for UGEN */ 1629 if (snprintf((char *)scratch_ptr, scratch_size, 1630 USB_DEVICE_NAME "%u.%u.0.0", 1631 device_get_unit(udev->bus->bdev), 1632 udev->device_index)) { 1633 /* ignore */ 1634 } 1635 udev->ugen_symlink = 1636 usb2_alloc_symlink((char *)scratch_ptr, "ugen%u.%u", 1637 device_get_unit(udev->bus->bdev), 1638 udev->device_index); 1639 1640 printf("ugen%u.%u: <%s> at %s\n", 1641 device_get_unit(udev->bus->bdev), 1642 udev->device_index, udev->manufacturer, 1643 device_get_nameunit(udev->bus->bdev)); 1644 1645 usb2_notify_addq("+", udev); 1646done: 1647 if (err) { 1648 /* free device */ 1649 usb2_free_device(udev); 1650 udev = NULL; 1651 } 1652 return (udev); 1653} 1654 1655/*------------------------------------------------------------------------* 1656 * usb2_free_device 1657 * 1658 * This function is NULL safe and will free an USB device. 1659 *------------------------------------------------------------------------*/ 1660void 1661usb2_free_device(struct usb2_device *udev) 1662{ 1663 struct usb2_bus *bus; 1664 1665 if (udev == NULL) { 1666 /* already freed */ 1667 return; 1668 } 1669 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 1670 1671 usb2_notify_addq("-", udev); 1672 1673 bus = udev->bus; 1674 1675 /* 1676 * Destroy UGEN symlink, if any 1677 */ 1678 if (udev->ugen_symlink) { 1679 usb2_free_symlink(udev->ugen_symlink); 1680 udev->ugen_symlink = NULL; 1681 } 1682 /* 1683 * Unregister our device first which will prevent any further 1684 * references: 1685 */ 1686 usb2_bus_port_set_device(bus, udev->parent_hub ? 1687 udev->parent_hub->hub->ports + udev->port_index : NULL, 1688 NULL, USB_ROOT_HUB_ADDR); 1689 1690 /* wait for all pending references to go away: */ 1691 1692 mtx_lock(&usb2_ref_lock); 1693 udev->refcount--; 1694 while (udev->refcount != 0) { 1695 usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock); 1696 } 1697 mtx_unlock(&usb2_ref_lock); 1698 1699 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 1700 /* stop receiving any control transfers (Device Side Mode) */ 1701 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1702 } 1703 /* free all FIFOs */ 1704 usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 1); 1705 1706 /* 1707 * Free all interface related data and FIFOs, if any. 1708 */ 1709 usb2_free_iface_data(udev); 1710 1711 /* unsetup any leftover default USB transfers */ 1712 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1713 1714 (usb2_temp_unsetup_p) (udev); 1715 1716 sx_destroy(udev->default_sx); 1717 sx_destroy(udev->default_sx + 1); 1718 1719 usb2_cv_destroy(udev->default_cv); 1720 usb2_cv_destroy(udev->default_cv + 1); 1721 1722 mtx_destroy(udev->default_mtx); 1723 1724 /* free device */ 1725 free(udev, M_USB); 1726 1727 return; 1728} 1729 1730/*------------------------------------------------------------------------* 1731 * usb2_get_iface 1732 * 1733 * This function is the safe way to get the USB interface structure 1734 * pointer by interface index. 1735 * 1736 * Return values: 1737 * NULL: Interface not present. 1738 * Else: Pointer to USB interface structure. 1739 *------------------------------------------------------------------------*/ 1740struct usb2_interface * 1741usb2_get_iface(struct usb2_device *udev, uint8_t iface_index) 1742{ 1743 struct usb2_interface *iface = udev->ifaces + iface_index; 1744 1745 if ((iface < udev->ifaces) || 1746 (iface_index >= USB_IFACE_MAX) || 1747 (udev->cdesc == NULL) || 1748 (iface_index >= udev->cdesc->bNumInterface)) { 1749 return (NULL); 1750 } 1751 return (iface); 1752} 1753 1754/*------------------------------------------------------------------------* 1755 * usb2_find_descriptor 1756 * 1757 * This function will lookup the first descriptor that matches the 1758 * criteria given by the arguments "type" and "subtype". Descriptors 1759 * will only be searched within the interface having the index 1760 * "iface_index". If the "id" argument points to an USB descriptor, 1761 * it will be skipped before the search is started. This allows 1762 * searching for multiple descriptors using the same criteria. Else 1763 * the search is started after the interface descriptor. 1764 * 1765 * Return values: 1766 * NULL: End of descriptors 1767 * Else: A descriptor matching the criteria 1768 *------------------------------------------------------------------------*/ 1769void * 1770usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index, 1771 uint8_t type, uint8_t type_mask, 1772 uint8_t subtype, uint8_t subtype_mask) 1773{ 1774 struct usb2_descriptor *desc; 1775 struct usb2_config_descriptor *cd; 1776 struct usb2_interface *iface; 1777 1778 cd = usb2_get_config_descriptor(udev); 1779 if (cd == NULL) { 1780 return (NULL); 1781 } 1782 if (id == NULL) { 1783 iface = usb2_get_iface(udev, iface_index); 1784 if (iface == NULL) { 1785 return (NULL); 1786 } 1787 id = usb2_get_interface_descriptor(iface); 1788 if (id == NULL) { 1789 return (NULL); 1790 } 1791 } 1792 desc = (void *)id; 1793 1794 while ((desc = usb2_desc_foreach(cd, desc))) { 1795 1796 if (desc->bDescriptorType == UDESC_INTERFACE) { 1797 break; 1798 } 1799 if (((desc->bDescriptorType & type_mask) == type) && 1800 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 1801 return (desc); 1802 } 1803 } 1804 return (NULL); 1805} 1806 1807/*------------------------------------------------------------------------* 1808 * usb2_devinfo 1809 * 1810 * This function will dump information from the device descriptor 1811 * belonging to the USB device pointed to by "udev", to the string 1812 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 1813 * including the terminating zero. 1814 *------------------------------------------------------------------------*/ 1815void 1816usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len) 1817{ 1818 struct usb2_device_descriptor *udd = &udev->ddesc; 1819 uint16_t bcdDevice; 1820 uint16_t bcdUSB; 1821 1822 bcdUSB = UGETW(udd->bcdUSB); 1823 bcdDevice = UGETW(udd->bcdDevice); 1824 1825 if (udd->bDeviceClass != 0xFF) { 1826 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 1827 "%x.%02x, addr %d", udev->manufacturer, udev->product, 1828 udd->bDeviceClass, udd->bDeviceSubClass, 1829 (bcdUSB >> 8), bcdUSB & 0xFF, 1830 (bcdDevice >> 8), bcdDevice & 0xFF, 1831 udev->address); 1832 } else { 1833 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 1834 "%x.%02x, addr %d", udev->manufacturer, udev->product, 1835 (bcdUSB >> 8), bcdUSB & 0xFF, 1836 (bcdDevice >> 8), bcdDevice & 0xFF, 1837 udev->address); 1838 } 1839 return; 1840} 1841 1842#if USB_VERBOSE 1843/* 1844 * Descriptions of of known vendors and devices ("products"). 1845 */ 1846struct usb_knowndev { 1847 uint16_t vendor; 1848 uint16_t product; 1849 uint32_t flags; 1850 const char *vendorname; 1851 const char *productname; 1852}; 1853 1854#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 1855 1856#include <dev/usb2/include/usb2_devid.h> 1857#include <dev/usb2/include/usb2_devtable.h> 1858#endif /* USB_VERBOSE */ 1859 1860/*------------------------------------------------------------------------* 1861 * usb2_check_strings 1862 * 1863 * This function checks the manufacturer and product strings and will 1864 * fill in defaults for missing strings. 1865 *------------------------------------------------------------------------*/ 1866static void 1867usb2_check_strings(struct usb2_device *udev) 1868{ 1869 struct usb2_device_descriptor *udd = &udev->ddesc; 1870 const char *vendor; 1871 const char *product; 1872 1873#if USB_VERBOSE 1874 const struct usb_knowndev *kdp; 1875 1876#endif 1877 uint16_t vendor_id; 1878 uint16_t product_id; 1879 1880 usb2_trim_spaces(udev->manufacturer); 1881 usb2_trim_spaces(udev->product); 1882 1883 if (udev->manufacturer[0]) { 1884 vendor = udev->manufacturer; 1885 } else { 1886 vendor = NULL; 1887 } 1888 1889 if (udev->product[0]) { 1890 product = udev->product; 1891 } else { 1892 product = NULL; 1893 } 1894 1895 vendor_id = UGETW(udd->idVendor); 1896 product_id = UGETW(udd->idProduct); 1897 1898#if USB_VERBOSE 1899 if (vendor == NULL || product == NULL) { 1900 1901 for (kdp = usb_knowndevs; 1902 kdp->vendorname != NULL; 1903 kdp++) { 1904 if (kdp->vendor == vendor_id && 1905 (kdp->product == product_id || 1906 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 1907 break; 1908 } 1909 if (kdp->vendorname != NULL) { 1910 if (vendor == NULL) 1911 vendor = kdp->vendorname; 1912 if (product == NULL) 1913 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ? 1914 kdp->productname : NULL; 1915 } 1916 } 1917#endif 1918 if (vendor && *vendor) { 1919 if (udev->manufacturer != vendor) { 1920 strlcpy(udev->manufacturer, vendor, 1921 sizeof(udev->manufacturer)); 1922 } 1923 } else { 1924 snprintf(udev->manufacturer, 1925 sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id); 1926 } 1927 1928 if (product && *product) { 1929 if (udev->product != product) { 1930 strlcpy(udev->product, product, 1931 sizeof(udev->product)); 1932 } 1933 } else { 1934 snprintf(udev->product, 1935 sizeof(udev->product), "product 0x%04x", product_id); 1936 } 1937 return; 1938} 1939 1940uint8_t 1941usb2_get_speed(struct usb2_device *udev) 1942{ 1943 return (udev->speed); 1944} 1945 1946struct usb2_device_descriptor * 1947usb2_get_device_descriptor(struct usb2_device *udev) 1948{ 1949 if (udev == NULL) 1950 return (NULL); /* be NULL safe */ 1951 return (&udev->ddesc); 1952} 1953 1954struct usb2_config_descriptor * 1955usb2_get_config_descriptor(struct usb2_device *udev) 1956{ 1957 if (udev == NULL) 1958 return (NULL); /* be NULL safe */ 1959 return (udev->cdesc); 1960} 1961 1962/*------------------------------------------------------------------------* 1963 * usb2_test_quirk - test a device for a given quirk 1964 * 1965 * Return values: 1966 * 0: The USB device does not have the given quirk. 1967 * Else: The USB device has the given quirk. 1968 *------------------------------------------------------------------------*/ 1969uint8_t 1970usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk) 1971{ 1972 uint8_t found; 1973 1974 found = (usb2_test_quirk_p) (&uaa->info, quirk); 1975 return (found); 1976} 1977 1978struct usb2_interface_descriptor * 1979usb2_get_interface_descriptor(struct usb2_interface *iface) 1980{ 1981 if (iface == NULL) 1982 return (NULL); /* be NULL safe */ 1983 return (iface->idesc); 1984} 1985 1986uint8_t 1987usb2_get_interface_altindex(struct usb2_interface *iface) 1988{ 1989 return (iface->alt_index); 1990} 1991 1992uint8_t 1993usb2_get_bus_index(struct usb2_device *udev) 1994{ 1995 return ((uint8_t)device_get_unit(udev->bus->bdev)); 1996} 1997 1998uint8_t 1999usb2_get_device_index(struct usb2_device *udev) 2000{ 2001 return (udev->device_index); 2002} 2003 2004/*------------------------------------------------------------------------* 2005 * usb2_notify_addq 2006 * 2007 * This function will generate events for dev. 2008 *------------------------------------------------------------------------*/ 2009static void 2010usb2_notify_addq(const char *type, struct usb2_device *udev) 2011{ 2012 char *data = NULL; 2013 struct malloc_type *mt; 2014 2015 mtx_lock(&malloc_mtx); 2016 mt = malloc_desc2type("bus"); /* XXX M_BUS */ 2017 mtx_unlock(&malloc_mtx); 2018 if (mt == NULL) 2019 return; 2020 2021 data = malloc(512, mt, M_NOWAIT); 2022 if (data == NULL) 2023 return; 2024 2025 /* String it all together. */ 2026 if (udev->parent_hub) { 2027 snprintf(data, 1024, 2028 "%s" 2029 "ugen%u.%u " 2030 "vendor=0x%04x " 2031 "product=0x%04x " 2032 "devclass=0x%02x " 2033 "devsubclass=0x%02x " 2034 "sernum=\"%s\" " 2035 "at " 2036 "port=%u " 2037 "on " 2038 "ugen%u.%u\n", 2039 type, 2040 device_get_unit(udev->bus->bdev), 2041 udev->device_index, 2042 UGETW(udev->ddesc.idVendor), 2043 UGETW(udev->ddesc.idProduct), 2044 udev->ddesc.bDeviceClass, 2045 udev->ddesc.bDeviceSubClass, 2046 udev->serial, 2047 udev->port_no, 2048 device_get_unit(udev->bus->bdev), 2049 udev->parent_hub->device_index); 2050 } else { 2051 snprintf(data, 1024, 2052 "%s" 2053 "ugen%u.%u " 2054 "vendor=0x%04x " 2055 "product=0x%04x " 2056 "devclass=0x%02x " 2057 "devsubclass=0x%02x " 2058 "sernum=\"%s\" " 2059 "at port=%u " 2060 "on " 2061 "%s\n", 2062 type, 2063 device_get_unit(udev->bus->bdev), 2064 udev->device_index, 2065 UGETW(udev->ddesc.idVendor), 2066 UGETW(udev->ddesc.idProduct), 2067 udev->ddesc.bDeviceClass, 2068 udev->ddesc.bDeviceSubClass, 2069 udev->serial, 2070 udev->port_no, 2071 device_get_nameunit(device_get_parent(udev->bus->bdev))); 2072 } 2073 devctl_queue_data(data); 2074 return; 2075} 2076 2077/*------------------------------------------------------------------------* 2078 * usb2_fifo_free_wrap 2079 * 2080 * The function will free the FIFOs. 2081 *------------------------------------------------------------------------*/ 2082static void 2083usb2_fifo_free_wrap(struct usb2_device *udev, 2084 uint8_t iface_index, uint8_t free_all) 2085{ 2086 struct usb2_fifo *f; 2087 struct usb2_pipe *pipe; 2088 uint16_t i; 2089 2090 /* 2091 * Free any USB FIFOs on the given interface: 2092 */ 2093 for (i = 0; i != USB_FIFO_MAX; i++) { 2094 f = udev->fifo[i]; 2095 if (f == NULL) { 2096 continue; 2097 } 2098 pipe = f->priv_sc0; 2099 if ((pipe == &udev->default_pipe) && (free_all == 0)) { 2100 /* don't free UGEN control endpoint yet */ 2101 continue; 2102 } 2103 /* Check if the interface index matches */ 2104 if ((iface_index == f->iface_index) || 2105 (iface_index == USB_IFACE_INDEX_ANY)) { 2106 usb2_fifo_free(f); 2107 } 2108 } 2109 return; 2110} 2111