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)) {
614 case UPS_PORT_LS_RESUME:
623 DPRINTF("suspended=%u\n", is_suspend);
625 /* do the suspend or resume */
629 * This code handle two cases: 1) Host Mode - we can only
630 * receive resume here 2) Device Mode - we can receive
631 * suspend and resume here
634 usb_dev_resume_peer(child);
635 else if ((child->flags.usb_mode == USB_MODE_DEVICE) ||
636 (usb_device_20_compatible(child) == 0))
637 usb_dev_suspend_peer(child);
643 /*------------------------------------------------------------------------*
644 * uhub_root_interrupt
646 * This function is called when a Root HUB interrupt has
647 * happened. "ptr" and "len" makes up the Root HUB interrupt
648 * packet. This function is called having the "bus_mtx" locked.
649 *------------------------------------------------------------------------*/
651 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
653 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
655 usb_needs_explore(bus, 0);
659 uhub_is_too_deep(struct usb_device *udev)
661 switch (udev->speed) {
665 if (udev->depth > USB_HUB_MAX_DEPTH)
668 case USB_SPEED_SUPER:
669 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
678 /*------------------------------------------------------------------------*
684 *------------------------------------------------------------------------*/
686 uhub_explore(struct usb_device *udev)
689 struct uhub_softc *sc;
698 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
700 /* ignore devices that are too deep */
701 if (uhub_is_too_deep(udev))
702 return (USB_ERR_TOO_DEEP);
704 /* check if device is suspended */
705 if (udev->flags.self_suspended) {
706 /* need to wait until the child signals resume */
707 DPRINTF("Device is suspended!\n");
712 * Make sure we don't race against user-space applications
715 usbd_enum_lock(udev);
717 for (x = 0; x != hub->nports; x++) {
721 err = uhub_read_port_status(sc, portno);
723 /* most likely the HUB is gone */
726 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
727 DPRINTF("Overcurrent on port %u.\n", portno);
728 err = usbd_req_clear_port_feature(
729 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
731 /* most likely the HUB is gone */
735 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
737 * Fake a connect status change so that the
738 * status gets checked initially!
740 sc->sc_st.port_change |=
741 UPS_C_CONNECT_STATUS;
743 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
744 err = usbd_req_clear_port_feature(
745 udev, NULL, portno, UHF_C_PORT_ENABLE);
747 /* most likely the HUB is gone */
750 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
752 * Ignore the port error if the device
755 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
756 DPRINTFN(0, "illegal enable change, "
757 "port %d\n", portno);
760 if (up->restartcnt == USB_RESTART_MAX) {
761 /* XXX could try another speed ? */
762 DPRINTFN(0, "port error, giving up "
763 "port %d\n", portno);
765 sc->sc_st.port_change |=
766 UPS_C_CONNECT_STATUS;
771 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
772 err = uhub_reattach_port(sc, portno);
774 /* most likely the HUB is gone */
778 if (sc->sc_st.port_change & (UPS_C_SUSPEND | UPS_C_PORT_LINK_STATE)) {
779 err = uhub_suspend_resume_port(sc, portno);
781 /* most likely the HUB is gone */
785 err = uhub_explore_sub(sc, up);
787 /* no device(s) present */
790 /* explore succeeded - reset restart counter */
794 usbd_enum_unlock(udev);
796 /* initial status checked */
797 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
800 return (USB_ERR_NORMAL_COMPLETION);
804 uhub_probe(device_t dev)
806 struct usb_attach_arg *uaa = device_get_ivars(dev);
808 if (uaa->usb_mode != USB_MODE_HOST)
812 * The subclass for USB HUBs is currently ignored because it
813 * is 0 for some and 1 for others.
815 if (uaa->info.bConfigIndex == 0 &&
816 uaa->info.bDeviceClass == UDCLASS_HUB)
822 /* NOTE: The information returned by this function can be wrong. */
824 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
826 struct usb_hub_descriptor hubdesc20;
827 struct usb_hub_ss_descriptor hubdesc30;
832 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
833 return (USB_ERR_INVAL);
838 switch (udev->speed) {
842 /* assuming that there is one port */
843 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
845 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
846 "error=%s\n", usbd_errstr(err));
849 nports = hubdesc20.bNbrPorts;
853 if (udev->speed == USB_SPEED_HIGH)
854 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
857 case USB_SPEED_SUPER:
858 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
860 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
861 "error=%s\n", usbd_errstr(err));
864 nports = hubdesc30.bNbrPorts;
884 uhub_attach(device_t dev)
886 struct uhub_softc *sc = device_get_softc(dev);
887 struct usb_attach_arg *uaa = device_get_ivars(dev);
888 struct usb_device *udev = uaa->device;
889 struct usb_device *parent_hub = udev->parent_hub;
891 struct usb_hub_descriptor hubdesc20;
892 struct usb_hub_ss_descriptor hubdesc30;
904 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
906 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
907 device_get_nameunit(dev));
909 device_set_usb_desc(dev);
911 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
912 "parent->selfpowered=%d\n",
914 udev->flags.self_powered,
917 parent_hub->flags.self_powered : 0);
919 if (uhub_is_too_deep(udev)) {
920 DPRINTFN(0, "HUB at depth %d, "
921 "exceeds maximum. HUB ignored\n", (int)udev->depth);
925 if (!udev->flags.self_powered && parent_hub &&
926 !parent_hub->flags.self_powered) {
927 DPRINTFN(0, "Bus powered HUB connected to "
928 "bus powered HUB. HUB ignored\n");
931 /* get HUB descriptor */
933 DPRINTFN(2, "Getting HUB descriptor\n");
935 switch (udev->speed) {
939 /* assuming that there is one port */
940 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
942 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
943 "error=%s\n", usbd_errstr(err));
946 /* get number of ports */
947 nports = hubdesc20.bNbrPorts;
949 /* get power delay */
950 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
951 USB_EXTRA_POWER_UP_TIME);
953 /* get complete HUB descriptor */
955 /* check number of ports */
957 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
958 "error=%s\n", usbd_errstr(err));
961 /* get complete HUB descriptor */
962 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
965 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
966 "error=%s\n", usbd_errstr(err));
969 if (hubdesc20.bNbrPorts != nports) {
970 DPRINTFN(0, "Number of ports changed\n");
975 case USB_SPEED_SUPER:
976 if (udev->parent_hub != NULL) {
977 err = usbd_req_set_hub_depth(udev, NULL,
980 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
981 "error=%s\n", usbd_errstr(err));
985 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
987 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
988 "error=%s\n", usbd_errstr(err));
991 /* get number of ports */
992 nports = hubdesc30.bNbrPorts;
994 /* get power delay */
995 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
996 USB_EXTRA_POWER_UP_TIME);
998 /* get complete HUB descriptor */
1000 /* check number of ports */
1001 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1002 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1003 "error=%s\n", usbd_errstr(err));
1006 /* get complete HUB descriptor */
1007 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1010 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1011 "error=%s\n", usbd_errstr(err));
1014 if (hubdesc30.bNbrPorts != nports) {
1015 DPRINTFN(0, "Number of ports changed\n");
1021 DPRINTF("Assuming HUB has only one port\n");
1022 /* default number of ports */
1024 /* default power delay */
1025 pwrdly = ((10 * UHD_PWRON_FACTOR) + USB_EXTRA_POWER_UP_TIME);
1029 DPRINTFN(0, "portless HUB\n");
1032 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1033 M_USBDEV, M_WAITOK | M_ZERO);
1040 #if USB_HAVE_TT_SUPPORT
1041 /* init FULL-speed ISOCHRONOUS schedule */
1042 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
1044 /* initialize HUB structure */
1046 hub->explore = &uhub_explore;
1047 hub->nports = nports;
1048 hub->hubudev = udev;
1050 /* if self powered hub, give ports maximum current */
1051 if (udev->flags.self_powered) {
1052 hub->portpower = USB_MAX_POWER;
1054 hub->portpower = USB_MIN_POWER;
1057 /* set up interrupt pipe */
1059 if (udev->parent_hub == NULL) {
1060 /* root HUB is special */
1064 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1065 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1068 DPRINTFN(0, "cannot setup interrupt transfer, "
1069 "errstr=%s\n", usbd_errstr(err));
1072 /* wait with power off for a while */
1073 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1076 * To have the best chance of success we do things in the exact same
1077 * order as Windoze98. This should not be necessary, but some
1078 * devices do not follow the USB specs to the letter.
1080 * These are the events on the bus when a hub is attached:
1081 * Get device and config descriptors (see attach code)
1082 * Get hub descriptor (see above)
1085 * wait for power to become stable
1086 * (all below happens in explore code)
1088 * clear C_PORT_CONNECTION
1091 * if device connected
1095 * clear C_PORT_RESET
1097 * proceed with device attachment
1100 /* XXX should check for none, individual, or ganged power? */
1104 for (x = 0; x != nports; x++) {
1105 /* set up data structures */
1106 struct usb_port *up = hub->ports + x;
1108 up->device_index = 0;
1112 /* check if port is removable */
1113 switch (udev->speed) {
1115 case USB_SPEED_FULL:
1116 case USB_SPEED_HIGH:
1117 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1120 case USB_SPEED_SUPER:
1121 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1125 DPRINTF("Assuming removable port\n");
1130 /* turn the power on */
1131 err = usbd_req_set_port_feature(udev, NULL,
1132 portno, UHF_PORT_POWER);
1135 DPRINTFN(0, "port %d power on failed, %s\n",
1136 portno, usbd_errstr(err));
1138 DPRINTF("turn on port %d power\n",
1141 /* wait for stable power */
1142 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1145 device_printf(dev, "%d port%s with %d "
1146 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1147 removable, udev->flags.self_powered ? "self" : "bus");
1149 /* Start the interrupt endpoint, if any */
1151 if (sc->sc_xfer[0] != NULL) {
1152 mtx_lock(&sc->sc_mtx);
1153 usbd_transfer_start(sc->sc_xfer[0]);
1154 mtx_unlock(&sc->sc_mtx);
1157 /* Enable automatic power save on all USB HUBs */
1159 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1164 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1167 free(udev->hub, M_USBDEV);
1171 mtx_destroy(&sc->sc_mtx);
1177 * Called from process context when the hub is gone.
1178 * Detach all devices on active ports.
1181 uhub_detach(device_t dev)
1183 struct uhub_softc *sc = device_get_softc(dev);
1184 struct usb_hub *hub = sc->sc_udev->hub;
1185 struct usb_device *child;
1188 if (hub == NULL) /* must be partially working */
1191 /* Make sure interrupt transfer is gone. */
1192 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1194 /* Detach all ports */
1195 for (x = 0; x != hub->nports; x++) {
1197 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1199 if (child == NULL) {
1204 * Free USB device and all subdevices, if any.
1206 usb_free_device(child, 0);
1209 free(hub, M_USBDEV);
1210 sc->sc_udev->hub = NULL;
1212 mtx_destroy(&sc->sc_mtx);
1218 uhub_suspend(device_t dev)
1221 /* Sub-devices are not suspended here! */
1226 uhub_resume(device_t dev)
1229 /* Sub-devices are not resumed here! */
1234 uhub_driver_added(device_t dev, driver_t *driver)
1236 usb_needs_explore_all();
1240 struct usb_device *udev;
1242 uint8_t iface_index;
1246 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1247 struct hub_result *res)
1249 struct usb_interface *iface;
1250 struct usb_device *udev;
1255 nports = hub->nports;
1256 for (x = 0; x != nports; x++) {
1257 udev = usb_bus_port_get_device(hub->hubudev->bus,
1262 for (i = 0; i != USB_IFACE_MAX; i++) {
1263 iface = usbd_get_iface(udev, i);
1265 (iface->subdev == child)) {
1266 res->iface_index = i;
1268 res->portno = x + 1;
1273 res->iface_index = 0;
1279 uhub_child_location_string(device_t parent, device_t child,
1280 char *buf, size_t buflen)
1282 struct uhub_softc *sc;
1283 struct usb_hub *hub;
1284 struct hub_result res;
1286 if (!device_is_attached(parent)) {
1292 sc = device_get_softc(parent);
1293 hub = sc->sc_udev->hub;
1296 uhub_find_iface_index(hub, child, &res);
1298 DPRINTF("device not on hub\n");
1304 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1305 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1306 res.portno, device_get_unit(res.udev->bus->bdev),
1307 res.udev->device_index, res.iface_index);
1315 uhub_child_pnpinfo_string(device_t parent, device_t child,
1316 char *buf, size_t buflen)
1318 struct uhub_softc *sc;
1319 struct usb_hub *hub;
1320 struct usb_interface *iface;
1321 struct hub_result res;
1323 if (!device_is_attached(parent)) {
1329 sc = device_get_softc(parent);
1330 hub = sc->sc_udev->hub;
1333 uhub_find_iface_index(hub, child, &res);
1335 DPRINTF("device not on hub\n");
1341 iface = usbd_get_iface(res.udev, res.iface_index);
1342 if (iface && iface->idesc) {
1343 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1344 "devclass=0x%02x devsubclass=0x%02x "
1348 "intclass=0x%02x intsubclass=0x%02x "
1349 "intprotocol=0x%02x " "%s%s",
1350 UGETW(res.udev->ddesc.idVendor),
1351 UGETW(res.udev->ddesc.idProduct),
1352 res.udev->ddesc.bDeviceClass,
1353 res.udev->ddesc.bDeviceSubClass,
1354 usb_get_serial(res.udev),
1355 UGETW(res.udev->ddesc.bcdDevice),
1356 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1357 iface->idesc->bInterfaceClass,
1358 iface->idesc->bInterfaceSubClass,
1359 iface->idesc->bInterfaceProtocol,
1360 iface->pnpinfo ? " " : "",
1361 iface->pnpinfo ? iface->pnpinfo : "");
1375 * The USB Transaction Translator:
1376 * ===============================
1378 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1379 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1380 * USB transfers. To utilize bandwidth dynamically the "scatter and
1381 * gather" principle must be applied. This means that bandwidth must
1382 * be divided into equal parts of bandwidth. With regard to USB all
1383 * data is transferred in smaller packets with length
1384 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1387 * The bandwidth scheduler which I have implemented will simply pack
1388 * the USB transfers back to back until there is no more space in the
1389 * schedule. Out of the 8 microframes which the USB 2.0 standard
1390 * provides, only 6 are available for non-HIGH-speed devices. I have
1391 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1392 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1393 * this division, it is very difficult to allocate and free bandwidth
1396 * NOTE about the Transaction Translator in USB HUBs:
1398 * USB HUBs have a very simple Transaction Translator, that will
1399 * simply pipeline all the SPLIT transactions. That means that the
1400 * transactions will be executed in the order they are queued!
1404 /*------------------------------------------------------------------------*
1405 * usb_intr_find_best_slot
1408 * The best Transaction Translation slot for an interrupt endpoint.
1409 *------------------------------------------------------------------------*/
1411 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1412 uint8_t end, uint8_t mask)
1414 usb_size_t min = 0 - 1;
1422 /* find the last slot with lesser used bandwidth */
1424 for (x = start; x < end; x++) {
1428 /* compute sum of bandwidth */
1429 for (z = x; z < end; z++) {
1430 if (mask & (1U << (z - x)))
1434 /* check if the current multi-slot is more optimal */
1440 /* check if the mask is about to be shifted out */
1441 if (mask & (1U << (end - 1 - x)))
1447 /*------------------------------------------------------------------------*
1448 * usb_hs_bandwidth_adjust
1450 * This function will update the bandwith usage for the microframe
1451 * having index "slot" by "len" bytes. "len" can be negative. If the
1452 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1453 * the "slot" argument will be replaced by the slot having least used
1454 * bandwidth. The "mask" argument is used for multi-slot allocations.
1457 * The slot in which the bandwidth update was done: 0..7
1458 *------------------------------------------------------------------------*/
1460 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1461 uint8_t slot, uint8_t mask)
1463 struct usb_bus *bus = udev->bus;
1464 struct usb_hub *hub;
1465 enum usb_dev_speed speed;
1468 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1470 speed = usbd_get_speed(udev);
1474 case USB_SPEED_FULL:
1475 if (speed == USB_SPEED_LOW) {
1479 * The Host Controller Driver should have
1480 * performed checks so that the lookup
1481 * below does not result in a NULL pointer
1485 hub = udev->parent_hs_hub->hub;
1486 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1487 slot = usb_intr_find_best_slot(hub->uframe_usage,
1488 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1490 for (x = slot; x < 8; x++) {
1491 if (mask & (1U << (x - slot))) {
1492 hub->uframe_usage[x] += len;
1493 bus->uframe_usage[x] += len;
1498 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1499 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1500 USB_HS_MICRO_FRAMES_MAX, mask);
1502 for (x = slot; x < 8; x++) {
1503 if (mask & (1U << (x - slot))) {
1504 bus->uframe_usage[x] += len;
1512 /*------------------------------------------------------------------------*
1513 * usb_hs_bandwidth_alloc
1515 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1516 *------------------------------------------------------------------------*/
1518 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1520 struct usb_device *udev;
1525 udev = xfer->xroot->udev;
1527 if (udev->flags.usb_mode != USB_MODE_HOST)
1528 return; /* not supported */
1530 xfer->endpoint->refcount_bw++;
1531 if (xfer->endpoint->refcount_bw != 1)
1532 return; /* already allocated */
1534 speed = usbd_get_speed(udev);
1536 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1538 /* allocate a microframe slot */
1541 slot = usb_hs_bandwidth_adjust(udev,
1542 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1544 xfer->endpoint->usb_uframe = slot;
1545 xfer->endpoint->usb_smask = mask << slot;
1547 if ((speed != USB_SPEED_FULL) &&
1548 (speed != USB_SPEED_LOW)) {
1549 xfer->endpoint->usb_cmask = 0x00 ;
1551 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1555 case UE_ISOCHRONOUS:
1556 switch (usbd_xfer_get_fps_shift(xfer)) {
1571 /* allocate a microframe multi-slot */
1573 slot = usb_hs_bandwidth_adjust(udev,
1574 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1576 xfer->endpoint->usb_uframe = slot;
1577 xfer->endpoint->usb_cmask = 0;
1578 xfer->endpoint->usb_smask = mask << slot;
1582 xfer->endpoint->usb_uframe = 0;
1583 xfer->endpoint->usb_cmask = 0;
1584 xfer->endpoint->usb_smask = 0;
1588 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1589 xfer->endpoint->usb_uframe,
1590 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1593 /*------------------------------------------------------------------------*
1594 * usb_hs_bandwidth_free
1596 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1597 *------------------------------------------------------------------------*/
1599 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1601 struct usb_device *udev;
1605 udev = xfer->xroot->udev;
1607 if (udev->flags.usb_mode != USB_MODE_HOST)
1608 return; /* not supported */
1610 xfer->endpoint->refcount_bw--;
1611 if (xfer->endpoint->refcount_bw != 0)
1612 return; /* still allocated */
1614 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1616 case UE_ISOCHRONOUS:
1618 slot = xfer->endpoint->usb_uframe;
1619 mask = xfer->endpoint->usb_smask;
1621 /* free microframe slot(s): */
1622 usb_hs_bandwidth_adjust(udev,
1623 -xfer->max_frame_size, slot, mask >> slot);
1625 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1626 slot, mask >> slot);
1628 xfer->endpoint->usb_uframe = 0;
1629 xfer->endpoint->usb_cmask = 0;
1630 xfer->endpoint->usb_smask = 0;
1638 /*------------------------------------------------------------------------*
1639 * usbd_fs_isoc_schedule_init_sub
1641 * This function initialises an USB FULL speed isochronous schedule
1643 *------------------------------------------------------------------------*/
1644 #if USB_HAVE_TT_SUPPORT
1646 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1648 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1649 USB_FS_BYTES_PER_HS_UFRAME);
1650 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1651 fss->frame_slot = 0;
1655 /*------------------------------------------------------------------------*
1656 * usbd_fs_isoc_schedule_init_all
1658 * This function will reset the complete USB FULL speed isochronous
1659 * bandwidth schedule.
1660 *------------------------------------------------------------------------*/
1661 #if USB_HAVE_TT_SUPPORT
1663 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1665 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1667 while (fss != fss_end) {
1668 usbd_fs_isoc_schedule_init_sub(fss);
1674 /*------------------------------------------------------------------------*
1675 * usb_isoc_time_expand
1677 * This function will expand the time counter from 7-bit to 16-bit.
1680 * 16-bit isochronous time counter.
1681 *------------------------------------------------------------------------*/
1683 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1687 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1689 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1691 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1693 if (isoc_time_curr < rem) {
1694 /* the time counter wrapped around */
1695 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1697 /* update the remainder */
1699 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1700 bus->isoc_time_last |= isoc_time_curr;
1702 return (bus->isoc_time_last);
1705 /*------------------------------------------------------------------------*
1706 * usbd_fs_isoc_schedule_isoc_time_expand
1708 * This function does multiple things. First of all it will expand the
1709 * passed isochronous time, which is the return value. Then it will
1710 * store where the current FULL speed isochronous schedule is
1711 * positioned in time and where the end is. See "pp_start" and
1712 * "pp_end" arguments.
1715 * Expanded version of "isoc_time".
1717 * NOTE: This function depends on being called regularly with
1718 * intervals less than "USB_ISOC_TIME_MAX".
1719 *------------------------------------------------------------------------*/
1720 #if USB_HAVE_TT_SUPPORT
1722 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1723 struct usb_fs_isoc_schedule **pp_start,
1724 struct usb_fs_isoc_schedule **pp_end,
1727 struct usb_fs_isoc_schedule *fss_end;
1728 struct usb_fs_isoc_schedule *fss_a;
1729 struct usb_fs_isoc_schedule *fss_b;
1730 struct usb_hub *hs_hub;
1732 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1734 hs_hub = udev->parent_hs_hub->hub;
1736 if (hs_hub != NULL) {
1738 fss_a = hs_hub->fs_isoc_schedule +
1739 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1741 hs_hub->isoc_last_time = isoc_time;
1743 fss_b = hs_hub->fs_isoc_schedule +
1744 (isoc_time % USB_ISOC_TIME_MAX);
1746 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1748 *pp_start = hs_hub->fs_isoc_schedule;
1751 while (fss_a != fss_b) {
1752 if (fss_a == fss_end) {
1753 fss_a = hs_hub->fs_isoc_schedule;
1756 usbd_fs_isoc_schedule_init_sub(fss_a);
1769 /*------------------------------------------------------------------------*
1770 * usbd_fs_isoc_schedule_alloc
1772 * This function will allocate bandwidth for an isochronous FULL speed
1773 * transaction in the FULL speed schedule. The microframe slot where
1774 * the transaction should be started is stored in the byte pointed to
1775 * by "pstart". The "len" argument specifies the length of the
1776 * transaction in bytes.
1781 *------------------------------------------------------------------------*/
1782 #if USB_HAVE_TT_SUPPORT
1784 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1785 uint8_t *pstart, uint16_t len)
1787 uint8_t slot = fss->frame_slot;
1789 /* Compute overhead and bit-stuffing */
1796 if (len > fss->total_bytes) {
1797 *pstart = 0; /* set some dummy value */
1798 return (1); /* error */
1802 fss->total_bytes -= len;
1804 while (len >= fss->frame_bytes) {
1805 len -= fss->frame_bytes;
1806 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1810 fss->frame_bytes -= len;
1813 return (0); /* success */
1817 /*------------------------------------------------------------------------*
1818 * usb_bus_port_get_device
1820 * This function is NULL safe.
1821 *------------------------------------------------------------------------*/
1823 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1825 if ((bus == NULL) || (up == NULL)) {
1829 if (up->device_index == 0) {
1833 return (bus->devices[up->device_index]);
1836 /*------------------------------------------------------------------------*
1837 * usb_bus_port_set_device
1839 * This function is NULL safe.
1840 *------------------------------------------------------------------------*/
1842 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1843 struct usb_device *udev, uint8_t device_index)
1850 * There is only one case where we don't
1851 * have an USB port, and that is the Root Hub!
1855 up->device_index = device_index;
1857 device_index = up->device_index;
1858 up->device_index = 0;
1862 * Make relationships to our new device
1864 if (device_index != 0) {
1866 mtx_lock(&usb_ref_lock);
1868 bus->devices[device_index] = udev;
1870 mtx_unlock(&usb_ref_lock);
1876 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1879 /*------------------------------------------------------------------------*
1882 * This functions is called when the USB event thread needs to run.
1883 *------------------------------------------------------------------------*/
1885 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1892 DPRINTF("No bus pointer!\n");
1895 if ((bus->devices == NULL) ||
1896 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1897 DPRINTF("No root HUB\n");
1900 if (mtx_owned(&bus->bus_mtx)) {
1909 if (usb_proc_msignal(&bus->explore_proc,
1910 &bus->explore_msg[0], &bus->explore_msg[1])) {
1914 USB_BUS_UNLOCK(bus);
1918 /*------------------------------------------------------------------------*
1919 * usb_needs_explore_all
1921 * This function is called whenever a new driver is loaded and will
1922 * cause that all USB busses are re-explored.
1923 *------------------------------------------------------------------------*/
1925 usb_needs_explore_all(void)
1927 struct usb_bus *bus;
1934 dc = usb_devclass_ptr;
1936 DPRINTFN(0, "no devclass\n");
1940 * Explore all USB busses in parallell.
1942 max = devclass_get_maxunit(dc);
1944 dev = devclass_get_device(dc, max);
1946 bus = device_get_softc(dev);
1948 usb_needs_explore(bus, 1);
1955 /*------------------------------------------------------------------------*
1956 * usb_bus_power_update
1958 * This function will ensure that all USB devices on the given bus are
1959 * properly suspended or resumed according to the device transfer
1961 *------------------------------------------------------------------------*/
1964 usb_bus_power_update(struct usb_bus *bus)
1966 usb_needs_explore(bus, 0 /* no probe */ );
1970 /*------------------------------------------------------------------------*
1971 * usbd_transfer_power_ref
1973 * This function will modify the power save reference counts and
1974 * wakeup the USB device associated with the given USB transfer, if
1976 *------------------------------------------------------------------------*/
1979 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1981 static const usb_power_mask_t power_mask[4] = {
1982 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1983 [UE_BULK] = USB_HW_POWER_BULK,
1984 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1985 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1987 struct usb_device *udev;
1988 uint8_t needs_explore;
1989 uint8_t needs_hw_power;
1992 udev = xfer->xroot->udev;
1994 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1995 /* no power save for root HUB */
1998 USB_BUS_LOCK(udev->bus);
2000 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2002 udev->pwr_save.last_xfer_time = ticks;
2003 udev->pwr_save.type_refs[xfer_type] += val;
2005 if (xfer->flags_int.control_xfr) {
2006 udev->pwr_save.read_refs += val;
2007 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2009 * It is not allowed to suspend during a
2012 udev->pwr_save.write_refs += val;
2014 } else if (USB_GET_DATA_ISREAD(xfer)) {
2015 udev->pwr_save.read_refs += val;
2017 udev->pwr_save.write_refs += val;
2021 if (udev->flags.self_suspended)
2022 needs_explore = usb_peer_should_wakeup(udev);
2026 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2027 DPRINTF("Adding type %u to power state\n", xfer_type);
2028 udev->bus->hw_power_state |= power_mask[xfer_type];
2038 USB_BUS_UNLOCK(udev->bus);
2040 if (needs_explore) {
2041 DPRINTF("update\n");
2042 usb_bus_power_update(udev->bus);
2043 } else if (needs_hw_power) {
2044 DPRINTF("needs power\n");
2045 if (udev->bus->methods->set_hw_power != NULL) {
2046 (udev->bus->methods->set_hw_power) (udev->bus);
2052 /*------------------------------------------------------------------------*
2053 * usb_peer_should_wakeup
2055 * This function returns non-zero if the current device should wake up.
2056 *------------------------------------------------------------------------*/
2058 usb_peer_should_wakeup(struct usb_device *udev)
2060 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2061 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2062 (udev->re_enumerate_wait != 0) ||
2063 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2064 (udev->pwr_save.write_refs != 0) ||
2065 ((udev->pwr_save.read_refs != 0) &&
2066 (udev->flags.usb_mode == USB_MODE_HOST) &&
2067 (usb_device_20_compatible(udev) != 0) &&
2068 (usb_peer_can_wakeup(udev) == 0)));
2071 /*------------------------------------------------------------------------*
2074 * This function implements the USB power daemon and is called
2075 * regularly from the USB explore thread.
2076 *------------------------------------------------------------------------*/
2079 usb_bus_powerd(struct usb_bus *bus)
2081 struct usb_device *udev;
2084 usb_ticks_t mintime;
2085 usb_size_t type_refs[5];
2088 limit = usb_power_timeout;
2091 else if (limit > 255)
2096 DPRINTF("bus=%p\n", bus);
2101 * The root HUB device is never suspended
2102 * and we simply skip it.
2104 for (x = USB_ROOT_HUB_ADDR + 1;
2105 x != bus->devices_max; x++) {
2107 udev = bus->devices[x];
2111 temp = ticks - udev->pwr_save.last_xfer_time;
2113 if (usb_peer_should_wakeup(udev)) {
2114 /* check if we are suspended */
2115 if (udev->flags.self_suspended != 0) {
2116 USB_BUS_UNLOCK(bus);
2117 usb_dev_resume_peer(udev);
2120 } else if ((temp >= limit) &&
2121 (udev->flags.usb_mode == USB_MODE_HOST) &&
2122 (udev->flags.self_suspended == 0)) {
2123 /* try to do suspend */
2125 USB_BUS_UNLOCK(bus);
2126 usb_dev_suspend_peer(udev);
2131 /* reset counters */
2140 /* Re-loop all the devices to get the actual state */
2142 for (x = USB_ROOT_HUB_ADDR + 1;
2143 x != bus->devices_max; x++) {
2145 udev = bus->devices[x];
2149 /* we found a non-Root-Hub USB device */
2152 /* "last_xfer_time" can be updated by a resume */
2153 temp = ticks - udev->pwr_save.last_xfer_time;
2156 * Compute minimum time since last transfer for the complete
2162 if (udev->flags.self_suspended == 0) {
2163 type_refs[0] += udev->pwr_save.type_refs[0];
2164 type_refs[1] += udev->pwr_save.type_refs[1];
2165 type_refs[2] += udev->pwr_save.type_refs[2];
2166 type_refs[3] += udev->pwr_save.type_refs[3];
2170 if (mintime >= (1 * hz)) {
2171 /* recompute power masks */
2172 DPRINTF("Recomputing power masks\n");
2173 bus->hw_power_state = 0;
2174 if (type_refs[UE_CONTROL] != 0)
2175 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2176 if (type_refs[UE_BULK] != 0)
2177 bus->hw_power_state |= USB_HW_POWER_BULK;
2178 if (type_refs[UE_INTERRUPT] != 0)
2179 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2180 if (type_refs[UE_ISOCHRONOUS] != 0)
2181 bus->hw_power_state |= USB_HW_POWER_ISOC;
2182 if (type_refs[4] != 0)
2183 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2185 USB_BUS_UNLOCK(bus);
2187 if (bus->methods->set_hw_power != NULL) {
2188 /* always update hardware power! */
2189 (bus->methods->set_hw_power) (bus);
2195 /*------------------------------------------------------------------------*
2196 * usb_dev_resume_peer
2198 * This function will resume an USB peer and do the required USB
2199 * signalling to get an USB device out of the suspended state.
2200 *------------------------------------------------------------------------*/
2202 usb_dev_resume_peer(struct usb_device *udev)
2204 struct usb_bus *bus;
2211 /* check if already resumed */
2212 if (udev->flags.self_suspended == 0)
2215 /* we need a parent HUB to do resume */
2216 if (udev->parent_hub == NULL)
2219 DPRINTF("udev=%p\n", udev);
2221 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2222 (udev->flags.remote_wakeup == 0)) {
2224 * If the host did not set the remote wakeup feature, we can
2225 * not wake it up either!
2227 DPRINTF("remote wakeup is not set!\n");
2230 /* get bus pointer */
2233 /* resume parent hub first */
2234 usb_dev_resume_peer(udev->parent_hub);
2236 /* reduce chance of instant resume failure by waiting a little bit */
2237 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2239 if (usb_device_20_compatible(udev)) {
2240 /* resume current port (Valid in Host and Device Mode) */
2241 err = usbd_req_clear_port_feature(udev->parent_hub,
2242 NULL, udev->port_no, UHF_PORT_SUSPEND);
2244 DPRINTFN(0, "Resuming port failed\n");
2249 /* resume settle time */
2250 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2252 if (bus->methods->device_resume != NULL) {
2253 /* resume USB device on the USB controller */
2254 (bus->methods->device_resume) (udev);
2257 /* set that this device is now resumed */
2258 udev->flags.self_suspended = 0;
2260 /* make sure that we don't go into suspend right away */
2261 udev->pwr_save.last_xfer_time = ticks;
2263 /* make sure the needed power masks are on */
2264 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2265 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2266 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2267 bus->hw_power_state |= USB_HW_POWER_BULK;
2268 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2269 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2270 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2271 bus->hw_power_state |= USB_HW_POWER_ISOC;
2273 USB_BUS_UNLOCK(bus);
2275 if (bus->methods->set_hw_power != NULL) {
2276 /* always update hardware power! */
2277 (bus->methods->set_hw_power) (bus);
2282 /* notify all sub-devices about resume */
2283 err = usb_suspend_resume(udev, 0);
2285 usbd_sr_unlock(udev);
2287 /* check if peer has wakeup capability */
2288 if (usb_peer_can_wakeup(udev) &&
2289 usb_device_20_compatible(udev)) {
2290 /* clear remote wakeup */
2291 err = usbd_req_clear_device_feature(udev,
2292 NULL, UF_DEVICE_REMOTE_WAKEUP);
2294 DPRINTFN(0, "Clearing device "
2295 "remote wakeup failed: %s\n",
2301 /*------------------------------------------------------------------------*
2302 * usb_dev_suspend_peer
2304 * This function will suspend an USB peer and do the required USB
2305 * signalling to get an USB device into the suspended state.
2306 *------------------------------------------------------------------------*/
2308 usb_dev_suspend_peer(struct usb_device *udev)
2310 struct usb_device *child;
2320 /* check if already suspended */
2321 if (udev->flags.self_suspended)
2324 /* we need a parent HUB to do suspend */
2325 if (udev->parent_hub == NULL)
2328 DPRINTF("udev=%p\n", udev);
2330 /* check if the current device is a HUB */
2331 if (udev->hub != NULL) {
2332 nports = udev->hub->nports;
2334 /* check if all devices on the HUB are suspended */
2335 for (x = 0; x != nports; x++) {
2336 child = usb_bus_port_get_device(udev->bus,
2337 udev->hub->ports + x);
2342 if (child->flags.self_suspended)
2345 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2350 if (usb_peer_can_wakeup(udev) &&
2351 usb_device_20_compatible(udev)) {
2353 * This request needs to be done before we set
2354 * "udev->flags.self_suspended":
2357 /* allow device to do remote wakeup */
2358 err = usbd_req_set_device_feature(udev,
2359 NULL, UF_DEVICE_REMOTE_WAKEUP);
2361 DPRINTFN(0, "Setting device "
2362 "remote wakeup failed\n");
2366 USB_BUS_LOCK(udev->bus);
2368 * Checking for suspend condition and setting suspended bit
2371 err = usb_peer_should_wakeup(udev);
2374 * Set that this device is suspended. This variable
2375 * must be set before calling USB controller suspend
2378 udev->flags.self_suspended = 1;
2380 USB_BUS_UNLOCK(udev->bus);
2383 if (usb_peer_can_wakeup(udev) &&
2384 usb_device_20_compatible(udev)) {
2385 /* allow device to do remote wakeup */
2386 err = usbd_req_clear_device_feature(udev,
2387 NULL, UF_DEVICE_REMOTE_WAKEUP);
2389 DPRINTFN(0, "Setting device "
2390 "remote wakeup failed\n");
2394 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2395 /* resume parent HUB first */
2396 usb_dev_resume_peer(udev->parent_hub);
2398 /* reduce chance of instant resume failure by waiting a little bit */
2399 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2401 /* resume current port (Valid in Host and Device Mode) */
2402 err = usbd_req_clear_port_feature(udev->parent_hub,
2403 NULL, udev->port_no, UHF_PORT_SUSPEND);
2405 /* resume settle time */
2406 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2408 DPRINTF("Suspend was cancelled!\n");
2414 /* notify all sub-devices about suspend */
2415 err = usb_suspend_resume(udev, 1);
2417 usbd_sr_unlock(udev);
2419 if (udev->bus->methods->device_suspend != NULL) {
2422 /* suspend device on the USB controller */
2423 (udev->bus->methods->device_suspend) (udev);
2426 temp = usbd_get_dma_delay(udev);
2428 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2432 if (usb_device_20_compatible(udev)) {
2433 /* suspend current port */
2434 err = usbd_req_set_port_feature(udev->parent_hub,
2435 NULL, udev->port_no, UHF_PORT_SUSPEND);
2437 DPRINTFN(0, "Suspending port failed\n");
2442 udev = udev->parent_hub;
2446 /*------------------------------------------------------------------------*
2447 * usbd_set_power_mode
2449 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2451 *------------------------------------------------------------------------*/
2453 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2455 /* filter input argument */
2456 if ((power_mode != USB_POWER_MODE_ON) &&
2457 (power_mode != USB_POWER_MODE_OFF))
2458 power_mode = USB_POWER_MODE_SAVE;
2460 power_mode = usbd_filter_power_mode(udev, power_mode);
2462 udev->power_mode = power_mode; /* update copy of power mode */
2465 usb_bus_power_update(udev->bus);
2469 /*------------------------------------------------------------------------*
2470 * usbd_filter_power_mode
2472 * This function filters the power mode based on hardware requirements.
2473 *------------------------------------------------------------------------*/
2475 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2477 struct usb_bus_methods *mtod;
2480 mtod = udev->bus->methods;
2483 if (mtod->get_power_mode != NULL)
2484 (mtod->get_power_mode) (udev, &temp);
2486 /* check if we should not filter */
2488 return (power_mode);
2490 /* use fixed power mode given by hardware driver */
2494 /*------------------------------------------------------------------------*
2495 * usbd_start_re_enumerate
2497 * This function starts re-enumeration of the given USB device. This
2498 * function does not need to be called BUS-locked. This function does
2499 * not wait until the re-enumeration is completed.
2500 *------------------------------------------------------------------------*/
2502 usbd_start_re_enumerate(struct usb_device *udev)
2504 if (udev->re_enumerate_wait == 0) {
2505 udev->re_enumerate_wait = 1;
2506 usb_needs_explore(udev->bus, 0);