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