3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR uhub_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define UHUB_INTR_INTERVAL 250 /* ms */
77 #define UHUB_N_TRANSFER 1
80 static int uhub_debug = 0;
82 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
83 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
85 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
89 static int usb_power_timeout = 30; /* seconds */
91 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
92 &usb_power_timeout, 0, "USB power timeout");
95 struct uhub_current_state {
101 struct uhub_current_state sc_st;/* current state */
102 #if (USB_HAVE_FIXED_PORT != 0)
103 struct usb_hub sc_hub;
105 device_t sc_dev; /* base device */
106 struct mtx sc_mtx; /* our mutex */
107 struct usb_device *sc_udev; /* USB device */
108 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
110 #define UHUB_FLAG_DID_EXPLORE 0x01
113 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
114 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
115 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
116 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
117 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
119 /* prototypes for type checking: */
121 static device_probe_t uhub_probe;
122 static device_attach_t uhub_attach;
123 static device_detach_t uhub_detach;
124 static device_suspend_t uhub_suspend;
125 static device_resume_t uhub_resume;
127 static bus_driver_added_t uhub_driver_added;
128 static bus_child_location_str_t uhub_child_location_string;
129 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
131 static usb_callback_t uhub_intr_callback;
133 static void usb_dev_resume_peer(struct usb_device *udev);
134 static void usb_dev_suspend_peer(struct usb_device *udev);
135 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
137 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
140 .type = UE_INTERRUPT,
141 .endpoint = UE_ADDR_ANY,
142 .direction = UE_DIR_ANY,
144 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
145 .bufsize = 0, /* use wMaxPacketSize */
146 .callback = &uhub_intr_callback,
147 .interval = UHUB_INTR_INTERVAL,
152 * driver instance for "hub" connected to "usb"
153 * and "hub" connected to "hub"
155 static devclass_t uhub_devclass;
157 static device_method_t uhub_methods[] = {
158 DEVMETHOD(device_probe, uhub_probe),
159 DEVMETHOD(device_attach, uhub_attach),
160 DEVMETHOD(device_detach, uhub_detach),
162 DEVMETHOD(device_suspend, uhub_suspend),
163 DEVMETHOD(device_resume, uhub_resume),
165 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
166 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
167 DEVMETHOD(bus_driver_added, uhub_driver_added),
171 static driver_t uhub_driver = {
173 .methods = uhub_methods,
174 .size = sizeof(struct uhub_softc)
177 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
178 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
179 MODULE_VERSION(uhub, 1);
182 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
184 struct uhub_softc *sc = usbd_xfer_softc(xfer);
186 switch (USB_GET_STATE(xfer)) {
187 case USB_ST_TRANSFERRED:
190 * This is an indication that some port
191 * has changed status. Notify the bus
192 * event handler thread that we need
193 * to be explored again:
195 usb_needs_explore(sc->sc_udev->bus, 0);
198 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
199 usbd_transfer_submit(xfer);
203 if (xfer->error != USB_ERR_CANCELLED) {
205 * Do a clear-stall. The "stall_pipe" flag
206 * will get cleared before next callback by
209 usbd_xfer_set_stall(xfer);
210 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
211 usbd_transfer_submit(xfer);
217 /*------------------------------------------------------------------------*
218 * uhub_explore_sub - subroutine
222 * Else: A control transaction failed
223 *------------------------------------------------------------------------*/
225 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
228 struct usb_device *child;
232 bus = sc->sc_udev->bus;
235 /* get driver added refcount from USB bus */
236 refcount = bus->driver_added_refcount;
238 /* get device assosiated with the given port */
239 child = usb_bus_port_get_device(bus, up);
245 /* check if device should be re-enumerated */
247 if (child->flags.usb_mode == USB_MODE_HOST) {
250 do_unlock = usbd_enum_lock(child);
251 switch (child->re_enumerate_wait) {
252 case USB_RE_ENUM_START:
253 err = usbd_set_config_index(child,
256 DPRINTF("Unconfigure failed: "
260 err = usbd_req_re_enumerate(child, NULL);
262 err = usbd_set_config_index(child, 0);
264 err = usb_probe_and_attach(child,
265 USB_IFACE_INDEX_ANY);
267 child->re_enumerate_wait = USB_RE_ENUM_DONE;
271 case USB_RE_ENUM_PWR_OFF:
272 /* get the device unconfigured */
273 err = usbd_set_config_index(child,
276 DPRINTFN(0, "Could not unconfigure "
277 "device (ignored)\n");
280 /* clear port enable */
281 err = usbd_req_clear_port_feature(child->parent_hub,
282 NULL, child->port_no, UHF_PORT_ENABLE);
284 DPRINTFN(0, "Could not disable port "
287 child->re_enumerate_wait = USB_RE_ENUM_DONE;
292 child->re_enumerate_wait = USB_RE_ENUM_DONE;
296 usbd_enum_unlock(child);
299 /* check if probe and attach should be done */
301 if (child->driver_added_refcount != refcount) {
302 child->driver_added_refcount = refcount;
303 err = usb_probe_and_attach(child,
304 USB_IFACE_INDEX_ANY);
309 /* start control transfer, if device mode */
311 if (child->flags.usb_mode == USB_MODE_DEVICE)
312 usbd_ctrl_transfer_setup(child);
314 /* if a HUB becomes present, do a recursive HUB explore */
317 err = (child->hub->explore) (child);
323 /*------------------------------------------------------------------------*
324 * uhub_read_port_status - factored out code
325 *------------------------------------------------------------------------*/
327 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
329 struct usb_port_status ps;
332 err = usbd_req_get_port_status(
333 sc->sc_udev, NULL, &ps, portno);
335 /* update status regardless of error */
337 sc->sc_st.port_status = UGETW(ps.wPortStatus);
338 sc->sc_st.port_change = UGETW(ps.wPortChange);
340 /* debugging print */
342 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
343 "wPortChange=0x%04x, err=%s\n",
344 portno, sc->sc_st.port_status,
345 sc->sc_st.port_change, usbd_errstr(err));
349 /*------------------------------------------------------------------------*
354 * Else: A control transaction failed
355 *------------------------------------------------------------------------*/
357 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
359 struct usb_device *child;
360 struct usb_device *udev;
361 enum usb_dev_speed speed;
362 enum usb_hc_mode mode;
367 DPRINTF("reattaching port %d\n", portno);
371 child = usb_bus_port_get_device(udev->bus,
372 udev->hub->ports + portno - 1);
376 /* first clear the port connection change bit */
378 err = usbd_req_clear_port_feature(udev, NULL,
379 portno, UHF_C_PORT_CONNECTION);
384 /* check if there is a child */
388 * Free USB device and all subdevices, if any.
390 usb_free_device(child, 0);
393 /* get fresh status */
395 err = uhub_read_port_status(sc, portno);
399 /* check if nothing is connected to the port */
401 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
404 /* check if there is no power on the port and print a warning */
406 switch (udev->speed) {
410 power_mask = UPS_PORT_POWER;
412 case USB_SPEED_SUPER:
413 if (udev->parent_hub == NULL)
414 power_mask = UPS_PORT_POWER;
416 power_mask = UPS_PORT_POWER_SS;
422 if (!(sc->sc_st.port_status & power_mask)) {
423 DPRINTF("WARNING: strange, connected port %d "
424 "has no power\n", portno);
427 /* check if the device is in Host Mode */
429 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
431 DPRINTF("Port %d is in Host Mode\n", portno);
433 if (sc->sc_st.port_status & UPS_SUSPEND) {
435 * NOTE: Should not get here in SuperSpeed
436 * mode, because the HUB should report this
439 DPRINTF("Port %d was still "
440 "suspended, clearing.\n", portno);
441 err = usbd_req_clear_port_feature(udev,
442 NULL, portno, UHF_PORT_SUSPEND);
447 /* wait for maximum device power up time */
450 USB_MS_TO_TICKS(usb_port_powerup_delay));
452 /* reset port, which implies enabling it */
454 err = usbd_req_reset_port(udev, NULL, portno);
457 DPRINTFN(0, "port %d reset "
458 "failed, error=%s\n",
459 portno, usbd_errstr(err));
462 /* get port status again, it might have changed during reset */
464 err = uhub_read_port_status(sc, portno);
468 /* check if something changed during port reset */
470 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
471 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
473 DPRINTFN(0, "giving up port reset "
474 "- device vanished\n");
481 DPRINTF("Port %d is in Device Mode\n", portno);
485 * Figure out the device speed
487 switch (udev->speed) {
489 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
490 speed = USB_SPEED_HIGH;
491 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
492 speed = USB_SPEED_LOW;
494 speed = USB_SPEED_FULL;
497 if (sc->sc_st.port_status & UPS_LOW_SPEED)
498 speed = USB_SPEED_LOW;
500 speed = USB_SPEED_FULL;
503 speed = USB_SPEED_LOW;
505 case USB_SPEED_SUPER:
506 if (udev->parent_hub == NULL) {
507 /* Root HUB - special case */
508 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
510 speed = USB_SPEED_FULL;
513 speed = USB_SPEED_LOW;
516 speed = USB_SPEED_HIGH;
519 speed = USB_SPEED_SUPER;
523 speed = USB_SPEED_SUPER;
527 /* same speed like parent */
531 if (speed == USB_SPEED_SUPER) {
532 err = usbd_req_set_hub_u1_timeout(udev, NULL,
533 portno, 128 - (2 * udev->depth));
535 DPRINTFN(0, "port %d U1 timeout "
536 "failed, error=%s\n",
537 portno, usbd_errstr(err));
539 err = usbd_req_set_hub_u2_timeout(udev, NULL,
540 portno, 128 - (2 * udev->depth));
542 DPRINTFN(0, "port %d U2 timeout "
543 "failed, error=%s\n",
544 portno, usbd_errstr(err));
549 * Figure out the device mode
551 * NOTE: This part is currently FreeBSD specific.
553 if (udev->parent_hub != NULL) {
554 /* inherit mode from the parent HUB */
555 mode = udev->parent_hub->flags.usb_mode;
556 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
557 mode = USB_MODE_DEVICE;
559 mode = USB_MODE_HOST;
561 /* need to create a new child */
562 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
563 udev->depth + 1, portno - 1, portno, speed, mode);
565 DPRINTFN(0, "could not allocate new device\n");
568 return (0); /* success */
573 * Free USB device and all subdevices, if any.
575 usb_free_device(child, 0);
579 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
580 err = usbd_req_clear_port_feature(
582 portno, UHF_PORT_ENABLE);
586 DPRINTFN(0, "device problem (%s), "
587 "disabling port %d\n", usbd_errstr(err), portno);
592 /*------------------------------------------------------------------------*
593 * usb_device_20_compatible
596 * 0: HUB does not support suspend and resume
597 * Else: HUB supports suspend and resume
598 *------------------------------------------------------------------------*/
600 usb_device_20_compatible(struct usb_device *udev)
604 switch (udev->speed) {
614 /*------------------------------------------------------------------------*
615 * uhub_suspend_resume_port
619 * Else: A control transaction failed
620 *------------------------------------------------------------------------*/
622 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
624 struct usb_device *child;
625 struct usb_device *udev;
629 DPRINTF("port %d\n", portno);
632 child = usb_bus_port_get_device(udev->bus,
633 udev->hub->ports + portno - 1);
635 /* first clear the port suspend change bit */
637 if (usb_device_20_compatible(udev)) {
638 err = usbd_req_clear_port_feature(udev, NULL,
639 portno, UHF_C_PORT_SUSPEND);
641 err = usbd_req_clear_port_feature(udev, NULL,
642 portno, UHF_C_PORT_LINK_STATE);
646 DPRINTF("clearing suspend failed.\n");
649 /* get fresh status */
651 err = uhub_read_port_status(sc, portno);
653 DPRINTF("reading port status failed.\n");
656 /* convert current state */
658 if (usb_device_20_compatible(udev)) {
659 if (sc->sc_st.port_status & UPS_SUSPEND) {
665 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
669 case UPS_PORT_LS_SS_INA:
670 usbd_req_warm_reset_port(udev, NULL, portno);
679 DPRINTF("suspended=%u\n", is_suspend);
681 /* do the suspend or resume */
685 * This code handle two cases: 1) Host Mode - we can only
686 * receive resume here 2) Device Mode - we can receive
687 * suspend and resume here
690 usb_dev_resume_peer(child);
691 else if (child->flags.usb_mode == USB_MODE_DEVICE)
692 usb_dev_suspend_peer(child);
698 /*------------------------------------------------------------------------*
699 * uhub_root_interrupt
701 * This function is called when a Root HUB interrupt has
702 * happened. "ptr" and "len" makes up the Root HUB interrupt
703 * packet. This function is called having the "bus_mtx" locked.
704 *------------------------------------------------------------------------*/
706 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
708 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
710 usb_needs_explore(bus, 0);
714 uhub_is_too_deep(struct usb_device *udev)
716 switch (udev->speed) {
720 if (udev->depth > USB_HUB_MAX_DEPTH)
723 case USB_SPEED_SUPER:
724 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
733 /*------------------------------------------------------------------------*
739 *------------------------------------------------------------------------*/
741 uhub_explore(struct usb_device *udev)
744 struct uhub_softc *sc;
754 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
756 /* ignore devices that are too deep */
757 if (uhub_is_too_deep(udev))
758 return (USB_ERR_TOO_DEEP);
760 /* check if device is suspended */
761 if (udev->flags.self_suspended) {
762 /* need to wait until the child signals resume */
763 DPRINTF("Device is suspended!\n");
768 * Make sure we don't race against user-space applications
771 do_unlock = usbd_enum_lock(udev);
773 for (x = 0; x != hub->nports; x++) {
777 err = uhub_read_port_status(sc, portno);
779 /* most likely the HUB is gone */
782 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
783 DPRINTF("Overcurrent on port %u.\n", portno);
784 err = usbd_req_clear_port_feature(
785 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
787 /* most likely the HUB is gone */
791 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
793 * Fake a connect status change so that the
794 * status gets checked initially!
796 sc->sc_st.port_change |=
797 UPS_C_CONNECT_STATUS;
799 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
800 err = usbd_req_clear_port_feature(
801 udev, NULL, portno, UHF_C_PORT_ENABLE);
803 /* most likely the HUB is gone */
806 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
808 * Ignore the port error if the device
811 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
812 DPRINTFN(0, "illegal enable change, "
813 "port %d\n", portno);
816 if (up->restartcnt == USB_RESTART_MAX) {
817 /* XXX could try another speed ? */
818 DPRINTFN(0, "port error, giving up "
819 "port %d\n", portno);
821 sc->sc_st.port_change |=
822 UPS_C_CONNECT_STATUS;
827 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
828 err = uhub_reattach_port(sc, portno);
830 /* most likely the HUB is gone */
834 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
835 UPS_C_PORT_LINK_STATE)) {
836 err = uhub_suspend_resume_port(sc, portno);
838 /* most likely the HUB is gone */
842 err = uhub_explore_sub(sc, up);
844 /* no device(s) present */
847 /* explore succeeded - reset restart counter */
852 usbd_enum_unlock(udev);
854 /* initial status checked */
855 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
858 return (USB_ERR_NORMAL_COMPLETION);
862 uhub_probe(device_t dev)
864 struct usb_attach_arg *uaa = device_get_ivars(dev);
866 if (uaa->usb_mode != USB_MODE_HOST)
870 * The subclass for USB HUBs is currently ignored because it
871 * is 0 for some and 1 for others.
873 if (uaa->info.bConfigIndex == 0 &&
874 uaa->info.bDeviceClass == UDCLASS_HUB)
880 /* NOTE: The information returned by this function can be wrong. */
882 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
884 struct usb_hub_descriptor hubdesc20;
885 struct usb_hub_ss_descriptor hubdesc30;
890 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
891 return (USB_ERR_INVAL);
896 switch (udev->speed) {
900 /* assuming that there is one port */
901 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
903 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
904 "error=%s\n", usbd_errstr(err));
907 nports = hubdesc20.bNbrPorts;
911 if (udev->speed == USB_SPEED_HIGH)
912 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
915 case USB_SPEED_SUPER:
916 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
918 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
919 "error=%s\n", usbd_errstr(err));
922 nports = hubdesc30.bNbrPorts;
942 uhub_attach(device_t dev)
944 struct uhub_softc *sc = device_get_softc(dev);
945 struct usb_attach_arg *uaa = device_get_ivars(dev);
946 struct usb_device *udev = uaa->device;
947 struct usb_device *parent_hub = udev->parent_hub;
949 struct usb_hub_descriptor hubdesc20;
950 struct usb_hub_ss_descriptor hubdesc30;
962 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
964 device_set_usb_desc(dev);
966 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
967 "parent->selfpowered=%d\n",
969 udev->flags.self_powered,
972 parent_hub->flags.self_powered : 0);
974 if (uhub_is_too_deep(udev)) {
975 DPRINTFN(0, "HUB at depth %d, "
976 "exceeds maximum. HUB ignored\n", (int)udev->depth);
980 if (!udev->flags.self_powered && parent_hub &&
981 !parent_hub->flags.self_powered) {
982 DPRINTFN(0, "Bus powered HUB connected to "
983 "bus powered HUB. HUB ignored\n");
987 if (UHUB_IS_MULTI_TT(sc)) {
988 err = usbd_set_alt_interface_index(udev, 0, 1);
990 device_printf(dev, "MTT could not be enabled\n");
993 device_printf(dev, "MTT enabled\n");
996 /* get HUB descriptor */
998 DPRINTFN(2, "Getting HUB descriptor\n");
1000 switch (udev->speed) {
1002 case USB_SPEED_FULL:
1003 case USB_SPEED_HIGH:
1004 /* assuming that there is one port */
1005 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1007 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1008 "error=%s\n", usbd_errstr(err));
1011 /* get number of ports */
1012 nports = hubdesc20.bNbrPorts;
1014 /* get power delay */
1015 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1016 usb_extra_power_up_time);
1018 /* get complete HUB descriptor */
1020 /* check number of ports */
1022 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1023 "error=%s\n", usbd_errstr(err));
1026 /* get complete HUB descriptor */
1027 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1030 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1031 "error=%s\n", usbd_errstr(err));
1034 if (hubdesc20.bNbrPorts != nports) {
1035 DPRINTFN(0, "Number of ports changed\n");
1040 case USB_SPEED_SUPER:
1041 if (udev->parent_hub != NULL) {
1042 err = usbd_req_set_hub_depth(udev, NULL,
1045 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1046 "error=%s\n", usbd_errstr(err));
1050 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1052 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1053 "error=%s\n", usbd_errstr(err));
1056 /* get number of ports */
1057 nports = hubdesc30.bNbrPorts;
1059 /* get power delay */
1060 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1061 usb_extra_power_up_time);
1063 /* get complete HUB descriptor */
1065 /* check number of ports */
1066 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1067 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1068 "error=%s\n", usbd_errstr(err));
1071 /* get complete HUB descriptor */
1072 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1075 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1076 "error=%s\n", usbd_errstr(err));
1079 if (hubdesc30.bNbrPorts != nports) {
1080 DPRINTFN(0, "Number of ports changed\n");
1086 DPRINTF("Assuming HUB has only one port\n");
1087 /* default number of ports */
1089 /* default power delay */
1090 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1094 DPRINTFN(0, "portless HUB\n");
1097 if (nports > USB_MAX_PORTS) {
1098 DPRINTF("Port limit exceeded\n");
1101 #if (USB_HAVE_FIXED_PORT == 0)
1102 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1103 M_USBDEV, M_WAITOK | M_ZERO);
1112 /* initialize HUB structure */
1114 hub->explore = &uhub_explore;
1115 hub->nports = nports;
1116 hub->hubudev = udev;
1118 /* if self powered hub, give ports maximum current */
1119 if (udev->flags.self_powered) {
1120 hub->portpower = USB_MAX_POWER;
1122 hub->portpower = USB_MIN_POWER;
1125 /* set up interrupt pipe */
1127 if (udev->parent_hub == NULL) {
1128 /* root HUB is special */
1132 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1133 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1136 DPRINTFN(0, "cannot setup interrupt transfer, "
1137 "errstr=%s\n", usbd_errstr(err));
1140 /* wait with power off for a while */
1141 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1144 * To have the best chance of success we do things in the exact same
1145 * order as Windoze98. This should not be necessary, but some
1146 * devices do not follow the USB specs to the letter.
1148 * These are the events on the bus when a hub is attached:
1149 * Get device and config descriptors (see attach code)
1150 * Get hub descriptor (see above)
1153 * wait for power to become stable
1154 * (all below happens in explore code)
1156 * clear C_PORT_CONNECTION
1159 * if device connected
1163 * clear C_PORT_RESET
1165 * proceed with device attachment
1168 /* XXX should check for none, individual, or ganged power? */
1172 for (x = 0; x != nports; x++) {
1173 /* set up data structures */
1174 struct usb_port *up = hub->ports + x;
1176 up->device_index = 0;
1180 /* check if port is removable */
1181 switch (udev->speed) {
1183 case USB_SPEED_FULL:
1184 case USB_SPEED_HIGH:
1185 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1188 case USB_SPEED_SUPER:
1189 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1193 DPRINTF("Assuming removable port\n");
1198 /* turn the power on */
1199 err = usbd_req_set_port_feature(udev, NULL,
1200 portno, UHF_PORT_POWER);
1203 DPRINTFN(0, "port %d power on failed, %s\n",
1204 portno, usbd_errstr(err));
1206 DPRINTF("turn on port %d power\n",
1209 /* wait for stable power */
1210 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1213 device_printf(dev, "%d port%s with %d "
1214 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1215 removable, udev->flags.self_powered ? "self" : "bus");
1217 /* Start the interrupt endpoint, if any */
1219 if (sc->sc_xfer[0] != NULL) {
1220 mtx_lock(&sc->sc_mtx);
1221 usbd_transfer_start(sc->sc_xfer[0]);
1222 mtx_unlock(&sc->sc_mtx);
1225 /* Enable automatic power save on all USB HUBs */
1227 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1232 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1234 #if (USB_HAVE_FIXED_PORT == 0)
1235 free(udev->hub, M_USBDEV);
1239 mtx_destroy(&sc->sc_mtx);
1245 * Called from process context when the hub is gone.
1246 * Detach all devices on active ports.
1249 uhub_detach(device_t dev)
1251 struct uhub_softc *sc = device_get_softc(dev);
1252 struct usb_hub *hub = sc->sc_udev->hub;
1253 struct usb_device *child;
1256 if (hub == NULL) /* must be partially working */
1259 /* Make sure interrupt transfer is gone. */
1260 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1262 /* Detach all ports */
1263 for (x = 0; x != hub->nports; x++) {
1265 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1267 if (child == NULL) {
1272 * Free USB device and all subdevices, if any.
1274 usb_free_device(child, 0);
1277 #if (USB_HAVE_FIXED_PORT == 0)
1278 free(hub, M_USBDEV);
1280 sc->sc_udev->hub = NULL;
1282 mtx_destroy(&sc->sc_mtx);
1288 uhub_suspend(device_t dev)
1291 /* Sub-devices are not suspended here! */
1296 uhub_resume(device_t dev)
1299 /* Sub-devices are not resumed here! */
1304 uhub_driver_added(device_t dev, driver_t *driver)
1306 usb_needs_explore_all();
1310 struct usb_device *udev;
1312 uint8_t iface_index;
1316 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1317 struct hub_result *res)
1319 struct usb_interface *iface;
1320 struct usb_device *udev;
1325 nports = hub->nports;
1326 for (x = 0; x != nports; x++) {
1327 udev = usb_bus_port_get_device(hub->hubudev->bus,
1332 for (i = 0; i != USB_IFACE_MAX; i++) {
1333 iface = usbd_get_iface(udev, i);
1335 (iface->subdev == child)) {
1336 res->iface_index = i;
1338 res->portno = x + 1;
1343 res->iface_index = 0;
1349 uhub_child_location_string(device_t parent, device_t child,
1350 char *buf, size_t buflen)
1352 struct uhub_softc *sc;
1353 struct usb_hub *hub;
1354 struct hub_result res;
1356 if (!device_is_attached(parent)) {
1362 sc = device_get_softc(parent);
1363 hub = sc->sc_udev->hub;
1366 uhub_find_iface_index(hub, child, &res);
1368 DPRINTF("device not on hub\n");
1374 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1375 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1376 res.portno, device_get_unit(res.udev->bus->bdev),
1377 res.udev->device_index, res.iface_index);
1385 uhub_child_pnpinfo_string(device_t parent, device_t child,
1386 char *buf, size_t buflen)
1388 struct uhub_softc *sc;
1389 struct usb_hub *hub;
1390 struct usb_interface *iface;
1391 struct hub_result res;
1393 if (!device_is_attached(parent)) {
1399 sc = device_get_softc(parent);
1400 hub = sc->sc_udev->hub;
1403 uhub_find_iface_index(hub, child, &res);
1405 DPRINTF("device not on hub\n");
1411 iface = usbd_get_iface(res.udev, res.iface_index);
1412 if (iface && iface->idesc) {
1413 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1414 "devclass=0x%02x devsubclass=0x%02x "
1418 "intclass=0x%02x intsubclass=0x%02x "
1419 "intprotocol=0x%02x " "%s%s",
1420 UGETW(res.udev->ddesc.idVendor),
1421 UGETW(res.udev->ddesc.idProduct),
1422 res.udev->ddesc.bDeviceClass,
1423 res.udev->ddesc.bDeviceSubClass,
1424 usb_get_serial(res.udev),
1425 UGETW(res.udev->ddesc.bcdDevice),
1426 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1427 iface->idesc->bInterfaceClass,
1428 iface->idesc->bInterfaceSubClass,
1429 iface->idesc->bInterfaceProtocol,
1430 iface->pnpinfo ? " " : "",
1431 iface->pnpinfo ? iface->pnpinfo : "");
1445 * The USB Transaction Translator:
1446 * ===============================
1448 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1449 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1450 * USB transfers. To utilize bandwidth dynamically the "scatter and
1451 * gather" principle must be applied. This means that bandwidth must
1452 * be divided into equal parts of bandwidth. With regard to USB all
1453 * data is transferred in smaller packets with length
1454 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1457 * The bandwidth scheduler which I have implemented will simply pack
1458 * the USB transfers back to back until there is no more space in the
1459 * schedule. Out of the 8 microframes which the USB 2.0 standard
1460 * provides, only 6 are available for non-HIGH-speed devices. I have
1461 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1462 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1463 * this division, it is very difficult to allocate and free bandwidth
1466 * NOTE about the Transaction Translator in USB HUBs:
1468 * USB HUBs have a very simple Transaction Translator, that will
1469 * simply pipeline all the SPLIT transactions. That means that the
1470 * transactions will be executed in the order they are queued!
1474 /*------------------------------------------------------------------------*
1475 * usb_intr_find_best_slot
1478 * The best Transaction Translation slot for an interrupt endpoint.
1479 *------------------------------------------------------------------------*/
1481 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1482 uint8_t end, uint8_t mask)
1484 usb_size_t min = (usb_size_t)-1;
1492 /* find the last slot with lesser used bandwidth */
1494 for (x = start; x < end; x++) {
1498 /* compute sum of bandwidth */
1499 for (z = x; z < end; z++) {
1500 if (mask & (1U << (z - x)))
1504 /* check if the current multi-slot is more optimal */
1510 /* check if the mask is about to be shifted out */
1511 if (mask & (1U << (end - 1 - x)))
1517 /*------------------------------------------------------------------------*
1518 * usb_hs_bandwidth_adjust
1520 * This function will update the bandwith usage for the microframe
1521 * having index "slot" by "len" bytes. "len" can be negative. If the
1522 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1523 * the "slot" argument will be replaced by the slot having least used
1524 * bandwidth. The "mask" argument is used for multi-slot allocations.
1527 * The slot in which the bandwidth update was done: 0..7
1528 *------------------------------------------------------------------------*/
1530 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1531 uint8_t slot, uint8_t mask)
1533 struct usb_bus *bus = udev->bus;
1534 struct usb_hub *hub;
1535 enum usb_dev_speed speed;
1538 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1540 speed = usbd_get_speed(udev);
1544 case USB_SPEED_FULL:
1545 if (speed == USB_SPEED_LOW) {
1549 * The Host Controller Driver should have
1550 * performed checks so that the lookup
1551 * below does not result in a NULL pointer
1555 hub = udev->parent_hs_hub->hub;
1556 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1557 slot = usb_intr_find_best_slot(hub->uframe_usage,
1558 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1560 for (x = slot; x < 8; x++) {
1561 if (mask & (1U << (x - slot))) {
1562 hub->uframe_usage[x] += len;
1563 bus->uframe_usage[x] += len;
1568 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1569 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1570 USB_HS_MICRO_FRAMES_MAX, mask);
1572 for (x = slot; x < 8; x++) {
1573 if (mask & (1U << (x - slot))) {
1574 bus->uframe_usage[x] += len;
1582 /*------------------------------------------------------------------------*
1583 * usb_hs_bandwidth_alloc
1585 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1586 *------------------------------------------------------------------------*/
1588 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1590 struct usb_device *udev;
1595 udev = xfer->xroot->udev;
1597 if (udev->flags.usb_mode != USB_MODE_HOST)
1598 return; /* not supported */
1600 xfer->endpoint->refcount_bw++;
1601 if (xfer->endpoint->refcount_bw != 1)
1602 return; /* already allocated */
1604 speed = usbd_get_speed(udev);
1606 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1608 /* allocate a microframe slot */
1611 slot = usb_hs_bandwidth_adjust(udev,
1612 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1614 xfer->endpoint->usb_uframe = slot;
1615 xfer->endpoint->usb_smask = mask << slot;
1617 if ((speed != USB_SPEED_FULL) &&
1618 (speed != USB_SPEED_LOW)) {
1619 xfer->endpoint->usb_cmask = 0x00 ;
1621 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1625 case UE_ISOCHRONOUS:
1626 switch (usbd_xfer_get_fps_shift(xfer)) {
1641 /* allocate a microframe multi-slot */
1643 slot = usb_hs_bandwidth_adjust(udev,
1644 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1646 xfer->endpoint->usb_uframe = slot;
1647 xfer->endpoint->usb_cmask = 0;
1648 xfer->endpoint->usb_smask = mask << slot;
1652 xfer->endpoint->usb_uframe = 0;
1653 xfer->endpoint->usb_cmask = 0;
1654 xfer->endpoint->usb_smask = 0;
1658 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1659 xfer->endpoint->usb_uframe,
1660 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1663 /*------------------------------------------------------------------------*
1664 * usb_hs_bandwidth_free
1666 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1667 *------------------------------------------------------------------------*/
1669 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1671 struct usb_device *udev;
1675 udev = xfer->xroot->udev;
1677 if (udev->flags.usb_mode != USB_MODE_HOST)
1678 return; /* not supported */
1680 xfer->endpoint->refcount_bw--;
1681 if (xfer->endpoint->refcount_bw != 0)
1682 return; /* still allocated */
1684 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1686 case UE_ISOCHRONOUS:
1688 slot = xfer->endpoint->usb_uframe;
1689 mask = xfer->endpoint->usb_smask;
1691 /* free microframe slot(s): */
1692 usb_hs_bandwidth_adjust(udev,
1693 -xfer->max_frame_size, slot, mask >> slot);
1695 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1696 slot, mask >> slot);
1698 xfer->endpoint->usb_uframe = 0;
1699 xfer->endpoint->usb_cmask = 0;
1700 xfer->endpoint->usb_smask = 0;
1708 /*------------------------------------------------------------------------*
1709 * usb_isoc_time_expand
1711 * This function will expand the time counter from 7-bit to 16-bit.
1714 * 16-bit isochronous time counter.
1715 *------------------------------------------------------------------------*/
1717 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1721 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1723 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1725 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1727 if (isoc_time_curr < rem) {
1728 /* the time counter wrapped around */
1729 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1731 /* update the remainder */
1733 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1734 bus->isoc_time_last |= isoc_time_curr;
1736 return (bus->isoc_time_last);
1739 /*------------------------------------------------------------------------*
1740 * usbd_fs_isoc_schedule_alloc_slot
1742 * This function will allocate bandwidth for an isochronous FULL speed
1743 * transaction in the FULL speed schedule.
1748 *------------------------------------------------------------------------*/
1749 #if USB_HAVE_TT_SUPPORT
1751 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1753 struct usb_xfer *xfer;
1754 struct usb_xfer *pipe_xfer;
1755 struct usb_bus *bus;
1757 usb_frlength_t data_len;
1765 bus = isoc_xfer->xroot->bus;
1767 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1769 /* skip self, if any */
1771 if (xfer == isoc_xfer)
1774 /* check if this USB transfer is going through the same TT */
1776 if (xfer->xroot->udev->parent_hs_hub !=
1777 isoc_xfer->xroot->udev->parent_hs_hub) {
1780 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1781 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1782 (xfer->xroot->udev->hs_port_no !=
1783 isoc_xfer->xroot->udev->hs_port_no)) {
1786 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1789 /* check if isoc_time is part of this transfer */
1791 delta = xfer->isoc_time_complete - isoc_time;
1792 if (delta > 0 && delta <= xfer->nframes) {
1793 delta = xfer->nframes - delta;
1795 len = xfer->frlengths[delta];
1804 * Check double buffered transfers. Only stream ID
1805 * equal to zero is valid here!
1807 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
1810 /* skip self, if any */
1812 if (pipe_xfer == isoc_xfer)
1815 /* check if isoc_time is part of this transfer */
1817 delta = pipe_xfer->isoc_time_complete - isoc_time;
1818 if (delta > 0 && delta <= pipe_xfer->nframes) {
1819 delta = pipe_xfer->nframes - delta;
1821 len = pipe_xfer->frlengths[delta];
1831 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1832 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1836 /* check for overflow */
1838 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1843 delta = isoc_xfer->isoc_time_complete - isoc_time;
1844 if (delta > 0 && delta <= isoc_xfer->nframes) {
1845 delta = isoc_xfer->nframes - delta;
1847 len = isoc_xfer->frlengths[delta];
1855 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1856 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1860 /* check for overflow */
1862 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1869 /*------------------------------------------------------------------------*
1870 * usb_bus_port_get_device
1872 * This function is NULL safe.
1873 *------------------------------------------------------------------------*/
1875 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1877 if ((bus == NULL) || (up == NULL)) {
1881 if (up->device_index == 0) {
1885 return (bus->devices[up->device_index]);
1888 /*------------------------------------------------------------------------*
1889 * usb_bus_port_set_device
1891 * This function is NULL safe.
1892 *------------------------------------------------------------------------*/
1894 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1895 struct usb_device *udev, uint8_t device_index)
1902 * There is only one case where we don't
1903 * have an USB port, and that is the Root Hub!
1907 up->device_index = device_index;
1909 device_index = up->device_index;
1910 up->device_index = 0;
1914 * Make relationships to our new device
1916 if (device_index != 0) {
1918 mtx_lock(&usb_ref_lock);
1920 bus->devices[device_index] = udev;
1922 mtx_unlock(&usb_ref_lock);
1928 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1931 /*------------------------------------------------------------------------*
1934 * This functions is called when the USB event thread needs to run.
1935 *------------------------------------------------------------------------*/
1937 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1944 DPRINTF("No bus pointer!\n");
1947 if ((bus->devices == NULL) ||
1948 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1949 DPRINTF("No root HUB\n");
1952 if (mtx_owned(&bus->bus_mtx)) {
1961 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
1962 &bus->explore_msg[0], &bus->explore_msg[1])) {
1966 USB_BUS_UNLOCK(bus);
1970 /*------------------------------------------------------------------------*
1971 * usb_needs_explore_all
1973 * This function is called whenever a new driver is loaded and will
1974 * cause that all USB busses are re-explored.
1975 *------------------------------------------------------------------------*/
1977 usb_needs_explore_all(void)
1979 struct usb_bus *bus;
1986 dc = usb_devclass_ptr;
1988 DPRINTFN(0, "no devclass\n");
1992 * Explore all USB busses in parallell.
1994 max = devclass_get_maxunit(dc);
1996 dev = devclass_get_device(dc, max);
1998 bus = device_get_softc(dev);
2000 usb_needs_explore(bus, 1);
2007 /*------------------------------------------------------------------------*
2008 * usb_bus_power_update
2010 * This function will ensure that all USB devices on the given bus are
2011 * properly suspended or resumed according to the device transfer
2013 *------------------------------------------------------------------------*/
2016 usb_bus_power_update(struct usb_bus *bus)
2018 usb_needs_explore(bus, 0 /* no probe */ );
2022 /*------------------------------------------------------------------------*
2023 * usbd_transfer_power_ref
2025 * This function will modify the power save reference counts and
2026 * wakeup the USB device associated with the given USB transfer, if
2028 *------------------------------------------------------------------------*/
2031 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2033 static const usb_power_mask_t power_mask[4] = {
2034 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2035 [UE_BULK] = USB_HW_POWER_BULK,
2036 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2037 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2039 struct usb_device *udev;
2040 uint8_t needs_explore;
2041 uint8_t needs_hw_power;
2044 udev = xfer->xroot->udev;
2046 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2047 /* no power save for root HUB */
2050 USB_BUS_LOCK(udev->bus);
2052 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2054 udev->pwr_save.last_xfer_time = ticks;
2055 udev->pwr_save.type_refs[xfer_type] += val;
2057 if (xfer->flags_int.control_xfr) {
2058 udev->pwr_save.read_refs += val;
2059 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2061 * It is not allowed to suspend during a
2064 udev->pwr_save.write_refs += val;
2066 } else if (USB_GET_DATA_ISREAD(xfer)) {
2067 udev->pwr_save.read_refs += val;
2069 udev->pwr_save.write_refs += val;
2073 if (udev->flags.self_suspended)
2074 needs_explore = usb_peer_should_wakeup(udev);
2078 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2079 DPRINTF("Adding type %u to power state\n", xfer_type);
2080 udev->bus->hw_power_state |= power_mask[xfer_type];
2090 USB_BUS_UNLOCK(udev->bus);
2092 if (needs_explore) {
2093 DPRINTF("update\n");
2094 usb_bus_power_update(udev->bus);
2095 } else if (needs_hw_power) {
2096 DPRINTF("needs power\n");
2097 if (udev->bus->methods->set_hw_power != NULL) {
2098 (udev->bus->methods->set_hw_power) (udev->bus);
2104 /*------------------------------------------------------------------------*
2105 * usb_peer_should_wakeup
2107 * This function returns non-zero if the current device should wake up.
2108 *------------------------------------------------------------------------*/
2110 usb_peer_should_wakeup(struct usb_device *udev)
2112 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2113 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2114 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2115 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2116 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2117 (udev->pwr_save.write_refs != 0) ||
2118 ((udev->pwr_save.read_refs != 0) &&
2119 (udev->flags.usb_mode == USB_MODE_HOST) &&
2120 (usb_peer_can_wakeup(udev) == 0)));
2123 /*------------------------------------------------------------------------*
2126 * This function implements the USB power daemon and is called
2127 * regularly from the USB explore thread.
2128 *------------------------------------------------------------------------*/
2131 usb_bus_powerd(struct usb_bus *bus)
2133 struct usb_device *udev;
2136 usb_ticks_t mintime;
2137 usb_size_t type_refs[5];
2140 limit = usb_power_timeout;
2143 else if (limit > 255)
2148 DPRINTF("bus=%p\n", bus);
2153 * The root HUB device is never suspended
2154 * and we simply skip it.
2156 for (x = USB_ROOT_HUB_ADDR + 1;
2157 x != bus->devices_max; x++) {
2159 udev = bus->devices[x];
2163 temp = ticks - udev->pwr_save.last_xfer_time;
2165 if (usb_peer_should_wakeup(udev)) {
2166 /* check if we are suspended */
2167 if (udev->flags.self_suspended != 0) {
2168 USB_BUS_UNLOCK(bus);
2169 usb_dev_resume_peer(udev);
2172 } else if ((temp >= limit) &&
2173 (udev->flags.usb_mode == USB_MODE_HOST) &&
2174 (udev->flags.self_suspended == 0)) {
2175 /* try to do suspend */
2177 USB_BUS_UNLOCK(bus);
2178 usb_dev_suspend_peer(udev);
2183 /* reset counters */
2185 mintime = (usb_ticks_t)-1;
2192 /* Re-loop all the devices to get the actual state */
2194 for (x = USB_ROOT_HUB_ADDR + 1;
2195 x != bus->devices_max; x++) {
2197 udev = bus->devices[x];
2201 /* we found a non-Root-Hub USB device */
2204 /* "last_xfer_time" can be updated by a resume */
2205 temp = ticks - udev->pwr_save.last_xfer_time;
2208 * Compute minimum time since last transfer for the complete
2214 if (udev->flags.self_suspended == 0) {
2215 type_refs[0] += udev->pwr_save.type_refs[0];
2216 type_refs[1] += udev->pwr_save.type_refs[1];
2217 type_refs[2] += udev->pwr_save.type_refs[2];
2218 type_refs[3] += udev->pwr_save.type_refs[3];
2222 if (mintime >= (usb_ticks_t)(1 * hz)) {
2223 /* recompute power masks */
2224 DPRINTF("Recomputing power masks\n");
2225 bus->hw_power_state = 0;
2226 if (type_refs[UE_CONTROL] != 0)
2227 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2228 if (type_refs[UE_BULK] != 0)
2229 bus->hw_power_state |= USB_HW_POWER_BULK;
2230 if (type_refs[UE_INTERRUPT] != 0)
2231 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2232 if (type_refs[UE_ISOCHRONOUS] != 0)
2233 bus->hw_power_state |= USB_HW_POWER_ISOC;
2234 if (type_refs[4] != 0)
2235 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2237 USB_BUS_UNLOCK(bus);
2239 if (bus->methods->set_hw_power != NULL) {
2240 /* always update hardware power! */
2241 (bus->methods->set_hw_power) (bus);
2247 /*------------------------------------------------------------------------*
2248 * usb_dev_resume_peer
2250 * This function will resume an USB peer and do the required USB
2251 * signalling to get an USB device out of the suspended state.
2252 *------------------------------------------------------------------------*/
2254 usb_dev_resume_peer(struct usb_device *udev)
2256 struct usb_bus *bus;
2263 /* check if already resumed */
2264 if (udev->flags.self_suspended == 0)
2267 /* we need a parent HUB to do resume */
2268 if (udev->parent_hub == NULL)
2271 DPRINTF("udev=%p\n", udev);
2273 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2274 (udev->flags.remote_wakeup == 0)) {
2276 * If the host did not set the remote wakeup feature, we can
2277 * not wake it up either!
2279 DPRINTF("remote wakeup is not set!\n");
2282 /* get bus pointer */
2285 /* resume parent hub first */
2286 usb_dev_resume_peer(udev->parent_hub);
2288 /* reduce chance of instant resume failure by waiting a little bit */
2289 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2291 if (usb_device_20_compatible(udev)) {
2292 /* resume current port (Valid in Host and Device Mode) */
2293 err = usbd_req_clear_port_feature(udev->parent_hub,
2294 NULL, udev->port_no, UHF_PORT_SUSPEND);
2296 DPRINTFN(0, "Resuming port failed\n");
2300 /* resume current port (Valid in Host and Device Mode) */
2301 err = usbd_req_set_port_link_state(udev->parent_hub,
2302 NULL, udev->port_no, UPS_PORT_LS_U0);
2304 DPRINTFN(0, "Resuming port failed\n");
2309 /* resume settle time */
2310 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2312 if (bus->methods->device_resume != NULL) {
2313 /* resume USB device on the USB controller */
2314 (bus->methods->device_resume) (udev);
2317 /* set that this device is now resumed */
2318 udev->flags.self_suspended = 0;
2320 /* make sure that we don't go into suspend right away */
2321 udev->pwr_save.last_xfer_time = ticks;
2323 /* make sure the needed power masks are on */
2324 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2325 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2326 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2327 bus->hw_power_state |= USB_HW_POWER_BULK;
2328 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2329 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2330 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2331 bus->hw_power_state |= USB_HW_POWER_ISOC;
2333 USB_BUS_UNLOCK(bus);
2335 if (bus->methods->set_hw_power != NULL) {
2336 /* always update hardware power! */
2337 (bus->methods->set_hw_power) (bus);
2342 /* notify all sub-devices about resume */
2343 err = usb_suspend_resume(udev, 0);
2345 usbd_sr_unlock(udev);
2347 /* check if peer has wakeup capability */
2348 if (usb_peer_can_wakeup(udev)) {
2349 /* clear remote wakeup */
2350 err = usbd_req_clear_device_feature(udev,
2351 NULL, UF_DEVICE_REMOTE_WAKEUP);
2353 DPRINTFN(0, "Clearing device "
2354 "remote wakeup failed: %s\n",
2360 /*------------------------------------------------------------------------*
2361 * usb_dev_suspend_peer
2363 * This function will suspend an USB peer and do the required USB
2364 * signalling to get an USB device into the suspended state.
2365 *------------------------------------------------------------------------*/
2367 usb_dev_suspend_peer(struct usb_device *udev)
2369 struct usb_device *child;
2379 /* check if already suspended */
2380 if (udev->flags.self_suspended)
2383 /* we need a parent HUB to do suspend */
2384 if (udev->parent_hub == NULL)
2387 DPRINTF("udev=%p\n", udev);
2389 /* check if the current device is a HUB */
2390 if (udev->hub != NULL) {
2391 nports = udev->hub->nports;
2393 /* check if all devices on the HUB are suspended */
2394 for (x = 0; x != nports; x++) {
2395 child = usb_bus_port_get_device(udev->bus,
2396 udev->hub->ports + x);
2401 if (child->flags.self_suspended)
2404 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2409 if (usb_peer_can_wakeup(udev)) {
2411 * This request needs to be done before we set
2412 * "udev->flags.self_suspended":
2415 /* allow device to do remote wakeup */
2416 err = usbd_req_set_device_feature(udev,
2417 NULL, UF_DEVICE_REMOTE_WAKEUP);
2419 DPRINTFN(0, "Setting device "
2420 "remote wakeup failed\n");
2424 USB_BUS_LOCK(udev->bus);
2426 * Checking for suspend condition and setting suspended bit
2429 err = usb_peer_should_wakeup(udev);
2432 * Set that this device is suspended. This variable
2433 * must be set before calling USB controller suspend
2436 udev->flags.self_suspended = 1;
2438 USB_BUS_UNLOCK(udev->bus);
2441 if (usb_peer_can_wakeup(udev)) {
2442 /* allow device to do remote wakeup */
2443 err = usbd_req_clear_device_feature(udev,
2444 NULL, UF_DEVICE_REMOTE_WAKEUP);
2446 DPRINTFN(0, "Setting device "
2447 "remote wakeup failed\n");
2451 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2452 /* resume parent HUB first */
2453 usb_dev_resume_peer(udev->parent_hub);
2455 /* reduce chance of instant resume failure by waiting a little bit */
2456 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2458 /* resume current port (Valid in Host and Device Mode) */
2459 err = usbd_req_clear_port_feature(udev->parent_hub,
2460 NULL, udev->port_no, UHF_PORT_SUSPEND);
2462 /* resume settle time */
2463 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2465 DPRINTF("Suspend was cancelled!\n");
2471 /* notify all sub-devices about suspend */
2472 err = usb_suspend_resume(udev, 1);
2474 usbd_sr_unlock(udev);
2476 if (udev->bus->methods->device_suspend != NULL) {
2479 /* suspend device on the USB controller */
2480 (udev->bus->methods->device_suspend) (udev);
2483 temp = usbd_get_dma_delay(udev);
2485 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2489 if (usb_device_20_compatible(udev)) {
2490 /* suspend current port */
2491 err = usbd_req_set_port_feature(udev->parent_hub,
2492 NULL, udev->port_no, UHF_PORT_SUSPEND);
2494 DPRINTFN(0, "Suspending port failed\n");
2498 /* suspend current port */
2499 err = usbd_req_set_port_link_state(udev->parent_hub,
2500 NULL, udev->port_no, UPS_PORT_LS_U3);
2502 DPRINTFN(0, "Suspending port failed\n");
2507 udev = udev->parent_hub;
2511 /*------------------------------------------------------------------------*
2512 * usbd_set_power_mode
2514 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2516 *------------------------------------------------------------------------*/
2518 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2520 /* filter input argument */
2521 if ((power_mode != USB_POWER_MODE_ON) &&
2522 (power_mode != USB_POWER_MODE_OFF))
2523 power_mode = USB_POWER_MODE_SAVE;
2525 power_mode = usbd_filter_power_mode(udev, power_mode);
2527 udev->power_mode = power_mode; /* update copy of power mode */
2530 usb_bus_power_update(udev->bus);
2532 usb_needs_explore(udev->bus, 0 /* no probe */ );
2536 /*------------------------------------------------------------------------*
2537 * usbd_filter_power_mode
2539 * This function filters the power mode based on hardware requirements.
2540 *------------------------------------------------------------------------*/
2542 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2544 struct usb_bus_methods *mtod;
2547 mtod = udev->bus->methods;
2550 if (mtod->get_power_mode != NULL)
2551 (mtod->get_power_mode) (udev, &temp);
2553 /* check if we should not filter */
2555 return (power_mode);
2557 /* use fixed power mode given by hardware driver */
2561 /*------------------------------------------------------------------------*
2562 * usbd_start_re_enumerate
2564 * This function starts re-enumeration of the given USB device. This
2565 * function does not need to be called BUS-locked. This function does
2566 * not wait until the re-enumeration is completed.
2567 *------------------------------------------------------------------------*/
2569 usbd_start_re_enumerate(struct usb_device *udev)
2571 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2572 udev->re_enumerate_wait = USB_RE_ENUM_START;
2573 usb_needs_explore(udev->bus, 0);