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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR uhub_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_request.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_hub.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_controller.h>
70 #include <dev/usb/usb_bus.h>
72 #define UHUB_INTR_INTERVAL 250 /* ms */
73 #define UHUB_N_TRANSFER 1
76 static int uhub_debug = 0;
78 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
79 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
81 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
85 static int usb_power_timeout = 30; /* seconds */
87 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
88 &usb_power_timeout, 0, "USB power timeout");
91 struct uhub_current_state {
97 struct uhub_current_state sc_st;/* current state */
98 device_t sc_dev; /* base device */
99 struct mtx sc_mtx; /* our mutex */
100 struct usb_device *sc_udev; /* USB device */
101 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
103 #define UHUB_FLAG_DID_EXPLORE 0x01
107 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
108 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
109 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
110 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
111 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
113 /* prototypes for type checking: */
115 static device_probe_t uhub_probe;
116 static device_attach_t uhub_attach;
117 static device_detach_t uhub_detach;
118 static device_suspend_t uhub_suspend;
119 static device_resume_t uhub_resume;
121 static bus_driver_added_t uhub_driver_added;
122 static bus_child_location_str_t uhub_child_location_string;
123 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
125 static usb_callback_t uhub_intr_callback;
127 static void usb_dev_resume_peer(struct usb_device *udev);
128 static void usb_dev_suspend_peer(struct usb_device *udev);
129 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
131 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
134 .type = UE_INTERRUPT,
135 .endpoint = UE_ADDR_ANY,
136 .direction = UE_DIR_ANY,
138 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
139 .bufsize = 0, /* use wMaxPacketSize */
140 .callback = &uhub_intr_callback,
141 .interval = UHUB_INTR_INTERVAL,
146 * driver instance for "hub" connected to "usb"
147 * and "hub" connected to "hub"
149 static devclass_t uhub_devclass;
151 static device_method_t uhub_methods[] = {
152 DEVMETHOD(device_probe, uhub_probe),
153 DEVMETHOD(device_attach, uhub_attach),
154 DEVMETHOD(device_detach, uhub_detach),
156 DEVMETHOD(device_suspend, uhub_suspend),
157 DEVMETHOD(device_resume, uhub_resume),
159 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
160 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
161 DEVMETHOD(bus_driver_added, uhub_driver_added),
165 static driver_t uhub_driver = {
167 .methods = uhub_methods,
168 .size = sizeof(struct uhub_softc)
171 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
172 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
173 MODULE_VERSION(uhub, 1);
176 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
178 struct uhub_softc *sc = usbd_xfer_softc(xfer);
180 switch (USB_GET_STATE(xfer)) {
181 case USB_ST_TRANSFERRED:
184 * This is an indication that some port
185 * has changed status. Notify the bus
186 * event handler thread that we need
187 * to be explored again:
189 usb_needs_explore(sc->sc_udev->bus, 0);
192 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
193 usbd_transfer_submit(xfer);
197 if (xfer->error != USB_ERR_CANCELLED) {
199 * Do a clear-stall. The "stall_pipe" flag
200 * will get cleared before next callback by
203 usbd_xfer_set_stall(xfer);
204 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
205 usbd_transfer_submit(xfer);
211 /*------------------------------------------------------------------------*
212 * uhub_explore_sub - subroutine
216 * Else: A control transaction failed
217 *------------------------------------------------------------------------*/
219 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
222 struct usb_device *child;
226 bus = sc->sc_udev->bus;
229 /* get driver added refcount from USB bus */
230 refcount = bus->driver_added_refcount;
232 /* get device assosiated with the given port */
233 child = usb_bus_port_get_device(bus, up);
239 /* check if device should be re-enumerated */
241 if (child->flags.usb_mode == USB_MODE_HOST) {
244 do_unlock = usbd_enum_lock(child);
245 switch (child->re_enumerate_wait) {
246 case USB_RE_ENUM_START:
247 err = usbd_set_config_index(child,
250 DPRINTF("Unconfigure failed: "
254 err = usbd_req_re_enumerate(child, NULL);
256 err = usbd_set_config_index(child, 0);
258 err = usb_probe_and_attach(child,
259 USB_IFACE_INDEX_ANY);
261 child->re_enumerate_wait = USB_RE_ENUM_DONE;
265 case USB_RE_ENUM_PWR_OFF:
266 /* get the device unconfigured */
267 err = usbd_set_config_index(child,
270 DPRINTFN(0, "Could not unconfigure "
271 "device (ignored)\n");
274 /* clear port enable */
275 err = usbd_req_clear_port_feature(child->parent_hub,
276 NULL, child->port_no, UHF_PORT_ENABLE);
278 DPRINTFN(0, "Could not disable port "
281 child->re_enumerate_wait = USB_RE_ENUM_DONE;
286 child->re_enumerate_wait = USB_RE_ENUM_DONE;
290 usbd_enum_unlock(child);
293 /* check if probe and attach should be done */
295 if (child->driver_added_refcount != refcount) {
296 child->driver_added_refcount = refcount;
297 err = usb_probe_and_attach(child,
298 USB_IFACE_INDEX_ANY);
303 /* start control transfer, if device mode */
305 if (child->flags.usb_mode == USB_MODE_DEVICE)
306 usbd_ctrl_transfer_setup(child);
308 /* if a HUB becomes present, do a recursive HUB explore */
311 err = (child->hub->explore) (child);
317 /*------------------------------------------------------------------------*
318 * uhub_read_port_status - factored out code
319 *------------------------------------------------------------------------*/
321 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
323 struct usb_port_status ps;
326 err = usbd_req_get_port_status(
327 sc->sc_udev, NULL, &ps, portno);
329 /* update status regardless of error */
331 sc->sc_st.port_status = UGETW(ps.wPortStatus);
332 sc->sc_st.port_change = UGETW(ps.wPortChange);
334 /* debugging print */
336 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
337 "wPortChange=0x%04x, err=%s\n",
338 portno, sc->sc_st.port_status,
339 sc->sc_st.port_change, usbd_errstr(err));
343 /*------------------------------------------------------------------------*
348 * Else: A control transaction failed
349 *------------------------------------------------------------------------*/
351 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
353 struct usb_device *child;
354 struct usb_device *udev;
355 enum usb_dev_speed speed;
356 enum usb_hc_mode mode;
361 DPRINTF("reattaching port %d\n", portno);
366 child = usb_bus_port_get_device(udev->bus,
367 udev->hub->ports + portno - 1);
371 /* first clear the port connection change bit */
373 err = usbd_req_clear_port_feature(udev, NULL,
374 portno, UHF_C_PORT_CONNECTION);
379 /* check if there is a child */
383 * Free USB device and all subdevices, if any.
385 usb_free_device(child, 0);
388 /* get fresh status */
390 err = uhub_read_port_status(sc, portno);
394 /* check if nothing is connected to the port */
396 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
399 /* check if there is no power on the port and print a warning */
401 switch (udev->speed) {
405 power_mask = UPS_PORT_POWER;
407 case USB_SPEED_SUPER:
408 if (udev->parent_hub == NULL)
409 power_mask = UPS_PORT_POWER;
411 power_mask = UPS_PORT_POWER_SS;
417 if (!(sc->sc_st.port_status & power_mask)) {
418 DPRINTF("WARNING: strange, connected port %d "
419 "has no power\n", portno);
422 /* check if the device is in Host Mode */
424 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
426 DPRINTF("Port %d is in Host Mode\n", portno);
428 if (sc->sc_st.port_status & UPS_SUSPEND) {
430 * NOTE: Should not get here in SuperSpeed
431 * mode, because the HUB should report this
434 DPRINTF("Port %d was still "
435 "suspended, clearing.\n", portno);
436 err = usbd_req_clear_port_feature(udev,
437 NULL, portno, UHF_PORT_SUSPEND);
442 /* wait for maximum device power up time */
445 USB_MS_TO_TICKS(usb_port_powerup_delay));
447 /* reset port, which implies enabling it */
449 err = usbd_req_reset_port(udev, NULL, portno);
452 DPRINTFN(0, "port %d reset "
453 "failed, error=%s\n",
454 portno, usbd_errstr(err));
457 /* get port status again, it might have changed during reset */
459 err = uhub_read_port_status(sc, portno);
463 /* check if something changed during port reset */
465 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
466 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
468 DPRINTFN(0, "giving up port reset "
469 "- device vanished\n");
476 DPRINTF("Port %d is in Device Mode\n", portno);
480 * Figure out the device speed
482 switch (udev->speed) {
484 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
485 speed = USB_SPEED_HIGH;
486 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
487 speed = USB_SPEED_LOW;
489 speed = USB_SPEED_FULL;
492 if (sc->sc_st.port_status & UPS_LOW_SPEED)
493 speed = USB_SPEED_LOW;
495 speed = USB_SPEED_FULL;
498 speed = USB_SPEED_LOW;
500 case USB_SPEED_SUPER:
501 if (udev->parent_hub == NULL) {
502 /* Root HUB - special case */
503 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
505 speed = USB_SPEED_FULL;
508 speed = USB_SPEED_LOW;
511 speed = USB_SPEED_HIGH;
514 speed = USB_SPEED_SUPER;
518 speed = USB_SPEED_SUPER;
522 /* same speed like parent */
526 if (speed == USB_SPEED_SUPER) {
527 err = usbd_req_set_hub_u1_timeout(udev, NULL,
528 portno, 128 - (2 * udev->depth));
530 DPRINTFN(0, "port %d U1 timeout "
531 "failed, error=%s\n",
532 portno, usbd_errstr(err));
534 err = usbd_req_set_hub_u2_timeout(udev, NULL,
535 portno, 128 - (2 * udev->depth));
537 DPRINTFN(0, "port %d U2 timeout "
538 "failed, error=%s\n",
539 portno, usbd_errstr(err));
544 * Figure out the device mode
546 * NOTE: This part is currently FreeBSD specific.
548 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
549 mode = USB_MODE_DEVICE;
551 mode = USB_MODE_HOST;
553 /* need to create a new child */
554 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
555 udev->depth + 1, portno - 1, portno, speed, mode);
557 DPRINTFN(0, "could not allocate new device\n");
560 return (0); /* success */
565 * Free USB device and all subdevices, if any.
567 usb_free_device(child, 0);
571 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
572 err = usbd_req_clear_port_feature(
574 portno, UHF_PORT_ENABLE);
578 DPRINTFN(0, "device problem (%s), "
579 "disabling port %d\n", usbd_errstr(err), portno);
584 /*------------------------------------------------------------------------*
585 * usb_device_20_compatible
588 * 0: HUB does not support suspend and resume
589 * Else: HUB supports suspend and resume
590 *------------------------------------------------------------------------*/
592 usb_device_20_compatible(struct usb_device *udev)
596 switch (udev->speed) {
606 /*------------------------------------------------------------------------*
607 * uhub_suspend_resume_port
611 * Else: A control transaction failed
612 *------------------------------------------------------------------------*/
614 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
616 struct usb_device *child;
617 struct usb_device *udev;
621 DPRINTF("port %d\n", portno);
624 child = usb_bus_port_get_device(udev->bus,
625 udev->hub->ports + portno - 1);
627 /* first clear the port suspend change bit */
629 if (usb_device_20_compatible(udev)) {
630 err = usbd_req_clear_port_feature(udev, NULL,
631 portno, UHF_C_PORT_SUSPEND);
633 err = usbd_req_clear_port_feature(udev, NULL,
634 portno, UHF_C_PORT_LINK_STATE);
638 DPRINTF("clearing suspend failed.\n");
641 /* get fresh status */
643 err = uhub_read_port_status(sc, portno);
645 DPRINTF("reading port status failed.\n");
648 /* convert current state */
650 if (usb_device_20_compatible(udev)) {
651 if (sc->sc_st.port_status & UPS_SUSPEND) {
657 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
661 case UPS_PORT_LS_SS_INA:
662 usbd_req_warm_reset_port(udev, NULL, portno);
671 DPRINTF("suspended=%u\n", is_suspend);
673 /* do the suspend or resume */
677 * This code handle two cases: 1) Host Mode - we can only
678 * receive resume here 2) Device Mode - we can receive
679 * suspend and resume here
682 usb_dev_resume_peer(child);
683 else if (child->flags.usb_mode == USB_MODE_DEVICE)
684 usb_dev_suspend_peer(child);
690 /*------------------------------------------------------------------------*
691 * uhub_root_interrupt
693 * This function is called when a Root HUB interrupt has
694 * happened. "ptr" and "len" makes up the Root HUB interrupt
695 * packet. This function is called having the "bus_mtx" locked.
696 *------------------------------------------------------------------------*/
698 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
700 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
702 usb_needs_explore(bus, 0);
706 uhub_is_too_deep(struct usb_device *udev)
708 switch (udev->speed) {
712 if (udev->depth > USB_HUB_MAX_DEPTH)
715 case USB_SPEED_SUPER:
716 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
725 /*------------------------------------------------------------------------*
731 *------------------------------------------------------------------------*/
733 uhub_explore(struct usb_device *udev)
736 struct uhub_softc *sc;
746 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
748 /* ignore devices that are too deep */
749 if (uhub_is_too_deep(udev))
750 return (USB_ERR_TOO_DEEP);
752 /* check if device is suspended */
753 if (udev->flags.self_suspended) {
754 /* need to wait until the child signals resume */
755 DPRINTF("Device is suspended!\n");
760 * Make sure we don't race against user-space applications
763 do_unlock = usbd_enum_lock(udev);
765 for (x = 0; x != hub->nports; x++) {
769 err = uhub_read_port_status(sc, portno);
771 /* most likely the HUB is gone */
774 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
775 DPRINTF("Overcurrent on port %u.\n", portno);
776 err = usbd_req_clear_port_feature(
777 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
779 /* most likely the HUB is gone */
783 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
785 * Fake a connect status change so that the
786 * status gets checked initially!
788 sc->sc_st.port_change |=
789 UPS_C_CONNECT_STATUS;
791 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
792 err = usbd_req_clear_port_feature(
793 udev, NULL, portno, UHF_C_PORT_ENABLE);
795 /* most likely the HUB is gone */
798 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
800 * Ignore the port error if the device
803 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
804 DPRINTFN(0, "illegal enable change, "
805 "port %d\n", portno);
808 if (up->restartcnt == USB_RESTART_MAX) {
809 /* XXX could try another speed ? */
810 DPRINTFN(0, "port error, giving up "
811 "port %d\n", portno);
813 sc->sc_st.port_change |=
814 UPS_C_CONNECT_STATUS;
819 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
820 err = uhub_reattach_port(sc, portno);
822 /* most likely the HUB is gone */
826 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
827 UPS_C_PORT_LINK_STATE)) {
828 err = uhub_suspend_resume_port(sc, portno);
830 /* most likely the HUB is gone */
834 err = uhub_explore_sub(sc, up);
836 /* no device(s) present */
839 /* explore succeeded - reset restart counter */
844 usbd_enum_unlock(udev);
846 /* initial status checked */
847 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
850 return (USB_ERR_NORMAL_COMPLETION);
854 uhub_probe(device_t dev)
856 struct usb_attach_arg *uaa = device_get_ivars(dev);
858 if (uaa->usb_mode != USB_MODE_HOST)
862 * The subclass for USB HUBs is currently ignored because it
863 * is 0 for some and 1 for others.
865 if (uaa->info.bConfigIndex == 0 &&
866 uaa->info.bDeviceClass == UDCLASS_HUB)
872 /* NOTE: The information returned by this function can be wrong. */
874 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
876 struct usb_hub_descriptor hubdesc20;
877 struct usb_hub_ss_descriptor hubdesc30;
882 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
883 return (USB_ERR_INVAL);
888 switch (udev->speed) {
892 /* assuming that there is one port */
893 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
895 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
896 "error=%s\n", usbd_errstr(err));
899 nports = hubdesc20.bNbrPorts;
903 if (udev->speed == USB_SPEED_HIGH)
904 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
907 case USB_SPEED_SUPER:
908 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
910 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
911 "error=%s\n", usbd_errstr(err));
914 nports = hubdesc30.bNbrPorts;
934 uhub_attach(device_t dev)
936 struct uhub_softc *sc = device_get_softc(dev);
937 struct usb_attach_arg *uaa = device_get_ivars(dev);
938 struct usb_device *udev = uaa->device;
939 struct usb_device *parent_hub = udev->parent_hub;
941 struct usb_hub_descriptor hubdesc20;
942 struct usb_hub_ss_descriptor hubdesc30;
954 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
956 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
957 device_get_nameunit(dev));
959 device_set_usb_desc(dev);
961 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
962 "parent->selfpowered=%d\n",
964 udev->flags.self_powered,
967 parent_hub->flags.self_powered : 0);
969 if (uhub_is_too_deep(udev)) {
970 DPRINTFN(0, "HUB at depth %d, "
971 "exceeds maximum. HUB ignored\n", (int)udev->depth);
975 if (!udev->flags.self_powered && parent_hub &&
976 !parent_hub->flags.self_powered) {
977 DPRINTFN(0, "Bus powered HUB connected to "
978 "bus powered HUB. HUB ignored\n");
982 if (UHUB_IS_MULTI_TT(sc)) {
983 err = usbd_set_alt_interface_index(udev, 0, 1);
985 device_printf(dev, "MTT could not be enabled\n");
988 device_printf(dev, "MTT enabled\n");
991 /* get HUB descriptor */
993 DPRINTFN(2, "Getting HUB descriptor\n");
995 switch (udev->speed) {
999 /* assuming that there is one port */
1000 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1002 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1003 "error=%s\n", usbd_errstr(err));
1006 /* get number of ports */
1007 nports = hubdesc20.bNbrPorts;
1009 /* get power delay */
1010 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1011 usb_extra_power_up_time);
1013 /* get complete HUB descriptor */
1015 /* check number of ports */
1017 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1018 "error=%s\n", usbd_errstr(err));
1021 /* get complete HUB descriptor */
1022 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1025 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1026 "error=%s\n", usbd_errstr(err));
1029 if (hubdesc20.bNbrPorts != nports) {
1030 DPRINTFN(0, "Number of ports changed\n");
1035 case USB_SPEED_SUPER:
1036 if (udev->parent_hub != NULL) {
1037 err = usbd_req_set_hub_depth(udev, NULL,
1040 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1041 "error=%s\n", usbd_errstr(err));
1045 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1047 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1048 "error=%s\n", usbd_errstr(err));
1051 /* get number of ports */
1052 nports = hubdesc30.bNbrPorts;
1054 /* get power delay */
1055 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1056 usb_extra_power_up_time);
1058 /* get complete HUB descriptor */
1060 /* check number of ports */
1061 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1062 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1063 "error=%s\n", usbd_errstr(err));
1066 /* get complete HUB descriptor */
1067 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1070 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1071 "error=%s\n", usbd_errstr(err));
1074 if (hubdesc30.bNbrPorts != nports) {
1075 DPRINTFN(0, "Number of ports changed\n");
1081 DPRINTF("Assuming HUB has only one port\n");
1082 /* default number of ports */
1084 /* default power delay */
1085 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1089 DPRINTFN(0, "portless HUB\n");
1092 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1093 M_USBDEV, M_WAITOK | M_ZERO);
1100 /* initialize HUB structure */
1102 hub->explore = &uhub_explore;
1103 hub->nports = nports;
1104 hub->hubudev = udev;
1106 /* if self powered hub, give ports maximum current */
1107 if (udev->flags.self_powered) {
1108 hub->portpower = USB_MAX_POWER;
1110 hub->portpower = USB_MIN_POWER;
1113 /* set up interrupt pipe */
1115 if (udev->parent_hub == NULL) {
1116 /* root HUB is special */
1120 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1121 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1124 DPRINTFN(0, "cannot setup interrupt transfer, "
1125 "errstr=%s\n", usbd_errstr(err));
1128 /* wait with power off for a while */
1129 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1132 * To have the best chance of success we do things in the exact same
1133 * order as Windoze98. This should not be necessary, but some
1134 * devices do not follow the USB specs to the letter.
1136 * These are the events on the bus when a hub is attached:
1137 * Get device and config descriptors (see attach code)
1138 * Get hub descriptor (see above)
1141 * wait for power to become stable
1142 * (all below happens in explore code)
1144 * clear C_PORT_CONNECTION
1147 * if device connected
1151 * clear C_PORT_RESET
1153 * proceed with device attachment
1156 /* XXX should check for none, individual, or ganged power? */
1160 for (x = 0; x != nports; x++) {
1161 /* set up data structures */
1162 struct usb_port *up = hub->ports + x;
1164 up->device_index = 0;
1168 /* check if port is removable */
1169 switch (udev->speed) {
1171 case USB_SPEED_FULL:
1172 case USB_SPEED_HIGH:
1173 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1176 case USB_SPEED_SUPER:
1177 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1181 DPRINTF("Assuming removable port\n");
1186 /* turn the power on */
1187 err = usbd_req_set_port_feature(udev, NULL,
1188 portno, UHF_PORT_POWER);
1191 DPRINTFN(0, "port %d power on failed, %s\n",
1192 portno, usbd_errstr(err));
1194 DPRINTF("turn on port %d power\n",
1197 /* wait for stable power */
1198 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1201 device_printf(dev, "%d port%s with %d "
1202 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1203 removable, udev->flags.self_powered ? "self" : "bus");
1205 /* Start the interrupt endpoint, if any */
1207 if (sc->sc_xfer[0] != NULL) {
1208 mtx_lock(&sc->sc_mtx);
1209 usbd_transfer_start(sc->sc_xfer[0]);
1210 mtx_unlock(&sc->sc_mtx);
1213 /* Enable automatic power save on all USB HUBs */
1215 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1220 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1223 free(udev->hub, M_USBDEV);
1227 mtx_destroy(&sc->sc_mtx);
1233 * Called from process context when the hub is gone.
1234 * Detach all devices on active ports.
1237 uhub_detach(device_t dev)
1239 struct uhub_softc *sc = device_get_softc(dev);
1240 struct usb_hub *hub = sc->sc_udev->hub;
1241 struct usb_device *child;
1244 if (hub == NULL) /* must be partially working */
1247 /* Make sure interrupt transfer is gone. */
1248 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1250 /* Detach all ports */
1251 for (x = 0; x != hub->nports; x++) {
1253 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1255 if (child == NULL) {
1260 * Free USB device and all subdevices, if any.
1262 usb_free_device(child, 0);
1265 free(hub, M_USBDEV);
1266 sc->sc_udev->hub = NULL;
1268 mtx_destroy(&sc->sc_mtx);
1274 uhub_suspend(device_t dev)
1277 /* Sub-devices are not suspended here! */
1282 uhub_resume(device_t dev)
1285 /* Sub-devices are not resumed here! */
1290 uhub_driver_added(device_t dev, driver_t *driver)
1292 usb_needs_explore_all();
1296 struct usb_device *udev;
1298 uint8_t iface_index;
1302 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1303 struct hub_result *res)
1305 struct usb_interface *iface;
1306 struct usb_device *udev;
1311 nports = hub->nports;
1312 for (x = 0; x != nports; x++) {
1313 udev = usb_bus_port_get_device(hub->hubudev->bus,
1318 for (i = 0; i != USB_IFACE_MAX; i++) {
1319 iface = usbd_get_iface(udev, i);
1321 (iface->subdev == child)) {
1322 res->iface_index = i;
1324 res->portno = x + 1;
1329 res->iface_index = 0;
1335 uhub_child_location_string(device_t parent, device_t child,
1336 char *buf, size_t buflen)
1338 struct uhub_softc *sc;
1339 struct usb_hub *hub;
1340 struct hub_result res;
1342 if (!device_is_attached(parent)) {
1348 sc = device_get_softc(parent);
1349 hub = sc->sc_udev->hub;
1352 uhub_find_iface_index(hub, child, &res);
1354 DPRINTF("device not on hub\n");
1360 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1361 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1362 res.portno, device_get_unit(res.udev->bus->bdev),
1363 res.udev->device_index, res.iface_index);
1371 uhub_child_pnpinfo_string(device_t parent, device_t child,
1372 char *buf, size_t buflen)
1374 struct uhub_softc *sc;
1375 struct usb_hub *hub;
1376 struct usb_interface *iface;
1377 struct hub_result res;
1379 if (!device_is_attached(parent)) {
1385 sc = device_get_softc(parent);
1386 hub = sc->sc_udev->hub;
1389 uhub_find_iface_index(hub, child, &res);
1391 DPRINTF("device not on hub\n");
1397 iface = usbd_get_iface(res.udev, res.iface_index);
1398 if (iface && iface->idesc) {
1399 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1400 "devclass=0x%02x devsubclass=0x%02x "
1404 "intclass=0x%02x intsubclass=0x%02x "
1405 "intprotocol=0x%02x " "%s%s",
1406 UGETW(res.udev->ddesc.idVendor),
1407 UGETW(res.udev->ddesc.idProduct),
1408 res.udev->ddesc.bDeviceClass,
1409 res.udev->ddesc.bDeviceSubClass,
1410 usb_get_serial(res.udev),
1411 UGETW(res.udev->ddesc.bcdDevice),
1412 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1413 iface->idesc->bInterfaceClass,
1414 iface->idesc->bInterfaceSubClass,
1415 iface->idesc->bInterfaceProtocol,
1416 iface->pnpinfo ? " " : "",
1417 iface->pnpinfo ? iface->pnpinfo : "");
1431 * The USB Transaction Translator:
1432 * ===============================
1434 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1435 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1436 * USB transfers. To utilize bandwidth dynamically the "scatter and
1437 * gather" principle must be applied. This means that bandwidth must
1438 * be divided into equal parts of bandwidth. With regard to USB all
1439 * data is transferred in smaller packets with length
1440 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1443 * The bandwidth scheduler which I have implemented will simply pack
1444 * the USB transfers back to back until there is no more space in the
1445 * schedule. Out of the 8 microframes which the USB 2.0 standard
1446 * provides, only 6 are available for non-HIGH-speed devices. I have
1447 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1448 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1449 * this division, it is very difficult to allocate and free bandwidth
1452 * NOTE about the Transaction Translator in USB HUBs:
1454 * USB HUBs have a very simple Transaction Translator, that will
1455 * simply pipeline all the SPLIT transactions. That means that the
1456 * transactions will be executed in the order they are queued!
1460 /*------------------------------------------------------------------------*
1461 * usb_intr_find_best_slot
1464 * The best Transaction Translation slot for an interrupt endpoint.
1465 *------------------------------------------------------------------------*/
1467 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1468 uint8_t end, uint8_t mask)
1470 usb_size_t min = (usb_size_t)-1;
1478 /* find the last slot with lesser used bandwidth */
1480 for (x = start; x < end; x++) {
1484 /* compute sum of bandwidth */
1485 for (z = x; z < end; z++) {
1486 if (mask & (1U << (z - x)))
1490 /* check if the current multi-slot is more optimal */
1496 /* check if the mask is about to be shifted out */
1497 if (mask & (1U << (end - 1 - x)))
1503 /*------------------------------------------------------------------------*
1504 * usb_hs_bandwidth_adjust
1506 * This function will update the bandwith usage for the microframe
1507 * having index "slot" by "len" bytes. "len" can be negative. If the
1508 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1509 * the "slot" argument will be replaced by the slot having least used
1510 * bandwidth. The "mask" argument is used for multi-slot allocations.
1513 * The slot in which the bandwidth update was done: 0..7
1514 *------------------------------------------------------------------------*/
1516 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1517 uint8_t slot, uint8_t mask)
1519 struct usb_bus *bus = udev->bus;
1520 struct usb_hub *hub;
1521 enum usb_dev_speed speed;
1524 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1526 speed = usbd_get_speed(udev);
1530 case USB_SPEED_FULL:
1531 if (speed == USB_SPEED_LOW) {
1535 * The Host Controller Driver should have
1536 * performed checks so that the lookup
1537 * below does not result in a NULL pointer
1541 hub = udev->parent_hs_hub->hub;
1542 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1543 slot = usb_intr_find_best_slot(hub->uframe_usage,
1544 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1546 for (x = slot; x < 8; x++) {
1547 if (mask & (1U << (x - slot))) {
1548 hub->uframe_usage[x] += len;
1549 bus->uframe_usage[x] += len;
1554 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1555 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1556 USB_HS_MICRO_FRAMES_MAX, mask);
1558 for (x = slot; x < 8; x++) {
1559 if (mask & (1U << (x - slot))) {
1560 bus->uframe_usage[x] += len;
1568 /*------------------------------------------------------------------------*
1569 * usb_hs_bandwidth_alloc
1571 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1572 *------------------------------------------------------------------------*/
1574 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1576 struct usb_device *udev;
1581 udev = xfer->xroot->udev;
1583 if (udev->flags.usb_mode != USB_MODE_HOST)
1584 return; /* not supported */
1586 xfer->endpoint->refcount_bw++;
1587 if (xfer->endpoint->refcount_bw != 1)
1588 return; /* already allocated */
1590 speed = usbd_get_speed(udev);
1592 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1594 /* allocate a microframe slot */
1597 slot = usb_hs_bandwidth_adjust(udev,
1598 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1600 xfer->endpoint->usb_uframe = slot;
1601 xfer->endpoint->usb_smask = mask << slot;
1603 if ((speed != USB_SPEED_FULL) &&
1604 (speed != USB_SPEED_LOW)) {
1605 xfer->endpoint->usb_cmask = 0x00 ;
1607 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1611 case UE_ISOCHRONOUS:
1612 switch (usbd_xfer_get_fps_shift(xfer)) {
1627 /* allocate a microframe multi-slot */
1629 slot = usb_hs_bandwidth_adjust(udev,
1630 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1632 xfer->endpoint->usb_uframe = slot;
1633 xfer->endpoint->usb_cmask = 0;
1634 xfer->endpoint->usb_smask = mask << slot;
1638 xfer->endpoint->usb_uframe = 0;
1639 xfer->endpoint->usb_cmask = 0;
1640 xfer->endpoint->usb_smask = 0;
1644 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1645 xfer->endpoint->usb_uframe,
1646 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1649 /*------------------------------------------------------------------------*
1650 * usb_hs_bandwidth_free
1652 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1653 *------------------------------------------------------------------------*/
1655 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1657 struct usb_device *udev;
1661 udev = xfer->xroot->udev;
1663 if (udev->flags.usb_mode != USB_MODE_HOST)
1664 return; /* not supported */
1666 xfer->endpoint->refcount_bw--;
1667 if (xfer->endpoint->refcount_bw != 0)
1668 return; /* still allocated */
1670 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1672 case UE_ISOCHRONOUS:
1674 slot = xfer->endpoint->usb_uframe;
1675 mask = xfer->endpoint->usb_smask;
1677 /* free microframe slot(s): */
1678 usb_hs_bandwidth_adjust(udev,
1679 -xfer->max_frame_size, slot, mask >> slot);
1681 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1682 slot, mask >> slot);
1684 xfer->endpoint->usb_uframe = 0;
1685 xfer->endpoint->usb_cmask = 0;
1686 xfer->endpoint->usb_smask = 0;
1694 /*------------------------------------------------------------------------*
1695 * usb_isoc_time_expand
1697 * This function will expand the time counter from 7-bit to 16-bit.
1700 * 16-bit isochronous time counter.
1701 *------------------------------------------------------------------------*/
1703 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1707 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1709 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1711 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1713 if (isoc_time_curr < rem) {
1714 /* the time counter wrapped around */
1715 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1717 /* update the remainder */
1719 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1720 bus->isoc_time_last |= isoc_time_curr;
1722 return (bus->isoc_time_last);
1725 /*------------------------------------------------------------------------*
1726 * usbd_fs_isoc_schedule_alloc_slot
1728 * This function will allocate bandwidth for an isochronous FULL speed
1729 * transaction in the FULL speed schedule.
1734 *------------------------------------------------------------------------*/
1735 #if USB_HAVE_TT_SUPPORT
1737 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1739 struct usb_xfer *xfer;
1740 struct usb_xfer *pipe_xfer;
1741 struct usb_bus *bus;
1743 usb_frlength_t data_len;
1751 bus = isoc_xfer->xroot->bus;
1753 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1755 /* skip self, if any */
1757 if (xfer == isoc_xfer)
1760 /* check if this USB transfer is going through the same TT */
1762 if (xfer->xroot->udev->parent_hs_hub !=
1763 isoc_xfer->xroot->udev->parent_hs_hub) {
1766 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1767 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1768 (xfer->xroot->udev->hs_port_no !=
1769 isoc_xfer->xroot->udev->hs_port_no)) {
1772 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1775 /* check if isoc_time is part of this transfer */
1777 delta = xfer->isoc_time_complete - isoc_time;
1778 if (delta > 0 && delta <= xfer->nframes) {
1779 delta = xfer->nframes - delta;
1781 len = xfer->frlengths[delta];
1789 /* check double buffered transfers */
1791 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
1794 /* skip self, if any */
1796 if (pipe_xfer == isoc_xfer)
1799 /* check if isoc_time is part of this transfer */
1801 delta = pipe_xfer->isoc_time_complete - isoc_time;
1802 if (delta > 0 && delta <= pipe_xfer->nframes) {
1803 delta = pipe_xfer->nframes - delta;
1805 len = pipe_xfer->frlengths[delta];
1815 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1816 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1820 /* check for overflow */
1822 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1827 delta = isoc_xfer->isoc_time_complete - isoc_time;
1828 if (delta > 0 && delta <= isoc_xfer->nframes) {
1829 delta = isoc_xfer->nframes - delta;
1831 len = isoc_xfer->frlengths[delta];
1839 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1840 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1844 /* check for overflow */
1846 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1853 /*------------------------------------------------------------------------*
1854 * usb_bus_port_get_device
1856 * This function is NULL safe.
1857 *------------------------------------------------------------------------*/
1859 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1861 if ((bus == NULL) || (up == NULL)) {
1865 if (up->device_index == 0) {
1869 return (bus->devices[up->device_index]);
1872 /*------------------------------------------------------------------------*
1873 * usb_bus_port_set_device
1875 * This function is NULL safe.
1876 *------------------------------------------------------------------------*/
1878 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1879 struct usb_device *udev, uint8_t device_index)
1886 * There is only one case where we don't
1887 * have an USB port, and that is the Root Hub!
1891 up->device_index = device_index;
1893 device_index = up->device_index;
1894 up->device_index = 0;
1898 * Make relationships to our new device
1900 if (device_index != 0) {
1902 mtx_lock(&usb_ref_lock);
1904 bus->devices[device_index] = udev;
1906 mtx_unlock(&usb_ref_lock);
1912 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1915 /*------------------------------------------------------------------------*
1918 * This functions is called when the USB event thread needs to run.
1919 *------------------------------------------------------------------------*/
1921 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1928 DPRINTF("No bus pointer!\n");
1931 if ((bus->devices == NULL) ||
1932 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1933 DPRINTF("No root HUB\n");
1936 if (mtx_owned(&bus->bus_mtx)) {
1945 if (usb_proc_msignal(&bus->explore_proc,
1946 &bus->explore_msg[0], &bus->explore_msg[1])) {
1950 USB_BUS_UNLOCK(bus);
1954 /*------------------------------------------------------------------------*
1955 * usb_needs_explore_all
1957 * This function is called whenever a new driver is loaded and will
1958 * cause that all USB busses are re-explored.
1959 *------------------------------------------------------------------------*/
1961 usb_needs_explore_all(void)
1963 struct usb_bus *bus;
1970 dc = usb_devclass_ptr;
1972 DPRINTFN(0, "no devclass\n");
1976 * Explore all USB busses in parallell.
1978 max = devclass_get_maxunit(dc);
1980 dev = devclass_get_device(dc, max);
1982 bus = device_get_softc(dev);
1984 usb_needs_explore(bus, 1);
1991 /*------------------------------------------------------------------------*
1992 * usb_bus_power_update
1994 * This function will ensure that all USB devices on the given bus are
1995 * properly suspended or resumed according to the device transfer
1997 *------------------------------------------------------------------------*/
2000 usb_bus_power_update(struct usb_bus *bus)
2002 usb_needs_explore(bus, 0 /* no probe */ );
2006 /*------------------------------------------------------------------------*
2007 * usbd_transfer_power_ref
2009 * This function will modify the power save reference counts and
2010 * wakeup the USB device associated with the given USB transfer, if
2012 *------------------------------------------------------------------------*/
2015 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2017 static const usb_power_mask_t power_mask[4] = {
2018 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2019 [UE_BULK] = USB_HW_POWER_BULK,
2020 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2021 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2023 struct usb_device *udev;
2024 uint8_t needs_explore;
2025 uint8_t needs_hw_power;
2028 udev = xfer->xroot->udev;
2030 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2031 /* no power save for root HUB */
2034 USB_BUS_LOCK(udev->bus);
2036 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2038 udev->pwr_save.last_xfer_time = ticks;
2039 udev->pwr_save.type_refs[xfer_type] += val;
2041 if (xfer->flags_int.control_xfr) {
2042 udev->pwr_save.read_refs += val;
2043 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2045 * It is not allowed to suspend during a
2048 udev->pwr_save.write_refs += val;
2050 } else if (USB_GET_DATA_ISREAD(xfer)) {
2051 udev->pwr_save.read_refs += val;
2053 udev->pwr_save.write_refs += val;
2057 if (udev->flags.self_suspended)
2058 needs_explore = usb_peer_should_wakeup(udev);
2062 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2063 DPRINTF("Adding type %u to power state\n", xfer_type);
2064 udev->bus->hw_power_state |= power_mask[xfer_type];
2074 USB_BUS_UNLOCK(udev->bus);
2076 if (needs_explore) {
2077 DPRINTF("update\n");
2078 usb_bus_power_update(udev->bus);
2079 } else if (needs_hw_power) {
2080 DPRINTF("needs power\n");
2081 if (udev->bus->methods->set_hw_power != NULL) {
2082 (udev->bus->methods->set_hw_power) (udev->bus);
2088 /*------------------------------------------------------------------------*
2089 * usb_peer_should_wakeup
2091 * This function returns non-zero if the current device should wake up.
2092 *------------------------------------------------------------------------*/
2094 usb_peer_should_wakeup(struct usb_device *udev)
2096 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2097 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2098 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2099 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2100 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2101 (udev->pwr_save.write_refs != 0) ||
2102 ((udev->pwr_save.read_refs != 0) &&
2103 (udev->flags.usb_mode == USB_MODE_HOST) &&
2104 (usb_peer_can_wakeup(udev) == 0)));
2107 /*------------------------------------------------------------------------*
2110 * This function implements the USB power daemon and is called
2111 * regularly from the USB explore thread.
2112 *------------------------------------------------------------------------*/
2115 usb_bus_powerd(struct usb_bus *bus)
2117 struct usb_device *udev;
2120 usb_ticks_t mintime;
2121 usb_size_t type_refs[5];
2124 limit = usb_power_timeout;
2127 else if (limit > 255)
2132 DPRINTF("bus=%p\n", bus);
2137 * The root HUB device is never suspended
2138 * and we simply skip it.
2140 for (x = USB_ROOT_HUB_ADDR + 1;
2141 x != bus->devices_max; x++) {
2143 udev = bus->devices[x];
2147 temp = ticks - udev->pwr_save.last_xfer_time;
2149 if (usb_peer_should_wakeup(udev)) {
2150 /* check if we are suspended */
2151 if (udev->flags.self_suspended != 0) {
2152 USB_BUS_UNLOCK(bus);
2153 usb_dev_resume_peer(udev);
2156 } else if ((temp >= limit) &&
2157 (udev->flags.usb_mode == USB_MODE_HOST) &&
2158 (udev->flags.self_suspended == 0)) {
2159 /* try to do suspend */
2161 USB_BUS_UNLOCK(bus);
2162 usb_dev_suspend_peer(udev);
2167 /* reset counters */
2169 mintime = (usb_ticks_t)-1;
2176 /* Re-loop all the devices to get the actual state */
2178 for (x = USB_ROOT_HUB_ADDR + 1;
2179 x != bus->devices_max; x++) {
2181 udev = bus->devices[x];
2185 /* we found a non-Root-Hub USB device */
2188 /* "last_xfer_time" can be updated by a resume */
2189 temp = ticks - udev->pwr_save.last_xfer_time;
2192 * Compute minimum time since last transfer for the complete
2198 if (udev->flags.self_suspended == 0) {
2199 type_refs[0] += udev->pwr_save.type_refs[0];
2200 type_refs[1] += udev->pwr_save.type_refs[1];
2201 type_refs[2] += udev->pwr_save.type_refs[2];
2202 type_refs[3] += udev->pwr_save.type_refs[3];
2206 if (mintime >= (usb_ticks_t)(1 * hz)) {
2207 /* recompute power masks */
2208 DPRINTF("Recomputing power masks\n");
2209 bus->hw_power_state = 0;
2210 if (type_refs[UE_CONTROL] != 0)
2211 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2212 if (type_refs[UE_BULK] != 0)
2213 bus->hw_power_state |= USB_HW_POWER_BULK;
2214 if (type_refs[UE_INTERRUPT] != 0)
2215 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2216 if (type_refs[UE_ISOCHRONOUS] != 0)
2217 bus->hw_power_state |= USB_HW_POWER_ISOC;
2218 if (type_refs[4] != 0)
2219 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2221 USB_BUS_UNLOCK(bus);
2223 if (bus->methods->set_hw_power != NULL) {
2224 /* always update hardware power! */
2225 (bus->methods->set_hw_power) (bus);
2231 /*------------------------------------------------------------------------*
2232 * usb_dev_resume_peer
2234 * This function will resume an USB peer and do the required USB
2235 * signalling to get an USB device out of the suspended state.
2236 *------------------------------------------------------------------------*/
2238 usb_dev_resume_peer(struct usb_device *udev)
2240 struct usb_bus *bus;
2247 /* check if already resumed */
2248 if (udev->flags.self_suspended == 0)
2251 /* we need a parent HUB to do resume */
2252 if (udev->parent_hub == NULL)
2255 DPRINTF("udev=%p\n", udev);
2257 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2258 (udev->flags.remote_wakeup == 0)) {
2260 * If the host did not set the remote wakeup feature, we can
2261 * not wake it up either!
2263 DPRINTF("remote wakeup is not set!\n");
2266 /* get bus pointer */
2269 /* resume parent hub first */
2270 usb_dev_resume_peer(udev->parent_hub);
2272 /* reduce chance of instant resume failure by waiting a little bit */
2273 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2275 if (usb_device_20_compatible(udev)) {
2276 /* resume current port (Valid in Host and Device Mode) */
2277 err = usbd_req_clear_port_feature(udev->parent_hub,
2278 NULL, udev->port_no, UHF_PORT_SUSPEND);
2280 DPRINTFN(0, "Resuming port failed\n");
2284 /* resume current port (Valid in Host and Device Mode) */
2285 err = usbd_req_set_port_link_state(udev->parent_hub,
2286 NULL, udev->port_no, UPS_PORT_LS_U0);
2288 DPRINTFN(0, "Resuming port failed\n");
2293 /* resume settle time */
2294 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2296 if (bus->methods->device_resume != NULL) {
2297 /* resume USB device on the USB controller */
2298 (bus->methods->device_resume) (udev);
2301 /* set that this device is now resumed */
2302 udev->flags.self_suspended = 0;
2304 /* make sure that we don't go into suspend right away */
2305 udev->pwr_save.last_xfer_time = ticks;
2307 /* make sure the needed power masks are on */
2308 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2309 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2310 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2311 bus->hw_power_state |= USB_HW_POWER_BULK;
2312 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2313 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2314 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2315 bus->hw_power_state |= USB_HW_POWER_ISOC;
2317 USB_BUS_UNLOCK(bus);
2319 if (bus->methods->set_hw_power != NULL) {
2320 /* always update hardware power! */
2321 (bus->methods->set_hw_power) (bus);
2326 /* notify all sub-devices about resume */
2327 err = usb_suspend_resume(udev, 0);
2329 usbd_sr_unlock(udev);
2331 /* check if peer has wakeup capability */
2332 if (usb_peer_can_wakeup(udev)) {
2333 /* clear remote wakeup */
2334 err = usbd_req_clear_device_feature(udev,
2335 NULL, UF_DEVICE_REMOTE_WAKEUP);
2337 DPRINTFN(0, "Clearing device "
2338 "remote wakeup failed: %s\n",
2344 /*------------------------------------------------------------------------*
2345 * usb_dev_suspend_peer
2347 * This function will suspend an USB peer and do the required USB
2348 * signalling to get an USB device into the suspended state.
2349 *------------------------------------------------------------------------*/
2351 usb_dev_suspend_peer(struct usb_device *udev)
2353 struct usb_device *child;
2363 /* check if already suspended */
2364 if (udev->flags.self_suspended)
2367 /* we need a parent HUB to do suspend */
2368 if (udev->parent_hub == NULL)
2371 DPRINTF("udev=%p\n", udev);
2373 /* check if the current device is a HUB */
2374 if (udev->hub != NULL) {
2375 nports = udev->hub->nports;
2377 /* check if all devices on the HUB are suspended */
2378 for (x = 0; x != nports; x++) {
2379 child = usb_bus_port_get_device(udev->bus,
2380 udev->hub->ports + x);
2385 if (child->flags.self_suspended)
2388 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2393 if (usb_peer_can_wakeup(udev)) {
2395 * This request needs to be done before we set
2396 * "udev->flags.self_suspended":
2399 /* allow device to do remote wakeup */
2400 err = usbd_req_set_device_feature(udev,
2401 NULL, UF_DEVICE_REMOTE_WAKEUP);
2403 DPRINTFN(0, "Setting device "
2404 "remote wakeup failed\n");
2408 USB_BUS_LOCK(udev->bus);
2410 * Checking for suspend condition and setting suspended bit
2413 err = usb_peer_should_wakeup(udev);
2416 * Set that this device is suspended. This variable
2417 * must be set before calling USB controller suspend
2420 udev->flags.self_suspended = 1;
2422 USB_BUS_UNLOCK(udev->bus);
2425 if (usb_peer_can_wakeup(udev)) {
2426 /* allow device to do remote wakeup */
2427 err = usbd_req_clear_device_feature(udev,
2428 NULL, UF_DEVICE_REMOTE_WAKEUP);
2430 DPRINTFN(0, "Setting device "
2431 "remote wakeup failed\n");
2435 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2436 /* resume parent HUB first */
2437 usb_dev_resume_peer(udev->parent_hub);
2439 /* reduce chance of instant resume failure by waiting a little bit */
2440 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2442 /* resume current port (Valid in Host and Device Mode) */
2443 err = usbd_req_clear_port_feature(udev->parent_hub,
2444 NULL, udev->port_no, UHF_PORT_SUSPEND);
2446 /* resume settle time */
2447 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2449 DPRINTF("Suspend was cancelled!\n");
2455 /* notify all sub-devices about suspend */
2456 err = usb_suspend_resume(udev, 1);
2458 usbd_sr_unlock(udev);
2460 if (udev->bus->methods->device_suspend != NULL) {
2463 /* suspend device on the USB controller */
2464 (udev->bus->methods->device_suspend) (udev);
2467 temp = usbd_get_dma_delay(udev);
2469 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2473 if (usb_device_20_compatible(udev)) {
2474 /* suspend current port */
2475 err = usbd_req_set_port_feature(udev->parent_hub,
2476 NULL, udev->port_no, UHF_PORT_SUSPEND);
2478 DPRINTFN(0, "Suspending port failed\n");
2482 /* suspend current port */
2483 err = usbd_req_set_port_link_state(udev->parent_hub,
2484 NULL, udev->port_no, UPS_PORT_LS_U3);
2486 DPRINTFN(0, "Suspending port failed\n");
2491 udev = udev->parent_hub;
2495 /*------------------------------------------------------------------------*
2496 * usbd_set_power_mode
2498 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2500 *------------------------------------------------------------------------*/
2502 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2504 /* filter input argument */
2505 if ((power_mode != USB_POWER_MODE_ON) &&
2506 (power_mode != USB_POWER_MODE_OFF))
2507 power_mode = USB_POWER_MODE_SAVE;
2509 power_mode = usbd_filter_power_mode(udev, power_mode);
2511 udev->power_mode = power_mode; /* update copy of power mode */
2514 usb_bus_power_update(udev->bus);
2516 usb_needs_explore(udev->bus, 0 /* no probe */ );
2520 /*------------------------------------------------------------------------*
2521 * usbd_filter_power_mode
2523 * This function filters the power mode based on hardware requirements.
2524 *------------------------------------------------------------------------*/
2526 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2528 struct usb_bus_methods *mtod;
2531 mtod = udev->bus->methods;
2534 if (mtod->get_power_mode != NULL)
2535 (mtod->get_power_mode) (udev, &temp);
2537 /* check if we should not filter */
2539 return (power_mode);
2541 /* use fixed power mode given by hardware driver */
2545 /*------------------------------------------------------------------------*
2546 * usbd_start_re_enumerate
2548 * This function starts re-enumeration of the given USB device. This
2549 * function does not need to be called BUS-locked. This function does
2550 * not wait until the re-enumeration is completed.
2551 *------------------------------------------------------------------------*/
2553 usbd_start_re_enumerate(struct usb_device *udev)
2555 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2556 udev->re_enumerate_wait = USB_RE_ENUM_START;
2557 usb_needs_explore(udev->bus, 0);