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