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