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");
710 for (x = 0; x != hub->nports; x++) {
714 err = uhub_read_port_status(sc, portno);
716 /* most likely the HUB is gone */
719 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
720 DPRINTF("Overcurrent on port %u.\n", portno);
721 err = usbd_req_clear_port_feature(
722 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
724 /* most likely the HUB is gone */
728 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
730 * Fake a connect status change so that the
731 * status gets checked initially!
733 sc->sc_st.port_change |=
734 UPS_C_CONNECT_STATUS;
736 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
737 err = usbd_req_clear_port_feature(
738 udev, NULL, portno, UHF_C_PORT_ENABLE);
740 /* most likely the HUB is gone */
743 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
745 * Ignore the port error if the device
748 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
749 DPRINTFN(0, "illegal enable change, "
750 "port %d\n", portno);
753 if (up->restartcnt == USB_RESTART_MAX) {
754 /* XXX could try another speed ? */
755 DPRINTFN(0, "port error, giving up "
756 "port %d\n", portno);
758 sc->sc_st.port_change |=
759 UPS_C_CONNECT_STATUS;
764 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
765 err = uhub_reattach_port(sc, portno);
767 /* most likely the HUB is gone */
771 if (sc->sc_st.port_change & (UPS_C_SUSPEND | UPS_C_PORT_LINK_STATE)) {
772 err = uhub_suspend_resume_port(sc, portno);
774 /* most likely the HUB is gone */
778 err = uhub_explore_sub(sc, up);
780 /* no device(s) present */
783 /* explore succeeded - reset restart counter */
787 /* initial status checked */
788 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
791 return (USB_ERR_NORMAL_COMPLETION);
795 uhub_probe(device_t dev)
797 struct usb_attach_arg *uaa = device_get_ivars(dev);
799 if (uaa->usb_mode != USB_MODE_HOST)
803 * The subclass for USB HUBs is currently ignored because it
804 * is 0 for some and 1 for others.
806 if (uaa->info.bConfigIndex == 0 &&
807 uaa->info.bDeviceClass == UDCLASS_HUB)
813 /* NOTE: The information returned by this function can be wrong. */
815 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
817 struct usb_hub_descriptor hubdesc20;
818 struct usb_hub_ss_descriptor hubdesc30;
823 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
824 return (USB_ERR_INVAL);
829 switch (udev->speed) {
833 /* assuming that there is one port */
834 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
836 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
837 "error=%s\n", usbd_errstr(err));
840 nports = hubdesc20.bNbrPorts;
844 if (udev->speed == USB_SPEED_HIGH)
845 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
848 case USB_SPEED_SUPER:
849 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
851 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
852 "error=%s\n", usbd_errstr(err));
855 nports = hubdesc30.bNbrPorts;
875 uhub_attach(device_t dev)
877 struct uhub_softc *sc = device_get_softc(dev);
878 struct usb_attach_arg *uaa = device_get_ivars(dev);
879 struct usb_device *udev = uaa->device;
880 struct usb_device *parent_hub = udev->parent_hub;
882 struct usb_hub_descriptor hubdesc20;
883 struct usb_hub_ss_descriptor hubdesc30;
895 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
897 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
898 device_get_nameunit(dev));
900 device_set_usb_desc(dev);
902 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
903 "parent->selfpowered=%d\n",
905 udev->flags.self_powered,
908 parent_hub->flags.self_powered : 0);
910 if (uhub_is_too_deep(udev)) {
911 DPRINTFN(0, "HUB at depth %d, "
912 "exceeds maximum. HUB ignored\n", (int)udev->depth);
916 if (!udev->flags.self_powered && parent_hub &&
917 !parent_hub->flags.self_powered) {
918 DPRINTFN(0, "Bus powered HUB connected to "
919 "bus powered HUB. HUB ignored\n");
922 /* get HUB descriptor */
924 DPRINTFN(2, "Getting HUB descriptor\n");
926 switch (udev->speed) {
930 /* assuming that there is one port */
931 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
933 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
934 "error=%s\n", usbd_errstr(err));
937 /* get number of ports */
938 nports = hubdesc20.bNbrPorts;
940 /* get power delay */
941 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
942 USB_EXTRA_POWER_UP_TIME);
944 /* get complete HUB descriptor */
946 /* check number of ports */
948 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
949 "error=%s\n", usbd_errstr(err));
952 /* get complete HUB descriptor */
953 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
956 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
957 "error=%s\n", usbd_errstr(err));
960 if (hubdesc20.bNbrPorts != nports) {
961 DPRINTFN(0, "Number of ports changed\n");
966 case USB_SPEED_SUPER:
967 if (udev->parent_hub != NULL) {
968 err = usbd_req_set_hub_depth(udev, NULL,
971 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
972 "error=%s\n", usbd_errstr(err));
976 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
978 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
979 "error=%s\n", usbd_errstr(err));
982 /* get number of ports */
983 nports = hubdesc30.bNbrPorts;
985 /* get power delay */
986 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
987 USB_EXTRA_POWER_UP_TIME);
989 /* get complete HUB descriptor */
991 /* check number of ports */
992 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
993 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
994 "error=%s\n", usbd_errstr(err));
997 /* get complete HUB descriptor */
998 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1001 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1002 "error=%s\n", usbd_errstr(err));
1005 if (hubdesc30.bNbrPorts != nports) {
1006 DPRINTFN(0, "Number of ports changed\n");
1012 DPRINTF("Assuming HUB has only one port\n");
1013 /* default number of ports */
1015 /* default power delay */
1016 pwrdly = ((10 * UHD_PWRON_FACTOR) + USB_EXTRA_POWER_UP_TIME);
1020 DPRINTFN(0, "portless HUB\n");
1023 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1024 M_USBDEV, M_WAITOK | M_ZERO);
1031 #if USB_HAVE_TT_SUPPORT
1032 /* init FULL-speed ISOCHRONOUS schedule */
1033 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
1035 /* initialize HUB structure */
1037 hub->explore = &uhub_explore;
1038 hub->nports = nports;
1039 hub->hubudev = udev;
1041 /* if self powered hub, give ports maximum current */
1042 if (udev->flags.self_powered) {
1043 hub->portpower = USB_MAX_POWER;
1045 hub->portpower = USB_MIN_POWER;
1048 /* set up interrupt pipe */
1050 if (udev->parent_hub == NULL) {
1051 /* root HUB is special */
1055 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1056 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1059 DPRINTFN(0, "cannot setup interrupt transfer, "
1060 "errstr=%s\n", usbd_errstr(err));
1063 /* wait with power off for a while */
1064 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1067 * To have the best chance of success we do things in the exact same
1068 * order as Windoze98. This should not be necessary, but some
1069 * devices do not follow the USB specs to the letter.
1071 * These are the events on the bus when a hub is attached:
1072 * Get device and config descriptors (see attach code)
1073 * Get hub descriptor (see above)
1076 * wait for power to become stable
1077 * (all below happens in explore code)
1079 * clear C_PORT_CONNECTION
1082 * if device connected
1086 * clear C_PORT_RESET
1088 * proceed with device attachment
1091 /* XXX should check for none, individual, or ganged power? */
1095 for (x = 0; x != nports; x++) {
1096 /* set up data structures */
1097 struct usb_port *up = hub->ports + x;
1099 up->device_index = 0;
1103 /* check if port is removable */
1104 switch (udev->speed) {
1106 case USB_SPEED_FULL:
1107 case USB_SPEED_HIGH:
1108 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1111 case USB_SPEED_SUPER:
1112 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1116 DPRINTF("Assuming removable port\n");
1121 /* turn the power on */
1122 err = usbd_req_set_port_feature(udev, NULL,
1123 portno, UHF_PORT_POWER);
1126 DPRINTFN(0, "port %d power on failed, %s\n",
1127 portno, usbd_errstr(err));
1129 DPRINTF("turn on port %d power\n",
1132 /* wait for stable power */
1133 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1136 device_printf(dev, "%d port%s with %d "
1137 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1138 removable, udev->flags.self_powered ? "self" : "bus");
1140 /* Start the interrupt endpoint, if any */
1142 if (sc->sc_xfer[0] != NULL) {
1143 mtx_lock(&sc->sc_mtx);
1144 usbd_transfer_start(sc->sc_xfer[0]);
1145 mtx_unlock(&sc->sc_mtx);
1148 /* Enable automatic power save on all USB HUBs */
1150 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1155 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1158 free(udev->hub, M_USBDEV);
1162 mtx_destroy(&sc->sc_mtx);
1168 * Called from process context when the hub is gone.
1169 * Detach all devices on active ports.
1172 uhub_detach(device_t dev)
1174 struct uhub_softc *sc = device_get_softc(dev);
1175 struct usb_hub *hub = sc->sc_udev->hub;
1176 struct usb_device *child;
1179 if (hub == NULL) /* must be partially working */
1182 /* Make sure interrupt transfer is gone. */
1183 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1185 /* Detach all ports */
1186 for (x = 0; x != hub->nports; x++) {
1188 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1190 if (child == NULL) {
1195 * Free USB device and all subdevices, if any.
1197 usb_free_device(child, 0);
1200 free(hub, M_USBDEV);
1201 sc->sc_udev->hub = NULL;
1203 mtx_destroy(&sc->sc_mtx);
1209 uhub_suspend(device_t dev)
1212 /* Sub-devices are not suspended here! */
1217 uhub_resume(device_t dev)
1220 /* Sub-devices are not resumed here! */
1225 uhub_driver_added(device_t dev, driver_t *driver)
1227 usb_needs_explore_all();
1231 struct usb_device *udev;
1233 uint8_t iface_index;
1237 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1238 struct hub_result *res)
1240 struct usb_interface *iface;
1241 struct usb_device *udev;
1246 nports = hub->nports;
1247 for (x = 0; x != nports; x++) {
1248 udev = usb_bus_port_get_device(hub->hubudev->bus,
1253 for (i = 0; i != USB_IFACE_MAX; i++) {
1254 iface = usbd_get_iface(udev, i);
1256 (iface->subdev == child)) {
1257 res->iface_index = i;
1259 res->portno = x + 1;
1264 res->iface_index = 0;
1270 uhub_child_location_string(device_t parent, device_t child,
1271 char *buf, size_t buflen)
1273 struct uhub_softc *sc;
1274 struct usb_hub *hub;
1275 struct hub_result res;
1277 if (!device_is_attached(parent)) {
1283 sc = device_get_softc(parent);
1284 hub = sc->sc_udev->hub;
1287 uhub_find_iface_index(hub, child, &res);
1289 DPRINTF("device not on hub\n");
1295 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1296 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1297 res.portno, device_get_unit(res.udev->bus->bdev),
1298 res.udev->device_index, res.iface_index);
1306 uhub_child_pnpinfo_string(device_t parent, device_t child,
1307 char *buf, size_t buflen)
1309 struct uhub_softc *sc;
1310 struct usb_hub *hub;
1311 struct usb_interface *iface;
1312 struct hub_result res;
1314 if (!device_is_attached(parent)) {
1320 sc = device_get_softc(parent);
1321 hub = sc->sc_udev->hub;
1324 uhub_find_iface_index(hub, child, &res);
1326 DPRINTF("device not on hub\n");
1332 iface = usbd_get_iface(res.udev, res.iface_index);
1333 if (iface && iface->idesc) {
1334 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1335 "devclass=0x%02x devsubclass=0x%02x "
1339 "intclass=0x%02x intsubclass=0x%02x "
1340 "intprotocol=0x%02x " "%s%s",
1341 UGETW(res.udev->ddesc.idVendor),
1342 UGETW(res.udev->ddesc.idProduct),
1343 res.udev->ddesc.bDeviceClass,
1344 res.udev->ddesc.bDeviceSubClass,
1345 usb_get_serial(res.udev),
1346 UGETW(res.udev->ddesc.bcdDevice),
1347 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1348 iface->idesc->bInterfaceClass,
1349 iface->idesc->bInterfaceSubClass,
1350 iface->idesc->bInterfaceProtocol,
1351 iface->pnpinfo ? " " : "",
1352 iface->pnpinfo ? iface->pnpinfo : "");
1366 * The USB Transaction Translator:
1367 * ===============================
1369 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1370 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1371 * USB transfers. To utilize bandwidth dynamically the "scatter and
1372 * gather" principle must be applied. This means that bandwidth must
1373 * be divided into equal parts of bandwidth. With regard to USB all
1374 * data is transferred in smaller packets with length
1375 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1378 * The bandwidth scheduler which I have implemented will simply pack
1379 * the USB transfers back to back until there is no more space in the
1380 * schedule. Out of the 8 microframes which the USB 2.0 standard
1381 * provides, only 6 are available for non-HIGH-speed devices. I have
1382 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1383 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1384 * this division, it is very difficult to allocate and free bandwidth
1387 * NOTE about the Transaction Translator in USB HUBs:
1389 * USB HUBs have a very simple Transaction Translator, that will
1390 * simply pipeline all the SPLIT transactions. That means that the
1391 * transactions will be executed in the order they are queued!
1395 /*------------------------------------------------------------------------*
1396 * usb_intr_find_best_slot
1399 * The best Transaction Translation slot for an interrupt endpoint.
1400 *------------------------------------------------------------------------*/
1402 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1403 uint8_t end, uint8_t mask)
1405 usb_size_t min = 0 - 1;
1413 /* find the last slot with lesser used bandwidth */
1415 for (x = start; x < end; x++) {
1419 /* compute sum of bandwidth */
1420 for (z = x; z < end; z++) {
1421 if (mask & (1U << (z - x)))
1425 /* check if the current multi-slot is more optimal */
1431 /* check if the mask is about to be shifted out */
1432 if (mask & (1U << (end - 1 - x)))
1438 /*------------------------------------------------------------------------*
1439 * usb_hs_bandwidth_adjust
1441 * This function will update the bandwith usage for the microframe
1442 * having index "slot" by "len" bytes. "len" can be negative. If the
1443 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1444 * the "slot" argument will be replaced by the slot having least used
1445 * bandwidth. The "mask" argument is used for multi-slot allocations.
1448 * The slot in which the bandwidth update was done: 0..7
1449 *------------------------------------------------------------------------*/
1451 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1452 uint8_t slot, uint8_t mask)
1454 struct usb_bus *bus = udev->bus;
1455 struct usb_hub *hub;
1456 enum usb_dev_speed speed;
1459 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1461 speed = usbd_get_speed(udev);
1465 case USB_SPEED_FULL:
1466 if (speed == USB_SPEED_LOW) {
1470 * The Host Controller Driver should have
1471 * performed checks so that the lookup
1472 * below does not result in a NULL pointer
1476 hub = udev->parent_hs_hub->hub;
1477 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1478 slot = usb_intr_find_best_slot(hub->uframe_usage,
1479 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1481 for (x = slot; x < 8; x++) {
1482 if (mask & (1U << (x - slot))) {
1483 hub->uframe_usage[x] += len;
1484 bus->uframe_usage[x] += len;
1489 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1490 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1491 USB_HS_MICRO_FRAMES_MAX, mask);
1493 for (x = slot; x < 8; x++) {
1494 if (mask & (1U << (x - slot))) {
1495 bus->uframe_usage[x] += len;
1503 /*------------------------------------------------------------------------*
1504 * usb_hs_bandwidth_alloc
1506 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1507 *------------------------------------------------------------------------*/
1509 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1511 struct usb_device *udev;
1516 udev = xfer->xroot->udev;
1518 if (udev->flags.usb_mode != USB_MODE_HOST)
1519 return; /* not supported */
1521 xfer->endpoint->refcount_bw++;
1522 if (xfer->endpoint->refcount_bw != 1)
1523 return; /* already allocated */
1525 speed = usbd_get_speed(udev);
1527 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1529 /* allocate a microframe slot */
1532 slot = usb_hs_bandwidth_adjust(udev,
1533 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1535 xfer->endpoint->usb_uframe = slot;
1536 xfer->endpoint->usb_smask = mask << slot;
1538 if ((speed != USB_SPEED_FULL) &&
1539 (speed != USB_SPEED_LOW)) {
1540 xfer->endpoint->usb_cmask = 0x00 ;
1542 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1546 case UE_ISOCHRONOUS:
1547 switch (usbd_xfer_get_fps_shift(xfer)) {
1562 /* allocate a microframe multi-slot */
1564 slot = usb_hs_bandwidth_adjust(udev,
1565 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1567 xfer->endpoint->usb_uframe = slot;
1568 xfer->endpoint->usb_cmask = 0;
1569 xfer->endpoint->usb_smask = mask << slot;
1573 xfer->endpoint->usb_uframe = 0;
1574 xfer->endpoint->usb_cmask = 0;
1575 xfer->endpoint->usb_smask = 0;
1579 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1580 xfer->endpoint->usb_uframe,
1581 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1584 /*------------------------------------------------------------------------*
1585 * usb_hs_bandwidth_free
1587 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1588 *------------------------------------------------------------------------*/
1590 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1592 struct usb_device *udev;
1596 udev = xfer->xroot->udev;
1598 if (udev->flags.usb_mode != USB_MODE_HOST)
1599 return; /* not supported */
1601 xfer->endpoint->refcount_bw--;
1602 if (xfer->endpoint->refcount_bw != 0)
1603 return; /* still allocated */
1605 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1607 case UE_ISOCHRONOUS:
1609 slot = xfer->endpoint->usb_uframe;
1610 mask = xfer->endpoint->usb_smask;
1612 /* free microframe slot(s): */
1613 usb_hs_bandwidth_adjust(udev,
1614 -xfer->max_frame_size, slot, mask >> slot);
1616 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1617 slot, mask >> slot);
1619 xfer->endpoint->usb_uframe = 0;
1620 xfer->endpoint->usb_cmask = 0;
1621 xfer->endpoint->usb_smask = 0;
1629 /*------------------------------------------------------------------------*
1630 * usbd_fs_isoc_schedule_init_sub
1632 * This function initialises an USB FULL speed isochronous schedule
1634 *------------------------------------------------------------------------*/
1635 #if USB_HAVE_TT_SUPPORT
1637 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1639 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1640 USB_FS_BYTES_PER_HS_UFRAME);
1641 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1642 fss->frame_slot = 0;
1646 /*------------------------------------------------------------------------*
1647 * usbd_fs_isoc_schedule_init_all
1649 * This function will reset the complete USB FULL speed isochronous
1650 * bandwidth schedule.
1651 *------------------------------------------------------------------------*/
1652 #if USB_HAVE_TT_SUPPORT
1654 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1656 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1658 while (fss != fss_end) {
1659 usbd_fs_isoc_schedule_init_sub(fss);
1665 /*------------------------------------------------------------------------*
1666 * usb_isoc_time_expand
1668 * This function will expand the time counter from 7-bit to 16-bit.
1671 * 16-bit isochronous time counter.
1672 *------------------------------------------------------------------------*/
1674 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1678 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1680 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1682 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1684 if (isoc_time_curr < rem) {
1685 /* the time counter wrapped around */
1686 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1688 /* update the remainder */
1690 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1691 bus->isoc_time_last |= isoc_time_curr;
1693 return (bus->isoc_time_last);
1696 /*------------------------------------------------------------------------*
1697 * usbd_fs_isoc_schedule_isoc_time_expand
1699 * This function does multiple things. First of all it will expand the
1700 * passed isochronous time, which is the return value. Then it will
1701 * store where the current FULL speed isochronous schedule is
1702 * positioned in time and where the end is. See "pp_start" and
1703 * "pp_end" arguments.
1706 * Expanded version of "isoc_time".
1708 * NOTE: This function depends on being called regularly with
1709 * intervals less than "USB_ISOC_TIME_MAX".
1710 *------------------------------------------------------------------------*/
1711 #if USB_HAVE_TT_SUPPORT
1713 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1714 struct usb_fs_isoc_schedule **pp_start,
1715 struct usb_fs_isoc_schedule **pp_end,
1718 struct usb_fs_isoc_schedule *fss_end;
1719 struct usb_fs_isoc_schedule *fss_a;
1720 struct usb_fs_isoc_schedule *fss_b;
1721 struct usb_hub *hs_hub;
1723 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1725 hs_hub = udev->parent_hs_hub->hub;
1727 if (hs_hub != NULL) {
1729 fss_a = hs_hub->fs_isoc_schedule +
1730 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1732 hs_hub->isoc_last_time = isoc_time;
1734 fss_b = hs_hub->fs_isoc_schedule +
1735 (isoc_time % USB_ISOC_TIME_MAX);
1737 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1739 *pp_start = hs_hub->fs_isoc_schedule;
1742 while (fss_a != fss_b) {
1743 if (fss_a == fss_end) {
1744 fss_a = hs_hub->fs_isoc_schedule;
1747 usbd_fs_isoc_schedule_init_sub(fss_a);
1760 /*------------------------------------------------------------------------*
1761 * usbd_fs_isoc_schedule_alloc
1763 * This function will allocate bandwidth for an isochronous FULL speed
1764 * transaction in the FULL speed schedule. The microframe slot where
1765 * the transaction should be started is stored in the byte pointed to
1766 * by "pstart". The "len" argument specifies the length of the
1767 * transaction in bytes.
1772 *------------------------------------------------------------------------*/
1773 #if USB_HAVE_TT_SUPPORT
1775 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1776 uint8_t *pstart, uint16_t len)
1778 uint8_t slot = fss->frame_slot;
1780 /* Compute overhead and bit-stuffing */
1787 if (len > fss->total_bytes) {
1788 *pstart = 0; /* set some dummy value */
1789 return (1); /* error */
1793 fss->total_bytes -= len;
1795 while (len >= fss->frame_bytes) {
1796 len -= fss->frame_bytes;
1797 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1801 fss->frame_bytes -= len;
1804 return (0); /* success */
1808 /*------------------------------------------------------------------------*
1809 * usb_bus_port_get_device
1811 * This function is NULL safe.
1812 *------------------------------------------------------------------------*/
1814 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1816 if ((bus == NULL) || (up == NULL)) {
1820 if (up->device_index == 0) {
1824 return (bus->devices[up->device_index]);
1827 /*------------------------------------------------------------------------*
1828 * usb_bus_port_set_device
1830 * This function is NULL safe.
1831 *------------------------------------------------------------------------*/
1833 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1834 struct usb_device *udev, uint8_t device_index)
1841 * There is only one case where we don't
1842 * have an USB port, and that is the Root Hub!
1846 up->device_index = device_index;
1848 device_index = up->device_index;
1849 up->device_index = 0;
1853 * Make relationships to our new device
1855 if (device_index != 0) {
1857 mtx_lock(&usb_ref_lock);
1859 bus->devices[device_index] = udev;
1861 mtx_unlock(&usb_ref_lock);
1867 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1870 /*------------------------------------------------------------------------*
1873 * This functions is called when the USB event thread needs to run.
1874 *------------------------------------------------------------------------*/
1876 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1883 DPRINTF("No bus pointer!\n");
1886 if ((bus->devices == NULL) ||
1887 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1888 DPRINTF("No root HUB\n");
1891 if (mtx_owned(&bus->bus_mtx)) {
1900 if (usb_proc_msignal(&bus->explore_proc,
1901 &bus->explore_msg[0], &bus->explore_msg[1])) {
1905 USB_BUS_UNLOCK(bus);
1909 /*------------------------------------------------------------------------*
1910 * usb_needs_explore_all
1912 * This function is called whenever a new driver is loaded and will
1913 * cause that all USB busses are re-explored.
1914 *------------------------------------------------------------------------*/
1916 usb_needs_explore_all(void)
1918 struct usb_bus *bus;
1925 dc = usb_devclass_ptr;
1927 DPRINTFN(0, "no devclass\n");
1931 * Explore all USB busses in parallell.
1933 max = devclass_get_maxunit(dc);
1935 dev = devclass_get_device(dc, max);
1937 bus = device_get_softc(dev);
1939 usb_needs_explore(bus, 1);
1946 /*------------------------------------------------------------------------*
1947 * usb_bus_power_update
1949 * This function will ensure that all USB devices on the given bus are
1950 * properly suspended or resumed according to the device transfer
1952 *------------------------------------------------------------------------*/
1955 usb_bus_power_update(struct usb_bus *bus)
1957 usb_needs_explore(bus, 0 /* no probe */ );
1961 /*------------------------------------------------------------------------*
1962 * usbd_transfer_power_ref
1964 * This function will modify the power save reference counts and
1965 * wakeup the USB device associated with the given USB transfer, if
1967 *------------------------------------------------------------------------*/
1970 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1972 static const usb_power_mask_t power_mask[4] = {
1973 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1974 [UE_BULK] = USB_HW_POWER_BULK,
1975 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1976 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1978 struct usb_device *udev;
1979 uint8_t needs_explore;
1980 uint8_t needs_hw_power;
1983 udev = xfer->xroot->udev;
1985 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1986 /* no power save for root HUB */
1989 USB_BUS_LOCK(udev->bus);
1991 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1993 udev->pwr_save.last_xfer_time = ticks;
1994 udev->pwr_save.type_refs[xfer_type] += val;
1996 if (xfer->flags_int.control_xfr) {
1997 udev->pwr_save.read_refs += val;
1998 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2000 * It is not allowed to suspend during a
2003 udev->pwr_save.write_refs += val;
2005 } else if (USB_GET_DATA_ISREAD(xfer)) {
2006 udev->pwr_save.read_refs += val;
2008 udev->pwr_save.write_refs += val;
2012 if (udev->flags.self_suspended)
2013 needs_explore = usb_peer_should_wakeup(udev);
2017 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2018 DPRINTF("Adding type %u to power state\n", xfer_type);
2019 udev->bus->hw_power_state |= power_mask[xfer_type];
2029 USB_BUS_UNLOCK(udev->bus);
2031 if (needs_explore) {
2032 DPRINTF("update\n");
2033 usb_bus_power_update(udev->bus);
2034 } else if (needs_hw_power) {
2035 DPRINTF("needs power\n");
2036 if (udev->bus->methods->set_hw_power != NULL) {
2037 (udev->bus->methods->set_hw_power) (udev->bus);
2043 /*------------------------------------------------------------------------*
2044 * usb_peer_should_wakeup
2046 * This function returns non-zero if the current device should wake up.
2047 *------------------------------------------------------------------------*/
2049 usb_peer_should_wakeup(struct usb_device *udev)
2051 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2052 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2053 (udev->re_enumerate_wait != 0) ||
2054 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2055 (udev->pwr_save.write_refs != 0) ||
2056 ((udev->pwr_save.read_refs != 0) &&
2057 (udev->flags.usb_mode == USB_MODE_HOST) &&
2058 (usb_device_20_compatible(udev) != 0) &&
2059 (usb_peer_can_wakeup(udev) == 0)));
2062 /*------------------------------------------------------------------------*
2065 * This function implements the USB power daemon and is called
2066 * regularly from the USB explore thread.
2067 *------------------------------------------------------------------------*/
2070 usb_bus_powerd(struct usb_bus *bus)
2072 struct usb_device *udev;
2075 usb_ticks_t mintime;
2076 usb_size_t type_refs[5];
2079 limit = usb_power_timeout;
2082 else if (limit > 255)
2087 DPRINTF("bus=%p\n", bus);
2092 * The root HUB device is never suspended
2093 * and we simply skip it.
2095 for (x = USB_ROOT_HUB_ADDR + 1;
2096 x != bus->devices_max; x++) {
2098 udev = bus->devices[x];
2102 temp = ticks - udev->pwr_save.last_xfer_time;
2104 if (usb_peer_should_wakeup(udev)) {
2105 /* check if we are suspended */
2106 if (udev->flags.self_suspended != 0) {
2107 USB_BUS_UNLOCK(bus);
2108 usb_dev_resume_peer(udev);
2111 } else if ((temp >= limit) &&
2112 (udev->flags.usb_mode == USB_MODE_HOST) &&
2113 (udev->flags.self_suspended == 0)) {
2114 /* try to do suspend */
2116 USB_BUS_UNLOCK(bus);
2117 usb_dev_suspend_peer(udev);
2122 /* reset counters */
2131 /* Re-loop all the devices to get the actual state */
2133 for (x = USB_ROOT_HUB_ADDR + 1;
2134 x != bus->devices_max; x++) {
2136 udev = bus->devices[x];
2140 /* we found a non-Root-Hub USB device */
2143 /* "last_xfer_time" can be updated by a resume */
2144 temp = ticks - udev->pwr_save.last_xfer_time;
2147 * Compute minimum time since last transfer for the complete
2153 if (udev->flags.self_suspended == 0) {
2154 type_refs[0] += udev->pwr_save.type_refs[0];
2155 type_refs[1] += udev->pwr_save.type_refs[1];
2156 type_refs[2] += udev->pwr_save.type_refs[2];
2157 type_refs[3] += udev->pwr_save.type_refs[3];
2161 if (mintime >= (1 * hz)) {
2162 /* recompute power masks */
2163 DPRINTF("Recomputing power masks\n");
2164 bus->hw_power_state = 0;
2165 if (type_refs[UE_CONTROL] != 0)
2166 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2167 if (type_refs[UE_BULK] != 0)
2168 bus->hw_power_state |= USB_HW_POWER_BULK;
2169 if (type_refs[UE_INTERRUPT] != 0)
2170 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2171 if (type_refs[UE_ISOCHRONOUS] != 0)
2172 bus->hw_power_state |= USB_HW_POWER_ISOC;
2173 if (type_refs[4] != 0)
2174 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2176 USB_BUS_UNLOCK(bus);
2178 if (bus->methods->set_hw_power != NULL) {
2179 /* always update hardware power! */
2180 (bus->methods->set_hw_power) (bus);
2186 /*------------------------------------------------------------------------*
2187 * usb_dev_resume_peer
2189 * This function will resume an USB peer and do the required USB
2190 * signalling to get an USB device out of the suspended state.
2191 *------------------------------------------------------------------------*/
2193 usb_dev_resume_peer(struct usb_device *udev)
2195 struct usb_bus *bus;
2202 /* check if already resumed */
2203 if (udev->flags.self_suspended == 0)
2206 /* we need a parent HUB to do resume */
2207 if (udev->parent_hub == NULL)
2210 DPRINTF("udev=%p\n", udev);
2212 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2213 (udev->flags.remote_wakeup == 0)) {
2215 * If the host did not set the remote wakeup feature, we can
2216 * not wake it up either!
2218 DPRINTF("remote wakeup is not set!\n");
2221 /* get bus pointer */
2224 /* resume parent hub first */
2225 usb_dev_resume_peer(udev->parent_hub);
2227 /* reduce chance of instant resume failure by waiting a little bit */
2228 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2230 if (usb_device_20_compatible(udev)) {
2231 /* resume current port (Valid in Host and Device Mode) */
2232 err = usbd_req_clear_port_feature(udev->parent_hub,
2233 NULL, udev->port_no, UHF_PORT_SUSPEND);
2235 DPRINTFN(0, "Resuming port failed\n");
2240 /* resume settle time */
2241 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2243 if (bus->methods->device_resume != NULL) {
2244 /* resume USB device on the USB controller */
2245 (bus->methods->device_resume) (udev);
2248 /* set that this device is now resumed */
2249 udev->flags.self_suspended = 0;
2251 /* make sure that we don't go into suspend right away */
2252 udev->pwr_save.last_xfer_time = ticks;
2254 /* make sure the needed power masks are on */
2255 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2256 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2257 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2258 bus->hw_power_state |= USB_HW_POWER_BULK;
2259 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2260 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2261 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2262 bus->hw_power_state |= USB_HW_POWER_ISOC;
2264 USB_BUS_UNLOCK(bus);
2266 if (bus->methods->set_hw_power != NULL) {
2267 /* always update hardware power! */
2268 (bus->methods->set_hw_power) (bus);
2273 /* notify all sub-devices about resume */
2274 err = usb_suspend_resume(udev, 0);
2276 usbd_sr_unlock(udev);
2278 /* check if peer has wakeup capability */
2279 if (usb_peer_can_wakeup(udev) &&
2280 usb_device_20_compatible(udev)) {
2281 /* clear remote wakeup */
2282 err = usbd_req_clear_device_feature(udev,
2283 NULL, UF_DEVICE_REMOTE_WAKEUP);
2285 DPRINTFN(0, "Clearing device "
2286 "remote wakeup failed: %s\n",
2292 /*------------------------------------------------------------------------*
2293 * usb_dev_suspend_peer
2295 * This function will suspend an USB peer and do the required USB
2296 * signalling to get an USB device into the suspended state.
2297 *------------------------------------------------------------------------*/
2299 usb_dev_suspend_peer(struct usb_device *udev)
2301 struct usb_device *child;
2311 /* check if already suspended */
2312 if (udev->flags.self_suspended)
2315 /* we need a parent HUB to do suspend */
2316 if (udev->parent_hub == NULL)
2319 DPRINTF("udev=%p\n", udev);
2321 /* check if the current device is a HUB */
2322 if (udev->hub != NULL) {
2323 nports = udev->hub->nports;
2325 /* check if all devices on the HUB are suspended */
2326 for (x = 0; x != nports; x++) {
2327 child = usb_bus_port_get_device(udev->bus,
2328 udev->hub->ports + x);
2333 if (child->flags.self_suspended)
2336 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2341 if (usb_peer_can_wakeup(udev) &&
2342 usb_device_20_compatible(udev)) {
2344 * This request needs to be done before we set
2345 * "udev->flags.self_suspended":
2348 /* allow device to do remote wakeup */
2349 err = usbd_req_set_device_feature(udev,
2350 NULL, UF_DEVICE_REMOTE_WAKEUP);
2352 DPRINTFN(0, "Setting device "
2353 "remote wakeup failed\n");
2357 USB_BUS_LOCK(udev->bus);
2359 * Checking for suspend condition and setting suspended bit
2362 err = usb_peer_should_wakeup(udev);
2365 * Set that this device is suspended. This variable
2366 * must be set before calling USB controller suspend
2369 udev->flags.self_suspended = 1;
2371 USB_BUS_UNLOCK(udev->bus);
2374 if (usb_peer_can_wakeup(udev) &&
2375 usb_device_20_compatible(udev)) {
2376 /* allow device to do remote wakeup */
2377 err = usbd_req_clear_device_feature(udev,
2378 NULL, UF_DEVICE_REMOTE_WAKEUP);
2380 DPRINTFN(0, "Setting device "
2381 "remote wakeup failed\n");
2385 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2386 /* resume parent HUB first */
2387 usb_dev_resume_peer(udev->parent_hub);
2389 /* reduce chance of instant resume failure by waiting a little bit */
2390 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2392 /* resume current port (Valid in Host and Device Mode) */
2393 err = usbd_req_clear_port_feature(udev->parent_hub,
2394 NULL, udev->port_no, UHF_PORT_SUSPEND);
2396 /* resume settle time */
2397 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2399 DPRINTF("Suspend was cancelled!\n");
2405 /* notify all sub-devices about suspend */
2406 err = usb_suspend_resume(udev, 1);
2408 usbd_sr_unlock(udev);
2410 if (udev->bus->methods->device_suspend != NULL) {
2413 /* suspend device on the USB controller */
2414 (udev->bus->methods->device_suspend) (udev);
2417 temp = usbd_get_dma_delay(udev);
2419 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2423 if (usb_device_20_compatible(udev)) {
2424 /* suspend current port */
2425 err = usbd_req_set_port_feature(udev->parent_hub,
2426 NULL, udev->port_no, UHF_PORT_SUSPEND);
2428 DPRINTFN(0, "Suspending port failed\n");
2433 udev = udev->parent_hub;
2437 /*------------------------------------------------------------------------*
2438 * usbd_set_power_mode
2440 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2442 *------------------------------------------------------------------------*/
2444 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2446 /* filter input argument */
2447 if ((power_mode != USB_POWER_MODE_ON) &&
2448 (power_mode != USB_POWER_MODE_OFF))
2449 power_mode = USB_POWER_MODE_SAVE;
2451 power_mode = usbd_filter_power_mode(udev, power_mode);
2453 udev->power_mode = power_mode; /* update copy of power mode */
2456 usb_bus_power_update(udev->bus);
2460 /*------------------------------------------------------------------------*
2461 * usbd_filter_power_mode
2463 * This function filters the power mode based on hardware requirements.
2464 *------------------------------------------------------------------------*/
2466 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2468 struct usb_bus_methods *mtod;
2471 mtod = udev->bus->methods;
2474 if (mtod->get_power_mode != NULL)
2475 (mtod->get_power_mode) (udev, &temp);
2477 /* check if we should not filter */
2479 return (power_mode);
2481 /* use fixed power mode given by hardware driver */
2485 /*------------------------------------------------------------------------*
2486 * usbd_start_re_enumerate
2488 * This function starts re-enumeration of the given USB device. This
2489 * function does not need to be called BUS-locked. This function does
2490 * not wait until the re-enumeration is completed.
2491 *------------------------------------------------------------------------*/
2493 usbd_start_re_enumerate(struct usb_device *udev)
2495 if (udev->re_enumerate_wait == 0) {
2496 udev->re_enumerate_wait = 1;
2497 usb_needs_explore(udev->bus, 0);