usb_device.c revision 191397
1/* $FreeBSD: head/sys/dev/usb/usb_device.c 191397 2009-04-22 17:07:59Z 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 if (err) 1115 device_printf(iface->subdev, "Suspend failed\n"); 1116 } 1117 return (0); /* success */ 1118 } else { 1119 /* No USB driver found */ 1120 iface->subdev = NULL; 1121 } 1122 return (1); /* failure */ 1123} 1124 1125/*------------------------------------------------------------------------* 1126 * usb2_set_parent_iface 1127 * 1128 * Using this function will lock the alternate interface setting on an 1129 * interface. It is typically used for multi interface drivers. In USB 1130 * device side mode it is assumed that the alternate interfaces all 1131 * have the same endpoint descriptors. The default parent index value 1132 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 1133 * locked. 1134 *------------------------------------------------------------------------*/ 1135void 1136usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index, 1137 uint8_t parent_index) 1138{ 1139 struct usb2_interface *iface; 1140 1141 iface = usb2_get_iface(udev, iface_index); 1142 if (iface) { 1143 iface->parent_iface_index = parent_index; 1144 } 1145} 1146 1147static void 1148usb2_init_attach_arg(struct usb2_device *udev, 1149 struct usb2_attach_arg *uaa) 1150{ 1151 bzero(uaa, sizeof(*uaa)); 1152 1153 uaa->device = udev; 1154 uaa->usb2_mode = udev->flags.usb2_mode; 1155 uaa->port = udev->port_no; 1156 1157 uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 1158 uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 1159 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 1160 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 1161 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 1162 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 1163 uaa->info.bConfigIndex = udev->curr_config_index; 1164 uaa->info.bConfigNum = udev->curr_config_no; 1165} 1166 1167/*------------------------------------------------------------------------* 1168 * usb2_probe_and_attach 1169 * 1170 * This function is called from "uhub_explore_sub()", 1171 * "usb2_handle_set_config()" and "usb2_handle_request()". 1172 * 1173 * Returns: 1174 * 0: Success 1175 * Else: A control transfer failed 1176 *------------------------------------------------------------------------*/ 1177usb2_error_t 1178usb2_probe_and_attach(struct usb2_device *udev, uint8_t iface_index) 1179{ 1180 struct usb2_attach_arg uaa; 1181 struct usb2_interface *iface; 1182 uint8_t i; 1183 uint8_t j; 1184 uint8_t do_unlock; 1185 1186 if (udev == NULL) { 1187 DPRINTF("udev == NULL\n"); 1188 return (USB_ERR_INVAL); 1189 } 1190 /* automatic locking */ 1191 if (sx_xlocked(udev->default_sx + 1)) { 1192 do_unlock = 0; 1193 } else { 1194 do_unlock = 1; 1195 sx_xlock(udev->default_sx + 1); 1196 } 1197 1198 if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 1199 /* do nothing - no configuration has been set */ 1200 goto done; 1201 } 1202 /* setup USB attach arguments */ 1203 1204 usb2_init_attach_arg(udev, &uaa); 1205 1206 /* Check if only one interface should be probed: */ 1207 if (iface_index != USB_IFACE_INDEX_ANY) { 1208 i = iface_index; 1209 j = i + 1; 1210 } else { 1211 i = 0; 1212 j = USB_IFACE_MAX; 1213 } 1214 1215 /* Do the probe and attach */ 1216 for (; i != j; i++) { 1217 1218 iface = usb2_get_iface(udev, i); 1219 if (iface == NULL) { 1220 /* 1221 * Looks like the end of the USB 1222 * interfaces ! 1223 */ 1224 DPRINTFN(2, "end of interfaces " 1225 "at %u\n", i); 1226 break; 1227 } 1228 if (iface->idesc == NULL) { 1229 /* no interface descriptor */ 1230 continue; 1231 } 1232 uaa.iface = iface; 1233 1234 uaa.info.bInterfaceClass = 1235 iface->idesc->bInterfaceClass; 1236 uaa.info.bInterfaceSubClass = 1237 iface->idesc->bInterfaceSubClass; 1238 uaa.info.bInterfaceProtocol = 1239 iface->idesc->bInterfaceProtocol; 1240 uaa.info.bIfaceIndex = i; 1241 uaa.info.bIfaceNum = 1242 iface->idesc->bInterfaceNumber; 1243 uaa.use_generic = 0; 1244 1245 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 1246 uaa.info.bInterfaceClass, 1247 uaa.info.bInterfaceSubClass, 1248 uaa.info.bInterfaceProtocol, 1249 uaa.info.bIfaceIndex, 1250 uaa.info.bIfaceNum); 1251 1252 /* try specific interface drivers first */ 1253 1254 if (usb2_probe_and_attach_sub(udev, &uaa)) { 1255 /* ignore */ 1256 } 1257 /* try generic interface drivers last */ 1258 1259 uaa.use_generic = 1; 1260 1261 if (usb2_probe_and_attach_sub(udev, &uaa)) { 1262 /* ignore */ 1263 } 1264 } 1265 1266 if (uaa.temp_dev) { 1267 /* remove the last created child; it is unused */ 1268 1269 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) { 1270 DPRINTFN(0, "device delete child failed!\n"); 1271 } 1272 } 1273done: 1274 if (do_unlock) { 1275 sx_unlock(udev->default_sx + 1); 1276 } 1277 return (0); 1278} 1279 1280/*------------------------------------------------------------------------* 1281 * usb2_suspend_resume_sub 1282 * 1283 * This function is called when the suspend or resume methods should 1284 * be executed on an USB device. 1285 *------------------------------------------------------------------------*/ 1286static void 1287usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend) 1288{ 1289 int err; 1290 1291 if (dev == NULL) { 1292 return; 1293 } 1294 if (!device_is_attached(dev)) { 1295 return; 1296 } 1297 if (do_suspend) { 1298 err = DEVICE_SUSPEND(dev); 1299 } else { 1300 err = DEVICE_RESUME(dev); 1301 } 1302 if (err) { 1303 device_printf(dev, "%s failed!\n", 1304 do_suspend ? "Suspend" : "Resume"); 1305 } 1306} 1307 1308/*------------------------------------------------------------------------* 1309 * usb2_suspend_resume 1310 * 1311 * The following function will suspend or resume the USB device. 1312 * 1313 * Returns: 1314 * 0: Success 1315 * Else: Failure 1316 *------------------------------------------------------------------------*/ 1317usb2_error_t 1318usb2_suspend_resume(struct usb2_device *udev, uint8_t do_suspend) 1319{ 1320 struct usb2_interface *iface; 1321 uint8_t i; 1322 1323 if (udev == NULL) { 1324 /* nothing to do */ 1325 return (0); 1326 } 1327 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 1328 1329 sx_assert(udev->default_sx + 1, SA_LOCKED); 1330 1331 USB_BUS_LOCK(udev->bus); 1332 /* filter the suspend events */ 1333 if (udev->flags.suspended == do_suspend) { 1334 USB_BUS_UNLOCK(udev->bus); 1335 /* nothing to do */ 1336 return (0); 1337 } 1338 udev->flags.suspended = do_suspend; 1339 USB_BUS_UNLOCK(udev->bus); 1340 1341 /* do the suspend or resume */ 1342 1343 for (i = 0; i != USB_IFACE_MAX; i++) { 1344 1345 iface = usb2_get_iface(udev, i); 1346 if (iface == NULL) { 1347 /* looks like the end of the USB interfaces */ 1348 break; 1349 } 1350 usb2_suspend_resume_sub(udev, iface->subdev, do_suspend); 1351 } 1352 return (0); 1353} 1354 1355/*------------------------------------------------------------------------* 1356 * usb2_clear_stall_proc 1357 * 1358 * This function performs generic USB clear stall operations. 1359 *------------------------------------------------------------------------*/ 1360static void 1361usb2_clear_stall_proc(struct usb2_proc_msg *_pm) 1362{ 1363 struct usb2_clear_stall_msg *pm = (void *)_pm; 1364 struct usb2_device *udev = pm->udev; 1365 1366 /* Change lock */ 1367 USB_BUS_UNLOCK(udev->bus); 1368 mtx_lock(udev->default_mtx); 1369 1370 /* Start clear stall callback */ 1371 usb2_transfer_start(udev->default_xfer[1]); 1372 1373 /* Change lock */ 1374 mtx_unlock(udev->default_mtx); 1375 USB_BUS_LOCK(udev->bus); 1376} 1377 1378/*------------------------------------------------------------------------* 1379 * usb2_alloc_device 1380 * 1381 * This function allocates a new USB device. This function is called 1382 * when a new device has been put in the powered state, but not yet in 1383 * the addressed state. Get initial descriptor, set the address, get 1384 * full descriptor and get strings. 1385 * 1386 * Return values: 1387 * 0: Failure 1388 * Else: Success 1389 *------------------------------------------------------------------------*/ 1390struct usb2_device * 1391usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus, 1392 struct usb2_device *parent_hub, uint8_t depth, 1393 uint8_t port_index, uint8_t port_no, uint8_t speed, uint8_t usb2_mode) 1394{ 1395 struct usb2_attach_arg uaa; 1396 struct usb2_device *udev; 1397 struct usb2_device *adev; 1398 struct usb2_device *hub; 1399 uint8_t *scratch_ptr; 1400 uint32_t scratch_size; 1401 usb2_error_t err; 1402 uint8_t device_index; 1403 1404 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1405 "port_index=%u, port_no=%u, speed=%u, usb2_mode=%u\n", 1406 parent_dev, bus, parent_hub, depth, port_index, port_no, 1407 speed, usb2_mode); 1408 1409 /* 1410 * Find an unused device index. In USB Host mode this is the 1411 * same as the device address. 1412 * 1413 * Device index zero is not used and device index 1 should 1414 * always be the root hub. 1415 */ 1416 for (device_index = USB_ROOT_HUB_ADDR; 1417 (device_index != bus->devices_max) && 1418 (bus->devices[device_index] != NULL); 1419 device_index++) /* nop */; 1420 1421 if (device_index == bus->devices_max) { 1422 device_printf(bus->bdev, 1423 "No free USB device index for new device!\n"); 1424 return (NULL); 1425 } 1426 1427 if (depth > 0x10) { 1428 device_printf(bus->bdev, 1429 "Invalid device depth!\n"); 1430 return (NULL); 1431 } 1432 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1433 if (udev == NULL) { 1434 return (NULL); 1435 } 1436 /* initialise our SX-lock */ 1437 sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth); 1438 1439 /* initialise our SX-lock */ 1440 sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth); 1441 1442 usb2_cv_init(udev->default_cv, "WCTRL"); 1443 usb2_cv_init(udev->default_cv + 1, "UGONE"); 1444 1445 /* initialise our mutex */ 1446 mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF); 1447 1448 /* initialise generic clear stall */ 1449 udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc; 1450 udev->cs_msg[0].udev = udev; 1451 udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc; 1452 udev->cs_msg[1].udev = udev; 1453 1454 /* initialise some USB device fields */ 1455 udev->parent_hub = parent_hub; 1456 udev->parent_dev = parent_dev; 1457 udev->port_index = port_index; 1458 udev->port_no = port_no; 1459 udev->depth = depth; 1460 udev->bus = bus; 1461 udev->address = USB_START_ADDR; /* default value */ 1462 udev->plugtime = (usb2_ticks_t)ticks; 1463 /* 1464 * We need to force the power mode to "on" because there are plenty 1465 * of USB devices out there that do not work very well with 1466 * automatic suspend and resume! 1467 */ 1468 udev->power_mode = USB_POWER_MODE_ON; 1469 udev->pwr_save.last_xfer_time = ticks; 1470 /* we are not ready yet */ 1471 udev->refcount = 1; 1472 1473 /* set up default endpoint descriptor */ 1474 udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc); 1475 udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1476 udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1477 udev->default_ep_desc.bmAttributes = UE_CONTROL; 1478 udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 1479 udev->default_ep_desc.wMaxPacketSize[1] = 0; 1480 udev->default_ep_desc.bInterval = 0; 1481 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1482 1483 udev->speed = speed; 1484 udev->flags.usb2_mode = usb2_mode; 1485 1486 /* search for our High Speed USB HUB, if any */ 1487 1488 adev = udev; 1489 hub = udev->parent_hub; 1490 1491 while (hub) { 1492 if (hub->speed == USB_SPEED_HIGH) { 1493 udev->hs_hub_addr = hub->address; 1494 udev->parent_hs_hub = hub; 1495 udev->hs_port_no = adev->port_no; 1496 break; 1497 } 1498 adev = hub; 1499 hub = hub->parent_hub; 1500 } 1501 1502 /* init the default pipe */ 1503 usb2_init_pipe(udev, 0, 1504 &udev->default_ep_desc, 1505 &udev->default_pipe); 1506 1507 /* set device index */ 1508 udev->device_index = device_index; 1509 1510#if USB_HAVE_UGEN 1511 /* Create ugen name */ 1512 snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1513 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1514 device_index); 1515 LIST_INIT(&udev->pd_list); 1516 1517 /* Create the control endpoint device */ 1518 udev->default_dev = usb2_make_dev(udev, 0, FREAD|FWRITE); 1519 1520 /* Create a link from /dev/ugenX.X to the default endpoint */ 1521 make_dev_alias(udev->default_dev, udev->ugen_name); 1522#endif 1523 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1524 1525 err = usb2_req_set_address(udev, NULL, device_index); 1526 1527 /* This is the new USB device address from now on */ 1528 1529 udev->address = device_index; 1530 1531 /* 1532 * We ignore any set-address errors, hence there are 1533 * buggy USB devices out there that actually receive 1534 * the SETUP PID, but manage to set the address before 1535 * the STATUS stage is ACK'ed. If the device responds 1536 * to the subsequent get-descriptor at the new 1537 * address, then we know that the set-address command 1538 * was successful. 1539 */ 1540 if (err) { 1541 DPRINTFN(0, "set address %d failed " 1542 "(%s, ignored)\n", udev->address, 1543 usb2_errstr(err)); 1544 } 1545 /* allow device time to set new address */ 1546 usb2_pause_mtx(NULL, 1547 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1548 } else { 1549 /* We are not self powered */ 1550 udev->flags.self_powered = 0; 1551 1552 /* Set unconfigured state */ 1553 udev->curr_config_no = USB_UNCONFIG_NO; 1554 udev->curr_config_index = USB_UNCONFIG_INDEX; 1555 1556 /* Setup USB descriptors */ 1557 err = (usb2_temp_setup_by_index_p) (udev, usb2_template); 1558 if (err) { 1559 DPRINTFN(0, "setting up USB template failed maybe the USB " 1560 "template module has not been loaded\n"); 1561 goto done; 1562 } 1563 } 1564 1565 /* 1566 * Get the first 8 bytes of the device descriptor ! 1567 * 1568 * NOTE: "usb2_do_request" will check the device descriptor 1569 * next time we do a request to see if the maximum packet size 1570 * changed! The 8 first bytes of the device descriptor 1571 * contains the maximum packet size to use on control endpoint 1572 * 0. If this value is different from "USB_MAX_IPACKET" a new 1573 * USB control request will be setup! 1574 */ 1575 err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc, 1576 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1577 if (err) { 1578 DPRINTFN(0, "getting device descriptor " 1579 "at addr %d failed, %s!\n", udev->address, 1580 usb2_errstr(err)); 1581 /* XXX try to re-enumerate the device */ 1582 err = usb2_req_re_enumerate(udev, NULL); 1583 if (err) { 1584 goto done; 1585 } 1586 } 1587 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1588 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1589 udev->address, UGETW(udev->ddesc.bcdUSB), 1590 udev->ddesc.bDeviceClass, 1591 udev->ddesc.bDeviceSubClass, 1592 udev->ddesc.bDeviceProtocol, 1593 udev->ddesc.bMaxPacketSize, 1594 udev->ddesc.bLength, 1595 udev->speed); 1596 1597 /* get the full device descriptor */ 1598 err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc); 1599 if (err) { 1600 DPRINTF("addr=%d, getting full desc failed\n", 1601 udev->address); 1602 goto done; 1603 } 1604 /* 1605 * Setup temporary USB attach args so that we can figure out some 1606 * basic quirks for this device. 1607 */ 1608 usb2_init_attach_arg(udev, &uaa); 1609 1610 if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) { 1611 udev->flags.uq_bus_powered = 1; 1612 } 1613 if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) { 1614 udev->flags.no_strings = 1; 1615 } 1616 /* 1617 * Workaround for buggy USB devices. 1618 * 1619 * It appears that some string-less USB chips will crash and 1620 * disappear if any attempts are made to read any string 1621 * descriptors. 1622 * 1623 * Try to detect such chips by checking the strings in the USB 1624 * device descriptor. If no strings are present there we 1625 * simply disable all USB strings. 1626 */ 1627 scratch_ptr = udev->bus->scratch[0].data; 1628 scratch_size = sizeof(udev->bus->scratch[0].data); 1629 1630 if (udev->ddesc.iManufacturer || 1631 udev->ddesc.iProduct || 1632 udev->ddesc.iSerialNumber) { 1633 /* read out the language ID string */ 1634 err = usb2_req_get_string_desc(udev, NULL, 1635 (char *)scratch_ptr, 4, scratch_size, 1636 USB_LANGUAGE_TABLE); 1637 } else { 1638 err = USB_ERR_INVAL; 1639 } 1640 1641 if (err || (scratch_ptr[0] < 4)) { 1642 udev->flags.no_strings = 1; 1643 } else { 1644 /* pick the first language as the default */ 1645 udev->langid = UGETW(scratch_ptr + 2); 1646 } 1647 1648 /* assume 100mA bus powered for now. Changed when configured. */ 1649 udev->power = USB_MIN_POWER; 1650 1651#if USB_HAVE_STRINGS 1652 /* get serial number string */ 1653 err = usb2_req_get_string_any 1654 (udev, NULL, (char *)scratch_ptr, 1655 scratch_size, udev->ddesc.iSerialNumber); 1656 1657 strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial)); 1658 1659 /* get manufacturer string */ 1660 err = usb2_req_get_string_any 1661 (udev, NULL, (char *)scratch_ptr, 1662 scratch_size, udev->ddesc.iManufacturer); 1663 1664 strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer)); 1665 1666 /* get product string */ 1667 err = usb2_req_get_string_any 1668 (udev, NULL, (char *)scratch_ptr, 1669 scratch_size, udev->ddesc.iProduct); 1670 1671 strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product)); 1672 1673 /* finish up all the strings */ 1674 usb2_check_strings(udev); 1675#endif 1676 1677 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1678 uint8_t config_index; 1679 uint8_t config_quirk; 1680 uint8_t set_config_failed = 0; 1681 1682 /* 1683 * Most USB devices should attach to config index 0 by 1684 * default 1685 */ 1686 if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1687 config_index = 0; 1688 config_quirk = 1; 1689 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1690 config_index = 1; 1691 config_quirk = 1; 1692 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1693 config_index = 2; 1694 config_quirk = 1; 1695 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1696 config_index = 3; 1697 config_quirk = 1; 1698 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1699 config_index = 4; 1700 config_quirk = 1; 1701 } else { 1702 config_index = 0; 1703 config_quirk = 0; 1704 } 1705 1706repeat_set_config: 1707 1708 DPRINTF("setting config %u\n", config_index); 1709 1710 /* get the USB device configured */ 1711 err = usb2_set_config_index(udev, config_index); 1712 if (err) { 1713 if (udev->ddesc.bNumConfigurations != 0) { 1714 if (!set_config_failed) { 1715 set_config_failed = 1; 1716 /* XXX try to re-enumerate the device */ 1717 err = usb2_req_re_enumerate( 1718 udev, NULL); 1719 if (err == 0) 1720 goto repeat_set_config; 1721 } 1722 DPRINTFN(0, "Failure selecting " 1723 "configuration index %u: %s, port %u, " 1724 "addr %u (ignored)\n", 1725 config_index, usb2_errstr(err), udev->port_no, 1726 udev->address); 1727 } 1728 /* 1729 * Some USB devices do not have any 1730 * configurations. Ignore any set config 1731 * failures! 1732 */ 1733 err = 0; 1734 } else if (config_quirk) { 1735 /* user quirk selects configuration index */ 1736 } else if ((config_index + 1) < udev->ddesc.bNumConfigurations) { 1737 1738 if ((udev->cdesc->bNumInterface < 2) && 1739 (usb2_get_no_descriptors(udev->cdesc, 1740 UDESC_ENDPOINT) == 0)) { 1741 DPRINTFN(0, "Found no endpoints " 1742 "(trying next config)!\n"); 1743 config_index++; 1744 goto repeat_set_config; 1745 } 1746 if (config_index == 0) { 1747 /* 1748 * Try to figure out if we have an 1749 * auto-install disk there: 1750 */ 1751 if (usb2_test_autoinstall(udev, 0, 0) == 0) { 1752 DPRINTFN(0, "Found possible auto-install " 1753 "disk (trying next config)\n"); 1754 config_index++; 1755 goto repeat_set_config; 1756 } 1757 } 1758 } else if (usb2_test_huawei_autoinst_p(udev, &uaa) == 0) { 1759 DPRINTFN(0, "Found Huawei auto-install disk!\n"); 1760 err = USB_ERR_STALLED; /* fake an error */ 1761 } 1762 } else { 1763 err = 0; /* set success */ 1764 } 1765 1766 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1767 udev->address, udev, udev->parent_hub); 1768 1769 /* register our device - we are ready */ 1770 usb2_bus_port_set_device(bus, parent_hub ? 1771 parent_hub->hub->ports + port_index : NULL, udev, device_index); 1772 1773#if USB_HAVE_UGEN 1774 /* Symlink the ugen device name */ 1775 udev->ugen_symlink = usb2_alloc_symlink(udev->ugen_name); 1776 1777 /* Announce device */ 1778 printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer, 1779 device_get_nameunit(udev->bus->bdev)); 1780 1781 usb2_notify_addq("+", udev); 1782#endif 1783done: 1784 if (err) { 1785 /* free device */ 1786 usb2_free_device(udev, 1787 USB_UNCFG_FLAG_FREE_SUBDEV | 1788 USB_UNCFG_FLAG_FREE_EP0); 1789 udev = NULL; 1790 } 1791 return (udev); 1792} 1793 1794#if USB_HAVE_UGEN 1795static struct cdev * 1796usb2_make_dev(struct usb2_device *udev, int ep, int mode) 1797{ 1798 struct usb2_fs_privdata* pd; 1799 char devname[20]; 1800 1801 /* Store information to locate ourselves again later */ 1802 pd = malloc(sizeof(struct usb2_fs_privdata), M_USBDEV, 1803 M_WAITOK | M_ZERO); 1804 pd->bus_index = device_get_unit(udev->bus->bdev); 1805 pd->dev_index = udev->device_index; 1806 pd->ep_addr = ep; 1807 pd->mode = mode; 1808 1809 /* Now, create the device itself */ 1810 snprintf(devname, sizeof(devname), "%u.%u.%u", 1811 pd->bus_index, pd->dev_index, pd->ep_addr); 1812 pd->cdev = make_dev(&usb2_devsw, 0, UID_ROOT, 1813 GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname); 1814 pd->cdev->si_drv1 = pd; 1815 1816 return (pd->cdev); 1817} 1818 1819static void 1820usb2_cdev_create(struct usb2_device *udev) 1821{ 1822 struct usb2_config_descriptor *cd; 1823 struct usb2_endpoint_descriptor *ed; 1824 struct usb2_descriptor *desc; 1825 struct usb2_fs_privdata* pd; 1826 struct cdev *dev; 1827 int inmode, outmode, inmask, outmask, mode; 1828 uint8_t ep; 1829 1830 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 1831 1832 DPRINTFN(2, "Creating device nodes\n"); 1833 1834 if (usb2_get_mode(udev) == USB_MODE_DEVICE) { 1835 inmode = FWRITE; 1836 outmode = FREAD; 1837 } else { /* USB_MODE_HOST */ 1838 inmode = FREAD; 1839 outmode = FWRITE; 1840 } 1841 1842 inmask = 0; 1843 outmask = 0; 1844 desc = NULL; 1845 1846 /* 1847 * Collect all used endpoint numbers instead of just 1848 * generating 16 static endpoints. 1849 */ 1850 cd = usb2_get_config_descriptor(udev); 1851 while ((desc = usb2_desc_foreach(cd, desc))) { 1852 /* filter out all endpoint descriptors */ 1853 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 1854 (desc->bLength >= sizeof(*ed))) { 1855 ed = (struct usb2_endpoint_descriptor *)desc; 1856 1857 /* update masks */ 1858 ep = ed->bEndpointAddress; 1859 if (UE_GET_DIR(ep) == UE_DIR_OUT) 1860 outmask |= 1 << UE_GET_ADDR(ep); 1861 else 1862 inmask |= 1 << UE_GET_ADDR(ep); 1863 } 1864 } 1865 1866 /* Create all available endpoints except EP0 */ 1867 for (ep = 1; ep < 16; ep++) { 1868 mode = inmask & (1 << ep) ? inmode : 0; 1869 mode |= outmask & (1 << ep) ? outmode : 0; 1870 if (mode == 0) 1871 continue; /* no IN or OUT endpoint */ 1872 1873 dev = usb2_make_dev(udev, ep, mode); 1874 pd = dev->si_drv1; 1875 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 1876 } 1877} 1878 1879static void 1880usb2_cdev_free(struct usb2_device *udev) 1881{ 1882 struct usb2_fs_privdata* pd; 1883 1884 DPRINTFN(2, "Freeing device nodes\n"); 1885 1886 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 1887 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 1888 1889 destroy_dev_sched_cb(pd->cdev, usb2_cdev_cleanup, pd); 1890 pd->cdev = NULL; 1891 LIST_REMOVE(pd, pd_next); 1892 } 1893} 1894 1895static void 1896usb2_cdev_cleanup(void* arg) 1897{ 1898 free(arg, M_USBDEV); 1899} 1900#endif 1901 1902/*------------------------------------------------------------------------* 1903 * usb2_free_device 1904 * 1905 * This function is NULL safe and will free an USB device. 1906 * 1907 * Flag values, see "USB_UNCFG_FLAG_XXX". 1908 *------------------------------------------------------------------------*/ 1909void 1910usb2_free_device(struct usb2_device *udev, uint8_t flag) 1911{ 1912 struct usb2_bus *bus; 1913 1914 if (udev == NULL) 1915 return; /* already freed */ 1916 1917 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 1918 1919 bus = udev->bus;; 1920 1921#if USB_HAVE_UGEN 1922 usb2_notify_addq("-", udev); 1923 1924 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name, 1925 udev->manufacturer, device_get_nameunit(bus->bdev)); 1926 1927 /* Destroy UGEN symlink, if any */ 1928 if (udev->ugen_symlink) { 1929 usb2_free_symlink(udev->ugen_symlink); 1930 udev->ugen_symlink = NULL; 1931 } 1932#endif 1933 /* 1934 * Unregister our device first which will prevent any further 1935 * references: 1936 */ 1937 usb2_bus_port_set_device(bus, udev->parent_hub ? 1938 udev->parent_hub->hub->ports + udev->port_index : NULL, 1939 NULL, USB_ROOT_HUB_ADDR); 1940 1941#if USB_HAVE_UGEN 1942 /* wait for all pending references to go away: */ 1943 mtx_lock(&usb2_ref_lock); 1944 udev->refcount--; 1945 while (udev->refcount != 0) { 1946 usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock); 1947 } 1948 mtx_unlock(&usb2_ref_lock); 1949 1950 destroy_dev_sched_cb(udev->default_dev, usb2_cdev_cleanup, 1951 udev->default_dev->si_drv1); 1952#endif 1953 1954 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 1955 /* stop receiving any control transfers (Device Side Mode) */ 1956 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1957 } 1958 1959 /* the following will get the device unconfigured in software */ 1960 usb2_unconfigure(udev, flag); 1961 1962 /* unsetup any leftover default USB transfers */ 1963 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1964 1965 /* template unsetup, if any */ 1966 (usb2_temp_unsetup_p) (udev); 1967 1968 /* 1969 * Make sure that our clear-stall messages are not queued 1970 * anywhere: 1971 */ 1972 USB_BUS_LOCK(udev->bus); 1973 usb2_proc_mwait(&udev->bus->non_giant_callback_proc, 1974 &udev->cs_msg[0], &udev->cs_msg[1]); 1975 USB_BUS_UNLOCK(udev->bus); 1976 1977 sx_destroy(udev->default_sx); 1978 sx_destroy(udev->default_sx + 1); 1979 1980 usb2_cv_destroy(udev->default_cv); 1981 usb2_cv_destroy(udev->default_cv + 1); 1982 1983 mtx_destroy(udev->default_mtx); 1984#if USB_HAVE_UGEN 1985 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 1986#endif 1987 1988 /* free device */ 1989 free(udev, M_USB); 1990} 1991 1992/*------------------------------------------------------------------------* 1993 * usb2_get_iface 1994 * 1995 * This function is the safe way to get the USB interface structure 1996 * pointer by interface index. 1997 * 1998 * Return values: 1999 * NULL: Interface not present. 2000 * Else: Pointer to USB interface structure. 2001 *------------------------------------------------------------------------*/ 2002struct usb2_interface * 2003usb2_get_iface(struct usb2_device *udev, uint8_t iface_index) 2004{ 2005 struct usb2_interface *iface = udev->ifaces + iface_index; 2006 2007 if (iface_index >= udev->ifaces_max) 2008 return (NULL); 2009 return (iface); 2010} 2011 2012/*------------------------------------------------------------------------* 2013 * usb2_find_descriptor 2014 * 2015 * This function will lookup the first descriptor that matches the 2016 * criteria given by the arguments "type" and "subtype". Descriptors 2017 * will only be searched within the interface having the index 2018 * "iface_index". If the "id" argument points to an USB descriptor, 2019 * it will be skipped before the search is started. This allows 2020 * searching for multiple descriptors using the same criteria. Else 2021 * the search is started after the interface descriptor. 2022 * 2023 * Return values: 2024 * NULL: End of descriptors 2025 * Else: A descriptor matching the criteria 2026 *------------------------------------------------------------------------*/ 2027void * 2028usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index, 2029 uint8_t type, uint8_t type_mask, 2030 uint8_t subtype, uint8_t subtype_mask) 2031{ 2032 struct usb2_descriptor *desc; 2033 struct usb2_config_descriptor *cd; 2034 struct usb2_interface *iface; 2035 2036 cd = usb2_get_config_descriptor(udev); 2037 if (cd == NULL) { 2038 return (NULL); 2039 } 2040 if (id == NULL) { 2041 iface = usb2_get_iface(udev, iface_index); 2042 if (iface == NULL) { 2043 return (NULL); 2044 } 2045 id = usb2_get_interface_descriptor(iface); 2046 if (id == NULL) { 2047 return (NULL); 2048 } 2049 } 2050 desc = (void *)id; 2051 2052 while ((desc = usb2_desc_foreach(cd, desc))) { 2053 2054 if (desc->bDescriptorType == UDESC_INTERFACE) { 2055 break; 2056 } 2057 if (((desc->bDescriptorType & type_mask) == type) && 2058 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 2059 return (desc); 2060 } 2061 } 2062 return (NULL); 2063} 2064 2065/*------------------------------------------------------------------------* 2066 * usb2_devinfo 2067 * 2068 * This function will dump information from the device descriptor 2069 * belonging to the USB device pointed to by "udev", to the string 2070 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 2071 * including the terminating zero. 2072 *------------------------------------------------------------------------*/ 2073void 2074usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len) 2075{ 2076 struct usb2_device_descriptor *udd = &udev->ddesc; 2077 uint16_t bcdDevice; 2078 uint16_t bcdUSB; 2079 2080 bcdUSB = UGETW(udd->bcdUSB); 2081 bcdDevice = UGETW(udd->bcdDevice); 2082 2083 if (udd->bDeviceClass != 0xFF) { 2084 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 2085 "%x.%02x, addr %d", 2086#if USB_HAVE_STRINGS 2087 udev->manufacturer, udev->product, 2088#else 2089 "-", "-", 2090#endif 2091 udd->bDeviceClass, udd->bDeviceSubClass, 2092 (bcdUSB >> 8), bcdUSB & 0xFF, 2093 (bcdDevice >> 8), bcdDevice & 0xFF, 2094 udev->address); 2095 } else { 2096 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 2097 "%x.%02x, addr %d", 2098#if USB_HAVE_STRINGS 2099 udev->manufacturer, udev->product, 2100#else 2101 "-", "-", 2102#endif 2103 (bcdUSB >> 8), bcdUSB & 0xFF, 2104 (bcdDevice >> 8), bcdDevice & 0xFF, 2105 udev->address); 2106 } 2107} 2108 2109#if USB_HAVE_STRINGS 2110#if USB_VERBOSE 2111/* 2112 * Descriptions of of known vendors and devices ("products"). 2113 */ 2114struct usb_knowndev { 2115 uint16_t vendor; 2116 uint16_t product; 2117 uint32_t flags; 2118 const char *vendorname; 2119 const char *productname; 2120}; 2121 2122#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 2123 2124#include "usbdevs.h" 2125#include "usbdevs_data.h" 2126#endif /* USB_VERBOSE */ 2127 2128/*------------------------------------------------------------------------* 2129 * usb2_check_strings 2130 * 2131 * This function checks the manufacturer and product strings and will 2132 * fill in defaults for missing strings. 2133 *------------------------------------------------------------------------*/ 2134static void 2135usb2_check_strings(struct usb2_device *udev) 2136{ 2137 struct usb2_device_descriptor *udd = &udev->ddesc; 2138 const char *vendor; 2139 const char *product; 2140 2141#if USB_VERBOSE 2142 const struct usb_knowndev *kdp; 2143 2144#endif 2145 uint16_t vendor_id; 2146 uint16_t product_id; 2147 2148 usb2_trim_spaces(udev->manufacturer); 2149 usb2_trim_spaces(udev->product); 2150 2151 if (udev->manufacturer[0]) { 2152 vendor = udev->manufacturer; 2153 } else { 2154 vendor = NULL; 2155 } 2156 2157 if (udev->product[0]) { 2158 product = udev->product; 2159 } else { 2160 product = NULL; 2161 } 2162 2163 vendor_id = UGETW(udd->idVendor); 2164 product_id = UGETW(udd->idProduct); 2165 2166#if USB_VERBOSE 2167 if (vendor == NULL || product == NULL) { 2168 2169 for (kdp = usb_knowndevs; 2170 kdp->vendorname != NULL; 2171 kdp++) { 2172 if (kdp->vendor == vendor_id && 2173 (kdp->product == product_id || 2174 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 2175 break; 2176 } 2177 if (kdp->vendorname != NULL) { 2178 if (vendor == NULL) 2179 vendor = kdp->vendorname; 2180 if (product == NULL) 2181 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ? 2182 kdp->productname : NULL; 2183 } 2184 } 2185#endif 2186 if (vendor && *vendor) { 2187 if (udev->manufacturer != vendor) { 2188 strlcpy(udev->manufacturer, vendor, 2189 sizeof(udev->manufacturer)); 2190 } 2191 } else { 2192 snprintf(udev->manufacturer, 2193 sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id); 2194 } 2195 2196 if (product && *product) { 2197 if (udev->product != product) { 2198 strlcpy(udev->product, product, 2199 sizeof(udev->product)); 2200 } 2201 } else { 2202 snprintf(udev->product, 2203 sizeof(udev->product), "product 0x%04x", product_id); 2204 } 2205} 2206#endif 2207 2208/* 2209 * Returns: 2210 * See: USB_MODE_XXX 2211 */ 2212uint8_t 2213usb2_get_mode(struct usb2_device *udev) 2214{ 2215 return (udev->flags.usb2_mode); 2216} 2217 2218/* 2219 * Returns: 2220 * See: USB_SPEED_XXX 2221 */ 2222uint8_t 2223usb2_get_speed(struct usb2_device *udev) 2224{ 2225 return (udev->speed); 2226} 2227 2228uint32_t 2229usb2_get_isoc_fps(struct usb2_device *udev) 2230{ 2231 ; /* indent fix */ 2232 switch (udev->speed) { 2233 case USB_SPEED_LOW: 2234 case USB_SPEED_FULL: 2235 return (1000); 2236 default: 2237 return (8000); 2238 } 2239} 2240 2241struct usb2_device_descriptor * 2242usb2_get_device_descriptor(struct usb2_device *udev) 2243{ 2244 if (udev == NULL) 2245 return (NULL); /* be NULL safe */ 2246 return (&udev->ddesc); 2247} 2248 2249struct usb2_config_descriptor * 2250usb2_get_config_descriptor(struct usb2_device *udev) 2251{ 2252 if (udev == NULL) 2253 return (NULL); /* be NULL safe */ 2254 return (udev->cdesc); 2255} 2256 2257/*------------------------------------------------------------------------* 2258 * usb2_test_quirk - test a device for a given quirk 2259 * 2260 * Return values: 2261 * 0: The USB device does not have the given quirk. 2262 * Else: The USB device has the given quirk. 2263 *------------------------------------------------------------------------*/ 2264uint8_t 2265usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk) 2266{ 2267 uint8_t found; 2268 2269 found = (usb2_test_quirk_p) (&uaa->info, quirk); 2270 return (found); 2271} 2272 2273struct usb2_interface_descriptor * 2274usb2_get_interface_descriptor(struct usb2_interface *iface) 2275{ 2276 if (iface == NULL) 2277 return (NULL); /* be NULL safe */ 2278 return (iface->idesc); 2279} 2280 2281uint8_t 2282usb2_get_interface_altindex(struct usb2_interface *iface) 2283{ 2284 return (iface->alt_index); 2285} 2286 2287uint8_t 2288usb2_get_bus_index(struct usb2_device *udev) 2289{ 2290 return ((uint8_t)device_get_unit(udev->bus->bdev)); 2291} 2292 2293uint8_t 2294usb2_get_device_index(struct usb2_device *udev) 2295{ 2296 return (udev->device_index); 2297} 2298 2299#if USB_HAVE_UGEN 2300/*------------------------------------------------------------------------* 2301 * usb2_notify_addq 2302 * 2303 * This function will generate events for dev. 2304 *------------------------------------------------------------------------*/ 2305static void 2306usb2_notify_addq(const char *type, struct usb2_device *udev) 2307{ 2308 char *data = NULL; 2309 struct malloc_type *mt; 2310 2311 mtx_lock(&malloc_mtx); 2312 mt = malloc_desc2type("bus"); /* XXX M_BUS */ 2313 mtx_unlock(&malloc_mtx); 2314 if (mt == NULL) 2315 return; 2316 2317 data = malloc(512, mt, M_NOWAIT); 2318 if (data == NULL) 2319 return; 2320 2321 /* String it all together. */ 2322 snprintf(data, 1024, 2323 "%s" 2324 "%s " 2325 "vendor=0x%04x " 2326 "product=0x%04x " 2327 "devclass=0x%02x " 2328 "devsubclass=0x%02x " 2329 "sernum=\"%s\" " 2330 "at " 2331 "port=%u " 2332 "on " 2333 "%s\n", 2334 type, 2335 udev->ugen_name, 2336 UGETW(udev->ddesc.idVendor), 2337 UGETW(udev->ddesc.idProduct), 2338 udev->ddesc.bDeviceClass, 2339 udev->ddesc.bDeviceSubClass, 2340#if USB_HAVE_STRINGS 2341 udev->serial, 2342#else 2343 "", 2344#endif 2345 udev->port_no, 2346 udev->parent_hub != NULL ? 2347 udev->parent_hub->ugen_name : 2348 device_get_nameunit(device_get_parent(udev->bus->bdev))); 2349 2350 devctl_queue_data(data); 2351} 2352 2353/*------------------------------------------------------------------------* 2354 * usb2_fifo_free_wrap 2355 * 2356 * This function will free the FIFOs. 2357 * 2358 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2359 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2360 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2361 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2362 * control endpoint FIFOs. If "iface_index" is not set to 2363 * "USB_IFACE_INDEX_ANY" the flag has no effect. 2364 *------------------------------------------------------------------------*/ 2365static void 2366usb2_fifo_free_wrap(struct usb2_device *udev, 2367 uint8_t iface_index, uint8_t flag) 2368{ 2369 struct usb2_fifo *f; 2370 uint16_t i; 2371 2372 /* 2373 * Free any USB FIFOs on the given interface: 2374 */ 2375 for (i = 0; i != USB_FIFO_MAX; i++) { 2376 f = udev->fifo[i]; 2377 if (f == NULL) { 2378 continue; 2379 } 2380 /* Check if the interface index matches */ 2381 if (iface_index == f->iface_index) { 2382 if (f->methods != &usb2_ugen_methods) { 2383 /* 2384 * Don't free any non-generic FIFOs in 2385 * this case. 2386 */ 2387 continue; 2388 } 2389 if ((f->dev_ep_index == 0) && 2390 (f->fs_xfer == NULL)) { 2391 /* no need to free this FIFO */ 2392 continue; 2393 } 2394 } else if (iface_index == USB_IFACE_INDEX_ANY) { 2395 if ((f->methods == &usb2_ugen_methods) && 2396 (f->dev_ep_index == 0) && 2397 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 2398 (f->fs_xfer == NULL)) { 2399 /* no need to free this FIFO */ 2400 continue; 2401 } 2402 } else { 2403 /* no need to free this FIFO */ 2404 continue; 2405 } 2406 /* free this FIFO */ 2407 usb2_fifo_free(f); 2408 } 2409} 2410#endif 2411 2412/*------------------------------------------------------------------------* 2413 * usb2_peer_can_wakeup 2414 * 2415 * Return values: 2416 * 0: Peer cannot do resume signalling. 2417 * Else: Peer can do resume signalling. 2418 *------------------------------------------------------------------------*/ 2419uint8_t 2420usb2_peer_can_wakeup(struct usb2_device *udev) 2421{ 2422 const struct usb2_config_descriptor *cdp; 2423 2424 cdp = udev->cdesc; 2425 if ((cdp != NULL) && (udev->flags.usb2_mode == USB_MODE_HOST)) { 2426 return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 2427 } 2428 return (0); /* not supported */ 2429} 2430