1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 31 */ 32 33 #ifdef USB_GLOBAL_INCLUDE_FILE 34 #include USB_GLOBAL_INCLUDE_FILE 35 #else 36 #include <sys/stdint.h> 37 #include <sys/stddef.h> 38 #include <sys/param.h> 39 #include <sys/queue.h> 40 #include <sys/types.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/bus.h> 44 #include <sys/module.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/condvar.h> 48 #include <sys/sysctl.h> 49 #include <sys/sx.h> 50 #include <sys/unistd.h> 51 #include <sys/callout.h> 52 #include <sys/malloc.h> 53 #include <sys/priv.h> 54 55 #include <dev/usb/usb.h> 56 #include <dev/usb/usbdi.h> 57 #include <dev/usb/usbdi_util.h> 58 59 #define USB_DEBUG_VAR uhub_debug 60 61 #include <dev/usb/usb_core.h> 62 #include <dev/usb/usb_process.h> 63 #include <dev/usb/usb_device.h> 64 #include <dev/usb/usb_request.h> 65 #include <dev/usb/usb_debug.h> 66 #include <dev/usb/usb_hub.h> 67 #include <dev/usb/usb_util.h> 68 #include <dev/usb/usb_busdma.h> 69 #include <dev/usb/usb_transfer.h> 70 #include <dev/usb/usb_dynamic.h> 71 72 #include <dev/usb/usb_controller.h> 73 #include <dev/usb/usb_bus.h> 74 #endif /* USB_GLOBAL_INCLUDE_FILE */ 75 76 #define UHUB_INTR_INTERVAL 250 /* ms */ 77 enum { 78 UHUB_INTR_TRANSFER, 79 #if USB_HAVE_TT_SUPPORT 80 UHUB_RESET_TT_TRANSFER, 81 #endif 82 UHUB_N_TRANSFER, 83 }; 84 85 #ifdef USB_DEBUG 86 static int uhub_debug = 0; 87 88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB"); 89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0, 90 "Debug level"); 91 #endif 92 93 #if USB_HAVE_POWERD 94 static int usb_power_timeout = 30; /* seconds */ 95 96 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW, 97 &usb_power_timeout, 0, "USB power timeout"); 98 #endif 99 100 struct uhub_current_state { 101 uint16_t port_change; 102 uint16_t port_status; 103 }; 104 105 struct uhub_softc { 106 struct uhub_current_state sc_st;/* current state */ 107 #if (USB_HAVE_FIXED_PORT != 0) 108 struct usb_hub sc_hub; 109 #endif 110 device_t sc_dev; /* base device */ 111 struct mtx sc_mtx; /* our mutex */ 112 struct usb_device *sc_udev; /* USB device */ 113 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */ 114 uint8_t sc_flags; 115 #define UHUB_FLAG_DID_EXPLORE 0x01 116 }; 117 118 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol) 119 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB) 120 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT) 121 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT) 122 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB) 123 124 /* prototypes for type checking: */ 125 126 static device_probe_t uhub_probe; 127 static device_attach_t uhub_attach; 128 static device_detach_t uhub_detach; 129 static device_suspend_t uhub_suspend; 130 static device_resume_t uhub_resume; 131 132 static bus_driver_added_t uhub_driver_added; 133 static bus_child_location_str_t uhub_child_location_string; 134 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string; 135 136 static usb_callback_t uhub_intr_callback; 137 #if USB_HAVE_TT_SUPPORT 138 static usb_callback_t uhub_reset_tt_callback; 139 #endif 140 141 static void usb_dev_resume_peer(struct usb_device *udev); 142 static void usb_dev_suspend_peer(struct usb_device *udev); 143 static uint8_t usb_peer_should_wakeup(struct usb_device *udev); 144 145 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = { 146 147 [UHUB_INTR_TRANSFER] = { 148 .type = UE_INTERRUPT, 149 .endpoint = UE_ADDR_ANY, 150 .direction = UE_DIR_ANY, 151 .timeout = 0, 152 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 153 .bufsize = 0, /* use wMaxPacketSize */ 154 .callback = &uhub_intr_callback, 155 .interval = UHUB_INTR_INTERVAL, 156 }, 157 #if USB_HAVE_TT_SUPPORT 158 [UHUB_RESET_TT_TRANSFER] = { 159 .type = UE_CONTROL, 160 .endpoint = 0x00, /* Control pipe */ 161 .direction = UE_DIR_ANY, 162 .bufsize = sizeof(struct usb_device_request), 163 .callback = &uhub_reset_tt_callback, 164 .timeout = 1000, /* 1 second */ 165 .usb_mode = USB_MODE_HOST, 166 }, 167 #endif 168 }; 169 170 /* 171 * driver instance for "hub" connected to "usb" 172 * and "hub" connected to "hub" 173 */ 174 static devclass_t uhub_devclass; 175 176 static device_method_t uhub_methods[] = { 177 DEVMETHOD(device_probe, uhub_probe), 178 DEVMETHOD(device_attach, uhub_attach), 179 DEVMETHOD(device_detach, uhub_detach), 180 181 DEVMETHOD(device_suspend, uhub_suspend), 182 DEVMETHOD(device_resume, uhub_resume), 183 184 DEVMETHOD(bus_child_location_str, uhub_child_location_string), 185 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string), 186 DEVMETHOD(bus_driver_added, uhub_driver_added), 187 DEVMETHOD_END 188 }; 189 190 static driver_t uhub_driver = { 191 .name = "uhub", 192 .methods = uhub_methods, 193 .size = sizeof(struct uhub_softc) 194 }; 195 196 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0); 197 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0); 198 MODULE_VERSION(uhub, 1); 199 200 static void 201 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error) 202 { 203 struct uhub_softc *sc = usbd_xfer_softc(xfer); 204 205 switch (USB_GET_STATE(xfer)) { 206 case USB_ST_TRANSFERRED: 207 DPRINTFN(2, "\n"); 208 /* 209 * This is an indication that some port 210 * has changed status. Notify the bus 211 * event handler thread that we need 212 * to be explored again: 213 */ 214 usb_needs_explore(sc->sc_udev->bus, 0); 215 216 case USB_ST_SETUP: 217 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 218 usbd_transfer_submit(xfer); 219 break; 220 221 default: /* Error */ 222 if (xfer->error != USB_ERR_CANCELLED) { 223 /* 224 * Do a clear-stall. The "stall_pipe" flag 225 * will get cleared before next callback by 226 * the USB stack. 227 */ 228 usbd_xfer_set_stall(xfer); 229 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 230 usbd_transfer_submit(xfer); 231 } 232 break; 233 } 234 } 235 236 /*------------------------------------------------------------------------* 237 * uhub_reset_tt_proc 238 * 239 * This function starts the TT reset USB request 240 *------------------------------------------------------------------------*/ 241 #if USB_HAVE_TT_SUPPORT 242 static void 243 uhub_reset_tt_proc(struct usb_proc_msg *_pm) 244 { 245 struct usb_udev_msg *pm = (void *)_pm; 246 struct usb_device *udev = pm->udev; 247 struct usb_hub *hub; 248 struct uhub_softc *sc; 249 250 hub = udev->hub; 251 if (hub == NULL) 252 return; 253 sc = hub->hubsoftc; 254 if (sc == NULL) 255 return; 256 257 /* Change lock */ 258 USB_BUS_UNLOCK(udev->bus); 259 mtx_lock(&sc->sc_mtx); 260 /* Start transfer */ 261 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]); 262 /* Change lock */ 263 mtx_unlock(&sc->sc_mtx); 264 USB_BUS_LOCK(udev->bus); 265 } 266 #endif 267 268 /*------------------------------------------------------------------------* 269 * uhub_tt_buffer_reset_async_locked 270 * 271 * This function queues a TT reset for the given USB device and endpoint. 272 *------------------------------------------------------------------------*/ 273 #if USB_HAVE_TT_SUPPORT 274 void 275 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep) 276 { 277 struct usb_device_request req; 278 struct usb_device *udev; 279 struct usb_hub *hub; 280 struct usb_port *up; 281 uint16_t wValue; 282 uint8_t port; 283 284 if (child == NULL || ep == NULL) 285 return; 286 287 udev = child->parent_hs_hub; 288 port = child->hs_port_no; 289 290 if (udev == NULL) 291 return; 292 293 hub = udev->hub; 294 if ((hub == NULL) || 295 (udev->speed != USB_SPEED_HIGH) || 296 (child->speed != USB_SPEED_LOW && 297 child->speed != USB_SPEED_FULL) || 298 (child->flags.usb_mode != USB_MODE_HOST) || 299 (port == 0) || (ep->edesc == NULL)) { 300 /* not applicable */ 301 return; 302 } 303 304 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 305 306 up = hub->ports + port - 1; 307 308 if (udev->ddesc.bDeviceClass == UDCLASS_HUB && 309 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT) 310 port = 1; 311 312 /* if we already received a clear buffer request, reset the whole TT */ 313 if (up->req_reset_tt.bRequest != 0) { 314 req.bmRequestType = UT_WRITE_CLASS_OTHER; 315 req.bRequest = UR_RESET_TT; 316 USETW(req.wValue, 0); 317 req.wIndex[0] = port; 318 req.wIndex[1] = 0; 319 USETW(req.wLength, 0); 320 } else { 321 wValue = (ep->edesc->bEndpointAddress & 0xF) | 322 ((child->address & 0x7F) << 4) | 323 ((ep->edesc->bEndpointAddress & 0x80) << 8) | 324 ((ep->edesc->bmAttributes & 3) << 12); 325 326 req.bmRequestType = UT_WRITE_CLASS_OTHER; 327 req.bRequest = UR_CLEAR_TT_BUFFER; 328 USETW(req.wValue, wValue); 329 req.wIndex[0] = port; 330 req.wIndex[1] = 0; 331 USETW(req.wLength, 0); 332 } 333 up->req_reset_tt = req; 334 /* get reset transfer started */ 335 usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev->bus), 336 &hub->tt_msg[0], &hub->tt_msg[1]); 337 } 338 #endif 339 340 #if USB_HAVE_TT_SUPPORT 341 static void 342 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error) 343 { 344 struct uhub_softc *sc; 345 struct usb_device *udev; 346 struct usb_port *up; 347 uint8_t x; 348 349 DPRINTF("TT buffer reset\n"); 350 351 sc = usbd_xfer_softc(xfer); 352 udev = sc->sc_udev; 353 354 switch (USB_GET_STATE(xfer)) { 355 case USB_ST_TRANSFERRED: 356 case USB_ST_SETUP: 357 tr_setup: 358 USB_BUS_LOCK(udev->bus); 359 /* find first port which needs a TT reset */ 360 for (x = 0; x != udev->hub->nports; x++) { 361 up = udev->hub->ports + x; 362 363 if (up->req_reset_tt.bRequest == 0) 364 continue; 365 366 /* copy in the transfer */ 367 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt, 368 sizeof(up->req_reset_tt)); 369 /* reset buffer */ 370 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt)); 371 372 /* set length */ 373 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt)); 374 xfer->nframes = 1; 375 USB_BUS_UNLOCK(udev->bus); 376 377 usbd_transfer_submit(xfer); 378 return; 379 } 380 USB_BUS_UNLOCK(udev->bus); 381 break; 382 383 default: 384 if (error == USB_ERR_CANCELLED) 385 break; 386 387 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error)); 388 goto tr_setup; 389 } 390 } 391 #endif 392 393 /*------------------------------------------------------------------------* 394 * uhub_count_active_host_ports 395 * 396 * This function counts the number of active ports at the given speed. 397 *------------------------------------------------------------------------*/ 398 uint8_t 399 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed) 400 { 401 struct uhub_softc *sc; 402 struct usb_device *child; 403 struct usb_hub *hub; 404 struct usb_port *up; 405 uint8_t retval = 0; 406 uint8_t x; 407 408 if (udev == NULL) 409 goto done; 410 hub = udev->hub; 411 if (hub == NULL) 412 goto done; 413 sc = hub->hubsoftc; 414 if (sc == NULL) 415 goto done; 416 417 for (x = 0; x != hub->nports; x++) { 418 up = hub->ports + x; 419 child = usb_bus_port_get_device(udev->bus, up); 420 if (child != NULL && 421 child->flags.usb_mode == USB_MODE_HOST && 422 child->speed == speed) 423 retval++; 424 } 425 done: 426 return (retval); 427 } 428 429 void 430 uhub_explore_handle_re_enumerate(struct usb_device *child) 431 { 432 uint8_t do_unlock; 433 usb_error_t err; 434 435 /* check if device should be re-enumerated */ 436 if (child->flags.usb_mode != USB_MODE_HOST) 437 return; 438 439 do_unlock = usbd_enum_lock(child); 440 switch (child->re_enumerate_wait) { 441 case USB_RE_ENUM_START: 442 err = usbd_set_config_index(child, 443 USB_UNCONFIG_INDEX); 444 if (err != 0) { 445 DPRINTF("Unconfigure failed: %s: Ignored.\n", 446 usbd_errstr(err)); 447 } 448 if (child->parent_hub == NULL) { 449 /* the root HUB cannot be re-enumerated */ 450 DPRINTFN(6, "cannot reset root HUB\n"); 451 err = 0; 452 } else { 453 err = usbd_req_re_enumerate(child, NULL); 454 } 455 if (err == 0) 456 err = usbd_set_config_index(child, 0); 457 if (err == 0) { 458 err = usb_probe_and_attach(child, 459 USB_IFACE_INDEX_ANY); 460 } 461 child->re_enumerate_wait = USB_RE_ENUM_DONE; 462 break; 463 464 case USB_RE_ENUM_PWR_OFF: 465 /* get the device unconfigured */ 466 err = usbd_set_config_index(child, 467 USB_UNCONFIG_INDEX); 468 if (err) { 469 DPRINTFN(0, "Could not unconfigure " 470 "device (ignored)\n"); 471 } 472 if (child->parent_hub == NULL) { 473 /* the root HUB cannot be re-enumerated */ 474 DPRINTFN(6, "cannot set port feature\n"); 475 err = 0; 476 } else { 477 /* clear port enable */ 478 err = usbd_req_clear_port_feature(child->parent_hub, 479 NULL, child->port_no, UHF_PORT_ENABLE); 480 if (err) { 481 DPRINTFN(0, "Could not disable port " 482 "(ignored)\n"); 483 } 484 } 485 child->re_enumerate_wait = USB_RE_ENUM_DONE; 486 break; 487 488 case USB_RE_ENUM_SET_CONFIG: 489 err = usbd_set_config_index(child, 490 child->next_config_index); 491 if (err != 0) { 492 DPRINTF("Configure failed: %s: Ignored.\n", 493 usbd_errstr(err)); 494 } else { 495 err = usb_probe_and_attach(child, 496 USB_IFACE_INDEX_ANY); 497 } 498 child->re_enumerate_wait = USB_RE_ENUM_DONE; 499 break; 500 501 default: 502 child->re_enumerate_wait = USB_RE_ENUM_DONE; 503 break; 504 } 505 if (do_unlock) 506 usbd_enum_unlock(child); 507 } 508 509 /*------------------------------------------------------------------------* 510 * uhub_explore_sub - subroutine 511 * 512 * Return values: 513 * 0: Success 514 * Else: A control transaction failed 515 *------------------------------------------------------------------------*/ 516 static usb_error_t 517 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up) 518 { 519 struct usb_bus *bus; 520 struct usb_device *child; 521 uint8_t refcount; 522 usb_error_t err; 523 524 bus = sc->sc_udev->bus; 525 err = 0; 526 527 /* get driver added refcount from USB bus */ 528 refcount = bus->driver_added_refcount; 529 530 /* get device assosiated with the given port */ 531 child = usb_bus_port_get_device(bus, up); 532 if (child == NULL) { 533 /* nothing to do */ 534 goto done; 535 } 536 537 uhub_explore_handle_re_enumerate(child); 538 539 /* check if probe and attach should be done */ 540 541 if (child->driver_added_refcount != refcount) { 542 child->driver_added_refcount = refcount; 543 err = usb_probe_and_attach(child, 544 USB_IFACE_INDEX_ANY); 545 if (err) { 546 goto done; 547 } 548 } 549 /* start control transfer, if device mode */ 550 551 if (child->flags.usb_mode == USB_MODE_DEVICE) 552 usbd_ctrl_transfer_setup(child); 553 554 /* if a HUB becomes present, do a recursive HUB explore */ 555 556 if (child->hub) 557 err = (child->hub->explore) (child); 558 559 done: 560 return (err); 561 } 562 563 /*------------------------------------------------------------------------* 564 * uhub_read_port_status - factored out code 565 *------------------------------------------------------------------------*/ 566 static usb_error_t 567 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno) 568 { 569 struct usb_port_status ps; 570 usb_error_t err; 571 572 err = usbd_req_get_port_status( 573 sc->sc_udev, NULL, &ps, portno); 574 575 /* update status regardless of error */ 576 577 sc->sc_st.port_status = UGETW(ps.wPortStatus); 578 sc->sc_st.port_change = UGETW(ps.wPortChange); 579 580 /* debugging print */ 581 582 DPRINTFN(4, "port %d, wPortStatus=0x%04x, " 583 "wPortChange=0x%04x, err=%s\n", 584 portno, sc->sc_st.port_status, 585 sc->sc_st.port_change, usbd_errstr(err)); 586 return (err); 587 } 588 589 /*------------------------------------------------------------------------* 590 * uhub_reattach_port 591 * 592 * Returns: 593 * 0: Success 594 * Else: A control transaction failed 595 *------------------------------------------------------------------------*/ 596 static usb_error_t 597 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno) 598 { 599 struct usb_device *child; 600 struct usb_device *udev; 601 enum usb_dev_speed speed; 602 enum usb_hc_mode mode; 603 usb_error_t err; 604 uint16_t power_mask; 605 uint8_t timeout; 606 607 DPRINTF("reattaching port %d\n", portno); 608 609 timeout = 0; 610 udev = sc->sc_udev; 611 child = usb_bus_port_get_device(udev->bus, 612 udev->hub->ports + portno - 1); 613 614 repeat: 615 616 /* first clear the port connection change bit */ 617 618 err = usbd_req_clear_port_feature(udev, NULL, 619 portno, UHF_C_PORT_CONNECTION); 620 621 if (err) { 622 goto error; 623 } 624 /* check if there is a child */ 625 626 if (child != NULL) { 627 /* 628 * Free USB device and all subdevices, if any. 629 */ 630 usb_free_device(child, 0); 631 child = NULL; 632 } 633 /* get fresh status */ 634 635 err = uhub_read_port_status(sc, portno); 636 if (err) { 637 goto error; 638 } 639 /* check if nothing is connected to the port */ 640 641 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) { 642 goto error; 643 } 644 /* check if there is no power on the port and print a warning */ 645 646 switch (udev->speed) { 647 case USB_SPEED_HIGH: 648 case USB_SPEED_FULL: 649 case USB_SPEED_LOW: 650 power_mask = UPS_PORT_POWER; 651 break; 652 case USB_SPEED_SUPER: 653 if (udev->parent_hub == NULL) 654 power_mask = UPS_PORT_POWER; 655 else 656 power_mask = UPS_PORT_POWER_SS; 657 break; 658 default: 659 power_mask = 0; 660 break; 661 } 662 if (!(sc->sc_st.port_status & power_mask)) { 663 DPRINTF("WARNING: strange, connected port %d " 664 "has no power\n", portno); 665 } 666 667 /* check if the device is in Host Mode */ 668 669 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) { 670 671 DPRINTF("Port %d is in Host Mode\n", portno); 672 673 if (sc->sc_st.port_status & UPS_SUSPEND) { 674 /* 675 * NOTE: Should not get here in SuperSpeed 676 * mode, because the HUB should report this 677 * bit as zero. 678 */ 679 DPRINTF("Port %d was still " 680 "suspended, clearing.\n", portno); 681 err = usbd_req_clear_port_feature(udev, 682 NULL, portno, UHF_PORT_SUSPEND); 683 } 684 685 /* USB Host Mode */ 686 687 /* wait for maximum device power up time */ 688 689 usb_pause_mtx(NULL, 690 USB_MS_TO_TICKS(usb_port_powerup_delay)); 691 692 /* reset port, which implies enabling it */ 693 694 err = usbd_req_reset_port(udev, NULL, portno); 695 696 if (err) { 697 DPRINTFN(0, "port %d reset " 698 "failed, error=%s\n", 699 portno, usbd_errstr(err)); 700 goto error; 701 } 702 /* get port status again, it might have changed during reset */ 703 704 err = uhub_read_port_status(sc, portno); 705 if (err) { 706 goto error; 707 } 708 /* check if something changed during port reset */ 709 710 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) || 711 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) { 712 if (timeout) { 713 DPRINTFN(0, "giving up port reset " 714 "- device vanished\n"); 715 goto error; 716 } 717 timeout = 1; 718 goto repeat; 719 } 720 } else { 721 DPRINTF("Port %d is in Device Mode\n", portno); 722 } 723 724 /* 725 * Figure out the device speed 726 */ 727 switch (udev->speed) { 728 case USB_SPEED_HIGH: 729 if (sc->sc_st.port_status & UPS_HIGH_SPEED) 730 speed = USB_SPEED_HIGH; 731 else if (sc->sc_st.port_status & UPS_LOW_SPEED) 732 speed = USB_SPEED_LOW; 733 else 734 speed = USB_SPEED_FULL; 735 break; 736 case USB_SPEED_FULL: 737 if (sc->sc_st.port_status & UPS_LOW_SPEED) 738 speed = USB_SPEED_LOW; 739 else 740 speed = USB_SPEED_FULL; 741 break; 742 case USB_SPEED_LOW: 743 speed = USB_SPEED_LOW; 744 break; 745 case USB_SPEED_SUPER: 746 if (udev->parent_hub == NULL) { 747 /* Root HUB - special case */ 748 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) { 749 case 0: 750 speed = USB_SPEED_FULL; 751 break; 752 case UPS_LOW_SPEED: 753 speed = USB_SPEED_LOW; 754 break; 755 case UPS_HIGH_SPEED: 756 speed = USB_SPEED_HIGH; 757 break; 758 default: 759 speed = USB_SPEED_SUPER; 760 break; 761 } 762 } else { 763 speed = USB_SPEED_SUPER; 764 } 765 break; 766 default: 767 /* same speed like parent */ 768 speed = udev->speed; 769 break; 770 } 771 if (speed == USB_SPEED_SUPER) { 772 err = usbd_req_set_hub_u1_timeout(udev, NULL, 773 portno, 128 - (2 * udev->depth)); 774 if (err) { 775 DPRINTFN(0, "port %d U1 timeout " 776 "failed, error=%s\n", 777 portno, usbd_errstr(err)); 778 } 779 err = usbd_req_set_hub_u2_timeout(udev, NULL, 780 portno, 128 - (2 * udev->depth)); 781 if (err) { 782 DPRINTFN(0, "port %d U2 timeout " 783 "failed, error=%s\n", 784 portno, usbd_errstr(err)); 785 } 786 } 787 788 /* 789 * Figure out the device mode 790 * 791 * NOTE: This part is currently FreeBSD specific. 792 */ 793 if (udev->parent_hub != NULL) { 794 /* inherit mode from the parent HUB */ 795 mode = udev->parent_hub->flags.usb_mode; 796 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE) 797 mode = USB_MODE_DEVICE; 798 else 799 mode = USB_MODE_HOST; 800 801 /* need to create a new child */ 802 child = usb_alloc_device(sc->sc_dev, udev->bus, udev, 803 udev->depth + 1, portno - 1, portno, speed, mode); 804 if (child == NULL) { 805 DPRINTFN(0, "could not allocate new device\n"); 806 goto error; 807 } 808 return (0); /* success */ 809 810 error: 811 if (child != NULL) { 812 /* 813 * Free USB device and all subdevices, if any. 814 */ 815 usb_free_device(child, 0); 816 child = NULL; 817 } 818 if (err == 0) { 819 if (sc->sc_st.port_status & UPS_PORT_ENABLED) { 820 err = usbd_req_clear_port_feature( 821 sc->sc_udev, NULL, 822 portno, UHF_PORT_ENABLE); 823 } 824 } 825 if (err) { 826 DPRINTFN(0, "device problem (%s), " 827 "disabling port %d\n", usbd_errstr(err), portno); 828 } 829 return (err); 830 } 831 832 /*------------------------------------------------------------------------* 833 * usb_device_20_compatible 834 * 835 * Returns: 836 * 0: HUB does not support suspend and resume 837 * Else: HUB supports suspend and resume 838 *------------------------------------------------------------------------*/ 839 static uint8_t 840 usb_device_20_compatible(struct usb_device *udev) 841 { 842 if (udev == NULL) 843 return (0); 844 switch (udev->speed) { 845 case USB_SPEED_LOW: 846 case USB_SPEED_FULL: 847 case USB_SPEED_HIGH: 848 return (1); 849 default: 850 return (0); 851 } 852 } 853 854 /*------------------------------------------------------------------------* 855 * uhub_suspend_resume_port 856 * 857 * Returns: 858 * 0: Success 859 * Else: A control transaction failed 860 *------------------------------------------------------------------------*/ 861 static usb_error_t 862 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno) 863 { 864 struct usb_device *child; 865 struct usb_device *udev; 866 uint8_t is_suspend; 867 usb_error_t err; 868 869 DPRINTF("port %d\n", portno); 870 871 udev = sc->sc_udev; 872 child = usb_bus_port_get_device(udev->bus, 873 udev->hub->ports + portno - 1); 874 875 /* first clear the port suspend change bit */ 876 877 if (usb_device_20_compatible(udev)) { 878 err = usbd_req_clear_port_feature(udev, NULL, 879 portno, UHF_C_PORT_SUSPEND); 880 } else { 881 err = usbd_req_clear_port_feature(udev, NULL, 882 portno, UHF_C_PORT_LINK_STATE); 883 } 884 885 if (err) { 886 DPRINTF("clearing suspend failed.\n"); 887 goto done; 888 } 889 /* get fresh status */ 890 891 err = uhub_read_port_status(sc, portno); 892 if (err) { 893 DPRINTF("reading port status failed.\n"); 894 goto done; 895 } 896 /* convert current state */ 897 898 if (usb_device_20_compatible(udev)) { 899 if (sc->sc_st.port_status & UPS_SUSPEND) { 900 is_suspend = 1; 901 } else { 902 is_suspend = 0; 903 } 904 } else { 905 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) { 906 case UPS_PORT_LS_U3: 907 is_suspend = 1; 908 break; 909 case UPS_PORT_LS_SS_INA: 910 usbd_req_warm_reset_port(udev, NULL, portno); 911 is_suspend = 0; 912 break; 913 default: 914 is_suspend = 0; 915 break; 916 } 917 } 918 919 DPRINTF("suspended=%u\n", is_suspend); 920 921 /* do the suspend or resume */ 922 923 if (child) { 924 /* 925 * This code handle two cases: 1) Host Mode - we can only 926 * receive resume here 2) Device Mode - we can receive 927 * suspend and resume here 928 */ 929 if (is_suspend == 0) 930 usb_dev_resume_peer(child); 931 else if (child->flags.usb_mode == USB_MODE_DEVICE) 932 usb_dev_suspend_peer(child); 933 } 934 done: 935 return (err); 936 } 937 938 /*------------------------------------------------------------------------* 939 * uhub_root_interrupt 940 * 941 * This function is called when a Root HUB interrupt has 942 * happened. "ptr" and "len" makes up the Root HUB interrupt 943 * packet. This function is called having the "bus_mtx" locked. 944 *------------------------------------------------------------------------*/ 945 void 946 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len) 947 { 948 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 949 950 usb_needs_explore(bus, 0); 951 } 952 953 static uint8_t 954 uhub_is_too_deep(struct usb_device *udev) 955 { 956 switch (udev->speed) { 957 case USB_SPEED_FULL: 958 case USB_SPEED_LOW: 959 case USB_SPEED_HIGH: 960 if (udev->depth > USB_HUB_MAX_DEPTH) 961 return (1); 962 break; 963 case USB_SPEED_SUPER: 964 if (udev->depth > USB_SS_HUB_DEPTH_MAX) 965 return (1); 966 break; 967 default: 968 break; 969 } 970 return (0); 971 } 972 973 /*------------------------------------------------------------------------* 974 * uhub_explore 975 * 976 * Returns: 977 * 0: Success 978 * Else: Failure 979 *------------------------------------------------------------------------*/ 980 static usb_error_t 981 uhub_explore(struct usb_device *udev) 982 { 983 struct usb_hub *hub; 984 struct uhub_softc *sc; 985 struct usb_port *up; 986 usb_error_t err; 987 uint8_t portno; 988 uint8_t x; 989 uint8_t do_unlock; 990 991 hub = udev->hub; 992 sc = hub->hubsoftc; 993 994 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address); 995 996 /* ignore devices that are too deep */ 997 if (uhub_is_too_deep(udev)) 998 return (USB_ERR_TOO_DEEP); 999 1000 /* check if device is suspended */ 1001 if (udev->flags.self_suspended) { 1002 /* need to wait until the child signals resume */ 1003 DPRINTF("Device is suspended!\n"); 1004 return (0); 1005 } 1006 1007 /* 1008 * Make sure we don't race against user-space applications 1009 * like LibUSB: 1010 */ 1011 do_unlock = usbd_enum_lock(udev); 1012 1013 for (x = 0; x != hub->nports; x++) { 1014 up = hub->ports + x; 1015 portno = x + 1; 1016 1017 err = uhub_read_port_status(sc, portno); 1018 if (err) { 1019 /* most likely the HUB is gone */ 1020 break; 1021 } 1022 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) { 1023 DPRINTF("Overcurrent on port %u.\n", portno); 1024 err = usbd_req_clear_port_feature( 1025 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT); 1026 if (err) { 1027 /* most likely the HUB is gone */ 1028 break; 1029 } 1030 } 1031 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) { 1032 /* 1033 * Fake a connect status change so that the 1034 * status gets checked initially! 1035 */ 1036 sc->sc_st.port_change |= 1037 UPS_C_CONNECT_STATUS; 1038 } 1039 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) { 1040 err = usbd_req_clear_port_feature( 1041 udev, NULL, portno, UHF_C_PORT_ENABLE); 1042 if (err) { 1043 /* most likely the HUB is gone */ 1044 break; 1045 } 1046 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) { 1047 /* 1048 * Ignore the port error if the device 1049 * has vanished ! 1050 */ 1051 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) { 1052 DPRINTFN(0, "illegal enable change, " 1053 "port %d\n", portno); 1054 } else { 1055 1056 if (up->restartcnt == USB_RESTART_MAX) { 1057 /* XXX could try another speed ? */ 1058 DPRINTFN(0, "port error, giving up " 1059 "port %d\n", portno); 1060 } else { 1061 sc->sc_st.port_change |= 1062 UPS_C_CONNECT_STATUS; 1063 up->restartcnt++; 1064 } 1065 } 1066 } 1067 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) { 1068 err = uhub_reattach_port(sc, portno); 1069 if (err) { 1070 /* most likely the HUB is gone */ 1071 break; 1072 } 1073 } 1074 if (sc->sc_st.port_change & (UPS_C_SUSPEND | 1075 UPS_C_PORT_LINK_STATE)) { 1076 err = uhub_suspend_resume_port(sc, portno); 1077 if (err) { 1078 /* most likely the HUB is gone */ 1079 break; 1080 } 1081 } 1082 err = uhub_explore_sub(sc, up); 1083 if (err) { 1084 /* no device(s) present */ 1085 continue; 1086 } 1087 /* explore succeeded - reset restart counter */ 1088 up->restartcnt = 0; 1089 } 1090 1091 if (do_unlock) 1092 usbd_enum_unlock(udev); 1093 1094 /* initial status checked */ 1095 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE; 1096 1097 /* return success */ 1098 return (USB_ERR_NORMAL_COMPLETION); 1099 } 1100 1101 static int 1102 uhub_probe(device_t dev) 1103 { 1104 struct usb_attach_arg *uaa = device_get_ivars(dev); 1105 1106 if (uaa->usb_mode != USB_MODE_HOST) 1107 return (ENXIO); 1108 1109 /* 1110 * The subclass for USB HUBs is currently ignored because it 1111 * is 0 for some and 1 for others. 1112 */ 1113 if (uaa->info.bConfigIndex == 0 && 1114 uaa->info.bDeviceClass == UDCLASS_HUB) 1115 return (0); 1116 1117 return (ENXIO); 1118 } 1119 1120 /* NOTE: The information returned by this function can be wrong. */ 1121 usb_error_t 1122 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt) 1123 { 1124 struct usb_hub_descriptor hubdesc20; 1125 struct usb_hub_ss_descriptor hubdesc30; 1126 usb_error_t err; 1127 uint8_t nports; 1128 uint8_t tt; 1129 1130 if (udev->ddesc.bDeviceClass != UDCLASS_HUB) 1131 return (USB_ERR_INVAL); 1132 1133 nports = 0; 1134 tt = 0; 1135 1136 switch (udev->speed) { 1137 case USB_SPEED_LOW: 1138 case USB_SPEED_FULL: 1139 case USB_SPEED_HIGH: 1140 /* assuming that there is one port */ 1141 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1); 1142 if (err) { 1143 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed," 1144 "error=%s\n", usbd_errstr(err)); 1145 break; 1146 } 1147 nports = hubdesc20.bNbrPorts; 1148 if (nports > 127) 1149 nports = 127; 1150 1151 if (udev->speed == USB_SPEED_HIGH) 1152 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3; 1153 break; 1154 1155 case USB_SPEED_SUPER: 1156 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1); 1157 if (err) { 1158 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed," 1159 "error=%s\n", usbd_errstr(err)); 1160 break; 1161 } 1162 nports = hubdesc30.bNbrPorts; 1163 if (nports > 16) 1164 nports = 16; 1165 break; 1166 1167 default: 1168 err = USB_ERR_INVAL; 1169 break; 1170 } 1171 1172 if (pnports != NULL) 1173 *pnports = nports; 1174 1175 if (ptt != NULL) 1176 *ptt = tt; 1177 1178 return (err); 1179 } 1180 1181 static int 1182 uhub_attach(device_t dev) 1183 { 1184 struct uhub_softc *sc = device_get_softc(dev); 1185 struct usb_attach_arg *uaa = device_get_ivars(dev); 1186 struct usb_device *udev = uaa->device; 1187 struct usb_device *parent_hub = udev->parent_hub; 1188 struct usb_hub *hub; 1189 struct usb_hub_descriptor hubdesc20; 1190 struct usb_hub_ss_descriptor hubdesc30; 1191 uint16_t pwrdly; 1192 uint16_t nports; 1193 uint8_t x; 1194 uint8_t portno; 1195 uint8_t removable; 1196 uint8_t iface_index; 1197 usb_error_t err; 1198 1199 sc->sc_udev = udev; 1200 sc->sc_dev = dev; 1201 1202 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF); 1203 1204 device_set_usb_desc(dev); 1205 1206 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, " 1207 "parent->selfpowered=%d\n", 1208 udev->depth, 1209 udev->flags.self_powered, 1210 parent_hub, 1211 parent_hub ? 1212 parent_hub->flags.self_powered : 0); 1213 1214 if (uhub_is_too_deep(udev)) { 1215 DPRINTFN(0, "HUB at depth %d, " 1216 "exceeds maximum. HUB ignored\n", (int)udev->depth); 1217 goto error; 1218 } 1219 1220 if (!udev->flags.self_powered && parent_hub && 1221 !parent_hub->flags.self_powered) { 1222 DPRINTFN(0, "Bus powered HUB connected to " 1223 "bus powered HUB. HUB ignored\n"); 1224 goto error; 1225 } 1226 1227 if (UHUB_IS_MULTI_TT(sc)) { 1228 err = usbd_set_alt_interface_index(udev, 0, 1); 1229 if (err) { 1230 device_printf(dev, "MTT could not be enabled\n"); 1231 goto error; 1232 } 1233 device_printf(dev, "MTT enabled\n"); 1234 } 1235 1236 /* get HUB descriptor */ 1237 1238 DPRINTFN(2, "Getting HUB descriptor\n"); 1239 1240 switch (udev->speed) { 1241 case USB_SPEED_LOW: 1242 case USB_SPEED_FULL: 1243 case USB_SPEED_HIGH: 1244 /* assuming that there is one port */ 1245 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1); 1246 if (err) { 1247 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed," 1248 "error=%s\n", usbd_errstr(err)); 1249 goto error; 1250 } 1251 /* get number of ports */ 1252 nports = hubdesc20.bNbrPorts; 1253 1254 /* get power delay */ 1255 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) + 1256 usb_extra_power_up_time); 1257 1258 /* get complete HUB descriptor */ 1259 if (nports >= 8) { 1260 /* check number of ports */ 1261 if (nports > 127) { 1262 DPRINTFN(0, "Invalid number of USB 2.0 ports," 1263 "error=%s\n", usbd_errstr(err)); 1264 goto error; 1265 } 1266 /* get complete HUB descriptor */ 1267 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports); 1268 1269 if (err) { 1270 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed," 1271 "error=%s\n", usbd_errstr(err)); 1272 goto error; 1273 } 1274 if (hubdesc20.bNbrPorts != nports) { 1275 DPRINTFN(0, "Number of ports changed\n"); 1276 goto error; 1277 } 1278 } 1279 break; 1280 case USB_SPEED_SUPER: 1281 if (udev->parent_hub != NULL) { 1282 err = usbd_req_set_hub_depth(udev, NULL, 1283 udev->depth - 1); 1284 if (err) { 1285 DPRINTFN(0, "Setting USB 3.0 HUB depth failed," 1286 "error=%s\n", usbd_errstr(err)); 1287 goto error; 1288 } 1289 } 1290 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1); 1291 if (err) { 1292 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed," 1293 "error=%s\n", usbd_errstr(err)); 1294 goto error; 1295 } 1296 /* get number of ports */ 1297 nports = hubdesc30.bNbrPorts; 1298 1299 /* get power delay */ 1300 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) + 1301 usb_extra_power_up_time); 1302 1303 /* get complete HUB descriptor */ 1304 if (nports >= 8) { 1305 /* check number of ports */ 1306 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) { 1307 DPRINTFN(0, "Invalid number of USB 3.0 ports," 1308 "error=%s\n", usbd_errstr(err)); 1309 goto error; 1310 } 1311 /* get complete HUB descriptor */ 1312 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports); 1313 1314 if (err) { 1315 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed," 1316 "error=%s\n", usbd_errstr(err)); 1317 goto error; 1318 } 1319 if (hubdesc30.bNbrPorts != nports) { 1320 DPRINTFN(0, "Number of ports changed\n"); 1321 goto error; 1322 } 1323 } 1324 break; 1325 default: 1326 DPRINTF("Assuming HUB has only one port\n"); 1327 /* default number of ports */ 1328 nports = 1; 1329 /* default power delay */ 1330 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time); 1331 break; 1332 } 1333 if (nports == 0) { 1334 DPRINTFN(0, "portless HUB\n"); 1335 goto error; 1336 } 1337 if (nports > USB_MAX_PORTS) { 1338 DPRINTF("Port limit exceeded\n"); 1339 goto error; 1340 } 1341 #if (USB_HAVE_FIXED_PORT == 0) 1342 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports), 1343 M_USBDEV, M_WAITOK | M_ZERO); 1344 1345 if (hub == NULL) 1346 goto error; 1347 #else 1348 hub = &sc->sc_hub; 1349 #endif 1350 udev->hub = hub; 1351 1352 /* initialize HUB structure */ 1353 hub->hubsoftc = sc; 1354 hub->explore = &uhub_explore; 1355 hub->nports = nports; 1356 hub->hubudev = udev; 1357 #if USB_HAVE_TT_SUPPORT 1358 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc; 1359 hub->tt_msg[0].udev = udev; 1360 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc; 1361 hub->tt_msg[1].udev = udev; 1362 #endif 1363 /* if self powered hub, give ports maximum current */ 1364 if (udev->flags.self_powered) { 1365 hub->portpower = USB_MAX_POWER; 1366 } else { 1367 hub->portpower = USB_MIN_POWER; 1368 } 1369 1370 /* set up interrupt pipe */ 1371 iface_index = 0; 1372 if (udev->parent_hub == NULL) { 1373 /* root HUB is special */ 1374 err = 0; 1375 } else { 1376 /* normal HUB */ 1377 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer, 1378 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx); 1379 } 1380 if (err) { 1381 DPRINTFN(0, "cannot setup interrupt transfer, " 1382 "errstr=%s\n", usbd_errstr(err)); 1383 goto error; 1384 } 1385 /* wait with power off for a while */ 1386 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME)); 1387 1388 /* 1389 * To have the best chance of success we do things in the exact same 1390 * order as Windoze98. This should not be necessary, but some 1391 * devices do not follow the USB specs to the letter. 1392 * 1393 * These are the events on the bus when a hub is attached: 1394 * Get device and config descriptors (see attach code) 1395 * Get hub descriptor (see above) 1396 * For all ports 1397 * turn on power 1398 * wait for power to become stable 1399 * (all below happens in explore code) 1400 * For all ports 1401 * clear C_PORT_CONNECTION 1402 * For all ports 1403 * get port status 1404 * if device connected 1405 * wait 100 ms 1406 * turn on reset 1407 * wait 1408 * clear C_PORT_RESET 1409 * get port status 1410 * proceed with device attachment 1411 */ 1412 1413 /* XXX should check for none, individual, or ganged power? */ 1414 1415 removable = 0; 1416 1417 for (x = 0; x != nports; x++) { 1418 /* set up data structures */ 1419 struct usb_port *up = hub->ports + x; 1420 1421 up->device_index = 0; 1422 up->restartcnt = 0; 1423 portno = x + 1; 1424 1425 /* check if port is removable */ 1426 switch (udev->speed) { 1427 case USB_SPEED_LOW: 1428 case USB_SPEED_FULL: 1429 case USB_SPEED_HIGH: 1430 if (!UHD_NOT_REMOV(&hubdesc20, portno)) 1431 removable++; 1432 break; 1433 case USB_SPEED_SUPER: 1434 if (!UHD_NOT_REMOV(&hubdesc30, portno)) 1435 removable++; 1436 break; 1437 default: 1438 DPRINTF("Assuming removable port\n"); 1439 removable++; 1440 break; 1441 } 1442 if (!err) { 1443 /* turn the power on */ 1444 err = usbd_req_set_port_feature(udev, NULL, 1445 portno, UHF_PORT_POWER); 1446 } 1447 if (err) { 1448 DPRINTFN(0, "port %d power on failed, %s\n", 1449 portno, usbd_errstr(err)); 1450 } 1451 DPRINTF("turn on port %d power\n", 1452 portno); 1453 1454 /* wait for stable power */ 1455 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly)); 1456 } 1457 1458 device_printf(dev, "%d port%s with %d " 1459 "removable, %s powered\n", nports, (nports != 1) ? "s" : "", 1460 removable, udev->flags.self_powered ? "self" : "bus"); 1461 1462 /* Start the interrupt endpoint, if any */ 1463 1464 mtx_lock(&sc->sc_mtx); 1465 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]); 1466 mtx_unlock(&sc->sc_mtx); 1467 1468 /* Enable automatic power save on all USB HUBs */ 1469 1470 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE); 1471 1472 return (0); 1473 1474 error: 1475 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER); 1476 1477 #if (USB_HAVE_FIXED_PORT == 0) 1478 free(udev->hub, M_USBDEV); 1479 #endif 1480 udev->hub = NULL; 1481 1482 mtx_destroy(&sc->sc_mtx); 1483 1484 return (ENXIO); 1485 } 1486 1487 /* 1488 * Called from process context when the hub is gone. 1489 * Detach all devices on active ports. 1490 */ 1491 static int 1492 uhub_detach(device_t dev) 1493 { 1494 struct uhub_softc *sc = device_get_softc(dev); 1495 struct usb_hub *hub = sc->sc_udev->hub; 1496 struct usb_bus *bus = sc->sc_udev->bus; 1497 struct usb_device *child; 1498 uint8_t x; 1499 1500 if (hub == NULL) /* must be partially working */ 1501 return (0); 1502 1503 /* Make sure interrupt transfer is gone. */ 1504 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER); 1505 1506 /* Detach all ports */ 1507 for (x = 0; x != hub->nports; x++) { 1508 1509 child = usb_bus_port_get_device(bus, hub->ports + x); 1510 1511 if (child == NULL) { 1512 continue; 1513 } 1514 1515 /* 1516 * Free USB device and all subdevices, if any. 1517 */ 1518 usb_free_device(child, 0); 1519 } 1520 1521 #if USB_HAVE_TT_SUPPORT 1522 /* Make sure our TT messages are not queued anywhere */ 1523 USB_BUS_LOCK(bus); 1524 usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus), 1525 &hub->tt_msg[0], &hub->tt_msg[1]); 1526 USB_BUS_UNLOCK(bus); 1527 #endif 1528 1529 #if (USB_HAVE_FIXED_PORT == 0) 1530 free(hub, M_USBDEV); 1531 #endif 1532 sc->sc_udev->hub = NULL; 1533 1534 mtx_destroy(&sc->sc_mtx); 1535 1536 return (0); 1537 } 1538 1539 static int 1540 uhub_suspend(device_t dev) 1541 { 1542 DPRINTF("\n"); 1543 /* Sub-devices are not suspended here! */ 1544 return (0); 1545 } 1546 1547 static int 1548 uhub_resume(device_t dev) 1549 { 1550 DPRINTF("\n"); 1551 /* Sub-devices are not resumed here! */ 1552 return (0); 1553 } 1554 1555 static void 1556 uhub_driver_added(device_t dev, driver_t *driver) 1557 { 1558 usb_needs_explore_all(); 1559 } 1560 1561 struct hub_result { 1562 struct usb_device *udev; 1563 uint8_t portno; 1564 uint8_t iface_index; 1565 }; 1566 1567 static void 1568 uhub_find_iface_index(struct usb_hub *hub, device_t child, 1569 struct hub_result *res) 1570 { 1571 struct usb_interface *iface; 1572 struct usb_device *udev; 1573 uint8_t nports; 1574 uint8_t x; 1575 uint8_t i; 1576 1577 nports = hub->nports; 1578 for (x = 0; x != nports; x++) { 1579 udev = usb_bus_port_get_device(hub->hubudev->bus, 1580 hub->ports + x); 1581 if (!udev) { 1582 continue; 1583 } 1584 for (i = 0; i != USB_IFACE_MAX; i++) { 1585 iface = usbd_get_iface(udev, i); 1586 if (iface && 1587 (iface->subdev == child)) { 1588 res->iface_index = i; 1589 res->udev = udev; 1590 res->portno = x + 1; 1591 return; 1592 } 1593 } 1594 } 1595 res->iface_index = 0; 1596 res->udev = NULL; 1597 res->portno = 0; 1598 } 1599 1600 static int 1601 uhub_child_location_string(device_t parent, device_t child, 1602 char *buf, size_t buflen) 1603 { 1604 struct uhub_softc *sc; 1605 struct usb_hub *hub; 1606 struct hub_result res; 1607 1608 if (!device_is_attached(parent)) { 1609 if (buflen) 1610 buf[0] = 0; 1611 return (0); 1612 } 1613 1614 sc = device_get_softc(parent); 1615 hub = sc->sc_udev->hub; 1616 1617 mtx_lock(&Giant); 1618 uhub_find_iface_index(hub, child, &res); 1619 if (!res.udev) { 1620 DPRINTF("device not on hub\n"); 1621 if (buflen) { 1622 buf[0] = '\0'; 1623 } 1624 goto done; 1625 } 1626 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u", 1627 device_get_unit(res.udev->bus->bdev), 1628 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0, 1629 res.portno, 1630 res.udev->device_index, res.iface_index); 1631 done: 1632 mtx_unlock(&Giant); 1633 1634 return (0); 1635 } 1636 1637 static int 1638 uhub_child_pnpinfo_string(device_t parent, device_t child, 1639 char *buf, size_t buflen) 1640 { 1641 struct uhub_softc *sc; 1642 struct usb_hub *hub; 1643 struct usb_interface *iface; 1644 struct hub_result res; 1645 1646 if (!device_is_attached(parent)) { 1647 if (buflen) 1648 buf[0] = 0; 1649 return (0); 1650 } 1651 1652 sc = device_get_softc(parent); 1653 hub = sc->sc_udev->hub; 1654 1655 mtx_lock(&Giant); 1656 uhub_find_iface_index(hub, child, &res); 1657 if (!res.udev) { 1658 DPRINTF("device not on hub\n"); 1659 if (buflen) { 1660 buf[0] = '\0'; 1661 } 1662 goto done; 1663 } 1664 iface = usbd_get_iface(res.udev, res.iface_index); 1665 if (iface && iface->idesc) { 1666 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x " 1667 "devclass=0x%02x devsubclass=0x%02x " 1668 "sernum=\"%s\" " 1669 "release=0x%04x " 1670 "mode=%s " 1671 "intclass=0x%02x intsubclass=0x%02x " 1672 "intprotocol=0x%02x " "%s%s", 1673 UGETW(res.udev->ddesc.idVendor), 1674 UGETW(res.udev->ddesc.idProduct), 1675 res.udev->ddesc.bDeviceClass, 1676 res.udev->ddesc.bDeviceSubClass, 1677 usb_get_serial(res.udev), 1678 UGETW(res.udev->ddesc.bcdDevice), 1679 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 1680 iface->idesc->bInterfaceClass, 1681 iface->idesc->bInterfaceSubClass, 1682 iface->idesc->bInterfaceProtocol, 1683 iface->pnpinfo ? " " : "", 1684 iface->pnpinfo ? iface->pnpinfo : ""); 1685 } else { 1686 if (buflen) { 1687 buf[0] = '\0'; 1688 } 1689 goto done; 1690 } 1691 done: 1692 mtx_unlock(&Giant); 1693 1694 return (0); 1695 } 1696 1697 /* 1698 * The USB Transaction Translator: 1699 * =============================== 1700 * 1701 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed 1702 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT 1703 * USB transfers. To utilize bandwidth dynamically the "scatter and 1704 * gather" principle must be applied. This means that bandwidth must 1705 * be divided into equal parts of bandwidth. With regard to USB all 1706 * data is transferred in smaller packets with length 1707 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is 1708 * not a constant! 1709 * 1710 * The bandwidth scheduler which I have implemented will simply pack 1711 * the USB transfers back to back until there is no more space in the 1712 * schedule. Out of the 8 microframes which the USB 2.0 standard 1713 * provides, only 6 are available for non-HIGH-speed devices. I have 1714 * reserved the first 4 microframes for ISOCHRONOUS transfers. The 1715 * last 2 microframes I have reserved for INTERRUPT transfers. Without 1716 * this division, it is very difficult to allocate and free bandwidth 1717 * dynamically. 1718 * 1719 * NOTE about the Transaction Translator in USB HUBs: 1720 * 1721 * USB HUBs have a very simple Transaction Translator, that will 1722 * simply pipeline all the SPLIT transactions. That means that the 1723 * transactions will be executed in the order they are queued! 1724 * 1725 */ 1726 1727 /*------------------------------------------------------------------------* 1728 * usb_intr_find_best_slot 1729 * 1730 * Return value: 1731 * The best Transaction Translation slot for an interrupt endpoint. 1732 *------------------------------------------------------------------------*/ 1733 static uint8_t 1734 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start, 1735 uint8_t end, uint8_t mask) 1736 { 1737 usb_size_t min = (usb_size_t)-1; 1738 usb_size_t sum; 1739 uint8_t x; 1740 uint8_t y; 1741 uint8_t z; 1742 1743 y = 0; 1744 1745 /* find the last slot with lesser used bandwidth */ 1746 1747 for (x = start; x < end; x++) { 1748 1749 sum = 0; 1750 1751 /* compute sum of bandwidth */ 1752 for (z = x; z < end; z++) { 1753 if (mask & (1U << (z - x))) 1754 sum += ptr[z]; 1755 } 1756 1757 /* check if the current multi-slot is more optimal */ 1758 if (min >= sum) { 1759 min = sum; 1760 y = x; 1761 } 1762 1763 /* check if the mask is about to be shifted out */ 1764 if (mask & (1U << (end - 1 - x))) 1765 break; 1766 } 1767 return (y); 1768 } 1769 1770 /*------------------------------------------------------------------------* 1771 * usb_hs_bandwidth_adjust 1772 * 1773 * This function will update the bandwith usage for the microframe 1774 * having index "slot" by "len" bytes. "len" can be negative. If the 1775 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX" 1776 * the "slot" argument will be replaced by the slot having least used 1777 * bandwidth. The "mask" argument is used for multi-slot allocations. 1778 * 1779 * Returns: 1780 * The slot in which the bandwidth update was done: 0..7 1781 *------------------------------------------------------------------------*/ 1782 static uint8_t 1783 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len, 1784 uint8_t slot, uint8_t mask) 1785 { 1786 struct usb_bus *bus = udev->bus; 1787 struct usb_hub *hub; 1788 enum usb_dev_speed speed; 1789 uint8_t x; 1790 1791 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 1792 1793 speed = usbd_get_speed(udev); 1794 1795 switch (speed) { 1796 case USB_SPEED_LOW: 1797 case USB_SPEED_FULL: 1798 if (speed == USB_SPEED_LOW) { 1799 len *= 8; 1800 } 1801 /* 1802 * The Host Controller Driver should have 1803 * performed checks so that the lookup 1804 * below does not result in a NULL pointer 1805 * access. 1806 */ 1807 1808 hub = udev->parent_hs_hub->hub; 1809 if (slot >= USB_HS_MICRO_FRAMES_MAX) { 1810 slot = usb_intr_find_best_slot(hub->uframe_usage, 1811 USB_FS_ISOC_UFRAME_MAX, 6, mask); 1812 } 1813 for (x = slot; x < 8; x++) { 1814 if (mask & (1U << (x - slot))) { 1815 hub->uframe_usage[x] += len; 1816 bus->uframe_usage[x] += len; 1817 } 1818 } 1819 break; 1820 default: 1821 if (slot >= USB_HS_MICRO_FRAMES_MAX) { 1822 slot = usb_intr_find_best_slot(bus->uframe_usage, 0, 1823 USB_HS_MICRO_FRAMES_MAX, mask); 1824 } 1825 for (x = slot; x < 8; x++) { 1826 if (mask & (1U << (x - slot))) { 1827 bus->uframe_usage[x] += len; 1828 } 1829 } 1830 break; 1831 } 1832 return (slot); 1833 } 1834 1835 /*------------------------------------------------------------------------* 1836 * usb_hs_bandwidth_alloc 1837 * 1838 * This function is a wrapper function for "usb_hs_bandwidth_adjust()". 1839 *------------------------------------------------------------------------*/ 1840 void 1841 usb_hs_bandwidth_alloc(struct usb_xfer *xfer) 1842 { 1843 struct usb_device *udev; 1844 uint8_t slot; 1845 uint8_t mask; 1846 uint8_t speed; 1847 1848 udev = xfer->xroot->udev; 1849 1850 if (udev->flags.usb_mode != USB_MODE_HOST) 1851 return; /* not supported */ 1852 1853 xfer->endpoint->refcount_bw++; 1854 if (xfer->endpoint->refcount_bw != 1) 1855 return; /* already allocated */ 1856 1857 speed = usbd_get_speed(udev); 1858 1859 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) { 1860 case UE_INTERRUPT: 1861 /* allocate a microframe slot */ 1862 1863 mask = 0x01; 1864 slot = usb_hs_bandwidth_adjust(udev, 1865 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask); 1866 1867 xfer->endpoint->usb_uframe = slot; 1868 xfer->endpoint->usb_smask = mask << slot; 1869 1870 if ((speed != USB_SPEED_FULL) && 1871 (speed != USB_SPEED_LOW)) { 1872 xfer->endpoint->usb_cmask = 0x00 ; 1873 } else { 1874 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE; 1875 } 1876 break; 1877 1878 case UE_ISOCHRONOUS: 1879 switch (usbd_xfer_get_fps_shift(xfer)) { 1880 case 0: 1881 mask = 0xFF; 1882 break; 1883 case 1: 1884 mask = 0x55; 1885 break; 1886 case 2: 1887 mask = 0x11; 1888 break; 1889 default: 1890 mask = 0x01; 1891 break; 1892 } 1893 1894 /* allocate a microframe multi-slot */ 1895 1896 slot = usb_hs_bandwidth_adjust(udev, 1897 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask); 1898 1899 xfer->endpoint->usb_uframe = slot; 1900 xfer->endpoint->usb_cmask = 0; 1901 xfer->endpoint->usb_smask = mask << slot; 1902 break; 1903 1904 default: 1905 xfer->endpoint->usb_uframe = 0; 1906 xfer->endpoint->usb_cmask = 0; 1907 xfer->endpoint->usb_smask = 0; 1908 break; 1909 } 1910 1911 DPRINTFN(11, "slot=%d, mask=0x%02x\n", 1912 xfer->endpoint->usb_uframe, 1913 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe); 1914 } 1915 1916 /*------------------------------------------------------------------------* 1917 * usb_hs_bandwidth_free 1918 * 1919 * This function is a wrapper function for "usb_hs_bandwidth_adjust()". 1920 *------------------------------------------------------------------------*/ 1921 void 1922 usb_hs_bandwidth_free(struct usb_xfer *xfer) 1923 { 1924 struct usb_device *udev; 1925 uint8_t slot; 1926 uint8_t mask; 1927 1928 udev = xfer->xroot->udev; 1929 1930 if (udev->flags.usb_mode != USB_MODE_HOST) 1931 return; /* not supported */ 1932 1933 xfer->endpoint->refcount_bw--; 1934 if (xfer->endpoint->refcount_bw != 0) 1935 return; /* still allocated */ 1936 1937 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) { 1938 case UE_INTERRUPT: 1939 case UE_ISOCHRONOUS: 1940 1941 slot = xfer->endpoint->usb_uframe; 1942 mask = xfer->endpoint->usb_smask; 1943 1944 /* free microframe slot(s): */ 1945 usb_hs_bandwidth_adjust(udev, 1946 -xfer->max_frame_size, slot, mask >> slot); 1947 1948 DPRINTFN(11, "slot=%d, mask=0x%02x\n", 1949 slot, mask >> slot); 1950 1951 xfer->endpoint->usb_uframe = 0; 1952 xfer->endpoint->usb_cmask = 0; 1953 xfer->endpoint->usb_smask = 0; 1954 break; 1955 1956 default: 1957 break; 1958 } 1959 } 1960 1961 /*------------------------------------------------------------------------* 1962 * usb_isoc_time_expand 1963 * 1964 * This function will expand the time counter from 7-bit to 16-bit. 1965 * 1966 * Returns: 1967 * 16-bit isochronous time counter. 1968 *------------------------------------------------------------------------*/ 1969 uint16_t 1970 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr) 1971 { 1972 uint16_t rem; 1973 1974 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 1975 1976 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1); 1977 1978 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1); 1979 1980 if (isoc_time_curr < rem) { 1981 /* the time counter wrapped around */ 1982 bus->isoc_time_last += USB_ISOC_TIME_MAX; 1983 } 1984 /* update the remainder */ 1985 1986 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1); 1987 bus->isoc_time_last |= isoc_time_curr; 1988 1989 return (bus->isoc_time_last); 1990 } 1991 1992 /*------------------------------------------------------------------------* 1993 * usbd_fs_isoc_schedule_alloc_slot 1994 * 1995 * This function will allocate bandwidth for an isochronous FULL speed 1996 * transaction in the FULL speed schedule. 1997 * 1998 * Returns: 1999 * <8: Success 2000 * Else: Error 2001 *------------------------------------------------------------------------*/ 2002 #if USB_HAVE_TT_SUPPORT 2003 uint8_t 2004 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time) 2005 { 2006 struct usb_xfer *xfer; 2007 struct usb_xfer *pipe_xfer; 2008 struct usb_bus *bus; 2009 usb_frlength_t len; 2010 usb_frlength_t data_len; 2011 uint16_t delta; 2012 uint16_t slot; 2013 uint8_t retval; 2014 2015 data_len = 0; 2016 slot = 0; 2017 2018 bus = isoc_xfer->xroot->bus; 2019 2020 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) { 2021 2022 /* skip self, if any */ 2023 2024 if (xfer == isoc_xfer) 2025 continue; 2026 2027 /* check if this USB transfer is going through the same TT */ 2028 2029 if (xfer->xroot->udev->parent_hs_hub != 2030 isoc_xfer->xroot->udev->parent_hs_hub) { 2031 continue; 2032 } 2033 if ((isoc_xfer->xroot->udev->parent_hs_hub-> 2034 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) && 2035 (xfer->xroot->udev->hs_port_no != 2036 isoc_xfer->xroot->udev->hs_port_no)) { 2037 continue; 2038 } 2039 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods) 2040 continue; 2041 2042 /* check if isoc_time is part of this transfer */ 2043 2044 delta = xfer->isoc_time_complete - isoc_time; 2045 if (delta > 0 && delta <= xfer->nframes) { 2046 delta = xfer->nframes - delta; 2047 2048 len = xfer->frlengths[delta]; 2049 len += 8; 2050 len *= 7; 2051 len /= 6; 2052 2053 data_len += len; 2054 } 2055 2056 /* 2057 * Check double buffered transfers. Only stream ID 2058 * equal to zero is valid here! 2059 */ 2060 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head, 2061 wait_entry) { 2062 2063 /* skip self, if any */ 2064 2065 if (pipe_xfer == isoc_xfer) 2066 continue; 2067 2068 /* check if isoc_time is part of this transfer */ 2069 2070 delta = pipe_xfer->isoc_time_complete - isoc_time; 2071 if (delta > 0 && delta <= pipe_xfer->nframes) { 2072 delta = pipe_xfer->nframes - delta; 2073 2074 len = pipe_xfer->frlengths[delta]; 2075 len += 8; 2076 len *= 7; 2077 len /= 6; 2078 2079 data_len += len; 2080 } 2081 } 2082 } 2083 2084 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) { 2085 data_len -= USB_FS_BYTES_PER_HS_UFRAME; 2086 slot++; 2087 } 2088 2089 /* check for overflow */ 2090 2091 if (slot >= USB_FS_ISOC_UFRAME_MAX) 2092 return (255); 2093 2094 retval = slot; 2095 2096 delta = isoc_xfer->isoc_time_complete - isoc_time; 2097 if (delta > 0 && delta <= isoc_xfer->nframes) { 2098 delta = isoc_xfer->nframes - delta; 2099 2100 len = isoc_xfer->frlengths[delta]; 2101 len += 8; 2102 len *= 7; 2103 len /= 6; 2104 2105 data_len += len; 2106 } 2107 2108 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) { 2109 data_len -= USB_FS_BYTES_PER_HS_UFRAME; 2110 slot++; 2111 } 2112 2113 /* check for overflow */ 2114 2115 if (slot >= USB_FS_ISOC_UFRAME_MAX) 2116 return (255); 2117 2118 return (retval); 2119 } 2120 #endif 2121 2122 /*------------------------------------------------------------------------* 2123 * usb_bus_port_get_device 2124 * 2125 * This function is NULL safe. 2126 *------------------------------------------------------------------------*/ 2127 struct usb_device * 2128 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up) 2129 { 2130 if ((bus == NULL) || (up == NULL)) { 2131 /* be NULL safe */ 2132 return (NULL); 2133 } 2134 if (up->device_index == 0) { 2135 /* nothing to do */ 2136 return (NULL); 2137 } 2138 return (bus->devices[up->device_index]); 2139 } 2140 2141 /*------------------------------------------------------------------------* 2142 * usb_bus_port_set_device 2143 * 2144 * This function is NULL safe. 2145 *------------------------------------------------------------------------*/ 2146 void 2147 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up, 2148 struct usb_device *udev, uint8_t device_index) 2149 { 2150 if (bus == NULL) { 2151 /* be NULL safe */ 2152 return; 2153 } 2154 /* 2155 * There is only one case where we don't 2156 * have an USB port, and that is the Root Hub! 2157 */ 2158 if (up) { 2159 if (udev) { 2160 up->device_index = device_index; 2161 } else { 2162 device_index = up->device_index; 2163 up->device_index = 0; 2164 } 2165 } 2166 /* 2167 * Make relationships to our new device 2168 */ 2169 if (device_index != 0) { 2170 #if USB_HAVE_UGEN 2171 mtx_lock(&usb_ref_lock); 2172 #endif 2173 bus->devices[device_index] = udev; 2174 #if USB_HAVE_UGEN 2175 mtx_unlock(&usb_ref_lock); 2176 #endif 2177 } 2178 /* 2179 * Debug print 2180 */ 2181 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev); 2182 } 2183 2184 /*------------------------------------------------------------------------* 2185 * usb_needs_explore 2186 * 2187 * This functions is called when the USB event thread needs to run. 2188 *------------------------------------------------------------------------*/ 2189 void 2190 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe) 2191 { 2192 uint8_t do_unlock; 2193 2194 DPRINTF("\n"); 2195 2196 if (bus == NULL) { 2197 DPRINTF("No bus pointer!\n"); 2198 return; 2199 } 2200 if ((bus->devices == NULL) || 2201 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) { 2202 DPRINTF("No root HUB\n"); 2203 return; 2204 } 2205 if (mtx_owned(&bus->bus_mtx)) { 2206 do_unlock = 0; 2207 } else { 2208 USB_BUS_LOCK(bus); 2209 do_unlock = 1; 2210 } 2211 if (do_probe) { 2212 bus->do_probe = 1; 2213 } 2214 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2215 &bus->explore_msg[0], &bus->explore_msg[1])) { 2216 /* ignore */ 2217 } 2218 if (do_unlock) { 2219 USB_BUS_UNLOCK(bus); 2220 } 2221 } 2222 2223 /*------------------------------------------------------------------------* 2224 * usb_needs_explore_all 2225 * 2226 * This function is called whenever a new driver is loaded and will 2227 * cause that all USB busses are re-explored. 2228 *------------------------------------------------------------------------*/ 2229 void 2230 usb_needs_explore_all(void) 2231 { 2232 struct usb_bus *bus; 2233 devclass_t dc; 2234 device_t dev; 2235 int max; 2236 2237 DPRINTFN(3, "\n"); 2238 2239 dc = usb_devclass_ptr; 2240 if (dc == NULL) { 2241 DPRINTFN(0, "no devclass\n"); 2242 return; 2243 } 2244 /* 2245 * Explore all USB busses in parallell. 2246 */ 2247 max = devclass_get_maxunit(dc); 2248 while (max >= 0) { 2249 dev = devclass_get_device(dc, max); 2250 if (dev) { 2251 bus = device_get_softc(dev); 2252 if (bus) { 2253 usb_needs_explore(bus, 1); 2254 } 2255 } 2256 max--; 2257 } 2258 } 2259 2260 /*------------------------------------------------------------------------* 2261 * usb_bus_power_update 2262 * 2263 * This function will ensure that all USB devices on the given bus are 2264 * properly suspended or resumed according to the device transfer 2265 * state. 2266 *------------------------------------------------------------------------*/ 2267 #if USB_HAVE_POWERD 2268 void 2269 usb_bus_power_update(struct usb_bus *bus) 2270 { 2271 usb_needs_explore(bus, 0 /* no probe */ ); 2272 } 2273 #endif 2274 2275 /*------------------------------------------------------------------------* 2276 * usbd_transfer_power_ref 2277 * 2278 * This function will modify the power save reference counts and 2279 * wakeup the USB device associated with the given USB transfer, if 2280 * needed. 2281 *------------------------------------------------------------------------*/ 2282 #if USB_HAVE_POWERD 2283 void 2284 usbd_transfer_power_ref(struct usb_xfer *xfer, int val) 2285 { 2286 static const usb_power_mask_t power_mask[4] = { 2287 [UE_CONTROL] = USB_HW_POWER_CONTROL, 2288 [UE_BULK] = USB_HW_POWER_BULK, 2289 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT, 2290 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC, 2291 }; 2292 struct usb_device *udev; 2293 uint8_t needs_explore; 2294 uint8_t needs_hw_power; 2295 uint8_t xfer_type; 2296 2297 udev = xfer->xroot->udev; 2298 2299 if (udev->device_index == USB_ROOT_HUB_ADDR) { 2300 /* no power save for root HUB */ 2301 return; 2302 } 2303 USB_BUS_LOCK(udev->bus); 2304 2305 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE; 2306 2307 udev->pwr_save.last_xfer_time = ticks; 2308 udev->pwr_save.type_refs[xfer_type] += val; 2309 2310 if (xfer->flags_int.control_xfr) { 2311 udev->pwr_save.read_refs += val; 2312 if (xfer->flags_int.usb_mode == USB_MODE_HOST) { 2313 /* 2314 * It is not allowed to suspend during a 2315 * control transfer: 2316 */ 2317 udev->pwr_save.write_refs += val; 2318 } 2319 } else if (USB_GET_DATA_ISREAD(xfer)) { 2320 udev->pwr_save.read_refs += val; 2321 } else { 2322 udev->pwr_save.write_refs += val; 2323 } 2324 2325 if (val > 0) { 2326 if (udev->flags.self_suspended) 2327 needs_explore = usb_peer_should_wakeup(udev); 2328 else 2329 needs_explore = 0; 2330 2331 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) { 2332 DPRINTF("Adding type %u to power state\n", xfer_type); 2333 udev->bus->hw_power_state |= power_mask[xfer_type]; 2334 needs_hw_power = 1; 2335 } else { 2336 needs_hw_power = 0; 2337 } 2338 } else { 2339 needs_explore = 0; 2340 needs_hw_power = 0; 2341 } 2342 2343 USB_BUS_UNLOCK(udev->bus); 2344 2345 if (needs_explore) { 2346 DPRINTF("update\n"); 2347 usb_bus_power_update(udev->bus); 2348 } else if (needs_hw_power) { 2349 DPRINTF("needs power\n"); 2350 if (udev->bus->methods->set_hw_power != NULL) { 2351 (udev->bus->methods->set_hw_power) (udev->bus); 2352 } 2353 } 2354 } 2355 #endif 2356 2357 /*------------------------------------------------------------------------* 2358 * usb_peer_should_wakeup 2359 * 2360 * This function returns non-zero if the current device should wake up. 2361 *------------------------------------------------------------------------*/ 2362 static uint8_t 2363 usb_peer_should_wakeup(struct usb_device *udev) 2364 { 2365 return (((udev->power_mode == USB_POWER_MODE_ON) && 2366 (udev->flags.usb_mode == USB_MODE_HOST)) || 2367 (udev->driver_added_refcount != udev->bus->driver_added_refcount) || 2368 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) || 2369 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) || 2370 (udev->pwr_save.write_refs != 0) || 2371 ((udev->pwr_save.read_refs != 0) && 2372 (udev->flags.usb_mode == USB_MODE_HOST) && 2373 (usb_peer_can_wakeup(udev) == 0))); 2374 } 2375 2376 /*------------------------------------------------------------------------* 2377 * usb_bus_powerd 2378 * 2379 * This function implements the USB power daemon and is called 2380 * regularly from the USB explore thread. 2381 *------------------------------------------------------------------------*/ 2382 #if USB_HAVE_POWERD 2383 void 2384 usb_bus_powerd(struct usb_bus *bus) 2385 { 2386 struct usb_device *udev; 2387 usb_ticks_t temp; 2388 usb_ticks_t limit; 2389 usb_ticks_t mintime; 2390 usb_size_t type_refs[5]; 2391 uint8_t x; 2392 2393 limit = usb_power_timeout; 2394 if (limit == 0) 2395 limit = hz; 2396 else if (limit > 255) 2397 limit = 255 * hz; 2398 else 2399 limit = limit * hz; 2400 2401 DPRINTF("bus=%p\n", bus); 2402 2403 USB_BUS_LOCK(bus); 2404 2405 /* 2406 * The root HUB device is never suspended 2407 * and we simply skip it. 2408 */ 2409 for (x = USB_ROOT_HUB_ADDR + 1; 2410 x != bus->devices_max; x++) { 2411 2412 udev = bus->devices[x]; 2413 if (udev == NULL) 2414 continue; 2415 2416 temp = ticks - udev->pwr_save.last_xfer_time; 2417 2418 if (usb_peer_should_wakeup(udev)) { 2419 /* check if we are suspended */ 2420 if (udev->flags.self_suspended != 0) { 2421 USB_BUS_UNLOCK(bus); 2422 usb_dev_resume_peer(udev); 2423 USB_BUS_LOCK(bus); 2424 } 2425 } else if ((temp >= limit) && 2426 (udev->flags.usb_mode == USB_MODE_HOST) && 2427 (udev->flags.self_suspended == 0)) { 2428 /* try to do suspend */ 2429 2430 USB_BUS_UNLOCK(bus); 2431 usb_dev_suspend_peer(udev); 2432 USB_BUS_LOCK(bus); 2433 } 2434 } 2435 2436 /* reset counters */ 2437 2438 mintime = (usb_ticks_t)-1; 2439 type_refs[0] = 0; 2440 type_refs[1] = 0; 2441 type_refs[2] = 0; 2442 type_refs[3] = 0; 2443 type_refs[4] = 0; 2444 2445 /* Re-loop all the devices to get the actual state */ 2446 2447 for (x = USB_ROOT_HUB_ADDR + 1; 2448 x != bus->devices_max; x++) { 2449 2450 udev = bus->devices[x]; 2451 if (udev == NULL) 2452 continue; 2453 2454 /* we found a non-Root-Hub USB device */ 2455 type_refs[4] += 1; 2456 2457 /* "last_xfer_time" can be updated by a resume */ 2458 temp = ticks - udev->pwr_save.last_xfer_time; 2459 2460 /* 2461 * Compute minimum time since last transfer for the complete 2462 * bus: 2463 */ 2464 if (temp < mintime) 2465 mintime = temp; 2466 2467 if (udev->flags.self_suspended == 0) { 2468 type_refs[0] += udev->pwr_save.type_refs[0]; 2469 type_refs[1] += udev->pwr_save.type_refs[1]; 2470 type_refs[2] += udev->pwr_save.type_refs[2]; 2471 type_refs[3] += udev->pwr_save.type_refs[3]; 2472 } 2473 } 2474 2475 if (mintime >= (usb_ticks_t)(1 * hz)) { 2476 /* recompute power masks */ 2477 DPRINTF("Recomputing power masks\n"); 2478 bus->hw_power_state = 0; 2479 if (type_refs[UE_CONTROL] != 0) 2480 bus->hw_power_state |= USB_HW_POWER_CONTROL; 2481 if (type_refs[UE_BULK] != 0) 2482 bus->hw_power_state |= USB_HW_POWER_BULK; 2483 if (type_refs[UE_INTERRUPT] != 0) 2484 bus->hw_power_state |= USB_HW_POWER_INTERRUPT; 2485 if (type_refs[UE_ISOCHRONOUS] != 0) 2486 bus->hw_power_state |= USB_HW_POWER_ISOC; 2487 if (type_refs[4] != 0) 2488 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB; 2489 } 2490 USB_BUS_UNLOCK(bus); 2491 2492 if (bus->methods->set_hw_power != NULL) { 2493 /* always update hardware power! */ 2494 (bus->methods->set_hw_power) (bus); 2495 } 2496 return; 2497 } 2498 #endif 2499 2500 /*------------------------------------------------------------------------* 2501 * usb_dev_resume_peer 2502 * 2503 * This function will resume an USB peer and do the required USB 2504 * signalling to get an USB device out of the suspended state. 2505 *------------------------------------------------------------------------*/ 2506 static void 2507 usb_dev_resume_peer(struct usb_device *udev) 2508 { 2509 struct usb_bus *bus; 2510 int err; 2511 2512 /* be NULL safe */ 2513 if (udev == NULL) 2514 return; 2515 2516 /* check if already resumed */ 2517 if (udev->flags.self_suspended == 0) 2518 return; 2519 2520 /* we need a parent HUB to do resume */ 2521 if (udev->parent_hub == NULL) 2522 return; 2523 2524 DPRINTF("udev=%p\n", udev); 2525 2526 if ((udev->flags.usb_mode == USB_MODE_DEVICE) && 2527 (udev->flags.remote_wakeup == 0)) { 2528 /* 2529 * If the host did not set the remote wakeup feature, we can 2530 * not wake it up either! 2531 */ 2532 DPRINTF("remote wakeup is not set!\n"); 2533 return; 2534 } 2535 /* get bus pointer */ 2536 bus = udev->bus; 2537 2538 /* resume parent hub first */ 2539 usb_dev_resume_peer(udev->parent_hub); 2540 2541 /* reduce chance of instant resume failure by waiting a little bit */ 2542 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20)); 2543 2544 if (usb_device_20_compatible(udev)) { 2545 /* resume current port (Valid in Host and Device Mode) */ 2546 err = usbd_req_clear_port_feature(udev->parent_hub, 2547 NULL, udev->port_no, UHF_PORT_SUSPEND); 2548 if (err) { 2549 DPRINTFN(0, "Resuming port failed\n"); 2550 return; 2551 } 2552 } else { 2553 /* resume current port (Valid in Host and Device Mode) */ 2554 err = usbd_req_set_port_link_state(udev->parent_hub, 2555 NULL, udev->port_no, UPS_PORT_LS_U0); 2556 if (err) { 2557 DPRINTFN(0, "Resuming port failed\n"); 2558 return; 2559 } 2560 } 2561 2562 /* resume settle time */ 2563 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay)); 2564 2565 if (bus->methods->device_resume != NULL) { 2566 /* resume USB device on the USB controller */ 2567 (bus->methods->device_resume) (udev); 2568 } 2569 USB_BUS_LOCK(bus); 2570 /* set that this device is now resumed */ 2571 udev->flags.self_suspended = 0; 2572 #if USB_HAVE_POWERD 2573 /* make sure that we don't go into suspend right away */ 2574 udev->pwr_save.last_xfer_time = ticks; 2575 2576 /* make sure the needed power masks are on */ 2577 if (udev->pwr_save.type_refs[UE_CONTROL] != 0) 2578 bus->hw_power_state |= USB_HW_POWER_CONTROL; 2579 if (udev->pwr_save.type_refs[UE_BULK] != 0) 2580 bus->hw_power_state |= USB_HW_POWER_BULK; 2581 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0) 2582 bus->hw_power_state |= USB_HW_POWER_INTERRUPT; 2583 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) 2584 bus->hw_power_state |= USB_HW_POWER_ISOC; 2585 #endif 2586 USB_BUS_UNLOCK(bus); 2587 2588 if (bus->methods->set_hw_power != NULL) { 2589 /* always update hardware power! */ 2590 (bus->methods->set_hw_power) (bus); 2591 } 2592 2593 usbd_sr_lock(udev); 2594 2595 /* notify all sub-devices about resume */ 2596 err = usb_suspend_resume(udev, 0); 2597 2598 usbd_sr_unlock(udev); 2599 2600 /* check if peer has wakeup capability */ 2601 if (usb_peer_can_wakeup(udev)) { 2602 /* clear remote wakeup */ 2603 err = usbd_req_clear_device_feature(udev, 2604 NULL, UF_DEVICE_REMOTE_WAKEUP); 2605 if (err) { 2606 DPRINTFN(0, "Clearing device " 2607 "remote wakeup failed: %s\n", 2608 usbd_errstr(err)); 2609 } 2610 } 2611 } 2612 2613 /*------------------------------------------------------------------------* 2614 * usb_dev_suspend_peer 2615 * 2616 * This function will suspend an USB peer and do the required USB 2617 * signalling to get an USB device into the suspended state. 2618 *------------------------------------------------------------------------*/ 2619 static void 2620 usb_dev_suspend_peer(struct usb_device *udev) 2621 { 2622 struct usb_device *child; 2623 int err; 2624 uint8_t x; 2625 uint8_t nports; 2626 2627 repeat: 2628 /* be NULL safe */ 2629 if (udev == NULL) 2630 return; 2631 2632 /* check if already suspended */ 2633 if (udev->flags.self_suspended) 2634 return; 2635 2636 /* we need a parent HUB to do suspend */ 2637 if (udev->parent_hub == NULL) 2638 return; 2639 2640 DPRINTF("udev=%p\n", udev); 2641 2642 /* check if the current device is a HUB */ 2643 if (udev->hub != NULL) { 2644 nports = udev->hub->nports; 2645 2646 /* check if all devices on the HUB are suspended */ 2647 for (x = 0; x != nports; x++) { 2648 child = usb_bus_port_get_device(udev->bus, 2649 udev->hub->ports + x); 2650 2651 if (child == NULL) 2652 continue; 2653 2654 if (child->flags.self_suspended) 2655 continue; 2656 2657 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1); 2658 return; 2659 } 2660 } 2661 2662 if (usb_peer_can_wakeup(udev)) { 2663 /* 2664 * This request needs to be done before we set 2665 * "udev->flags.self_suspended": 2666 */ 2667 2668 /* allow device to do remote wakeup */ 2669 err = usbd_req_set_device_feature(udev, 2670 NULL, UF_DEVICE_REMOTE_WAKEUP); 2671 if (err) { 2672 DPRINTFN(0, "Setting device " 2673 "remote wakeup failed\n"); 2674 } 2675 } 2676 2677 USB_BUS_LOCK(udev->bus); 2678 /* 2679 * Checking for suspend condition and setting suspended bit 2680 * must be atomic! 2681 */ 2682 err = usb_peer_should_wakeup(udev); 2683 if (err == 0) { 2684 /* 2685 * Set that this device is suspended. This variable 2686 * must be set before calling USB controller suspend 2687 * callbacks. 2688 */ 2689 udev->flags.self_suspended = 1; 2690 } 2691 USB_BUS_UNLOCK(udev->bus); 2692 2693 if (err != 0) { 2694 if (usb_peer_can_wakeup(udev)) { 2695 /* allow device to do remote wakeup */ 2696 err = usbd_req_clear_device_feature(udev, 2697 NULL, UF_DEVICE_REMOTE_WAKEUP); 2698 if (err) { 2699 DPRINTFN(0, "Setting device " 2700 "remote wakeup failed\n"); 2701 } 2702 } 2703 2704 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2705 /* resume parent HUB first */ 2706 usb_dev_resume_peer(udev->parent_hub); 2707 2708 /* reduce chance of instant resume failure by waiting a little bit */ 2709 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20)); 2710 2711 /* resume current port (Valid in Host and Device Mode) */ 2712 err = usbd_req_clear_port_feature(udev->parent_hub, 2713 NULL, udev->port_no, UHF_PORT_SUSPEND); 2714 2715 /* resume settle time */ 2716 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay)); 2717 } 2718 DPRINTF("Suspend was cancelled!\n"); 2719 return; 2720 } 2721 2722 usbd_sr_lock(udev); 2723 2724 /* notify all sub-devices about suspend */ 2725 err = usb_suspend_resume(udev, 1); 2726 2727 usbd_sr_unlock(udev); 2728 2729 if (udev->bus->methods->device_suspend != NULL) { 2730 usb_timeout_t temp; 2731 2732 /* suspend device on the USB controller */ 2733 (udev->bus->methods->device_suspend) (udev); 2734 2735 /* do DMA delay */ 2736 temp = usbd_get_dma_delay(udev); 2737 if (temp != 0) 2738 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp)); 2739 2740 } 2741 2742 if (usb_device_20_compatible(udev)) { 2743 /* suspend current port */ 2744 err = usbd_req_set_port_feature(udev->parent_hub, 2745 NULL, udev->port_no, UHF_PORT_SUSPEND); 2746 if (err) { 2747 DPRINTFN(0, "Suspending port failed\n"); 2748 return; 2749 } 2750 } else { 2751 /* suspend current port */ 2752 err = usbd_req_set_port_link_state(udev->parent_hub, 2753 NULL, udev->port_no, UPS_PORT_LS_U3); 2754 if (err) { 2755 DPRINTFN(0, "Suspending port failed\n"); 2756 return; 2757 } 2758 } 2759 2760 udev = udev->parent_hub; 2761 goto repeat; 2762 } 2763 2764 /*------------------------------------------------------------------------* 2765 * usbd_set_power_mode 2766 * 2767 * This function will set the power mode, see USB_POWER_MODE_XXX for a 2768 * USB device. 2769 *------------------------------------------------------------------------*/ 2770 void 2771 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode) 2772 { 2773 /* filter input argument */ 2774 if ((power_mode != USB_POWER_MODE_ON) && 2775 (power_mode != USB_POWER_MODE_OFF)) 2776 power_mode = USB_POWER_MODE_SAVE; 2777 2778 power_mode = usbd_filter_power_mode(udev, power_mode); 2779 2780 udev->power_mode = power_mode; /* update copy of power mode */ 2781 2782 #if USB_HAVE_POWERD 2783 usb_bus_power_update(udev->bus); 2784 #else 2785 usb_needs_explore(udev->bus, 0 /* no probe */ ); 2786 #endif 2787 } 2788 2789 /*------------------------------------------------------------------------* 2790 * usbd_filter_power_mode 2791 * 2792 * This function filters the power mode based on hardware requirements. 2793 *------------------------------------------------------------------------*/ 2794 uint8_t 2795 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode) 2796 { 2797 const struct usb_bus_methods *mtod; 2798 int8_t temp; 2799 2800 mtod = udev->bus->methods; 2801 temp = -1; 2802 2803 if (mtod->get_power_mode != NULL) 2804 (mtod->get_power_mode) (udev, &temp); 2805 2806 /* check if we should not filter */ 2807 if (temp < 0) 2808 return (power_mode); 2809 2810 /* use fixed power mode given by hardware driver */ 2811 return (temp); 2812 } 2813 2814 /*------------------------------------------------------------------------* 2815 * usbd_start_re_enumerate 2816 * 2817 * This function starts re-enumeration of the given USB device. This 2818 * function does not need to be called BUS-locked. This function does 2819 * not wait until the re-enumeration is completed. 2820 *------------------------------------------------------------------------*/ 2821 void 2822 usbd_start_re_enumerate(struct usb_device *udev) 2823 { 2824 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) { 2825 udev->re_enumerate_wait = USB_RE_ENUM_START; 2826 usb_needs_explore(udev->bus, 0); 2827 } 2828 } 2829 2830 /*-----------------------------------------------------------------------* 2831 * usbd_start_set_config 2832 * 2833 * This function starts setting a USB configuration. This function 2834 * does not need to be called BUS-locked. This function does not wait 2835 * until the set USB configuratino is completed. 2836 *------------------------------------------------------------------------*/ 2837 usb_error_t 2838 usbd_start_set_config(struct usb_device *udev, uint8_t index) 2839 { 2840 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) { 2841 if (udev->curr_config_index == index) { 2842 /* no change needed */ 2843 return (0); 2844 } 2845 udev->next_config_index = index; 2846 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG; 2847 usb_needs_explore(udev->bus, 0); 2848 return (0); 2849 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) { 2850 if (udev->next_config_index == index) { 2851 /* no change needed */ 2852 return (0); 2853 } 2854 } 2855 return (USB_ERR_PENDING_REQUESTS); 2856 } 2857