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) {
245 do_unlock = usbd_enum_lock(child);
246 if (child->re_enumerate_wait) {
247 err = usbd_set_config_index(child,
250 DPRINTF("Unconfigure failed: "
254 err = usbd_req_re_enumerate(child, NULL);
256 err = usbd_set_config_index(child, 0);
258 err = usb_probe_and_attach(child,
259 USB_IFACE_INDEX_ANY);
261 child->re_enumerate_wait = 0;
265 usbd_enum_unlock(child);
268 /* check if probe and attach should be done */
270 if (child->driver_added_refcount != refcount) {
271 child->driver_added_refcount = refcount;
272 err = usb_probe_and_attach(child,
273 USB_IFACE_INDEX_ANY);
278 /* start control transfer, if device mode */
280 if (child->flags.usb_mode == USB_MODE_DEVICE)
281 usbd_ctrl_transfer_setup(child);
283 /* if a HUB becomes present, do a recursive HUB explore */
286 err = (child->hub->explore) (child);
292 /*------------------------------------------------------------------------*
293 * uhub_read_port_status - factored out code
294 *------------------------------------------------------------------------*/
296 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
298 struct usb_port_status ps;
301 err = usbd_req_get_port_status(
302 sc->sc_udev, NULL, &ps, portno);
304 /* update status regardless of error */
306 sc->sc_st.port_status = UGETW(ps.wPortStatus);
307 sc->sc_st.port_change = UGETW(ps.wPortChange);
309 /* debugging print */
311 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
312 "wPortChange=0x%04x, err=%s\n",
313 portno, sc->sc_st.port_status,
314 sc->sc_st.port_change, usbd_errstr(err));
318 /*------------------------------------------------------------------------*
323 * Else: A control transaction failed
324 *------------------------------------------------------------------------*/
326 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
328 struct usb_device *child;
329 struct usb_device *udev;
330 enum usb_dev_speed speed;
331 enum usb_hc_mode mode;
336 DPRINTF("reattaching port %d\n", portno);
341 child = usb_bus_port_get_device(udev->bus,
342 udev->hub->ports + portno - 1);
346 /* first clear the port connection change bit */
348 err = usbd_req_clear_port_feature(udev, NULL,
349 portno, UHF_C_PORT_CONNECTION);
354 /* check if there is a child */
358 * Free USB device and all subdevices, if any.
360 usb_free_device(child, 0);
363 /* get fresh status */
365 err = uhub_read_port_status(sc, portno);
369 /* check if nothing is connected to the port */
371 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
374 /* check if there is no power on the port and print a warning */
376 switch (udev->speed) {
380 power_mask = UPS_PORT_POWER;
382 case USB_SPEED_SUPER:
383 if (udev->parent_hub == NULL)
384 power_mask = UPS_PORT_POWER;
386 power_mask = UPS_PORT_POWER_SS;
392 if (!(sc->sc_st.port_status & power_mask)) {
393 DPRINTF("WARNING: strange, connected port %d "
394 "has no power\n", portno);
397 /* check if the device is in Host Mode */
399 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
401 DPRINTF("Port %d is in Host Mode\n", portno);
403 if (sc->sc_st.port_status & UPS_SUSPEND) {
405 * NOTE: Should not get here in SuperSpeed
406 * mode, because the HUB should report this
409 DPRINTF("Port %d was still "
410 "suspended, clearing.\n", portno);
411 err = usbd_req_clear_port_feature(udev,
412 NULL, portno, UHF_PORT_SUSPEND);
417 /* wait for maximum device power up time */
420 USB_MS_TO_TICKS(usb_port_powerup_delay));
422 /* reset port, which implies enabling it */
424 err = usbd_req_reset_port(udev, NULL, portno);
427 DPRINTFN(0, "port %d reset "
428 "failed, error=%s\n",
429 portno, usbd_errstr(err));
432 /* get port status again, it might have changed during reset */
434 err = uhub_read_port_status(sc, portno);
438 /* check if something changed during port reset */
440 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
441 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
443 DPRINTFN(0, "giving up port reset "
444 "- device vanished\n");
451 DPRINTF("Port %d is in Device Mode\n", portno);
455 * Figure out the device speed
457 switch (udev->speed) {
459 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
460 speed = USB_SPEED_HIGH;
461 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
462 speed = USB_SPEED_LOW;
464 speed = USB_SPEED_FULL;
467 if (sc->sc_st.port_status & UPS_LOW_SPEED)
468 speed = USB_SPEED_LOW;
470 speed = USB_SPEED_FULL;
473 speed = USB_SPEED_LOW;
475 case USB_SPEED_SUPER:
476 if (udev->parent_hub == NULL) {
477 /* Root HUB - special case */
478 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
480 speed = USB_SPEED_FULL;
483 speed = USB_SPEED_LOW;
486 speed = USB_SPEED_HIGH;
489 speed = USB_SPEED_SUPER;
493 speed = USB_SPEED_SUPER;
497 /* same speed like parent */
501 if (speed == USB_SPEED_SUPER) {
502 err = usbd_req_set_hub_u1_timeout(udev, NULL,
503 portno, 128 - (2 * udev->depth));
505 DPRINTFN(0, "port %d U1 timeout "
506 "failed, error=%s\n",
507 portno, usbd_errstr(err));
509 err = usbd_req_set_hub_u2_timeout(udev, NULL,
510 portno, 128 - (2 * udev->depth));
512 DPRINTFN(0, "port %d U2 timeout "
513 "failed, error=%s\n",
514 portno, usbd_errstr(err));
519 * Figure out the device mode
521 * NOTE: This part is currently FreeBSD specific.
523 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
524 mode = USB_MODE_DEVICE;
526 mode = USB_MODE_HOST;
528 /* need to create a new child */
529 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
530 udev->depth + 1, portno - 1, portno, speed, mode);
532 DPRINTFN(0, "could not allocate new device\n");
535 return (0); /* success */
540 * Free USB device and all subdevices, if any.
542 usb_free_device(child, 0);
546 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
547 err = usbd_req_clear_port_feature(
549 portno, UHF_PORT_ENABLE);
553 DPRINTFN(0, "device problem (%s), "
554 "disabling port %d\n", usbd_errstr(err), portno);
559 /*------------------------------------------------------------------------*
560 * usb_device_20_compatible
563 * 0: HUB does not support suspend and resume
564 * Else: HUB supports suspend and resume
565 *------------------------------------------------------------------------*/
567 usb_device_20_compatible(struct usb_device *udev)
571 switch (udev->speed) {
581 /*------------------------------------------------------------------------*
582 * uhub_suspend_resume_port
586 * Else: A control transaction failed
587 *------------------------------------------------------------------------*/
589 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
591 struct usb_device *child;
592 struct usb_device *udev;
596 DPRINTF("port %d\n", portno);
599 child = usb_bus_port_get_device(udev->bus,
600 udev->hub->ports + portno - 1);
602 /* first clear the port suspend change bit */
604 if (usb_device_20_compatible(udev)) {
605 err = usbd_req_clear_port_feature(udev, NULL,
606 portno, UHF_C_PORT_SUSPEND);
608 err = usbd_req_clear_port_feature(udev, NULL,
609 portno, UHF_C_PORT_LINK_STATE);
613 DPRINTF("clearing suspend failed.\n");
616 /* get fresh status */
618 err = uhub_read_port_status(sc, portno);
620 DPRINTF("reading port status failed.\n");
623 /* convert current state */
625 if (usb_device_20_compatible(udev)) {
626 if (sc->sc_st.port_status & UPS_SUSPEND) {
632 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
636 case UPS_PORT_LS_SS_INA:
637 usbd_req_warm_reset_port(udev, NULL, portno);
646 DPRINTF("suspended=%u\n", is_suspend);
648 /* do the suspend or resume */
652 * This code handle two cases: 1) Host Mode - we can only
653 * receive resume here 2) Device Mode - we can receive
654 * suspend and resume here
657 usb_dev_resume_peer(child);
658 else if (child->flags.usb_mode == USB_MODE_DEVICE)
659 usb_dev_suspend_peer(child);
665 /*------------------------------------------------------------------------*
666 * uhub_root_interrupt
668 * This function is called when a Root HUB interrupt has
669 * happened. "ptr" and "len" makes up the Root HUB interrupt
670 * packet. This function is called having the "bus_mtx" locked.
671 *------------------------------------------------------------------------*/
673 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
675 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
677 usb_needs_explore(bus, 0);
681 uhub_is_too_deep(struct usb_device *udev)
683 switch (udev->speed) {
687 if (udev->depth > USB_HUB_MAX_DEPTH)
690 case USB_SPEED_SUPER:
691 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
700 /*------------------------------------------------------------------------*
706 *------------------------------------------------------------------------*/
708 uhub_explore(struct usb_device *udev)
711 struct uhub_softc *sc;
721 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
723 /* ignore devices that are too deep */
724 if (uhub_is_too_deep(udev))
725 return (USB_ERR_TOO_DEEP);
727 /* check if device is suspended */
728 if (udev->flags.self_suspended) {
729 /* need to wait until the child signals resume */
730 DPRINTF("Device is suspended!\n");
735 * Make sure we don't race against user-space applications
738 do_unlock = usbd_enum_lock(udev);
740 for (x = 0; x != hub->nports; x++) {
744 err = uhub_read_port_status(sc, portno);
746 /* most likely the HUB is gone */
749 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
750 DPRINTF("Overcurrent on port %u.\n", portno);
751 err = usbd_req_clear_port_feature(
752 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
754 /* most likely the HUB is gone */
758 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
760 * Fake a connect status change so that the
761 * status gets checked initially!
763 sc->sc_st.port_change |=
764 UPS_C_CONNECT_STATUS;
766 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
767 err = usbd_req_clear_port_feature(
768 udev, NULL, portno, UHF_C_PORT_ENABLE);
770 /* most likely the HUB is gone */
773 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
775 * Ignore the port error if the device
778 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
779 DPRINTFN(0, "illegal enable change, "
780 "port %d\n", portno);
783 if (up->restartcnt == USB_RESTART_MAX) {
784 /* XXX could try another speed ? */
785 DPRINTFN(0, "port error, giving up "
786 "port %d\n", portno);
788 sc->sc_st.port_change |=
789 UPS_C_CONNECT_STATUS;
794 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
795 err = uhub_reattach_port(sc, portno);
797 /* most likely the HUB is gone */
801 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
802 UPS_C_PORT_LINK_STATE)) {
803 err = uhub_suspend_resume_port(sc, portno);
805 /* most likely the HUB is gone */
809 err = uhub_explore_sub(sc, up);
811 /* no device(s) present */
814 /* explore succeeded - reset restart counter */
819 usbd_enum_unlock(udev);
821 /* initial status checked */
822 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
825 return (USB_ERR_NORMAL_COMPLETION);
829 uhub_probe(device_t dev)
831 struct usb_attach_arg *uaa = device_get_ivars(dev);
833 if (uaa->usb_mode != USB_MODE_HOST)
837 * The subclass for USB HUBs is currently ignored because it
838 * is 0 for some and 1 for others.
840 if (uaa->info.bConfigIndex == 0 &&
841 uaa->info.bDeviceClass == UDCLASS_HUB)
847 /* NOTE: The information returned by this function can be wrong. */
849 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
851 struct usb_hub_descriptor hubdesc20;
852 struct usb_hub_ss_descriptor hubdesc30;
857 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
858 return (USB_ERR_INVAL);
863 switch (udev->speed) {
867 /* assuming that there is one port */
868 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
870 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
871 "error=%s\n", usbd_errstr(err));
874 nports = hubdesc20.bNbrPorts;
878 if (udev->speed == USB_SPEED_HIGH)
879 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
882 case USB_SPEED_SUPER:
883 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
885 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
886 "error=%s\n", usbd_errstr(err));
889 nports = hubdesc30.bNbrPorts;
909 uhub_attach(device_t dev)
911 struct uhub_softc *sc = device_get_softc(dev);
912 struct usb_attach_arg *uaa = device_get_ivars(dev);
913 struct usb_device *udev = uaa->device;
914 struct usb_device *parent_hub = udev->parent_hub;
916 struct usb_hub_descriptor hubdesc20;
917 struct usb_hub_ss_descriptor hubdesc30;
929 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
931 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
932 device_get_nameunit(dev));
934 device_set_usb_desc(dev);
936 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
937 "parent->selfpowered=%d\n",
939 udev->flags.self_powered,
942 parent_hub->flags.self_powered : 0);
944 if (uhub_is_too_deep(udev)) {
945 DPRINTFN(0, "HUB at depth %d, "
946 "exceeds maximum. HUB ignored\n", (int)udev->depth);
950 if (!udev->flags.self_powered && parent_hub &&
951 !parent_hub->flags.self_powered) {
952 DPRINTFN(0, "Bus powered HUB connected to "
953 "bus powered HUB. HUB ignored\n");
957 if (UHUB_IS_MULTI_TT(sc)) {
958 err = usbd_set_alt_interface_index(udev, 0, 1);
960 device_printf(dev, "MTT could not be enabled\n");
963 device_printf(dev, "MTT enabled\n");
966 /* get HUB descriptor */
968 DPRINTFN(2, "Getting HUB descriptor\n");
970 switch (udev->speed) {
974 /* assuming that there is one port */
975 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
977 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
978 "error=%s\n", usbd_errstr(err));
981 /* get number of ports */
982 nports = hubdesc20.bNbrPorts;
984 /* get power delay */
985 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
986 usb_extra_power_up_time);
988 /* get complete HUB descriptor */
990 /* check number of ports */
992 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
993 "error=%s\n", usbd_errstr(err));
996 /* get complete HUB descriptor */
997 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1000 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1001 "error=%s\n", usbd_errstr(err));
1004 if (hubdesc20.bNbrPorts != nports) {
1005 DPRINTFN(0, "Number of ports changed\n");
1010 case USB_SPEED_SUPER:
1011 if (udev->parent_hub != NULL) {
1012 err = usbd_req_set_hub_depth(udev, NULL,
1015 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1016 "error=%s\n", usbd_errstr(err));
1020 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1022 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1023 "error=%s\n", usbd_errstr(err));
1026 /* get number of ports */
1027 nports = hubdesc30.bNbrPorts;
1029 /* get power delay */
1030 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1031 usb_extra_power_up_time);
1033 /* get complete HUB descriptor */
1035 /* check number of ports */
1036 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1037 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1038 "error=%s\n", usbd_errstr(err));
1041 /* get complete HUB descriptor */
1042 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1045 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1046 "error=%s\n", usbd_errstr(err));
1049 if (hubdesc30.bNbrPorts != nports) {
1050 DPRINTFN(0, "Number of ports changed\n");
1056 DPRINTF("Assuming HUB has only one port\n");
1057 /* default number of ports */
1059 /* default power delay */
1060 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1064 DPRINTFN(0, "portless HUB\n");
1067 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1068 M_USBDEV, M_WAITOK | M_ZERO);
1075 /* initialize HUB structure */
1077 hub->explore = &uhub_explore;
1078 hub->nports = nports;
1079 hub->hubudev = udev;
1081 /* if self powered hub, give ports maximum current */
1082 if (udev->flags.self_powered) {
1083 hub->portpower = USB_MAX_POWER;
1085 hub->portpower = USB_MIN_POWER;
1088 /* set up interrupt pipe */
1090 if (udev->parent_hub == NULL) {
1091 /* root HUB is special */
1095 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1096 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1099 DPRINTFN(0, "cannot setup interrupt transfer, "
1100 "errstr=%s\n", usbd_errstr(err));
1103 /* wait with power off for a while */
1104 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1107 * To have the best chance of success we do things in the exact same
1108 * order as Windoze98. This should not be necessary, but some
1109 * devices do not follow the USB specs to the letter.
1111 * These are the events on the bus when a hub is attached:
1112 * Get device and config descriptors (see attach code)
1113 * Get hub descriptor (see above)
1116 * wait for power to become stable
1117 * (all below happens in explore code)
1119 * clear C_PORT_CONNECTION
1122 * if device connected
1126 * clear C_PORT_RESET
1128 * proceed with device attachment
1131 /* XXX should check for none, individual, or ganged power? */
1135 for (x = 0; x != nports; x++) {
1136 /* set up data structures */
1137 struct usb_port *up = hub->ports + x;
1139 up->device_index = 0;
1143 /* check if port is removable */
1144 switch (udev->speed) {
1146 case USB_SPEED_FULL:
1147 case USB_SPEED_HIGH:
1148 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1151 case USB_SPEED_SUPER:
1152 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1156 DPRINTF("Assuming removable port\n");
1161 /* turn the power on */
1162 err = usbd_req_set_port_feature(udev, NULL,
1163 portno, UHF_PORT_POWER);
1166 DPRINTFN(0, "port %d power on failed, %s\n",
1167 portno, usbd_errstr(err));
1169 DPRINTF("turn on port %d power\n",
1172 /* wait for stable power */
1173 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1176 device_printf(dev, "%d port%s with %d "
1177 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1178 removable, udev->flags.self_powered ? "self" : "bus");
1180 /* Start the interrupt endpoint, if any */
1182 if (sc->sc_xfer[0] != NULL) {
1183 mtx_lock(&sc->sc_mtx);
1184 usbd_transfer_start(sc->sc_xfer[0]);
1185 mtx_unlock(&sc->sc_mtx);
1188 /* Enable automatic power save on all USB HUBs */
1190 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1195 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1198 free(udev->hub, M_USBDEV);
1202 mtx_destroy(&sc->sc_mtx);
1208 * Called from process context when the hub is gone.
1209 * Detach all devices on active ports.
1212 uhub_detach(device_t dev)
1214 struct uhub_softc *sc = device_get_softc(dev);
1215 struct usb_hub *hub = sc->sc_udev->hub;
1216 struct usb_device *child;
1219 if (hub == NULL) /* must be partially working */
1222 /* Make sure interrupt transfer is gone. */
1223 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1225 /* Detach all ports */
1226 for (x = 0; x != hub->nports; x++) {
1228 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1230 if (child == NULL) {
1235 * Free USB device and all subdevices, if any.
1237 usb_free_device(child, 0);
1240 free(hub, M_USBDEV);
1241 sc->sc_udev->hub = NULL;
1243 mtx_destroy(&sc->sc_mtx);
1249 uhub_suspend(device_t dev)
1252 /* Sub-devices are not suspended here! */
1257 uhub_resume(device_t dev)
1260 /* Sub-devices are not resumed here! */
1265 uhub_driver_added(device_t dev, driver_t *driver)
1267 usb_needs_explore_all();
1271 struct usb_device *udev;
1273 uint8_t iface_index;
1277 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1278 struct hub_result *res)
1280 struct usb_interface *iface;
1281 struct usb_device *udev;
1286 nports = hub->nports;
1287 for (x = 0; x != nports; x++) {
1288 udev = usb_bus_port_get_device(hub->hubudev->bus,
1293 for (i = 0; i != USB_IFACE_MAX; i++) {
1294 iface = usbd_get_iface(udev, i);
1296 (iface->subdev == child)) {
1297 res->iface_index = i;
1299 res->portno = x + 1;
1304 res->iface_index = 0;
1310 uhub_child_location_string(device_t parent, device_t child,
1311 char *buf, size_t buflen)
1313 struct uhub_softc *sc;
1314 struct usb_hub *hub;
1315 struct hub_result res;
1317 if (!device_is_attached(parent)) {
1323 sc = device_get_softc(parent);
1324 hub = sc->sc_udev->hub;
1327 uhub_find_iface_index(hub, child, &res);
1329 DPRINTF("device not on hub\n");
1335 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1336 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1337 res.portno, device_get_unit(res.udev->bus->bdev),
1338 res.udev->device_index, res.iface_index);
1346 uhub_child_pnpinfo_string(device_t parent, device_t child,
1347 char *buf, size_t buflen)
1349 struct uhub_softc *sc;
1350 struct usb_hub *hub;
1351 struct usb_interface *iface;
1352 struct hub_result res;
1354 if (!device_is_attached(parent)) {
1360 sc = device_get_softc(parent);
1361 hub = sc->sc_udev->hub;
1364 uhub_find_iface_index(hub, child, &res);
1366 DPRINTF("device not on hub\n");
1372 iface = usbd_get_iface(res.udev, res.iface_index);
1373 if (iface && iface->idesc) {
1374 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1375 "devclass=0x%02x devsubclass=0x%02x "
1379 "intclass=0x%02x intsubclass=0x%02x "
1380 "intprotocol=0x%02x " "%s%s",
1381 UGETW(res.udev->ddesc.idVendor),
1382 UGETW(res.udev->ddesc.idProduct),
1383 res.udev->ddesc.bDeviceClass,
1384 res.udev->ddesc.bDeviceSubClass,
1385 usb_get_serial(res.udev),
1386 UGETW(res.udev->ddesc.bcdDevice),
1387 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1388 iface->idesc->bInterfaceClass,
1389 iface->idesc->bInterfaceSubClass,
1390 iface->idesc->bInterfaceProtocol,
1391 iface->pnpinfo ? " " : "",
1392 iface->pnpinfo ? iface->pnpinfo : "");
1406 * The USB Transaction Translator:
1407 * ===============================
1409 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1410 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1411 * USB transfers. To utilize bandwidth dynamically the "scatter and
1412 * gather" principle must be applied. This means that bandwidth must
1413 * be divided into equal parts of bandwidth. With regard to USB all
1414 * data is transferred in smaller packets with length
1415 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1418 * The bandwidth scheduler which I have implemented will simply pack
1419 * the USB transfers back to back until there is no more space in the
1420 * schedule. Out of the 8 microframes which the USB 2.0 standard
1421 * provides, only 6 are available for non-HIGH-speed devices. I have
1422 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1423 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1424 * this division, it is very difficult to allocate and free bandwidth
1427 * NOTE about the Transaction Translator in USB HUBs:
1429 * USB HUBs have a very simple Transaction Translator, that will
1430 * simply pipeline all the SPLIT transactions. That means that the
1431 * transactions will be executed in the order they are queued!
1435 /*------------------------------------------------------------------------*
1436 * usb_intr_find_best_slot
1439 * The best Transaction Translation slot for an interrupt endpoint.
1440 *------------------------------------------------------------------------*/
1442 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1443 uint8_t end, uint8_t mask)
1445 usb_size_t min = (usb_size_t)-1;
1453 /* find the last slot with lesser used bandwidth */
1455 for (x = start; x < end; x++) {
1459 /* compute sum of bandwidth */
1460 for (z = x; z < end; z++) {
1461 if (mask & (1U << (z - x)))
1465 /* check if the current multi-slot is more optimal */
1471 /* check if the mask is about to be shifted out */
1472 if (mask & (1U << (end - 1 - x)))
1478 /*------------------------------------------------------------------------*
1479 * usb_hs_bandwidth_adjust
1481 * This function will update the bandwith usage for the microframe
1482 * having index "slot" by "len" bytes. "len" can be negative. If the
1483 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1484 * the "slot" argument will be replaced by the slot having least used
1485 * bandwidth. The "mask" argument is used for multi-slot allocations.
1488 * The slot in which the bandwidth update was done: 0..7
1489 *------------------------------------------------------------------------*/
1491 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1492 uint8_t slot, uint8_t mask)
1494 struct usb_bus *bus = udev->bus;
1495 struct usb_hub *hub;
1496 enum usb_dev_speed speed;
1499 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1501 speed = usbd_get_speed(udev);
1505 case USB_SPEED_FULL:
1506 if (speed == USB_SPEED_LOW) {
1510 * The Host Controller Driver should have
1511 * performed checks so that the lookup
1512 * below does not result in a NULL pointer
1516 hub = udev->parent_hs_hub->hub;
1517 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1518 slot = usb_intr_find_best_slot(hub->uframe_usage,
1519 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1521 for (x = slot; x < 8; x++) {
1522 if (mask & (1U << (x - slot))) {
1523 hub->uframe_usage[x] += len;
1524 bus->uframe_usage[x] += len;
1529 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1530 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1531 USB_HS_MICRO_FRAMES_MAX, mask);
1533 for (x = slot; x < 8; x++) {
1534 if (mask & (1U << (x - slot))) {
1535 bus->uframe_usage[x] += len;
1543 /*------------------------------------------------------------------------*
1544 * usb_hs_bandwidth_alloc
1546 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1547 *------------------------------------------------------------------------*/
1549 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1551 struct usb_device *udev;
1556 udev = xfer->xroot->udev;
1558 if (udev->flags.usb_mode != USB_MODE_HOST)
1559 return; /* not supported */
1561 xfer->endpoint->refcount_bw++;
1562 if (xfer->endpoint->refcount_bw != 1)
1563 return; /* already allocated */
1565 speed = usbd_get_speed(udev);
1567 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1569 /* allocate a microframe slot */
1572 slot = usb_hs_bandwidth_adjust(udev,
1573 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1575 xfer->endpoint->usb_uframe = slot;
1576 xfer->endpoint->usb_smask = mask << slot;
1578 if ((speed != USB_SPEED_FULL) &&
1579 (speed != USB_SPEED_LOW)) {
1580 xfer->endpoint->usb_cmask = 0x00 ;
1582 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1586 case UE_ISOCHRONOUS:
1587 switch (usbd_xfer_get_fps_shift(xfer)) {
1602 /* allocate a microframe multi-slot */
1604 slot = usb_hs_bandwidth_adjust(udev,
1605 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1607 xfer->endpoint->usb_uframe = slot;
1608 xfer->endpoint->usb_cmask = 0;
1609 xfer->endpoint->usb_smask = mask << slot;
1613 xfer->endpoint->usb_uframe = 0;
1614 xfer->endpoint->usb_cmask = 0;
1615 xfer->endpoint->usb_smask = 0;
1619 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1620 xfer->endpoint->usb_uframe,
1621 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1624 /*------------------------------------------------------------------------*
1625 * usb_hs_bandwidth_free
1627 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1628 *------------------------------------------------------------------------*/
1630 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1632 struct usb_device *udev;
1636 udev = xfer->xroot->udev;
1638 if (udev->flags.usb_mode != USB_MODE_HOST)
1639 return; /* not supported */
1641 xfer->endpoint->refcount_bw--;
1642 if (xfer->endpoint->refcount_bw != 0)
1643 return; /* still allocated */
1645 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1647 case UE_ISOCHRONOUS:
1649 slot = xfer->endpoint->usb_uframe;
1650 mask = xfer->endpoint->usb_smask;
1652 /* free microframe slot(s): */
1653 usb_hs_bandwidth_adjust(udev,
1654 -xfer->max_frame_size, slot, mask >> slot);
1656 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1657 slot, mask >> slot);
1659 xfer->endpoint->usb_uframe = 0;
1660 xfer->endpoint->usb_cmask = 0;
1661 xfer->endpoint->usb_smask = 0;
1669 /*------------------------------------------------------------------------*
1670 * usb_isoc_time_expand
1672 * This function will expand the time counter from 7-bit to 16-bit.
1675 * 16-bit isochronous time counter.
1676 *------------------------------------------------------------------------*/
1678 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1682 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1684 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1686 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1688 if (isoc_time_curr < rem) {
1689 /* the time counter wrapped around */
1690 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1692 /* update the remainder */
1694 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1695 bus->isoc_time_last |= isoc_time_curr;
1697 return (bus->isoc_time_last);
1700 /*------------------------------------------------------------------------*
1701 * usbd_fs_isoc_schedule_alloc_slot
1703 * This function will allocate bandwidth for an isochronous FULL speed
1704 * transaction in the FULL speed schedule.
1709 *------------------------------------------------------------------------*/
1710 #if USB_HAVE_TT_SUPPORT
1712 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1714 struct usb_xfer *xfer;
1715 struct usb_xfer *pipe_xfer;
1716 struct usb_bus *bus;
1718 usb_frlength_t data_len;
1726 bus = isoc_xfer->xroot->bus;
1728 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1730 /* skip self, if any */
1732 if (xfer == isoc_xfer)
1735 /* check if this USB transfer is going through the same TT */
1737 if (xfer->xroot->udev->parent_hs_hub !=
1738 isoc_xfer->xroot->udev->parent_hs_hub) {
1741 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1742 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1743 (xfer->xroot->udev->hs_port_no !=
1744 isoc_xfer->xroot->udev->hs_port_no)) {
1747 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1750 /* check if isoc_time is part of this transfer */
1752 delta = xfer->isoc_time_complete - isoc_time;
1753 if (delta > 0 && delta <= xfer->nframes) {
1754 delta = xfer->nframes - delta;
1756 len = xfer->frlengths[delta];
1764 /* check double buffered transfers */
1766 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
1769 /* skip self, if any */
1771 if (pipe_xfer == isoc_xfer)
1774 /* check if isoc_time is part of this transfer */
1776 delta = pipe_xfer->isoc_time_complete - isoc_time;
1777 if (delta > 0 && delta <= pipe_xfer->nframes) {
1778 delta = pipe_xfer->nframes - delta;
1780 len = pipe_xfer->frlengths[delta];
1790 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1791 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1795 /* check for overflow */
1797 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1802 delta = isoc_xfer->isoc_time_complete - isoc_time;
1803 if (delta > 0 && delta <= isoc_xfer->nframes) {
1804 delta = isoc_xfer->nframes - delta;
1806 len = isoc_xfer->frlengths[delta];
1814 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1815 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1819 /* check for overflow */
1821 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1828 /*------------------------------------------------------------------------*
1829 * usb_bus_port_get_device
1831 * This function is NULL safe.
1832 *------------------------------------------------------------------------*/
1834 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1836 if ((bus == NULL) || (up == NULL)) {
1840 if (up->device_index == 0) {
1844 return (bus->devices[up->device_index]);
1847 /*------------------------------------------------------------------------*
1848 * usb_bus_port_set_device
1850 * This function is NULL safe.
1851 *------------------------------------------------------------------------*/
1853 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1854 struct usb_device *udev, uint8_t device_index)
1861 * There is only one case where we don't
1862 * have an USB port, and that is the Root Hub!
1866 up->device_index = device_index;
1868 device_index = up->device_index;
1869 up->device_index = 0;
1873 * Make relationships to our new device
1875 if (device_index != 0) {
1877 mtx_lock(&usb_ref_lock);
1879 bus->devices[device_index] = udev;
1881 mtx_unlock(&usb_ref_lock);
1887 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1890 /*------------------------------------------------------------------------*
1893 * This functions is called when the USB event thread needs to run.
1894 *------------------------------------------------------------------------*/
1896 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1903 DPRINTF("No bus pointer!\n");
1906 if ((bus->devices == NULL) ||
1907 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1908 DPRINTF("No root HUB\n");
1911 if (mtx_owned(&bus->bus_mtx)) {
1920 if (usb_proc_msignal(&bus->explore_proc,
1921 &bus->explore_msg[0], &bus->explore_msg[1])) {
1925 USB_BUS_UNLOCK(bus);
1929 /*------------------------------------------------------------------------*
1930 * usb_needs_explore_all
1932 * This function is called whenever a new driver is loaded and will
1933 * cause that all USB busses are re-explored.
1934 *------------------------------------------------------------------------*/
1936 usb_needs_explore_all(void)
1938 struct usb_bus *bus;
1945 dc = usb_devclass_ptr;
1947 DPRINTFN(0, "no devclass\n");
1951 * Explore all USB busses in parallell.
1953 max = devclass_get_maxunit(dc);
1955 dev = devclass_get_device(dc, max);
1957 bus = device_get_softc(dev);
1959 usb_needs_explore(bus, 1);
1966 /*------------------------------------------------------------------------*
1967 * usb_bus_power_update
1969 * This function will ensure that all USB devices on the given bus are
1970 * properly suspended or resumed according to the device transfer
1972 *------------------------------------------------------------------------*/
1975 usb_bus_power_update(struct usb_bus *bus)
1977 usb_needs_explore(bus, 0 /* no probe */ );
1981 /*------------------------------------------------------------------------*
1982 * usbd_transfer_power_ref
1984 * This function will modify the power save reference counts and
1985 * wakeup the USB device associated with the given USB transfer, if
1987 *------------------------------------------------------------------------*/
1990 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1992 static const usb_power_mask_t power_mask[4] = {
1993 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1994 [UE_BULK] = USB_HW_POWER_BULK,
1995 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1996 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1998 struct usb_device *udev;
1999 uint8_t needs_explore;
2000 uint8_t needs_hw_power;
2003 udev = xfer->xroot->udev;
2005 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2006 /* no power save for root HUB */
2009 USB_BUS_LOCK(udev->bus);
2011 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2013 udev->pwr_save.last_xfer_time = ticks;
2014 udev->pwr_save.type_refs[xfer_type] += val;
2016 if (xfer->flags_int.control_xfr) {
2017 udev->pwr_save.read_refs += val;
2018 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2020 * It is not allowed to suspend during a
2023 udev->pwr_save.write_refs += val;
2025 } else if (USB_GET_DATA_ISREAD(xfer)) {
2026 udev->pwr_save.read_refs += val;
2028 udev->pwr_save.write_refs += val;
2032 if (udev->flags.self_suspended)
2033 needs_explore = usb_peer_should_wakeup(udev);
2037 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2038 DPRINTF("Adding type %u to power state\n", xfer_type);
2039 udev->bus->hw_power_state |= power_mask[xfer_type];
2049 USB_BUS_UNLOCK(udev->bus);
2051 if (needs_explore) {
2052 DPRINTF("update\n");
2053 usb_bus_power_update(udev->bus);
2054 } else if (needs_hw_power) {
2055 DPRINTF("needs power\n");
2056 if (udev->bus->methods->set_hw_power != NULL) {
2057 (udev->bus->methods->set_hw_power) (udev->bus);
2063 /*------------------------------------------------------------------------*
2064 * usb_peer_should_wakeup
2066 * This function returns non-zero if the current device should wake up.
2067 *------------------------------------------------------------------------*/
2069 usb_peer_should_wakeup(struct usb_device *udev)
2071 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2072 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2073 (udev->re_enumerate_wait != 0) ||
2074 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2075 (udev->pwr_save.write_refs != 0) ||
2076 ((udev->pwr_save.read_refs != 0) &&
2077 (udev->flags.usb_mode == USB_MODE_HOST) &&
2078 (usb_peer_can_wakeup(udev) == 0)));
2081 /*------------------------------------------------------------------------*
2084 * This function implements the USB power daemon and is called
2085 * regularly from the USB explore thread.
2086 *------------------------------------------------------------------------*/
2089 usb_bus_powerd(struct usb_bus *bus)
2091 struct usb_device *udev;
2094 usb_ticks_t mintime;
2095 usb_size_t type_refs[5];
2098 limit = usb_power_timeout;
2101 else if (limit > 255)
2106 DPRINTF("bus=%p\n", bus);
2111 * The root HUB device is never suspended
2112 * and we simply skip it.
2114 for (x = USB_ROOT_HUB_ADDR + 1;
2115 x != bus->devices_max; x++) {
2117 udev = bus->devices[x];
2121 temp = ticks - udev->pwr_save.last_xfer_time;
2123 if (usb_peer_should_wakeup(udev)) {
2124 /* check if we are suspended */
2125 if (udev->flags.self_suspended != 0) {
2126 USB_BUS_UNLOCK(bus);
2127 usb_dev_resume_peer(udev);
2130 } else if ((temp >= limit) &&
2131 (udev->flags.usb_mode == USB_MODE_HOST) &&
2132 (udev->flags.self_suspended == 0)) {
2133 /* try to do suspend */
2135 USB_BUS_UNLOCK(bus);
2136 usb_dev_suspend_peer(udev);
2141 /* reset counters */
2143 mintime = (usb_ticks_t)-1;
2150 /* Re-loop all the devices to get the actual state */
2152 for (x = USB_ROOT_HUB_ADDR + 1;
2153 x != bus->devices_max; x++) {
2155 udev = bus->devices[x];
2159 /* we found a non-Root-Hub USB device */
2162 /* "last_xfer_time" can be updated by a resume */
2163 temp = ticks - udev->pwr_save.last_xfer_time;
2166 * Compute minimum time since last transfer for the complete
2172 if (udev->flags.self_suspended == 0) {
2173 type_refs[0] += udev->pwr_save.type_refs[0];
2174 type_refs[1] += udev->pwr_save.type_refs[1];
2175 type_refs[2] += udev->pwr_save.type_refs[2];
2176 type_refs[3] += udev->pwr_save.type_refs[3];
2180 if (mintime >= (usb_ticks_t)(1 * hz)) {
2181 /* recompute power masks */
2182 DPRINTF("Recomputing power masks\n");
2183 bus->hw_power_state = 0;
2184 if (type_refs[UE_CONTROL] != 0)
2185 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2186 if (type_refs[UE_BULK] != 0)
2187 bus->hw_power_state |= USB_HW_POWER_BULK;
2188 if (type_refs[UE_INTERRUPT] != 0)
2189 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2190 if (type_refs[UE_ISOCHRONOUS] != 0)
2191 bus->hw_power_state |= USB_HW_POWER_ISOC;
2192 if (type_refs[4] != 0)
2193 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2195 USB_BUS_UNLOCK(bus);
2197 if (bus->methods->set_hw_power != NULL) {
2198 /* always update hardware power! */
2199 (bus->methods->set_hw_power) (bus);
2205 /*------------------------------------------------------------------------*
2206 * usb_dev_resume_peer
2208 * This function will resume an USB peer and do the required USB
2209 * signalling to get an USB device out of the suspended state.
2210 *------------------------------------------------------------------------*/
2212 usb_dev_resume_peer(struct usb_device *udev)
2214 struct usb_bus *bus;
2221 /* check if already resumed */
2222 if (udev->flags.self_suspended == 0)
2225 /* we need a parent HUB to do resume */
2226 if (udev->parent_hub == NULL)
2229 DPRINTF("udev=%p\n", udev);
2231 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2232 (udev->flags.remote_wakeup == 0)) {
2234 * If the host did not set the remote wakeup feature, we can
2235 * not wake it up either!
2237 DPRINTF("remote wakeup is not set!\n");
2240 /* get bus pointer */
2243 /* resume parent hub first */
2244 usb_dev_resume_peer(udev->parent_hub);
2246 /* reduce chance of instant resume failure by waiting a little bit */
2247 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2249 if (usb_device_20_compatible(udev)) {
2250 /* resume current port (Valid in Host and Device Mode) */
2251 err = usbd_req_clear_port_feature(udev->parent_hub,
2252 NULL, udev->port_no, UHF_PORT_SUSPEND);
2254 DPRINTFN(0, "Resuming port failed\n");
2258 /* resume current port (Valid in Host and Device Mode) */
2259 err = usbd_req_set_port_link_state(udev->parent_hub,
2260 NULL, udev->port_no, UPS_PORT_LS_U0);
2262 DPRINTFN(0, "Resuming port failed\n");
2267 /* resume settle time */
2268 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2270 if (bus->methods->device_resume != NULL) {
2271 /* resume USB device on the USB controller */
2272 (bus->methods->device_resume) (udev);
2275 /* set that this device is now resumed */
2276 udev->flags.self_suspended = 0;
2278 /* make sure that we don't go into suspend right away */
2279 udev->pwr_save.last_xfer_time = ticks;
2281 /* make sure the needed power masks are on */
2282 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2283 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2284 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2285 bus->hw_power_state |= USB_HW_POWER_BULK;
2286 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2287 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2288 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2289 bus->hw_power_state |= USB_HW_POWER_ISOC;
2291 USB_BUS_UNLOCK(bus);
2293 if (bus->methods->set_hw_power != NULL) {
2294 /* always update hardware power! */
2295 (bus->methods->set_hw_power) (bus);
2300 /* notify all sub-devices about resume */
2301 err = usb_suspend_resume(udev, 0);
2303 usbd_sr_unlock(udev);
2305 /* check if peer has wakeup capability */
2306 if (usb_peer_can_wakeup(udev)) {
2307 /* clear remote wakeup */
2308 err = usbd_req_clear_device_feature(udev,
2309 NULL, UF_DEVICE_REMOTE_WAKEUP);
2311 DPRINTFN(0, "Clearing device "
2312 "remote wakeup failed: %s\n",
2318 /*------------------------------------------------------------------------*
2319 * usb_dev_suspend_peer
2321 * This function will suspend an USB peer and do the required USB
2322 * signalling to get an USB device into the suspended state.
2323 *------------------------------------------------------------------------*/
2325 usb_dev_suspend_peer(struct usb_device *udev)
2327 struct usb_device *child;
2337 /* check if already suspended */
2338 if (udev->flags.self_suspended)
2341 /* we need a parent HUB to do suspend */
2342 if (udev->parent_hub == NULL)
2345 DPRINTF("udev=%p\n", udev);
2347 /* check if the current device is a HUB */
2348 if (udev->hub != NULL) {
2349 nports = udev->hub->nports;
2351 /* check if all devices on the HUB are suspended */
2352 for (x = 0; x != nports; x++) {
2353 child = usb_bus_port_get_device(udev->bus,
2354 udev->hub->ports + x);
2359 if (child->flags.self_suspended)
2362 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2367 if (usb_peer_can_wakeup(udev)) {
2369 * This request needs to be done before we set
2370 * "udev->flags.self_suspended":
2373 /* allow device to do remote wakeup */
2374 err = usbd_req_set_device_feature(udev,
2375 NULL, UF_DEVICE_REMOTE_WAKEUP);
2377 DPRINTFN(0, "Setting device "
2378 "remote wakeup failed\n");
2382 USB_BUS_LOCK(udev->bus);
2384 * Checking for suspend condition and setting suspended bit
2387 err = usb_peer_should_wakeup(udev);
2390 * Set that this device is suspended. This variable
2391 * must be set before calling USB controller suspend
2394 udev->flags.self_suspended = 1;
2396 USB_BUS_UNLOCK(udev->bus);
2399 if (usb_peer_can_wakeup(udev)) {
2400 /* allow device to do remote wakeup */
2401 err = usbd_req_clear_device_feature(udev,
2402 NULL, UF_DEVICE_REMOTE_WAKEUP);
2404 DPRINTFN(0, "Setting device "
2405 "remote wakeup failed\n");
2409 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2410 /* resume parent HUB first */
2411 usb_dev_resume_peer(udev->parent_hub);
2413 /* reduce chance of instant resume failure by waiting a little bit */
2414 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2416 /* resume current port (Valid in Host and Device Mode) */
2417 err = usbd_req_clear_port_feature(udev->parent_hub,
2418 NULL, udev->port_no, UHF_PORT_SUSPEND);
2420 /* resume settle time */
2421 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2423 DPRINTF("Suspend was cancelled!\n");
2429 /* notify all sub-devices about suspend */
2430 err = usb_suspend_resume(udev, 1);
2432 usbd_sr_unlock(udev);
2434 if (udev->bus->methods->device_suspend != NULL) {
2437 /* suspend device on the USB controller */
2438 (udev->bus->methods->device_suspend) (udev);
2441 temp = usbd_get_dma_delay(udev);
2443 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2447 if (usb_device_20_compatible(udev)) {
2448 /* suspend current port */
2449 err = usbd_req_set_port_feature(udev->parent_hub,
2450 NULL, udev->port_no, UHF_PORT_SUSPEND);
2452 DPRINTFN(0, "Suspending port failed\n");
2456 /* suspend current port */
2457 err = usbd_req_set_port_link_state(udev->parent_hub,
2458 NULL, udev->port_no, UPS_PORT_LS_U3);
2460 DPRINTFN(0, "Suspending port failed\n");
2465 udev = udev->parent_hub;
2469 /*------------------------------------------------------------------------*
2470 * usbd_set_power_mode
2472 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2474 *------------------------------------------------------------------------*/
2476 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2478 /* filter input argument */
2479 if ((power_mode != USB_POWER_MODE_ON) &&
2480 (power_mode != USB_POWER_MODE_OFF))
2481 power_mode = USB_POWER_MODE_SAVE;
2483 power_mode = usbd_filter_power_mode(udev, power_mode);
2485 udev->power_mode = power_mode; /* update copy of power mode */
2488 usb_bus_power_update(udev->bus);
2492 /*------------------------------------------------------------------------*
2493 * usbd_filter_power_mode
2495 * This function filters the power mode based on hardware requirements.
2496 *------------------------------------------------------------------------*/
2498 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2500 struct usb_bus_methods *mtod;
2503 mtod = udev->bus->methods;
2506 if (mtod->get_power_mode != NULL)
2507 (mtod->get_power_mode) (udev, &temp);
2509 /* check if we should not filter */
2511 return (power_mode);
2513 /* use fixed power mode given by hardware driver */
2517 /*------------------------------------------------------------------------*
2518 * usbd_start_re_enumerate
2520 * This function starts re-enumeration of the given USB device. This
2521 * function does not need to be called BUS-locked. This function does
2522 * not wait until the re-enumeration is completed.
2523 *------------------------------------------------------------------------*/
2525 usbd_start_re_enumerate(struct usb_device *udev)
2527 if (udev->re_enumerate_wait == 0) {
2528 udev->re_enumerate_wait = 1;
2529 usb_needs_explore(udev->bus, 0);