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