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