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/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usb_ioctl.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usbdi_util.h>
55 #define USB_DEBUG_VAR uhub_debug
57 #include <bus/u4b/usb_core.h>
58 #include <bus/u4b/usb_process.h>
59 #include <bus/u4b/usb_device.h>
60 #include <bus/u4b/usb_request.h>
61 #include <bus/u4b/usb_debug.h>
62 #include <bus/u4b/usb_hub.h>
63 #include <bus/u4b/usb_util.h>
64 #include <bus/u4b/usb_busdma.h>
65 #include <bus/u4b/usb_transfer.h>
66 #include <bus/u4b/usb_dynamic.h>
68 #include <bus/u4b/usb_controller.h>
69 #include <bus/u4b/usb_bus.h>
71 #define UHUB_INTR_INTERVAL 250 /* ms */
72 #define UHUB_N_TRANSFER 1
75 static int uhub_debug = 0;
77 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
78 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
81 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
85 static int usb_power_timeout = 30; /* seconds */
87 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
88 &usb_power_timeout, 0, "USB power timeout");
91 struct uhub_current_state {
97 struct uhub_current_state sc_st;/* current state */
98 device_t sc_dev; /* base device */
99 struct lock sc_lock; /* our mutex */
100 struct usb_device *sc_udev; /* USB device */
101 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
103 #define UHUB_FLAG_DID_EXPLORE 0x01
107 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
108 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
109 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
110 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
112 /* prototypes for type checking: */
114 static device_probe_t uhub_probe;
115 static device_attach_t uhub_attach;
116 static device_detach_t uhub_detach;
117 static device_suspend_t uhub_suspend;
118 static device_resume_t uhub_resume;
120 static bus_driver_added_t uhub_driver_added;
121 static bus_child_location_str_t uhub_child_location_string;
122 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
124 static usb_callback_t uhub_intr_callback;
126 static void usb_dev_resume_peer(struct usb_device *udev);
127 static void usb_dev_suspend_peer(struct usb_device *udev);
128 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
130 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
133 .type = UE_INTERRUPT,
134 .endpoint = UE_ADDR_ANY,
135 .direction = UE_DIR_ANY,
137 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
138 .bufsize = 0, /* use wMaxPacketSize */
139 .callback = &uhub_intr_callback,
140 .interval = UHUB_INTR_INTERVAL,
145 * driver instance for "hub" connected to "usb"
146 * and "hub" connected to "hub"
148 static devclass_t uhub_devclass;
150 static device_method_t uhub_methods[] = {
151 DEVMETHOD(device_probe, uhub_probe),
152 DEVMETHOD(device_attach, uhub_attach),
153 DEVMETHOD(device_detach, uhub_detach),
155 DEVMETHOD(device_suspend, uhub_suspend),
156 DEVMETHOD(device_resume, uhub_resume),
158 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
159 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
160 DEVMETHOD(bus_driver_added, uhub_driver_added),
164 static driver_t uhub_driver = {
166 .methods = uhub_methods,
167 .size = sizeof(struct uhub_softc)
170 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
171 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
172 MODULE_VERSION(uhub, 1);
175 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
177 struct uhub_softc *sc = usbd_xfer_softc(xfer);
179 switch (USB_GET_STATE(xfer)) {
180 case USB_ST_TRANSFERRED:
183 * This is an indication that some port
184 * has changed status. Notify the bus
185 * event handler thread that we need
186 * to be explored again:
188 usb_needs_explore(sc->sc_udev->bus, 0);
191 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
192 usbd_transfer_submit(xfer);
196 if (xfer->error != USB_ERR_CANCELLED) {
198 * Do a clear-stall. The "stall_pipe" flag
199 * will get cleared before next callback by
202 usbd_xfer_set_stall(xfer);
203 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
204 usbd_transfer_submit(xfer);
210 /*------------------------------------------------------------------------*
211 * uhub_explore_sub - subroutine
215 * Else: A control transaction failed
216 *------------------------------------------------------------------------*/
218 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
221 struct usb_device *child;
225 bus = sc->sc_udev->bus;
228 /* get driver added refcount from USB bus */
229 refcount = bus->driver_added_refcount;
231 /* get device assosiated with the given port */
232 child = usb_bus_port_get_device(bus, up);
238 /* check if device should be re-enumerated */
240 if (child->flags.usb_mode == USB_MODE_HOST) {
241 usbd_enum_lock(child);
242 if (child->re_enumerate_wait) {
243 err = usbd_set_config_index(child,
246 DPRINTF("Unconfigure failed: "
250 err = usbd_req_re_enumerate(child, NULL);
252 err = usbd_set_config_index(child, 0);
254 err = usb_probe_and_attach(child,
255 USB_IFACE_INDEX_ANY);
257 child->re_enumerate_wait = 0;
260 usbd_enum_unlock(child);
263 /* check if probe and attach should be done */
265 if (child->driver_added_refcount != refcount) {
266 child->driver_added_refcount = refcount;
267 err = usb_probe_and_attach(child,
268 USB_IFACE_INDEX_ANY);
273 /* start control transfer, if device mode */
275 if (child->flags.usb_mode == USB_MODE_DEVICE)
276 usbd_ctrl_transfer_setup(child);
278 /* if a HUB becomes present, do a recursive HUB explore */
281 err = (child->hub->explore) (child);
287 /*------------------------------------------------------------------------*
288 * uhub_read_port_status - factored out code
289 *------------------------------------------------------------------------*/
291 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
293 struct usb_port_status ps;
296 err = usbd_req_get_port_status(
297 sc->sc_udev, NULL, &ps, portno);
299 /* update status regardless of error */
301 sc->sc_st.port_status = UGETW(ps.wPortStatus);
302 sc->sc_st.port_change = UGETW(ps.wPortChange);
304 /* debugging print */
306 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
307 "wPortChange=0x%04x, err=%s\n",
308 portno, sc->sc_st.port_status,
309 sc->sc_st.port_change, usbd_errstr(err));
313 /*------------------------------------------------------------------------*
318 * Else: A control transaction failed
319 *------------------------------------------------------------------------*/
321 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
323 struct usb_device *child;
324 struct usb_device *udev;
325 enum usb_dev_speed speed;
326 enum usb_hc_mode mode;
331 DPRINTF("reattaching port %d\n", portno);
336 child = usb_bus_port_get_device(udev->bus,
337 udev->hub->ports + portno - 1);
341 /* first clear the port connection change bit */
343 err = usbd_req_clear_port_feature(udev, NULL,
344 portno, UHF_C_PORT_CONNECTION);
349 /* check if there is a child */
353 * Free USB device and all subdevices, if any.
355 usb_free_device(child, 0);
358 /* get fresh status */
360 err = uhub_read_port_status(sc, portno);
364 /* check if nothing is connected to the port */
366 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
369 /* check if there is no power on the port and print a warning */
371 switch (udev->speed) {
375 power_mask = UPS_PORT_POWER;
377 case USB_SPEED_SUPER:
378 if (udev->parent_hub == NULL)
379 power_mask = UPS_PORT_POWER;
381 power_mask = UPS_PORT_POWER_SS;
387 if (!(sc->sc_st.port_status & power_mask)) {
388 DPRINTF("WARNING: strange, connected port %d "
389 "has no power\n", portno);
392 /* check if the device is in Host Mode */
394 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
396 DPRINTF("Port %d is in Host Mode\n", portno);
398 if (sc->sc_st.port_status & UPS_SUSPEND) {
400 * NOTE: Should not get here in SuperSpeed
401 * mode, because the HUB should report this
404 DPRINTF("Port %d was still "
405 "suspended, clearing.\n", portno);
406 err = usbd_req_clear_port_feature(udev,
407 NULL, portno, UHF_PORT_SUSPEND);
412 /* wait for maximum device power up time */
415 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
417 /* reset port, which implies enabling it */
419 err = usbd_req_reset_port(udev, NULL, portno);
422 DPRINTFN(0, "port %d reset "
423 "failed, error=%s\n",
424 portno, usbd_errstr(err));
427 /* get port status again, it might have changed during reset */
429 err = uhub_read_port_status(sc, portno);
433 /* check if something changed during port reset */
435 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
436 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
438 DPRINTFN(0, "giving up port reset "
439 "- device vanished\n");
446 DPRINTF("Port %d is in Device Mode\n", portno);
450 * Figure out the device speed
452 switch (udev->speed) {
454 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
455 speed = USB_SPEED_HIGH;
456 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
457 speed = USB_SPEED_LOW;
459 speed = USB_SPEED_FULL;
462 if (sc->sc_st.port_status & UPS_LOW_SPEED)
463 speed = USB_SPEED_LOW;
465 speed = USB_SPEED_FULL;
468 speed = USB_SPEED_LOW;
470 case USB_SPEED_SUPER:
471 if (udev->parent_hub == NULL) {
472 /* Root HUB - special case */
473 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
475 speed = USB_SPEED_FULL;
478 speed = USB_SPEED_LOW;
481 speed = USB_SPEED_HIGH;
484 speed = USB_SPEED_SUPER;
488 speed = USB_SPEED_SUPER;
492 /* same speed like parent */
496 if (speed == USB_SPEED_SUPER) {
497 err = usbd_req_set_hub_u1_timeout(udev, NULL,
498 portno, 128 - (2 * udev->depth));
500 DPRINTFN(0, "port %d U1 timeout "
501 "failed, error=%s\n",
502 portno, usbd_errstr(err));
504 err = usbd_req_set_hub_u2_timeout(udev, NULL,
505 portno, 128 - (2 * udev->depth));
507 DPRINTFN(0, "port %d U2 timeout "
508 "failed, error=%s\n",
509 portno, usbd_errstr(err));
514 * Figure out the device mode
516 * NOTE: This part is currently FreeBSD specific.
518 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
519 mode = USB_MODE_DEVICE;
521 mode = USB_MODE_HOST;
523 /* need to create a new child */
524 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
525 udev->depth + 1, portno - 1, portno, speed, mode);
527 DPRINTFN(0, "could not allocate new device\n");
530 return (0); /* success */
535 * Free USB device and all subdevices, if any.
537 usb_free_device(child, 0);
541 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
542 err = usbd_req_clear_port_feature(
544 portno, UHF_PORT_ENABLE);
548 DPRINTFN(0, "device problem (%s), "
549 "disabling port %d\n", usbd_errstr(err), portno);
554 /*------------------------------------------------------------------------*
555 * usb_device_20_compatible
558 * 0: HUB does not support suspend and resume
559 * Else: HUB supports suspend and resume
560 *------------------------------------------------------------------------*/
562 usb_device_20_compatible(struct usb_device *udev)
566 switch (udev->speed) {
576 /*------------------------------------------------------------------------*
577 * uhub_suspend_resume_port
581 * Else: A control transaction failed
582 *------------------------------------------------------------------------*/
584 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
586 struct usb_device *child;
587 struct usb_device *udev;
591 DPRINTF("port %d\n", portno);
594 child = usb_bus_port_get_device(udev->bus,
595 udev->hub->ports + portno - 1);
597 /* first clear the port suspend change bit */
599 if (usb_device_20_compatible(udev)) {
600 err = usbd_req_clear_port_feature(udev, NULL,
601 portno, UHF_C_PORT_SUSPEND);
603 err = usbd_req_clear_port_feature(udev, NULL,
604 portno, UHF_C_PORT_LINK_STATE);
608 DPRINTF("clearing suspend failed.\n");
611 /* get fresh status */
613 err = uhub_read_port_status(sc, portno);
615 DPRINTF("reading port status failed.\n");
618 /* convert current state */
620 if (usb_device_20_compatible(udev)) {
621 if (sc->sc_st.port_status & UPS_SUSPEND) {
627 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
631 case UPS_PORT_LS_SS_INA:
632 usbd_req_warm_reset_port(udev, NULL, portno);
641 DPRINTF("suspended=%u\n", is_suspend);
643 /* do the suspend or resume */
647 * This code handle two cases: 1) Host Mode - we can only
648 * receive resume here 2) Device Mode - we can receive
649 * suspend and resume here
652 usb_dev_resume_peer(child);
653 else if (child->flags.usb_mode == USB_MODE_DEVICE)
654 usb_dev_suspend_peer(child);
660 /*------------------------------------------------------------------------*
661 * uhub_root_interrupt
663 * This function is called when a Root HUB interrupt has
664 * happened. "ptr" and "len" makes up the Root HUB interrupt
665 * packet. This function is called having the "bus_mtx" locked.
666 *------------------------------------------------------------------------*/
668 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
670 USB_BUS_LOCK_ASSERT(bus);
672 usb_needs_explore(bus, 0);
676 uhub_is_too_deep(struct usb_device *udev)
678 switch (udev->speed) {
682 if (udev->depth > USB_HUB_MAX_DEPTH)
685 case USB_SPEED_SUPER:
686 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
695 /*------------------------------------------------------------------------*
701 *------------------------------------------------------------------------*/
703 uhub_explore(struct usb_device *udev)
706 struct uhub_softc *sc;
715 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
717 /* ignore devices that are too deep */
718 if (uhub_is_too_deep(udev))
719 return (USB_ERR_TOO_DEEP);
721 /* check if device is suspended */
722 if (udev->flags.self_suspended) {
723 /* need to wait until the child signals resume */
724 DPRINTF("Device is suspended!\n");
729 * Make sure we don't race against user-space applications
732 usbd_enum_lock(udev);
734 for (x = 0; x != hub->nports; x++) {
738 err = uhub_read_port_status(sc, portno);
740 /* most likely the HUB is gone */
743 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
744 DPRINTF("Overcurrent on port %u.\n", portno);
745 err = usbd_req_clear_port_feature(
746 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
748 /* most likely the HUB is gone */
752 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
754 * Fake a connect status change so that the
755 * status gets checked initially!
757 sc->sc_st.port_change |=
758 UPS_C_CONNECT_STATUS;
760 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
761 err = usbd_req_clear_port_feature(
762 udev, NULL, portno, UHF_C_PORT_ENABLE);
764 /* most likely the HUB is gone */
767 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
769 * Ignore the port error if the device
772 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
773 DPRINTFN(0, "illegal enable change, "
774 "port %d\n", portno);
777 if (up->restartcnt == USB_RESTART_MAX) {
778 /* XXX could try another speed ? */
779 DPRINTFN(0, "port error, giving up "
780 "port %d\n", portno);
782 sc->sc_st.port_change |=
783 UPS_C_CONNECT_STATUS;
788 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
789 err = uhub_reattach_port(sc, portno);
791 /* most likely the HUB is gone */
795 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
796 UPS_C_PORT_LINK_STATE)) {
797 err = uhub_suspend_resume_port(sc, portno);
799 /* most likely the HUB is gone */
803 err = uhub_explore_sub(sc, up);
805 /* no device(s) present */
808 /* explore succeeded - reset restart counter */
812 usbd_enum_unlock(udev);
814 /* initial status checked */
815 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
818 return (USB_ERR_NORMAL_COMPLETION);
822 uhub_probe(device_t dev)
824 struct usb_attach_arg *uaa = device_get_ivars(dev);
826 if (uaa->usb_mode != USB_MODE_HOST)
830 * The subclass for USB HUBs is currently ignored because it
831 * is 0 for some and 1 for others.
833 if (uaa->info.bConfigIndex == 0 &&
834 uaa->info.bDeviceClass == UDCLASS_HUB)
840 /* NOTE: The information returned by this function can be wrong. */
842 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
844 struct usb_hub_descriptor hubdesc20;
845 struct usb_hub_ss_descriptor hubdesc30;
850 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
851 return (USB_ERR_INVAL);
856 switch (udev->speed) {
860 /* assuming that there is one port */
861 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
863 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
864 "error=%s\n", usbd_errstr(err));
867 nports = hubdesc20.bNbrPorts;
871 if (udev->speed == USB_SPEED_HIGH)
872 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
875 case USB_SPEED_SUPER:
876 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
878 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
879 "error=%s\n", usbd_errstr(err));
882 nports = hubdesc30.bNbrPorts;
902 uhub_attach(device_t dev)
904 struct uhub_softc *sc = device_get_softc(dev);
905 struct usb_attach_arg *uaa = device_get_ivars(dev);
906 struct usb_device *udev = uaa->device;
907 struct usb_device *parent_hub = udev->parent_hub;
909 struct usb_hub_descriptor hubdesc20;
910 struct usb_hub_ss_descriptor hubdesc30;
922 lockinit(&sc->sc_lock, "USB HUB mutex", 0, 0);
925 ksnprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
926 device_get_nameunit(dev));
928 device_set_usb_desc(dev);
930 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
931 "parent->selfpowered=%d\n",
933 udev->flags.self_powered,
936 parent_hub->flags.self_powered : 0);
938 if (uhub_is_too_deep(udev)) {
939 DPRINTFN(0, "HUB at depth %d, "
940 "exceeds maximum. HUB ignored\n", (int)udev->depth);
944 if (!udev->flags.self_powered && parent_hub &&
945 !parent_hub->flags.self_powered) {
946 DPRINTFN(0, "Bus powered HUB connected to "
947 "bus powered HUB. HUB ignored\n");
950 /* get HUB descriptor */
952 DPRINTFN(2, "Getting HUB descriptor\n");
954 switch (udev->speed) {
958 /* assuming that there is one port */
959 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
961 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
962 "error=%s\n", usbd_errstr(err));
965 /* get number of ports */
966 nports = hubdesc20.bNbrPorts;
968 /* get power delay */
969 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
970 USB_EXTRA_POWER_UP_TIME);
972 /* get complete HUB descriptor */
974 /* check number of ports */
976 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
977 "error=%s\n", usbd_errstr(err));
980 /* get complete HUB descriptor */
981 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
984 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
985 "error=%s\n", usbd_errstr(err));
988 if (hubdesc20.bNbrPorts != nports) {
989 DPRINTFN(0, "Number of ports changed\n");
994 case USB_SPEED_SUPER:
995 if (udev->parent_hub != NULL) {
996 err = usbd_req_set_hub_depth(udev, NULL,
999 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1000 "error=%s\n", usbd_errstr(err));
1004 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1006 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1007 "error=%s\n", usbd_errstr(err));
1010 /* get number of ports */
1011 nports = hubdesc30.bNbrPorts;
1013 /* get power delay */
1014 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1015 USB_EXTRA_POWER_UP_TIME);
1017 /* get complete HUB descriptor */
1019 /* check number of ports */
1020 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1021 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1022 "error=%s\n", usbd_errstr(err));
1025 /* get complete HUB descriptor */
1026 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1029 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1030 "error=%s\n", usbd_errstr(err));
1033 if (hubdesc30.bNbrPorts != nports) {
1034 DPRINTFN(0, "Number of ports changed\n");
1040 DPRINTF("Assuming HUB has only one port\n");
1041 /* default number of ports */
1043 /* default power delay */
1044 pwrdly = ((10 * UHD_PWRON_FACTOR) + USB_EXTRA_POWER_UP_TIME);
1048 DPRINTFN(0, "portless HUB\n");
1051 hub = kmalloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1052 M_USBDEV, M_WAITOK | M_ZERO);
1059 #if USB_HAVE_TT_SUPPORT
1060 /* init FULL-speed ISOCHRONOUS schedule */
1061 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
1063 /* initialize HUB structure */
1065 hub->explore = &uhub_explore;
1066 hub->nports = nports;
1067 hub->hubudev = udev;
1069 /* if self powered hub, give ports maximum current */
1070 if (udev->flags.self_powered) {
1071 hub->portpower = USB_MAX_POWER;
1073 hub->portpower = USB_MIN_POWER;
1076 /* set up interrupt pipe */
1078 if (udev->parent_hub == NULL) {
1079 /* root HUB is special */
1083 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1084 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_lock);
1087 DPRINTFN(0, "cannot setup interrupt transfer, "
1088 "errstr=%s\n", usbd_errstr(err));
1091 /* wait with power off for a while */
1092 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1095 * To have the best chance of success we do things in the exact same
1096 * order as Windoze98. This should not be necessary, but some
1097 * devices do not follow the USB specs to the letter.
1099 * These are the events on the bus when a hub is attached:
1100 * Get device and config descriptors (see attach code)
1101 * Get hub descriptor (see above)
1104 * wait for power to become stable
1105 * (all below happens in explore code)
1107 * clear C_PORT_CONNECTION
1110 * if device connected
1114 * clear C_PORT_RESET
1116 * proceed with device attachment
1119 /* XXX should check for none, individual, or ganged power? */
1123 for (x = 0; x != nports; x++) {
1124 /* set up data structures */
1125 struct usb_port *up = hub->ports + x;
1127 up->device_index = 0;
1131 /* check if port is removable */
1132 switch (udev->speed) {
1134 case USB_SPEED_FULL:
1135 case USB_SPEED_HIGH:
1136 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1139 case USB_SPEED_SUPER:
1140 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1144 DPRINTF("Assuming removable port\n");
1149 /* turn the power on */
1150 err = usbd_req_set_port_feature(udev, NULL,
1151 portno, UHF_PORT_POWER);
1154 DPRINTFN(0, "port %d power on failed, %s\n",
1155 portno, usbd_errstr(err));
1157 DPRINTF("turn on port %d power\n",
1160 /* wait for stable power */
1161 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1164 device_printf(dev, "%d port%s with %d "
1165 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1166 removable, udev->flags.self_powered ? "self" : "bus");
1168 /* Start the interrupt endpoint, if any */
1170 if (sc->sc_xfer[0] != NULL) {
1171 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1172 usbd_transfer_start(sc->sc_xfer[0]);
1173 lockmgr(&sc->sc_lock, LK_RELEASE);
1176 /* Enable automatic power save on all USB HUBs */
1178 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1183 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1186 kfree(udev->hub, M_USBDEV);
1190 lockuninit(&sc->sc_lock);
1196 * Called from process context when the hub is gone.
1197 * Detach all devices on active ports.
1200 uhub_detach(device_t dev)
1202 struct uhub_softc *sc = device_get_softc(dev);
1203 struct usb_hub *hub = sc->sc_udev->hub;
1204 struct usb_device *child;
1207 if (hub == NULL) /* must be partially working */
1210 /* Make sure interrupt transfer is gone. */
1211 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1213 /* Detach all ports */
1214 for (x = 0; x != hub->nports; x++) {
1216 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1218 if (child == NULL) {
1223 * Free USB device and all subdevices, if any.
1225 usb_free_device(child, 0);
1228 kfree(hub, M_USBDEV);
1229 sc->sc_udev->hub = NULL;
1231 lockuninit(&sc->sc_lock);
1237 uhub_suspend(device_t dev)
1240 /* Sub-devices are not suspended here! */
1245 uhub_resume(device_t dev)
1248 /* Sub-devices are not resumed here! */
1253 uhub_driver_added(device_t dev, driver_t *driver)
1255 usb_needs_explore_all();
1259 struct usb_device *udev;
1261 uint8_t iface_index;
1265 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1266 struct hub_result *res)
1268 struct usb_interface *iface;
1269 struct usb_device *udev;
1274 nports = hub->nports;
1275 for (x = 0; x != nports; x++) {
1276 udev = usb_bus_port_get_device(hub->hubudev->bus,
1281 for (i = 0; i != USB_IFACE_MAX; i++) {
1282 iface = usbd_get_iface(udev, i);
1284 (iface->subdev == child)) {
1285 res->iface_index = i;
1287 res->portno = x + 1;
1292 res->iface_index = 0;
1298 uhub_child_location_string(device_t parent, device_t child,
1299 char *buf, size_t buflen)
1301 struct uhub_softc *sc;
1302 struct usb_hub *hub;
1303 struct hub_result res;
1305 if (!device_is_attached(parent)) {
1311 sc = device_get_softc(parent);
1312 hub = sc->sc_udev->hub;
1317 uhub_find_iface_index(hub, child, &res);
1319 DPRINTF("device not on hub\n");
1325 ksnprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1326 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1327 res.portno, device_get_unit(res.udev->bus->bdev),
1328 res.udev->device_index, res.iface_index);
1331 * mtx_unlock(&Giant);
1338 uhub_child_pnpinfo_string(device_t parent, device_t child,
1339 char *buf, size_t buflen)
1341 struct uhub_softc *sc;
1342 struct usb_hub *hub;
1343 struct usb_interface *iface;
1344 struct hub_result res;
1346 if (!device_is_attached(parent)) {
1352 sc = device_get_softc(parent);
1353 hub = sc->sc_udev->hub;
1358 uhub_find_iface_index(hub, child, &res);
1360 DPRINTF("device not on hub\n");
1366 iface = usbd_get_iface(res.udev, res.iface_index);
1367 if (iface && iface->idesc) {
1368 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1369 "devclass=0x%02x devsubclass=0x%02x "
1373 "intclass=0x%02x intsubclass=0x%02x "
1374 "intprotocol=0x%02x " "%s%s",
1375 UGETW(res.udev->ddesc.idVendor),
1376 UGETW(res.udev->ddesc.idProduct),
1377 res.udev->ddesc.bDeviceClass,
1378 res.udev->ddesc.bDeviceSubClass,
1379 usb_get_serial(res.udev),
1380 UGETW(res.udev->ddesc.bcdDevice),
1381 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1382 iface->idesc->bInterfaceClass,
1383 iface->idesc->bInterfaceSubClass,
1384 iface->idesc->bInterfaceProtocol,
1385 iface->pnpinfo ? " " : "",
1386 iface->pnpinfo ? iface->pnpinfo : "");
1401 * The USB Transaction Translator:
1402 * ===============================
1404 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1405 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1406 * USB transfers. To utilize bandwidth dynamically the "scatter and
1407 * gather" principle must be applied. This means that bandwidth must
1408 * be divided into equal parts of bandwidth. With regard to USB all
1409 * data is transferred in smaller packets with length
1410 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1413 * The bandwidth scheduler which I have implemented will simply pack
1414 * the USB transfers back to back until there is no more space in the
1415 * schedule. Out of the 8 microframes which the USB 2.0 standard
1416 * provides, only 6 are available for non-HIGH-speed devices. I have
1417 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1418 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1419 * this division, it is very difficult to allocate and free bandwidth
1422 * NOTE about the Transaction Translator in USB HUBs:
1424 * USB HUBs have a very simple Transaction Translator, that will
1425 * simply pipeline all the SPLIT transactions. That means that the
1426 * transactions will be executed in the order they are queued!
1430 /*------------------------------------------------------------------------*
1431 * usb_intr_find_best_slot
1434 * The best Transaction Translation slot for an interrupt endpoint.
1435 *------------------------------------------------------------------------*/
1437 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1438 uint8_t end, uint8_t mask)
1440 usb_size_t min = 0 - 1;
1448 /* find the last slot with lesser used bandwidth */
1450 for (x = start; x < end; x++) {
1454 /* compute sum of bandwidth */
1455 for (z = x; z < end; z++) {
1456 if (mask & (1U << (z - x)))
1460 /* check if the current multi-slot is more optimal */
1466 /* check if the mask is about to be shifted out */
1467 if (mask & (1U << (end - 1 - x)))
1473 /*------------------------------------------------------------------------*
1474 * usb_hs_bandwidth_adjust
1476 * This function will update the bandwith usage for the microframe
1477 * having index "slot" by "len" bytes. "len" can be negative. If the
1478 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1479 * the "slot" argument will be replaced by the slot having least used
1480 * bandwidth. The "mask" argument is used for multi-slot allocations.
1483 * The slot in which the bandwidth update was done: 0..7
1484 *------------------------------------------------------------------------*/
1486 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1487 uint8_t slot, uint8_t mask)
1489 struct usb_bus *bus = udev->bus;
1490 struct usb_hub *hub;
1491 enum usb_dev_speed speed;
1494 USB_BUS_LOCK_ASSERT(bus);
1495 speed = usbd_get_speed(udev);
1499 case USB_SPEED_FULL:
1500 if (speed == USB_SPEED_LOW) {
1504 * The Host Controller Driver should have
1505 * performed checks so that the lookup
1506 * below does not result in a NULL pointer
1510 hub = udev->parent_hs_hub->hub;
1511 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1512 slot = usb_intr_find_best_slot(hub->uframe_usage,
1513 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1515 for (x = slot; x < 8; x++) {
1516 if (mask & (1U << (x - slot))) {
1517 hub->uframe_usage[x] += len;
1518 bus->uframe_usage[x] += len;
1523 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1524 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1525 USB_HS_MICRO_FRAMES_MAX, mask);
1527 for (x = slot; x < 8; x++) {
1528 if (mask & (1U << (x - slot))) {
1529 bus->uframe_usage[x] += len;
1537 /*------------------------------------------------------------------------*
1538 * usb_hs_bandwidth_alloc
1540 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1541 *------------------------------------------------------------------------*/
1543 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1545 struct usb_device *udev;
1550 udev = xfer->xroot->udev;
1552 if (udev->flags.usb_mode != USB_MODE_HOST)
1553 return; /* not supported */
1555 xfer->endpoint->refcount_bw++;
1556 if (xfer->endpoint->refcount_bw != 1)
1557 return; /* already allocated */
1559 speed = usbd_get_speed(udev);
1561 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1563 /* allocate a microframe slot */
1566 slot = usb_hs_bandwidth_adjust(udev,
1567 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1569 xfer->endpoint->usb_uframe = slot;
1570 xfer->endpoint->usb_smask = mask << slot;
1572 if ((speed != USB_SPEED_FULL) &&
1573 (speed != USB_SPEED_LOW)) {
1574 xfer->endpoint->usb_cmask = 0x00 ;
1576 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1580 case UE_ISOCHRONOUS:
1581 switch (usbd_xfer_get_fps_shift(xfer)) {
1596 /* allocate a microframe multi-slot */
1598 slot = usb_hs_bandwidth_adjust(udev,
1599 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1601 xfer->endpoint->usb_uframe = slot;
1602 xfer->endpoint->usb_cmask = 0;
1603 xfer->endpoint->usb_smask = mask << slot;
1607 xfer->endpoint->usb_uframe = 0;
1608 xfer->endpoint->usb_cmask = 0;
1609 xfer->endpoint->usb_smask = 0;
1613 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1614 xfer->endpoint->usb_uframe,
1615 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1618 /*------------------------------------------------------------------------*
1619 * usb_hs_bandwidth_free
1621 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1622 *------------------------------------------------------------------------*/
1624 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1626 struct usb_device *udev;
1630 udev = xfer->xroot->udev;
1632 if (udev->flags.usb_mode != USB_MODE_HOST)
1633 return; /* not supported */
1635 xfer->endpoint->refcount_bw--;
1636 if (xfer->endpoint->refcount_bw != 0)
1637 return; /* still allocated */
1639 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1641 case UE_ISOCHRONOUS:
1643 slot = xfer->endpoint->usb_uframe;
1644 mask = xfer->endpoint->usb_smask;
1646 /* free microframe slot(s): */
1647 usb_hs_bandwidth_adjust(udev,
1648 -xfer->max_frame_size, slot, mask >> slot);
1650 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1651 slot, mask >> slot);
1653 xfer->endpoint->usb_uframe = 0;
1654 xfer->endpoint->usb_cmask = 0;
1655 xfer->endpoint->usb_smask = 0;
1663 /*------------------------------------------------------------------------*
1664 * usbd_fs_isoc_schedule_init_sub
1666 * This function initialises an USB FULL speed isochronous schedule
1668 *------------------------------------------------------------------------*/
1669 #if USB_HAVE_TT_SUPPORT
1671 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1673 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1674 USB_FS_BYTES_PER_HS_UFRAME);
1675 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1676 fss->frame_slot = 0;
1680 /*------------------------------------------------------------------------*
1681 * usbd_fs_isoc_schedule_init_all
1683 * This function will reset the complete USB FULL speed isochronous
1684 * bandwidth schedule.
1685 *------------------------------------------------------------------------*/
1686 #if USB_HAVE_TT_SUPPORT
1688 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1690 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1692 while (fss != fss_end) {
1693 usbd_fs_isoc_schedule_init_sub(fss);
1699 /*------------------------------------------------------------------------*
1700 * usb_isoc_time_expand
1702 * This function will expand the time counter from 7-bit to 16-bit.
1705 * 16-bit isochronous time counter.
1706 *------------------------------------------------------------------------*/
1708 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1712 USB_BUS_LOCK_ASSERT(bus);
1714 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1716 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1718 if (isoc_time_curr < rem) {
1719 /* the time counter wrapped around */
1720 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1722 /* update the remainder */
1724 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1725 bus->isoc_time_last |= isoc_time_curr;
1727 return (bus->isoc_time_last);
1730 /*------------------------------------------------------------------------*
1731 * usbd_fs_isoc_schedule_isoc_time_expand
1733 * This function does multiple things. First of all it will expand the
1734 * passed isochronous time, which is the return value. Then it will
1735 * store where the current FULL speed isochronous schedule is
1736 * positioned in time and where the end is. See "pp_start" and
1737 * "pp_end" arguments.
1740 * Expanded version of "isoc_time".
1742 * NOTE: This function depends on being called regularly with
1743 * intervals less than "USB_ISOC_TIME_MAX".
1744 *------------------------------------------------------------------------*/
1745 #if USB_HAVE_TT_SUPPORT
1747 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1748 struct usb_fs_isoc_schedule **pp_start,
1749 struct usb_fs_isoc_schedule **pp_end,
1752 struct usb_fs_isoc_schedule *fss_end;
1753 struct usb_fs_isoc_schedule *fss_a;
1754 struct usb_fs_isoc_schedule *fss_b;
1755 struct usb_hub *hs_hub;
1757 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1759 hs_hub = udev->parent_hs_hub->hub;
1761 if (hs_hub != NULL) {
1763 fss_a = hs_hub->fs_isoc_schedule +
1764 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1766 hs_hub->isoc_last_time = isoc_time;
1768 fss_b = hs_hub->fs_isoc_schedule +
1769 (isoc_time % USB_ISOC_TIME_MAX);
1771 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1773 *pp_start = hs_hub->fs_isoc_schedule;
1776 while (fss_a != fss_b) {
1777 if (fss_a == fss_end) {
1778 fss_a = hs_hub->fs_isoc_schedule;
1781 usbd_fs_isoc_schedule_init_sub(fss_a);
1794 /*------------------------------------------------------------------------*
1795 * usbd_fs_isoc_schedule_alloc
1797 * This function will allocate bandwidth for an isochronous FULL speed
1798 * transaction in the FULL speed schedule. The microframe slot where
1799 * the transaction should be started is stored in the byte pointed to
1800 * by "pstart". The "len" argument specifies the length of the
1801 * transaction in bytes.
1806 *------------------------------------------------------------------------*/
1807 #if USB_HAVE_TT_SUPPORT
1809 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1810 uint8_t *pstart, uint16_t len)
1812 uint8_t slot = fss->frame_slot;
1814 /* Compute overhead and bit-stuffing */
1821 if (len > fss->total_bytes) {
1822 *pstart = 0; /* set some dummy value */
1823 return (1); /* error */
1827 fss->total_bytes -= len;
1829 while (len >= fss->frame_bytes) {
1830 len -= fss->frame_bytes;
1831 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1835 fss->frame_bytes -= len;
1838 return (0); /* success */
1842 /*------------------------------------------------------------------------*
1843 * usb_bus_port_get_device
1845 * This function is NULL safe.
1846 *------------------------------------------------------------------------*/
1848 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1850 if ((bus == NULL) || (up == NULL)) {
1854 if (up->device_index == 0) {
1858 return (bus->devices[up->device_index]);
1861 /*------------------------------------------------------------------------*
1862 * usb_bus_port_set_device
1864 * This function is NULL safe.
1865 *------------------------------------------------------------------------*/
1867 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1868 struct usb_device *udev, uint8_t device_index)
1875 * There is only one case where we don't
1876 * have an USB port, and that is the Root Hub!
1880 up->device_index = device_index;
1882 device_index = up->device_index;
1883 up->device_index = 0;
1887 * Make relationships to our new device
1889 if (device_index != 0) {
1891 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1893 bus->devices[device_index] = udev;
1895 lockmgr(&usb_ref_lock, LK_RELEASE);
1901 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1904 /*------------------------------------------------------------------------*
1907 * This functions is called when the USB event thread needs to run.
1908 *------------------------------------------------------------------------*/
1910 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1917 DPRINTF("No bus pointer!\n");
1920 if ((bus->devices == NULL) ||
1921 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1922 DPRINTF("No root HUB\n");
1925 if (lockstatus(&bus->bus_lock, curthread) != 0) {
1934 if (usb_proc_msignal(&bus->explore_proc,
1935 &bus->explore_msg[0], &bus->explore_msg[1])) {
1939 USB_BUS_UNLOCK(bus);
1943 /*------------------------------------------------------------------------*
1944 * usb_needs_explore_all
1946 * This function is called whenever a new driver is loaded and will
1947 * cause that all USB busses are re-explored.
1948 *------------------------------------------------------------------------*/
1950 usb_needs_explore_all(void)
1952 struct usb_bus *bus;
1959 dc = usb_devclass_ptr;
1961 DPRINTFN(0, "no devclass\n");
1965 * Explore all USB busses in parallell.
1967 max = devclass_get_maxunit(dc);
1969 dev = devclass_get_device(dc, max);
1971 bus = device_get_softc(dev);
1973 usb_needs_explore(bus, 1);
1980 /*------------------------------------------------------------------------*
1981 * usb_bus_power_update
1983 * This function will ensure that all USB devices on the given bus are
1984 * properly suspended or resumed according to the device transfer
1986 *------------------------------------------------------------------------*/
1989 usb_bus_power_update(struct usb_bus *bus)
1991 usb_needs_explore(bus, 0 /* no probe */ );
1995 /*------------------------------------------------------------------------*
1996 * usbd_transfer_power_ref
1998 * This function will modify the power save reference counts and
1999 * wakeup the USB device associated with the given USB transfer, if
2001 *------------------------------------------------------------------------*/
2004 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2006 static const usb_power_mask_t power_mask[4] = {
2007 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2008 [UE_BULK] = USB_HW_POWER_BULK,
2009 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2010 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2012 struct usb_device *udev;
2013 uint8_t needs_explore;
2014 uint8_t needs_hw_power;
2017 udev = xfer->xroot->udev;
2019 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2020 /* no power save for root HUB */
2023 USB_BUS_LOCK(udev->bus);
2025 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2027 udev->pwr_save.last_xfer_time = ticks;
2028 udev->pwr_save.type_refs[xfer_type] += val;
2030 if (xfer->flags_int.control_xfr) {
2031 udev->pwr_save.read_refs += val;
2032 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2034 * It is not allowed to suspend during a
2037 udev->pwr_save.write_refs += val;
2039 } else if (USB_GET_DATA_ISREAD(xfer)) {
2040 udev->pwr_save.read_refs += val;
2042 udev->pwr_save.write_refs += val;
2046 if (udev->flags.self_suspended)
2047 needs_explore = usb_peer_should_wakeup(udev);
2051 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2052 DPRINTF("Adding type %u to power state\n", xfer_type);
2053 udev->bus->hw_power_state |= power_mask[xfer_type];
2063 USB_BUS_UNLOCK(udev->bus);
2065 if (needs_explore) {
2066 DPRINTF("update\n");
2067 usb_bus_power_update(udev->bus);
2068 } else if (needs_hw_power) {
2069 DPRINTF("needs power\n");
2070 if (udev->bus->methods->set_hw_power != NULL) {
2071 (udev->bus->methods->set_hw_power) (udev->bus);
2077 /*------------------------------------------------------------------------*
2078 * usb_peer_should_wakeup
2080 * This function returns non-zero if the current device should wake up.
2081 *------------------------------------------------------------------------*/
2083 usb_peer_should_wakeup(struct usb_device *udev)
2085 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2086 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2087 (udev->re_enumerate_wait != 0) ||
2088 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2089 (udev->pwr_save.write_refs != 0) ||
2090 ((udev->pwr_save.read_refs != 0) &&
2091 (udev->flags.usb_mode == USB_MODE_HOST) &&
2092 (usb_peer_can_wakeup(udev) == 0)));
2095 /*------------------------------------------------------------------------*
2098 * This function implements the USB power daemon and is called
2099 * regularly from the USB explore thread.
2100 *------------------------------------------------------------------------*/
2103 usb_bus_powerd(struct usb_bus *bus)
2105 struct usb_device *udev;
2108 usb_ticks_t mintime;
2109 usb_size_t type_refs[5];
2112 limit = usb_power_timeout;
2115 else if (limit > 255)
2120 DPRINTF("bus=%p\n", bus);
2125 * The root HUB device is never suspended
2126 * and we simply skip it.
2128 for (x = USB_ROOT_HUB_ADDR + 1;
2129 x != bus->devices_max; x++) {
2131 udev = bus->devices[x];
2135 temp = ticks - udev->pwr_save.last_xfer_time;
2137 if (usb_peer_should_wakeup(udev)) {
2138 /* check if we are suspended */
2139 if (udev->flags.self_suspended != 0) {
2140 USB_BUS_UNLOCK(bus);
2141 usb_dev_resume_peer(udev);
2144 } else if ((temp >= limit) &&
2145 (udev->flags.usb_mode == USB_MODE_HOST) &&
2146 (udev->flags.self_suspended == 0)) {
2147 /* try to do suspend */
2149 USB_BUS_UNLOCK(bus);
2150 usb_dev_suspend_peer(udev);
2155 /* reset counters */
2164 /* Re-loop all the devices to get the actual state */
2166 for (x = USB_ROOT_HUB_ADDR + 1;
2167 x != bus->devices_max; x++) {
2169 udev = bus->devices[x];
2173 /* we found a non-Root-Hub USB device */
2176 /* "last_xfer_time" can be updated by a resume */
2177 temp = ticks - udev->pwr_save.last_xfer_time;
2180 * Compute minimum time since last transfer for the complete
2186 if (udev->flags.self_suspended == 0) {
2187 type_refs[0] += udev->pwr_save.type_refs[0];
2188 type_refs[1] += udev->pwr_save.type_refs[1];
2189 type_refs[2] += udev->pwr_save.type_refs[2];
2190 type_refs[3] += udev->pwr_save.type_refs[3];
2194 if (mintime >= (1 * hz)) {
2195 /* recompute power masks */
2196 DPRINTF("Recomputing power masks\n");
2197 bus->hw_power_state = 0;
2198 if (type_refs[UE_CONTROL] != 0)
2199 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2200 if (type_refs[UE_BULK] != 0)
2201 bus->hw_power_state |= USB_HW_POWER_BULK;
2202 if (type_refs[UE_INTERRUPT] != 0)
2203 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2204 if (type_refs[UE_ISOCHRONOUS] != 0)
2205 bus->hw_power_state |= USB_HW_POWER_ISOC;
2206 if (type_refs[4] != 0)
2207 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2209 USB_BUS_UNLOCK(bus);
2211 if (bus->methods->set_hw_power != NULL) {
2212 /* always update hardware power! */
2213 (bus->methods->set_hw_power) (bus);
2219 /*------------------------------------------------------------------------*
2220 * usb_dev_resume_peer
2222 * This function will resume an USB peer and do the required USB
2223 * signalling to get an USB device out of the suspended state.
2224 *------------------------------------------------------------------------*/
2226 usb_dev_resume_peer(struct usb_device *udev)
2228 struct usb_bus *bus;
2235 /* check if already resumed */
2236 if (udev->flags.self_suspended == 0)
2239 /* we need a parent HUB to do resume */
2240 if (udev->parent_hub == NULL)
2243 DPRINTF("udev=%p\n", udev);
2245 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2246 (udev->flags.remote_wakeup == 0)) {
2248 * If the host did not set the remote wakeup feature, we can
2249 * not wake it up either!
2251 DPRINTF("remote wakeup is not set!\n");
2254 /* get bus pointer */
2257 /* resume parent hub first */
2258 usb_dev_resume_peer(udev->parent_hub);
2260 /* reduce chance of instant resume failure by waiting a little bit */
2261 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2263 if (usb_device_20_compatible(udev)) {
2264 /* resume current port (Valid in Host and Device Mode) */
2265 err = usbd_req_clear_port_feature(udev->parent_hub,
2266 NULL, udev->port_no, UHF_PORT_SUSPEND);
2268 DPRINTFN(0, "Resuming port failed\n");
2272 /* resume current port (Valid in Host and Device Mode) */
2273 err = usbd_req_set_port_link_state(udev->parent_hub,
2274 NULL, udev->port_no, UPS_PORT_LS_U0);
2276 DPRINTFN(0, "Resuming port failed\n");
2281 /* resume settle time */
2282 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2284 if (bus->methods->device_resume != NULL) {
2285 /* resume USB device on the USB controller */
2286 (bus->methods->device_resume) (udev);
2289 /* set that this device is now resumed */
2290 udev->flags.self_suspended = 0;
2292 /* make sure that we don't go into suspend right away */
2293 udev->pwr_save.last_xfer_time = ticks;
2295 /* make sure the needed power masks are on */
2296 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2297 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2298 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2299 bus->hw_power_state |= USB_HW_POWER_BULK;
2300 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2301 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2302 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2303 bus->hw_power_state |= USB_HW_POWER_ISOC;
2305 USB_BUS_UNLOCK(bus);
2307 if (bus->methods->set_hw_power != NULL) {
2308 /* always update hardware power! */
2309 (bus->methods->set_hw_power) (bus);
2314 /* notify all sub-devices about resume */
2315 err = usb_suspend_resume(udev, 0);
2317 usbd_sr_unlock(udev);
2319 /* check if peer has wakeup capability */
2320 if (usb_peer_can_wakeup(udev)) {
2321 /* clear remote wakeup */
2322 err = usbd_req_clear_device_feature(udev,
2323 NULL, UF_DEVICE_REMOTE_WAKEUP);
2325 DPRINTFN(0, "Clearing device "
2326 "remote wakeup failed: %s\n",
2332 /*------------------------------------------------------------------------*
2333 * usb_dev_suspend_peer
2335 * This function will suspend an USB peer and do the required USB
2336 * signalling to get an USB device into the suspended state.
2337 *------------------------------------------------------------------------*/
2339 usb_dev_suspend_peer(struct usb_device *udev)
2341 struct usb_device *child;
2351 /* check if already suspended */
2352 if (udev->flags.self_suspended)
2355 /* we need a parent HUB to do suspend */
2356 if (udev->parent_hub == NULL)
2359 DPRINTF("udev=%p\n", udev);
2361 /* check if the current device is a HUB */
2362 if (udev->hub != NULL) {
2363 nports = udev->hub->nports;
2365 /* check if all devices on the HUB are suspended */
2366 for (x = 0; x != nports; x++) {
2367 child = usb_bus_port_get_device(udev->bus,
2368 udev->hub->ports + x);
2373 if (child->flags.self_suspended)
2376 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2381 if (usb_peer_can_wakeup(udev)) {
2383 * This request needs to be done before we set
2384 * "udev->flags.self_suspended":
2387 /* allow device to do remote wakeup */
2388 err = usbd_req_set_device_feature(udev,
2389 NULL, UF_DEVICE_REMOTE_WAKEUP);
2391 DPRINTFN(0, "Setting device "
2392 "remote wakeup failed\n");
2396 USB_BUS_LOCK(udev->bus);
2398 * Checking for suspend condition and setting suspended bit
2401 err = usb_peer_should_wakeup(udev);
2404 * Set that this device is suspended. This variable
2405 * must be set before calling USB controller suspend
2408 udev->flags.self_suspended = 1;
2410 USB_BUS_UNLOCK(udev->bus);
2413 if (usb_peer_can_wakeup(udev)) {
2414 /* allow device to do remote wakeup */
2415 err = usbd_req_clear_device_feature(udev,
2416 NULL, UF_DEVICE_REMOTE_WAKEUP);
2418 DPRINTFN(0, "Setting device "
2419 "remote wakeup failed\n");
2423 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2424 /* resume parent HUB first */
2425 usb_dev_resume_peer(udev->parent_hub);
2427 /* reduce chance of instant resume failure by waiting a little bit */
2428 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2430 /* resume current port (Valid in Host and Device Mode) */
2431 err = usbd_req_clear_port_feature(udev->parent_hub,
2432 NULL, udev->port_no, UHF_PORT_SUSPEND);
2434 /* resume settle time */
2435 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2437 DPRINTF("Suspend was cancelled!\n");
2443 /* notify all sub-devices about suspend */
2444 err = usb_suspend_resume(udev, 1);
2446 usbd_sr_unlock(udev);
2448 if (udev->bus->methods->device_suspend != NULL) {
2451 /* suspend device on the USB controller */
2452 (udev->bus->methods->device_suspend) (udev);
2455 temp = usbd_get_dma_delay(udev);
2457 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2461 if (usb_device_20_compatible(udev)) {
2462 /* suspend current port */
2463 err = usbd_req_set_port_feature(udev->parent_hub,
2464 NULL, udev->port_no, UHF_PORT_SUSPEND);
2466 DPRINTFN(0, "Suspending port failed\n");
2470 /* suspend current port */
2471 err = usbd_req_set_port_link_state(udev->parent_hub,
2472 NULL, udev->port_no, UPS_PORT_LS_U3);
2474 DPRINTFN(0, "Suspending port failed\n");
2479 udev = udev->parent_hub;
2483 /*------------------------------------------------------------------------*
2484 * usbd_set_power_mode
2486 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2488 *------------------------------------------------------------------------*/
2490 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2492 /* filter input argument */
2493 if ((power_mode != USB_POWER_MODE_ON) &&
2494 (power_mode != USB_POWER_MODE_OFF))
2495 power_mode = USB_POWER_MODE_SAVE;
2497 power_mode = usbd_filter_power_mode(udev, power_mode);
2499 udev->power_mode = power_mode; /* update copy of power mode */
2502 usb_bus_power_update(udev->bus);
2506 /*------------------------------------------------------------------------*
2507 * usbd_filter_power_mode
2509 * This function filters the power mode based on hardware requirements.
2510 *------------------------------------------------------------------------*/
2512 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2514 struct usb_bus_methods *mtod;
2517 mtod = udev->bus->methods;
2520 if (mtod->get_power_mode != NULL)
2521 (mtod->get_power_mode) (udev, &temp);
2523 /* check if we should not filter */
2525 return (power_mode);
2527 /* use fixed power mode given by hardware driver */
2531 /*------------------------------------------------------------------------*
2532 * usbd_start_re_enumerate
2534 * This function starts re-enumeration of the given USB device. This
2535 * function does not need to be called BUS-locked. This function does
2536 * not wait until the re-enumeration is completed.
2537 *------------------------------------------------------------------------*/
2539 usbd_start_re_enumerate(struct usb_device *udev)
2541 if (udev->re_enumerate_wait == 0) {
2542 udev->re_enumerate_wait = 1;
2543 usb_needs_explore(udev->bus, 0);