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