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