3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_ioctl.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
57 #define USB_DEBUG_VAR uhub_debug
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/usb_device.h>
62 #include <dev/usb/usb_request.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_hub.h>
65 #include <dev/usb/usb_util.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_dynamic.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
73 #define UHUB_INTR_INTERVAL 250 /* ms */
74 #define UHUB_N_TRANSFER 1
77 static int uhub_debug = 0;
79 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
80 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
83 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
87 static int usb_power_timeout = 30; /* seconds */
89 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
90 &usb_power_timeout, 0, "USB power timeout");
93 struct uhub_current_state {
99 struct uhub_current_state sc_st;/* current state */
100 device_t sc_dev; /* base device */
101 struct mtx sc_mtx; /* our mutex */
102 struct usb_device *sc_udev; /* USB device */
103 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
105 #define UHUB_FLAG_DID_EXPLORE 0x01
109 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
110 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
111 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
112 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
114 /* prototypes for type checking: */
116 static device_probe_t uhub_probe;
117 static device_attach_t uhub_attach;
118 static device_detach_t uhub_detach;
119 static device_suspend_t uhub_suspend;
120 static device_resume_t uhub_resume;
122 static bus_driver_added_t uhub_driver_added;
123 static bus_child_location_str_t uhub_child_location_string;
124 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
126 static usb_callback_t uhub_intr_callback;
128 static void usb_dev_resume_peer(struct usb_device *udev);
129 static void usb_dev_suspend_peer(struct usb_device *udev);
130 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
132 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
135 .type = UE_INTERRUPT,
136 .endpoint = UE_ADDR_ANY,
137 .direction = UE_DIR_ANY,
139 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
140 .bufsize = 0, /* use wMaxPacketSize */
141 .callback = &uhub_intr_callback,
142 .interval = UHUB_INTR_INTERVAL,
147 * driver instance for "hub" connected to "usb"
148 * and "hub" connected to "hub"
150 static devclass_t uhub_devclass;
152 static device_method_t uhub_methods[] = {
153 DEVMETHOD(device_probe, uhub_probe),
154 DEVMETHOD(device_attach, uhub_attach),
155 DEVMETHOD(device_detach, uhub_detach),
157 DEVMETHOD(device_suspend, uhub_suspend),
158 DEVMETHOD(device_resume, uhub_resume),
160 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
161 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
162 DEVMETHOD(bus_driver_added, uhub_driver_added),
166 static driver_t uhub_driver = {
168 .methods = uhub_methods,
169 .size = sizeof(struct uhub_softc)
172 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
173 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
174 MODULE_VERSION(uhub, 1);
177 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
179 struct uhub_softc *sc = usbd_xfer_softc(xfer);
181 switch (USB_GET_STATE(xfer)) {
182 case USB_ST_TRANSFERRED:
185 * This is an indication that some port
186 * has changed status. Notify the bus
187 * event handler thread that we need
188 * to be explored again:
190 usb_needs_explore(sc->sc_udev->bus, 0);
193 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
194 usbd_transfer_submit(xfer);
198 if (xfer->error != USB_ERR_CANCELLED) {
200 * Do a clear-stall. The "stall_pipe" flag
201 * will get cleared before next callback by
204 usbd_xfer_set_stall(xfer);
205 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
206 usbd_transfer_submit(xfer);
212 /*------------------------------------------------------------------------*
213 * uhub_explore_sub - subroutine
217 * Else: A control transaction failed
218 *------------------------------------------------------------------------*/
220 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
223 struct usb_device *child;
227 bus = sc->sc_udev->bus;
230 /* get driver added refcount from USB bus */
231 refcount = bus->driver_added_refcount;
233 /* get device assosiated with the given port */
234 child = usb_bus_port_get_device(bus, up);
240 /* check if device should be re-enumerated */
242 if (child->flags.usb_mode == USB_MODE_HOST) {
243 usbd_enum_lock(child);
244 if (child->re_enumerate_wait) {
245 err = usbd_set_config_index(child,
248 DPRINTF("Unconfigure failed: "
252 err = usbd_req_re_enumerate(child, NULL);
254 err = usbd_set_config_index(child, 0);
256 err = usb_probe_and_attach(child,
257 USB_IFACE_INDEX_ANY);
259 child->re_enumerate_wait = 0;
262 usbd_enum_unlock(child);
265 /* check if probe and attach should be done */
267 if (child->driver_added_refcount != refcount) {
268 child->driver_added_refcount = refcount;
269 err = usb_probe_and_attach(child,
270 USB_IFACE_INDEX_ANY);
275 /* start control transfer, if device mode */
277 if (child->flags.usb_mode == USB_MODE_DEVICE)
278 usbd_ctrl_transfer_setup(child);
280 /* if a HUB becomes present, do a recursive HUB explore */
283 err = (child->hub->explore) (child);
289 /*------------------------------------------------------------------------*
290 * uhub_read_port_status - factored out code
291 *------------------------------------------------------------------------*/
293 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
295 struct usb_port_status ps;
298 err = usbd_req_get_port_status(
299 sc->sc_udev, NULL, &ps, portno);
301 /* update status regardless of error */
303 sc->sc_st.port_status = UGETW(ps.wPortStatus);
304 sc->sc_st.port_change = UGETW(ps.wPortChange);
306 /* debugging print */
308 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
309 "wPortChange=0x%04x, err=%s\n",
310 portno, sc->sc_st.port_status,
311 sc->sc_st.port_change, usbd_errstr(err));
315 /*------------------------------------------------------------------------*
320 * Else: A control transaction failed
321 *------------------------------------------------------------------------*/
323 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
325 struct usb_device *child;
326 struct usb_device *udev;
327 enum usb_dev_speed speed;
328 enum usb_hc_mode mode;
332 DPRINTF("reattaching port %d\n", portno);
337 child = usb_bus_port_get_device(udev->bus,
338 udev->hub->ports + portno - 1);
342 /* first clear the port connection change bit */
344 err = usbd_req_clear_port_feature(udev, NULL,
345 portno, UHF_C_PORT_CONNECTION);
350 /* check if there is a child */
354 * Free USB device and all subdevices, if any.
356 usb_free_device(child, 0);
359 /* get fresh status */
361 err = uhub_read_port_status(sc, portno);
365 /* check if nothing is connected to the port */
367 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
370 /* check if there is no power on the port and print a warning */
372 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
373 DPRINTF("WARNING: strange, connected port %d "
374 "has no power\n", portno);
376 /* check if the device is in Host Mode */
378 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
380 DPRINTF("Port %d is in Host Mode\n", portno);
382 if (sc->sc_st.port_status & UPS_SUSPEND) {
384 * NOTE: Should not get here in SuperSpeed
385 * mode, because the HUB should report this
388 DPRINTF("Port %d was still "
389 "suspended, clearing.\n", portno);
390 err = usbd_req_clear_port_feature(udev,
391 NULL, portno, UHF_PORT_SUSPEND);
396 /* wait for maximum device power up time */
399 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
401 /* reset port, which implies enabling it */
403 err = usbd_req_reset_port(udev, NULL, portno);
406 DPRINTFN(0, "port %d reset "
407 "failed, error=%s\n",
408 portno, usbd_errstr(err));
411 /* get port status again, it might have changed during reset */
413 err = uhub_read_port_status(sc, portno);
417 /* check if something changed during port reset */
419 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
420 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
422 DPRINTFN(0, "giving up port reset "
423 "- device vanished\n");
430 DPRINTF("Port %d is in Device Mode\n", portno);
434 * Figure out the device speed
436 switch (udev->speed) {
438 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
439 speed = USB_SPEED_HIGH;
440 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
441 speed = USB_SPEED_LOW;
443 speed = USB_SPEED_FULL;
446 if (sc->sc_st.port_status & UPS_LOW_SPEED)
447 speed = USB_SPEED_LOW;
449 speed = USB_SPEED_FULL;
452 speed = USB_SPEED_LOW;
454 case USB_SPEED_SUPER:
455 if (udev->parent_hub == NULL) {
456 /* Root HUB - special case */
457 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
459 speed = USB_SPEED_FULL;
462 speed = USB_SPEED_LOW;
465 speed = USB_SPEED_HIGH;
468 speed = USB_SPEED_SUPER;
472 speed = USB_SPEED_SUPER;
476 /* same speed like parent */
480 if (speed == USB_SPEED_SUPER) {
481 err = usbd_req_set_hub_u1_timeout(udev, NULL,
482 portno, 128 - (2 * udev->depth));
484 DPRINTFN(0, "port %d U1 timeout "
485 "failed, error=%s\n",
486 portno, usbd_errstr(err));
488 err = usbd_req_set_hub_u2_timeout(udev, NULL,
489 portno, 128 - (2 * udev->depth));
491 DPRINTFN(0, "port %d U2 timeout "
492 "failed, error=%s\n",
493 portno, usbd_errstr(err));
498 * Figure out the device mode
500 * NOTE: This part is currently FreeBSD specific.
502 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
503 mode = USB_MODE_DEVICE;
505 mode = USB_MODE_HOST;
507 /* need to create a new child */
508 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
509 udev->depth + 1, portno - 1, portno, speed, mode);
511 DPRINTFN(0, "could not allocate new device\n");
514 return (0); /* success */
519 * Free USB device and all subdevices, if any.
521 usb_free_device(child, 0);
525 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
526 err = usbd_req_clear_port_feature(
528 portno, UHF_PORT_ENABLE);
532 DPRINTFN(0, "device problem (%s), "
533 "disabling port %d\n", usbd_errstr(err), portno);
538 /*------------------------------------------------------------------------*
539 * usb_device_20_compatible
542 * 0: HUB does not support suspend and resume
543 * Else: HUB supports suspend and resume
544 *------------------------------------------------------------------------*/
546 usb_device_20_compatible(struct usb_device *udev)
550 switch (udev->speed) {
560 /*------------------------------------------------------------------------*
561 * uhub_suspend_resume_port
565 * Else: A control transaction failed
566 *------------------------------------------------------------------------*/
568 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
570 struct usb_device *child;
571 struct usb_device *udev;
575 DPRINTF("port %d\n", portno);
578 child = usb_bus_port_get_device(udev->bus,
579 udev->hub->ports + portno - 1);
581 /* first clear the port suspend change bit */
583 if (usb_device_20_compatible(udev)) {
584 err = usbd_req_clear_port_feature(udev, NULL,
585 portno, UHF_C_PORT_SUSPEND);
587 err = usbd_req_clear_port_feature(udev, NULL,
588 portno, UHF_C_PORT_LINK_STATE);
592 DPRINTF("clearing suspend failed.\n");
595 /* get fresh status */
597 err = uhub_read_port_status(sc, portno);
599 DPRINTF("reading port status failed.\n");
602 /* convert current state */
604 if (usb_device_20_compatible(udev)) {
605 if (sc->sc_st.port_status & UPS_SUSPEND) {
611 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
622 DPRINTF("suspended=%u\n", is_suspend);
624 /* do the suspend or resume */
628 * This code handle two cases: 1) Host Mode - we can only
629 * receive resume here 2) Device Mode - we can receive
630 * suspend and resume here
633 usb_dev_resume_peer(child);
634 else if ((child->flags.usb_mode == USB_MODE_DEVICE) ||
635 (usb_device_20_compatible(child) == 0))
636 usb_dev_suspend_peer(child);
642 /*------------------------------------------------------------------------*
643 * uhub_root_interrupt
645 * This function is called when a Root HUB interrupt has
646 * happened. "ptr" and "len" makes up the Root HUB interrupt
647 * packet. This function is called having the "bus_mtx" locked.
648 *------------------------------------------------------------------------*/
650 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
652 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
654 usb_needs_explore(bus, 0);
658 uhub_is_too_deep(struct usb_device *udev)
660 switch (udev->speed) {
664 if (udev->depth > USB_HUB_MAX_DEPTH)
667 case USB_SPEED_SUPER:
668 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
677 /*------------------------------------------------------------------------*
683 *------------------------------------------------------------------------*/
685 uhub_explore(struct usb_device *udev)
688 struct uhub_softc *sc;
697 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
699 /* ignore devices that are too deep */
700 if (uhub_is_too_deep(udev))
701 return (USB_ERR_TOO_DEEP);
703 /* check if device is suspended */
704 if (udev->flags.self_suspended) {
705 /* need to wait until the child signals resume */
706 DPRINTF("Device is suspended!\n");
709 for (x = 0; x != hub->nports; x++) {
713 err = uhub_read_port_status(sc, portno);
715 /* most likely the HUB is gone */
718 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
719 DPRINTF("Overcurrent on port %u.\n", portno);
720 err = usbd_req_clear_port_feature(
721 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
723 /* most likely the HUB is gone */
727 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
729 * Fake a connect status change so that the
730 * status gets checked initially!
732 sc->sc_st.port_change |=
733 UPS_C_CONNECT_STATUS;
735 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
736 err = usbd_req_clear_port_feature(
737 udev, NULL, portno, UHF_C_PORT_ENABLE);
739 /* most likely the HUB is gone */
742 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
744 * Ignore the port error if the device
747 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
748 DPRINTFN(0, "illegal enable change, "
749 "port %d\n", portno);
752 if (up->restartcnt == USB_RESTART_MAX) {
753 /* XXX could try another speed ? */
754 DPRINTFN(0, "port error, giving up "
755 "port %d\n", portno);
757 sc->sc_st.port_change |=
758 UPS_C_CONNECT_STATUS;
763 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
764 err = uhub_reattach_port(sc, portno);
766 /* most likely the HUB is gone */
770 if (sc->sc_st.port_change & (UPS_C_SUSPEND | UPS_C_PORT_LINK_STATE)) {
771 err = uhub_suspend_resume_port(sc, portno);
773 /* most likely the HUB is gone */
777 err = uhub_explore_sub(sc, up);
779 /* no device(s) present */
782 /* explore succeeded - reset restart counter */
786 /* initial status checked */
787 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
790 return (USB_ERR_NORMAL_COMPLETION);
794 uhub_probe(device_t dev)
796 struct usb_attach_arg *uaa = device_get_ivars(dev);
798 if (uaa->usb_mode != USB_MODE_HOST)
802 * The subclass for USB HUBs is currently ignored because it
803 * is 0 for some and 1 for others.
805 if (uaa->info.bConfigIndex == 0 &&
806 uaa->info.bDeviceClass == UDCLASS_HUB)
812 /* NOTE: The information returned by this function can be wrong. */
814 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
816 struct usb_hub_descriptor hubdesc20;
817 struct usb_hub_ss_descriptor hubdesc30;
822 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
823 return (USB_ERR_INVAL);
828 switch (udev->speed) {
832 /* assuming that there is one port */
833 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
835 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
836 "error=%s\n", usbd_errstr(err));
839 nports = hubdesc20.bNbrPorts;
843 if (udev->speed == USB_SPEED_HIGH)
844 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
847 case USB_SPEED_SUPER:
848 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
850 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
851 "error=%s\n", usbd_errstr(err));
854 nports = hubdesc30.bNbrPorts;
874 uhub_attach(device_t dev)
876 struct uhub_softc *sc = device_get_softc(dev);
877 struct usb_attach_arg *uaa = device_get_ivars(dev);
878 struct usb_device *udev = uaa->device;
879 struct usb_device *parent_hub = udev->parent_hub;
881 struct usb_hub_descriptor hubdesc20;
882 struct usb_hub_ss_descriptor hubdesc30;
894 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
896 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
897 device_get_nameunit(dev));
899 device_set_usb_desc(dev);
901 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
902 "parent->selfpowered=%d\n",
904 udev->flags.self_powered,
907 parent_hub->flags.self_powered : 0);
909 if (uhub_is_too_deep(udev)) {
910 DPRINTFN(0, "HUB at depth %d, "
911 "exceeds maximum. HUB ignored\n", (int)udev->depth);
915 if (!udev->flags.self_powered && parent_hub &&
916 !parent_hub->flags.self_powered) {
917 DPRINTFN(0, "Bus powered HUB connected to "
918 "bus powered HUB. HUB ignored\n");
921 /* get HUB descriptor */
923 DPRINTFN(2, "Getting HUB descriptor\n");
925 switch (udev->speed) {
929 /* assuming that there is one port */
930 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
932 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
933 "error=%s\n", usbd_errstr(err));
936 /* get number of ports */
937 nports = hubdesc20.bNbrPorts;
939 /* get power delay */
940 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
941 USB_EXTRA_POWER_UP_TIME);
943 /* get complete HUB descriptor */
945 /* check number of ports */
947 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
948 "error=%s\n", usbd_errstr(err));
951 /* get complete HUB descriptor */
952 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
955 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
956 "error=%s\n", usbd_errstr(err));
959 if (hubdesc20.bNbrPorts != nports) {
960 DPRINTFN(0, "Number of ports changed\n");
965 case USB_SPEED_SUPER:
966 if (udev->parent_hub != NULL) {
967 err = usbd_req_set_hub_depth(udev, NULL,
970 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
971 "error=%s\n", usbd_errstr(err));
975 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
977 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
978 "error=%s\n", usbd_errstr(err));
981 /* get number of ports */
982 nports = hubdesc30.bNbrPorts;
984 /* get power delay */
985 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
986 USB_EXTRA_POWER_UP_TIME);
988 /* get complete HUB descriptor */
990 /* check number of ports */
991 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
992 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
993 "error=%s\n", usbd_errstr(err));
996 /* get complete HUB descriptor */
997 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1000 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1001 "error=%s\n", usbd_errstr(err));
1004 if (hubdesc30.bNbrPorts != nports) {
1005 DPRINTFN(0, "Number of ports changed\n");
1011 DPRINTF("Assuming HUB has only one port\n");
1012 /* default number of ports */
1014 /* default power delay */
1015 pwrdly = ((10 * UHD_PWRON_FACTOR) + USB_EXTRA_POWER_UP_TIME);
1019 DPRINTFN(0, "portless HUB\n");
1022 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1023 M_USBDEV, M_WAITOK | M_ZERO);
1030 #if USB_HAVE_TT_SUPPORT
1031 /* init FULL-speed ISOCHRONOUS schedule */
1032 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
1034 /* initialize HUB structure */
1036 hub->explore = &uhub_explore;
1037 hub->nports = nports;
1038 hub->hubudev = udev;
1040 /* if self powered hub, give ports maximum current */
1041 if (udev->flags.self_powered) {
1042 hub->portpower = USB_MAX_POWER;
1044 hub->portpower = USB_MIN_POWER;
1047 /* set up interrupt pipe */
1049 if (udev->parent_hub == NULL) {
1050 /* root HUB is special */
1054 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1055 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1058 DPRINTFN(0, "cannot setup interrupt transfer, "
1059 "errstr=%s\n", usbd_errstr(err));
1062 /* wait with power off for a while */
1063 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1066 * To have the best chance of success we do things in the exact same
1067 * order as Windoze98. This should not be necessary, but some
1068 * devices do not follow the USB specs to the letter.
1070 * These are the events on the bus when a hub is attached:
1071 * Get device and config descriptors (see attach code)
1072 * Get hub descriptor (see above)
1075 * wait for power to become stable
1076 * (all below happens in explore code)
1078 * clear C_PORT_CONNECTION
1081 * if device connected
1085 * clear C_PORT_RESET
1087 * proceed with device attachment
1090 /* XXX should check for none, individual, or ganged power? */
1094 for (x = 0; x != nports; x++) {
1095 /* set up data structures */
1096 struct usb_port *up = hub->ports + x;
1098 up->device_index = 0;
1102 /* check if port is removable */
1103 switch (udev->speed) {
1105 case USB_SPEED_FULL:
1106 case USB_SPEED_HIGH:
1107 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1110 case USB_SPEED_SUPER:
1111 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1115 DPRINTF("Assuming removable port\n");
1120 /* turn the power on */
1121 err = usbd_req_set_port_feature(udev, NULL,
1122 portno, UHF_PORT_POWER);
1125 DPRINTFN(0, "port %d power on failed, %s\n",
1126 portno, usbd_errstr(err));
1128 DPRINTF("turn on port %d power\n",
1131 /* wait for stable power */
1132 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1135 device_printf(dev, "%d port%s with %d "
1136 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1137 removable, udev->flags.self_powered ? "self" : "bus");
1139 /* Start the interrupt endpoint, if any */
1141 if (sc->sc_xfer[0] != NULL) {
1142 mtx_lock(&sc->sc_mtx);
1143 usbd_transfer_start(sc->sc_xfer[0]);
1144 mtx_unlock(&sc->sc_mtx);
1147 /* Enable automatic power save on all USB HUBs */
1149 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1154 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1157 free(udev->hub, M_USBDEV);
1161 mtx_destroy(&sc->sc_mtx);
1167 * Called from process context when the hub is gone.
1168 * Detach all devices on active ports.
1171 uhub_detach(device_t dev)
1173 struct uhub_softc *sc = device_get_softc(dev);
1174 struct usb_hub *hub = sc->sc_udev->hub;
1175 struct usb_device *child;
1178 if (hub == NULL) /* must be partially working */
1181 /* Make sure interrupt transfer is gone. */
1182 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1184 /* Detach all ports */
1185 for (x = 0; x != hub->nports; x++) {
1187 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1189 if (child == NULL) {
1194 * Free USB device and all subdevices, if any.
1196 usb_free_device(child, 0);
1199 free(hub, M_USBDEV);
1200 sc->sc_udev->hub = NULL;
1202 mtx_destroy(&sc->sc_mtx);
1208 uhub_suspend(device_t dev)
1211 /* Sub-devices are not suspended here! */
1216 uhub_resume(device_t dev)
1219 /* Sub-devices are not resumed here! */
1224 uhub_driver_added(device_t dev, driver_t *driver)
1226 usb_needs_explore_all();
1230 struct usb_device *udev;
1232 uint8_t iface_index;
1236 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1237 struct hub_result *res)
1239 struct usb_interface *iface;
1240 struct usb_device *udev;
1245 nports = hub->nports;
1246 for (x = 0; x != nports; x++) {
1247 udev = usb_bus_port_get_device(hub->hubudev->bus,
1252 for (i = 0; i != USB_IFACE_MAX; i++) {
1253 iface = usbd_get_iface(udev, i);
1255 (iface->subdev == child)) {
1256 res->iface_index = i;
1258 res->portno = x + 1;
1263 res->iface_index = 0;
1269 uhub_child_location_string(device_t parent, device_t child,
1270 char *buf, size_t buflen)
1272 struct uhub_softc *sc;
1273 struct usb_hub *hub;
1274 struct hub_result res;
1276 if (!device_is_attached(parent)) {
1282 sc = device_get_softc(parent);
1283 hub = sc->sc_udev->hub;
1286 uhub_find_iface_index(hub, child, &res);
1288 DPRINTF("device not on hub\n");
1294 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1295 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1296 res.portno, device_get_unit(res.udev->bus->bdev),
1297 res.udev->device_index, res.iface_index);
1305 uhub_child_pnpinfo_string(device_t parent, device_t child,
1306 char *buf, size_t buflen)
1308 struct uhub_softc *sc;
1309 struct usb_hub *hub;
1310 struct usb_interface *iface;
1311 struct hub_result res;
1313 if (!device_is_attached(parent)) {
1319 sc = device_get_softc(parent);
1320 hub = sc->sc_udev->hub;
1323 uhub_find_iface_index(hub, child, &res);
1325 DPRINTF("device not on hub\n");
1331 iface = usbd_get_iface(res.udev, res.iface_index);
1332 if (iface && iface->idesc) {
1333 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1334 "devclass=0x%02x devsubclass=0x%02x "
1338 "intclass=0x%02x intsubclass=0x%02x "
1339 "intprotocol=0x%02x " "%s%s",
1340 UGETW(res.udev->ddesc.idVendor),
1341 UGETW(res.udev->ddesc.idProduct),
1342 res.udev->ddesc.bDeviceClass,
1343 res.udev->ddesc.bDeviceSubClass,
1344 usb_get_serial(res.udev),
1345 UGETW(res.udev->ddesc.bcdDevice),
1346 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1347 iface->idesc->bInterfaceClass,
1348 iface->idesc->bInterfaceSubClass,
1349 iface->idesc->bInterfaceProtocol,
1350 iface->pnpinfo ? " " : "",
1351 iface->pnpinfo ? iface->pnpinfo : "");
1365 * The USB Transaction Translator:
1366 * ===============================
1368 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1369 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1370 * USB transfers. To utilize bandwidth dynamically the "scatter and
1371 * gather" principle must be applied. This means that bandwidth must
1372 * be divided into equal parts of bandwidth. With regard to USB all
1373 * data is transferred in smaller packets with length
1374 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1377 * The bandwidth scheduler which I have implemented will simply pack
1378 * the USB transfers back to back until there is no more space in the
1379 * schedule. Out of the 8 microframes which the USB 2.0 standard
1380 * provides, only 6 are available for non-HIGH-speed devices. I have
1381 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1382 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1383 * this division, it is very difficult to allocate and free bandwidth
1386 * NOTE about the Transaction Translator in USB HUBs:
1388 * USB HUBs have a very simple Transaction Translator, that will
1389 * simply pipeline all the SPLIT transactions. That means that the
1390 * transactions will be executed in the order they are queued!
1394 /*------------------------------------------------------------------------*
1395 * usb_intr_find_best_slot
1398 * The best Transaction Translation slot for an interrupt endpoint.
1399 *------------------------------------------------------------------------*/
1401 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1402 uint8_t end, uint8_t mask)
1404 usb_size_t min = 0 - 1;
1412 /* find the last slot with lesser used bandwidth */
1414 for (x = start; x < end; x++) {
1418 /* compute sum of bandwidth */
1419 for (z = x; z < end; z++) {
1420 if (mask & (1U << (z - x)))
1424 /* check if the current multi-slot is more optimal */
1430 /* check if the mask is about to be shifted out */
1431 if (mask & (1U << (end - 1 - x)))
1437 /*------------------------------------------------------------------------*
1438 * usb_hs_bandwidth_adjust
1440 * This function will update the bandwith usage for the microframe
1441 * having index "slot" by "len" bytes. "len" can be negative. If the
1442 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1443 * the "slot" argument will be replaced by the slot having least used
1444 * bandwidth. The "mask" argument is used for multi-slot allocations.
1447 * The slot in which the bandwidth update was done: 0..7
1448 *------------------------------------------------------------------------*/
1450 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1451 uint8_t slot, uint8_t mask)
1453 struct usb_bus *bus = udev->bus;
1454 struct usb_hub *hub;
1455 enum usb_dev_speed speed;
1458 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1460 speed = usbd_get_speed(udev);
1464 case USB_SPEED_FULL:
1465 if (speed == USB_SPEED_LOW) {
1469 * The Host Controller Driver should have
1470 * performed checks so that the lookup
1471 * below does not result in a NULL pointer
1475 hub = udev->parent_hs_hub->hub;
1476 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1477 slot = usb_intr_find_best_slot(hub->uframe_usage,
1478 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1480 for (x = slot; x < 8; x++) {
1481 if (mask & (1U << (x - slot))) {
1482 hub->uframe_usage[x] += len;
1483 bus->uframe_usage[x] += len;
1488 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1489 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1490 USB_HS_MICRO_FRAMES_MAX, mask);
1492 for (x = slot; x < 8; x++) {
1493 if (mask & (1U << (x - slot))) {
1494 bus->uframe_usage[x] += len;
1502 /*------------------------------------------------------------------------*
1503 * usb_hs_bandwidth_alloc
1505 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1506 *------------------------------------------------------------------------*/
1508 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1510 struct usb_device *udev;
1515 udev = xfer->xroot->udev;
1517 if (udev->flags.usb_mode != USB_MODE_HOST)
1518 return; /* not supported */
1520 xfer->endpoint->refcount_bw++;
1521 if (xfer->endpoint->refcount_bw != 1)
1522 return; /* already allocated */
1524 speed = usbd_get_speed(udev);
1526 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1528 /* allocate a microframe slot */
1531 slot = usb_hs_bandwidth_adjust(udev,
1532 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1534 xfer->endpoint->usb_uframe = slot;
1535 xfer->endpoint->usb_smask = mask << slot;
1537 if ((speed != USB_SPEED_FULL) &&
1538 (speed != USB_SPEED_LOW)) {
1539 xfer->endpoint->usb_cmask = 0x00 ;
1541 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1545 case UE_ISOCHRONOUS:
1546 switch (usbd_xfer_get_fps_shift(xfer)) {
1561 /* allocate a microframe multi-slot */
1563 slot = usb_hs_bandwidth_adjust(udev,
1564 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1566 xfer->endpoint->usb_uframe = slot;
1567 xfer->endpoint->usb_cmask = 0;
1568 xfer->endpoint->usb_smask = mask << slot;
1572 xfer->endpoint->usb_uframe = 0;
1573 xfer->endpoint->usb_cmask = 0;
1574 xfer->endpoint->usb_smask = 0;
1578 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1579 xfer->endpoint->usb_uframe,
1580 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1583 /*------------------------------------------------------------------------*
1584 * usb_hs_bandwidth_free
1586 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1587 *------------------------------------------------------------------------*/
1589 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1591 struct usb_device *udev;
1595 udev = xfer->xroot->udev;
1597 if (udev->flags.usb_mode != USB_MODE_HOST)
1598 return; /* not supported */
1600 xfer->endpoint->refcount_bw--;
1601 if (xfer->endpoint->refcount_bw != 0)
1602 return; /* still allocated */
1604 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1606 case UE_ISOCHRONOUS:
1608 slot = xfer->endpoint->usb_uframe;
1609 mask = xfer->endpoint->usb_smask;
1611 /* free microframe slot(s): */
1612 usb_hs_bandwidth_adjust(udev,
1613 -xfer->max_frame_size, slot, mask >> slot);
1615 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1616 slot, mask >> slot);
1618 xfer->endpoint->usb_uframe = 0;
1619 xfer->endpoint->usb_cmask = 0;
1620 xfer->endpoint->usb_smask = 0;
1628 /*------------------------------------------------------------------------*
1629 * usbd_fs_isoc_schedule_init_sub
1631 * This function initialises an USB FULL speed isochronous schedule
1633 *------------------------------------------------------------------------*/
1634 #if USB_HAVE_TT_SUPPORT
1636 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1638 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1639 USB_FS_BYTES_PER_HS_UFRAME);
1640 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1641 fss->frame_slot = 0;
1645 /*------------------------------------------------------------------------*
1646 * usbd_fs_isoc_schedule_init_all
1648 * This function will reset the complete USB FULL speed isochronous
1649 * bandwidth schedule.
1650 *------------------------------------------------------------------------*/
1651 #if USB_HAVE_TT_SUPPORT
1653 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1655 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1657 while (fss != fss_end) {
1658 usbd_fs_isoc_schedule_init_sub(fss);
1664 /*------------------------------------------------------------------------*
1665 * usb_isoc_time_expand
1667 * This function will expand the time counter from 7-bit to 16-bit.
1670 * 16-bit isochronous time counter.
1671 *------------------------------------------------------------------------*/
1673 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1677 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1679 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1681 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1683 if (isoc_time_curr < rem) {
1684 /* the time counter wrapped around */
1685 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1687 /* update the remainder */
1689 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1690 bus->isoc_time_last |= isoc_time_curr;
1692 return (bus->isoc_time_last);
1695 /*------------------------------------------------------------------------*
1696 * usbd_fs_isoc_schedule_isoc_time_expand
1698 * This function does multiple things. First of all it will expand the
1699 * passed isochronous time, which is the return value. Then it will
1700 * store where the current FULL speed isochronous schedule is
1701 * positioned in time and where the end is. See "pp_start" and
1702 * "pp_end" arguments.
1705 * Expanded version of "isoc_time".
1707 * NOTE: This function depends on being called regularly with
1708 * intervals less than "USB_ISOC_TIME_MAX".
1709 *------------------------------------------------------------------------*/
1710 #if USB_HAVE_TT_SUPPORT
1712 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1713 struct usb_fs_isoc_schedule **pp_start,
1714 struct usb_fs_isoc_schedule **pp_end,
1717 struct usb_fs_isoc_schedule *fss_end;
1718 struct usb_fs_isoc_schedule *fss_a;
1719 struct usb_fs_isoc_schedule *fss_b;
1720 struct usb_hub *hs_hub;
1722 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1724 hs_hub = udev->parent_hs_hub->hub;
1726 if (hs_hub != NULL) {
1728 fss_a = hs_hub->fs_isoc_schedule +
1729 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1731 hs_hub->isoc_last_time = isoc_time;
1733 fss_b = hs_hub->fs_isoc_schedule +
1734 (isoc_time % USB_ISOC_TIME_MAX);
1736 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1738 *pp_start = hs_hub->fs_isoc_schedule;
1741 while (fss_a != fss_b) {
1742 if (fss_a == fss_end) {
1743 fss_a = hs_hub->fs_isoc_schedule;
1746 usbd_fs_isoc_schedule_init_sub(fss_a);
1759 /*------------------------------------------------------------------------*
1760 * usbd_fs_isoc_schedule_alloc
1762 * This function will allocate bandwidth for an isochronous FULL speed
1763 * transaction in the FULL speed schedule. The microframe slot where
1764 * the transaction should be started is stored in the byte pointed to
1765 * by "pstart". The "len" argument specifies the length of the
1766 * transaction in bytes.
1771 *------------------------------------------------------------------------*/
1772 #if USB_HAVE_TT_SUPPORT
1774 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1775 uint8_t *pstart, uint16_t len)
1777 uint8_t slot = fss->frame_slot;
1779 /* Compute overhead and bit-stuffing */
1786 if (len > fss->total_bytes) {
1787 *pstart = 0; /* set some dummy value */
1788 return (1); /* error */
1792 fss->total_bytes -= len;
1794 while (len >= fss->frame_bytes) {
1795 len -= fss->frame_bytes;
1796 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1800 fss->frame_bytes -= len;
1803 return (0); /* success */
1807 /*------------------------------------------------------------------------*
1808 * usb_bus_port_get_device
1810 * This function is NULL safe.
1811 *------------------------------------------------------------------------*/
1813 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1815 if ((bus == NULL) || (up == NULL)) {
1819 if (up->device_index == 0) {
1823 return (bus->devices[up->device_index]);
1826 /*------------------------------------------------------------------------*
1827 * usb_bus_port_set_device
1829 * This function is NULL safe.
1830 *------------------------------------------------------------------------*/
1832 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1833 struct usb_device *udev, uint8_t device_index)
1840 * There is only one case where we don't
1841 * have an USB port, and that is the Root Hub!
1845 up->device_index = device_index;
1847 device_index = up->device_index;
1848 up->device_index = 0;
1852 * Make relationships to our new device
1854 if (device_index != 0) {
1856 mtx_lock(&usb_ref_lock);
1858 bus->devices[device_index] = udev;
1860 mtx_unlock(&usb_ref_lock);
1866 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1869 /*------------------------------------------------------------------------*
1872 * This functions is called when the USB event thread needs to run.
1873 *------------------------------------------------------------------------*/
1875 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1882 DPRINTF("No bus pointer!\n");
1885 if ((bus->devices == NULL) ||
1886 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1887 DPRINTF("No root HUB\n");
1890 if (mtx_owned(&bus->bus_mtx)) {
1899 if (usb_proc_msignal(&bus->explore_proc,
1900 &bus->explore_msg[0], &bus->explore_msg[1])) {
1904 USB_BUS_UNLOCK(bus);
1908 /*------------------------------------------------------------------------*
1909 * usb_needs_explore_all
1911 * This function is called whenever a new driver is loaded and will
1912 * cause that all USB busses are re-explored.
1913 *------------------------------------------------------------------------*/
1915 usb_needs_explore_all(void)
1917 struct usb_bus *bus;
1924 dc = usb_devclass_ptr;
1926 DPRINTFN(0, "no devclass\n");
1930 * Explore all USB busses in parallell.
1932 max = devclass_get_maxunit(dc);
1934 dev = devclass_get_device(dc, max);
1936 bus = device_get_softc(dev);
1938 usb_needs_explore(bus, 1);
1945 /*------------------------------------------------------------------------*
1946 * usb_bus_power_update
1948 * This function will ensure that all USB devices on the given bus are
1949 * properly suspended or resumed according to the device transfer
1951 *------------------------------------------------------------------------*/
1954 usb_bus_power_update(struct usb_bus *bus)
1956 usb_needs_explore(bus, 0 /* no probe */ );
1960 /*------------------------------------------------------------------------*
1961 * usbd_transfer_power_ref
1963 * This function will modify the power save reference counts and
1964 * wakeup the USB device associated with the given USB transfer, if
1966 *------------------------------------------------------------------------*/
1969 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1971 static const usb_power_mask_t power_mask[4] = {
1972 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1973 [UE_BULK] = USB_HW_POWER_BULK,
1974 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1975 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1977 struct usb_device *udev;
1978 uint8_t needs_explore;
1979 uint8_t needs_hw_power;
1982 udev = xfer->xroot->udev;
1984 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1985 /* no power save for root HUB */
1988 USB_BUS_LOCK(udev->bus);
1990 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1992 udev->pwr_save.last_xfer_time = ticks;
1993 udev->pwr_save.type_refs[xfer_type] += val;
1995 if (xfer->flags_int.control_xfr) {
1996 udev->pwr_save.read_refs += val;
1997 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1999 * It is not allowed to suspend during a
2002 udev->pwr_save.write_refs += val;
2004 } else if (USB_GET_DATA_ISREAD(xfer)) {
2005 udev->pwr_save.read_refs += val;
2007 udev->pwr_save.write_refs += val;
2011 if (udev->flags.self_suspended)
2012 needs_explore = usb_peer_should_wakeup(udev);
2016 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2017 DPRINTF("Adding type %u to power state\n", xfer_type);
2018 udev->bus->hw_power_state |= power_mask[xfer_type];
2028 USB_BUS_UNLOCK(udev->bus);
2030 if (needs_explore) {
2031 DPRINTF("update\n");
2032 usb_bus_power_update(udev->bus);
2033 } else if (needs_hw_power) {
2034 DPRINTF("needs power\n");
2035 if (udev->bus->methods->set_hw_power != NULL) {
2036 (udev->bus->methods->set_hw_power) (udev->bus);
2042 /*------------------------------------------------------------------------*
2043 * usb_peer_should_wakeup
2045 * This function returns non-zero if the current device should wake up.
2046 *------------------------------------------------------------------------*/
2048 usb_peer_should_wakeup(struct usb_device *udev)
2050 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2051 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2052 (udev->re_enumerate_wait != 0) ||
2053 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2054 (udev->pwr_save.write_refs != 0) ||
2055 ((udev->pwr_save.read_refs != 0) &&
2056 (udev->flags.usb_mode == USB_MODE_HOST) &&
2057 (usb_device_20_compatible(udev) != 0) &&
2058 (usb_peer_can_wakeup(udev) == 0)));
2061 /*------------------------------------------------------------------------*
2064 * This function implements the USB power daemon and is called
2065 * regularly from the USB explore thread.
2066 *------------------------------------------------------------------------*/
2069 usb_bus_powerd(struct usb_bus *bus)
2071 struct usb_device *udev;
2074 usb_ticks_t mintime;
2075 usb_size_t type_refs[5];
2078 limit = usb_power_timeout;
2081 else if (limit > 255)
2086 DPRINTF("bus=%p\n", bus);
2091 * The root HUB device is never suspended
2092 * and we simply skip it.
2094 for (x = USB_ROOT_HUB_ADDR + 1;
2095 x != bus->devices_max; x++) {
2097 udev = bus->devices[x];
2101 temp = ticks - udev->pwr_save.last_xfer_time;
2103 if (usb_peer_should_wakeup(udev)) {
2104 /* check if we are suspended */
2105 if (udev->flags.self_suspended != 0) {
2106 USB_BUS_UNLOCK(bus);
2107 usb_dev_resume_peer(udev);
2110 } else if ((temp >= limit) &&
2111 (udev->flags.usb_mode == USB_MODE_HOST) &&
2112 (udev->flags.self_suspended == 0)) {
2113 /* try to do suspend */
2115 USB_BUS_UNLOCK(bus);
2116 usb_dev_suspend_peer(udev);
2121 /* reset counters */
2130 /* Re-loop all the devices to get the actual state */
2132 for (x = USB_ROOT_HUB_ADDR + 1;
2133 x != bus->devices_max; x++) {
2135 udev = bus->devices[x];
2139 /* we found a non-Root-Hub USB device */
2142 /* "last_xfer_time" can be updated by a resume */
2143 temp = ticks - udev->pwr_save.last_xfer_time;
2146 * Compute minimum time since last transfer for the complete
2152 if (udev->flags.self_suspended == 0) {
2153 type_refs[0] += udev->pwr_save.type_refs[0];
2154 type_refs[1] += udev->pwr_save.type_refs[1];
2155 type_refs[2] += udev->pwr_save.type_refs[2];
2156 type_refs[3] += udev->pwr_save.type_refs[3];
2160 if (mintime >= (1 * hz)) {
2161 /* recompute power masks */
2162 DPRINTF("Recomputing power masks\n");
2163 bus->hw_power_state = 0;
2164 if (type_refs[UE_CONTROL] != 0)
2165 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2166 if (type_refs[UE_BULK] != 0)
2167 bus->hw_power_state |= USB_HW_POWER_BULK;
2168 if (type_refs[UE_INTERRUPT] != 0)
2169 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2170 if (type_refs[UE_ISOCHRONOUS] != 0)
2171 bus->hw_power_state |= USB_HW_POWER_ISOC;
2172 if (type_refs[4] != 0)
2173 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2175 USB_BUS_UNLOCK(bus);
2177 if (bus->methods->set_hw_power != NULL) {
2178 /* always update hardware power! */
2179 (bus->methods->set_hw_power) (bus);
2185 /*------------------------------------------------------------------------*
2186 * usb_dev_resume_peer
2188 * This function will resume an USB peer and do the required USB
2189 * signalling to get an USB device out of the suspended state.
2190 *------------------------------------------------------------------------*/
2192 usb_dev_resume_peer(struct usb_device *udev)
2194 struct usb_bus *bus;
2201 /* check if already resumed */
2202 if (udev->flags.self_suspended == 0)
2205 /* we need a parent HUB to do resume */
2206 if (udev->parent_hub == NULL)
2209 DPRINTF("udev=%p\n", udev);
2211 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2212 (udev->flags.remote_wakeup == 0)) {
2214 * If the host did not set the remote wakeup feature, we can
2215 * not wake it up either!
2217 DPRINTF("remote wakeup is not set!\n");
2220 /* get bus pointer */
2223 /* resume parent hub first */
2224 usb_dev_resume_peer(udev->parent_hub);
2226 /* reduce chance of instant resume failure by waiting a little bit */
2227 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2229 if (usb_device_20_compatible(udev)) {
2230 /* resume current port (Valid in Host and Device Mode) */
2231 err = usbd_req_clear_port_feature(udev->parent_hub,
2232 NULL, udev->port_no, UHF_PORT_SUSPEND);
2234 DPRINTFN(0, "Resuming port failed\n");
2239 /* resume settle time */
2240 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2242 if (bus->methods->device_resume != NULL) {
2243 /* resume USB device on the USB controller */
2244 (bus->methods->device_resume) (udev);
2247 /* set that this device is now resumed */
2248 udev->flags.self_suspended = 0;
2250 /* make sure that we don't go into suspend right away */
2251 udev->pwr_save.last_xfer_time = ticks;
2253 /* make sure the needed power masks are on */
2254 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2255 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2256 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2257 bus->hw_power_state |= USB_HW_POWER_BULK;
2258 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2259 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2260 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2261 bus->hw_power_state |= USB_HW_POWER_ISOC;
2263 USB_BUS_UNLOCK(bus);
2265 if (bus->methods->set_hw_power != NULL) {
2266 /* always update hardware power! */
2267 (bus->methods->set_hw_power) (bus);
2272 /* notify all sub-devices about resume */
2273 err = usb_suspend_resume(udev, 0);
2275 usbd_sr_unlock(udev);
2277 /* check if peer has wakeup capability */
2278 if (usb_peer_can_wakeup(udev) &&
2279 usb_device_20_compatible(udev)) {
2280 /* clear remote wakeup */
2281 err = usbd_req_clear_device_feature(udev,
2282 NULL, UF_DEVICE_REMOTE_WAKEUP);
2284 DPRINTFN(0, "Clearing device "
2285 "remote wakeup failed: %s\n",
2291 /*------------------------------------------------------------------------*
2292 * usb_dev_suspend_peer
2294 * This function will suspend an USB peer and do the required USB
2295 * signalling to get an USB device into the suspended state.
2296 *------------------------------------------------------------------------*/
2298 usb_dev_suspend_peer(struct usb_device *udev)
2300 struct usb_device *child;
2310 /* check if already suspended */
2311 if (udev->flags.self_suspended)
2314 /* we need a parent HUB to do suspend */
2315 if (udev->parent_hub == NULL)
2318 DPRINTF("udev=%p\n", udev);
2320 /* check if the current device is a HUB */
2321 if (udev->hub != NULL) {
2322 nports = udev->hub->nports;
2324 /* check if all devices on the HUB are suspended */
2325 for (x = 0; x != nports; x++) {
2326 child = usb_bus_port_get_device(udev->bus,
2327 udev->hub->ports + x);
2332 if (child->flags.self_suspended)
2335 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2340 if (usb_peer_can_wakeup(udev) &&
2341 usb_device_20_compatible(udev)) {
2343 * This request needs to be done before we set
2344 * "udev->flags.self_suspended":
2347 /* allow device to do remote wakeup */
2348 err = usbd_req_set_device_feature(udev,
2349 NULL, UF_DEVICE_REMOTE_WAKEUP);
2351 DPRINTFN(0, "Setting device "
2352 "remote wakeup failed\n");
2356 USB_BUS_LOCK(udev->bus);
2358 * Checking for suspend condition and setting suspended bit
2361 err = usb_peer_should_wakeup(udev);
2364 * Set that this device is suspended. This variable
2365 * must be set before calling USB controller suspend
2368 udev->flags.self_suspended = 1;
2370 USB_BUS_UNLOCK(udev->bus);
2373 if (usb_peer_can_wakeup(udev) &&
2374 usb_device_20_compatible(udev)) {
2375 /* allow device to do remote wakeup */
2376 err = usbd_req_clear_device_feature(udev,
2377 NULL, UF_DEVICE_REMOTE_WAKEUP);
2379 DPRINTFN(0, "Setting device "
2380 "remote wakeup failed\n");
2384 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2385 /* resume parent HUB first */
2386 usb_dev_resume_peer(udev->parent_hub);
2388 /* reduce chance of instant resume failure by waiting a little bit */
2389 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2391 /* resume current port (Valid in Host and Device Mode) */
2392 err = usbd_req_clear_port_feature(udev->parent_hub,
2393 NULL, udev->port_no, UHF_PORT_SUSPEND);
2395 /* resume settle time */
2396 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2398 DPRINTF("Suspend was cancelled!\n");
2404 /* notify all sub-devices about suspend */
2405 err = usb_suspend_resume(udev, 1);
2407 usbd_sr_unlock(udev);
2409 if (udev->bus->methods->device_suspend != NULL) {
2412 /* suspend device on the USB controller */
2413 (udev->bus->methods->device_suspend) (udev);
2416 temp = usbd_get_dma_delay(udev);
2418 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2422 if (usb_device_20_compatible(udev)) {
2423 /* suspend current port */
2424 err = usbd_req_set_port_feature(udev->parent_hub,
2425 NULL, udev->port_no, UHF_PORT_SUSPEND);
2427 DPRINTFN(0, "Suspending port failed\n");
2432 udev = udev->parent_hub;
2436 /*------------------------------------------------------------------------*
2437 * usbd_set_power_mode
2439 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2441 *------------------------------------------------------------------------*/
2443 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2445 /* filter input argument */
2446 if ((power_mode != USB_POWER_MODE_ON) &&
2447 (power_mode != USB_POWER_MODE_OFF))
2448 power_mode = USB_POWER_MODE_SAVE;
2450 power_mode = usbd_filter_power_mode(udev, power_mode);
2452 udev->power_mode = power_mode; /* update copy of power mode */
2455 usb_bus_power_update(udev->bus);
2459 /*------------------------------------------------------------------------*
2460 * usbd_filter_power_mode
2462 * This function filters the power mode based on hardware requirements.
2463 *------------------------------------------------------------------------*/
2465 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2467 struct usb_bus_methods *mtod;
2470 mtod = udev->bus->methods;
2473 if (mtod->get_power_mode != NULL)
2474 (mtod->get_power_mode) (udev, &temp);
2476 /* check if we should not filter */
2478 return (power_mode);
2480 /* use fixed power mode given by hardware driver */
2484 /*------------------------------------------------------------------------*
2485 * usbd_start_re_enumerate
2487 * This function starts re-enumeration of the given USB device. This
2488 * function does not need to be called BUS-locked. This function does
2489 * not wait until the re-enumeration is completed.
2490 *------------------------------------------------------------------------*/
2492 usbd_start_re_enumerate(struct usb_device *udev)
2494 if (udev->re_enumerate_wait == 0) {
2495 udev->re_enumerate_wait = 1;
2496 usb_needs_explore(udev->bus, 0);