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/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usb_ioctl.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usbdi_util.h>
55 #define USB_DEBUG_VAR uhub_debug
57 #include <bus/u4b/usb_core.h>
58 #include <bus/u4b/usb_process.h>
59 #include <bus/u4b/usb_device.h>
60 #include <bus/u4b/usb_request.h>
61 #include <bus/u4b/usb_debug.h>
62 #include <bus/u4b/usb_hub.h>
63 #include <bus/u4b/usb_util.h>
64 #include <bus/u4b/usb_busdma.h>
65 #include <bus/u4b/usb_transfer.h>
66 #include <bus/u4b/usb_dynamic.h>
68 #include <bus/u4b/usb_controller.h>
69 #include <bus/u4b/usb_bus.h>
71 #define UHUB_INTR_INTERVAL 250 /* ms */
72 #define UHUB_N_TRANSFER 1
75 static int uhub_debug = 0;
77 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
78 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
81 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
85 static int usb_power_timeout = 30; /* seconds */
87 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
88 &usb_power_timeout, 0, "USB power timeout");
91 struct uhub_current_state {
97 struct uhub_current_state sc_st;/* current state */
98 #if (USB_HAVE_FIXED_PORT != 0)
99 struct usb_hub sc_hub;
101 device_t sc_dev; /* base device */
102 struct lock sc_lock; /* our mutex */
103 struct usb_device *sc_udev; /* USB device */
104 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
106 #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) {
246 do_unlock = usbd_enum_lock(child);
247 switch (child->re_enumerate_wait) {
248 case USB_RE_ENUM_START:
249 err = usbd_set_config_index(child,
252 DPRINTF("Unconfigure failed: "
256 err = usbd_req_re_enumerate(child, NULL);
258 err = usbd_set_config_index(child, 0);
260 err = usb_probe_and_attach(child,
261 USB_IFACE_INDEX_ANY);
263 child->re_enumerate_wait = USB_RE_ENUM_DONE;
267 case USB_RE_ENUM_PWR_OFF:
268 /* get the device unconfigured */
269 err = usbd_set_config_index(child,
272 DPRINTFN(0, "Could not unconfigure "
273 "device (ignored)\n");
276 /* clear port enable */
277 err = usbd_req_clear_port_feature(child->parent_hub,
278 NULL, child->port_no, UHF_PORT_ENABLE);
280 DPRINTFN(0, "Could not disable port "
283 child->re_enumerate_wait = USB_RE_ENUM_DONE;
288 child->re_enumerate_wait = USB_RE_ENUM_DONE;
292 usbd_enum_unlock(child);
295 /* check if probe and attach should be done */
297 if (child->driver_added_refcount != refcount) {
298 child->driver_added_refcount = refcount;
299 err = usb_probe_and_attach(child,
300 USB_IFACE_INDEX_ANY);
305 /* start control transfer, if device mode */
307 if (child->flags.usb_mode == USB_MODE_DEVICE)
308 usbd_ctrl_transfer_setup(child);
310 /* if a HUB becomes present, do a recursive HUB explore */
313 err = (child->hub->explore) (child);
319 /*------------------------------------------------------------------------*
320 * uhub_read_port_status - factored out code
321 *------------------------------------------------------------------------*/
323 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
325 struct usb_port_status ps;
328 err = usbd_req_get_port_status(
329 sc->sc_udev, NULL, &ps, portno);
331 /* update status regardless of error */
333 sc->sc_st.port_status = UGETW(ps.wPortStatus);
334 sc->sc_st.port_change = UGETW(ps.wPortChange);
336 /* debugging print */
338 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
339 "wPortChange=0x%04x, err=%s\n",
340 portno, sc->sc_st.port_status,
341 sc->sc_st.port_change, usbd_errstr(err));
345 /*------------------------------------------------------------------------*
350 * Else: A control transaction failed
351 *------------------------------------------------------------------------*/
353 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
355 struct usb_device *child;
356 struct usb_device *udev;
357 enum usb_dev_speed speed;
358 enum usb_hc_mode mode;
363 DPRINTF("reattaching port %d\n", portno);
367 child = usb_bus_port_get_device(udev->bus,
368 udev->hub->ports + portno - 1);
372 /* first clear the port connection change bit */
374 err = usbd_req_clear_port_feature(udev, NULL,
375 portno, UHF_C_PORT_CONNECTION);
380 /* check if there is a child */
384 * Free USB device and all subdevices, if any.
386 usb_free_device(child, 0);
389 /* get fresh status */
391 err = uhub_read_port_status(sc, portno);
395 /* check if nothing is connected to the port */
397 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
400 /* check if there is no power on the port and print a warning */
402 switch (udev->speed) {
406 power_mask = UPS_PORT_POWER;
408 case USB_SPEED_SUPER:
409 if (udev->parent_hub == NULL)
410 power_mask = UPS_PORT_POWER;
412 power_mask = UPS_PORT_POWER_SS;
418 if (!(sc->sc_st.port_status & power_mask)) {
419 DPRINTF("WARNING: strange, connected port %d "
420 "has no power\n", portno);
423 /* check if the device is in Host Mode */
425 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
427 DPRINTF("Port %d is in Host Mode\n", portno);
429 if (sc->sc_st.port_status & UPS_SUSPEND) {
431 * NOTE: Should not get here in SuperSpeed
432 * mode, because the HUB should report this
435 DPRINTF("Port %d was still "
436 "suspended, clearing.\n", portno);
437 err = usbd_req_clear_port_feature(udev,
438 NULL, portno, UHF_PORT_SUSPEND);
443 /* wait for maximum device power up time */
446 USB_MS_TO_TICKS(usb_port_powerup_delay));
448 /* reset port, which implies enabling it */
450 err = usbd_req_reset_port(udev, NULL, portno);
453 DPRINTFN(0, "port %d reset "
454 "failed, error=%s\n",
455 portno, usbd_errstr(err));
458 /* get port status again, it might have changed during reset */
460 err = uhub_read_port_status(sc, portno);
464 /* check if something changed during port reset */
466 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
467 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
469 DPRINTFN(0, "giving up port reset "
470 "- device vanished\n");
477 DPRINTF("Port %d is in Device Mode\n", portno);
481 * Figure out the device speed
483 switch (udev->speed) {
485 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
486 speed = USB_SPEED_HIGH;
487 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
488 speed = USB_SPEED_LOW;
490 speed = USB_SPEED_FULL;
493 if (sc->sc_st.port_status & UPS_LOW_SPEED)
494 speed = USB_SPEED_LOW;
496 speed = USB_SPEED_FULL;
499 speed = USB_SPEED_LOW;
501 case USB_SPEED_SUPER:
502 if (udev->parent_hub == NULL) {
503 /* Root HUB - special case */
504 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
506 speed = USB_SPEED_FULL;
509 speed = USB_SPEED_LOW;
512 speed = USB_SPEED_HIGH;
515 speed = USB_SPEED_SUPER;
519 speed = USB_SPEED_SUPER;
523 /* same speed like parent */
527 if (speed == USB_SPEED_SUPER) {
528 err = usbd_req_set_hub_u1_timeout(udev, NULL,
529 portno, 128 - (2 * udev->depth));
531 DPRINTFN(0, "port %d U1 timeout "
532 "failed, error=%s\n",
533 portno, usbd_errstr(err));
535 err = usbd_req_set_hub_u2_timeout(udev, NULL,
536 portno, 128 - (2 * udev->depth));
538 DPRINTFN(0, "port %d U2 timeout "
539 "failed, error=%s\n",
540 portno, usbd_errstr(err));
545 * Figure out the device mode
547 * NOTE: This part is currently FreeBSD specific.
549 if (udev->parent_hub != NULL) {
550 /* inherit mode from the parent HUB */
551 mode = udev->parent_hub->flags.usb_mode;
552 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
553 mode = USB_MODE_DEVICE;
555 mode = USB_MODE_HOST;
557 /* need to create a new child */
558 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
559 udev->depth + 1, portno - 1, portno, speed, mode);
561 DPRINTFN(0, "could not allocate new device\n");
564 return (0); /* success */
569 * Free USB device and all subdevices, if any.
571 usb_free_device(child, 0);
575 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
576 err = usbd_req_clear_port_feature(
578 portno, UHF_PORT_ENABLE);
582 DPRINTFN(0, "device problem (%s), "
583 "disabling port %d\n", usbd_errstr(err), portno);
588 /*------------------------------------------------------------------------*
589 * usb_device_20_compatible
592 * 0: HUB does not support suspend and resume
593 * Else: HUB supports suspend and resume
594 *------------------------------------------------------------------------*/
596 usb_device_20_compatible(struct usb_device *udev)
600 switch (udev->speed) {
610 /*------------------------------------------------------------------------*
611 * uhub_suspend_resume_port
615 * Else: A control transaction failed
616 *------------------------------------------------------------------------*/
618 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
620 struct usb_device *child;
621 struct usb_device *udev;
625 DPRINTF("port %d\n", portno);
628 child = usb_bus_port_get_device(udev->bus,
629 udev->hub->ports + portno - 1);
631 /* first clear the port suspend change bit */
633 if (usb_device_20_compatible(udev)) {
634 err = usbd_req_clear_port_feature(udev, NULL,
635 portno, UHF_C_PORT_SUSPEND);
637 err = usbd_req_clear_port_feature(udev, NULL,
638 portno, UHF_C_PORT_LINK_STATE);
642 DPRINTF("clearing suspend failed.\n");
645 /* get fresh status */
647 err = uhub_read_port_status(sc, portno);
649 DPRINTF("reading port status failed.\n");
652 /* convert current state */
654 if (usb_device_20_compatible(udev)) {
655 if (sc->sc_st.port_status & UPS_SUSPEND) {
661 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
665 case UPS_PORT_LS_SS_INA:
666 usbd_req_warm_reset_port(udev, NULL, portno);
675 DPRINTF("suspended=%u\n", is_suspend);
677 /* do the suspend or resume */
681 * This code handle two cases: 1) Host Mode - we can only
682 * receive resume here 2) Device Mode - we can receive
683 * suspend and resume here
686 usb_dev_resume_peer(child);
687 else if (child->flags.usb_mode == USB_MODE_DEVICE)
688 usb_dev_suspend_peer(child);
694 /*------------------------------------------------------------------------*
695 * uhub_root_interrupt
697 * This function is called when a Root HUB interrupt has
698 * happened. "ptr" and "len" makes up the Root HUB interrupt
699 * packet. This function is called having the "bus_mtx" locked.
700 *------------------------------------------------------------------------*/
702 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
704 USB_BUS_LOCK_ASSERT(bus);
706 usb_needs_explore(bus, 0);
710 uhub_is_too_deep(struct usb_device *udev)
712 switch (udev->speed) {
716 if (udev->depth > USB_HUB_MAX_DEPTH)
719 case USB_SPEED_SUPER:
720 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
729 /*------------------------------------------------------------------------*
735 *------------------------------------------------------------------------*/
737 uhub_explore(struct usb_device *udev)
740 struct uhub_softc *sc;
750 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
752 /* ignore devices that are too deep */
753 if (uhub_is_too_deep(udev))
754 return (USB_ERR_TOO_DEEP);
756 /* check if device is suspended */
757 if (udev->flags.self_suspended) {
758 /* need to wait until the child signals resume */
759 DPRINTF("Device is suspended!\n");
764 * Make sure we don't race against user-space applications
767 do_unlock = usbd_enum_lock(udev);
769 for (x = 0; x != hub->nports; x++) {
773 err = uhub_read_port_status(sc, portno);
775 /* most likely the HUB is gone */
778 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
779 DPRINTF("Overcurrent on port %u.\n", portno);
780 err = usbd_req_clear_port_feature(
781 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
783 /* most likely the HUB is gone */
787 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
789 * Fake a connect status change so that the
790 * status gets checked initially!
792 sc->sc_st.port_change |=
793 UPS_C_CONNECT_STATUS;
795 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
796 err = usbd_req_clear_port_feature(
797 udev, NULL, portno, UHF_C_PORT_ENABLE);
799 /* most likely the HUB is gone */
802 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
804 * Ignore the port error if the device
807 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
808 DPRINTFN(0, "illegal enable change, "
809 "port %d\n", portno);
812 if (up->restartcnt == USB_RESTART_MAX) {
813 /* XXX could try another speed ? */
814 DPRINTFN(0, "port error, giving up "
815 "port %d\n", portno);
817 sc->sc_st.port_change |=
818 UPS_C_CONNECT_STATUS;
823 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
824 err = uhub_reattach_port(sc, portno);
826 /* most likely the HUB is gone */
830 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
831 UPS_C_PORT_LINK_STATE)) {
832 err = uhub_suspend_resume_port(sc, portno);
834 /* most likely the HUB is gone */
838 err = uhub_explore_sub(sc, up);
840 /* no device(s) present */
843 /* explore succeeded - reset restart counter */
848 usbd_enum_unlock(udev);
850 /* initial status checked */
851 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
854 return (USB_ERR_NORMAL_COMPLETION);
858 uhub_probe(device_t dev)
860 struct usb_attach_arg *uaa = device_get_ivars(dev);
862 if (uaa->usb_mode != USB_MODE_HOST)
866 * The subclass for USB HUBs is currently ignored because it
867 * is 0 for some and 1 for others.
869 if (uaa->info.bConfigIndex == 0 &&
870 uaa->info.bDeviceClass == UDCLASS_HUB)
876 /* NOTE: The information returned by this function can be wrong. */
878 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
880 struct usb_hub_descriptor hubdesc20;
881 struct usb_hub_ss_descriptor hubdesc30;
886 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
887 return (USB_ERR_INVAL);
892 switch (udev->speed) {
896 /* assuming that there is one port */
897 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
899 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
900 "error=%s\n", usbd_errstr(err));
903 nports = hubdesc20.bNbrPorts;
907 if (udev->speed == USB_SPEED_HIGH)
908 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
911 case USB_SPEED_SUPER:
912 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
914 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
915 "error=%s\n", usbd_errstr(err));
918 nports = hubdesc30.bNbrPorts;
938 uhub_attach(device_t dev)
940 struct uhub_softc *sc = device_get_softc(dev);
941 struct usb_attach_arg *uaa = device_get_ivars(dev);
942 struct usb_device *udev = uaa->device;
943 struct usb_device *parent_hub = udev->parent_hub;
945 struct usb_hub_descriptor hubdesc20;
946 struct usb_hub_ss_descriptor hubdesc30;
958 lockinit(&sc->sc_lock, "USB HUB mutex", 0, 0);
960 device_set_usb_desc(dev);
962 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
963 "parent->selfpowered=%d\n",
965 udev->flags.self_powered,
968 parent_hub->flags.self_powered : 0);
970 if (uhub_is_too_deep(udev)) {
971 DPRINTFN(0, "HUB at depth %d, "
972 "exceeds maximum. HUB ignored\n", (int)udev->depth);
976 if (!udev->flags.self_powered && parent_hub &&
977 !parent_hub->flags.self_powered) {
978 DPRINTFN(0, "Bus powered HUB connected to "
979 "bus powered HUB. HUB ignored\n");
983 if (UHUB_IS_MULTI_TT(sc)) {
984 err = usbd_set_alt_interface_index(udev, 0, 1);
986 device_printf(dev, "MTT could not be enabled\n");
989 device_printf(dev, "MTT enabled\n");
992 /* get HUB descriptor */
994 DPRINTFN(2, "Getting HUB descriptor\n");
996 switch (udev->speed) {
1000 /* assuming that there is one port */
1001 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1003 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1004 "error=%s\n", usbd_errstr(err));
1007 /* get number of ports */
1008 nports = hubdesc20.bNbrPorts;
1010 /* get power delay */
1011 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1012 usb_extra_power_up_time);
1014 /* get complete HUB descriptor */
1016 /* check number of ports */
1018 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1019 "error=%s\n", usbd_errstr(err));
1022 /* get complete HUB descriptor */
1023 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1026 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1027 "error=%s\n", usbd_errstr(err));
1030 if (hubdesc20.bNbrPorts != nports) {
1031 DPRINTFN(0, "Number of ports changed\n");
1036 case USB_SPEED_SUPER:
1037 if (udev->parent_hub != NULL) {
1038 err = usbd_req_set_hub_depth(udev, NULL,
1041 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1042 "error=%s\n", usbd_errstr(err));
1046 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1048 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1049 "error=%s\n", usbd_errstr(err));
1052 /* get number of ports */
1053 nports = hubdesc30.bNbrPorts;
1055 /* get power delay */
1056 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1057 usb_extra_power_up_time);
1059 /* get complete HUB descriptor */
1061 /* check number of ports */
1062 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1063 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1064 "error=%s\n", usbd_errstr(err));
1067 /* get complete HUB descriptor */
1068 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1071 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1072 "error=%s\n", usbd_errstr(err));
1075 if (hubdesc30.bNbrPorts != nports) {
1076 DPRINTFN(0, "Number of ports changed\n");
1082 DPRINTF("Assuming HUB has only one port\n");
1083 /* default number of ports */
1085 /* default power delay */
1086 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1090 DPRINTFN(0, "portless HUB\n");
1093 if (nports > USB_MAX_PORTS) {
1094 DPRINTF("Port limit exceeded\n");
1097 #if (USB_HAVE_FIXED_PORT == 0)
1098 hub = kmalloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1099 M_USBDEV, M_WAITOK | M_ZERO);
1108 /* initialize HUB structure */
1110 hub->explore = &uhub_explore;
1111 hub->nports = nports;
1112 hub->hubudev = udev;
1114 /* if self powered hub, give ports maximum current */
1115 if (udev->flags.self_powered) {
1116 hub->portpower = USB_MAX_POWER;
1118 hub->portpower = USB_MIN_POWER;
1121 /* set up interrupt pipe */
1123 if (udev->parent_hub == NULL) {
1124 /* root HUB is special */
1128 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1129 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_lock);
1132 DPRINTFN(0, "cannot setup interrupt transfer, "
1133 "errstr=%s\n", usbd_errstr(err));
1136 /* wait with power off for a while */
1137 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1140 * To have the best chance of success we do things in the exact same
1141 * order as Windoze98. This should not be necessary, but some
1142 * devices do not follow the USB specs to the letter.
1144 * These are the events on the bus when a hub is attached:
1145 * Get device and config descriptors (see attach code)
1146 * Get hub descriptor (see above)
1149 * wait for power to become stable
1150 * (all below happens in explore code)
1152 * clear C_PORT_CONNECTION
1155 * if device connected
1159 * clear C_PORT_RESET
1161 * proceed with device attachment
1164 /* XXX should check for none, individual, or ganged power? */
1168 for (x = 0; x != nports; x++) {
1169 /* set up data structures */
1170 struct usb_port *up = hub->ports + x;
1172 up->device_index = 0;
1176 /* check if port is removable */
1177 switch (udev->speed) {
1179 case USB_SPEED_FULL:
1180 case USB_SPEED_HIGH:
1181 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1184 case USB_SPEED_SUPER:
1185 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1189 DPRINTF("Assuming removable port\n");
1194 /* turn the power on */
1195 err = usbd_req_set_port_feature(udev, NULL,
1196 portno, UHF_PORT_POWER);
1199 DPRINTFN(0, "port %d power on failed, %s\n",
1200 portno, usbd_errstr(err));
1202 DPRINTF("turn on port %d power\n",
1205 /* wait for stable power */
1206 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1209 device_printf(dev, "%d port%s with %d "
1210 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1211 removable, udev->flags.self_powered ? "self" : "bus");
1213 /* Start the interrupt endpoint, if any */
1215 if (sc->sc_xfer[0] != NULL) {
1216 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1217 usbd_transfer_start(sc->sc_xfer[0]);
1218 lockmgr(&sc->sc_lock, LK_RELEASE);
1221 /* Enable automatic power save on all USB HUBs */
1223 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1228 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1231 #if (USB_HAVE_FIXED_PORT == 0)
1232 kfree(udev->hub, M_USBDEV);
1236 lockuninit(&sc->sc_lock);
1242 * Called from process context when the hub is gone.
1243 * Detach all devices on active ports.
1246 uhub_detach(device_t dev)
1248 struct uhub_softc *sc = device_get_softc(dev);
1249 struct usb_hub *hub = sc->sc_udev->hub;
1250 struct usb_device *child;
1253 if (hub == NULL) /* must be partially working */
1256 /* Make sure interrupt transfer is gone. */
1257 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1259 /* Detach all ports */
1260 for (x = 0; x != hub->nports; x++) {
1262 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1264 if (child == NULL) {
1269 * Free USB device and all subdevices, if any.
1271 usb_free_device(child, 0);
1274 #if (USB_HAVE_FIXED_PORT == 0)
1275 kfree(hub, M_USBDEV);
1277 sc->sc_udev->hub = NULL;
1279 lockuninit(&sc->sc_lock);
1285 uhub_suspend(device_t dev)
1288 /* Sub-devices are not suspended here! */
1293 uhub_resume(device_t dev)
1296 /* Sub-devices are not resumed here! */
1301 uhub_driver_added(device_t dev, driver_t *driver)
1303 usb_needs_explore_all();
1307 struct usb_device *udev;
1309 uint8_t iface_index;
1313 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1314 struct hub_result *res)
1316 struct usb_interface *iface;
1317 struct usb_device *udev;
1322 nports = hub->nports;
1323 for (x = 0; x != nports; x++) {
1324 udev = usb_bus_port_get_device(hub->hubudev->bus,
1329 for (i = 0; i != USB_IFACE_MAX; i++) {
1330 iface = usbd_get_iface(udev, i);
1332 (iface->subdev == child)) {
1333 res->iface_index = i;
1335 res->portno = x + 1;
1340 res->iface_index = 0;
1346 uhub_child_location_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 hub_result res;
1353 if (!device_is_attached(parent)) {
1359 sc = device_get_softc(parent);
1360 hub = sc->sc_udev->hub;
1365 uhub_find_iface_index(hub, child, &res);
1367 DPRINTF("device not on hub\n");
1373 ksnprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1374 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1375 res.portno, device_get_unit(res.udev->bus->bdev),
1376 res.udev->device_index, res.iface_index);
1386 uhub_child_pnpinfo_string(device_t parent, device_t child,
1387 char *buf, size_t buflen)
1389 struct uhub_softc *sc;
1390 struct usb_hub *hub;
1391 struct usb_interface *iface;
1392 struct hub_result res;
1394 if (!device_is_attached(parent)) {
1400 sc = device_get_softc(parent);
1401 hub = sc->sc_udev->hub;
1406 uhub_find_iface_index(hub, child, &res);
1408 DPRINTF("device not on hub\n");
1414 iface = usbd_get_iface(res.udev, res.iface_index);
1415 if (iface && iface->idesc) {
1416 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1417 "devclass=0x%02x devsubclass=0x%02x "
1421 "intclass=0x%02x intsubclass=0x%02x "
1422 "intprotocol=0x%02x " "%s%s",
1423 UGETW(res.udev->ddesc.idVendor),
1424 UGETW(res.udev->ddesc.idProduct),
1425 res.udev->ddesc.bDeviceClass,
1426 res.udev->ddesc.bDeviceSubClass,
1427 usb_get_serial(res.udev),
1428 UGETW(res.udev->ddesc.bcdDevice),
1429 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1430 iface->idesc->bInterfaceClass,
1431 iface->idesc->bInterfaceSubClass,
1432 iface->idesc->bInterfaceProtocol,
1433 iface->pnpinfo ? " " : "",
1434 iface->pnpinfo ? iface->pnpinfo : "");
1450 * The USB Transaction Translator:
1451 * ===============================
1453 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1454 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1455 * USB transfers. To utilize bandwidth dynamically the "scatter and
1456 * gather" principle must be applied. This means that bandwidth must
1457 * be divided into equal parts of bandwidth. With regard to USB all
1458 * data is transferred in smaller packets with length
1459 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1462 * The bandwidth scheduler which I have implemented will simply pack
1463 * the USB transfers back to back until there is no more space in the
1464 * schedule. Out of the 8 microframes which the USB 2.0 standard
1465 * provides, only 6 are available for non-HIGH-speed devices. I have
1466 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1467 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1468 * this division, it is very difficult to allocate and free bandwidth
1471 * NOTE about the Transaction Translator in USB HUBs:
1473 * USB HUBs have a very simple Transaction Translator, that will
1474 * simply pipeline all the SPLIT transactions. That means that the
1475 * transactions will be executed in the order they are queued!
1479 /*------------------------------------------------------------------------*
1480 * usb_intr_find_best_slot
1483 * The best Transaction Translation slot for an interrupt endpoint.
1484 *------------------------------------------------------------------------*/
1486 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1487 uint8_t end, uint8_t mask)
1489 usb_size_t min = (usb_size_t)-1;
1497 /* find the last slot with lesser used bandwidth */
1499 for (x = start; x < end; x++) {
1503 /* compute sum of bandwidth */
1504 for (z = x; z < end; z++) {
1505 if (mask & (1U << (z - x)))
1509 /* check if the current multi-slot is more optimal */
1515 /* check if the mask is about to be shifted out */
1516 if (mask & (1U << (end - 1 - x)))
1522 /*------------------------------------------------------------------------*
1523 * usb_hs_bandwidth_adjust
1525 * This function will update the bandwith usage for the microframe
1526 * having index "slot" by "len" bytes. "len" can be negative. If the
1527 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1528 * the "slot" argument will be replaced by the slot having least used
1529 * bandwidth. The "mask" argument is used for multi-slot allocations.
1532 * The slot in which the bandwidth update was done: 0..7
1533 *------------------------------------------------------------------------*/
1535 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1536 uint8_t slot, uint8_t mask)
1538 struct usb_bus *bus = udev->bus;
1539 struct usb_hub *hub;
1540 enum usb_dev_speed speed;
1543 USB_BUS_LOCK_ASSERT(bus);
1545 speed = usbd_get_speed(udev);
1549 case USB_SPEED_FULL:
1550 if (speed == USB_SPEED_LOW) {
1554 * The Host Controller Driver should have
1555 * performed checks so that the lookup
1556 * below does not result in a NULL pointer
1560 hub = udev->parent_hs_hub->hub;
1561 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1562 slot = usb_intr_find_best_slot(hub->uframe_usage,
1563 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1565 for (x = slot; x < 8; x++) {
1566 if (mask & (1U << (x - slot))) {
1567 hub->uframe_usage[x] += len;
1568 bus->uframe_usage[x] += len;
1573 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1574 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1575 USB_HS_MICRO_FRAMES_MAX, mask);
1577 for (x = slot; x < 8; x++) {
1578 if (mask & (1U << (x - slot))) {
1579 bus->uframe_usage[x] += len;
1587 /*------------------------------------------------------------------------*
1588 * usb_hs_bandwidth_alloc
1590 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1591 *------------------------------------------------------------------------*/
1593 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1595 struct usb_device *udev;
1600 udev = xfer->xroot->udev;
1602 if (udev->flags.usb_mode != USB_MODE_HOST)
1603 return; /* not supported */
1605 xfer->endpoint->refcount_bw++;
1606 if (xfer->endpoint->refcount_bw != 1)
1607 return; /* already allocated */
1609 speed = usbd_get_speed(udev);
1611 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1613 /* allocate a microframe slot */
1616 slot = usb_hs_bandwidth_adjust(udev,
1617 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1619 xfer->endpoint->usb_uframe = slot;
1620 xfer->endpoint->usb_smask = mask << slot;
1622 if ((speed != USB_SPEED_FULL) &&
1623 (speed != USB_SPEED_LOW)) {
1624 xfer->endpoint->usb_cmask = 0x00 ;
1626 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1630 case UE_ISOCHRONOUS:
1631 switch (usbd_xfer_get_fps_shift(xfer)) {
1646 /* allocate a microframe multi-slot */
1648 slot = usb_hs_bandwidth_adjust(udev,
1649 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1651 xfer->endpoint->usb_uframe = slot;
1652 xfer->endpoint->usb_cmask = 0;
1653 xfer->endpoint->usb_smask = mask << slot;
1657 xfer->endpoint->usb_uframe = 0;
1658 xfer->endpoint->usb_cmask = 0;
1659 xfer->endpoint->usb_smask = 0;
1663 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1664 xfer->endpoint->usb_uframe,
1665 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1668 /*------------------------------------------------------------------------*
1669 * usb_hs_bandwidth_free
1671 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1672 *------------------------------------------------------------------------*/
1674 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1676 struct usb_device *udev;
1680 udev = xfer->xroot->udev;
1682 if (udev->flags.usb_mode != USB_MODE_HOST)
1683 return; /* not supported */
1685 xfer->endpoint->refcount_bw--;
1686 if (xfer->endpoint->refcount_bw != 0)
1687 return; /* still allocated */
1689 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1691 case UE_ISOCHRONOUS:
1693 slot = xfer->endpoint->usb_uframe;
1694 mask = xfer->endpoint->usb_smask;
1696 /* free microframe slot(s): */
1697 usb_hs_bandwidth_adjust(udev,
1698 -xfer->max_frame_size, slot, mask >> slot);
1700 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1701 slot, mask >> slot);
1703 xfer->endpoint->usb_uframe = 0;
1704 xfer->endpoint->usb_cmask = 0;
1705 xfer->endpoint->usb_smask = 0;
1713 /*------------------------------------------------------------------------*
1714 * usb_isoc_time_expand
1716 * This function will expand the time counter from 7-bit to 16-bit.
1719 * 16-bit isochronous time counter.
1720 *------------------------------------------------------------------------*/
1722 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1726 USB_BUS_LOCK_ASSERT(bus);
1728 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1730 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1732 if (isoc_time_curr < rem) {
1733 /* the time counter wrapped around */
1734 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1736 /* update the remainder */
1738 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1739 bus->isoc_time_last |= isoc_time_curr;
1741 return (bus->isoc_time_last);
1744 /*------------------------------------------------------------------------*
1745 * usbd_fs_isoc_schedule_alloc_slot
1747 * This function will allocate bandwidth for an isochronous FULL speed
1748 * transaction in the FULL speed schedule.
1753 *------------------------------------------------------------------------*/
1754 #if USB_HAVE_TT_SUPPORT
1756 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1758 struct usb_xfer *xfer;
1759 struct usb_xfer *pipe_xfer;
1760 struct usb_bus *bus;
1762 usb_frlength_t data_len;
1770 bus = isoc_xfer->xroot->bus;
1772 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1774 /* skip self, if any */
1776 if (xfer == isoc_xfer)
1779 /* check if this USB transfer is going through the same TT */
1781 if (xfer->xroot->udev->parent_hs_hub !=
1782 isoc_xfer->xroot->udev->parent_hs_hub) {
1785 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1786 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1787 (xfer->xroot->udev->hs_port_no !=
1788 isoc_xfer->xroot->udev->hs_port_no)) {
1791 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1794 /* check if isoc_time is part of this transfer */
1796 delta = xfer->isoc_time_complete - isoc_time;
1797 if (delta > 0 && delta <= xfer->nframes) {
1798 delta = xfer->nframes - delta;
1800 len = xfer->frlengths[delta];
1809 * Check double buffered transfers. Only stream ID
1810 * equal to zero is valid here!
1812 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
1815 /* skip self, if any */
1817 if (pipe_xfer == isoc_xfer)
1820 /* check if isoc_time is part of this transfer */
1822 delta = pipe_xfer->isoc_time_complete - isoc_time;
1823 if (delta > 0 && delta <= pipe_xfer->nframes) {
1824 delta = pipe_xfer->nframes - delta;
1826 len = pipe_xfer->frlengths[delta];
1836 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1837 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1841 /* check for overflow */
1843 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1848 delta = isoc_xfer->isoc_time_complete - isoc_time;
1849 if (delta > 0 && delta <= isoc_xfer->nframes) {
1850 delta = isoc_xfer->nframes - delta;
1852 len = isoc_xfer->frlengths[delta];
1860 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1861 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1865 /* check for overflow */
1867 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1874 /*------------------------------------------------------------------------*
1875 * usb_bus_port_get_device
1877 * This function is NULL safe.
1878 *------------------------------------------------------------------------*/
1880 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1882 if ((bus == NULL) || (up == NULL)) {
1886 if (up->device_index == 0) {
1890 return (bus->devices[up->device_index]);
1893 /*------------------------------------------------------------------------*
1894 * usb_bus_port_set_device
1896 * This function is NULL safe.
1897 *------------------------------------------------------------------------*/
1899 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1900 struct usb_device *udev, uint8_t device_index)
1907 * There is only one case where we don't
1908 * have an USB port, and that is the Root Hub!
1912 up->device_index = device_index;
1914 device_index = up->device_index;
1915 up->device_index = 0;
1919 * Make relationships to our new device
1921 if (device_index != 0) {
1923 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1925 bus->devices[device_index] = udev;
1927 lockmgr(&usb_ref_lock, LK_RELEASE);
1933 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1936 /*------------------------------------------------------------------------*
1939 * This functions is called when the USB event thread needs to run.
1940 *------------------------------------------------------------------------*/
1942 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1949 DPRINTF("No bus pointer!\n");
1952 if ((bus->devices == NULL) ||
1953 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1954 DPRINTF("No root HUB\n");
1957 if (lockowned(&bus->bus_lock) != 0) {
1966 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
1967 &bus->explore_msg[0], &bus->explore_msg[1])) {
1971 USB_BUS_UNLOCK(bus);
1975 /*------------------------------------------------------------------------*
1976 * usb_needs_explore_all
1978 * This function is called whenever a new driver is loaded and will
1979 * cause that all USB busses are re-explored.
1980 *------------------------------------------------------------------------*/
1982 usb_needs_explore_all(void)
1984 struct usb_bus *bus;
1991 dc = usb_devclass_ptr;
1993 DPRINTFN(0, "no devclass\n");
1997 * Explore all USB busses in parallell.
1999 max = devclass_get_maxunit(dc);
2001 dev = devclass_get_device(dc, max);
2003 bus = device_get_softc(dev);
2005 usb_needs_explore(bus, 1);
2012 /*------------------------------------------------------------------------*
2013 * usb_bus_power_update
2015 * This function will ensure that all USB devices on the given bus are
2016 * properly suspended or resumed according to the device transfer
2018 *------------------------------------------------------------------------*/
2021 usb_bus_power_update(struct usb_bus *bus)
2023 usb_needs_explore(bus, 0 /* no probe */ );
2027 /*------------------------------------------------------------------------*
2028 * usbd_transfer_power_ref
2030 * This function will modify the power save reference counts and
2031 * wakeup the USB device associated with the given USB transfer, if
2033 *------------------------------------------------------------------------*/
2036 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2038 static const usb_power_mask_t power_mask[4] = {
2039 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2040 [UE_BULK] = USB_HW_POWER_BULK,
2041 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2042 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2044 struct usb_device *udev;
2045 uint8_t needs_explore;
2046 uint8_t needs_hw_power;
2049 udev = xfer->xroot->udev;
2051 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2052 /* no power save for root HUB */
2055 USB_BUS_LOCK(udev->bus);
2057 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2059 udev->pwr_save.last_xfer_time = ticks;
2060 udev->pwr_save.type_refs[xfer_type] += val;
2062 if (xfer->flags_int.control_xfr) {
2063 udev->pwr_save.read_refs += val;
2064 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2066 * It is not allowed to suspend during a
2069 udev->pwr_save.write_refs += val;
2071 } else if (USB_GET_DATA_ISREAD(xfer)) {
2072 udev->pwr_save.read_refs += val;
2074 udev->pwr_save.write_refs += val;
2078 if (udev->flags.self_suspended)
2079 needs_explore = usb_peer_should_wakeup(udev);
2083 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2084 DPRINTF("Adding type %u to power state\n", xfer_type);
2085 udev->bus->hw_power_state |= power_mask[xfer_type];
2095 USB_BUS_UNLOCK(udev->bus);
2097 if (needs_explore) {
2098 DPRINTF("update\n");
2099 usb_bus_power_update(udev->bus);
2100 } else if (needs_hw_power) {
2101 DPRINTF("needs power\n");
2102 if (udev->bus->methods->set_hw_power != NULL) {
2103 (udev->bus->methods->set_hw_power) (udev->bus);
2109 /*------------------------------------------------------------------------*
2110 * usb_peer_should_wakeup
2112 * This function returns non-zero if the current device should wake up.
2113 *------------------------------------------------------------------------*/
2115 usb_peer_should_wakeup(struct usb_device *udev)
2117 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2118 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2119 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2120 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2121 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2122 (udev->pwr_save.write_refs != 0) ||
2123 ((udev->pwr_save.read_refs != 0) &&
2124 (udev->flags.usb_mode == USB_MODE_HOST) &&
2125 (usb_peer_can_wakeup(udev) == 0)));
2128 /*------------------------------------------------------------------------*
2131 * This function implements the USB power daemon and is called
2132 * regularly from the USB explore thread.
2133 *------------------------------------------------------------------------*/
2136 usb_bus_powerd(struct usb_bus *bus)
2138 struct usb_device *udev;
2141 usb_ticks_t mintime;
2142 usb_size_t type_refs[5];
2145 limit = usb_power_timeout;
2148 else if (limit > 255)
2153 DPRINTF("bus=%p\n", bus);
2158 * The root HUB device is never suspended
2159 * and we simply skip it.
2161 for (x = USB_ROOT_HUB_ADDR + 1;
2162 x != bus->devices_max; x++) {
2164 udev = bus->devices[x];
2168 temp = ticks - udev->pwr_save.last_xfer_time;
2170 if (usb_peer_should_wakeup(udev)) {
2171 /* check if we are suspended */
2172 if (udev->flags.self_suspended != 0) {
2173 USB_BUS_UNLOCK(bus);
2174 usb_dev_resume_peer(udev);
2177 } else if ((temp >= limit) &&
2178 (udev->flags.usb_mode == USB_MODE_HOST) &&
2179 (udev->flags.self_suspended == 0)) {
2180 /* try to do suspend */
2182 USB_BUS_UNLOCK(bus);
2183 usb_dev_suspend_peer(udev);
2188 /* reset counters */
2190 mintime = (usb_ticks_t)-1;
2197 /* Re-loop all the devices to get the actual state */
2199 for (x = USB_ROOT_HUB_ADDR + 1;
2200 x != bus->devices_max; x++) {
2202 udev = bus->devices[x];
2206 /* we found a non-Root-Hub USB device */
2209 /* "last_xfer_time" can be updated by a resume */
2210 temp = ticks - udev->pwr_save.last_xfer_time;
2213 * Compute minimum time since last transfer for the complete
2219 if (udev->flags.self_suspended == 0) {
2220 type_refs[0] += udev->pwr_save.type_refs[0];
2221 type_refs[1] += udev->pwr_save.type_refs[1];
2222 type_refs[2] += udev->pwr_save.type_refs[2];
2223 type_refs[3] += udev->pwr_save.type_refs[3];
2227 if (mintime >= (usb_ticks_t)(1 * hz)) {
2228 /* recompute power masks */
2229 DPRINTF("Recomputing power masks\n");
2230 bus->hw_power_state = 0;
2231 if (type_refs[UE_CONTROL] != 0)
2232 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2233 if (type_refs[UE_BULK] != 0)
2234 bus->hw_power_state |= USB_HW_POWER_BULK;
2235 if (type_refs[UE_INTERRUPT] != 0)
2236 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2237 if (type_refs[UE_ISOCHRONOUS] != 0)
2238 bus->hw_power_state |= USB_HW_POWER_ISOC;
2239 if (type_refs[4] != 0)
2240 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2242 USB_BUS_UNLOCK(bus);
2244 if (bus->methods->set_hw_power != NULL) {
2245 /* always update hardware power! */
2246 (bus->methods->set_hw_power) (bus);
2252 /*------------------------------------------------------------------------*
2253 * usb_dev_resume_peer
2255 * This function will resume an USB peer and do the required USB
2256 * signalling to get an USB device out of the suspended state.
2257 *------------------------------------------------------------------------*/
2259 usb_dev_resume_peer(struct usb_device *udev)
2261 struct usb_bus *bus;
2268 /* check if already resumed */
2269 if (udev->flags.self_suspended == 0)
2272 /* we need a parent HUB to do resume */
2273 if (udev->parent_hub == NULL)
2276 DPRINTF("udev=%p\n", udev);
2278 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2279 (udev->flags.remote_wakeup == 0)) {
2281 * If the host did not set the remote wakeup feature, we can
2282 * not wake it up either!
2284 DPRINTF("remote wakeup is not set!\n");
2287 /* get bus pointer */
2290 /* resume parent hub first */
2291 usb_dev_resume_peer(udev->parent_hub);
2293 /* reduce chance of instant resume failure by waiting a little bit */
2294 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2296 if (usb_device_20_compatible(udev)) {
2297 /* resume current port (Valid in Host and Device Mode) */
2298 err = usbd_req_clear_port_feature(udev->parent_hub,
2299 NULL, udev->port_no, UHF_PORT_SUSPEND);
2301 DPRINTFN(0, "Resuming port failed\n");
2305 /* resume current port (Valid in Host and Device Mode) */
2306 err = usbd_req_set_port_link_state(udev->parent_hub,
2307 NULL, udev->port_no, UPS_PORT_LS_U0);
2309 DPRINTFN(0, "Resuming port failed\n");
2314 /* resume settle time */
2315 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2317 if (bus->methods->device_resume != NULL) {
2318 /* resume USB device on the USB controller */
2319 (bus->methods->device_resume) (udev);
2322 /* set that this device is now resumed */
2323 udev->flags.self_suspended = 0;
2325 /* make sure that we don't go into suspend right away */
2326 udev->pwr_save.last_xfer_time = ticks;
2328 /* make sure the needed power masks are on */
2329 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2330 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2331 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2332 bus->hw_power_state |= USB_HW_POWER_BULK;
2333 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2334 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2335 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2336 bus->hw_power_state |= USB_HW_POWER_ISOC;
2338 USB_BUS_UNLOCK(bus);
2340 if (bus->methods->set_hw_power != NULL) {
2341 /* always update hardware power! */
2342 (bus->methods->set_hw_power) (bus);
2347 /* notify all sub-devices about resume */
2348 err = usb_suspend_resume(udev, 0);
2350 usbd_sr_unlock(udev);
2352 /* check if peer has wakeup capability */
2353 if (usb_peer_can_wakeup(udev)) {
2354 /* clear remote wakeup */
2355 err = usbd_req_clear_device_feature(udev,
2356 NULL, UF_DEVICE_REMOTE_WAKEUP);
2358 DPRINTFN(0, "Clearing device "
2359 "remote wakeup failed: %s\n",
2365 /*------------------------------------------------------------------------*
2366 * usb_dev_suspend_peer
2368 * This function will suspend an USB peer and do the required USB
2369 * signalling to get an USB device into the suspended state.
2370 *------------------------------------------------------------------------*/
2372 usb_dev_suspend_peer(struct usb_device *udev)
2374 struct usb_device *child;
2384 /* check if already suspended */
2385 if (udev->flags.self_suspended)
2388 /* we need a parent HUB to do suspend */
2389 if (udev->parent_hub == NULL)
2392 DPRINTF("udev=%p\n", udev);
2394 /* check if the current device is a HUB */
2395 if (udev->hub != NULL) {
2396 nports = udev->hub->nports;
2398 /* check if all devices on the HUB are suspended */
2399 for (x = 0; x != nports; x++) {
2400 child = usb_bus_port_get_device(udev->bus,
2401 udev->hub->ports + x);
2406 if (child->flags.self_suspended)
2409 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2414 if (usb_peer_can_wakeup(udev)) {
2416 * This request needs to be done before we set
2417 * "udev->flags.self_suspended":
2420 /* allow device to do remote wakeup */
2421 err = usbd_req_set_device_feature(udev,
2422 NULL, UF_DEVICE_REMOTE_WAKEUP);
2424 DPRINTFN(0, "Setting device "
2425 "remote wakeup failed\n");
2429 USB_BUS_LOCK(udev->bus);
2431 * Checking for suspend condition and setting suspended bit
2434 err = usb_peer_should_wakeup(udev);
2437 * Set that this device is suspended. This variable
2438 * must be set before calling USB controller suspend
2441 udev->flags.self_suspended = 1;
2443 USB_BUS_UNLOCK(udev->bus);
2446 if (usb_peer_can_wakeup(udev)) {
2447 /* allow device to do remote wakeup */
2448 err = usbd_req_clear_device_feature(udev,
2449 NULL, UF_DEVICE_REMOTE_WAKEUP);
2451 DPRINTFN(0, "Setting device "
2452 "remote wakeup failed\n");
2456 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2457 /* resume parent HUB first */
2458 usb_dev_resume_peer(udev->parent_hub);
2460 /* reduce chance of instant resume failure by waiting a little bit */
2461 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2463 /* resume current port (Valid in Host and Device Mode) */
2464 err = usbd_req_clear_port_feature(udev->parent_hub,
2465 NULL, udev->port_no, UHF_PORT_SUSPEND);
2467 /* resume settle time */
2468 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2470 DPRINTF("Suspend was cancelled!\n");
2476 /* notify all sub-devices about suspend */
2477 err = usb_suspend_resume(udev, 1);
2479 usbd_sr_unlock(udev);
2481 if (udev->bus->methods->device_suspend != NULL) {
2484 /* suspend device on the USB controller */
2485 (udev->bus->methods->device_suspend) (udev);
2488 temp = usbd_get_dma_delay(udev);
2490 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2494 if (usb_device_20_compatible(udev)) {
2495 /* suspend current port */
2496 err = usbd_req_set_port_feature(udev->parent_hub,
2497 NULL, udev->port_no, UHF_PORT_SUSPEND);
2499 DPRINTFN(0, "Suspending port failed\n");
2503 /* suspend current port */
2504 err = usbd_req_set_port_link_state(udev->parent_hub,
2505 NULL, udev->port_no, UPS_PORT_LS_U3);
2507 DPRINTFN(0, "Suspending port failed\n");
2512 udev = udev->parent_hub;
2516 /*------------------------------------------------------------------------*
2517 * usbd_set_power_mode
2519 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2521 *------------------------------------------------------------------------*/
2523 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2525 /* filter input argument */
2526 if ((power_mode != USB_POWER_MODE_ON) &&
2527 (power_mode != USB_POWER_MODE_OFF))
2528 power_mode = USB_POWER_MODE_SAVE;
2530 power_mode = usbd_filter_power_mode(udev, power_mode);
2532 udev->power_mode = power_mode; /* update copy of power mode */
2535 usb_bus_power_update(udev->bus);
2537 usb_needs_explore(udev->bus, 0 /* no probe */ );
2541 /*------------------------------------------------------------------------*
2542 * usbd_filter_power_mode
2544 * This function filters the power mode based on hardware requirements.
2545 *------------------------------------------------------------------------*/
2547 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2549 const struct usb_bus_methods *mtod;
2552 mtod = udev->bus->methods;
2555 if (mtod->get_power_mode != NULL)
2556 (mtod->get_power_mode) (udev, &temp);
2558 /* check if we should not filter */
2560 return (power_mode);
2562 /* use fixed power mode given by hardware driver */
2566 /*------------------------------------------------------------------------*
2567 * usbd_start_re_enumerate
2569 * This function starts re-enumeration of the given USB device. This
2570 * function does not need to be called BUS-locked. This function does
2571 * not wait until the re-enumeration is completed.
2572 *------------------------------------------------------------------------*/
2574 usbd_start_re_enumerate(struct usb_device *udev)
2576 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2577 udev->re_enumerate_wait = USB_RE_ENUM_START;
2578 usb_needs_explore(udev->bus, 0);