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