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 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
108 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
109 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
110 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
111 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
112 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
114 /* prototypes for type checking: */
116 static device_probe_t uhub_probe;
117 static device_attach_t uhub_attach;
118 static device_detach_t uhub_detach;
119 static device_suspend_t uhub_suspend;
120 static device_resume_t uhub_resume;
122 static bus_driver_added_t uhub_driver_added;
123 static bus_child_location_str_t uhub_child_location_string;
124 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
126 static usb_callback_t uhub_intr_callback;
128 static void usb_dev_resume_peer(struct usb_device *udev);
129 static void usb_dev_suspend_peer(struct usb_device *udev);
130 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
132 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
135 .type = UE_INTERRUPT,
136 .endpoint = UE_ADDR_ANY,
137 .direction = UE_DIR_ANY,
139 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
140 .bufsize = 0, /* use wMaxPacketSize */
141 .callback = &uhub_intr_callback,
142 .interval = UHUB_INTR_INTERVAL,
147 * driver instance for "hub" connected to "usb"
148 * and "hub" connected to "hub"
150 static devclass_t uhub_devclass;
152 static device_method_t uhub_methods[] = {
153 DEVMETHOD(device_probe, uhub_probe),
154 DEVMETHOD(device_attach, uhub_attach),
155 DEVMETHOD(device_detach, uhub_detach),
157 DEVMETHOD(device_suspend, uhub_suspend),
158 DEVMETHOD(device_resume, uhub_resume),
160 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
161 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
162 DEVMETHOD(bus_driver_added, uhub_driver_added),
166 static driver_t uhub_driver = {
168 .methods = uhub_methods,
169 .size = sizeof(struct uhub_softc)
172 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
173 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
174 MODULE_VERSION(uhub, 1);
177 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
179 struct uhub_softc *sc = usbd_xfer_softc(xfer);
181 switch (USB_GET_STATE(xfer)) {
182 case USB_ST_TRANSFERRED:
185 * This is an indication that some port
186 * has changed status. Notify the bus
187 * event handler thread that we need
188 * to be explored again:
190 usb_needs_explore(sc->sc_udev->bus, 0);
193 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
194 usbd_transfer_submit(xfer);
198 if (xfer->error != USB_ERR_CANCELLED) {
200 * Do a clear-stall. The "stall_pipe" flag
201 * will get cleared before next callback by
204 usbd_xfer_set_stall(xfer);
205 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
206 usbd_transfer_submit(xfer);
212 /*------------------------------------------------------------------------*
213 * uhub_explore_sub - subroutine
217 * Else: A control transaction failed
218 *------------------------------------------------------------------------*/
220 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
223 struct usb_device *child;
227 bus = sc->sc_udev->bus;
230 /* get driver added refcount from USB bus */
231 refcount = bus->driver_added_refcount;
233 /* get device assosiated with the given port */
234 child = usb_bus_port_get_device(bus, up);
240 /* check if device should be re-enumerated */
242 if (child->flags.usb_mode == USB_MODE_HOST) {
243 usbd_enum_lock(child);
244 if (child->re_enumerate_wait) {
245 err = usbd_set_config_index(child,
248 DPRINTF("Unconfigure failed: "
252 err = usbd_req_re_enumerate(child, NULL);
254 err = usbd_set_config_index(child, 0);
256 err = usb_probe_and_attach(child,
257 USB_IFACE_INDEX_ANY);
259 child->re_enumerate_wait = 0;
262 usbd_enum_unlock(child);
265 /* check if probe and attach should be done */
267 if (child->driver_added_refcount != refcount) {
268 child->driver_added_refcount = refcount;
269 err = usb_probe_and_attach(child,
270 USB_IFACE_INDEX_ANY);
275 /* start control transfer, if device mode */
277 if (child->flags.usb_mode == USB_MODE_DEVICE)
278 usbd_ctrl_transfer_setup(child);
280 /* if a HUB becomes present, do a recursive HUB explore */
283 err = (child->hub->explore) (child);
289 /*------------------------------------------------------------------------*
290 * uhub_read_port_status - factored out code
291 *------------------------------------------------------------------------*/
293 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
295 struct usb_port_status ps;
298 err = usbd_req_get_port_status(
299 sc->sc_udev, NULL, &ps, portno);
301 /* update status regardless of error */
303 sc->sc_st.port_status = UGETW(ps.wPortStatus);
304 sc->sc_st.port_change = UGETW(ps.wPortChange);
306 /* debugging print */
308 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
309 "wPortChange=0x%04x, err=%s\n",
310 portno, sc->sc_st.port_status,
311 sc->sc_st.port_change, usbd_errstr(err));
315 /*------------------------------------------------------------------------*
320 * Else: A control transaction failed
321 *------------------------------------------------------------------------*/
323 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
325 struct usb_device *child;
326 struct usb_device *udev;
327 enum usb_dev_speed speed;
328 enum usb_hc_mode mode;
333 DPRINTF("reattaching port %d\n", portno);
338 child = usb_bus_port_get_device(udev->bus,
339 udev->hub->ports + portno - 1);
343 /* first clear the port connection change bit */
345 err = usbd_req_clear_port_feature(udev, NULL,
346 portno, UHF_C_PORT_CONNECTION);
351 /* check if there is a child */
355 * Free USB device and all subdevices, if any.
357 usb_free_device(child, 0);
360 /* get fresh status */
362 err = uhub_read_port_status(sc, portno);
366 /* check if nothing is connected to the port */
368 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
371 /* check if there is no power on the port and print a warning */
373 switch (udev->speed) {
377 power_mask = UPS_PORT_POWER;
379 case USB_SPEED_SUPER:
380 if (udev->parent_hub == NULL)
381 power_mask = UPS_PORT_POWER;
383 power_mask = UPS_PORT_POWER_SS;
389 if (!(sc->sc_st.port_status & power_mask)) {
390 DPRINTF("WARNING: strange, connected port %d "
391 "has no power\n", portno);
394 /* check if the device is in Host Mode */
396 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
398 DPRINTF("Port %d is in Host Mode\n", portno);
400 if (sc->sc_st.port_status & UPS_SUSPEND) {
402 * NOTE: Should not get here in SuperSpeed
403 * mode, because the HUB should report this
406 DPRINTF("Port %d was still "
407 "suspended, clearing.\n", portno);
408 err = usbd_req_clear_port_feature(udev,
409 NULL, portno, UHF_PORT_SUSPEND);
414 /* wait for maximum device power up time */
417 USB_MS_TO_TICKS(usb_port_powerup_delay));
419 /* reset port, which implies enabling it */
421 err = usbd_req_reset_port(udev, NULL, portno);
424 DPRINTFN(0, "port %d reset "
425 "failed, error=%s\n",
426 portno, usbd_errstr(err));
429 /* get port status again, it might have changed during reset */
431 err = uhub_read_port_status(sc, portno);
435 /* check if something changed during port reset */
437 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
438 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
440 DPRINTFN(0, "giving up port reset "
441 "- device vanished\n");
448 DPRINTF("Port %d is in Device Mode\n", portno);
452 * Figure out the device speed
454 switch (udev->speed) {
456 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
457 speed = USB_SPEED_HIGH;
458 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
459 speed = USB_SPEED_LOW;
461 speed = USB_SPEED_FULL;
464 if (sc->sc_st.port_status & UPS_LOW_SPEED)
465 speed = USB_SPEED_LOW;
467 speed = USB_SPEED_FULL;
470 speed = USB_SPEED_LOW;
472 case USB_SPEED_SUPER:
473 if (udev->parent_hub == NULL) {
474 /* Root HUB - special case */
475 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
477 speed = USB_SPEED_FULL;
480 speed = USB_SPEED_LOW;
483 speed = USB_SPEED_HIGH;
486 speed = USB_SPEED_SUPER;
490 speed = USB_SPEED_SUPER;
494 /* same speed like parent */
498 if (speed == USB_SPEED_SUPER) {
499 err = usbd_req_set_hub_u1_timeout(udev, NULL,
500 portno, 128 - (2 * udev->depth));
502 DPRINTFN(0, "port %d U1 timeout "
503 "failed, error=%s\n",
504 portno, usbd_errstr(err));
506 err = usbd_req_set_hub_u2_timeout(udev, NULL,
507 portno, 128 - (2 * udev->depth));
509 DPRINTFN(0, "port %d U2 timeout "
510 "failed, error=%s\n",
511 portno, usbd_errstr(err));
516 * Figure out the device mode
518 * NOTE: This part is currently FreeBSD specific.
520 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
521 mode = USB_MODE_DEVICE;
523 mode = USB_MODE_HOST;
525 /* need to create a new child */
526 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
527 udev->depth + 1, portno - 1, portno, speed, mode);
529 DPRINTFN(0, "could not allocate new device\n");
532 return (0); /* success */
537 * Free USB device and all subdevices, if any.
539 usb_free_device(child, 0);
543 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
544 err = usbd_req_clear_port_feature(
546 portno, UHF_PORT_ENABLE);
550 DPRINTFN(0, "device problem (%s), "
551 "disabling port %d\n", usbd_errstr(err), portno);
556 /*------------------------------------------------------------------------*
557 * usb_device_20_compatible
560 * 0: HUB does not support suspend and resume
561 * Else: HUB supports suspend and resume
562 *------------------------------------------------------------------------*/
564 usb_device_20_compatible(struct usb_device *udev)
568 switch (udev->speed) {
578 /*------------------------------------------------------------------------*
579 * uhub_suspend_resume_port
583 * Else: A control transaction failed
584 *------------------------------------------------------------------------*/
586 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
588 struct usb_device *child;
589 struct usb_device *udev;
593 DPRINTF("port %d\n", portno);
596 child = usb_bus_port_get_device(udev->bus,
597 udev->hub->ports + portno - 1);
599 /* first clear the port suspend change bit */
601 if (usb_device_20_compatible(udev)) {
602 err = usbd_req_clear_port_feature(udev, NULL,
603 portno, UHF_C_PORT_SUSPEND);
605 err = usbd_req_clear_port_feature(udev, NULL,
606 portno, UHF_C_PORT_LINK_STATE);
610 DPRINTF("clearing suspend failed.\n");
613 /* get fresh status */
615 err = uhub_read_port_status(sc, portno);
617 DPRINTF("reading port status failed.\n");
620 /* convert current state */
622 if (usb_device_20_compatible(udev)) {
623 if (sc->sc_st.port_status & UPS_SUSPEND) {
629 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
633 case UPS_PORT_LS_SS_INA:
634 usbd_req_warm_reset_port(udev, NULL, portno);
643 DPRINTF("suspended=%u\n", is_suspend);
645 /* do the suspend or resume */
649 * This code handle two cases: 1) Host Mode - we can only
650 * receive resume here 2) Device Mode - we can receive
651 * suspend and resume here
654 usb_dev_resume_peer(child);
655 else if (child->flags.usb_mode == USB_MODE_DEVICE)
656 usb_dev_suspend_peer(child);
662 /*------------------------------------------------------------------------*
663 * uhub_root_interrupt
665 * This function is called when a Root HUB interrupt has
666 * happened. "ptr" and "len" makes up the Root HUB interrupt
667 * packet. This function is called having the "bus_mtx" locked.
668 *------------------------------------------------------------------------*/
670 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
672 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
674 usb_needs_explore(bus, 0);
678 uhub_is_too_deep(struct usb_device *udev)
680 switch (udev->speed) {
684 if (udev->depth > USB_HUB_MAX_DEPTH)
687 case USB_SPEED_SUPER:
688 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
697 /*------------------------------------------------------------------------*
703 *------------------------------------------------------------------------*/
705 uhub_explore(struct usb_device *udev)
708 struct uhub_softc *sc;
717 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
719 /* ignore devices that are too deep */
720 if (uhub_is_too_deep(udev))
721 return (USB_ERR_TOO_DEEP);
723 /* check if device is suspended */
724 if (udev->flags.self_suspended) {
725 /* need to wait until the child signals resume */
726 DPRINTF("Device is suspended!\n");
731 * Make sure we don't race against user-space applications
734 usbd_enum_lock(udev);
736 for (x = 0; x != hub->nports; x++) {
740 err = uhub_read_port_status(sc, portno);
742 /* most likely the HUB is gone */
745 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
746 DPRINTF("Overcurrent on port %u.\n", portno);
747 err = usbd_req_clear_port_feature(
748 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
750 /* most likely the HUB is gone */
754 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
756 * Fake a connect status change so that the
757 * status gets checked initially!
759 sc->sc_st.port_change |=
760 UPS_C_CONNECT_STATUS;
762 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
763 err = usbd_req_clear_port_feature(
764 udev, NULL, portno, UHF_C_PORT_ENABLE);
766 /* most likely the HUB is gone */
769 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
771 * Ignore the port error if the device
774 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
775 DPRINTFN(0, "illegal enable change, "
776 "port %d\n", portno);
779 if (up->restartcnt == USB_RESTART_MAX) {
780 /* XXX could try another speed ? */
781 DPRINTFN(0, "port error, giving up "
782 "port %d\n", portno);
784 sc->sc_st.port_change |=
785 UPS_C_CONNECT_STATUS;
790 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
791 err = uhub_reattach_port(sc, portno);
793 /* most likely the HUB is gone */
797 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
798 UPS_C_PORT_LINK_STATE)) {
799 err = uhub_suspend_resume_port(sc, portno);
801 /* most likely the HUB is gone */
805 err = uhub_explore_sub(sc, up);
807 /* no device(s) present */
810 /* explore succeeded - reset restart counter */
814 usbd_enum_unlock(udev);
816 /* initial status checked */
817 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
820 return (USB_ERR_NORMAL_COMPLETION);
824 uhub_probe(device_t dev)
826 struct usb_attach_arg *uaa = device_get_ivars(dev);
828 if (uaa->usb_mode != USB_MODE_HOST)
832 * The subclass for USB HUBs is currently ignored because it
833 * is 0 for some and 1 for others.
835 if (uaa->info.bConfigIndex == 0 &&
836 uaa->info.bDeviceClass == UDCLASS_HUB)
842 /* NOTE: The information returned by this function can be wrong. */
844 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
846 struct usb_hub_descriptor hubdesc20;
847 struct usb_hub_ss_descriptor hubdesc30;
852 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
853 return (USB_ERR_INVAL);
858 switch (udev->speed) {
862 /* assuming that there is one port */
863 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
865 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
866 "error=%s\n", usbd_errstr(err));
869 nports = hubdesc20.bNbrPorts;
873 if (udev->speed == USB_SPEED_HIGH)
874 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
877 case USB_SPEED_SUPER:
878 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
880 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
881 "error=%s\n", usbd_errstr(err));
884 nports = hubdesc30.bNbrPorts;
904 uhub_attach(device_t dev)
906 struct uhub_softc *sc = device_get_softc(dev);
907 struct usb_attach_arg *uaa = device_get_ivars(dev);
908 struct usb_device *udev = uaa->device;
909 struct usb_device *parent_hub = udev->parent_hub;
911 struct usb_hub_descriptor hubdesc20;
912 struct usb_hub_ss_descriptor hubdesc30;
924 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
926 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
927 device_get_nameunit(dev));
929 device_set_usb_desc(dev);
931 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
932 "parent->selfpowered=%d\n",
934 udev->flags.self_powered,
937 parent_hub->flags.self_powered : 0);
939 if (uhub_is_too_deep(udev)) {
940 DPRINTFN(0, "HUB at depth %d, "
941 "exceeds maximum. HUB ignored\n", (int)udev->depth);
945 if (!udev->flags.self_powered && parent_hub &&
946 !parent_hub->flags.self_powered) {
947 DPRINTFN(0, "Bus powered HUB connected to "
948 "bus powered HUB. HUB ignored\n");
952 if (UHUB_IS_MULTI_TT(sc)) {
953 err = usbd_set_alt_interface_index(udev, 0, 1);
955 device_printf(dev, "MTT could not be enabled\n");
958 device_printf(dev, "MTT enabled\n");
961 /* get HUB descriptor */
963 DPRINTFN(2, "Getting HUB descriptor\n");
965 switch (udev->speed) {
969 /* assuming that there is one port */
970 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
972 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
973 "error=%s\n", usbd_errstr(err));
976 /* get number of ports */
977 nports = hubdesc20.bNbrPorts;
979 /* get power delay */
980 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
981 usb_extra_power_up_time);
983 /* get complete HUB descriptor */
985 /* check number of ports */
987 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
988 "error=%s\n", usbd_errstr(err));
991 /* get complete HUB descriptor */
992 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
995 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
996 "error=%s\n", usbd_errstr(err));
999 if (hubdesc20.bNbrPorts != nports) {
1000 DPRINTFN(0, "Number of ports changed\n");
1005 case USB_SPEED_SUPER:
1006 if (udev->parent_hub != NULL) {
1007 err = usbd_req_set_hub_depth(udev, NULL,
1010 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1011 "error=%s\n", usbd_errstr(err));
1015 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1017 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1018 "error=%s\n", usbd_errstr(err));
1021 /* get number of ports */
1022 nports = hubdesc30.bNbrPorts;
1024 /* get power delay */
1025 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1026 usb_extra_power_up_time);
1028 /* get complete HUB descriptor */
1030 /* check number of ports */
1031 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1032 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1033 "error=%s\n", usbd_errstr(err));
1036 /* get complete HUB descriptor */
1037 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1040 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1041 "error=%s\n", usbd_errstr(err));
1044 if (hubdesc30.bNbrPorts != nports) {
1045 DPRINTFN(0, "Number of ports changed\n");
1051 DPRINTF("Assuming HUB has only one port\n");
1052 /* default number of ports */
1054 /* default power delay */
1055 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1059 DPRINTFN(0, "portless HUB\n");
1062 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1063 M_USBDEV, M_WAITOK | M_ZERO);
1070 /* initialize HUB structure */
1072 hub->explore = &uhub_explore;
1073 hub->nports = nports;
1074 hub->hubudev = udev;
1076 /* if self powered hub, give ports maximum current */
1077 if (udev->flags.self_powered) {
1078 hub->portpower = USB_MAX_POWER;
1080 hub->portpower = USB_MIN_POWER;
1083 /* set up interrupt pipe */
1085 if (udev->parent_hub == NULL) {
1086 /* root HUB is special */
1090 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1091 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1094 DPRINTFN(0, "cannot setup interrupt transfer, "
1095 "errstr=%s\n", usbd_errstr(err));
1098 /* wait with power off for a while */
1099 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1102 * To have the best chance of success we do things in the exact same
1103 * order as Windoze98. This should not be necessary, but some
1104 * devices do not follow the USB specs to the letter.
1106 * These are the events on the bus when a hub is attached:
1107 * Get device and config descriptors (see attach code)
1108 * Get hub descriptor (see above)
1111 * wait for power to become stable
1112 * (all below happens in explore code)
1114 * clear C_PORT_CONNECTION
1117 * if device connected
1121 * clear C_PORT_RESET
1123 * proceed with device attachment
1126 /* XXX should check for none, individual, or ganged power? */
1130 for (x = 0; x != nports; x++) {
1131 /* set up data structures */
1132 struct usb_port *up = hub->ports + x;
1134 up->device_index = 0;
1138 /* check if port is removable */
1139 switch (udev->speed) {
1141 case USB_SPEED_FULL:
1142 case USB_SPEED_HIGH:
1143 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1146 case USB_SPEED_SUPER:
1147 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1151 DPRINTF("Assuming removable port\n");
1156 /* turn the power on */
1157 err = usbd_req_set_port_feature(udev, NULL,
1158 portno, UHF_PORT_POWER);
1161 DPRINTFN(0, "port %d power on failed, %s\n",
1162 portno, usbd_errstr(err));
1164 DPRINTF("turn on port %d power\n",
1167 /* wait for stable power */
1168 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1171 device_printf(dev, "%d port%s with %d "
1172 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1173 removable, udev->flags.self_powered ? "self" : "bus");
1175 /* Start the interrupt endpoint, if any */
1177 if (sc->sc_xfer[0] != NULL) {
1178 mtx_lock(&sc->sc_mtx);
1179 usbd_transfer_start(sc->sc_xfer[0]);
1180 mtx_unlock(&sc->sc_mtx);
1183 /* Enable automatic power save on all USB HUBs */
1185 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1190 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1193 free(udev->hub, M_USBDEV);
1197 mtx_destroy(&sc->sc_mtx);
1203 * Called from process context when the hub is gone.
1204 * Detach all devices on active ports.
1207 uhub_detach(device_t dev)
1209 struct uhub_softc *sc = device_get_softc(dev);
1210 struct usb_hub *hub = sc->sc_udev->hub;
1211 struct usb_device *child;
1214 if (hub == NULL) /* must be partially working */
1217 /* Make sure interrupt transfer is gone. */
1218 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1220 /* Detach all ports */
1221 for (x = 0; x != hub->nports; x++) {
1223 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1225 if (child == NULL) {
1230 * Free USB device and all subdevices, if any.
1232 usb_free_device(child, 0);
1235 free(hub, M_USBDEV);
1236 sc->sc_udev->hub = NULL;
1238 mtx_destroy(&sc->sc_mtx);
1244 uhub_suspend(device_t dev)
1247 /* Sub-devices are not suspended here! */
1252 uhub_resume(device_t dev)
1255 /* Sub-devices are not resumed here! */
1260 uhub_driver_added(device_t dev, driver_t *driver)
1262 usb_needs_explore_all();
1266 struct usb_device *udev;
1268 uint8_t iface_index;
1272 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1273 struct hub_result *res)
1275 struct usb_interface *iface;
1276 struct usb_device *udev;
1281 nports = hub->nports;
1282 for (x = 0; x != nports; x++) {
1283 udev = usb_bus_port_get_device(hub->hubudev->bus,
1288 for (i = 0; i != USB_IFACE_MAX; i++) {
1289 iface = usbd_get_iface(udev, i);
1291 (iface->subdev == child)) {
1292 res->iface_index = i;
1294 res->portno = x + 1;
1299 res->iface_index = 0;
1305 uhub_child_location_string(device_t parent, device_t child,
1306 char *buf, size_t buflen)
1308 struct uhub_softc *sc;
1309 struct usb_hub *hub;
1310 struct hub_result res;
1312 if (!device_is_attached(parent)) {
1318 sc = device_get_softc(parent);
1319 hub = sc->sc_udev->hub;
1322 uhub_find_iface_index(hub, child, &res);
1324 DPRINTF("device not on hub\n");
1330 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1331 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1332 res.portno, device_get_unit(res.udev->bus->bdev),
1333 res.udev->device_index, res.iface_index);
1341 uhub_child_pnpinfo_string(device_t parent, device_t child,
1342 char *buf, size_t buflen)
1344 struct uhub_softc *sc;
1345 struct usb_hub *hub;
1346 struct usb_interface *iface;
1347 struct hub_result res;
1349 if (!device_is_attached(parent)) {
1355 sc = device_get_softc(parent);
1356 hub = sc->sc_udev->hub;
1359 uhub_find_iface_index(hub, child, &res);
1361 DPRINTF("device not on hub\n");
1367 iface = usbd_get_iface(res.udev, res.iface_index);
1368 if (iface && iface->idesc) {
1369 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1370 "devclass=0x%02x devsubclass=0x%02x "
1374 "intclass=0x%02x intsubclass=0x%02x "
1375 "intprotocol=0x%02x " "%s%s",
1376 UGETW(res.udev->ddesc.idVendor),
1377 UGETW(res.udev->ddesc.idProduct),
1378 res.udev->ddesc.bDeviceClass,
1379 res.udev->ddesc.bDeviceSubClass,
1380 usb_get_serial(res.udev),
1381 UGETW(res.udev->ddesc.bcdDevice),
1382 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1383 iface->idesc->bInterfaceClass,
1384 iface->idesc->bInterfaceSubClass,
1385 iface->idesc->bInterfaceProtocol,
1386 iface->pnpinfo ? " " : "",
1387 iface->pnpinfo ? iface->pnpinfo : "");
1401 * The USB Transaction Translator:
1402 * ===============================
1404 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1405 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1406 * USB transfers. To utilize bandwidth dynamically the "scatter and
1407 * gather" principle must be applied. This means that bandwidth must
1408 * be divided into equal parts of bandwidth. With regard to USB all
1409 * data is transferred in smaller packets with length
1410 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1413 * The bandwidth scheduler which I have implemented will simply pack
1414 * the USB transfers back to back until there is no more space in the
1415 * schedule. Out of the 8 microframes which the USB 2.0 standard
1416 * provides, only 6 are available for non-HIGH-speed devices. I have
1417 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1418 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1419 * this division, it is very difficult to allocate and free bandwidth
1422 * NOTE about the Transaction Translator in USB HUBs:
1424 * USB HUBs have a very simple Transaction Translator, that will
1425 * simply pipeline all the SPLIT transactions. That means that the
1426 * transactions will be executed in the order they are queued!
1430 /*------------------------------------------------------------------------*
1431 * usb_intr_find_best_slot
1434 * The best Transaction Translation slot for an interrupt endpoint.
1435 *------------------------------------------------------------------------*/
1437 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1438 uint8_t end, uint8_t mask)
1440 usb_size_t min = (usb_size_t)-1;
1448 /* find the last slot with lesser used bandwidth */
1450 for (x = start; x < end; x++) {
1454 /* compute sum of bandwidth */
1455 for (z = x; z < end; z++) {
1456 if (mask & (1U << (z - x)))
1460 /* check if the current multi-slot is more optimal */
1466 /* check if the mask is about to be shifted out */
1467 if (mask & (1U << (end - 1 - x)))
1473 /*------------------------------------------------------------------------*
1474 * usb_hs_bandwidth_adjust
1476 * This function will update the bandwith usage for the microframe
1477 * having index "slot" by "len" bytes. "len" can be negative. If the
1478 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1479 * the "slot" argument will be replaced by the slot having least used
1480 * bandwidth. The "mask" argument is used for multi-slot allocations.
1483 * The slot in which the bandwidth update was done: 0..7
1484 *------------------------------------------------------------------------*/
1486 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1487 uint8_t slot, uint8_t mask)
1489 struct usb_bus *bus = udev->bus;
1490 struct usb_hub *hub;
1491 enum usb_dev_speed speed;
1494 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1496 speed = usbd_get_speed(udev);
1500 case USB_SPEED_FULL:
1501 if (speed == USB_SPEED_LOW) {
1505 * The Host Controller Driver should have
1506 * performed checks so that the lookup
1507 * below does not result in a NULL pointer
1511 hub = udev->parent_hs_hub->hub;
1512 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1513 slot = usb_intr_find_best_slot(hub->uframe_usage,
1514 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1516 for (x = slot; x < 8; x++) {
1517 if (mask & (1U << (x - slot))) {
1518 hub->uframe_usage[x] += len;
1519 bus->uframe_usage[x] += len;
1524 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1525 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1526 USB_HS_MICRO_FRAMES_MAX, mask);
1528 for (x = slot; x < 8; x++) {
1529 if (mask & (1U << (x - slot))) {
1530 bus->uframe_usage[x] += len;
1538 /*------------------------------------------------------------------------*
1539 * usb_hs_bandwidth_alloc
1541 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1542 *------------------------------------------------------------------------*/
1544 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1546 struct usb_device *udev;
1551 udev = xfer->xroot->udev;
1553 if (udev->flags.usb_mode != USB_MODE_HOST)
1554 return; /* not supported */
1556 xfer->endpoint->refcount_bw++;
1557 if (xfer->endpoint->refcount_bw != 1)
1558 return; /* already allocated */
1560 speed = usbd_get_speed(udev);
1562 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1564 /* allocate a microframe slot */
1567 slot = usb_hs_bandwidth_adjust(udev,
1568 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1570 xfer->endpoint->usb_uframe = slot;
1571 xfer->endpoint->usb_smask = mask << slot;
1573 if ((speed != USB_SPEED_FULL) &&
1574 (speed != USB_SPEED_LOW)) {
1575 xfer->endpoint->usb_cmask = 0x00 ;
1577 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1581 case UE_ISOCHRONOUS:
1582 switch (usbd_xfer_get_fps_shift(xfer)) {
1597 /* allocate a microframe multi-slot */
1599 slot = usb_hs_bandwidth_adjust(udev,
1600 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1602 xfer->endpoint->usb_uframe = slot;
1603 xfer->endpoint->usb_cmask = 0;
1604 xfer->endpoint->usb_smask = mask << slot;
1608 xfer->endpoint->usb_uframe = 0;
1609 xfer->endpoint->usb_cmask = 0;
1610 xfer->endpoint->usb_smask = 0;
1614 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1615 xfer->endpoint->usb_uframe,
1616 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1619 /*------------------------------------------------------------------------*
1620 * usb_hs_bandwidth_free
1622 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1623 *------------------------------------------------------------------------*/
1625 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1627 struct usb_device *udev;
1631 udev = xfer->xroot->udev;
1633 if (udev->flags.usb_mode != USB_MODE_HOST)
1634 return; /* not supported */
1636 xfer->endpoint->refcount_bw--;
1637 if (xfer->endpoint->refcount_bw != 0)
1638 return; /* still allocated */
1640 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1642 case UE_ISOCHRONOUS:
1644 slot = xfer->endpoint->usb_uframe;
1645 mask = xfer->endpoint->usb_smask;
1647 /* free microframe slot(s): */
1648 usb_hs_bandwidth_adjust(udev,
1649 -xfer->max_frame_size, slot, mask >> slot);
1651 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1652 slot, mask >> slot);
1654 xfer->endpoint->usb_uframe = 0;
1655 xfer->endpoint->usb_cmask = 0;
1656 xfer->endpoint->usb_smask = 0;
1664 /*------------------------------------------------------------------------*
1665 * usb_isoc_time_expand
1667 * This function will expand the time counter from 7-bit to 16-bit.
1670 * 16-bit isochronous time counter.
1671 *------------------------------------------------------------------------*/
1673 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1677 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1679 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1681 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1683 if (isoc_time_curr < rem) {
1684 /* the time counter wrapped around */
1685 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1687 /* update the remainder */
1689 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1690 bus->isoc_time_last |= isoc_time_curr;
1692 return (bus->isoc_time_last);
1695 /*------------------------------------------------------------------------*
1696 * usbd_fs_isoc_schedule_alloc_slot
1698 * This function will allocate bandwidth for an isochronous FULL speed
1699 * transaction in the FULL speed schedule.
1704 *------------------------------------------------------------------------*/
1705 #if USB_HAVE_TT_SUPPORT
1707 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1709 struct usb_xfer *xfer;
1710 struct usb_xfer *pipe_xfer;
1711 struct usb_bus *bus;
1713 usb_frlength_t data_len;
1721 bus = isoc_xfer->xroot->bus;
1723 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1725 /* skip self, if any */
1727 if (xfer == isoc_xfer)
1730 /* check if this USB transfer is going through the same TT */
1732 if (xfer->xroot->udev->parent_hs_hub !=
1733 isoc_xfer->xroot->udev->parent_hs_hub) {
1736 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1737 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1738 (xfer->xroot->udev->hs_port_no !=
1739 isoc_xfer->xroot->udev->hs_port_no)) {
1742 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1745 /* check if isoc_time is part of this transfer */
1747 delta = xfer->isoc_time_complete - isoc_time;
1748 if (delta > 0 && delta <= xfer->nframes) {
1749 delta = xfer->nframes - delta;
1751 len = xfer->frlengths[delta];
1759 /* check double buffered transfers */
1761 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
1764 /* skip self, if any */
1766 if (pipe_xfer == isoc_xfer)
1769 /* check if isoc_time is part of this transfer */
1771 delta = pipe_xfer->isoc_time_complete - isoc_time;
1772 if (delta > 0 && delta <= pipe_xfer->nframes) {
1773 delta = pipe_xfer->nframes - delta;
1775 len = pipe_xfer->frlengths[delta];
1785 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1786 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1790 /* check for overflow */
1792 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1797 delta = isoc_xfer->isoc_time_complete - isoc_time;
1798 if (delta > 0 && delta <= isoc_xfer->nframes) {
1799 delta = isoc_xfer->nframes - delta;
1801 len = isoc_xfer->frlengths[delta];
1809 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1810 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1814 /* check for overflow */
1816 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1823 /*------------------------------------------------------------------------*
1824 * usb_bus_port_get_device
1826 * This function is NULL safe.
1827 *------------------------------------------------------------------------*/
1829 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1831 if ((bus == NULL) || (up == NULL)) {
1835 if (up->device_index == 0) {
1839 return (bus->devices[up->device_index]);
1842 /*------------------------------------------------------------------------*
1843 * usb_bus_port_set_device
1845 * This function is NULL safe.
1846 *------------------------------------------------------------------------*/
1848 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1849 struct usb_device *udev, uint8_t device_index)
1856 * There is only one case where we don't
1857 * have an USB port, and that is the Root Hub!
1861 up->device_index = device_index;
1863 device_index = up->device_index;
1864 up->device_index = 0;
1868 * Make relationships to our new device
1870 if (device_index != 0) {
1872 mtx_lock(&usb_ref_lock);
1874 bus->devices[device_index] = udev;
1876 mtx_unlock(&usb_ref_lock);
1882 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1885 /*------------------------------------------------------------------------*
1888 * This functions is called when the USB event thread needs to run.
1889 *------------------------------------------------------------------------*/
1891 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1898 DPRINTF("No bus pointer!\n");
1901 if ((bus->devices == NULL) ||
1902 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1903 DPRINTF("No root HUB\n");
1906 if (mtx_owned(&bus->bus_mtx)) {
1915 if (usb_proc_msignal(&bus->explore_proc,
1916 &bus->explore_msg[0], &bus->explore_msg[1])) {
1920 USB_BUS_UNLOCK(bus);
1924 /*------------------------------------------------------------------------*
1925 * usb_needs_explore_all
1927 * This function is called whenever a new driver is loaded and will
1928 * cause that all USB busses are re-explored.
1929 *------------------------------------------------------------------------*/
1931 usb_needs_explore_all(void)
1933 struct usb_bus *bus;
1940 dc = usb_devclass_ptr;
1942 DPRINTFN(0, "no devclass\n");
1946 * Explore all USB busses in parallell.
1948 max = devclass_get_maxunit(dc);
1950 dev = devclass_get_device(dc, max);
1952 bus = device_get_softc(dev);
1954 usb_needs_explore(bus, 1);
1961 /*------------------------------------------------------------------------*
1962 * usb_bus_power_update
1964 * This function will ensure that all USB devices on the given bus are
1965 * properly suspended or resumed according to the device transfer
1967 *------------------------------------------------------------------------*/
1970 usb_bus_power_update(struct usb_bus *bus)
1972 usb_needs_explore(bus, 0 /* no probe */ );
1976 /*------------------------------------------------------------------------*
1977 * usbd_transfer_power_ref
1979 * This function will modify the power save reference counts and
1980 * wakeup the USB device associated with the given USB transfer, if
1982 *------------------------------------------------------------------------*/
1985 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1987 static const usb_power_mask_t power_mask[4] = {
1988 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1989 [UE_BULK] = USB_HW_POWER_BULK,
1990 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1991 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1993 struct usb_device *udev;
1994 uint8_t needs_explore;
1995 uint8_t needs_hw_power;
1998 udev = xfer->xroot->udev;
2000 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2001 /* no power save for root HUB */
2004 USB_BUS_LOCK(udev->bus);
2006 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2008 udev->pwr_save.last_xfer_time = ticks;
2009 udev->pwr_save.type_refs[xfer_type] += val;
2011 if (xfer->flags_int.control_xfr) {
2012 udev->pwr_save.read_refs += val;
2013 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2015 * It is not allowed to suspend during a
2018 udev->pwr_save.write_refs += val;
2020 } else if (USB_GET_DATA_ISREAD(xfer)) {
2021 udev->pwr_save.read_refs += val;
2023 udev->pwr_save.write_refs += val;
2027 if (udev->flags.self_suspended)
2028 needs_explore = usb_peer_should_wakeup(udev);
2032 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2033 DPRINTF("Adding type %u to power state\n", xfer_type);
2034 udev->bus->hw_power_state |= power_mask[xfer_type];
2044 USB_BUS_UNLOCK(udev->bus);
2046 if (needs_explore) {
2047 DPRINTF("update\n");
2048 usb_bus_power_update(udev->bus);
2049 } else if (needs_hw_power) {
2050 DPRINTF("needs power\n");
2051 if (udev->bus->methods->set_hw_power != NULL) {
2052 (udev->bus->methods->set_hw_power) (udev->bus);
2058 /*------------------------------------------------------------------------*
2059 * usb_peer_should_wakeup
2061 * This function returns non-zero if the current device should wake up.
2062 *------------------------------------------------------------------------*/
2064 usb_peer_should_wakeup(struct usb_device *udev)
2066 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2067 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2068 (udev->re_enumerate_wait != 0) ||
2069 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2070 (udev->pwr_save.write_refs != 0) ||
2071 ((udev->pwr_save.read_refs != 0) &&
2072 (udev->flags.usb_mode == USB_MODE_HOST) &&
2073 (usb_peer_can_wakeup(udev) == 0)));
2076 /*------------------------------------------------------------------------*
2079 * This function implements the USB power daemon and is called
2080 * regularly from the USB explore thread.
2081 *------------------------------------------------------------------------*/
2084 usb_bus_powerd(struct usb_bus *bus)
2086 struct usb_device *udev;
2089 usb_ticks_t mintime;
2090 usb_size_t type_refs[5];
2093 limit = usb_power_timeout;
2096 else if (limit > 255)
2101 DPRINTF("bus=%p\n", bus);
2106 * The root HUB device is never suspended
2107 * and we simply skip it.
2109 for (x = USB_ROOT_HUB_ADDR + 1;
2110 x != bus->devices_max; x++) {
2112 udev = bus->devices[x];
2116 temp = ticks - udev->pwr_save.last_xfer_time;
2118 if (usb_peer_should_wakeup(udev)) {
2119 /* check if we are suspended */
2120 if (udev->flags.self_suspended != 0) {
2121 USB_BUS_UNLOCK(bus);
2122 usb_dev_resume_peer(udev);
2125 } else if ((temp >= limit) &&
2126 (udev->flags.usb_mode == USB_MODE_HOST) &&
2127 (udev->flags.self_suspended == 0)) {
2128 /* try to do suspend */
2130 USB_BUS_UNLOCK(bus);
2131 usb_dev_suspend_peer(udev);
2136 /* reset counters */
2138 mintime = (usb_ticks_t)-1;
2145 /* Re-loop all the devices to get the actual state */
2147 for (x = USB_ROOT_HUB_ADDR + 1;
2148 x != bus->devices_max; x++) {
2150 udev = bus->devices[x];
2154 /* we found a non-Root-Hub USB device */
2157 /* "last_xfer_time" can be updated by a resume */
2158 temp = ticks - udev->pwr_save.last_xfer_time;
2161 * Compute minimum time since last transfer for the complete
2167 if (udev->flags.self_suspended == 0) {
2168 type_refs[0] += udev->pwr_save.type_refs[0];
2169 type_refs[1] += udev->pwr_save.type_refs[1];
2170 type_refs[2] += udev->pwr_save.type_refs[2];
2171 type_refs[3] += udev->pwr_save.type_refs[3];
2175 if (mintime >= (usb_ticks_t)(1 * hz)) {
2176 /* recompute power masks */
2177 DPRINTF("Recomputing power masks\n");
2178 bus->hw_power_state = 0;
2179 if (type_refs[UE_CONTROL] != 0)
2180 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2181 if (type_refs[UE_BULK] != 0)
2182 bus->hw_power_state |= USB_HW_POWER_BULK;
2183 if (type_refs[UE_INTERRUPT] != 0)
2184 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2185 if (type_refs[UE_ISOCHRONOUS] != 0)
2186 bus->hw_power_state |= USB_HW_POWER_ISOC;
2187 if (type_refs[4] != 0)
2188 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2190 USB_BUS_UNLOCK(bus);
2192 if (bus->methods->set_hw_power != NULL) {
2193 /* always update hardware power! */
2194 (bus->methods->set_hw_power) (bus);
2200 /*------------------------------------------------------------------------*
2201 * usb_dev_resume_peer
2203 * This function will resume an USB peer and do the required USB
2204 * signalling to get an USB device out of the suspended state.
2205 *------------------------------------------------------------------------*/
2207 usb_dev_resume_peer(struct usb_device *udev)
2209 struct usb_bus *bus;
2216 /* check if already resumed */
2217 if (udev->flags.self_suspended == 0)
2220 /* we need a parent HUB to do resume */
2221 if (udev->parent_hub == NULL)
2224 DPRINTF("udev=%p\n", udev);
2226 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2227 (udev->flags.remote_wakeup == 0)) {
2229 * If the host did not set the remote wakeup feature, we can
2230 * not wake it up either!
2232 DPRINTF("remote wakeup is not set!\n");
2235 /* get bus pointer */
2238 /* resume parent hub first */
2239 usb_dev_resume_peer(udev->parent_hub);
2241 /* reduce chance of instant resume failure by waiting a little bit */
2242 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2244 if (usb_device_20_compatible(udev)) {
2245 /* resume current port (Valid in Host and Device Mode) */
2246 err = usbd_req_clear_port_feature(udev->parent_hub,
2247 NULL, udev->port_no, UHF_PORT_SUSPEND);
2249 DPRINTFN(0, "Resuming port failed\n");
2253 /* resume current port (Valid in Host and Device Mode) */
2254 err = usbd_req_set_port_link_state(udev->parent_hub,
2255 NULL, udev->port_no, UPS_PORT_LS_U0);
2257 DPRINTFN(0, "Resuming port failed\n");
2262 /* resume settle time */
2263 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2265 if (bus->methods->device_resume != NULL) {
2266 /* resume USB device on the USB controller */
2267 (bus->methods->device_resume) (udev);
2270 /* set that this device is now resumed */
2271 udev->flags.self_suspended = 0;
2273 /* make sure that we don't go into suspend right away */
2274 udev->pwr_save.last_xfer_time = ticks;
2276 /* make sure the needed power masks are on */
2277 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2278 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2279 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2280 bus->hw_power_state |= USB_HW_POWER_BULK;
2281 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2282 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2283 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2284 bus->hw_power_state |= USB_HW_POWER_ISOC;
2286 USB_BUS_UNLOCK(bus);
2288 if (bus->methods->set_hw_power != NULL) {
2289 /* always update hardware power! */
2290 (bus->methods->set_hw_power) (bus);
2295 /* notify all sub-devices about resume */
2296 err = usb_suspend_resume(udev, 0);
2298 usbd_sr_unlock(udev);
2300 /* check if peer has wakeup capability */
2301 if (usb_peer_can_wakeup(udev)) {
2302 /* clear remote wakeup */
2303 err = usbd_req_clear_device_feature(udev,
2304 NULL, UF_DEVICE_REMOTE_WAKEUP);
2306 DPRINTFN(0, "Clearing device "
2307 "remote wakeup failed: %s\n",
2313 /*------------------------------------------------------------------------*
2314 * usb_dev_suspend_peer
2316 * This function will suspend an USB peer and do the required USB
2317 * signalling to get an USB device into the suspended state.
2318 *------------------------------------------------------------------------*/
2320 usb_dev_suspend_peer(struct usb_device *udev)
2322 struct usb_device *child;
2332 /* check if already suspended */
2333 if (udev->flags.self_suspended)
2336 /* we need a parent HUB to do suspend */
2337 if (udev->parent_hub == NULL)
2340 DPRINTF("udev=%p\n", udev);
2342 /* check if the current device is a HUB */
2343 if (udev->hub != NULL) {
2344 nports = udev->hub->nports;
2346 /* check if all devices on the HUB are suspended */
2347 for (x = 0; x != nports; x++) {
2348 child = usb_bus_port_get_device(udev->bus,
2349 udev->hub->ports + x);
2354 if (child->flags.self_suspended)
2357 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2362 if (usb_peer_can_wakeup(udev)) {
2364 * This request needs to be done before we set
2365 * "udev->flags.self_suspended":
2368 /* allow device to do remote wakeup */
2369 err = usbd_req_set_device_feature(udev,
2370 NULL, UF_DEVICE_REMOTE_WAKEUP);
2372 DPRINTFN(0, "Setting device "
2373 "remote wakeup failed\n");
2377 USB_BUS_LOCK(udev->bus);
2379 * Checking for suspend condition and setting suspended bit
2382 err = usb_peer_should_wakeup(udev);
2385 * Set that this device is suspended. This variable
2386 * must be set before calling USB controller suspend
2389 udev->flags.self_suspended = 1;
2391 USB_BUS_UNLOCK(udev->bus);
2394 if (usb_peer_can_wakeup(udev)) {
2395 /* allow device to do remote wakeup */
2396 err = usbd_req_clear_device_feature(udev,
2397 NULL, UF_DEVICE_REMOTE_WAKEUP);
2399 DPRINTFN(0, "Setting device "
2400 "remote wakeup failed\n");
2404 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2405 /* resume parent HUB first */
2406 usb_dev_resume_peer(udev->parent_hub);
2408 /* reduce chance of instant resume failure by waiting a little bit */
2409 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2411 /* resume current port (Valid in Host and Device Mode) */
2412 err = usbd_req_clear_port_feature(udev->parent_hub,
2413 NULL, udev->port_no, UHF_PORT_SUSPEND);
2415 /* resume settle time */
2416 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2418 DPRINTF("Suspend was cancelled!\n");
2424 /* notify all sub-devices about suspend */
2425 err = usb_suspend_resume(udev, 1);
2427 usbd_sr_unlock(udev);
2429 if (udev->bus->methods->device_suspend != NULL) {
2432 /* suspend device on the USB controller */
2433 (udev->bus->methods->device_suspend) (udev);
2436 temp = usbd_get_dma_delay(udev);
2438 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2442 if (usb_device_20_compatible(udev)) {
2443 /* suspend current port */
2444 err = usbd_req_set_port_feature(udev->parent_hub,
2445 NULL, udev->port_no, UHF_PORT_SUSPEND);
2447 DPRINTFN(0, "Suspending port failed\n");
2451 /* suspend current port */
2452 err = usbd_req_set_port_link_state(udev->parent_hub,
2453 NULL, udev->port_no, UPS_PORT_LS_U3);
2455 DPRINTFN(0, "Suspending port failed\n");
2460 udev = udev->parent_hub;
2464 /*------------------------------------------------------------------------*
2465 * usbd_set_power_mode
2467 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2469 *------------------------------------------------------------------------*/
2471 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2473 /* filter input argument */
2474 if ((power_mode != USB_POWER_MODE_ON) &&
2475 (power_mode != USB_POWER_MODE_OFF))
2476 power_mode = USB_POWER_MODE_SAVE;
2478 power_mode = usbd_filter_power_mode(udev, power_mode);
2480 udev->power_mode = power_mode; /* update copy of power mode */
2483 usb_bus_power_update(udev->bus);
2487 /*------------------------------------------------------------------------*
2488 * usbd_filter_power_mode
2490 * This function filters the power mode based on hardware requirements.
2491 *------------------------------------------------------------------------*/
2493 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2495 struct usb_bus_methods *mtod;
2498 mtod = udev->bus->methods;
2501 if (mtod->get_power_mode != NULL)
2502 (mtod->get_power_mode) (udev, &temp);
2504 /* check if we should not filter */
2506 return (power_mode);
2508 /* use fixed power mode given by hardware driver */
2512 /*------------------------------------------------------------------------*
2513 * usbd_start_re_enumerate
2515 * This function starts re-enumeration of the given USB device. This
2516 * function does not need to be called BUS-locked. This function does
2517 * not wait until the re-enumeration is completed.
2518 *------------------------------------------------------------------------*/
2520 usbd_start_re_enumerate(struct usb_device *udev)
2522 if (udev->re_enumerate_wait == 0) {
2523 udev->re_enumerate_wait = 1;
2524 usb_needs_explore(udev->bus, 0);