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 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR uhub_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define UHUB_INTR_INTERVAL 250 /* ms */
77 #define UHUB_N_TRANSFER 1
80 static int uhub_debug = 0;
82 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
83 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
85 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
89 static int usb_power_timeout = 30; /* seconds */
91 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
92 &usb_power_timeout, 0, "USB power timeout");
95 struct uhub_current_state {
101 struct uhub_current_state sc_st;/* current state */
102 #if (USB_HAVE_FIXED_PORT != 0)
103 struct usb_hub sc_hub;
105 device_t sc_dev; /* base device */
106 struct mtx sc_mtx; /* our mutex */
107 struct usb_device *sc_udev; /* USB device */
108 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
110 #define UHUB_FLAG_DID_EXPLORE 0x01
113 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
114 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
115 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
116 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
117 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
119 /* prototypes for type checking: */
121 static device_probe_t uhub_probe;
122 static device_attach_t uhub_attach;
123 static device_detach_t uhub_detach;
124 static device_suspend_t uhub_suspend;
125 static device_resume_t uhub_resume;
127 static bus_driver_added_t uhub_driver_added;
128 static bus_child_location_str_t uhub_child_location_string;
129 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
131 static usb_callback_t uhub_intr_callback;
133 static void usb_dev_resume_peer(struct usb_device *udev);
134 static void usb_dev_suspend_peer(struct usb_device *udev);
135 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
137 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
140 .type = UE_INTERRUPT,
141 .endpoint = UE_ADDR_ANY,
142 .direction = UE_DIR_ANY,
144 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
145 .bufsize = 0, /* use wMaxPacketSize */
146 .callback = &uhub_intr_callback,
147 .interval = UHUB_INTR_INTERVAL,
152 * driver instance for "hub" connected to "usb"
153 * and "hub" connected to "hub"
155 static devclass_t uhub_devclass;
157 static device_method_t uhub_methods[] = {
158 DEVMETHOD(device_probe, uhub_probe),
159 DEVMETHOD(device_attach, uhub_attach),
160 DEVMETHOD(device_detach, uhub_detach),
162 DEVMETHOD(device_suspend, uhub_suspend),
163 DEVMETHOD(device_resume, uhub_resume),
165 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
166 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
167 DEVMETHOD(bus_driver_added, uhub_driver_added),
171 static driver_t uhub_driver = {
173 .methods = uhub_methods,
174 .size = sizeof(struct uhub_softc)
177 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
178 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
179 MODULE_VERSION(uhub, 1);
182 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
184 struct uhub_softc *sc = usbd_xfer_softc(xfer);
186 switch (USB_GET_STATE(xfer)) {
187 case USB_ST_TRANSFERRED:
190 * This is an indication that some port
191 * has changed status. Notify the bus
192 * event handler thread that we need
193 * to be explored again:
195 usb_needs_explore(sc->sc_udev->bus, 0);
198 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
199 usbd_transfer_submit(xfer);
203 if (xfer->error != USB_ERR_CANCELLED) {
205 * Do a clear-stall. The "stall_pipe" flag
206 * will get cleared before next callback by
209 usbd_xfer_set_stall(xfer);
210 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
211 usbd_transfer_submit(xfer);
217 /*------------------------------------------------------------------------*
218 * uhub_explore_sub - subroutine
222 * Else: A control transaction failed
223 *------------------------------------------------------------------------*/
225 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
228 struct usb_device *child;
232 bus = sc->sc_udev->bus;
235 /* get driver added refcount from USB bus */
236 refcount = bus->driver_added_refcount;
238 /* get device assosiated with the given port */
239 child = usb_bus_port_get_device(bus, up);
245 /* check if device should be re-enumerated */
247 if (child->flags.usb_mode == USB_MODE_HOST) {
250 do_unlock = usbd_enum_lock(child);
251 if (child->re_enumerate_wait) {
252 err = usbd_set_config_index(child,
255 DPRINTF("Unconfigure failed: "
259 err = usbd_req_re_enumerate(child, NULL);
261 err = usbd_set_config_index(child, 0);
263 err = usb_probe_and_attach(child,
264 USB_IFACE_INDEX_ANY);
266 child->re_enumerate_wait = 0;
270 usbd_enum_unlock(child);
273 /* check if probe and attach should be done */
275 if (child->driver_added_refcount != refcount) {
276 child->driver_added_refcount = refcount;
277 err = usb_probe_and_attach(child,
278 USB_IFACE_INDEX_ANY);
283 /* start control transfer, if device mode */
285 if (child->flags.usb_mode == USB_MODE_DEVICE)
286 usbd_ctrl_transfer_setup(child);
288 /* if a HUB becomes present, do a recursive HUB explore */
291 err = (child->hub->explore) (child);
297 /*------------------------------------------------------------------------*
298 * uhub_read_port_status - factored out code
299 *------------------------------------------------------------------------*/
301 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
303 struct usb_port_status ps;
306 err = usbd_req_get_port_status(
307 sc->sc_udev, NULL, &ps, portno);
309 /* update status regardless of error */
311 sc->sc_st.port_status = UGETW(ps.wPortStatus);
312 sc->sc_st.port_change = UGETW(ps.wPortChange);
314 /* debugging print */
316 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
317 "wPortChange=0x%04x, err=%s\n",
318 portno, sc->sc_st.port_status,
319 sc->sc_st.port_change, usbd_errstr(err));
323 /*------------------------------------------------------------------------*
328 * Else: A control transaction failed
329 *------------------------------------------------------------------------*/
331 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
333 struct usb_device *child;
334 struct usb_device *udev;
335 enum usb_dev_speed speed;
336 enum usb_hc_mode mode;
341 DPRINTF("reattaching port %d\n", portno);
345 child = usb_bus_port_get_device(udev->bus,
346 udev->hub->ports + portno - 1);
350 /* first clear the port connection change bit */
352 err = usbd_req_clear_port_feature(udev, NULL,
353 portno, UHF_C_PORT_CONNECTION);
358 /* check if there is a child */
362 * Free USB device and all subdevices, if any.
364 usb_free_device(child, 0);
367 /* get fresh status */
369 err = uhub_read_port_status(sc, portno);
373 /* check if nothing is connected to the port */
375 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
378 /* check if there is no power on the port and print a warning */
380 switch (udev->speed) {
384 power_mask = UPS_PORT_POWER;
386 case USB_SPEED_SUPER:
387 if (udev->parent_hub == NULL)
388 power_mask = UPS_PORT_POWER;
390 power_mask = UPS_PORT_POWER_SS;
396 if (!(sc->sc_st.port_status & power_mask)) {
397 DPRINTF("WARNING: strange, connected port %d "
398 "has no power\n", portno);
401 /* check if the device is in Host Mode */
403 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
405 DPRINTF("Port %d is in Host Mode\n", portno);
407 if (sc->sc_st.port_status & UPS_SUSPEND) {
409 * NOTE: Should not get here in SuperSpeed
410 * mode, because the HUB should report this
413 DPRINTF("Port %d was still "
414 "suspended, clearing.\n", portno);
415 err = usbd_req_clear_port_feature(udev,
416 NULL, portno, UHF_PORT_SUSPEND);
421 /* wait for maximum device power up time */
424 USB_MS_TO_TICKS(usb_port_powerup_delay));
426 /* reset port, which implies enabling it */
428 err = usbd_req_reset_port(udev, NULL, portno);
431 DPRINTFN(0, "port %d reset "
432 "failed, error=%s\n",
433 portno, usbd_errstr(err));
436 /* get port status again, it might have changed during reset */
438 err = uhub_read_port_status(sc, portno);
442 /* check if something changed during port reset */
444 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
445 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
447 DPRINTFN(0, "giving up port reset "
448 "- device vanished\n");
455 DPRINTF("Port %d is in Device Mode\n", portno);
459 * Figure out the device speed
461 switch (udev->speed) {
463 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
464 speed = USB_SPEED_HIGH;
465 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
466 speed = USB_SPEED_LOW;
468 speed = USB_SPEED_FULL;
471 if (sc->sc_st.port_status & UPS_LOW_SPEED)
472 speed = USB_SPEED_LOW;
474 speed = USB_SPEED_FULL;
477 speed = USB_SPEED_LOW;
479 case USB_SPEED_SUPER:
480 if (udev->parent_hub == NULL) {
481 /* Root HUB - special case */
482 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
484 speed = USB_SPEED_FULL;
487 speed = USB_SPEED_LOW;
490 speed = USB_SPEED_HIGH;
493 speed = USB_SPEED_SUPER;
497 speed = USB_SPEED_SUPER;
501 /* same speed like parent */
505 if (speed == USB_SPEED_SUPER) {
506 err = usbd_req_set_hub_u1_timeout(udev, NULL,
507 portno, 128 - (2 * udev->depth));
509 DPRINTFN(0, "port %d U1 timeout "
510 "failed, error=%s\n",
511 portno, usbd_errstr(err));
513 err = usbd_req_set_hub_u2_timeout(udev, NULL,
514 portno, 128 - (2 * udev->depth));
516 DPRINTFN(0, "port %d U2 timeout "
517 "failed, error=%s\n",
518 portno, usbd_errstr(err));
523 * Figure out the device mode
525 * NOTE: This part is currently FreeBSD specific.
527 if (udev->parent_hub != NULL) {
528 /* inherit mode from the parent HUB */
529 mode = udev->parent_hub->flags.usb_mode;
530 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
531 mode = USB_MODE_DEVICE;
533 mode = USB_MODE_HOST;
535 /* need to create a new child */
536 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
537 udev->depth + 1, portno - 1, portno, speed, mode);
539 DPRINTFN(0, "could not allocate new device\n");
542 return (0); /* success */
547 * Free USB device and all subdevices, if any.
549 usb_free_device(child, 0);
553 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
554 err = usbd_req_clear_port_feature(
556 portno, UHF_PORT_ENABLE);
560 DPRINTFN(0, "device problem (%s), "
561 "disabling port %d\n", usbd_errstr(err), portno);
566 /*------------------------------------------------------------------------*
567 * usb_device_20_compatible
570 * 0: HUB does not support suspend and resume
571 * Else: HUB supports suspend and resume
572 *------------------------------------------------------------------------*/
574 usb_device_20_compatible(struct usb_device *udev)
578 switch (udev->speed) {
588 /*------------------------------------------------------------------------*
589 * uhub_suspend_resume_port
593 * Else: A control transaction failed
594 *------------------------------------------------------------------------*/
596 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
598 struct usb_device *child;
599 struct usb_device *udev;
603 DPRINTF("port %d\n", portno);
606 child = usb_bus_port_get_device(udev->bus,
607 udev->hub->ports + portno - 1);
609 /* first clear the port suspend change bit */
611 if (usb_device_20_compatible(udev)) {
612 err = usbd_req_clear_port_feature(udev, NULL,
613 portno, UHF_C_PORT_SUSPEND);
615 err = usbd_req_clear_port_feature(udev, NULL,
616 portno, UHF_C_PORT_LINK_STATE);
620 DPRINTF("clearing suspend failed.\n");
623 /* get fresh status */
625 err = uhub_read_port_status(sc, portno);
627 DPRINTF("reading port status failed.\n");
630 /* convert current state */
632 if (usb_device_20_compatible(udev)) {
633 if (sc->sc_st.port_status & UPS_SUSPEND) {
639 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
643 case UPS_PORT_LS_SS_INA:
644 usbd_req_warm_reset_port(udev, NULL, portno);
653 DPRINTF("suspended=%u\n", is_suspend);
655 /* do the suspend or resume */
659 * This code handle two cases: 1) Host Mode - we can only
660 * receive resume here 2) Device Mode - we can receive
661 * suspend and resume here
664 usb_dev_resume_peer(child);
665 else if (child->flags.usb_mode == USB_MODE_DEVICE)
666 usb_dev_suspend_peer(child);
672 /*------------------------------------------------------------------------*
673 * uhub_root_interrupt
675 * This function is called when a Root HUB interrupt has
676 * happened. "ptr" and "len" makes up the Root HUB interrupt
677 * packet. This function is called having the "bus_mtx" locked.
678 *------------------------------------------------------------------------*/
680 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
682 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
684 usb_needs_explore(bus, 0);
688 uhub_is_too_deep(struct usb_device *udev)
690 switch (udev->speed) {
694 if (udev->depth > USB_HUB_MAX_DEPTH)
697 case USB_SPEED_SUPER:
698 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
707 /*------------------------------------------------------------------------*
713 *------------------------------------------------------------------------*/
715 uhub_explore(struct usb_device *udev)
718 struct uhub_softc *sc;
728 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
730 /* ignore devices that are too deep */
731 if (uhub_is_too_deep(udev))
732 return (USB_ERR_TOO_DEEP);
734 /* check if device is suspended */
735 if (udev->flags.self_suspended) {
736 /* need to wait until the child signals resume */
737 DPRINTF("Device is suspended!\n");
742 * Make sure we don't race against user-space applications
745 do_unlock = usbd_enum_lock(udev);
747 for (x = 0; x != hub->nports; x++) {
751 err = uhub_read_port_status(sc, portno);
753 /* most likely the HUB is gone */
756 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
757 DPRINTF("Overcurrent on port %u.\n", portno);
758 err = usbd_req_clear_port_feature(
759 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
761 /* most likely the HUB is gone */
765 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
767 * Fake a connect status change so that the
768 * status gets checked initially!
770 sc->sc_st.port_change |=
771 UPS_C_CONNECT_STATUS;
773 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
774 err = usbd_req_clear_port_feature(
775 udev, NULL, portno, UHF_C_PORT_ENABLE);
777 /* most likely the HUB is gone */
780 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
782 * Ignore the port error if the device
785 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
786 DPRINTFN(0, "illegal enable change, "
787 "port %d\n", portno);
790 if (up->restartcnt == USB_RESTART_MAX) {
791 /* XXX could try another speed ? */
792 DPRINTFN(0, "port error, giving up "
793 "port %d\n", portno);
795 sc->sc_st.port_change |=
796 UPS_C_CONNECT_STATUS;
801 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
802 err = uhub_reattach_port(sc, portno);
804 /* most likely the HUB is gone */
808 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
809 UPS_C_PORT_LINK_STATE)) {
810 err = uhub_suspend_resume_port(sc, portno);
812 /* most likely the HUB is gone */
816 err = uhub_explore_sub(sc, up);
818 /* no device(s) present */
821 /* explore succeeded - reset restart counter */
826 usbd_enum_unlock(udev);
828 /* initial status checked */
829 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
832 return (USB_ERR_NORMAL_COMPLETION);
836 uhub_probe(device_t dev)
838 struct usb_attach_arg *uaa = device_get_ivars(dev);
840 if (uaa->usb_mode != USB_MODE_HOST)
844 * The subclass for USB HUBs is currently ignored because it
845 * is 0 for some and 1 for others.
847 if (uaa->info.bConfigIndex == 0 &&
848 uaa->info.bDeviceClass == UDCLASS_HUB)
854 /* NOTE: The information returned by this function can be wrong. */
856 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
858 struct usb_hub_descriptor hubdesc20;
859 struct usb_hub_ss_descriptor hubdesc30;
864 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
865 return (USB_ERR_INVAL);
870 switch (udev->speed) {
874 /* assuming that there is one port */
875 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
877 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
878 "error=%s\n", usbd_errstr(err));
881 nports = hubdesc20.bNbrPorts;
885 if (udev->speed == USB_SPEED_HIGH)
886 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
889 case USB_SPEED_SUPER:
890 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
892 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
893 "error=%s\n", usbd_errstr(err));
896 nports = hubdesc30.bNbrPorts;
916 uhub_attach(device_t dev)
918 struct uhub_softc *sc = device_get_softc(dev);
919 struct usb_attach_arg *uaa = device_get_ivars(dev);
920 struct usb_device *udev = uaa->device;
921 struct usb_device *parent_hub = udev->parent_hub;
923 struct usb_hub_descriptor hubdesc20;
924 struct usb_hub_ss_descriptor hubdesc30;
936 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
938 device_set_usb_desc(dev);
940 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
941 "parent->selfpowered=%d\n",
943 udev->flags.self_powered,
946 parent_hub->flags.self_powered : 0);
948 if (uhub_is_too_deep(udev)) {
949 DPRINTFN(0, "HUB at depth %d, "
950 "exceeds maximum. HUB ignored\n", (int)udev->depth);
954 if (!udev->flags.self_powered && parent_hub &&
955 !parent_hub->flags.self_powered) {
956 DPRINTFN(0, "Bus powered HUB connected to "
957 "bus powered HUB. HUB ignored\n");
961 if (UHUB_IS_MULTI_TT(sc)) {
962 err = usbd_set_alt_interface_index(udev, 0, 1);
964 device_printf(dev, "MTT could not be enabled\n");
967 device_printf(dev, "MTT enabled\n");
970 /* get HUB descriptor */
972 DPRINTFN(2, "Getting HUB descriptor\n");
974 switch (udev->speed) {
978 /* assuming that there is one port */
979 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
981 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
982 "error=%s\n", usbd_errstr(err));
985 /* get number of ports */
986 nports = hubdesc20.bNbrPorts;
988 /* get power delay */
989 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
990 usb_extra_power_up_time);
992 /* get complete HUB descriptor */
994 /* check number of ports */
996 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
997 "error=%s\n", usbd_errstr(err));
1000 /* get complete HUB descriptor */
1001 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1004 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1005 "error=%s\n", usbd_errstr(err));
1008 if (hubdesc20.bNbrPorts != nports) {
1009 DPRINTFN(0, "Number of ports changed\n");
1014 case USB_SPEED_SUPER:
1015 if (udev->parent_hub != NULL) {
1016 err = usbd_req_set_hub_depth(udev, NULL,
1019 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1020 "error=%s\n", usbd_errstr(err));
1024 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1026 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1027 "error=%s\n", usbd_errstr(err));
1030 /* get number of ports */
1031 nports = hubdesc30.bNbrPorts;
1033 /* get power delay */
1034 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1035 usb_extra_power_up_time);
1037 /* get complete HUB descriptor */
1039 /* check number of ports */
1040 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1041 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1042 "error=%s\n", usbd_errstr(err));
1045 /* get complete HUB descriptor */
1046 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1049 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1050 "error=%s\n", usbd_errstr(err));
1053 if (hubdesc30.bNbrPorts != nports) {
1054 DPRINTFN(0, "Number of ports changed\n");
1060 DPRINTF("Assuming HUB has only one port\n");
1061 /* default number of ports */
1063 /* default power delay */
1064 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1068 DPRINTFN(0, "portless HUB\n");
1071 if (nports > USB_MAX_PORTS) {
1072 DPRINTF("Port limit exceeded\n");
1075 #if (USB_HAVE_FIXED_PORT == 0)
1076 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1077 M_USBDEV, M_WAITOK | M_ZERO);
1086 /* initialize HUB structure */
1088 hub->explore = &uhub_explore;
1089 hub->nports = nports;
1090 hub->hubudev = udev;
1092 /* if self powered hub, give ports maximum current */
1093 if (udev->flags.self_powered) {
1094 hub->portpower = USB_MAX_POWER;
1096 hub->portpower = USB_MIN_POWER;
1099 /* set up interrupt pipe */
1101 if (udev->parent_hub == NULL) {
1102 /* root HUB is special */
1106 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1107 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1110 DPRINTFN(0, "cannot setup interrupt transfer, "
1111 "errstr=%s\n", usbd_errstr(err));
1114 /* wait with power off for a while */
1115 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1118 * To have the best chance of success we do things in the exact same
1119 * order as Windoze98. This should not be necessary, but some
1120 * devices do not follow the USB specs to the letter.
1122 * These are the events on the bus when a hub is attached:
1123 * Get device and config descriptors (see attach code)
1124 * Get hub descriptor (see above)
1127 * wait for power to become stable
1128 * (all below happens in explore code)
1130 * clear C_PORT_CONNECTION
1133 * if device connected
1137 * clear C_PORT_RESET
1139 * proceed with device attachment
1142 /* XXX should check for none, individual, or ganged power? */
1146 for (x = 0; x != nports; x++) {
1147 /* set up data structures */
1148 struct usb_port *up = hub->ports + x;
1150 up->device_index = 0;
1154 /* check if port is removable */
1155 switch (udev->speed) {
1157 case USB_SPEED_FULL:
1158 case USB_SPEED_HIGH:
1159 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1162 case USB_SPEED_SUPER:
1163 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1167 DPRINTF("Assuming removable port\n");
1172 /* turn the power on */
1173 err = usbd_req_set_port_feature(udev, NULL,
1174 portno, UHF_PORT_POWER);
1177 DPRINTFN(0, "port %d power on failed, %s\n",
1178 portno, usbd_errstr(err));
1180 DPRINTF("turn on port %d power\n",
1183 /* wait for stable power */
1184 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1187 device_printf(dev, "%d port%s with %d "
1188 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1189 removable, udev->flags.self_powered ? "self" : "bus");
1191 /* Start the interrupt endpoint, if any */
1193 if (sc->sc_xfer[0] != NULL) {
1194 mtx_lock(&sc->sc_mtx);
1195 usbd_transfer_start(sc->sc_xfer[0]);
1196 mtx_unlock(&sc->sc_mtx);
1199 /* Enable automatic power save on all USB HUBs */
1201 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1206 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1208 #if (USB_HAVE_FIXED_PORT == 0)
1209 free(udev->hub, M_USBDEV);
1213 mtx_destroy(&sc->sc_mtx);
1219 * Called from process context when the hub is gone.
1220 * Detach all devices on active ports.
1223 uhub_detach(device_t dev)
1225 struct uhub_softc *sc = device_get_softc(dev);
1226 struct usb_hub *hub = sc->sc_udev->hub;
1227 struct usb_device *child;
1230 if (hub == NULL) /* must be partially working */
1233 /* Make sure interrupt transfer is gone. */
1234 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1236 /* Detach all ports */
1237 for (x = 0; x != hub->nports; x++) {
1239 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1241 if (child == NULL) {
1246 * Free USB device and all subdevices, if any.
1248 usb_free_device(child, 0);
1251 #if (USB_HAVE_FIXED_PORT == 0)
1252 free(hub, M_USBDEV);
1254 sc->sc_udev->hub = NULL;
1256 mtx_destroy(&sc->sc_mtx);
1262 uhub_suspend(device_t dev)
1265 /* Sub-devices are not suspended here! */
1270 uhub_resume(device_t dev)
1273 /* Sub-devices are not resumed here! */
1278 uhub_driver_added(device_t dev, driver_t *driver)
1280 usb_needs_explore_all();
1284 struct usb_device *udev;
1286 uint8_t iface_index;
1290 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1291 struct hub_result *res)
1293 struct usb_interface *iface;
1294 struct usb_device *udev;
1299 nports = hub->nports;
1300 for (x = 0; x != nports; x++) {
1301 udev = usb_bus_port_get_device(hub->hubudev->bus,
1306 for (i = 0; i != USB_IFACE_MAX; i++) {
1307 iface = usbd_get_iface(udev, i);
1309 (iface->subdev == child)) {
1310 res->iface_index = i;
1312 res->portno = x + 1;
1317 res->iface_index = 0;
1323 uhub_child_location_string(device_t parent, device_t child,
1324 char *buf, size_t buflen)
1326 struct uhub_softc *sc;
1327 struct usb_hub *hub;
1328 struct hub_result res;
1330 if (!device_is_attached(parent)) {
1336 sc = device_get_softc(parent);
1337 hub = sc->sc_udev->hub;
1340 uhub_find_iface_index(hub, child, &res);
1342 DPRINTF("device not on hub\n");
1348 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1349 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1350 res.portno, device_get_unit(res.udev->bus->bdev),
1351 res.udev->device_index, res.iface_index);
1359 uhub_child_pnpinfo_string(device_t parent, device_t child,
1360 char *buf, size_t buflen)
1362 struct uhub_softc *sc;
1363 struct usb_hub *hub;
1364 struct usb_interface *iface;
1365 struct hub_result res;
1367 if (!device_is_attached(parent)) {
1373 sc = device_get_softc(parent);
1374 hub = sc->sc_udev->hub;
1377 uhub_find_iface_index(hub, child, &res);
1379 DPRINTF("device not on hub\n");
1385 iface = usbd_get_iface(res.udev, res.iface_index);
1386 if (iface && iface->idesc) {
1387 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1388 "devclass=0x%02x devsubclass=0x%02x "
1392 "intclass=0x%02x intsubclass=0x%02x "
1393 "intprotocol=0x%02x " "%s%s",
1394 UGETW(res.udev->ddesc.idVendor),
1395 UGETW(res.udev->ddesc.idProduct),
1396 res.udev->ddesc.bDeviceClass,
1397 res.udev->ddesc.bDeviceSubClass,
1398 usb_get_serial(res.udev),
1399 UGETW(res.udev->ddesc.bcdDevice),
1400 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1401 iface->idesc->bInterfaceClass,
1402 iface->idesc->bInterfaceSubClass,
1403 iface->idesc->bInterfaceProtocol,
1404 iface->pnpinfo ? " " : "",
1405 iface->pnpinfo ? iface->pnpinfo : "");
1419 * The USB Transaction Translator:
1420 * ===============================
1422 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1423 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1424 * USB transfers. To utilize bandwidth dynamically the "scatter and
1425 * gather" principle must be applied. This means that bandwidth must
1426 * be divided into equal parts of bandwidth. With regard to USB all
1427 * data is transferred in smaller packets with length
1428 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1431 * The bandwidth scheduler which I have implemented will simply pack
1432 * the USB transfers back to back until there is no more space in the
1433 * schedule. Out of the 8 microframes which the USB 2.0 standard
1434 * provides, only 6 are available for non-HIGH-speed devices. I have
1435 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1436 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1437 * this division, it is very difficult to allocate and free bandwidth
1440 * NOTE about the Transaction Translator in USB HUBs:
1442 * USB HUBs have a very simple Transaction Translator, that will
1443 * simply pipeline all the SPLIT transactions. That means that the
1444 * transactions will be executed in the order they are queued!
1448 /*------------------------------------------------------------------------*
1449 * usb_intr_find_best_slot
1452 * The best Transaction Translation slot for an interrupt endpoint.
1453 *------------------------------------------------------------------------*/
1455 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1456 uint8_t end, uint8_t mask)
1458 usb_size_t min = (usb_size_t)-1;
1466 /* find the last slot with lesser used bandwidth */
1468 for (x = start; x < end; x++) {
1472 /* compute sum of bandwidth */
1473 for (z = x; z < end; z++) {
1474 if (mask & (1U << (z - x)))
1478 /* check if the current multi-slot is more optimal */
1484 /* check if the mask is about to be shifted out */
1485 if (mask & (1U << (end - 1 - x)))
1491 /*------------------------------------------------------------------------*
1492 * usb_hs_bandwidth_adjust
1494 * This function will update the bandwith usage for the microframe
1495 * having index "slot" by "len" bytes. "len" can be negative. If the
1496 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1497 * the "slot" argument will be replaced by the slot having least used
1498 * bandwidth. The "mask" argument is used for multi-slot allocations.
1501 * The slot in which the bandwidth update was done: 0..7
1502 *------------------------------------------------------------------------*/
1504 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1505 uint8_t slot, uint8_t mask)
1507 struct usb_bus *bus = udev->bus;
1508 struct usb_hub *hub;
1509 enum usb_dev_speed speed;
1512 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1514 speed = usbd_get_speed(udev);
1518 case USB_SPEED_FULL:
1519 if (speed == USB_SPEED_LOW) {
1523 * The Host Controller Driver should have
1524 * performed checks so that the lookup
1525 * below does not result in a NULL pointer
1529 hub = udev->parent_hs_hub->hub;
1530 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1531 slot = usb_intr_find_best_slot(hub->uframe_usage,
1532 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1534 for (x = slot; x < 8; x++) {
1535 if (mask & (1U << (x - slot))) {
1536 hub->uframe_usage[x] += len;
1537 bus->uframe_usage[x] += len;
1542 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1543 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1544 USB_HS_MICRO_FRAMES_MAX, mask);
1546 for (x = slot; x < 8; x++) {
1547 if (mask & (1U << (x - slot))) {
1548 bus->uframe_usage[x] += len;
1556 /*------------------------------------------------------------------------*
1557 * usb_hs_bandwidth_alloc
1559 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1560 *------------------------------------------------------------------------*/
1562 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1564 struct usb_device *udev;
1569 udev = xfer->xroot->udev;
1571 if (udev->flags.usb_mode != USB_MODE_HOST)
1572 return; /* not supported */
1574 xfer->endpoint->refcount_bw++;
1575 if (xfer->endpoint->refcount_bw != 1)
1576 return; /* already allocated */
1578 speed = usbd_get_speed(udev);
1580 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1582 /* allocate a microframe slot */
1585 slot = usb_hs_bandwidth_adjust(udev,
1586 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1588 xfer->endpoint->usb_uframe = slot;
1589 xfer->endpoint->usb_smask = mask << slot;
1591 if ((speed != USB_SPEED_FULL) &&
1592 (speed != USB_SPEED_LOW)) {
1593 xfer->endpoint->usb_cmask = 0x00 ;
1595 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1599 case UE_ISOCHRONOUS:
1600 switch (usbd_xfer_get_fps_shift(xfer)) {
1615 /* allocate a microframe multi-slot */
1617 slot = usb_hs_bandwidth_adjust(udev,
1618 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1620 xfer->endpoint->usb_uframe = slot;
1621 xfer->endpoint->usb_cmask = 0;
1622 xfer->endpoint->usb_smask = mask << slot;
1626 xfer->endpoint->usb_uframe = 0;
1627 xfer->endpoint->usb_cmask = 0;
1628 xfer->endpoint->usb_smask = 0;
1632 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1633 xfer->endpoint->usb_uframe,
1634 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1637 /*------------------------------------------------------------------------*
1638 * usb_hs_bandwidth_free
1640 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1641 *------------------------------------------------------------------------*/
1643 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1645 struct usb_device *udev;
1649 udev = xfer->xroot->udev;
1651 if (udev->flags.usb_mode != USB_MODE_HOST)
1652 return; /* not supported */
1654 xfer->endpoint->refcount_bw--;
1655 if (xfer->endpoint->refcount_bw != 0)
1656 return; /* still allocated */
1658 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1660 case UE_ISOCHRONOUS:
1662 slot = xfer->endpoint->usb_uframe;
1663 mask = xfer->endpoint->usb_smask;
1665 /* free microframe slot(s): */
1666 usb_hs_bandwidth_adjust(udev,
1667 -xfer->max_frame_size, slot, mask >> slot);
1669 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1670 slot, mask >> slot);
1672 xfer->endpoint->usb_uframe = 0;
1673 xfer->endpoint->usb_cmask = 0;
1674 xfer->endpoint->usb_smask = 0;
1682 /*------------------------------------------------------------------------*
1683 * usb_isoc_time_expand
1685 * This function will expand the time counter from 7-bit to 16-bit.
1688 * 16-bit isochronous time counter.
1689 *------------------------------------------------------------------------*/
1691 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1695 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1697 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1699 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1701 if (isoc_time_curr < rem) {
1702 /* the time counter wrapped around */
1703 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1705 /* update the remainder */
1707 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1708 bus->isoc_time_last |= isoc_time_curr;
1710 return (bus->isoc_time_last);
1713 /*------------------------------------------------------------------------*
1714 * usbd_fs_isoc_schedule_alloc_slot
1716 * This function will allocate bandwidth for an isochronous FULL speed
1717 * transaction in the FULL speed schedule.
1722 *------------------------------------------------------------------------*/
1723 #if USB_HAVE_TT_SUPPORT
1725 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1727 struct usb_xfer *xfer;
1728 struct usb_xfer *pipe_xfer;
1729 struct usb_bus *bus;
1731 usb_frlength_t data_len;
1739 bus = isoc_xfer->xroot->bus;
1741 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1743 /* skip self, if any */
1745 if (xfer == isoc_xfer)
1748 /* check if this USB transfer is going through the same TT */
1750 if (xfer->xroot->udev->parent_hs_hub !=
1751 isoc_xfer->xroot->udev->parent_hs_hub) {
1754 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1755 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1756 (xfer->xroot->udev->hs_port_no !=
1757 isoc_xfer->xroot->udev->hs_port_no)) {
1760 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1763 /* check if isoc_time is part of this transfer */
1765 delta = xfer->isoc_time_complete - isoc_time;
1766 if (delta > 0 && delta <= xfer->nframes) {
1767 delta = xfer->nframes - delta;
1769 len = xfer->frlengths[delta];
1778 * Check double buffered transfers. Only stream ID
1779 * equal to zero is valid here!
1781 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
1784 /* skip self, if any */
1786 if (pipe_xfer == isoc_xfer)
1789 /* check if isoc_time is part of this transfer */
1791 delta = pipe_xfer->isoc_time_complete - isoc_time;
1792 if (delta > 0 && delta <= pipe_xfer->nframes) {
1793 delta = pipe_xfer->nframes - delta;
1795 len = pipe_xfer->frlengths[delta];
1805 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1806 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1810 /* check for overflow */
1812 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1817 delta = isoc_xfer->isoc_time_complete - isoc_time;
1818 if (delta > 0 && delta <= isoc_xfer->nframes) {
1819 delta = isoc_xfer->nframes - delta;
1821 len = isoc_xfer->frlengths[delta];
1829 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1830 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1834 /* check for overflow */
1836 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1843 /*------------------------------------------------------------------------*
1844 * usb_bus_port_get_device
1846 * This function is NULL safe.
1847 *------------------------------------------------------------------------*/
1849 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1851 if ((bus == NULL) || (up == NULL)) {
1855 if (up->device_index == 0) {
1859 return (bus->devices[up->device_index]);
1862 /*------------------------------------------------------------------------*
1863 * usb_bus_port_set_device
1865 * This function is NULL safe.
1866 *------------------------------------------------------------------------*/
1868 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1869 struct usb_device *udev, uint8_t device_index)
1876 * There is only one case where we don't
1877 * have an USB port, and that is the Root Hub!
1881 up->device_index = device_index;
1883 device_index = up->device_index;
1884 up->device_index = 0;
1888 * Make relationships to our new device
1890 if (device_index != 0) {
1892 mtx_lock(&usb_ref_lock);
1894 bus->devices[device_index] = udev;
1896 mtx_unlock(&usb_ref_lock);
1902 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1905 /*------------------------------------------------------------------------*
1908 * This functions is called when the USB event thread needs to run.
1909 *------------------------------------------------------------------------*/
1911 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1918 DPRINTF("No bus pointer!\n");
1921 if ((bus->devices == NULL) ||
1922 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1923 DPRINTF("No root HUB\n");
1926 if (mtx_owned(&bus->bus_mtx)) {
1935 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
1936 &bus->explore_msg[0], &bus->explore_msg[1])) {
1940 USB_BUS_UNLOCK(bus);
1944 /*------------------------------------------------------------------------*
1945 * usb_needs_explore_all
1947 * This function is called whenever a new driver is loaded and will
1948 * cause that all USB busses are re-explored.
1949 *------------------------------------------------------------------------*/
1951 usb_needs_explore_all(void)
1953 struct usb_bus *bus;
1960 dc = usb_devclass_ptr;
1962 DPRINTFN(0, "no devclass\n");
1966 * Explore all USB busses in parallell.
1968 max = devclass_get_maxunit(dc);
1970 dev = devclass_get_device(dc, max);
1972 bus = device_get_softc(dev);
1974 usb_needs_explore(bus, 1);
1981 /*------------------------------------------------------------------------*
1982 * usb_bus_power_update
1984 * This function will ensure that all USB devices on the given bus are
1985 * properly suspended or resumed according to the device transfer
1987 *------------------------------------------------------------------------*/
1990 usb_bus_power_update(struct usb_bus *bus)
1992 usb_needs_explore(bus, 0 /* no probe */ );
1996 /*------------------------------------------------------------------------*
1997 * usbd_transfer_power_ref
1999 * This function will modify the power save reference counts and
2000 * wakeup the USB device associated with the given USB transfer, if
2002 *------------------------------------------------------------------------*/
2005 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2007 static const usb_power_mask_t power_mask[4] = {
2008 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2009 [UE_BULK] = USB_HW_POWER_BULK,
2010 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2011 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2013 struct usb_device *udev;
2014 uint8_t needs_explore;
2015 uint8_t needs_hw_power;
2018 udev = xfer->xroot->udev;
2020 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2021 /* no power save for root HUB */
2024 USB_BUS_LOCK(udev->bus);
2026 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2028 udev->pwr_save.last_xfer_time = ticks;
2029 udev->pwr_save.type_refs[xfer_type] += val;
2031 if (xfer->flags_int.control_xfr) {
2032 udev->pwr_save.read_refs += val;
2033 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2035 * It is not allowed to suspend during a
2038 udev->pwr_save.write_refs += val;
2040 } else if (USB_GET_DATA_ISREAD(xfer)) {
2041 udev->pwr_save.read_refs += val;
2043 udev->pwr_save.write_refs += val;
2047 if (udev->flags.self_suspended)
2048 needs_explore = usb_peer_should_wakeup(udev);
2052 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2053 DPRINTF("Adding type %u to power state\n", xfer_type);
2054 udev->bus->hw_power_state |= power_mask[xfer_type];
2064 USB_BUS_UNLOCK(udev->bus);
2066 if (needs_explore) {
2067 DPRINTF("update\n");
2068 usb_bus_power_update(udev->bus);
2069 } else if (needs_hw_power) {
2070 DPRINTF("needs power\n");
2071 if (udev->bus->methods->set_hw_power != NULL) {
2072 (udev->bus->methods->set_hw_power) (udev->bus);
2078 /*------------------------------------------------------------------------*
2079 * usb_peer_should_wakeup
2081 * This function returns non-zero if the current device should wake up.
2082 *------------------------------------------------------------------------*/
2084 usb_peer_should_wakeup(struct usb_device *udev)
2086 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2087 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2088 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2089 (udev->re_enumerate_wait != 0) ||
2090 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2091 (udev->pwr_save.write_refs != 0) ||
2092 ((udev->pwr_save.read_refs != 0) &&
2093 (udev->flags.usb_mode == USB_MODE_HOST) &&
2094 (usb_peer_can_wakeup(udev) == 0)));
2097 /*------------------------------------------------------------------------*
2100 * This function implements the USB power daemon and is called
2101 * regularly from the USB explore thread.
2102 *------------------------------------------------------------------------*/
2105 usb_bus_powerd(struct usb_bus *bus)
2107 struct usb_device *udev;
2110 usb_ticks_t mintime;
2111 usb_size_t type_refs[5];
2114 limit = usb_power_timeout;
2117 else if (limit > 255)
2122 DPRINTF("bus=%p\n", bus);
2127 * The root HUB device is never suspended
2128 * and we simply skip it.
2130 for (x = USB_ROOT_HUB_ADDR + 1;
2131 x != bus->devices_max; x++) {
2133 udev = bus->devices[x];
2137 temp = ticks - udev->pwr_save.last_xfer_time;
2139 if (usb_peer_should_wakeup(udev)) {
2140 /* check if we are suspended */
2141 if (udev->flags.self_suspended != 0) {
2142 USB_BUS_UNLOCK(bus);
2143 usb_dev_resume_peer(udev);
2146 } else if ((temp >= limit) &&
2147 (udev->flags.usb_mode == USB_MODE_HOST) &&
2148 (udev->flags.self_suspended == 0)) {
2149 /* try to do suspend */
2151 USB_BUS_UNLOCK(bus);
2152 usb_dev_suspend_peer(udev);
2157 /* reset counters */
2159 mintime = (usb_ticks_t)-1;
2166 /* Re-loop all the devices to get the actual state */
2168 for (x = USB_ROOT_HUB_ADDR + 1;
2169 x != bus->devices_max; x++) {
2171 udev = bus->devices[x];
2175 /* we found a non-Root-Hub USB device */
2178 /* "last_xfer_time" can be updated by a resume */
2179 temp = ticks - udev->pwr_save.last_xfer_time;
2182 * Compute minimum time since last transfer for the complete
2188 if (udev->flags.self_suspended == 0) {
2189 type_refs[0] += udev->pwr_save.type_refs[0];
2190 type_refs[1] += udev->pwr_save.type_refs[1];
2191 type_refs[2] += udev->pwr_save.type_refs[2];
2192 type_refs[3] += udev->pwr_save.type_refs[3];
2196 if (mintime >= (usb_ticks_t)(1 * hz)) {
2197 /* recompute power masks */
2198 DPRINTF("Recomputing power masks\n");
2199 bus->hw_power_state = 0;
2200 if (type_refs[UE_CONTROL] != 0)
2201 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2202 if (type_refs[UE_BULK] != 0)
2203 bus->hw_power_state |= USB_HW_POWER_BULK;
2204 if (type_refs[UE_INTERRUPT] != 0)
2205 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2206 if (type_refs[UE_ISOCHRONOUS] != 0)
2207 bus->hw_power_state |= USB_HW_POWER_ISOC;
2208 if (type_refs[4] != 0)
2209 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2211 USB_BUS_UNLOCK(bus);
2213 if (bus->methods->set_hw_power != NULL) {
2214 /* always update hardware power! */
2215 (bus->methods->set_hw_power) (bus);
2221 /*------------------------------------------------------------------------*
2222 * usb_dev_resume_peer
2224 * This function will resume an USB peer and do the required USB
2225 * signalling to get an USB device out of the suspended state.
2226 *------------------------------------------------------------------------*/
2228 usb_dev_resume_peer(struct usb_device *udev)
2230 struct usb_bus *bus;
2237 /* check if already resumed */
2238 if (udev->flags.self_suspended == 0)
2241 /* we need a parent HUB to do resume */
2242 if (udev->parent_hub == NULL)
2245 DPRINTF("udev=%p\n", udev);
2247 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2248 (udev->flags.remote_wakeup == 0)) {
2250 * If the host did not set the remote wakeup feature, we can
2251 * not wake it up either!
2253 DPRINTF("remote wakeup is not set!\n");
2256 /* get bus pointer */
2259 /* resume parent hub first */
2260 usb_dev_resume_peer(udev->parent_hub);
2262 /* reduce chance of instant resume failure by waiting a little bit */
2263 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2265 if (usb_device_20_compatible(udev)) {
2266 /* resume current port (Valid in Host and Device Mode) */
2267 err = usbd_req_clear_port_feature(udev->parent_hub,
2268 NULL, udev->port_no, UHF_PORT_SUSPEND);
2270 DPRINTFN(0, "Resuming port failed\n");
2274 /* resume current port (Valid in Host and Device Mode) */
2275 err = usbd_req_set_port_link_state(udev->parent_hub,
2276 NULL, udev->port_no, UPS_PORT_LS_U0);
2278 DPRINTFN(0, "Resuming port failed\n");
2283 /* resume settle time */
2284 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2286 if (bus->methods->device_resume != NULL) {
2287 /* resume USB device on the USB controller */
2288 (bus->methods->device_resume) (udev);
2291 /* set that this device is now resumed */
2292 udev->flags.self_suspended = 0;
2294 /* make sure that we don't go into suspend right away */
2295 udev->pwr_save.last_xfer_time = ticks;
2297 /* make sure the needed power masks are on */
2298 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2299 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2300 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2301 bus->hw_power_state |= USB_HW_POWER_BULK;
2302 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2303 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2304 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2305 bus->hw_power_state |= USB_HW_POWER_ISOC;
2307 USB_BUS_UNLOCK(bus);
2309 if (bus->methods->set_hw_power != NULL) {
2310 /* always update hardware power! */
2311 (bus->methods->set_hw_power) (bus);
2316 /* notify all sub-devices about resume */
2317 err = usb_suspend_resume(udev, 0);
2319 usbd_sr_unlock(udev);
2321 /* check if peer has wakeup capability */
2322 if (usb_peer_can_wakeup(udev)) {
2323 /* clear remote wakeup */
2324 err = usbd_req_clear_device_feature(udev,
2325 NULL, UF_DEVICE_REMOTE_WAKEUP);
2327 DPRINTFN(0, "Clearing device "
2328 "remote wakeup failed: %s\n",
2334 /*------------------------------------------------------------------------*
2335 * usb_dev_suspend_peer
2337 * This function will suspend an USB peer and do the required USB
2338 * signalling to get an USB device into the suspended state.
2339 *------------------------------------------------------------------------*/
2341 usb_dev_suspend_peer(struct usb_device *udev)
2343 struct usb_device *child;
2353 /* check if already suspended */
2354 if (udev->flags.self_suspended)
2357 /* we need a parent HUB to do suspend */
2358 if (udev->parent_hub == NULL)
2361 DPRINTF("udev=%p\n", udev);
2363 /* check if the current device is a HUB */
2364 if (udev->hub != NULL) {
2365 nports = udev->hub->nports;
2367 /* check if all devices on the HUB are suspended */
2368 for (x = 0; x != nports; x++) {
2369 child = usb_bus_port_get_device(udev->bus,
2370 udev->hub->ports + x);
2375 if (child->flags.self_suspended)
2378 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2383 if (usb_peer_can_wakeup(udev)) {
2385 * This request needs to be done before we set
2386 * "udev->flags.self_suspended":
2389 /* allow device to do remote wakeup */
2390 err = usbd_req_set_device_feature(udev,
2391 NULL, UF_DEVICE_REMOTE_WAKEUP);
2393 DPRINTFN(0, "Setting device "
2394 "remote wakeup failed\n");
2398 USB_BUS_LOCK(udev->bus);
2400 * Checking for suspend condition and setting suspended bit
2403 err = usb_peer_should_wakeup(udev);
2406 * Set that this device is suspended. This variable
2407 * must be set before calling USB controller suspend
2410 udev->flags.self_suspended = 1;
2412 USB_BUS_UNLOCK(udev->bus);
2415 if (usb_peer_can_wakeup(udev)) {
2416 /* allow device to do remote wakeup */
2417 err = usbd_req_clear_device_feature(udev,
2418 NULL, UF_DEVICE_REMOTE_WAKEUP);
2420 DPRINTFN(0, "Setting device "
2421 "remote wakeup failed\n");
2425 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2426 /* resume parent HUB first */
2427 usb_dev_resume_peer(udev->parent_hub);
2429 /* reduce chance of instant resume failure by waiting a little bit */
2430 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2432 /* resume current port (Valid in Host and Device Mode) */
2433 err = usbd_req_clear_port_feature(udev->parent_hub,
2434 NULL, udev->port_no, UHF_PORT_SUSPEND);
2436 /* resume settle time */
2437 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2439 DPRINTF("Suspend was cancelled!\n");
2445 /* notify all sub-devices about suspend */
2446 err = usb_suspend_resume(udev, 1);
2448 usbd_sr_unlock(udev);
2450 if (udev->bus->methods->device_suspend != NULL) {
2453 /* suspend device on the USB controller */
2454 (udev->bus->methods->device_suspend) (udev);
2457 temp = usbd_get_dma_delay(udev);
2459 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2463 if (usb_device_20_compatible(udev)) {
2464 /* suspend current port */
2465 err = usbd_req_set_port_feature(udev->parent_hub,
2466 NULL, udev->port_no, UHF_PORT_SUSPEND);
2468 DPRINTFN(0, "Suspending port failed\n");
2472 /* suspend current port */
2473 err = usbd_req_set_port_link_state(udev->parent_hub,
2474 NULL, udev->port_no, UPS_PORT_LS_U3);
2476 DPRINTFN(0, "Suspending port failed\n");
2481 udev = udev->parent_hub;
2485 /*------------------------------------------------------------------------*
2486 * usbd_set_power_mode
2488 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2490 *------------------------------------------------------------------------*/
2492 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2494 /* filter input argument */
2495 if ((power_mode != USB_POWER_MODE_ON) &&
2496 (power_mode != USB_POWER_MODE_OFF))
2497 power_mode = USB_POWER_MODE_SAVE;
2499 power_mode = usbd_filter_power_mode(udev, power_mode);
2501 udev->power_mode = power_mode; /* update copy of power mode */
2504 usb_bus_power_update(udev->bus);
2508 /*------------------------------------------------------------------------*
2509 * usbd_filter_power_mode
2511 * This function filters the power mode based on hardware requirements.
2512 *------------------------------------------------------------------------*/
2514 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2516 struct usb_bus_methods *mtod;
2519 mtod = udev->bus->methods;
2522 if (mtod->get_power_mode != NULL)
2523 (mtod->get_power_mode) (udev, &temp);
2525 /* check if we should not filter */
2527 return (power_mode);
2529 /* use fixed power mode given by hardware driver */
2533 /*------------------------------------------------------------------------*
2534 * usbd_start_re_enumerate
2536 * This function starts re-enumeration of the given USB device. This
2537 * function does not need to be called BUS-locked. This function does
2538 * not wait until the re-enumeration is completed.
2539 *------------------------------------------------------------------------*/
2541 usbd_start_re_enumerate(struct usb_device *udev)
2543 if (udev->re_enumerate_wait == 0) {
2544 udev->re_enumerate_wait = 1;
2545 usb_needs_explore(udev->bus, 0);