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