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