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