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