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