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