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/linker_set.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usb_ioctl.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
58 #define USB_DEBUG_VAR uhub_debug
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_request.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_hub.h>
66 #include <dev/usb/usb_util.h>
67 #include <dev/usb/usb_busdma.h>
68 #include <dev/usb/usb_transfer.h>
69 #include <dev/usb/usb_dynamic.h>
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
74 #define UHUB_INTR_INTERVAL 250 /* ms */
75 #define UHUB_N_TRANSFER 1
78 static int uhub_debug = 0;
80 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
81 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
84 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
88 static int usb_power_timeout = 30; /* seconds */
90 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
91 &usb_power_timeout, 0, "USB power timeout");
94 struct uhub_current_state {
100 struct uhub_current_state sc_st;/* current state */
101 device_t sc_dev; /* base device */
102 struct mtx sc_mtx; /* our mutex */
103 struct usb_device *sc_udev; /* USB device */
104 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
106 #define UHUB_FLAG_DID_EXPLORE 0x01
110 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
111 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
112 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
113 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
115 /* prototypes for type checking: */
117 static device_probe_t uhub_probe;
118 static device_attach_t uhub_attach;
119 static device_detach_t uhub_detach;
120 static device_suspend_t uhub_suspend;
121 static device_resume_t uhub_resume;
123 static bus_driver_added_t uhub_driver_added;
124 static bus_child_location_str_t uhub_child_location_string;
125 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
127 static usb_callback_t uhub_intr_callback;
129 static void usb_dev_resume_peer(struct usb_device *udev);
130 static void usb_dev_suspend_peer(struct usb_device *udev);
131 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
133 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
136 .type = UE_INTERRUPT,
137 .endpoint = UE_ADDR_ANY,
138 .direction = UE_DIR_ANY,
140 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
141 .bufsize = 0, /* use wMaxPacketSize */
142 .callback = &uhub_intr_callback,
143 .interval = UHUB_INTR_INTERVAL,
148 * driver instance for "hub" connected to "usb"
149 * and "hub" connected to "hub"
151 static devclass_t uhub_devclass;
153 static device_method_t uhub_methods[] = {
154 DEVMETHOD(device_probe, uhub_probe),
155 DEVMETHOD(device_attach, uhub_attach),
156 DEVMETHOD(device_detach, uhub_detach),
158 DEVMETHOD(device_suspend, uhub_suspend),
159 DEVMETHOD(device_resume, uhub_resume),
161 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
162 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
163 DEVMETHOD(bus_driver_added, uhub_driver_added),
167 static driver_t uhub_driver = {
169 .methods = uhub_methods,
170 .size = sizeof(struct uhub_softc)
173 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
174 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
175 MODULE_VERSION(uhub, 1);
178 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
180 struct uhub_softc *sc = usbd_xfer_softc(xfer);
182 switch (USB_GET_STATE(xfer)) {
183 case USB_ST_TRANSFERRED:
186 * This is an indication that some port
187 * has changed status. Notify the bus
188 * event handler thread that we need
189 * to be explored again:
191 usb_needs_explore(sc->sc_udev->bus, 0);
194 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
195 usbd_transfer_submit(xfer);
199 if (xfer->error != USB_ERR_CANCELLED) {
201 * Do a clear-stall. The "stall_pipe" flag
202 * will get cleared before next callback by
205 usbd_xfer_set_stall(xfer);
206 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
207 usbd_transfer_submit(xfer);
213 /*------------------------------------------------------------------------*
214 * uhub_explore_sub - subroutine
218 * Else: A control transaction failed
219 *------------------------------------------------------------------------*/
221 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
224 struct usb_device *child;
228 bus = sc->sc_udev->bus;
231 /* get driver added refcount from USB bus */
232 refcount = bus->driver_added_refcount;
234 /* get device assosiated with the given port */
235 child = usb_bus_port_get_device(bus, up);
241 /* check if device should be re-enumerated */
243 if (child->flags.usb_mode == USB_MODE_HOST) {
244 usbd_enum_lock(child);
245 if (child->re_enumerate_wait) {
246 err = usbd_set_config_index(child, USB_UNCONFIG_INDEX);
248 err = usbd_req_re_enumerate(child, NULL);
250 err = usbd_set_config_index(child, 0);
252 err = usb_probe_and_attach(child,
253 USB_IFACE_INDEX_ANY);
255 child->re_enumerate_wait = 0;
258 usbd_enum_unlock(child);
261 /* check if probe and attach should be done */
263 if (child->driver_added_refcount != refcount) {
264 child->driver_added_refcount = refcount;
265 err = usb_probe_and_attach(child,
266 USB_IFACE_INDEX_ANY);
271 /* start control transfer, if device mode */
273 if (child->flags.usb_mode == USB_MODE_DEVICE)
274 usbd_ctrl_transfer_setup(child);
276 /* if a HUB becomes present, do a recursive HUB explore */
279 err = (child->hub->explore) (child);
285 /*------------------------------------------------------------------------*
286 * uhub_read_port_status - factored out code
287 *------------------------------------------------------------------------*/
289 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
291 struct usb_port_status ps;
294 err = usbd_req_get_port_status(
295 sc->sc_udev, NULL, &ps, portno);
297 /* update status regardless of error */
299 sc->sc_st.port_status = UGETW(ps.wPortStatus);
300 sc->sc_st.port_change = UGETW(ps.wPortChange);
302 /* debugging print */
304 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
305 "wPortChange=0x%04x, err=%s\n",
306 portno, sc->sc_st.port_status,
307 sc->sc_st.port_change, usbd_errstr(err));
311 /*------------------------------------------------------------------------*
316 * Else: A control transaction failed
317 *------------------------------------------------------------------------*/
319 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
321 struct usb_device *child;
322 struct usb_device *udev;
323 enum usb_dev_speed speed;
324 enum usb_hc_mode mode;
328 DPRINTF("reattaching port %d\n", portno);
333 child = usb_bus_port_get_device(udev->bus,
334 udev->hub->ports + portno - 1);
338 /* first clear the port connection change bit */
340 err = usbd_req_clear_port_feature(udev, NULL,
341 portno, UHF_C_PORT_CONNECTION);
346 /* check if there is a child */
350 * Free USB device and all subdevices, if any.
352 usb_free_device(child, 0);
355 /* get fresh status */
357 err = uhub_read_port_status(sc, portno);
361 /* check if nothing is connected to the port */
363 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
366 /* check if there is no power on the port and print a warning */
368 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
369 DPRINTF("WARNING: strange, connected port %d "
370 "has no power\n", portno);
372 /* check if the device is in Host Mode */
374 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
376 DPRINTF("Port %d is in Host Mode\n", portno);
378 if (sc->sc_st.port_status & UPS_SUSPEND) {
380 * NOTE: Should not get here in SuperSpeed
381 * mode, because the HUB should report this
384 DPRINTF("Port %d was still "
385 "suspended, clearing.\n", portno);
386 err = usbd_req_clear_port_feature(udev,
387 NULL, portno, UHF_PORT_SUSPEND);
392 /* wait for maximum device power up time */
395 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
397 /* reset port, which implies enabling it */
399 err = usbd_req_reset_port(udev, NULL, portno);
402 DPRINTFN(0, "port %d reset "
403 "failed, error=%s\n",
404 portno, usbd_errstr(err));
407 /* get port status again, it might have changed during reset */
409 err = uhub_read_port_status(sc, portno);
413 /* check if something changed during port reset */
415 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
416 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
418 DPRINTFN(0, "giving up port reset "
419 "- device vanished\n");
426 DPRINTF("Port %d is in Device Mode\n", portno);
430 * Figure out the device speed
432 switch (udev->speed) {
434 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
435 speed = USB_SPEED_HIGH;
436 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
437 speed = USB_SPEED_LOW;
439 speed = USB_SPEED_FULL;
442 if (sc->sc_st.port_status & UPS_LOW_SPEED)
443 speed = USB_SPEED_LOW;
445 speed = USB_SPEED_FULL;
448 speed = USB_SPEED_LOW;
450 case USB_SPEED_SUPER:
451 if (udev->parent_hub == NULL) {
452 /* Root HUB - special case */
453 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
455 speed = USB_SPEED_FULL;
458 speed = USB_SPEED_LOW;
461 speed = USB_SPEED_HIGH;
464 speed = USB_SPEED_SUPER;
468 speed = USB_SPEED_SUPER;
472 /* same speed like parent */
476 if (speed == USB_SPEED_SUPER) {
477 err = usbd_req_set_hub_u1_timeout(udev, NULL,
478 portno, 128 - (2 * udev->depth));
480 DPRINTFN(0, "port %d U1 timeout "
481 "failed, error=%s\n",
482 portno, usbd_errstr(err));
484 err = usbd_req_set_hub_u2_timeout(udev, NULL,
485 portno, 128 - (2 * udev->depth));
487 DPRINTFN(0, "port %d U2 timeout "
488 "failed, error=%s\n",
489 portno, usbd_errstr(err));
494 * Figure out the device mode
496 * NOTE: This part is currently FreeBSD specific.
498 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
499 mode = USB_MODE_DEVICE;
501 mode = USB_MODE_HOST;
503 /* need to create a new child */
504 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
505 udev->depth + 1, portno - 1, portno, speed, mode);
507 DPRINTFN(0, "could not allocate new device\n");
510 return (0); /* success */
515 * Free USB device and all subdevices, if any.
517 usb_free_device(child, 0);
521 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
522 err = usbd_req_clear_port_feature(
524 portno, UHF_PORT_ENABLE);
528 DPRINTFN(0, "device problem (%s), "
529 "disabling port %d\n", usbd_errstr(err), portno);
534 /*------------------------------------------------------------------------*
535 * usb_device_20_compatible
538 * 0: HUB does not support suspend and resume
539 * Else: HUB supports suspend and resume
540 *------------------------------------------------------------------------*/
542 usb_device_20_compatible(struct usb_device *udev)
546 switch (udev->speed) {
556 /*------------------------------------------------------------------------*
557 * uhub_suspend_resume_port
561 * Else: A control transaction failed
562 *------------------------------------------------------------------------*/
564 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
566 struct usb_device *child;
567 struct usb_device *udev;
571 DPRINTF("port %d\n", portno);
574 child = usb_bus_port_get_device(udev->bus,
575 udev->hub->ports + portno - 1);
577 /* first clear the port suspend change bit */
579 if (usb_device_20_compatible(udev)) {
580 err = usbd_req_clear_port_feature(udev, NULL,
581 portno, UHF_C_PORT_SUSPEND);
583 err = usbd_req_clear_port_feature(udev, NULL,
584 portno, UHF_C_PORT_LINK_STATE);
588 DPRINTF("clearing suspend failed.\n");
591 /* get fresh status */
593 err = uhub_read_port_status(sc, portno);
595 DPRINTF("reading port status failed.\n");
598 /* convert current state */
600 if (usb_device_20_compatible(udev)) {
601 if (sc->sc_st.port_status & UPS_SUSPEND) {
607 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
618 DPRINTF("suspended=%u\n", is_suspend);
620 /* do the suspend or resume */
624 * This code handle two cases: 1) Host Mode - we can only
625 * receive resume here 2) Device Mode - we can receive
626 * suspend and resume here
629 usb_dev_resume_peer(child);
630 else if ((child->flags.usb_mode == USB_MODE_DEVICE) ||
631 (usb_device_20_compatible(child) == 0))
632 usb_dev_suspend_peer(child);
638 /*------------------------------------------------------------------------*
639 * uhub_root_interrupt
641 * This function is called when a Root HUB interrupt has
642 * happened. "ptr" and "len" makes up the Root HUB interrupt
643 * packet. This function is called having the "bus_mtx" locked.
644 *------------------------------------------------------------------------*/
646 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
648 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
650 usb_needs_explore(bus, 0);
654 uhub_is_too_deep(struct usb_device *udev)
656 switch (udev->speed) {
660 if (udev->depth > USB_HUB_MAX_DEPTH)
663 case USB_SPEED_SUPER:
664 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
673 /*------------------------------------------------------------------------*
679 *------------------------------------------------------------------------*/
681 uhub_explore(struct usb_device *udev)
684 struct uhub_softc *sc;
693 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
695 /* ignore devices that are too deep */
696 if (uhub_is_too_deep(udev))
697 return (USB_ERR_TOO_DEEP);
699 /* check if device is suspended */
700 if (udev->flags.self_suspended) {
701 /* need to wait until the child signals resume */
702 DPRINTF("Device is suspended!\n");
705 for (x = 0; x != hub->nports; x++) {
709 err = uhub_read_port_status(sc, portno);
711 /* most likely the HUB is gone */
714 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
715 DPRINTF("Overcurrent on port %u.\n", portno);
716 err = usbd_req_clear_port_feature(
717 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
719 /* most likely the HUB is gone */
723 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
725 * Fake a connect status change so that the
726 * status gets checked initially!
728 sc->sc_st.port_change |=
729 UPS_C_CONNECT_STATUS;
731 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
732 err = usbd_req_clear_port_feature(
733 udev, NULL, portno, UHF_C_PORT_ENABLE);
735 /* most likely the HUB is gone */
738 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
740 * Ignore the port error if the device
743 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
744 DPRINTFN(0, "illegal enable change, "
745 "port %d\n", portno);
748 if (up->restartcnt == USB_RESTART_MAX) {
749 /* XXX could try another speed ? */
750 DPRINTFN(0, "port error, giving up "
751 "port %d\n", portno);
753 sc->sc_st.port_change |=
754 UPS_C_CONNECT_STATUS;
759 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
760 err = uhub_reattach_port(sc, portno);
762 /* most likely the HUB is gone */
766 if (sc->sc_st.port_change & (UPS_C_SUSPEND | UPS_C_PORT_LINK_STATE)) {
767 err = uhub_suspend_resume_port(sc, portno);
769 /* most likely the HUB is gone */
773 err = uhub_explore_sub(sc, up);
775 /* no device(s) present */
778 /* explore succeeded - reset restart counter */
782 /* initial status checked */
783 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
786 return (USB_ERR_NORMAL_COMPLETION);
790 uhub_probe(device_t dev)
792 struct usb_attach_arg *uaa = device_get_ivars(dev);
794 if (uaa->usb_mode != USB_MODE_HOST)
798 * The subclass for USB HUBs is currently ignored because it
799 * is 0 for some and 1 for others.
801 if (uaa->info.bConfigIndex == 0 &&
802 uaa->info.bDeviceClass == UDCLASS_HUB)
808 /* NOTE: The information returned by this function can be wrong. */
810 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
812 struct usb_hub_descriptor hubdesc20;
813 struct usb_hub_ss_descriptor hubdesc30;
818 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
819 return (USB_ERR_INVAL);
824 switch (udev->speed) {
828 /* assuming that there is one port */
829 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
831 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
832 "error=%s\n", usbd_errstr(err));
835 nports = hubdesc20.bNbrPorts;
839 if (udev->speed == USB_SPEED_HIGH)
840 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
843 case USB_SPEED_SUPER:
844 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
846 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
847 "error=%s\n", usbd_errstr(err));
850 nports = hubdesc30.bNbrPorts;
870 uhub_attach(device_t dev)
872 struct uhub_softc *sc = device_get_softc(dev);
873 struct usb_attach_arg *uaa = device_get_ivars(dev);
874 struct usb_device *udev = uaa->device;
875 struct usb_device *parent_hub = udev->parent_hub;
877 struct usb_hub_descriptor hubdesc20;
878 struct usb_hub_ss_descriptor hubdesc30;
890 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
892 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
893 device_get_nameunit(dev));
895 device_set_usb_desc(dev);
897 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
898 "parent->selfpowered=%d\n",
900 udev->flags.self_powered,
903 parent_hub->flags.self_powered : 0);
905 if (uhub_is_too_deep(udev)) {
906 DPRINTFN(0, "HUB at depth %d, "
907 "exceeds maximum. HUB ignored\n", (int)udev->depth);
911 if (!udev->flags.self_powered && parent_hub &&
912 !parent_hub->flags.self_powered) {
913 DPRINTFN(0, "Bus powered HUB connected to "
914 "bus powered HUB. HUB ignored\n");
917 /* get HUB descriptor */
919 DPRINTFN(2, "Getting HUB descriptor\n");
921 switch (udev->speed) {
925 /* assuming that there is one port */
926 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
928 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
929 "error=%s\n", usbd_errstr(err));
932 /* get number of ports */
933 nports = hubdesc20.bNbrPorts;
935 /* get power delay */
936 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
937 USB_EXTRA_POWER_UP_TIME);
939 /* get complete HUB descriptor */
941 /* check number of ports */
943 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
944 "error=%s\n", usbd_errstr(err));
947 /* get complete HUB descriptor */
948 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
951 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
952 "error=%s\n", usbd_errstr(err));
955 if (hubdesc20.bNbrPorts != nports) {
956 DPRINTFN(0, "Number of ports changed\n");
961 case USB_SPEED_SUPER:
962 if (udev->parent_hub != NULL) {
963 err = usbd_req_set_hub_depth(udev, NULL,
966 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
967 "error=%s\n", usbd_errstr(err));
971 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
973 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
974 "error=%s\n", usbd_errstr(err));
977 /* get number of ports */
978 nports = hubdesc30.bNbrPorts;
980 /* get power delay */
981 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
982 USB_EXTRA_POWER_UP_TIME);
984 /* get complete HUB descriptor */
986 /* check number of ports */
987 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
988 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
989 "error=%s\n", usbd_errstr(err));
992 /* get complete HUB descriptor */
993 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
996 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
997 "error=%s\n", usbd_errstr(err));
1000 if (hubdesc30.bNbrPorts != nports) {
1001 DPRINTFN(0, "Number of ports changed\n");
1007 DPRINTF("Assuming HUB has only one port\n");
1008 /* default number of ports */
1010 /* default power delay */
1011 pwrdly = ((10 * UHD_PWRON_FACTOR) + USB_EXTRA_POWER_UP_TIME);
1015 DPRINTFN(0, "portless HUB\n");
1018 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1019 M_USBDEV, M_WAITOK | M_ZERO);
1026 #if USB_HAVE_TT_SUPPORT
1027 /* init FULL-speed ISOCHRONOUS schedule */
1028 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
1030 /* initialize HUB structure */
1032 hub->explore = &uhub_explore;
1033 hub->nports = nports;
1034 hub->hubudev = udev;
1036 /* if self powered hub, give ports maximum current */
1037 if (udev->flags.self_powered) {
1038 hub->portpower = USB_MAX_POWER;
1040 hub->portpower = USB_MIN_POWER;
1043 /* set up interrupt pipe */
1045 if (udev->parent_hub == NULL) {
1046 /* root HUB is special */
1050 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1051 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1054 DPRINTFN(0, "cannot setup interrupt transfer, "
1055 "errstr=%s\n", usbd_errstr(err));
1058 /* wait with power off for a while */
1059 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1062 * To have the best chance of success we do things in the exact same
1063 * order as Windoze98. This should not be necessary, but some
1064 * devices do not follow the USB specs to the letter.
1066 * These are the events on the bus when a hub is attached:
1067 * Get device and config descriptors (see attach code)
1068 * Get hub descriptor (see above)
1071 * wait for power to become stable
1072 * (all below happens in explore code)
1074 * clear C_PORT_CONNECTION
1077 * if device connected
1081 * clear C_PORT_RESET
1083 * proceed with device attachment
1086 /* XXX should check for none, individual, or ganged power? */
1090 for (x = 0; x != nports; x++) {
1091 /* set up data structures */
1092 struct usb_port *up = hub->ports + x;
1094 up->device_index = 0;
1098 /* check if port is removable */
1099 switch (udev->speed) {
1101 case USB_SPEED_FULL:
1102 case USB_SPEED_HIGH:
1103 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1106 case USB_SPEED_SUPER:
1107 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1111 DPRINTF("Assuming removable port\n");
1116 /* turn the power on */
1117 err = usbd_req_set_port_feature(udev, NULL,
1118 portno, UHF_PORT_POWER);
1121 DPRINTFN(0, "port %d power on failed, %s\n",
1122 portno, usbd_errstr(err));
1124 DPRINTF("turn on port %d power\n",
1127 /* wait for stable power */
1128 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1131 device_printf(dev, "%d port%s with %d "
1132 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1133 removable, udev->flags.self_powered ? "self" : "bus");
1135 /* Start the interrupt endpoint, if any */
1137 if (sc->sc_xfer[0] != NULL) {
1138 mtx_lock(&sc->sc_mtx);
1139 usbd_transfer_start(sc->sc_xfer[0]);
1140 mtx_unlock(&sc->sc_mtx);
1143 /* Enable automatic power save on all USB HUBs */
1145 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1150 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1153 free(udev->hub, M_USBDEV);
1157 mtx_destroy(&sc->sc_mtx);
1163 * Called from process context when the hub is gone.
1164 * Detach all devices on active ports.
1167 uhub_detach(device_t dev)
1169 struct uhub_softc *sc = device_get_softc(dev);
1170 struct usb_hub *hub = sc->sc_udev->hub;
1171 struct usb_device *child;
1174 if (hub == NULL) /* must be partially working */
1177 /* Make sure interrupt transfer is gone. */
1178 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1180 /* Detach all ports */
1181 for (x = 0; x != hub->nports; x++) {
1183 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1185 if (child == NULL) {
1190 * Free USB device and all subdevices, if any.
1192 usb_free_device(child, 0);
1195 free(hub, M_USBDEV);
1196 sc->sc_udev->hub = NULL;
1198 mtx_destroy(&sc->sc_mtx);
1204 uhub_suspend(device_t dev)
1207 /* Sub-devices are not suspended here! */
1212 uhub_resume(device_t dev)
1215 /* Sub-devices are not resumed here! */
1220 uhub_driver_added(device_t dev, driver_t *driver)
1222 usb_needs_explore_all();
1226 struct usb_device *udev;
1228 uint8_t iface_index;
1232 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1233 struct hub_result *res)
1235 struct usb_interface *iface;
1236 struct usb_device *udev;
1241 nports = hub->nports;
1242 for (x = 0; x != nports; x++) {
1243 udev = usb_bus_port_get_device(hub->hubudev->bus,
1248 for (i = 0; i != USB_IFACE_MAX; i++) {
1249 iface = usbd_get_iface(udev, i);
1251 (iface->subdev == child)) {
1252 res->iface_index = i;
1254 res->portno = x + 1;
1259 res->iface_index = 0;
1265 uhub_child_location_string(device_t parent, device_t child,
1266 char *buf, size_t buflen)
1268 struct uhub_softc *sc;
1269 struct usb_hub *hub;
1270 struct hub_result res;
1272 if (!device_is_attached(parent)) {
1278 sc = device_get_softc(parent);
1279 hub = sc->sc_udev->hub;
1282 uhub_find_iface_index(hub, child, &res);
1284 DPRINTF("device not on hub\n");
1290 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1291 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1292 res.portno, device_get_unit(res.udev->bus->bdev),
1293 res.udev->device_index, res.iface_index);
1301 uhub_child_pnpinfo_string(device_t parent, device_t child,
1302 char *buf, size_t buflen)
1304 struct uhub_softc *sc;
1305 struct usb_hub *hub;
1306 struct usb_interface *iface;
1307 struct hub_result res;
1309 if (!device_is_attached(parent)) {
1315 sc = device_get_softc(parent);
1316 hub = sc->sc_udev->hub;
1319 uhub_find_iface_index(hub, child, &res);
1321 DPRINTF("device not on hub\n");
1327 iface = usbd_get_iface(res.udev, res.iface_index);
1328 if (iface && iface->idesc) {
1329 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1330 "devclass=0x%02x devsubclass=0x%02x "
1333 "intclass=0x%02x intsubclass=0x%02x",
1334 UGETW(res.udev->ddesc.idVendor),
1335 UGETW(res.udev->ddesc.idProduct),
1336 res.udev->ddesc.bDeviceClass,
1337 res.udev->ddesc.bDeviceSubClass,
1338 usb_get_serial(res.udev),
1339 UGETW(res.udev->ddesc.bcdDevice),
1340 iface->idesc->bInterfaceClass,
1341 iface->idesc->bInterfaceSubClass);
1355 * The USB Transaction Translator:
1356 * ===============================
1358 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1359 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1360 * USB transfers. To utilize bandwidth dynamically the "scatter and
1361 * gather" principle must be applied. This means that bandwidth must
1362 * be divided into equal parts of bandwidth. With regard to USB all
1363 * data is transferred in smaller packets with length
1364 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1367 * The bandwidth scheduler which I have implemented will simply pack
1368 * the USB transfers back to back until there is no more space in the
1369 * schedule. Out of the 8 microframes which the USB 2.0 standard
1370 * provides, only 6 are available for non-HIGH-speed devices. I have
1371 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1372 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1373 * this division, it is very difficult to allocate and free bandwidth
1376 * NOTE about the Transaction Translator in USB HUBs:
1378 * USB HUBs have a very simple Transaction Translator, that will
1379 * simply pipeline all the SPLIT transactions. That means that the
1380 * transactions will be executed in the order they are queued!
1384 /*------------------------------------------------------------------------*
1385 * usb_intr_find_best_slot
1388 * The best Transaction Translation slot for an interrupt endpoint.
1389 *------------------------------------------------------------------------*/
1391 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1392 uint8_t end, uint8_t mask)
1394 usb_size_t min = 0 - 1;
1402 /* find the last slot with lesser used bandwidth */
1404 for (x = start; x < end; x++) {
1408 /* compute sum of bandwidth */
1409 for (z = x; z < end; z++) {
1410 if (mask & (1U << (z - x)))
1414 /* check if the current multi-slot is more optimal */
1420 /* check if the mask is about to be shifted out */
1421 if (mask & (1U << (end - 1 - x)))
1427 /*------------------------------------------------------------------------*
1428 * usb_hs_bandwidth_adjust
1430 * This function will update the bandwith usage for the microframe
1431 * having index "slot" by "len" bytes. "len" can be negative. If the
1432 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1433 * the "slot" argument will be replaced by the slot having least used
1434 * bandwidth. The "mask" argument is used for multi-slot allocations.
1437 * The slot in which the bandwidth update was done: 0..7
1438 *------------------------------------------------------------------------*/
1440 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1441 uint8_t slot, uint8_t mask)
1443 struct usb_bus *bus = udev->bus;
1444 struct usb_hub *hub;
1445 enum usb_dev_speed speed;
1448 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1450 speed = usbd_get_speed(udev);
1454 case USB_SPEED_FULL:
1455 if (speed == USB_SPEED_LOW) {
1459 * The Host Controller Driver should have
1460 * performed checks so that the lookup
1461 * below does not result in a NULL pointer
1465 hub = udev->parent_hs_hub->hub;
1466 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1467 slot = usb_intr_find_best_slot(hub->uframe_usage,
1468 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1470 for (x = slot; x < 8; x++) {
1471 if (mask & (1U << (x - slot))) {
1472 hub->uframe_usage[x] += len;
1473 bus->uframe_usage[x] += len;
1478 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1479 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1480 USB_HS_MICRO_FRAMES_MAX, mask);
1482 for (x = slot; x < 8; x++) {
1483 if (mask & (1U << (x - slot))) {
1484 bus->uframe_usage[x] += len;
1492 /*------------------------------------------------------------------------*
1493 * usb_hs_bandwidth_alloc
1495 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1496 *------------------------------------------------------------------------*/
1498 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1500 struct usb_device *udev;
1505 udev = xfer->xroot->udev;
1507 if (udev->flags.usb_mode != USB_MODE_HOST)
1508 return; /* not supported */
1510 xfer->endpoint->refcount_bw++;
1511 if (xfer->endpoint->refcount_bw != 1)
1512 return; /* already allocated */
1514 speed = usbd_get_speed(udev);
1516 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1518 /* allocate a microframe slot */
1521 slot = usb_hs_bandwidth_adjust(udev,
1522 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1524 xfer->endpoint->usb_uframe = slot;
1525 xfer->endpoint->usb_smask = mask << slot;
1527 if ((speed != USB_SPEED_FULL) &&
1528 (speed != USB_SPEED_LOW)) {
1529 xfer->endpoint->usb_cmask = 0x00 ;
1531 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1535 case UE_ISOCHRONOUS:
1536 switch (usbd_xfer_get_fps_shift(xfer)) {
1551 /* allocate a microframe multi-slot */
1553 slot = usb_hs_bandwidth_adjust(udev,
1554 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1556 xfer->endpoint->usb_uframe = slot;
1557 xfer->endpoint->usb_cmask = 0;
1558 xfer->endpoint->usb_smask = mask << slot;
1562 xfer->endpoint->usb_uframe = 0;
1563 xfer->endpoint->usb_cmask = 0;
1564 xfer->endpoint->usb_smask = 0;
1568 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1569 xfer->endpoint->usb_uframe,
1570 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1573 /*------------------------------------------------------------------------*
1574 * usb_hs_bandwidth_free
1576 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1577 *------------------------------------------------------------------------*/
1579 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1581 struct usb_device *udev;
1585 udev = xfer->xroot->udev;
1587 if (udev->flags.usb_mode != USB_MODE_HOST)
1588 return; /* not supported */
1590 xfer->endpoint->refcount_bw--;
1591 if (xfer->endpoint->refcount_bw != 0)
1592 return; /* still allocated */
1594 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1596 case UE_ISOCHRONOUS:
1598 slot = xfer->endpoint->usb_uframe;
1599 mask = xfer->endpoint->usb_smask;
1601 /* free microframe slot(s): */
1602 usb_hs_bandwidth_adjust(udev,
1603 -xfer->max_frame_size, slot, mask >> slot);
1605 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1606 slot, mask >> slot);
1608 xfer->endpoint->usb_uframe = 0;
1609 xfer->endpoint->usb_cmask = 0;
1610 xfer->endpoint->usb_smask = 0;
1618 /*------------------------------------------------------------------------*
1619 * usbd_fs_isoc_schedule_init_sub
1621 * This function initialises an USB FULL speed isochronous schedule
1623 *------------------------------------------------------------------------*/
1624 #if USB_HAVE_TT_SUPPORT
1626 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1628 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1629 USB_FS_BYTES_PER_HS_UFRAME);
1630 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1631 fss->frame_slot = 0;
1635 /*------------------------------------------------------------------------*
1636 * usbd_fs_isoc_schedule_init_all
1638 * This function will reset the complete USB FULL speed isochronous
1639 * bandwidth schedule.
1640 *------------------------------------------------------------------------*/
1641 #if USB_HAVE_TT_SUPPORT
1643 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1645 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1647 while (fss != fss_end) {
1648 usbd_fs_isoc_schedule_init_sub(fss);
1654 /*------------------------------------------------------------------------*
1655 * usb_isoc_time_expand
1657 * This function will expand the time counter from 7-bit to 16-bit.
1660 * 16-bit isochronous time counter.
1661 *------------------------------------------------------------------------*/
1663 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1667 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1669 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1671 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1673 if (isoc_time_curr < rem) {
1674 /* the time counter wrapped around */
1675 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1677 /* update the remainder */
1679 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1680 bus->isoc_time_last |= isoc_time_curr;
1682 return (bus->isoc_time_last);
1685 /*------------------------------------------------------------------------*
1686 * usbd_fs_isoc_schedule_isoc_time_expand
1688 * This function does multiple things. First of all it will expand the
1689 * passed isochronous time, which is the return value. Then it will
1690 * store where the current FULL speed isochronous schedule is
1691 * positioned in time and where the end is. See "pp_start" and
1692 * "pp_end" arguments.
1695 * Expanded version of "isoc_time".
1697 * NOTE: This function depends on being called regularly with
1698 * intervals less than "USB_ISOC_TIME_MAX".
1699 *------------------------------------------------------------------------*/
1700 #if USB_HAVE_TT_SUPPORT
1702 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1703 struct usb_fs_isoc_schedule **pp_start,
1704 struct usb_fs_isoc_schedule **pp_end,
1707 struct usb_fs_isoc_schedule *fss_end;
1708 struct usb_fs_isoc_schedule *fss_a;
1709 struct usb_fs_isoc_schedule *fss_b;
1710 struct usb_hub *hs_hub;
1712 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1714 hs_hub = udev->parent_hs_hub->hub;
1716 if (hs_hub != NULL) {
1718 fss_a = hs_hub->fs_isoc_schedule +
1719 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1721 hs_hub->isoc_last_time = isoc_time;
1723 fss_b = hs_hub->fs_isoc_schedule +
1724 (isoc_time % USB_ISOC_TIME_MAX);
1726 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1728 *pp_start = hs_hub->fs_isoc_schedule;
1731 while (fss_a != fss_b) {
1732 if (fss_a == fss_end) {
1733 fss_a = hs_hub->fs_isoc_schedule;
1736 usbd_fs_isoc_schedule_init_sub(fss_a);
1749 /*------------------------------------------------------------------------*
1750 * usbd_fs_isoc_schedule_alloc
1752 * This function will allocate bandwidth for an isochronous FULL speed
1753 * transaction in the FULL speed schedule. The microframe slot where
1754 * the transaction should be started is stored in the byte pointed to
1755 * by "pstart". The "len" argument specifies the length of the
1756 * transaction in bytes.
1761 *------------------------------------------------------------------------*/
1762 #if USB_HAVE_TT_SUPPORT
1764 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1765 uint8_t *pstart, uint16_t len)
1767 uint8_t slot = fss->frame_slot;
1769 /* Compute overhead and bit-stuffing */
1776 if (len > fss->total_bytes) {
1777 *pstart = 0; /* set some dummy value */
1778 return (1); /* error */
1782 fss->total_bytes -= len;
1784 while (len >= fss->frame_bytes) {
1785 len -= fss->frame_bytes;
1786 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1790 fss->frame_bytes -= len;
1793 return (0); /* success */
1797 /*------------------------------------------------------------------------*
1798 * usb_bus_port_get_device
1800 * This function is NULL safe.
1801 *------------------------------------------------------------------------*/
1803 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1805 if ((bus == NULL) || (up == NULL)) {
1809 if (up->device_index == 0) {
1813 return (bus->devices[up->device_index]);
1816 /*------------------------------------------------------------------------*
1817 * usb_bus_port_set_device
1819 * This function is NULL safe.
1820 *------------------------------------------------------------------------*/
1822 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1823 struct usb_device *udev, uint8_t device_index)
1830 * There is only one case where we don't
1831 * have an USB port, and that is the Root Hub!
1835 up->device_index = device_index;
1837 device_index = up->device_index;
1838 up->device_index = 0;
1842 * Make relationships to our new device
1844 if (device_index != 0) {
1846 mtx_lock(&usb_ref_lock);
1848 bus->devices[device_index] = udev;
1850 mtx_unlock(&usb_ref_lock);
1856 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1859 /*------------------------------------------------------------------------*
1862 * This functions is called when the USB event thread needs to run.
1863 *------------------------------------------------------------------------*/
1865 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1872 DPRINTF("No bus pointer!\n");
1875 if ((bus->devices == NULL) ||
1876 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1877 DPRINTF("No root HUB\n");
1880 if (mtx_owned(&bus->bus_mtx)) {
1889 if (usb_proc_msignal(&bus->explore_proc,
1890 &bus->explore_msg[0], &bus->explore_msg[1])) {
1894 USB_BUS_UNLOCK(bus);
1898 /*------------------------------------------------------------------------*
1899 * usb_needs_explore_all
1901 * This function is called whenever a new driver is loaded and will
1902 * cause that all USB busses are re-explored.
1903 *------------------------------------------------------------------------*/
1905 usb_needs_explore_all(void)
1907 struct usb_bus *bus;
1914 dc = usb_devclass_ptr;
1916 DPRINTFN(0, "no devclass\n");
1920 * Explore all USB busses in parallell.
1922 max = devclass_get_maxunit(dc);
1924 dev = devclass_get_device(dc, max);
1926 bus = device_get_softc(dev);
1928 usb_needs_explore(bus, 1);
1935 /*------------------------------------------------------------------------*
1936 * usb_bus_power_update
1938 * This function will ensure that all USB devices on the given bus are
1939 * properly suspended or resumed according to the device transfer
1941 *------------------------------------------------------------------------*/
1944 usb_bus_power_update(struct usb_bus *bus)
1946 usb_needs_explore(bus, 0 /* no probe */ );
1950 /*------------------------------------------------------------------------*
1951 * usbd_transfer_power_ref
1953 * This function will modify the power save reference counts and
1954 * wakeup the USB device associated with the given USB transfer, if
1956 *------------------------------------------------------------------------*/
1959 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1961 static const usb_power_mask_t power_mask[4] = {
1962 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1963 [UE_BULK] = USB_HW_POWER_BULK,
1964 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1965 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1967 struct usb_device *udev;
1968 uint8_t needs_explore;
1969 uint8_t needs_hw_power;
1972 udev = xfer->xroot->udev;
1974 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1975 /* no power save for root HUB */
1978 USB_BUS_LOCK(udev->bus);
1980 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1982 udev->pwr_save.last_xfer_time = ticks;
1983 udev->pwr_save.type_refs[xfer_type] += val;
1985 if (xfer->flags_int.control_xfr) {
1986 udev->pwr_save.read_refs += val;
1987 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1989 * It is not allowed to suspend during a
1992 udev->pwr_save.write_refs += val;
1994 } else if (USB_GET_DATA_ISREAD(xfer)) {
1995 udev->pwr_save.read_refs += val;
1997 udev->pwr_save.write_refs += val;
2001 if (udev->flags.self_suspended)
2002 needs_explore = usb_peer_should_wakeup(udev);
2006 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2007 DPRINTF("Adding type %u to power state\n", xfer_type);
2008 udev->bus->hw_power_state |= power_mask[xfer_type];
2018 USB_BUS_UNLOCK(udev->bus);
2020 if (needs_explore) {
2021 DPRINTF("update\n");
2022 usb_bus_power_update(udev->bus);
2023 } else if (needs_hw_power) {
2024 DPRINTF("needs power\n");
2025 if (udev->bus->methods->set_hw_power != NULL) {
2026 (udev->bus->methods->set_hw_power) (udev->bus);
2032 /*------------------------------------------------------------------------*
2033 * usb_peer_should_wakeup
2035 * This function returns non-zero if the current device should wake up.
2036 *------------------------------------------------------------------------*/
2038 usb_peer_should_wakeup(struct usb_device *udev)
2040 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2041 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2042 (udev->re_enumerate_wait != 0) ||
2043 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2044 (udev->pwr_save.write_refs != 0) ||
2045 ((udev->pwr_save.read_refs != 0) &&
2046 (udev->flags.usb_mode == USB_MODE_HOST) &&
2047 (usb_device_20_compatible(udev) != 0) &&
2048 (usb_peer_can_wakeup(udev) == 0)));
2051 /*------------------------------------------------------------------------*
2054 * This function implements the USB power daemon and is called
2055 * regularly from the USB explore thread.
2056 *------------------------------------------------------------------------*/
2059 usb_bus_powerd(struct usb_bus *bus)
2061 struct usb_device *udev;
2064 usb_ticks_t mintime;
2065 usb_size_t type_refs[5];
2068 limit = usb_power_timeout;
2071 else if (limit > 255)
2076 DPRINTF("bus=%p\n", bus);
2081 * The root HUB device is never suspended
2082 * and we simply skip it.
2084 for (x = USB_ROOT_HUB_ADDR + 1;
2085 x != bus->devices_max; x++) {
2087 udev = bus->devices[x];
2091 temp = ticks - udev->pwr_save.last_xfer_time;
2093 if (usb_peer_should_wakeup(udev)) {
2094 /* check if we are suspended */
2095 if (udev->flags.self_suspended != 0) {
2096 USB_BUS_UNLOCK(bus);
2097 usb_dev_resume_peer(udev);
2100 } else if ((temp >= limit) &&
2101 (udev->flags.usb_mode == USB_MODE_HOST) &&
2102 (udev->flags.self_suspended == 0)) {
2103 /* try to do suspend */
2105 USB_BUS_UNLOCK(bus);
2106 usb_dev_suspend_peer(udev);
2111 /* reset counters */
2120 /* Re-loop all the devices to get the actual state */
2122 for (x = USB_ROOT_HUB_ADDR + 1;
2123 x != bus->devices_max; x++) {
2125 udev = bus->devices[x];
2129 /* we found a non-Root-Hub USB device */
2132 /* "last_xfer_time" can be updated by a resume */
2133 temp = ticks - udev->pwr_save.last_xfer_time;
2136 * Compute minimum time since last transfer for the complete
2142 if (udev->flags.self_suspended == 0) {
2143 type_refs[0] += udev->pwr_save.type_refs[0];
2144 type_refs[1] += udev->pwr_save.type_refs[1];
2145 type_refs[2] += udev->pwr_save.type_refs[2];
2146 type_refs[3] += udev->pwr_save.type_refs[3];
2150 if (mintime >= (1 * hz)) {
2151 /* recompute power masks */
2152 DPRINTF("Recomputing power masks\n");
2153 bus->hw_power_state = 0;
2154 if (type_refs[UE_CONTROL] != 0)
2155 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2156 if (type_refs[UE_BULK] != 0)
2157 bus->hw_power_state |= USB_HW_POWER_BULK;
2158 if (type_refs[UE_INTERRUPT] != 0)
2159 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2160 if (type_refs[UE_ISOCHRONOUS] != 0)
2161 bus->hw_power_state |= USB_HW_POWER_ISOC;
2162 if (type_refs[4] != 0)
2163 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2165 USB_BUS_UNLOCK(bus);
2167 if (bus->methods->set_hw_power != NULL) {
2168 /* always update hardware power! */
2169 (bus->methods->set_hw_power) (bus);
2175 /*------------------------------------------------------------------------*
2176 * usb_dev_resume_peer
2178 * This function will resume an USB peer and do the required USB
2179 * signalling to get an USB device out of the suspended state.
2180 *------------------------------------------------------------------------*/
2182 usb_dev_resume_peer(struct usb_device *udev)
2184 struct usb_bus *bus;
2191 /* check if already resumed */
2192 if (udev->flags.self_suspended == 0)
2195 /* we need a parent HUB to do resume */
2196 if (udev->parent_hub == NULL)
2199 DPRINTF("udev=%p\n", udev);
2201 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2202 (udev->flags.remote_wakeup == 0)) {
2204 * If the host did not set the remote wakeup feature, we can
2205 * not wake it up either!
2207 DPRINTF("remote wakeup is not set!\n");
2210 /* get bus pointer */
2213 /* resume parent hub first */
2214 usb_dev_resume_peer(udev->parent_hub);
2216 /* reduce chance of instant resume failure by waiting a little bit */
2217 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2219 if (usb_device_20_compatible(udev)) {
2220 /* resume current port (Valid in Host and Device Mode) */
2221 err = usbd_req_clear_port_feature(udev->parent_hub,
2222 NULL, udev->port_no, UHF_PORT_SUSPEND);
2224 DPRINTFN(0, "Resuming port failed\n");
2229 /* resume settle time */
2230 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2232 if (bus->methods->device_resume != NULL) {
2233 /* resume USB device on the USB controller */
2234 (bus->methods->device_resume) (udev);
2237 /* set that this device is now resumed */
2238 udev->flags.self_suspended = 0;
2240 /* make sure that we don't go into suspend right away */
2241 udev->pwr_save.last_xfer_time = ticks;
2243 /* make sure the needed power masks are on */
2244 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2245 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2246 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2247 bus->hw_power_state |= USB_HW_POWER_BULK;
2248 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2249 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2250 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2251 bus->hw_power_state |= USB_HW_POWER_ISOC;
2253 USB_BUS_UNLOCK(bus);
2255 if (bus->methods->set_hw_power != NULL) {
2256 /* always update hardware power! */
2257 (bus->methods->set_hw_power) (bus);
2262 /* notify all sub-devices about resume */
2263 err = usb_suspend_resume(udev, 0);
2265 usbd_sr_unlock(udev);
2267 /* check if peer has wakeup capability */
2268 if (usb_peer_can_wakeup(udev) &&
2269 usb_device_20_compatible(udev)) {
2270 /* clear remote wakeup */
2271 err = usbd_req_clear_device_feature(udev,
2272 NULL, UF_DEVICE_REMOTE_WAKEUP);
2274 DPRINTFN(0, "Clearing device "
2275 "remote wakeup failed: %s\n",
2281 /*------------------------------------------------------------------------*
2282 * usb_dev_suspend_peer
2284 * This function will suspend an USB peer and do the required USB
2285 * signalling to get an USB device into the suspended state.
2286 *------------------------------------------------------------------------*/
2288 usb_dev_suspend_peer(struct usb_device *udev)
2290 struct usb_device *child;
2300 /* check if already suspended */
2301 if (udev->flags.self_suspended)
2304 /* we need a parent HUB to do suspend */
2305 if (udev->parent_hub == NULL)
2308 DPRINTF("udev=%p\n", udev);
2310 /* check if the current device is a HUB */
2311 if (udev->hub != NULL) {
2312 nports = udev->hub->nports;
2314 /* check if all devices on the HUB are suspended */
2315 for (x = 0; x != nports; x++) {
2316 child = usb_bus_port_get_device(udev->bus,
2317 udev->hub->ports + x);
2322 if (child->flags.self_suspended)
2325 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2330 if (usb_peer_can_wakeup(udev) &&
2331 usb_device_20_compatible(udev)) {
2333 * This request needs to be done before we set
2334 * "udev->flags.self_suspended":
2337 /* allow device to do remote wakeup */
2338 err = usbd_req_set_device_feature(udev,
2339 NULL, UF_DEVICE_REMOTE_WAKEUP);
2341 DPRINTFN(0, "Setting device "
2342 "remote wakeup failed\n");
2346 USB_BUS_LOCK(udev->bus);
2348 * Checking for suspend condition and setting suspended bit
2351 err = usb_peer_should_wakeup(udev);
2354 * Set that this device is suspended. This variable
2355 * must be set before calling USB controller suspend
2358 udev->flags.self_suspended = 1;
2360 USB_BUS_UNLOCK(udev->bus);
2363 if (usb_peer_can_wakeup(udev) &&
2364 usb_device_20_compatible(udev)) {
2365 /* allow device to do remote wakeup */
2366 err = usbd_req_clear_device_feature(udev,
2367 NULL, UF_DEVICE_REMOTE_WAKEUP);
2369 DPRINTFN(0, "Setting device "
2370 "remote wakeup failed\n");
2374 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2375 /* resume parent HUB first */
2376 usb_dev_resume_peer(udev->parent_hub);
2378 /* reduce chance of instant resume failure by waiting a little bit */
2379 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2381 /* resume current port (Valid in Host and Device Mode) */
2382 err = usbd_req_clear_port_feature(udev->parent_hub,
2383 NULL, udev->port_no, UHF_PORT_SUSPEND);
2385 /* resume settle time */
2386 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2388 DPRINTF("Suspend was cancelled!\n");
2394 /* notify all sub-devices about suspend */
2395 err = usb_suspend_resume(udev, 1);
2397 usbd_sr_unlock(udev);
2399 if (udev->bus->methods->device_suspend != NULL) {
2402 /* suspend device on the USB controller */
2403 (udev->bus->methods->device_suspend) (udev);
2406 temp = usbd_get_dma_delay(udev);
2408 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2412 if (usb_device_20_compatible(udev)) {
2413 /* suspend current port */
2414 err = usbd_req_set_port_feature(udev->parent_hub,
2415 NULL, udev->port_no, UHF_PORT_SUSPEND);
2417 DPRINTFN(0, "Suspending port failed\n");
2422 udev = udev->parent_hub;
2426 /*------------------------------------------------------------------------*
2427 * usbd_set_power_mode
2429 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2431 *------------------------------------------------------------------------*/
2433 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2435 /* filter input argument */
2436 if ((power_mode != USB_POWER_MODE_ON) &&
2437 (power_mode != USB_POWER_MODE_OFF))
2438 power_mode = USB_POWER_MODE_SAVE;
2440 power_mode = usbd_filter_power_mode(udev, power_mode);
2442 udev->power_mode = power_mode; /* update copy of power mode */
2445 usb_bus_power_update(udev->bus);
2449 /*------------------------------------------------------------------------*
2450 * usbd_filter_power_mode
2452 * This function filters the power mode based on hardware requirements.
2453 *------------------------------------------------------------------------*/
2455 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2457 struct usb_bus_methods *mtod;
2460 mtod = udev->bus->methods;
2463 if (mtod->get_power_mode != NULL)
2464 (mtod->get_power_mode) (udev, &temp);
2466 /* check if we should not filter */
2468 return (power_mode);
2470 /* use fixed power mode given by hardware driver */