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