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