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