usb_device.c revision 191395
1/* $FreeBSD: head/sys/dev/usb/usb_device.c 191395 2009-04-22 17:07:53Z 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->parent_hs_hub = hub; 1494 udev->hs_port_no = adev->port_no; 1495 break; 1496 } 1497 adev = hub; 1498 hub = hub->parent_hub; 1499 } 1500 1501 /* init the default pipe */ 1502 usb2_init_pipe(udev, 0, 1503 &udev->default_ep_desc, 1504 &udev->default_pipe); 1505 1506 /* set device index */ 1507 udev->device_index = device_index; 1508 1509#if USB_HAVE_UGEN 1510 /* Create ugen name */ 1511 snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1512 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1513 device_index); 1514 LIST_INIT(&udev->pd_list); 1515 1516 /* Create the control endpoint device */ 1517 udev->default_dev = usb2_make_dev(udev, 0, FREAD|FWRITE); 1518 1519 /* Create a link from /dev/ugenX.X to the default endpoint */ 1520 make_dev_alias(udev->default_dev, udev->ugen_name); 1521#endif 1522 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1523 1524 err = usb2_req_set_address(udev, NULL, device_index); 1525 1526 /* This is the new USB device address from now on */ 1527 1528 udev->address = device_index; 1529 1530 /* 1531 * We ignore any set-address errors, hence there are 1532 * buggy USB devices out there that actually receive 1533 * the SETUP PID, but manage to set the address before 1534 * the STATUS stage is ACK'ed. If the device responds 1535 * to the subsequent get-descriptor at the new 1536 * address, then we know that the set-address command 1537 * was successful. 1538 */ 1539 if (err) { 1540 DPRINTFN(0, "set address %d failed " 1541 "(%s, ignored)\n", udev->address, 1542 usb2_errstr(err)); 1543 } 1544 /* allow device time to set new address */ 1545 usb2_pause_mtx(NULL, 1546 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1547 } else { 1548 /* We are not self powered */ 1549 udev->flags.self_powered = 0; 1550 1551 /* Set unconfigured state */ 1552 udev->curr_config_no = USB_UNCONFIG_NO; 1553 udev->curr_config_index = USB_UNCONFIG_INDEX; 1554 1555 /* Setup USB descriptors */ 1556 err = (usb2_temp_setup_by_index_p) (udev, usb2_template); 1557 if (err) { 1558 DPRINTFN(0, "setting up USB template failed maybe the USB " 1559 "template module has not been loaded\n"); 1560 goto done; 1561 } 1562 } 1563 1564 /* 1565 * Get the first 8 bytes of the device descriptor ! 1566 * 1567 * NOTE: "usb2_do_request" will check the device descriptor 1568 * next time we do a request to see if the maximum packet size 1569 * changed! The 8 first bytes of the device descriptor 1570 * contains the maximum packet size to use on control endpoint 1571 * 0. If this value is different from "USB_MAX_IPACKET" a new 1572 * USB control request will be setup! 1573 */ 1574 err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc, 1575 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1576 if (err) { 1577 DPRINTFN(0, "getting device descriptor " 1578 "at addr %d failed, %s!\n", udev->address, 1579 usb2_errstr(err)); 1580 /* XXX try to re-enumerate the device */ 1581 err = usb2_req_re_enumerate(udev, NULL); 1582 if (err) { 1583 goto done; 1584 } 1585 } 1586 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1587 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1588 udev->address, UGETW(udev->ddesc.bcdUSB), 1589 udev->ddesc.bDeviceClass, 1590 udev->ddesc.bDeviceSubClass, 1591 udev->ddesc.bDeviceProtocol, 1592 udev->ddesc.bMaxPacketSize, 1593 udev->ddesc.bLength, 1594 udev->speed); 1595 1596 /* get the full device descriptor */ 1597 err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc); 1598 if (err) { 1599 DPRINTF("addr=%d, getting full desc failed\n", 1600 udev->address); 1601 goto done; 1602 } 1603 /* 1604 * Setup temporary USB attach args so that we can figure out some 1605 * basic quirks for this device. 1606 */ 1607 usb2_init_attach_arg(udev, &uaa); 1608 1609 if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) { 1610 udev->flags.uq_bus_powered = 1; 1611 } 1612 if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) { 1613 udev->flags.no_strings = 1; 1614 } 1615 /* 1616 * Workaround for buggy USB devices. 1617 * 1618 * It appears that some string-less USB chips will crash and 1619 * disappear if any attempts are made to read any string 1620 * descriptors. 1621 * 1622 * Try to detect such chips by checking the strings in the USB 1623 * device descriptor. If no strings are present there we 1624 * simply disable all USB strings. 1625 */ 1626 scratch_ptr = udev->bus->scratch[0].data; 1627 scratch_size = sizeof(udev->bus->scratch[0].data); 1628 1629 if (udev->ddesc.iManufacturer || 1630 udev->ddesc.iProduct || 1631 udev->ddesc.iSerialNumber) { 1632 /* read out the language ID string */ 1633 err = usb2_req_get_string_desc(udev, NULL, 1634 (char *)scratch_ptr, 4, scratch_size, 1635 USB_LANGUAGE_TABLE); 1636 } else { 1637 err = USB_ERR_INVAL; 1638 } 1639 1640 if (err || (scratch_ptr[0] < 4)) { 1641 udev->flags.no_strings = 1; 1642 } else { 1643 /* pick the first language as the default */ 1644 udev->langid = UGETW(scratch_ptr + 2); 1645 } 1646 1647 /* assume 100mA bus powered for now. Changed when configured. */ 1648 udev->power = USB_MIN_POWER; 1649 1650#if USB_HAVE_STRINGS 1651 /* get serial number string */ 1652 err = usb2_req_get_string_any 1653 (udev, NULL, (char *)scratch_ptr, 1654 scratch_size, udev->ddesc.iSerialNumber); 1655 1656 strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial)); 1657 1658 /* get manufacturer string */ 1659 err = usb2_req_get_string_any 1660 (udev, NULL, (char *)scratch_ptr, 1661 scratch_size, udev->ddesc.iManufacturer); 1662 1663 strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer)); 1664 1665 /* get product string */ 1666 err = usb2_req_get_string_any 1667 (udev, NULL, (char *)scratch_ptr, 1668 scratch_size, udev->ddesc.iProduct); 1669 1670 strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product)); 1671 1672 /* finish up all the strings */ 1673 usb2_check_strings(udev); 1674#endif 1675 1676 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1677 uint8_t config_index; 1678 uint8_t config_quirk; 1679 uint8_t set_config_failed = 0; 1680 1681 /* 1682 * Most USB devices should attach to config index 0 by 1683 * default 1684 */ 1685 if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1686 config_index = 0; 1687 config_quirk = 1; 1688 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1689 config_index = 1; 1690 config_quirk = 1; 1691 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1692 config_index = 2; 1693 config_quirk = 1; 1694 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1695 config_index = 3; 1696 config_quirk = 1; 1697 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1698 config_index = 4; 1699 config_quirk = 1; 1700 } else { 1701 config_index = 0; 1702 config_quirk = 0; 1703 } 1704 1705repeat_set_config: 1706 1707 DPRINTF("setting config %u\n", config_index); 1708 1709 /* get the USB device configured */ 1710 err = usb2_set_config_index(udev, config_index); 1711 if (err) { 1712 if (udev->ddesc.bNumConfigurations != 0) { 1713 if (!set_config_failed) { 1714 set_config_failed = 1; 1715 /* XXX try to re-enumerate the device */ 1716 err = usb2_req_re_enumerate( 1717 udev, NULL); 1718 if (err == 0) 1719 goto repeat_set_config; 1720 } 1721 DPRINTFN(0, "Failure selecting " 1722 "configuration index %u: %s, port %u, " 1723 "addr %u (ignored)\n", 1724 config_index, usb2_errstr(err), udev->port_no, 1725 udev->address); 1726 } 1727 /* 1728 * Some USB devices do not have any 1729 * configurations. Ignore any set config 1730 * failures! 1731 */ 1732 err = 0; 1733 } else if (config_quirk) { 1734 /* user quirk selects configuration index */ 1735 } else if ((config_index + 1) < udev->ddesc.bNumConfigurations) { 1736 1737 if ((udev->cdesc->bNumInterface < 2) && 1738 (usb2_get_no_descriptors(udev->cdesc, 1739 UDESC_ENDPOINT) == 0)) { 1740 DPRINTFN(0, "Found no endpoints " 1741 "(trying next config)!\n"); 1742 config_index++; 1743 goto repeat_set_config; 1744 } 1745 if (config_index == 0) { 1746 /* 1747 * Try to figure out if we have an 1748 * auto-install disk there: 1749 */ 1750 if (usb2_test_autoinstall(udev, 0, 0) == 0) { 1751 DPRINTFN(0, "Found possible auto-install " 1752 "disk (trying next config)\n"); 1753 config_index++; 1754 goto repeat_set_config; 1755 } 1756 } 1757 } else if (usb2_test_huawei_autoinst_p(udev, &uaa) == 0) { 1758 DPRINTFN(0, "Found Huawei auto-install disk!\n"); 1759 err = USB_ERR_STALLED; /* fake an error */ 1760 } 1761 } else { 1762 err = 0; /* set success */ 1763 } 1764 1765 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1766 udev->address, udev, udev->parent_hub); 1767 1768 /* register our device - we are ready */ 1769 usb2_bus_port_set_device(bus, parent_hub ? 1770 parent_hub->hub->ports + port_index : NULL, udev, device_index); 1771 1772#if USB_HAVE_UGEN 1773 /* Symlink the ugen device name */ 1774 udev->ugen_symlink = usb2_alloc_symlink(udev->ugen_name); 1775 1776 /* Announce device */ 1777 printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer, 1778 device_get_nameunit(udev->bus->bdev)); 1779 1780 usb2_notify_addq("+", udev); 1781#endif 1782done: 1783 if (err) { 1784 /* free device */ 1785 usb2_free_device(udev, 1786 USB_UNCFG_FLAG_FREE_SUBDEV | 1787 USB_UNCFG_FLAG_FREE_EP0); 1788 udev = NULL; 1789 } 1790 return (udev); 1791} 1792 1793#if USB_HAVE_UGEN 1794static struct cdev * 1795usb2_make_dev(struct usb2_device *udev, int ep, int mode) 1796{ 1797 struct usb2_fs_privdata* pd; 1798 char devname[20]; 1799 1800 /* Store information to locate ourselves again later */ 1801 pd = malloc(sizeof(struct usb2_fs_privdata), M_USBDEV, 1802 M_WAITOK | M_ZERO); 1803 pd->bus_index = device_get_unit(udev->bus->bdev); 1804 pd->dev_index = udev->device_index; 1805 pd->ep_addr = ep; 1806 pd->mode = mode; 1807 1808 /* Now, create the device itself */ 1809 snprintf(devname, sizeof(devname), "%u.%u.%u", 1810 pd->bus_index, pd->dev_index, pd->ep_addr); 1811 pd->cdev = make_dev(&usb2_devsw, 0, UID_ROOT, 1812 GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname); 1813 pd->cdev->si_drv1 = pd; 1814 1815 return (pd->cdev); 1816} 1817 1818static void 1819usb2_cdev_create(struct usb2_device *udev) 1820{ 1821 struct usb2_config_descriptor *cd; 1822 struct usb2_endpoint_descriptor *ed; 1823 struct usb2_descriptor *desc; 1824 struct usb2_fs_privdata* pd; 1825 struct cdev *dev; 1826 int inmode, outmode, inmask, outmask, mode; 1827 uint8_t ep; 1828 1829 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 1830 1831 DPRINTFN(2, "Creating device nodes\n"); 1832 1833 if (usb2_get_mode(udev) == USB_MODE_DEVICE) { 1834 inmode = FWRITE; 1835 outmode = FREAD; 1836 } else { /* USB_MODE_HOST */ 1837 inmode = FREAD; 1838 outmode = FWRITE; 1839 } 1840 1841 inmask = 0; 1842 outmask = 0; 1843 desc = NULL; 1844 1845 /* 1846 * Collect all used endpoint numbers instead of just 1847 * generating 16 static endpoints. 1848 */ 1849 cd = usb2_get_config_descriptor(udev); 1850 while ((desc = usb2_desc_foreach(cd, desc))) { 1851 /* filter out all endpoint descriptors */ 1852 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 1853 (desc->bLength >= sizeof(*ed))) { 1854 ed = (struct usb2_endpoint_descriptor *)desc; 1855 1856 /* update masks */ 1857 ep = ed->bEndpointAddress; 1858 if (UE_GET_DIR(ep) == UE_DIR_OUT) 1859 outmask |= 1 << UE_GET_ADDR(ep); 1860 else 1861 inmask |= 1 << UE_GET_ADDR(ep); 1862 } 1863 } 1864 1865 /* Create all available endpoints except EP0 */ 1866 for (ep = 1; ep < 16; ep++) { 1867 mode = inmask & (1 << ep) ? inmode : 0; 1868 mode |= outmask & (1 << ep) ? outmode : 0; 1869 if (mode == 0) 1870 continue; /* no IN or OUT endpoint */ 1871 1872 dev = usb2_make_dev(udev, ep, mode); 1873 pd = dev->si_drv1; 1874 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 1875 } 1876} 1877 1878static void 1879usb2_cdev_free(struct usb2_device *udev) 1880{ 1881 struct usb2_fs_privdata* pd; 1882 1883 DPRINTFN(2, "Freeing device nodes\n"); 1884 1885 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 1886 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 1887 1888 destroy_dev_sched_cb(pd->cdev, usb2_cdev_cleanup, pd); 1889 pd->cdev = NULL; 1890 LIST_REMOVE(pd, pd_next); 1891 } 1892} 1893 1894static void 1895usb2_cdev_cleanup(void* arg) 1896{ 1897 free(arg, M_USBDEV); 1898} 1899#endif 1900 1901/*------------------------------------------------------------------------* 1902 * usb2_free_device 1903 * 1904 * This function is NULL safe and will free an USB device. 1905 * 1906 * Flag values, see "USB_UNCFG_FLAG_XXX". 1907 *------------------------------------------------------------------------*/ 1908void 1909usb2_free_device(struct usb2_device *udev, uint8_t flag) 1910{ 1911 struct usb2_bus *bus; 1912 1913 if (udev == NULL) 1914 return; /* already freed */ 1915 1916 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 1917 1918 bus = udev->bus;; 1919 1920#if USB_HAVE_UGEN 1921 usb2_notify_addq("-", udev); 1922 1923 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name, 1924 udev->manufacturer, device_get_nameunit(bus->bdev)); 1925 1926 /* Destroy UGEN symlink, if any */ 1927 if (udev->ugen_symlink) { 1928 usb2_free_symlink(udev->ugen_symlink); 1929 udev->ugen_symlink = NULL; 1930 } 1931#endif 1932 /* 1933 * Unregister our device first which will prevent any further 1934 * references: 1935 */ 1936 usb2_bus_port_set_device(bus, udev->parent_hub ? 1937 udev->parent_hub->hub->ports + udev->port_index : NULL, 1938 NULL, USB_ROOT_HUB_ADDR); 1939 1940#if USB_HAVE_UGEN 1941 /* wait for all pending references to go away: */ 1942 mtx_lock(&usb2_ref_lock); 1943 udev->refcount--; 1944 while (udev->refcount != 0) { 1945 usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock); 1946 } 1947 mtx_unlock(&usb2_ref_lock); 1948 1949 destroy_dev_sched_cb(udev->default_dev, usb2_cdev_cleanup, 1950 udev->default_dev->si_drv1); 1951#endif 1952 1953 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 1954 /* stop receiving any control transfers (Device Side Mode) */ 1955 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1956 } 1957 1958 /* the following will get the device unconfigured in software */ 1959 usb2_unconfigure(udev, flag); 1960 1961 /* unsetup any leftover default USB transfers */ 1962 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1963 1964 /* template unsetup, if any */ 1965 (usb2_temp_unsetup_p) (udev); 1966 1967 /* 1968 * Make sure that our clear-stall messages are not queued 1969 * anywhere: 1970 */ 1971 USB_BUS_LOCK(udev->bus); 1972 usb2_proc_mwait(&udev->bus->non_giant_callback_proc, 1973 &udev->cs_msg[0], &udev->cs_msg[1]); 1974 USB_BUS_UNLOCK(udev->bus); 1975 1976 sx_destroy(udev->default_sx); 1977 sx_destroy(udev->default_sx + 1); 1978 1979 usb2_cv_destroy(udev->default_cv); 1980 usb2_cv_destroy(udev->default_cv + 1); 1981 1982 mtx_destroy(udev->default_mtx); 1983#if USB_HAVE_UGEN 1984 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 1985#endif 1986 1987 /* free device */ 1988 free(udev, M_USB); 1989} 1990 1991/*------------------------------------------------------------------------* 1992 * usb2_get_iface 1993 * 1994 * This function is the safe way to get the USB interface structure 1995 * pointer by interface index. 1996 * 1997 * Return values: 1998 * NULL: Interface not present. 1999 * Else: Pointer to USB interface structure. 2000 *------------------------------------------------------------------------*/ 2001struct usb2_interface * 2002usb2_get_iface(struct usb2_device *udev, uint8_t iface_index) 2003{ 2004 struct usb2_interface *iface = udev->ifaces + iface_index; 2005 2006 if (iface_index >= udev->ifaces_max) 2007 return (NULL); 2008 return (iface); 2009} 2010 2011/*------------------------------------------------------------------------* 2012 * usb2_find_descriptor 2013 * 2014 * This function will lookup the first descriptor that matches the 2015 * criteria given by the arguments "type" and "subtype". Descriptors 2016 * will only be searched within the interface having the index 2017 * "iface_index". If the "id" argument points to an USB descriptor, 2018 * it will be skipped before the search is started. This allows 2019 * searching for multiple descriptors using the same criteria. Else 2020 * the search is started after the interface descriptor. 2021 * 2022 * Return values: 2023 * NULL: End of descriptors 2024 * Else: A descriptor matching the criteria 2025 *------------------------------------------------------------------------*/ 2026void * 2027usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index, 2028 uint8_t type, uint8_t type_mask, 2029 uint8_t subtype, uint8_t subtype_mask) 2030{ 2031 struct usb2_descriptor *desc; 2032 struct usb2_config_descriptor *cd; 2033 struct usb2_interface *iface; 2034 2035 cd = usb2_get_config_descriptor(udev); 2036 if (cd == NULL) { 2037 return (NULL); 2038 } 2039 if (id == NULL) { 2040 iface = usb2_get_iface(udev, iface_index); 2041 if (iface == NULL) { 2042 return (NULL); 2043 } 2044 id = usb2_get_interface_descriptor(iface); 2045 if (id == NULL) { 2046 return (NULL); 2047 } 2048 } 2049 desc = (void *)id; 2050 2051 while ((desc = usb2_desc_foreach(cd, desc))) { 2052 2053 if (desc->bDescriptorType == UDESC_INTERFACE) { 2054 break; 2055 } 2056 if (((desc->bDescriptorType & type_mask) == type) && 2057 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 2058 return (desc); 2059 } 2060 } 2061 return (NULL); 2062} 2063 2064/*------------------------------------------------------------------------* 2065 * usb2_devinfo 2066 * 2067 * This function will dump information from the device descriptor 2068 * belonging to the USB device pointed to by "udev", to the string 2069 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 2070 * including the terminating zero. 2071 *------------------------------------------------------------------------*/ 2072void 2073usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len) 2074{ 2075 struct usb2_device_descriptor *udd = &udev->ddesc; 2076 uint16_t bcdDevice; 2077 uint16_t bcdUSB; 2078 2079 bcdUSB = UGETW(udd->bcdUSB); 2080 bcdDevice = UGETW(udd->bcdDevice); 2081 2082 if (udd->bDeviceClass != 0xFF) { 2083 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 2084 "%x.%02x, addr %d", 2085#if USB_HAVE_STRINGS 2086 udev->manufacturer, udev->product, 2087#else 2088 "-", "-", 2089#endif 2090 udd->bDeviceClass, udd->bDeviceSubClass, 2091 (bcdUSB >> 8), bcdUSB & 0xFF, 2092 (bcdDevice >> 8), bcdDevice & 0xFF, 2093 udev->address); 2094 } else { 2095 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 2096 "%x.%02x, addr %d", 2097#if USB_HAVE_STRINGS 2098 udev->manufacturer, udev->product, 2099#else 2100 "-", "-", 2101#endif 2102 (bcdUSB >> 8), bcdUSB & 0xFF, 2103 (bcdDevice >> 8), bcdDevice & 0xFF, 2104 udev->address); 2105 } 2106} 2107 2108#if USB_HAVE_STRINGS 2109#if USB_VERBOSE 2110/* 2111 * Descriptions of of known vendors and devices ("products"). 2112 */ 2113struct usb_knowndev { 2114 uint16_t vendor; 2115 uint16_t product; 2116 uint32_t flags; 2117 const char *vendorname; 2118 const char *productname; 2119}; 2120 2121#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 2122 2123#include "usbdevs.h" 2124#include "usbdevs_data.h" 2125#endif /* USB_VERBOSE */ 2126 2127/*------------------------------------------------------------------------* 2128 * usb2_check_strings 2129 * 2130 * This function checks the manufacturer and product strings and will 2131 * fill in defaults for missing strings. 2132 *------------------------------------------------------------------------*/ 2133static void 2134usb2_check_strings(struct usb2_device *udev) 2135{ 2136 struct usb2_device_descriptor *udd = &udev->ddesc; 2137 const char *vendor; 2138 const char *product; 2139 2140#if USB_VERBOSE 2141 const struct usb_knowndev *kdp; 2142 2143#endif 2144 uint16_t vendor_id; 2145 uint16_t product_id; 2146 2147 usb2_trim_spaces(udev->manufacturer); 2148 usb2_trim_spaces(udev->product); 2149 2150 if (udev->manufacturer[0]) { 2151 vendor = udev->manufacturer; 2152 } else { 2153 vendor = NULL; 2154 } 2155 2156 if (udev->product[0]) { 2157 product = udev->product; 2158 } else { 2159 product = NULL; 2160 } 2161 2162 vendor_id = UGETW(udd->idVendor); 2163 product_id = UGETW(udd->idProduct); 2164 2165#if USB_VERBOSE 2166 if (vendor == NULL || product == NULL) { 2167 2168 for (kdp = usb_knowndevs; 2169 kdp->vendorname != NULL; 2170 kdp++) { 2171 if (kdp->vendor == vendor_id && 2172 (kdp->product == product_id || 2173 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 2174 break; 2175 } 2176 if (kdp->vendorname != NULL) { 2177 if (vendor == NULL) 2178 vendor = kdp->vendorname; 2179 if (product == NULL) 2180 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ? 2181 kdp->productname : NULL; 2182 } 2183 } 2184#endif 2185 if (vendor && *vendor) { 2186 if (udev->manufacturer != vendor) { 2187 strlcpy(udev->manufacturer, vendor, 2188 sizeof(udev->manufacturer)); 2189 } 2190 } else { 2191 snprintf(udev->manufacturer, 2192 sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id); 2193 } 2194 2195 if (product && *product) { 2196 if (udev->product != product) { 2197 strlcpy(udev->product, product, 2198 sizeof(udev->product)); 2199 } 2200 } else { 2201 snprintf(udev->product, 2202 sizeof(udev->product), "product 0x%04x", product_id); 2203 } 2204} 2205#endif 2206 2207/* 2208 * Returns: 2209 * See: USB_MODE_XXX 2210 */ 2211uint8_t 2212usb2_get_mode(struct usb2_device *udev) 2213{ 2214 return (udev->flags.usb2_mode); 2215} 2216 2217/* 2218 * Returns: 2219 * See: USB_SPEED_XXX 2220 */ 2221uint8_t 2222usb2_get_speed(struct usb2_device *udev) 2223{ 2224 return (udev->speed); 2225} 2226 2227uint32_t 2228usb2_get_isoc_fps(struct usb2_device *udev) 2229{ 2230 ; /* indent fix */ 2231 switch (udev->speed) { 2232 case USB_SPEED_LOW: 2233 case USB_SPEED_FULL: 2234 return (1000); 2235 default: 2236 return (8000); 2237 } 2238} 2239 2240struct usb2_device_descriptor * 2241usb2_get_device_descriptor(struct usb2_device *udev) 2242{ 2243 if (udev == NULL) 2244 return (NULL); /* be NULL safe */ 2245 return (&udev->ddesc); 2246} 2247 2248struct usb2_config_descriptor * 2249usb2_get_config_descriptor(struct usb2_device *udev) 2250{ 2251 if (udev == NULL) 2252 return (NULL); /* be NULL safe */ 2253 return (udev->cdesc); 2254} 2255 2256/*------------------------------------------------------------------------* 2257 * usb2_test_quirk - test a device for a given quirk 2258 * 2259 * Return values: 2260 * 0: The USB device does not have the given quirk. 2261 * Else: The USB device has the given quirk. 2262 *------------------------------------------------------------------------*/ 2263uint8_t 2264usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk) 2265{ 2266 uint8_t found; 2267 2268 found = (usb2_test_quirk_p) (&uaa->info, quirk); 2269 return (found); 2270} 2271 2272struct usb2_interface_descriptor * 2273usb2_get_interface_descriptor(struct usb2_interface *iface) 2274{ 2275 if (iface == NULL) 2276 return (NULL); /* be NULL safe */ 2277 return (iface->idesc); 2278} 2279 2280uint8_t 2281usb2_get_interface_altindex(struct usb2_interface *iface) 2282{ 2283 return (iface->alt_index); 2284} 2285 2286uint8_t 2287usb2_get_bus_index(struct usb2_device *udev) 2288{ 2289 return ((uint8_t)device_get_unit(udev->bus->bdev)); 2290} 2291 2292uint8_t 2293usb2_get_device_index(struct usb2_device *udev) 2294{ 2295 return (udev->device_index); 2296} 2297 2298#if USB_HAVE_UGEN 2299/*------------------------------------------------------------------------* 2300 * usb2_notify_addq 2301 * 2302 * This function will generate events for dev. 2303 *------------------------------------------------------------------------*/ 2304static void 2305usb2_notify_addq(const char *type, struct usb2_device *udev) 2306{ 2307 char *data = NULL; 2308 struct malloc_type *mt; 2309 2310 mtx_lock(&malloc_mtx); 2311 mt = malloc_desc2type("bus"); /* XXX M_BUS */ 2312 mtx_unlock(&malloc_mtx); 2313 if (mt == NULL) 2314 return; 2315 2316 data = malloc(512, mt, M_NOWAIT); 2317 if (data == NULL) 2318 return; 2319 2320 /* String it all together. */ 2321 snprintf(data, 1024, 2322 "%s" 2323 "%s " 2324 "vendor=0x%04x " 2325 "product=0x%04x " 2326 "devclass=0x%02x " 2327 "devsubclass=0x%02x " 2328 "sernum=\"%s\" " 2329 "at " 2330 "port=%u " 2331 "on " 2332 "%s\n", 2333 type, 2334 udev->ugen_name, 2335 UGETW(udev->ddesc.idVendor), 2336 UGETW(udev->ddesc.idProduct), 2337 udev->ddesc.bDeviceClass, 2338 udev->ddesc.bDeviceSubClass, 2339#if USB_HAVE_STRINGS 2340 udev->serial, 2341#else 2342 "", 2343#endif 2344 udev->port_no, 2345 udev->parent_hub != NULL ? 2346 udev->parent_hub->ugen_name : 2347 device_get_nameunit(device_get_parent(udev->bus->bdev))); 2348 2349 devctl_queue_data(data); 2350} 2351 2352/*------------------------------------------------------------------------* 2353 * usb2_fifo_free_wrap 2354 * 2355 * This function will free the FIFOs. 2356 * 2357 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2358 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2359 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2360 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2361 * control endpoint FIFOs. If "iface_index" is not set to 2362 * "USB_IFACE_INDEX_ANY" the flag has no effect. 2363 *------------------------------------------------------------------------*/ 2364static void 2365usb2_fifo_free_wrap(struct usb2_device *udev, 2366 uint8_t iface_index, uint8_t flag) 2367{ 2368 struct usb2_fifo *f; 2369 uint16_t i; 2370 2371 /* 2372 * Free any USB FIFOs on the given interface: 2373 */ 2374 for (i = 0; i != USB_FIFO_MAX; i++) { 2375 f = udev->fifo[i]; 2376 if (f == NULL) { 2377 continue; 2378 } 2379 /* Check if the interface index matches */ 2380 if (iface_index == f->iface_index) { 2381 if (f->methods != &usb2_ugen_methods) { 2382 /* 2383 * Don't free any non-generic FIFOs in 2384 * this case. 2385 */ 2386 continue; 2387 } 2388 if ((f->dev_ep_index == 0) && 2389 (f->fs_xfer == NULL)) { 2390 /* no need to free this FIFO */ 2391 continue; 2392 } 2393 } else if (iface_index == USB_IFACE_INDEX_ANY) { 2394 if ((f->methods == &usb2_ugen_methods) && 2395 (f->dev_ep_index == 0) && 2396 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 2397 (f->fs_xfer == NULL)) { 2398 /* no need to free this FIFO */ 2399 continue; 2400 } 2401 } else { 2402 /* no need to free this FIFO */ 2403 continue; 2404 } 2405 /* free this FIFO */ 2406 usb2_fifo_free(f); 2407 } 2408} 2409#endif 2410 2411/*------------------------------------------------------------------------* 2412 * usb2_peer_can_wakeup 2413 * 2414 * Return values: 2415 * 0: Peer cannot do resume signalling. 2416 * Else: Peer can do resume signalling. 2417 *------------------------------------------------------------------------*/ 2418uint8_t 2419usb2_peer_can_wakeup(struct usb2_device *udev) 2420{ 2421 const struct usb2_config_descriptor *cdp; 2422 2423 cdp = udev->cdesc; 2424 if ((cdp != NULL) && (udev->flags.usb2_mode == USB_MODE_HOST)) { 2425 return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 2426 } 2427 return (0); /* not supported */ 2428} 2429