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, &uhub_debug, 0,
83 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
87 static int usb_power_timeout = 30; /* seconds */
89 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
90 &usb_power_timeout, 0, "USB power timeout");
93 struct uhub_current_state {
99 struct uhub_current_state sc_st;/* current state */
100 device_t sc_dev; /* base device */
101 struct mtx sc_mtx; /* our mutex */
102 struct usb_device *sc_udev; /* USB device */
103 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
105 #define UHUB_FLAG_DID_EXPLORE 0x01
109 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
110 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
111 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
112 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
113 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
115 /* prototypes for type checking: */
117 static device_probe_t uhub_probe;
118 static device_attach_t uhub_attach;
119 static device_detach_t uhub_detach;
120 static device_suspend_t uhub_suspend;
121 static device_resume_t uhub_resume;
123 static bus_driver_added_t uhub_driver_added;
124 static bus_child_location_str_t uhub_child_location_string;
125 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
127 static usb_callback_t uhub_intr_callback;
129 static void usb_dev_resume_peer(struct usb_device *udev);
130 static void usb_dev_suspend_peer(struct usb_device *udev);
131 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
133 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
136 .type = UE_INTERRUPT,
137 .endpoint = UE_ADDR_ANY,
138 .direction = UE_DIR_ANY,
140 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
141 .bufsize = 0, /* use wMaxPacketSize */
142 .callback = &uhub_intr_callback,
143 .interval = UHUB_INTR_INTERVAL,
148 * driver instance for "hub" connected to "usb"
149 * and "hub" connected to "hub"
151 static devclass_t uhub_devclass;
153 static device_method_t uhub_methods[] = {
154 DEVMETHOD(device_probe, uhub_probe),
155 DEVMETHOD(device_attach, uhub_attach),
156 DEVMETHOD(device_detach, uhub_detach),
158 DEVMETHOD(device_suspend, uhub_suspend),
159 DEVMETHOD(device_resume, uhub_resume),
161 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
162 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
163 DEVMETHOD(bus_driver_added, uhub_driver_added),
167 static driver_t uhub_driver = {
169 .methods = uhub_methods,
170 .size = sizeof(struct uhub_softc)
173 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
174 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
175 MODULE_VERSION(uhub, 1);
178 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
180 struct uhub_softc *sc = usbd_xfer_softc(xfer);
182 switch (USB_GET_STATE(xfer)) {
183 case USB_ST_TRANSFERRED:
186 * This is an indication that some port
187 * has changed status. Notify the bus
188 * event handler thread that we need
189 * to be explored again:
191 usb_needs_explore(sc->sc_udev->bus, 0);
194 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
195 usbd_transfer_submit(xfer);
199 if (xfer->error != USB_ERR_CANCELLED) {
201 * Do a clear-stall. The "stall_pipe" flag
202 * will get cleared before next callback by
205 usbd_xfer_set_stall(xfer);
206 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
207 usbd_transfer_submit(xfer);
213 /*------------------------------------------------------------------------*
214 * uhub_explore_sub - subroutine
218 * Else: A control transaction failed
219 *------------------------------------------------------------------------*/
221 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
224 struct usb_device *child;
228 bus = sc->sc_udev->bus;
231 /* get driver added refcount from USB bus */
232 refcount = bus->driver_added_refcount;
234 /* get device assosiated with the given port */
235 child = usb_bus_port_get_device(bus, up);
241 /* check if device should be re-enumerated */
243 if (child->flags.usb_mode == USB_MODE_HOST) {
244 usbd_enum_lock(child);
245 if (child->re_enumerate_wait) {
246 err = usbd_set_config_index(child,
249 DPRINTF("Unconfigure failed: "
253 err = usbd_req_re_enumerate(child, NULL);
255 err = usbd_set_config_index(child, 0);
257 err = usb_probe_and_attach(child,
258 USB_IFACE_INDEX_ANY);
260 child->re_enumerate_wait = 0;
263 usbd_enum_unlock(child);
266 /* check if probe and attach should be done */
268 if (child->driver_added_refcount != refcount) {
269 child->driver_added_refcount = refcount;
270 err = usb_probe_and_attach(child,
271 USB_IFACE_INDEX_ANY);
276 /* start control transfer, if device mode */
278 if (child->flags.usb_mode == USB_MODE_DEVICE)
279 usbd_ctrl_transfer_setup(child);
281 /* if a HUB becomes present, do a recursive HUB explore */
284 err = (child->hub->explore) (child);
290 /*------------------------------------------------------------------------*
291 * uhub_read_port_status - factored out code
292 *------------------------------------------------------------------------*/
294 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
296 struct usb_port_status ps;
299 err = usbd_req_get_port_status(
300 sc->sc_udev, NULL, &ps, portno);
302 /* update status regardless of error */
304 sc->sc_st.port_status = UGETW(ps.wPortStatus);
305 sc->sc_st.port_change = UGETW(ps.wPortChange);
307 /* debugging print */
309 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
310 "wPortChange=0x%04x, err=%s\n",
311 portno, sc->sc_st.port_status,
312 sc->sc_st.port_change, usbd_errstr(err));
316 /*------------------------------------------------------------------------*
321 * Else: A control transaction failed
322 *------------------------------------------------------------------------*/
324 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
326 struct usb_device *child;
327 struct usb_device *udev;
328 enum usb_dev_speed speed;
329 enum usb_hc_mode mode;
334 DPRINTF("reattaching port %d\n", portno);
339 child = usb_bus_port_get_device(udev->bus,
340 udev->hub->ports + portno - 1);
344 /* first clear the port connection change bit */
346 err = usbd_req_clear_port_feature(udev, NULL,
347 portno, UHF_C_PORT_CONNECTION);
352 /* check if there is a child */
356 * Free USB device and all subdevices, if any.
358 usb_free_device(child, 0);
361 /* get fresh status */
363 err = uhub_read_port_status(sc, portno);
367 /* check if nothing is connected to the port */
369 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
372 /* check if there is no power on the port and print a warning */
374 switch (udev->speed) {
378 power_mask = UPS_PORT_POWER;
380 case USB_SPEED_SUPER:
381 if (udev->parent_hub == NULL)
382 power_mask = UPS_PORT_POWER;
384 power_mask = UPS_PORT_POWER_SS;
390 if (!(sc->sc_st.port_status & power_mask)) {
391 DPRINTF("WARNING: strange, connected port %d "
392 "has no power\n", portno);
395 /* check if the device is in Host Mode */
397 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
399 DPRINTF("Port %d is in Host Mode\n", portno);
401 if (sc->sc_st.port_status & UPS_SUSPEND) {
403 * NOTE: Should not get here in SuperSpeed
404 * mode, because the HUB should report this
407 DPRINTF("Port %d was still "
408 "suspended, clearing.\n", portno);
409 err = usbd_req_clear_port_feature(udev,
410 NULL, portno, UHF_PORT_SUSPEND);
415 /* wait for maximum device power up time */
418 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
420 /* reset port, which implies enabling it */
422 err = usbd_req_reset_port(udev, NULL, portno);
425 DPRINTFN(0, "port %d reset "
426 "failed, error=%s\n",
427 portno, usbd_errstr(err));
430 /* get port status again, it might have changed during reset */
432 err = uhub_read_port_status(sc, portno);
436 /* check if something changed during port reset */
438 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
439 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
441 DPRINTFN(0, "giving up port reset "
442 "- device vanished\n");
449 DPRINTF("Port %d is in Device Mode\n", portno);
453 * Figure out the device speed
455 switch (udev->speed) {
457 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
458 speed = USB_SPEED_HIGH;
459 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
460 speed = USB_SPEED_LOW;
462 speed = USB_SPEED_FULL;
465 if (sc->sc_st.port_status & UPS_LOW_SPEED)
466 speed = USB_SPEED_LOW;
468 speed = USB_SPEED_FULL;
471 speed = USB_SPEED_LOW;
473 case USB_SPEED_SUPER:
474 if (udev->parent_hub == NULL) {
475 /* Root HUB - special case */
476 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
478 speed = USB_SPEED_FULL;
481 speed = USB_SPEED_LOW;
484 speed = USB_SPEED_HIGH;
487 speed = USB_SPEED_SUPER;
491 speed = USB_SPEED_SUPER;
495 /* same speed like parent */
499 if (speed == USB_SPEED_SUPER) {
500 err = usbd_req_set_hub_u1_timeout(udev, NULL,
501 portno, 128 - (2 * udev->depth));
503 DPRINTFN(0, "port %d U1 timeout "
504 "failed, error=%s\n",
505 portno, usbd_errstr(err));
507 err = usbd_req_set_hub_u2_timeout(udev, NULL,
508 portno, 128 - (2 * udev->depth));
510 DPRINTFN(0, "port %d U2 timeout "
511 "failed, error=%s\n",
512 portno, usbd_errstr(err));
517 * Figure out the device mode
519 * NOTE: This part is currently FreeBSD specific.
521 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
522 mode = USB_MODE_DEVICE;
524 mode = USB_MODE_HOST;
526 /* need to create a new child */
527 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
528 udev->depth + 1, portno - 1, portno, speed, mode);
530 DPRINTFN(0, "could not allocate new device\n");
533 return (0); /* success */
538 * Free USB device and all subdevices, if any.
540 usb_free_device(child, 0);
544 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
545 err = usbd_req_clear_port_feature(
547 portno, UHF_PORT_ENABLE);
551 DPRINTFN(0, "device problem (%s), "
552 "disabling port %d\n", usbd_errstr(err), portno);
557 /*------------------------------------------------------------------------*
558 * usb_device_20_compatible
561 * 0: HUB does not support suspend and resume
562 * Else: HUB supports suspend and resume
563 *------------------------------------------------------------------------*/
565 usb_device_20_compatible(struct usb_device *udev)
569 switch (udev->speed) {
579 /*------------------------------------------------------------------------*
580 * uhub_suspend_resume_port
584 * Else: A control transaction failed
585 *------------------------------------------------------------------------*/
587 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
589 struct usb_device *child;
590 struct usb_device *udev;
594 DPRINTF("port %d\n", portno);
597 child = usb_bus_port_get_device(udev->bus,
598 udev->hub->ports + portno - 1);
600 /* first clear the port suspend change bit */
602 if (usb_device_20_compatible(udev)) {
603 err = usbd_req_clear_port_feature(udev, NULL,
604 portno, UHF_C_PORT_SUSPEND);
606 err = usbd_req_clear_port_feature(udev, NULL,
607 portno, UHF_C_PORT_LINK_STATE);
611 DPRINTF("clearing suspend failed.\n");
614 /* get fresh status */
616 err = uhub_read_port_status(sc, portno);
618 DPRINTF("reading port status failed.\n");
621 /* convert current state */
623 if (usb_device_20_compatible(udev)) {
624 if (sc->sc_st.port_status & UPS_SUSPEND) {
630 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
634 case UPS_PORT_LS_SS_INA:
635 usbd_req_warm_reset_port(udev, NULL, portno);
644 DPRINTF("suspended=%u\n", is_suspend);
646 /* do the suspend or resume */
650 * This code handle two cases: 1) Host Mode - we can only
651 * receive resume here 2) Device Mode - we can receive
652 * suspend and resume here
655 usb_dev_resume_peer(child);
656 else if (child->flags.usb_mode == USB_MODE_DEVICE)
657 usb_dev_suspend_peer(child);
663 /*------------------------------------------------------------------------*
664 * uhub_root_interrupt
666 * This function is called when a Root HUB interrupt has
667 * happened. "ptr" and "len" makes up the Root HUB interrupt
668 * packet. This function is called having the "bus_mtx" locked.
669 *------------------------------------------------------------------------*/
671 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
673 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
675 usb_needs_explore(bus, 0);
679 uhub_is_too_deep(struct usb_device *udev)
681 switch (udev->speed) {
685 if (udev->depth > USB_HUB_MAX_DEPTH)
688 case USB_SPEED_SUPER:
689 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
698 /*------------------------------------------------------------------------*
704 *------------------------------------------------------------------------*/
706 uhub_explore(struct usb_device *udev)
709 struct uhub_softc *sc;
718 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
720 /* ignore devices that are too deep */
721 if (uhub_is_too_deep(udev))
722 return (USB_ERR_TOO_DEEP);
724 /* check if device is suspended */
725 if (udev->flags.self_suspended) {
726 /* need to wait until the child signals resume */
727 DPRINTF("Device is suspended!\n");
732 * Make sure we don't race against user-space applications
735 usbd_enum_lock(udev);
737 for (x = 0; x != hub->nports; x++) {
741 err = uhub_read_port_status(sc, portno);
743 /* most likely the HUB is gone */
746 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
747 DPRINTF("Overcurrent on port %u.\n", portno);
748 err = usbd_req_clear_port_feature(
749 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
751 /* most likely the HUB is gone */
755 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
757 * Fake a connect status change so that the
758 * status gets checked initially!
760 sc->sc_st.port_change |=
761 UPS_C_CONNECT_STATUS;
763 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
764 err = usbd_req_clear_port_feature(
765 udev, NULL, portno, UHF_C_PORT_ENABLE);
767 /* most likely the HUB is gone */
770 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
772 * Ignore the port error if the device
775 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
776 DPRINTFN(0, "illegal enable change, "
777 "port %d\n", portno);
780 if (up->restartcnt == USB_RESTART_MAX) {
781 /* XXX could try another speed ? */
782 DPRINTFN(0, "port error, giving up "
783 "port %d\n", portno);
785 sc->sc_st.port_change |=
786 UPS_C_CONNECT_STATUS;
791 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
792 err = uhub_reattach_port(sc, portno);
794 /* most likely the HUB is gone */
798 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
799 UPS_C_PORT_LINK_STATE)) {
800 err = uhub_suspend_resume_port(sc, portno);
802 /* most likely the HUB is gone */
806 err = uhub_explore_sub(sc, up);
808 /* no device(s) present */
811 /* explore succeeded - reset restart counter */
815 usbd_enum_unlock(udev);
817 /* initial status checked */
818 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
821 return (USB_ERR_NORMAL_COMPLETION);
825 uhub_probe(device_t dev)
827 struct usb_attach_arg *uaa = device_get_ivars(dev);
829 if (uaa->usb_mode != USB_MODE_HOST)
833 * The subclass for USB HUBs is currently ignored because it
834 * is 0 for some and 1 for others.
836 if (uaa->info.bConfigIndex == 0 &&
837 uaa->info.bDeviceClass == UDCLASS_HUB)
843 /* NOTE: The information returned by this function can be wrong. */
845 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
847 struct usb_hub_descriptor hubdesc20;
848 struct usb_hub_ss_descriptor hubdesc30;
853 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
854 return (USB_ERR_INVAL);
859 switch (udev->speed) {
863 /* assuming that there is one port */
864 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
866 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
867 "error=%s\n", usbd_errstr(err));
870 nports = hubdesc20.bNbrPorts;
874 if (udev->speed == USB_SPEED_HIGH)
875 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
878 case USB_SPEED_SUPER:
879 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
881 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
882 "error=%s\n", usbd_errstr(err));
885 nports = hubdesc30.bNbrPorts;
905 uhub_attach(device_t dev)
907 struct uhub_softc *sc = device_get_softc(dev);
908 struct usb_attach_arg *uaa = device_get_ivars(dev);
909 struct usb_device *udev = uaa->device;
910 struct usb_device *parent_hub = udev->parent_hub;
912 struct usb_hub_descriptor hubdesc20;
913 struct usb_hub_ss_descriptor hubdesc30;
925 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
927 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
928 device_get_nameunit(dev));
930 device_set_usb_desc(dev);
932 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
933 "parent->selfpowered=%d\n",
935 udev->flags.self_powered,
938 parent_hub->flags.self_powered : 0);
940 if (uhub_is_too_deep(udev)) {
941 DPRINTFN(0, "HUB at depth %d, "
942 "exceeds maximum. HUB ignored\n", (int)udev->depth);
946 if (!udev->flags.self_powered && parent_hub &&
947 !parent_hub->flags.self_powered) {
948 DPRINTFN(0, "Bus powered HUB connected to "
949 "bus powered HUB. HUB ignored\n");
953 if (UHUB_IS_MULTI_TT(sc)) {
954 err = usbd_set_alt_interface_index(udev, 0, 1);
956 device_printf(dev, "MTT could not be enabled\n");
959 device_printf(dev, "MTT enabled\n");
962 /* get HUB descriptor */
964 DPRINTFN(2, "Getting HUB descriptor\n");
966 switch (udev->speed) {
970 /* assuming that there is one port */
971 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
973 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
974 "error=%s\n", usbd_errstr(err));
977 /* get number of ports */
978 nports = hubdesc20.bNbrPorts;
980 /* get power delay */
981 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
982 USB_EXTRA_POWER_UP_TIME);
984 /* get complete HUB descriptor */
986 /* check number of ports */
988 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
989 "error=%s\n", usbd_errstr(err));
992 /* get complete HUB descriptor */
993 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
996 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
997 "error=%s\n", usbd_errstr(err));
1000 if (hubdesc20.bNbrPorts != nports) {
1001 DPRINTFN(0, "Number of ports changed\n");
1006 case USB_SPEED_SUPER:
1007 if (udev->parent_hub != NULL) {
1008 err = usbd_req_set_hub_depth(udev, NULL,
1011 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1012 "error=%s\n", usbd_errstr(err));
1016 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1018 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1019 "error=%s\n", usbd_errstr(err));
1022 /* get number of ports */
1023 nports = hubdesc30.bNbrPorts;
1025 /* get power delay */
1026 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1027 USB_EXTRA_POWER_UP_TIME);
1029 /* get complete HUB descriptor */
1031 /* check number of ports */
1032 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1033 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1034 "error=%s\n", usbd_errstr(err));
1037 /* get complete HUB descriptor */
1038 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1041 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1042 "error=%s\n", usbd_errstr(err));
1045 if (hubdesc30.bNbrPorts != nports) {
1046 DPRINTFN(0, "Number of ports changed\n");
1052 DPRINTF("Assuming HUB has only one port\n");
1053 /* default number of ports */
1055 /* default power delay */
1056 pwrdly = ((10 * UHD_PWRON_FACTOR) + USB_EXTRA_POWER_UP_TIME);
1060 DPRINTFN(0, "portless HUB\n");
1063 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1064 M_USBDEV, M_WAITOK | M_ZERO);
1071 /* initialize HUB structure */
1073 hub->explore = &uhub_explore;
1074 hub->nports = nports;
1075 hub->hubudev = udev;
1077 /* if self powered hub, give ports maximum current */
1078 if (udev->flags.self_powered) {
1079 hub->portpower = USB_MAX_POWER;
1081 hub->portpower = USB_MIN_POWER;
1084 /* set up interrupt pipe */
1086 if (udev->parent_hub == NULL) {
1087 /* root HUB is special */
1091 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1092 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1095 DPRINTFN(0, "cannot setup interrupt transfer, "
1096 "errstr=%s\n", usbd_errstr(err));
1099 /* wait with power off for a while */
1100 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1103 * To have the best chance of success we do things in the exact same
1104 * order as Windoze98. This should not be necessary, but some
1105 * devices do not follow the USB specs to the letter.
1107 * These are the events on the bus when a hub is attached:
1108 * Get device and config descriptors (see attach code)
1109 * Get hub descriptor (see above)
1112 * wait for power to become stable
1113 * (all below happens in explore code)
1115 * clear C_PORT_CONNECTION
1118 * if device connected
1122 * clear C_PORT_RESET
1124 * proceed with device attachment
1127 /* XXX should check for none, individual, or ganged power? */
1131 for (x = 0; x != nports; x++) {
1132 /* set up data structures */
1133 struct usb_port *up = hub->ports + x;
1135 up->device_index = 0;
1139 /* check if port is removable */
1140 switch (udev->speed) {
1142 case USB_SPEED_FULL:
1143 case USB_SPEED_HIGH:
1144 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1147 case USB_SPEED_SUPER:
1148 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1152 DPRINTF("Assuming removable port\n");
1157 /* turn the power on */
1158 err = usbd_req_set_port_feature(udev, NULL,
1159 portno, UHF_PORT_POWER);
1162 DPRINTFN(0, "port %d power on failed, %s\n",
1163 portno, usbd_errstr(err));
1165 DPRINTF("turn on port %d power\n",
1168 /* wait for stable power */
1169 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1172 device_printf(dev, "%d port%s with %d "
1173 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1174 removable, udev->flags.self_powered ? "self" : "bus");
1176 /* Start the interrupt endpoint, if any */
1178 if (sc->sc_xfer[0] != NULL) {
1179 mtx_lock(&sc->sc_mtx);
1180 usbd_transfer_start(sc->sc_xfer[0]);
1181 mtx_unlock(&sc->sc_mtx);
1184 /* Enable automatic power save on all USB HUBs */
1186 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1191 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1194 free(udev->hub, M_USBDEV);
1198 mtx_destroy(&sc->sc_mtx);
1204 * Called from process context when the hub is gone.
1205 * Detach all devices on active ports.
1208 uhub_detach(device_t dev)
1210 struct uhub_softc *sc = device_get_softc(dev);
1211 struct usb_hub *hub = sc->sc_udev->hub;
1212 struct usb_device *child;
1215 if (hub == NULL) /* must be partially working */
1218 /* Make sure interrupt transfer is gone. */
1219 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1221 /* Detach all ports */
1222 for (x = 0; x != hub->nports; x++) {
1224 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1226 if (child == NULL) {
1231 * Free USB device and all subdevices, if any.
1233 usb_free_device(child, 0);
1236 free(hub, M_USBDEV);
1237 sc->sc_udev->hub = NULL;
1239 mtx_destroy(&sc->sc_mtx);
1245 uhub_suspend(device_t dev)
1248 /* Sub-devices are not suspended here! */
1253 uhub_resume(device_t dev)
1256 /* Sub-devices are not resumed here! */
1261 uhub_driver_added(device_t dev, driver_t *driver)
1263 usb_needs_explore_all();
1267 struct usb_device *udev;
1269 uint8_t iface_index;
1273 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1274 struct hub_result *res)
1276 struct usb_interface *iface;
1277 struct usb_device *udev;
1282 nports = hub->nports;
1283 for (x = 0; x != nports; x++) {
1284 udev = usb_bus_port_get_device(hub->hubudev->bus,
1289 for (i = 0; i != USB_IFACE_MAX; i++) {
1290 iface = usbd_get_iface(udev, i);
1292 (iface->subdev == child)) {
1293 res->iface_index = i;
1295 res->portno = x + 1;
1300 res->iface_index = 0;
1306 uhub_child_location_string(device_t parent, device_t child,
1307 char *buf, size_t buflen)
1309 struct uhub_softc *sc;
1310 struct usb_hub *hub;
1311 struct hub_result res;
1313 if (!device_is_attached(parent)) {
1319 sc = device_get_softc(parent);
1320 hub = sc->sc_udev->hub;
1323 uhub_find_iface_index(hub, child, &res);
1325 DPRINTF("device not on hub\n");
1331 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1332 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1333 res.portno, device_get_unit(res.udev->bus->bdev),
1334 res.udev->device_index, res.iface_index);
1342 uhub_child_pnpinfo_string(device_t parent, device_t child,
1343 char *buf, size_t buflen)
1345 struct uhub_softc *sc;
1346 struct usb_hub *hub;
1347 struct usb_interface *iface;
1348 struct hub_result res;
1350 if (!device_is_attached(parent)) {
1356 sc = device_get_softc(parent);
1357 hub = sc->sc_udev->hub;
1360 uhub_find_iface_index(hub, child, &res);
1362 DPRINTF("device not on hub\n");
1368 iface = usbd_get_iface(res.udev, res.iface_index);
1369 if (iface && iface->idesc) {
1370 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1371 "devclass=0x%02x devsubclass=0x%02x "
1375 "intclass=0x%02x intsubclass=0x%02x "
1376 "intprotocol=0x%02x " "%s%s",
1377 UGETW(res.udev->ddesc.idVendor),
1378 UGETW(res.udev->ddesc.idProduct),
1379 res.udev->ddesc.bDeviceClass,
1380 res.udev->ddesc.bDeviceSubClass,
1381 usb_get_serial(res.udev),
1382 UGETW(res.udev->ddesc.bcdDevice),
1383 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1384 iface->idesc->bInterfaceClass,
1385 iface->idesc->bInterfaceSubClass,
1386 iface->idesc->bInterfaceProtocol,
1387 iface->pnpinfo ? " " : "",
1388 iface->pnpinfo ? iface->pnpinfo : "");
1402 * The USB Transaction Translator:
1403 * ===============================
1405 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1406 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1407 * USB transfers. To utilize bandwidth dynamically the "scatter and
1408 * gather" principle must be applied. This means that bandwidth must
1409 * be divided into equal parts of bandwidth. With regard to USB all
1410 * data is transferred in smaller packets with length
1411 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1414 * The bandwidth scheduler which I have implemented will simply pack
1415 * the USB transfers back to back until there is no more space in the
1416 * schedule. Out of the 8 microframes which the USB 2.0 standard
1417 * provides, only 6 are available for non-HIGH-speed devices. I have
1418 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1419 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1420 * this division, it is very difficult to allocate and free bandwidth
1423 * NOTE about the Transaction Translator in USB HUBs:
1425 * USB HUBs have a very simple Transaction Translator, that will
1426 * simply pipeline all the SPLIT transactions. That means that the
1427 * transactions will be executed in the order they are queued!
1431 /*------------------------------------------------------------------------*
1432 * usb_intr_find_best_slot
1435 * The best Transaction Translation slot for an interrupt endpoint.
1436 *------------------------------------------------------------------------*/
1438 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1439 uint8_t end, uint8_t mask)
1441 usb_size_t min = 0 - 1;
1449 /* find the last slot with lesser used bandwidth */
1451 for (x = start; x < end; x++) {
1455 /* compute sum of bandwidth */
1456 for (z = x; z < end; z++) {
1457 if (mask & (1U << (z - x)))
1461 /* check if the current multi-slot is more optimal */
1467 /* check if the mask is about to be shifted out */
1468 if (mask & (1U << (end - 1 - x)))
1474 /*------------------------------------------------------------------------*
1475 * usb_hs_bandwidth_adjust
1477 * This function will update the bandwith usage for the microframe
1478 * having index "slot" by "len" bytes. "len" can be negative. If the
1479 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1480 * the "slot" argument will be replaced by the slot having least used
1481 * bandwidth. The "mask" argument is used for multi-slot allocations.
1484 * The slot in which the bandwidth update was done: 0..7
1485 *------------------------------------------------------------------------*/
1487 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1488 uint8_t slot, uint8_t mask)
1490 struct usb_bus *bus = udev->bus;
1491 struct usb_hub *hub;
1492 enum usb_dev_speed speed;
1495 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1497 speed = usbd_get_speed(udev);
1501 case USB_SPEED_FULL:
1502 if (speed == USB_SPEED_LOW) {
1506 * The Host Controller Driver should have
1507 * performed checks so that the lookup
1508 * below does not result in a NULL pointer
1512 hub = udev->parent_hs_hub->hub;
1513 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1514 slot = usb_intr_find_best_slot(hub->uframe_usage,
1515 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1517 for (x = slot; x < 8; x++) {
1518 if (mask & (1U << (x - slot))) {
1519 hub->uframe_usage[x] += len;
1520 bus->uframe_usage[x] += len;
1525 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1526 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1527 USB_HS_MICRO_FRAMES_MAX, mask);
1529 for (x = slot; x < 8; x++) {
1530 if (mask & (1U << (x - slot))) {
1531 bus->uframe_usage[x] += len;
1539 /*------------------------------------------------------------------------*
1540 * usb_hs_bandwidth_alloc
1542 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1543 *------------------------------------------------------------------------*/
1545 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1547 struct usb_device *udev;
1552 udev = xfer->xroot->udev;
1554 if (udev->flags.usb_mode != USB_MODE_HOST)
1555 return; /* not supported */
1557 xfer->endpoint->refcount_bw++;
1558 if (xfer->endpoint->refcount_bw != 1)
1559 return; /* already allocated */
1561 speed = usbd_get_speed(udev);
1563 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1565 /* allocate a microframe slot */
1568 slot = usb_hs_bandwidth_adjust(udev,
1569 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1571 xfer->endpoint->usb_uframe = slot;
1572 xfer->endpoint->usb_smask = mask << slot;
1574 if ((speed != USB_SPEED_FULL) &&
1575 (speed != USB_SPEED_LOW)) {
1576 xfer->endpoint->usb_cmask = 0x00 ;
1578 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1582 case UE_ISOCHRONOUS:
1583 switch (usbd_xfer_get_fps_shift(xfer)) {
1598 /* allocate a microframe multi-slot */
1600 slot = usb_hs_bandwidth_adjust(udev,
1601 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1603 xfer->endpoint->usb_uframe = slot;
1604 xfer->endpoint->usb_cmask = 0;
1605 xfer->endpoint->usb_smask = mask << slot;
1609 xfer->endpoint->usb_uframe = 0;
1610 xfer->endpoint->usb_cmask = 0;
1611 xfer->endpoint->usb_smask = 0;
1615 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1616 xfer->endpoint->usb_uframe,
1617 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1620 /*------------------------------------------------------------------------*
1621 * usb_hs_bandwidth_free
1623 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1624 *------------------------------------------------------------------------*/
1626 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1628 struct usb_device *udev;
1632 udev = xfer->xroot->udev;
1634 if (udev->flags.usb_mode != USB_MODE_HOST)
1635 return; /* not supported */
1637 xfer->endpoint->refcount_bw--;
1638 if (xfer->endpoint->refcount_bw != 0)
1639 return; /* still allocated */
1641 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1643 case UE_ISOCHRONOUS:
1645 slot = xfer->endpoint->usb_uframe;
1646 mask = xfer->endpoint->usb_smask;
1648 /* free microframe slot(s): */
1649 usb_hs_bandwidth_adjust(udev,
1650 -xfer->max_frame_size, slot, mask >> slot);
1652 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1653 slot, mask >> slot);
1655 xfer->endpoint->usb_uframe = 0;
1656 xfer->endpoint->usb_cmask = 0;
1657 xfer->endpoint->usb_smask = 0;
1665 /*------------------------------------------------------------------------*
1666 * usb_isoc_time_expand
1668 * This function will expand the time counter from 7-bit to 16-bit.
1671 * 16-bit isochronous time counter.
1672 *------------------------------------------------------------------------*/
1674 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1678 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1680 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1682 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1684 if (isoc_time_curr < rem) {
1685 /* the time counter wrapped around */
1686 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1688 /* update the remainder */
1690 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1691 bus->isoc_time_last |= isoc_time_curr;
1693 return (bus->isoc_time_last);
1696 /*------------------------------------------------------------------------*
1697 * usbd_fs_isoc_schedule_alloc_slot
1699 * This function will allocate bandwidth for an isochronous FULL speed
1700 * transaction in the FULL speed schedule.
1705 *------------------------------------------------------------------------*/
1706 #if USB_HAVE_TT_SUPPORT
1708 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1710 struct usb_xfer *xfer;
1711 struct usb_xfer *pipe_xfer;
1712 struct usb_bus *bus;
1714 usb_frlength_t data_len;
1722 bus = isoc_xfer->xroot->bus;
1724 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1726 /* skip self, if any */
1728 if (xfer == isoc_xfer)
1731 /* check if this USB transfer is going through the same TT */
1733 if (xfer->xroot->udev->parent_hs_hub !=
1734 isoc_xfer->xroot->udev->parent_hs_hub) {
1737 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1738 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1739 (xfer->xroot->udev->hs_port_no !=
1740 isoc_xfer->xroot->udev->hs_port_no)) {
1743 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1746 /* check if isoc_time is part of this transfer */
1748 delta = xfer->isoc_time_complete - isoc_time;
1749 if (delta > 0 && delta <= xfer->nframes) {
1750 delta = xfer->nframes - delta;
1752 len = xfer->frlengths[delta];
1760 /* check double buffered transfers */
1762 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.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 */
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 >= (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);