3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_ioctl.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
57 #define USB_DEBUG_VAR uhub_debug
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/usb_device.h>
62 #include <dev/usb/usb_request.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_hub.h>
65 #include <dev/usb/usb_util.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_dynamic.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
73 #define UHUB_INTR_INTERVAL 250 /* ms */
74 #define UHUB_N_TRANSFER 1
77 static int uhub_debug = 0;
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
80 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
82 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
86 static int usb_power_timeout = 30; /* seconds */
88 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
89 &usb_power_timeout, 0, "USB power timeout");
92 struct uhub_current_state {
98 struct uhub_current_state sc_st;/* current state */
99 device_t sc_dev; /* base device */
100 struct mtx sc_mtx; /* our mutex */
101 struct usb_device *sc_udev; /* USB device */
102 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
104 #define UHUB_FLAG_DID_EXPLORE 0x01
107 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
108 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
109 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
110 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
111 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
113 /* prototypes for type checking: */
115 static device_probe_t uhub_probe;
116 static device_attach_t uhub_attach;
117 static device_detach_t uhub_detach;
118 static device_suspend_t uhub_suspend;
119 static device_resume_t uhub_resume;
121 static bus_driver_added_t uhub_driver_added;
122 static bus_child_location_str_t uhub_child_location_string;
123 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
125 static usb_callback_t uhub_intr_callback;
127 static void usb_dev_resume_peer(struct usb_device *udev);
128 static void usb_dev_suspend_peer(struct usb_device *udev);
129 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
131 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
134 .type = UE_INTERRUPT,
135 .endpoint = UE_ADDR_ANY,
136 .direction = UE_DIR_ANY,
138 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
139 .bufsize = 0, /* use wMaxPacketSize */
140 .callback = &uhub_intr_callback,
141 .interval = UHUB_INTR_INTERVAL,
146 * driver instance for "hub" connected to "usb"
147 * and "hub" connected to "hub"
149 static devclass_t uhub_devclass;
151 static device_method_t uhub_methods[] = {
152 DEVMETHOD(device_probe, uhub_probe),
153 DEVMETHOD(device_attach, uhub_attach),
154 DEVMETHOD(device_detach, uhub_detach),
156 DEVMETHOD(device_suspend, uhub_suspend),
157 DEVMETHOD(device_resume, uhub_resume),
159 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
160 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
161 DEVMETHOD(bus_driver_added, uhub_driver_added),
165 static driver_t uhub_driver = {
167 .methods = uhub_methods,
168 .size = sizeof(struct uhub_softc)
171 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
172 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
173 MODULE_VERSION(uhub, 1);
176 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
178 struct uhub_softc *sc = usbd_xfer_softc(xfer);
180 switch (USB_GET_STATE(xfer)) {
181 case USB_ST_TRANSFERRED:
184 * This is an indication that some port
185 * has changed status. Notify the bus
186 * event handler thread that we need
187 * to be explored again:
189 usb_needs_explore(sc->sc_udev->bus, 0);
192 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
193 usbd_transfer_submit(xfer);
197 if (xfer->error != USB_ERR_CANCELLED) {
199 * Do a clear-stall. The "stall_pipe" flag
200 * will get cleared before next callback by
203 usbd_xfer_set_stall(xfer);
204 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
205 usbd_transfer_submit(xfer);
211 /*------------------------------------------------------------------------*
212 * uhub_explore_sub - subroutine
216 * Else: A control transaction failed
217 *------------------------------------------------------------------------*/
219 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
222 struct usb_device *child;
226 bus = sc->sc_udev->bus;
229 /* get driver added refcount from USB bus */
230 refcount = bus->driver_added_refcount;
232 /* get device assosiated with the given port */
233 child = usb_bus_port_get_device(bus, up);
239 /* check if device should be re-enumerated */
241 if (child->flags.usb_mode == USB_MODE_HOST) {
242 usbd_enum_lock(child);
243 if (child->re_enumerate_wait) {
244 err = usbd_set_config_index(child,
247 DPRINTF("Unconfigure failed: "
251 err = usbd_req_re_enumerate(child, NULL);
253 err = usbd_set_config_index(child, 0);
255 err = usb_probe_and_attach(child,
256 USB_IFACE_INDEX_ANY);
258 child->re_enumerate_wait = 0;
261 usbd_enum_unlock(child);
264 /* check if probe and attach should be done */
266 if (child->driver_added_refcount != refcount) {
267 child->driver_added_refcount = refcount;
268 err = usb_probe_and_attach(child,
269 USB_IFACE_INDEX_ANY);
274 /* start control transfer, if device mode */
276 if (child->flags.usb_mode == USB_MODE_DEVICE)
277 usbd_ctrl_transfer_setup(child);
279 /* if a HUB becomes present, do a recursive HUB explore */
282 err = (child->hub->explore) (child);
288 /*------------------------------------------------------------------------*
289 * uhub_read_port_status - factored out code
290 *------------------------------------------------------------------------*/
292 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
294 struct usb_port_status ps;
297 err = usbd_req_get_port_status(
298 sc->sc_udev, NULL, &ps, portno);
300 /* update status regardless of error */
302 sc->sc_st.port_status = UGETW(ps.wPortStatus);
303 sc->sc_st.port_change = UGETW(ps.wPortChange);
305 /* debugging print */
307 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
308 "wPortChange=0x%04x, err=%s\n",
309 portno, sc->sc_st.port_status,
310 sc->sc_st.port_change, usbd_errstr(err));
314 /*------------------------------------------------------------------------*
319 * Else: A control transaction failed
320 *------------------------------------------------------------------------*/
322 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
324 struct usb_device *child;
325 struct usb_device *udev;
326 enum usb_dev_speed speed;
327 enum usb_hc_mode mode;
332 DPRINTF("reattaching port %d\n", portno);
337 child = usb_bus_port_get_device(udev->bus,
338 udev->hub->ports + portno - 1);
342 /* first clear the port connection change bit */
344 err = usbd_req_clear_port_feature(udev, NULL,
345 portno, UHF_C_PORT_CONNECTION);
350 /* check if there is a child */
354 * Free USB device and all subdevices, if any.
356 usb_free_device(child, 0);
359 /* get fresh status */
361 err = uhub_read_port_status(sc, portno);
365 /* check if nothing is connected to the port */
367 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
370 /* check if there is no power on the port and print a warning */
372 switch (udev->speed) {
376 power_mask = UPS_PORT_POWER;
378 case USB_SPEED_SUPER:
379 if (udev->parent_hub == NULL)
380 power_mask = UPS_PORT_POWER;
382 power_mask = UPS_PORT_POWER_SS;
388 if (!(sc->sc_st.port_status & power_mask)) {
389 DPRINTF("WARNING: strange, connected port %d "
390 "has no power\n", portno);
393 /* check if the device is in Host Mode */
395 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
397 DPRINTF("Port %d is in Host Mode\n", portno);
399 if (sc->sc_st.port_status & UPS_SUSPEND) {
401 * NOTE: Should not get here in SuperSpeed
402 * mode, because the HUB should report this
405 DPRINTF("Port %d was still "
406 "suspended, clearing.\n", portno);
407 err = usbd_req_clear_port_feature(udev,
408 NULL, portno, UHF_PORT_SUSPEND);
413 /* wait for maximum device power up time */
416 USB_MS_TO_TICKS(usb_port_powerup_delay));
418 /* reset port, which implies enabling it */
420 err = usbd_req_reset_port(udev, NULL, portno);
423 DPRINTFN(0, "port %d reset "
424 "failed, error=%s\n",
425 portno, usbd_errstr(err));
428 /* get port status again, it might have changed during reset */
430 err = uhub_read_port_status(sc, portno);
434 /* check if something changed during port reset */
436 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
437 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
439 DPRINTFN(0, "giving up port reset "
440 "- device vanished\n");
447 DPRINTF("Port %d is in Device Mode\n", portno);
451 * Figure out the device speed
453 switch (udev->speed) {
455 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
456 speed = USB_SPEED_HIGH;
457 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
458 speed = USB_SPEED_LOW;
460 speed = USB_SPEED_FULL;
463 if (sc->sc_st.port_status & UPS_LOW_SPEED)
464 speed = USB_SPEED_LOW;
466 speed = USB_SPEED_FULL;
469 speed = USB_SPEED_LOW;
471 case USB_SPEED_SUPER:
472 if (udev->parent_hub == NULL) {
473 /* Root HUB - special case */
474 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
476 speed = USB_SPEED_FULL;
479 speed = USB_SPEED_LOW;
482 speed = USB_SPEED_HIGH;
485 speed = USB_SPEED_SUPER;
489 speed = USB_SPEED_SUPER;
493 /* same speed like parent */
497 if (speed == USB_SPEED_SUPER) {
498 err = usbd_req_set_hub_u1_timeout(udev, NULL,
499 portno, 128 - (2 * udev->depth));
501 DPRINTFN(0, "port %d U1 timeout "
502 "failed, error=%s\n",
503 portno, usbd_errstr(err));
505 err = usbd_req_set_hub_u2_timeout(udev, NULL,
506 portno, 128 - (2 * udev->depth));
508 DPRINTFN(0, "port %d U2 timeout "
509 "failed, error=%s\n",
510 portno, usbd_errstr(err));
515 * Figure out the device mode
517 * NOTE: This part is currently FreeBSD specific.
519 if (udev->parent_hub != NULL) {
520 /* inherit mode from the parent HUB */
521 mode = udev->parent_hub->flags.usb_mode;
522 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
523 mode = USB_MODE_DEVICE;
525 mode = USB_MODE_HOST;
527 /* need to create a new child */
528 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
529 udev->depth + 1, portno - 1, portno, speed, mode);
531 DPRINTFN(0, "could not allocate new device\n");
534 return (0); /* success */
539 * Free USB device and all subdevices, if any.
541 usb_free_device(child, 0);
545 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
546 err = usbd_req_clear_port_feature(
548 portno, UHF_PORT_ENABLE);
552 DPRINTFN(0, "device problem (%s), "
553 "disabling port %d\n", usbd_errstr(err), portno);
558 /*------------------------------------------------------------------------*
559 * usb_device_20_compatible
562 * 0: HUB does not support suspend and resume
563 * Else: HUB supports suspend and resume
564 *------------------------------------------------------------------------*/
566 usb_device_20_compatible(struct usb_device *udev)
570 switch (udev->speed) {
580 /*------------------------------------------------------------------------*
581 * uhub_suspend_resume_port
585 * Else: A control transaction failed
586 *------------------------------------------------------------------------*/
588 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
590 struct usb_device *child;
591 struct usb_device *udev;
595 DPRINTF("port %d\n", portno);
598 child = usb_bus_port_get_device(udev->bus,
599 udev->hub->ports + portno - 1);
601 /* first clear the port suspend change bit */
603 if (usb_device_20_compatible(udev)) {
604 err = usbd_req_clear_port_feature(udev, NULL,
605 portno, UHF_C_PORT_SUSPEND);
607 err = usbd_req_clear_port_feature(udev, NULL,
608 portno, UHF_C_PORT_LINK_STATE);
612 DPRINTF("clearing suspend failed.\n");
615 /* get fresh status */
617 err = uhub_read_port_status(sc, portno);
619 DPRINTF("reading port status failed.\n");
622 /* convert current state */
624 if (usb_device_20_compatible(udev)) {
625 if (sc->sc_st.port_status & UPS_SUSPEND) {
631 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
635 case UPS_PORT_LS_SS_INA:
636 usbd_req_warm_reset_port(udev, NULL, portno);
645 DPRINTF("suspended=%u\n", is_suspend);
647 /* do the suspend or resume */
651 * This code handle two cases: 1) Host Mode - we can only
652 * receive resume here 2) Device Mode - we can receive
653 * suspend and resume here
656 usb_dev_resume_peer(child);
657 else if (child->flags.usb_mode == USB_MODE_DEVICE)
658 usb_dev_suspend_peer(child);
664 /*------------------------------------------------------------------------*
665 * uhub_root_interrupt
667 * This function is called when a Root HUB interrupt has
668 * happened. "ptr" and "len" makes up the Root HUB interrupt
669 * packet. This function is called having the "bus_mtx" locked.
670 *------------------------------------------------------------------------*/
672 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
674 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
676 usb_needs_explore(bus, 0);
680 uhub_is_too_deep(struct usb_device *udev)
682 switch (udev->speed) {
686 if (udev->depth > USB_HUB_MAX_DEPTH)
689 case USB_SPEED_SUPER:
690 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
699 /*------------------------------------------------------------------------*
705 *------------------------------------------------------------------------*/
707 uhub_explore(struct usb_device *udev)
710 struct uhub_softc *sc;
719 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
721 /* ignore devices that are too deep */
722 if (uhub_is_too_deep(udev))
723 return (USB_ERR_TOO_DEEP);
725 /* check if device is suspended */
726 if (udev->flags.self_suspended) {
727 /* need to wait until the child signals resume */
728 DPRINTF("Device is suspended!\n");
733 * Make sure we don't race against user-space applications
736 usbd_enum_lock(udev);
738 for (x = 0; x != hub->nports; x++) {
742 err = uhub_read_port_status(sc, portno);
744 /* most likely the HUB is gone */
747 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
748 DPRINTF("Overcurrent on port %u.\n", portno);
749 err = usbd_req_clear_port_feature(
750 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
752 /* most likely the HUB is gone */
756 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
758 * Fake a connect status change so that the
759 * status gets checked initially!
761 sc->sc_st.port_change |=
762 UPS_C_CONNECT_STATUS;
764 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
765 err = usbd_req_clear_port_feature(
766 udev, NULL, portno, UHF_C_PORT_ENABLE);
768 /* most likely the HUB is gone */
771 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
773 * Ignore the port error if the device
776 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
777 DPRINTFN(0, "illegal enable change, "
778 "port %d\n", portno);
781 if (up->restartcnt == USB_RESTART_MAX) {
782 /* XXX could try another speed ? */
783 DPRINTFN(0, "port error, giving up "
784 "port %d\n", portno);
786 sc->sc_st.port_change |=
787 UPS_C_CONNECT_STATUS;
792 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
793 err = uhub_reattach_port(sc, portno);
795 /* most likely the HUB is gone */
799 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
800 UPS_C_PORT_LINK_STATE)) {
801 err = uhub_suspend_resume_port(sc, portno);
803 /* most likely the HUB is gone */
807 err = uhub_explore_sub(sc, up);
809 /* no device(s) present */
812 /* explore succeeded - reset restart counter */
816 usbd_enum_unlock(udev);
818 /* initial status checked */
819 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
822 return (USB_ERR_NORMAL_COMPLETION);
826 uhub_probe(device_t dev)
828 struct usb_attach_arg *uaa = device_get_ivars(dev);
830 if (uaa->usb_mode != USB_MODE_HOST)
834 * The subclass for USB HUBs is currently ignored because it
835 * is 0 for some and 1 for others.
837 if (uaa->info.bConfigIndex == 0 &&
838 uaa->info.bDeviceClass == UDCLASS_HUB)
844 /* NOTE: The information returned by this function can be wrong. */
846 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
848 struct usb_hub_descriptor hubdesc20;
849 struct usb_hub_ss_descriptor hubdesc30;
854 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
855 return (USB_ERR_INVAL);
860 switch (udev->speed) {
864 /* assuming that there is one port */
865 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
867 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
868 "error=%s\n", usbd_errstr(err));
871 nports = hubdesc20.bNbrPorts;
875 if (udev->speed == USB_SPEED_HIGH)
876 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
879 case USB_SPEED_SUPER:
880 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
882 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
883 "error=%s\n", usbd_errstr(err));
886 nports = hubdesc30.bNbrPorts;
906 uhub_attach(device_t dev)
908 struct uhub_softc *sc = device_get_softc(dev);
909 struct usb_attach_arg *uaa = device_get_ivars(dev);
910 struct usb_device *udev = uaa->device;
911 struct usb_device *parent_hub = udev->parent_hub;
913 struct usb_hub_descriptor hubdesc20;
914 struct usb_hub_ss_descriptor hubdesc30;
926 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
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");
951 if (UHUB_IS_MULTI_TT(sc)) {
952 err = usbd_set_alt_interface_index(udev, 0, 1);
954 device_printf(dev, "MTT could not be enabled\n");
957 device_printf(dev, "MTT enabled\n");
960 /* get HUB descriptor */
962 DPRINTFN(2, "Getting HUB descriptor\n");
964 switch (udev->speed) {
968 /* assuming that there is one port */
969 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
971 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
972 "error=%s\n", usbd_errstr(err));
975 /* get number of ports */
976 nports = hubdesc20.bNbrPorts;
978 /* get power delay */
979 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
980 usb_extra_power_up_time);
982 /* get complete HUB descriptor */
984 /* check number of ports */
986 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
987 "error=%s\n", usbd_errstr(err));
990 /* get complete HUB descriptor */
991 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
994 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
995 "error=%s\n", usbd_errstr(err));
998 if (hubdesc20.bNbrPorts != nports) {
999 DPRINTFN(0, "Number of ports changed\n");
1004 case USB_SPEED_SUPER:
1005 if (udev->parent_hub != NULL) {
1006 err = usbd_req_set_hub_depth(udev, NULL,
1009 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1010 "error=%s\n", usbd_errstr(err));
1014 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1016 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1017 "error=%s\n", usbd_errstr(err));
1020 /* get number of ports */
1021 nports = hubdesc30.bNbrPorts;
1023 /* get power delay */
1024 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1025 usb_extra_power_up_time);
1027 /* get complete HUB descriptor */
1029 /* check number of ports */
1030 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1031 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1032 "error=%s\n", usbd_errstr(err));
1035 /* get complete HUB descriptor */
1036 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1039 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1040 "error=%s\n", usbd_errstr(err));
1043 if (hubdesc30.bNbrPorts != nports) {
1044 DPRINTFN(0, "Number of ports changed\n");
1050 DPRINTF("Assuming HUB has only one port\n");
1051 /* default number of ports */
1053 /* default power delay */
1054 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1058 DPRINTFN(0, "portless HUB\n");
1061 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1062 M_USBDEV, M_WAITOK | M_ZERO);
1069 /* initialize HUB structure */
1071 hub->explore = &uhub_explore;
1072 hub->nports = nports;
1073 hub->hubudev = udev;
1075 /* if self powered hub, give ports maximum current */
1076 if (udev->flags.self_powered) {
1077 hub->portpower = USB_MAX_POWER;
1079 hub->portpower = USB_MIN_POWER;
1082 /* set up interrupt pipe */
1084 if (udev->parent_hub == NULL) {
1085 /* root HUB is special */
1089 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1090 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1093 DPRINTFN(0, "cannot setup interrupt transfer, "
1094 "errstr=%s\n", usbd_errstr(err));
1097 /* wait with power off for a while */
1098 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1101 * To have the best chance of success we do things in the exact same
1102 * order as Windoze98. This should not be necessary, but some
1103 * devices do not follow the USB specs to the letter.
1105 * These are the events on the bus when a hub is attached:
1106 * Get device and config descriptors (see attach code)
1107 * Get hub descriptor (see above)
1110 * wait for power to become stable
1111 * (all below happens in explore code)
1113 * clear C_PORT_CONNECTION
1116 * if device connected
1120 * clear C_PORT_RESET
1122 * proceed with device attachment
1125 /* XXX should check for none, individual, or ganged power? */
1129 for (x = 0; x != nports; x++) {
1130 /* set up data structures */
1131 struct usb_port *up = hub->ports + x;
1133 up->device_index = 0;
1137 /* check if port is removable */
1138 switch (udev->speed) {
1140 case USB_SPEED_FULL:
1141 case USB_SPEED_HIGH:
1142 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1145 case USB_SPEED_SUPER:
1146 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1150 DPRINTF("Assuming removable port\n");
1155 /* turn the power on */
1156 err = usbd_req_set_port_feature(udev, NULL,
1157 portno, UHF_PORT_POWER);
1160 DPRINTFN(0, "port %d power on failed, %s\n",
1161 portno, usbd_errstr(err));
1163 DPRINTF("turn on port %d power\n",
1166 /* wait for stable power */
1167 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1170 device_printf(dev, "%d port%s with %d "
1171 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1172 removable, udev->flags.self_powered ? "self" : "bus");
1174 /* Start the interrupt endpoint, if any */
1176 if (sc->sc_xfer[0] != NULL) {
1177 mtx_lock(&sc->sc_mtx);
1178 usbd_transfer_start(sc->sc_xfer[0]);
1179 mtx_unlock(&sc->sc_mtx);
1182 /* Enable automatic power save on all USB HUBs */
1184 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1189 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1192 free(udev->hub, M_USBDEV);
1196 mtx_destroy(&sc->sc_mtx);
1202 * Called from process context when the hub is gone.
1203 * Detach all devices on active ports.
1206 uhub_detach(device_t dev)
1208 struct uhub_softc *sc = device_get_softc(dev);
1209 struct usb_hub *hub = sc->sc_udev->hub;
1210 struct usb_device *child;
1213 if (hub == NULL) /* must be partially working */
1216 /* Make sure interrupt transfer is gone. */
1217 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1219 /* Detach all ports */
1220 for (x = 0; x != hub->nports; x++) {
1222 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1224 if (child == NULL) {
1229 * Free USB device and all subdevices, if any.
1231 usb_free_device(child, 0);
1234 free(hub, M_USBDEV);
1235 sc->sc_udev->hub = NULL;
1237 mtx_destroy(&sc->sc_mtx);
1243 uhub_suspend(device_t dev)
1246 /* Sub-devices are not suspended here! */
1251 uhub_resume(device_t dev)
1254 /* Sub-devices are not resumed here! */
1259 uhub_driver_added(device_t dev, driver_t *driver)
1261 usb_needs_explore_all();
1265 struct usb_device *udev;
1267 uint8_t iface_index;
1271 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1272 struct hub_result *res)
1274 struct usb_interface *iface;
1275 struct usb_device *udev;
1280 nports = hub->nports;
1281 for (x = 0; x != nports; x++) {
1282 udev = usb_bus_port_get_device(hub->hubudev->bus,
1287 for (i = 0; i != USB_IFACE_MAX; i++) {
1288 iface = usbd_get_iface(udev, i);
1290 (iface->subdev == child)) {
1291 res->iface_index = i;
1293 res->portno = x + 1;
1298 res->iface_index = 0;
1304 uhub_child_location_string(device_t parent, device_t child,
1305 char *buf, size_t buflen)
1307 struct uhub_softc *sc;
1308 struct usb_hub *hub;
1309 struct hub_result res;
1311 if (!device_is_attached(parent)) {
1317 sc = device_get_softc(parent);
1318 hub = sc->sc_udev->hub;
1321 uhub_find_iface_index(hub, child, &res);
1323 DPRINTF("device not on hub\n");
1329 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1330 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1331 res.portno, device_get_unit(res.udev->bus->bdev),
1332 res.udev->device_index, res.iface_index);
1340 uhub_child_pnpinfo_string(device_t parent, device_t child,
1341 char *buf, size_t buflen)
1343 struct uhub_softc *sc;
1344 struct usb_hub *hub;
1345 struct usb_interface *iface;
1346 struct hub_result res;
1348 if (!device_is_attached(parent)) {
1354 sc = device_get_softc(parent);
1355 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 snprintf(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 : "");
1400 * The USB Transaction Translator:
1401 * ===============================
1403 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1404 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1405 * USB transfers. To utilize bandwidth dynamically the "scatter and
1406 * gather" principle must be applied. This means that bandwidth must
1407 * be divided into equal parts of bandwidth. With regard to USB all
1408 * data is transferred in smaller packets with length
1409 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1412 * The bandwidth scheduler which I have implemented will simply pack
1413 * the USB transfers back to back until there is no more space in the
1414 * schedule. Out of the 8 microframes which the USB 2.0 standard
1415 * provides, only 6 are available for non-HIGH-speed devices. I have
1416 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1417 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1418 * this division, it is very difficult to allocate and free bandwidth
1421 * NOTE about the Transaction Translator in USB HUBs:
1423 * USB HUBs have a very simple Transaction Translator, that will
1424 * simply pipeline all the SPLIT transactions. That means that the
1425 * transactions will be executed in the order they are queued!
1429 /*------------------------------------------------------------------------*
1430 * usb_intr_find_best_slot
1433 * The best Transaction Translation slot for an interrupt endpoint.
1434 *------------------------------------------------------------------------*/
1436 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1437 uint8_t end, uint8_t mask)
1439 usb_size_t min = (usb_size_t)-1;
1447 /* find the last slot with lesser used bandwidth */
1449 for (x = start; x < end; x++) {
1453 /* compute sum of bandwidth */
1454 for (z = x; z < end; z++) {
1455 if (mask & (1U << (z - x)))
1459 /* check if the current multi-slot is more optimal */
1465 /* check if the mask is about to be shifted out */
1466 if (mask & (1U << (end - 1 - x)))
1472 /*------------------------------------------------------------------------*
1473 * usb_hs_bandwidth_adjust
1475 * This function will update the bandwith usage for the microframe
1476 * having index "slot" by "len" bytes. "len" can be negative. If the
1477 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1478 * the "slot" argument will be replaced by the slot having least used
1479 * bandwidth. The "mask" argument is used for multi-slot allocations.
1482 * The slot in which the bandwidth update was done: 0..7
1483 *------------------------------------------------------------------------*/
1485 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1486 uint8_t slot, uint8_t mask)
1488 struct usb_bus *bus = udev->bus;
1489 struct usb_hub *hub;
1490 enum usb_dev_speed speed;
1493 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
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 * usb_isoc_time_expand
1666 * This function will expand the time counter from 7-bit to 16-bit.
1669 * 16-bit isochronous time counter.
1670 *------------------------------------------------------------------------*/
1672 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1676 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1678 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1680 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1682 if (isoc_time_curr < rem) {
1683 /* the time counter wrapped around */
1684 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1686 /* update the remainder */
1688 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1689 bus->isoc_time_last |= isoc_time_curr;
1691 return (bus->isoc_time_last);
1694 /*------------------------------------------------------------------------*
1695 * usbd_fs_isoc_schedule_alloc_slot
1697 * This function will allocate bandwidth for an isochronous FULL speed
1698 * transaction in the FULL speed schedule.
1703 *------------------------------------------------------------------------*/
1704 #if USB_HAVE_TT_SUPPORT
1706 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1708 struct usb_xfer *xfer;
1709 struct usb_xfer *pipe_xfer;
1710 struct usb_bus *bus;
1712 usb_frlength_t data_len;
1720 bus = isoc_xfer->xroot->bus;
1722 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1724 /* skip self, if any */
1726 if (xfer == isoc_xfer)
1729 /* check if this USB transfer is going through the same TT */
1731 if (xfer->xroot->udev->parent_hs_hub !=
1732 isoc_xfer->xroot->udev->parent_hs_hub) {
1735 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1736 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1737 (xfer->xroot->udev->hs_port_no !=
1738 isoc_xfer->xroot->udev->hs_port_no)) {
1741 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1744 /* check if isoc_time is part of this transfer */
1746 delta = xfer->isoc_time_complete - isoc_time;
1747 if (delta > 0 && delta <= xfer->nframes) {
1748 delta = xfer->nframes - delta;
1750 len = xfer->frlengths[delta];
1759 * Check double buffered transfers. Only stream ID
1760 * equal to zero is valid here!
1762 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
1765 /* skip self, if any */
1767 if (pipe_xfer == isoc_xfer)
1770 /* check if isoc_time is part of this transfer */
1772 delta = pipe_xfer->isoc_time_complete - isoc_time;
1773 if (delta > 0 && delta <= pipe_xfer->nframes) {
1774 delta = pipe_xfer->nframes - delta;
1776 len = pipe_xfer->frlengths[delta];
1786 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1787 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1791 /* check for overflow */
1793 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1798 delta = isoc_xfer->isoc_time_complete - isoc_time;
1799 if (delta > 0 && delta <= isoc_xfer->nframes) {
1800 delta = isoc_xfer->nframes - delta;
1802 len = isoc_xfer->frlengths[delta];
1810 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1811 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1815 /* check for overflow */
1817 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1824 /*------------------------------------------------------------------------*
1825 * usb_bus_port_get_device
1827 * This function is NULL safe.
1828 *------------------------------------------------------------------------*/
1830 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1832 if ((bus == NULL) || (up == NULL)) {
1836 if (up->device_index == 0) {
1840 return (bus->devices[up->device_index]);
1843 /*------------------------------------------------------------------------*
1844 * usb_bus_port_set_device
1846 * This function is NULL safe.
1847 *------------------------------------------------------------------------*/
1849 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1850 struct usb_device *udev, uint8_t device_index)
1857 * There is only one case where we don't
1858 * have an USB port, and that is the Root Hub!
1862 up->device_index = device_index;
1864 device_index = up->device_index;
1865 up->device_index = 0;
1869 * Make relationships to our new device
1871 if (device_index != 0) {
1873 mtx_lock(&usb_ref_lock);
1875 bus->devices[device_index] = udev;
1877 mtx_unlock(&usb_ref_lock);
1883 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1886 /*------------------------------------------------------------------------*
1889 * This functions is called when the USB event thread needs to run.
1890 *------------------------------------------------------------------------*/
1892 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1899 DPRINTF("No bus pointer!\n");
1902 if ((bus->devices == NULL) ||
1903 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1904 DPRINTF("No root HUB\n");
1907 if (mtx_owned(&bus->bus_mtx)) {
1916 if (usb_proc_msignal(&bus->explore_proc,
1917 &bus->explore_msg[0], &bus->explore_msg[1])) {
1921 USB_BUS_UNLOCK(bus);
1925 /*------------------------------------------------------------------------*
1926 * usb_needs_explore_all
1928 * This function is called whenever a new driver is loaded and will
1929 * cause that all USB busses are re-explored.
1930 *------------------------------------------------------------------------*/
1932 usb_needs_explore_all(void)
1934 struct usb_bus *bus;
1941 dc = usb_devclass_ptr;
1943 DPRINTFN(0, "no devclass\n");
1947 * Explore all USB busses in parallell.
1949 max = devclass_get_maxunit(dc);
1951 dev = devclass_get_device(dc, max);
1953 bus = device_get_softc(dev);
1955 usb_needs_explore(bus, 1);
1962 /*------------------------------------------------------------------------*
1963 * usb_bus_power_update
1965 * This function will ensure that all USB devices on the given bus are
1966 * properly suspended or resumed according to the device transfer
1968 *------------------------------------------------------------------------*/
1971 usb_bus_power_update(struct usb_bus *bus)
1973 usb_needs_explore(bus, 0 /* no probe */ );
1977 /*------------------------------------------------------------------------*
1978 * usbd_transfer_power_ref
1980 * This function will modify the power save reference counts and
1981 * wakeup the USB device associated with the given USB transfer, if
1983 *------------------------------------------------------------------------*/
1986 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1988 static const usb_power_mask_t power_mask[4] = {
1989 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1990 [UE_BULK] = USB_HW_POWER_BULK,
1991 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1992 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1994 struct usb_device *udev;
1995 uint8_t needs_explore;
1996 uint8_t needs_hw_power;
1999 udev = xfer->xroot->udev;
2001 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2002 /* no power save for root HUB */
2005 USB_BUS_LOCK(udev->bus);
2007 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2009 udev->pwr_save.last_xfer_time = ticks;
2010 udev->pwr_save.type_refs[xfer_type] += val;
2012 if (xfer->flags_int.control_xfr) {
2013 udev->pwr_save.read_refs += val;
2014 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2016 * It is not allowed to suspend during a
2019 udev->pwr_save.write_refs += val;
2021 } else if (USB_GET_DATA_ISREAD(xfer)) {
2022 udev->pwr_save.read_refs += val;
2024 udev->pwr_save.write_refs += val;
2028 if (udev->flags.self_suspended)
2029 needs_explore = usb_peer_should_wakeup(udev);
2033 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2034 DPRINTF("Adding type %u to power state\n", xfer_type);
2035 udev->bus->hw_power_state |= power_mask[xfer_type];
2045 USB_BUS_UNLOCK(udev->bus);
2047 if (needs_explore) {
2048 DPRINTF("update\n");
2049 usb_bus_power_update(udev->bus);
2050 } else if (needs_hw_power) {
2051 DPRINTF("needs power\n");
2052 if (udev->bus->methods->set_hw_power != NULL) {
2053 (udev->bus->methods->set_hw_power) (udev->bus);
2059 /*------------------------------------------------------------------------*
2060 * usb_peer_should_wakeup
2062 * This function returns non-zero if the current device should wake up.
2063 *------------------------------------------------------------------------*/
2065 usb_peer_should_wakeup(struct usb_device *udev)
2067 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2068 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2069 (udev->re_enumerate_wait != 0) ||
2070 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2071 (udev->pwr_save.write_refs != 0) ||
2072 ((udev->pwr_save.read_refs != 0) &&
2073 (udev->flags.usb_mode == USB_MODE_HOST) &&
2074 (usb_peer_can_wakeup(udev) == 0)));
2077 /*------------------------------------------------------------------------*
2080 * This function implements the USB power daemon and is called
2081 * regularly from the USB explore thread.
2082 *------------------------------------------------------------------------*/
2085 usb_bus_powerd(struct usb_bus *bus)
2087 struct usb_device *udev;
2090 usb_ticks_t mintime;
2091 usb_size_t type_refs[5];
2094 limit = usb_power_timeout;
2097 else if (limit > 255)
2102 DPRINTF("bus=%p\n", bus);
2107 * The root HUB device is never suspended
2108 * and we simply skip it.
2110 for (x = USB_ROOT_HUB_ADDR + 1;
2111 x != bus->devices_max; x++) {
2113 udev = bus->devices[x];
2117 temp = ticks - udev->pwr_save.last_xfer_time;
2119 if (usb_peer_should_wakeup(udev)) {
2120 /* check if we are suspended */
2121 if (udev->flags.self_suspended != 0) {
2122 USB_BUS_UNLOCK(bus);
2123 usb_dev_resume_peer(udev);
2126 } else if ((temp >= limit) &&
2127 (udev->flags.usb_mode == USB_MODE_HOST) &&
2128 (udev->flags.self_suspended == 0)) {
2129 /* try to do suspend */
2131 USB_BUS_UNLOCK(bus);
2132 usb_dev_suspend_peer(udev);
2137 /* reset counters */
2139 mintime = (usb_ticks_t)-1;
2146 /* Re-loop all the devices to get the actual state */
2148 for (x = USB_ROOT_HUB_ADDR + 1;
2149 x != bus->devices_max; x++) {
2151 udev = bus->devices[x];
2155 /* we found a non-Root-Hub USB device */
2158 /* "last_xfer_time" can be updated by a resume */
2159 temp = ticks - udev->pwr_save.last_xfer_time;
2162 * Compute minimum time since last transfer for the complete
2168 if (udev->flags.self_suspended == 0) {
2169 type_refs[0] += udev->pwr_save.type_refs[0];
2170 type_refs[1] += udev->pwr_save.type_refs[1];
2171 type_refs[2] += udev->pwr_save.type_refs[2];
2172 type_refs[3] += udev->pwr_save.type_refs[3];
2176 if (mintime >= (usb_ticks_t)(1 * hz)) {
2177 /* recompute power masks */
2178 DPRINTF("Recomputing power masks\n");
2179 bus->hw_power_state = 0;
2180 if (type_refs[UE_CONTROL] != 0)
2181 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2182 if (type_refs[UE_BULK] != 0)
2183 bus->hw_power_state |= USB_HW_POWER_BULK;
2184 if (type_refs[UE_INTERRUPT] != 0)
2185 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2186 if (type_refs[UE_ISOCHRONOUS] != 0)
2187 bus->hw_power_state |= USB_HW_POWER_ISOC;
2188 if (type_refs[4] != 0)
2189 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2191 USB_BUS_UNLOCK(bus);
2193 if (bus->methods->set_hw_power != NULL) {
2194 /* always update hardware power! */
2195 (bus->methods->set_hw_power) (bus);
2201 /*------------------------------------------------------------------------*
2202 * usb_dev_resume_peer
2204 * This function will resume an USB peer and do the required USB
2205 * signalling to get an USB device out of the suspended state.
2206 *------------------------------------------------------------------------*/
2208 usb_dev_resume_peer(struct usb_device *udev)
2210 struct usb_bus *bus;
2217 /* check if already resumed */
2218 if (udev->flags.self_suspended == 0)
2221 /* we need a parent HUB to do resume */
2222 if (udev->parent_hub == NULL)
2225 DPRINTF("udev=%p\n", udev);
2227 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2228 (udev->flags.remote_wakeup == 0)) {
2230 * If the host did not set the remote wakeup feature, we can
2231 * not wake it up either!
2233 DPRINTF("remote wakeup is not set!\n");
2236 /* get bus pointer */
2239 /* resume parent hub first */
2240 usb_dev_resume_peer(udev->parent_hub);
2242 /* reduce chance of instant resume failure by waiting a little bit */
2243 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2245 if (usb_device_20_compatible(udev)) {
2246 /* resume current port (Valid in Host and Device Mode) */
2247 err = usbd_req_clear_port_feature(udev->parent_hub,
2248 NULL, udev->port_no, UHF_PORT_SUSPEND);
2250 DPRINTFN(0, "Resuming port failed\n");
2254 /* resume current port (Valid in Host and Device Mode) */
2255 err = usbd_req_set_port_link_state(udev->parent_hub,
2256 NULL, udev->port_no, UPS_PORT_LS_U0);
2258 DPRINTFN(0, "Resuming port failed\n");
2263 /* resume settle time */
2264 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2266 if (bus->methods->device_resume != NULL) {
2267 /* resume USB device on the USB controller */
2268 (bus->methods->device_resume) (udev);
2271 /* set that this device is now resumed */
2272 udev->flags.self_suspended = 0;
2274 /* make sure that we don't go into suspend right away */
2275 udev->pwr_save.last_xfer_time = ticks;
2277 /* make sure the needed power masks are on */
2278 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2279 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2280 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2281 bus->hw_power_state |= USB_HW_POWER_BULK;
2282 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2283 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2284 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2285 bus->hw_power_state |= USB_HW_POWER_ISOC;
2287 USB_BUS_UNLOCK(bus);
2289 if (bus->methods->set_hw_power != NULL) {
2290 /* always update hardware power! */
2291 (bus->methods->set_hw_power) (bus);
2296 /* notify all sub-devices about resume */
2297 err = usb_suspend_resume(udev, 0);
2299 usbd_sr_unlock(udev);
2301 /* check if peer has wakeup capability */
2302 if (usb_peer_can_wakeup(udev)) {
2303 /* clear remote wakeup */
2304 err = usbd_req_clear_device_feature(udev,
2305 NULL, UF_DEVICE_REMOTE_WAKEUP);
2307 DPRINTFN(0, "Clearing device "
2308 "remote wakeup failed: %s\n",
2314 /*------------------------------------------------------------------------*
2315 * usb_dev_suspend_peer
2317 * This function will suspend an USB peer and do the required USB
2318 * signalling to get an USB device into the suspended state.
2319 *------------------------------------------------------------------------*/
2321 usb_dev_suspend_peer(struct usb_device *udev)
2323 struct usb_device *child;
2333 /* check if already suspended */
2334 if (udev->flags.self_suspended)
2337 /* we need a parent HUB to do suspend */
2338 if (udev->parent_hub == NULL)
2341 DPRINTF("udev=%p\n", udev);
2343 /* check if the current device is a HUB */
2344 if (udev->hub != NULL) {
2345 nports = udev->hub->nports;
2347 /* check if all devices on the HUB are suspended */
2348 for (x = 0; x != nports; x++) {
2349 child = usb_bus_port_get_device(udev->bus,
2350 udev->hub->ports + x);
2355 if (child->flags.self_suspended)
2358 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2363 if (usb_peer_can_wakeup(udev)) {
2365 * This request needs to be done before we set
2366 * "udev->flags.self_suspended":
2369 /* allow device to do remote wakeup */
2370 err = usbd_req_set_device_feature(udev,
2371 NULL, UF_DEVICE_REMOTE_WAKEUP);
2373 DPRINTFN(0, "Setting device "
2374 "remote wakeup failed\n");
2378 USB_BUS_LOCK(udev->bus);
2380 * Checking for suspend condition and setting suspended bit
2383 err = usb_peer_should_wakeup(udev);
2386 * Set that this device is suspended. This variable
2387 * must be set before calling USB controller suspend
2390 udev->flags.self_suspended = 1;
2392 USB_BUS_UNLOCK(udev->bus);
2395 if (usb_peer_can_wakeup(udev)) {
2396 /* allow device to do remote wakeup */
2397 err = usbd_req_clear_device_feature(udev,
2398 NULL, UF_DEVICE_REMOTE_WAKEUP);
2400 DPRINTFN(0, "Setting device "
2401 "remote wakeup failed\n");
2405 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2406 /* resume parent HUB first */
2407 usb_dev_resume_peer(udev->parent_hub);
2409 /* reduce chance of instant resume failure by waiting a little bit */
2410 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2412 /* resume current port (Valid in Host and Device Mode) */
2413 err = usbd_req_clear_port_feature(udev->parent_hub,
2414 NULL, udev->port_no, UHF_PORT_SUSPEND);
2416 /* resume settle time */
2417 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2419 DPRINTF("Suspend was cancelled!\n");
2425 /* notify all sub-devices about suspend */
2426 err = usb_suspend_resume(udev, 1);
2428 usbd_sr_unlock(udev);
2430 if (udev->bus->methods->device_suspend != NULL) {
2433 /* suspend device on the USB controller */
2434 (udev->bus->methods->device_suspend) (udev);
2437 temp = usbd_get_dma_delay(udev);
2439 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2443 if (usb_device_20_compatible(udev)) {
2444 /* suspend current port */
2445 err = usbd_req_set_port_feature(udev->parent_hub,
2446 NULL, udev->port_no, UHF_PORT_SUSPEND);
2448 DPRINTFN(0, "Suspending port failed\n");
2452 /* suspend current port */
2453 err = usbd_req_set_port_link_state(udev->parent_hub,
2454 NULL, udev->port_no, UPS_PORT_LS_U3);
2456 DPRINTFN(0, "Suspending port failed\n");
2461 udev = udev->parent_hub;
2465 /*------------------------------------------------------------------------*
2466 * usbd_set_power_mode
2468 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2470 *------------------------------------------------------------------------*/
2472 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2474 /* filter input argument */
2475 if ((power_mode != USB_POWER_MODE_ON) &&
2476 (power_mode != USB_POWER_MODE_OFF))
2477 power_mode = USB_POWER_MODE_SAVE;
2479 power_mode = usbd_filter_power_mode(udev, power_mode);
2481 udev->power_mode = power_mode; /* update copy of power mode */
2484 usb_bus_power_update(udev->bus);
2488 /*------------------------------------------------------------------------*
2489 * usbd_filter_power_mode
2491 * This function filters the power mode based on hardware requirements.
2492 *------------------------------------------------------------------------*/
2494 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2496 struct usb_bus_methods *mtod;
2499 mtod = udev->bus->methods;
2502 if (mtod->get_power_mode != NULL)
2503 (mtod->get_power_mode) (udev, &temp);
2505 /* check if we should not filter */
2507 return (power_mode);
2509 /* use fixed power mode given by hardware driver */
2513 /*------------------------------------------------------------------------*
2514 * usbd_start_re_enumerate
2516 * This function starts re-enumeration of the given USB device. This
2517 * function does not need to be called BUS-locked. This function does
2518 * not wait until the re-enumeration is completed.
2519 *------------------------------------------------------------------------*/
2521 usbd_start_re_enumerate(struct usb_device *udev)
2523 if (udev->re_enumerate_wait == 0) {
2524 udev->re_enumerate_wait = 1;
2525 usb_needs_explore(udev->bus, 0);