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/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR uhub_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_request.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_hub.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_controller.h>
70 #include <dev/usb/usb_bus.h>
72 #define UHUB_INTR_INTERVAL 250 /* ms */
73 #define UHUB_N_TRANSFER 1
76 static int uhub_debug = 0;
78 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
79 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
81 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
85 static int usb_power_timeout = 30; /* seconds */
87 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
88 &usb_power_timeout, 0, "USB power timeout");
91 struct uhub_current_state {
97 struct uhub_current_state sc_st;/* current state */
98 device_t sc_dev; /* base device */
99 struct mtx sc_mtx; /* our mutex */
100 struct usb_device *sc_udev; /* USB device */
101 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
103 #define UHUB_FLAG_DID_EXPLORE 0x01
107 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
108 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
109 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
110 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
111 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
113 /* prototypes for type checking: */
115 static device_probe_t uhub_probe;
116 static device_attach_t uhub_attach;
117 static device_detach_t uhub_detach;
118 static device_suspend_t uhub_suspend;
119 static device_resume_t uhub_resume;
121 static bus_driver_added_t uhub_driver_added;
122 static bus_child_location_str_t uhub_child_location_string;
123 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
125 static usb_callback_t uhub_intr_callback;
127 static void usb_dev_resume_peer(struct usb_device *udev);
128 static void usb_dev_suspend_peer(struct usb_device *udev);
129 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
131 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
134 .type = UE_INTERRUPT,
135 .endpoint = UE_ADDR_ANY,
136 .direction = UE_DIR_ANY,
138 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
139 .bufsize = 0, /* use wMaxPacketSize */
140 .callback = &uhub_intr_callback,
141 .interval = UHUB_INTR_INTERVAL,
146 * driver instance for "hub" connected to "usb"
147 * and "hub" connected to "hub"
149 static devclass_t uhub_devclass;
151 static device_method_t uhub_methods[] = {
152 DEVMETHOD(device_probe, uhub_probe),
153 DEVMETHOD(device_attach, uhub_attach),
154 DEVMETHOD(device_detach, uhub_detach),
156 DEVMETHOD(device_suspend, uhub_suspend),
157 DEVMETHOD(device_resume, uhub_resume),
159 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
160 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
161 DEVMETHOD(bus_driver_added, uhub_driver_added),
165 static driver_t uhub_driver = {
167 .methods = uhub_methods,
168 .size = sizeof(struct uhub_softc)
171 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
172 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
173 MODULE_VERSION(uhub, 1);
176 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
178 struct uhub_softc *sc = usbd_xfer_softc(xfer);
180 switch (USB_GET_STATE(xfer)) {
181 case USB_ST_TRANSFERRED:
184 * This is an indication that some port
185 * has changed status. Notify the bus
186 * event handler thread that we need
187 * to be explored again:
189 usb_needs_explore(sc->sc_udev->bus, 0);
192 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
193 usbd_transfer_submit(xfer);
197 if (xfer->error != USB_ERR_CANCELLED) {
199 * Do a clear-stall. The "stall_pipe" flag
200 * will get cleared before next callback by
203 usbd_xfer_set_stall(xfer);
204 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
205 usbd_transfer_submit(xfer);
211 /*------------------------------------------------------------------------*
212 * uhub_explore_sub - subroutine
216 * Else: A control transaction failed
217 *------------------------------------------------------------------------*/
219 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
222 struct usb_device *child;
226 bus = sc->sc_udev->bus;
229 /* get driver added refcount from USB bus */
230 refcount = bus->driver_added_refcount;
232 /* get device assosiated with the given port */
233 child = usb_bus_port_get_device(bus, up);
239 /* check if device should be re-enumerated */
241 if (child->flags.usb_mode == USB_MODE_HOST) {
244 do_unlock = usbd_enum_lock(child);
245 switch (child->re_enumerate_wait) {
246 case USB_RE_ENUM_START:
247 err = usbd_set_config_index(child,
250 DPRINTF("Unconfigure failed: "
254 err = usbd_req_re_enumerate(child, NULL);
256 err = usbd_set_config_index(child, 0);
258 err = usb_probe_and_attach(child,
259 USB_IFACE_INDEX_ANY);
261 child->re_enumerate_wait = USB_RE_ENUM_DONE;
265 case USB_RE_ENUM_PWR_OFF:
266 /* get the device unconfigured */
267 err = usbd_set_config_index(child,
270 DPRINTFN(0, "Could not unconfigure "
271 "device (ignored)\n");
274 /* clear port enable */
275 err = usbd_req_clear_port_feature(child->parent_hub,
276 NULL, child->port_no, UHF_PORT_ENABLE);
278 DPRINTFN(0, "Could not disable port "
281 child->re_enumerate_wait = USB_RE_ENUM_DONE;
286 child->re_enumerate_wait = USB_RE_ENUM_DONE;
290 usbd_enum_unlock(child);
293 /* check if probe and attach should be done */
295 if (child->driver_added_refcount != refcount) {
296 child->driver_added_refcount = refcount;
297 err = usb_probe_and_attach(child,
298 USB_IFACE_INDEX_ANY);
303 /* start control transfer, if device mode */
305 if (child->flags.usb_mode == USB_MODE_DEVICE)
306 usbd_ctrl_transfer_setup(child);
308 /* if a HUB becomes present, do a recursive HUB explore */
311 err = (child->hub->explore) (child);
317 /*------------------------------------------------------------------------*
318 * uhub_read_port_status - factored out code
319 *------------------------------------------------------------------------*/
321 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
323 struct usb_port_status ps;
326 err = usbd_req_get_port_status(
327 sc->sc_udev, NULL, &ps, portno);
329 /* update status regardless of error */
331 sc->sc_st.port_status = UGETW(ps.wPortStatus);
332 sc->sc_st.port_change = UGETW(ps.wPortChange);
334 /* debugging print */
336 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
337 "wPortChange=0x%04x, err=%s\n",
338 portno, sc->sc_st.port_status,
339 sc->sc_st.port_change, usbd_errstr(err));
343 /*------------------------------------------------------------------------*
348 * Else: A control transaction failed
349 *------------------------------------------------------------------------*/
351 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
353 struct usb_device *child;
354 struct usb_device *udev;
355 enum usb_dev_speed speed;
356 enum usb_hc_mode mode;
361 DPRINTF("reattaching port %d\n", portno);
366 child = usb_bus_port_get_device(udev->bus,
367 udev->hub->ports + portno - 1);
371 /* first clear the port connection change bit */
373 err = usbd_req_clear_port_feature(udev, NULL,
374 portno, UHF_C_PORT_CONNECTION);
379 /* check if there is a child */
383 * Free USB device and all subdevices, if any.
385 usb_free_device(child, 0);
388 /* get fresh status */
390 err = uhub_read_port_status(sc, portno);
394 /* check if nothing is connected to the port */
396 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
399 /* check if there is no power on the port and print a warning */
401 switch (udev->speed) {
405 power_mask = UPS_PORT_POWER;
407 case USB_SPEED_SUPER:
408 if (udev->parent_hub == NULL)
409 power_mask = UPS_PORT_POWER;
411 power_mask = UPS_PORT_POWER_SS;
417 if (!(sc->sc_st.port_status & power_mask)) {
418 DPRINTF("WARNING: strange, connected port %d "
419 "has no power\n", portno);
422 /* check if the device is in Host Mode */
424 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
426 DPRINTF("Port %d is in Host Mode\n", portno);
428 if (sc->sc_st.port_status & UPS_SUSPEND) {
430 * NOTE: Should not get here in SuperSpeed
431 * mode, because the HUB should report this
434 DPRINTF("Port %d was still "
435 "suspended, clearing.\n", portno);
436 err = usbd_req_clear_port_feature(udev,
437 NULL, portno, UHF_PORT_SUSPEND);
442 /* wait for maximum device power up time */
445 USB_MS_TO_TICKS(usb_port_powerup_delay));
447 /* reset port, which implies enabling it */
449 err = usbd_req_reset_port(udev, NULL, portno);
452 DPRINTFN(0, "port %d reset "
453 "failed, error=%s\n",
454 portno, usbd_errstr(err));
457 /* get port status again, it might have changed during reset */
459 err = uhub_read_port_status(sc, portno);
463 /* check if something changed during port reset */
465 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
466 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
468 DPRINTFN(0, "giving up port reset "
469 "- device vanished\n");
476 DPRINTF("Port %d is in Device Mode\n", portno);
480 * Figure out the device speed
482 switch (udev->speed) {
484 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
485 speed = USB_SPEED_HIGH;
486 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
487 speed = USB_SPEED_LOW;
489 speed = USB_SPEED_FULL;
492 if (sc->sc_st.port_status & UPS_LOW_SPEED)
493 speed = USB_SPEED_LOW;
495 speed = USB_SPEED_FULL;
498 speed = USB_SPEED_LOW;
500 case USB_SPEED_SUPER:
501 if (udev->parent_hub == NULL) {
502 /* Root HUB - special case */
503 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
505 speed = USB_SPEED_FULL;
508 speed = USB_SPEED_LOW;
511 speed = USB_SPEED_HIGH;
514 speed = USB_SPEED_SUPER;
518 speed = USB_SPEED_SUPER;
522 /* same speed like parent */
526 if (speed == USB_SPEED_SUPER) {
527 err = usbd_req_set_hub_u1_timeout(udev, NULL,
528 portno, 128 - (2 * udev->depth));
530 DPRINTFN(0, "port %d U1 timeout "
531 "failed, error=%s\n",
532 portno, usbd_errstr(err));
534 err = usbd_req_set_hub_u2_timeout(udev, NULL,
535 portno, 128 - (2 * udev->depth));
537 DPRINTFN(0, "port %d U2 timeout "
538 "failed, error=%s\n",
539 portno, usbd_errstr(err));
544 * Figure out the device mode
546 * NOTE: This part is currently FreeBSD specific.
548 if (udev->parent_hub != NULL) {
549 /* inherit mode from the parent HUB */
550 mode = udev->parent_hub->flags.usb_mode;
551 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
552 mode = USB_MODE_DEVICE;
554 mode = USB_MODE_HOST;
556 /* need to create a new child */
557 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
558 udev->depth + 1, portno - 1, portno, speed, mode);
560 DPRINTFN(0, "could not allocate new device\n");
563 return (0); /* success */
568 * Free USB device and all subdevices, if any.
570 usb_free_device(child, 0);
574 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
575 err = usbd_req_clear_port_feature(
577 portno, UHF_PORT_ENABLE);
581 DPRINTFN(0, "device problem (%s), "
582 "disabling port %d\n", usbd_errstr(err), portno);
587 /*------------------------------------------------------------------------*
588 * usb_device_20_compatible
591 * 0: HUB does not support suspend and resume
592 * Else: HUB supports suspend and resume
593 *------------------------------------------------------------------------*/
595 usb_device_20_compatible(struct usb_device *udev)
599 switch (udev->speed) {
609 /*------------------------------------------------------------------------*
610 * uhub_suspend_resume_port
614 * Else: A control transaction failed
615 *------------------------------------------------------------------------*/
617 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
619 struct usb_device *child;
620 struct usb_device *udev;
624 DPRINTF("port %d\n", portno);
627 child = usb_bus_port_get_device(udev->bus,
628 udev->hub->ports + portno - 1);
630 /* first clear the port suspend change bit */
632 if (usb_device_20_compatible(udev)) {
633 err = usbd_req_clear_port_feature(udev, NULL,
634 portno, UHF_C_PORT_SUSPEND);
636 err = usbd_req_clear_port_feature(udev, NULL,
637 portno, UHF_C_PORT_LINK_STATE);
641 DPRINTF("clearing suspend failed.\n");
644 /* get fresh status */
646 err = uhub_read_port_status(sc, portno);
648 DPRINTF("reading port status failed.\n");
651 /* convert current state */
653 if (usb_device_20_compatible(udev)) {
654 if (sc->sc_st.port_status & UPS_SUSPEND) {
660 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
664 case UPS_PORT_LS_SS_INA:
665 usbd_req_warm_reset_port(udev, NULL, portno);
674 DPRINTF("suspended=%u\n", is_suspend);
676 /* do the suspend or resume */
680 * This code handle two cases: 1) Host Mode - we can only
681 * receive resume here 2) Device Mode - we can receive
682 * suspend and resume here
685 usb_dev_resume_peer(child);
686 else if (child->flags.usb_mode == USB_MODE_DEVICE)
687 usb_dev_suspend_peer(child);
693 /*------------------------------------------------------------------------*
694 * uhub_root_interrupt
696 * This function is called when a Root HUB interrupt has
697 * happened. "ptr" and "len" makes up the Root HUB interrupt
698 * packet. This function is called having the "bus_mtx" locked.
699 *------------------------------------------------------------------------*/
701 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
703 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
705 usb_needs_explore(bus, 0);
709 uhub_is_too_deep(struct usb_device *udev)
711 switch (udev->speed) {
715 if (udev->depth > USB_HUB_MAX_DEPTH)
718 case USB_SPEED_SUPER:
719 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
728 /*------------------------------------------------------------------------*
734 *------------------------------------------------------------------------*/
736 uhub_explore(struct usb_device *udev)
739 struct uhub_softc *sc;
749 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
751 /* ignore devices that are too deep */
752 if (uhub_is_too_deep(udev))
753 return (USB_ERR_TOO_DEEP);
755 /* check if device is suspended */
756 if (udev->flags.self_suspended) {
757 /* need to wait until the child signals resume */
758 DPRINTF("Device is suspended!\n");
763 * Make sure we don't race against user-space applications
766 do_unlock = usbd_enum_lock(udev);
768 for (x = 0; x != hub->nports; x++) {
772 err = uhub_read_port_status(sc, portno);
774 /* most likely the HUB is gone */
777 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
778 DPRINTF("Overcurrent on port %u.\n", portno);
779 err = usbd_req_clear_port_feature(
780 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
782 /* most likely the HUB is gone */
786 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
788 * Fake a connect status change so that the
789 * status gets checked initially!
791 sc->sc_st.port_change |=
792 UPS_C_CONNECT_STATUS;
794 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
795 err = usbd_req_clear_port_feature(
796 udev, NULL, portno, UHF_C_PORT_ENABLE);
798 /* most likely the HUB is gone */
801 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
803 * Ignore the port error if the device
806 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
807 DPRINTFN(0, "illegal enable change, "
808 "port %d\n", portno);
811 if (up->restartcnt == USB_RESTART_MAX) {
812 /* XXX could try another speed ? */
813 DPRINTFN(0, "port error, giving up "
814 "port %d\n", portno);
816 sc->sc_st.port_change |=
817 UPS_C_CONNECT_STATUS;
822 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
823 err = uhub_reattach_port(sc, portno);
825 /* most likely the HUB is gone */
829 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
830 UPS_C_PORT_LINK_STATE)) {
831 err = uhub_suspend_resume_port(sc, portno);
833 /* most likely the HUB is gone */
837 err = uhub_explore_sub(sc, up);
839 /* no device(s) present */
842 /* explore succeeded - reset restart counter */
847 usbd_enum_unlock(udev);
849 /* initial status checked */
850 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
853 return (USB_ERR_NORMAL_COMPLETION);
857 uhub_probe(device_t dev)
859 struct usb_attach_arg *uaa = device_get_ivars(dev);
861 if (uaa->usb_mode != USB_MODE_HOST)
865 * The subclass for USB HUBs is currently ignored because it
866 * is 0 for some and 1 for others.
868 if (uaa->info.bConfigIndex == 0 &&
869 uaa->info.bDeviceClass == UDCLASS_HUB)
875 /* NOTE: The information returned by this function can be wrong. */
877 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
879 struct usb_hub_descriptor hubdesc20;
880 struct usb_hub_ss_descriptor hubdesc30;
885 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
886 return (USB_ERR_INVAL);
891 switch (udev->speed) {
895 /* assuming that there is one port */
896 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
898 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
899 "error=%s\n", usbd_errstr(err));
902 nports = hubdesc20.bNbrPorts;
906 if (udev->speed == USB_SPEED_HIGH)
907 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
910 case USB_SPEED_SUPER:
911 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
913 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
914 "error=%s\n", usbd_errstr(err));
917 nports = hubdesc30.bNbrPorts;
937 uhub_attach(device_t dev)
939 struct uhub_softc *sc = device_get_softc(dev);
940 struct usb_attach_arg *uaa = device_get_ivars(dev);
941 struct usb_device *udev = uaa->device;
942 struct usb_device *parent_hub = udev->parent_hub;
944 struct usb_hub_descriptor hubdesc20;
945 struct usb_hub_ss_descriptor hubdesc30;
957 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
959 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
960 device_get_nameunit(dev));
962 device_set_usb_desc(dev);
964 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
965 "parent->selfpowered=%d\n",
967 udev->flags.self_powered,
970 parent_hub->flags.self_powered : 0);
972 if (uhub_is_too_deep(udev)) {
973 DPRINTFN(0, "HUB at depth %d, "
974 "exceeds maximum. HUB ignored\n", (int)udev->depth);
978 if (!udev->flags.self_powered && parent_hub &&
979 !parent_hub->flags.self_powered) {
980 DPRINTFN(0, "Bus powered HUB connected to "
981 "bus powered HUB. HUB ignored\n");
985 if (UHUB_IS_MULTI_TT(sc)) {
986 err = usbd_set_alt_interface_index(udev, 0, 1);
988 device_printf(dev, "MTT could not be enabled\n");
991 device_printf(dev, "MTT enabled\n");
994 /* get HUB descriptor */
996 DPRINTFN(2, "Getting HUB descriptor\n");
998 switch (udev->speed) {
1000 case USB_SPEED_FULL:
1001 case USB_SPEED_HIGH:
1002 /* assuming that there is one port */
1003 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1005 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1006 "error=%s\n", usbd_errstr(err));
1009 /* get number of ports */
1010 nports = hubdesc20.bNbrPorts;
1012 /* get power delay */
1013 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1014 usb_extra_power_up_time);
1016 /* get complete HUB descriptor */
1018 /* check number of ports */
1020 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1021 "error=%s\n", usbd_errstr(err));
1024 /* get complete HUB descriptor */
1025 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1028 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1029 "error=%s\n", usbd_errstr(err));
1032 if (hubdesc20.bNbrPorts != nports) {
1033 DPRINTFN(0, "Number of ports changed\n");
1038 case USB_SPEED_SUPER:
1039 if (udev->parent_hub != NULL) {
1040 err = usbd_req_set_hub_depth(udev, NULL,
1043 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1044 "error=%s\n", usbd_errstr(err));
1048 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1050 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1051 "error=%s\n", usbd_errstr(err));
1054 /* get number of ports */
1055 nports = hubdesc30.bNbrPorts;
1057 /* get power delay */
1058 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1059 usb_extra_power_up_time);
1061 /* get complete HUB descriptor */
1063 /* check number of ports */
1064 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1065 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1066 "error=%s\n", usbd_errstr(err));
1069 /* get complete HUB descriptor */
1070 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1073 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1074 "error=%s\n", usbd_errstr(err));
1077 if (hubdesc30.bNbrPorts != nports) {
1078 DPRINTFN(0, "Number of ports changed\n");
1084 DPRINTF("Assuming HUB has only one port\n");
1085 /* default number of ports */
1087 /* default power delay */
1088 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1092 DPRINTFN(0, "portless HUB\n");
1095 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1096 M_USBDEV, M_WAITOK | M_ZERO);
1103 /* initialize HUB structure */
1105 hub->explore = &uhub_explore;
1106 hub->nports = nports;
1107 hub->hubudev = udev;
1109 /* if self powered hub, give ports maximum current */
1110 if (udev->flags.self_powered) {
1111 hub->portpower = USB_MAX_POWER;
1113 hub->portpower = USB_MIN_POWER;
1116 /* set up interrupt pipe */
1118 if (udev->parent_hub == NULL) {
1119 /* root HUB is special */
1123 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1124 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1127 DPRINTFN(0, "cannot setup interrupt transfer, "
1128 "errstr=%s\n", usbd_errstr(err));
1131 /* wait with power off for a while */
1132 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1135 * To have the best chance of success we do things in the exact same
1136 * order as Windoze98. This should not be necessary, but some
1137 * devices do not follow the USB specs to the letter.
1139 * These are the events on the bus when a hub is attached:
1140 * Get device and config descriptors (see attach code)
1141 * Get hub descriptor (see above)
1144 * wait for power to become stable
1145 * (all below happens in explore code)
1147 * clear C_PORT_CONNECTION
1150 * if device connected
1154 * clear C_PORT_RESET
1156 * proceed with device attachment
1159 /* XXX should check for none, individual, or ganged power? */
1163 for (x = 0; x != nports; x++) {
1164 /* set up data structures */
1165 struct usb_port *up = hub->ports + x;
1167 up->device_index = 0;
1171 /* check if port is removable */
1172 switch (udev->speed) {
1174 case USB_SPEED_FULL:
1175 case USB_SPEED_HIGH:
1176 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1179 case USB_SPEED_SUPER:
1180 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1184 DPRINTF("Assuming removable port\n");
1189 /* turn the power on */
1190 err = usbd_req_set_port_feature(udev, NULL,
1191 portno, UHF_PORT_POWER);
1194 DPRINTFN(0, "port %d power on failed, %s\n",
1195 portno, usbd_errstr(err));
1197 DPRINTF("turn on port %d power\n",
1200 /* wait for stable power */
1201 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1204 device_printf(dev, "%d port%s with %d "
1205 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1206 removable, udev->flags.self_powered ? "self" : "bus");
1208 /* Start the interrupt endpoint, if any */
1210 if (sc->sc_xfer[0] != NULL) {
1211 mtx_lock(&sc->sc_mtx);
1212 usbd_transfer_start(sc->sc_xfer[0]);
1213 mtx_unlock(&sc->sc_mtx);
1216 /* Enable automatic power save on all USB HUBs */
1218 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1223 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1226 free(udev->hub, M_USBDEV);
1230 mtx_destroy(&sc->sc_mtx);
1236 * Called from process context when the hub is gone.
1237 * Detach all devices on active ports.
1240 uhub_detach(device_t dev)
1242 struct uhub_softc *sc = device_get_softc(dev);
1243 struct usb_hub *hub = sc->sc_udev->hub;
1244 struct usb_device *child;
1247 if (hub == NULL) /* must be partially working */
1250 /* Make sure interrupt transfer is gone. */
1251 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1253 /* Detach all ports */
1254 for (x = 0; x != hub->nports; x++) {
1256 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1258 if (child == NULL) {
1263 * Free USB device and all subdevices, if any.
1265 usb_free_device(child, 0);
1268 free(hub, M_USBDEV);
1269 sc->sc_udev->hub = NULL;
1271 mtx_destroy(&sc->sc_mtx);
1277 uhub_suspend(device_t dev)
1280 /* Sub-devices are not suspended here! */
1285 uhub_resume(device_t dev)
1288 /* Sub-devices are not resumed here! */
1293 uhub_driver_added(device_t dev, driver_t *driver)
1295 usb_needs_explore_all();
1299 struct usb_device *udev;
1301 uint8_t iface_index;
1305 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1306 struct hub_result *res)
1308 struct usb_interface *iface;
1309 struct usb_device *udev;
1314 nports = hub->nports;
1315 for (x = 0; x != nports; x++) {
1316 udev = usb_bus_port_get_device(hub->hubudev->bus,
1321 for (i = 0; i != USB_IFACE_MAX; i++) {
1322 iface = usbd_get_iface(udev, i);
1324 (iface->subdev == child)) {
1325 res->iface_index = i;
1327 res->portno = x + 1;
1332 res->iface_index = 0;
1338 uhub_child_location_string(device_t parent, device_t child,
1339 char *buf, size_t buflen)
1341 struct uhub_softc *sc;
1342 struct usb_hub *hub;
1343 struct hub_result res;
1345 if (!device_is_attached(parent)) {
1351 sc = device_get_softc(parent);
1352 hub = sc->sc_udev->hub;
1355 uhub_find_iface_index(hub, child, &res);
1357 DPRINTF("device not on hub\n");
1363 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1364 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1365 res.portno, device_get_unit(res.udev->bus->bdev),
1366 res.udev->device_index, res.iface_index);
1374 uhub_child_pnpinfo_string(device_t parent, device_t child,
1375 char *buf, size_t buflen)
1377 struct uhub_softc *sc;
1378 struct usb_hub *hub;
1379 struct usb_interface *iface;
1380 struct hub_result res;
1382 if (!device_is_attached(parent)) {
1388 sc = device_get_softc(parent);
1389 hub = sc->sc_udev->hub;
1392 uhub_find_iface_index(hub, child, &res);
1394 DPRINTF("device not on hub\n");
1400 iface = usbd_get_iface(res.udev, res.iface_index);
1401 if (iface && iface->idesc) {
1402 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1403 "devclass=0x%02x devsubclass=0x%02x "
1407 "intclass=0x%02x intsubclass=0x%02x "
1408 "intprotocol=0x%02x " "%s%s",
1409 UGETW(res.udev->ddesc.idVendor),
1410 UGETW(res.udev->ddesc.idProduct),
1411 res.udev->ddesc.bDeviceClass,
1412 res.udev->ddesc.bDeviceSubClass,
1413 usb_get_serial(res.udev),
1414 UGETW(res.udev->ddesc.bcdDevice),
1415 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1416 iface->idesc->bInterfaceClass,
1417 iface->idesc->bInterfaceSubClass,
1418 iface->idesc->bInterfaceProtocol,
1419 iface->pnpinfo ? " " : "",
1420 iface->pnpinfo ? iface->pnpinfo : "");
1434 * The USB Transaction Translator:
1435 * ===============================
1437 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1438 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1439 * USB transfers. To utilize bandwidth dynamically the "scatter and
1440 * gather" principle must be applied. This means that bandwidth must
1441 * be divided into equal parts of bandwidth. With regard to USB all
1442 * data is transferred in smaller packets with length
1443 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1446 * The bandwidth scheduler which I have implemented will simply pack
1447 * the USB transfers back to back until there is no more space in the
1448 * schedule. Out of the 8 microframes which the USB 2.0 standard
1449 * provides, only 6 are available for non-HIGH-speed devices. I have
1450 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1451 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1452 * this division, it is very difficult to allocate and free bandwidth
1455 * NOTE about the Transaction Translator in USB HUBs:
1457 * USB HUBs have a very simple Transaction Translator, that will
1458 * simply pipeline all the SPLIT transactions. That means that the
1459 * transactions will be executed in the order they are queued!
1463 /*------------------------------------------------------------------------*
1464 * usb_intr_find_best_slot
1467 * The best Transaction Translation slot for an interrupt endpoint.
1468 *------------------------------------------------------------------------*/
1470 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1471 uint8_t end, uint8_t mask)
1473 usb_size_t min = (usb_size_t)-1;
1481 /* find the last slot with lesser used bandwidth */
1483 for (x = start; x < end; x++) {
1487 /* compute sum of bandwidth */
1488 for (z = x; z < end; z++) {
1489 if (mask & (1U << (z - x)))
1493 /* check if the current multi-slot is more optimal */
1499 /* check if the mask is about to be shifted out */
1500 if (mask & (1U << (end - 1 - x)))
1506 /*------------------------------------------------------------------------*
1507 * usb_hs_bandwidth_adjust
1509 * This function will update the bandwith usage for the microframe
1510 * having index "slot" by "len" bytes. "len" can be negative. If the
1511 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1512 * the "slot" argument will be replaced by the slot having least used
1513 * bandwidth. The "mask" argument is used for multi-slot allocations.
1516 * The slot in which the bandwidth update was done: 0..7
1517 *------------------------------------------------------------------------*/
1519 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1520 uint8_t slot, uint8_t mask)
1522 struct usb_bus *bus = udev->bus;
1523 struct usb_hub *hub;
1524 enum usb_dev_speed speed;
1527 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1529 speed = usbd_get_speed(udev);
1533 case USB_SPEED_FULL:
1534 if (speed == USB_SPEED_LOW) {
1538 * The Host Controller Driver should have
1539 * performed checks so that the lookup
1540 * below does not result in a NULL pointer
1544 hub = udev->parent_hs_hub->hub;
1545 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1546 slot = usb_intr_find_best_slot(hub->uframe_usage,
1547 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1549 for (x = slot; x < 8; x++) {
1550 if (mask & (1U << (x - slot))) {
1551 hub->uframe_usage[x] += len;
1552 bus->uframe_usage[x] += len;
1557 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1558 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1559 USB_HS_MICRO_FRAMES_MAX, mask);
1561 for (x = slot; x < 8; x++) {
1562 if (mask & (1U << (x - slot))) {
1563 bus->uframe_usage[x] += len;
1571 /*------------------------------------------------------------------------*
1572 * usb_hs_bandwidth_alloc
1574 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1575 *------------------------------------------------------------------------*/
1577 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1579 struct usb_device *udev;
1584 udev = xfer->xroot->udev;
1586 if (udev->flags.usb_mode != USB_MODE_HOST)
1587 return; /* not supported */
1589 xfer->endpoint->refcount_bw++;
1590 if (xfer->endpoint->refcount_bw != 1)
1591 return; /* already allocated */
1593 speed = usbd_get_speed(udev);
1595 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1597 /* allocate a microframe slot */
1600 slot = usb_hs_bandwidth_adjust(udev,
1601 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1603 xfer->endpoint->usb_uframe = slot;
1604 xfer->endpoint->usb_smask = mask << slot;
1606 if ((speed != USB_SPEED_FULL) &&
1607 (speed != USB_SPEED_LOW)) {
1608 xfer->endpoint->usb_cmask = 0x00 ;
1610 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1614 case UE_ISOCHRONOUS:
1615 switch (usbd_xfer_get_fps_shift(xfer)) {
1630 /* allocate a microframe multi-slot */
1632 slot = usb_hs_bandwidth_adjust(udev,
1633 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1635 xfer->endpoint->usb_uframe = slot;
1636 xfer->endpoint->usb_cmask = 0;
1637 xfer->endpoint->usb_smask = mask << slot;
1641 xfer->endpoint->usb_uframe = 0;
1642 xfer->endpoint->usb_cmask = 0;
1643 xfer->endpoint->usb_smask = 0;
1647 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1648 xfer->endpoint->usb_uframe,
1649 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1652 /*------------------------------------------------------------------------*
1653 * usb_hs_bandwidth_free
1655 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1656 *------------------------------------------------------------------------*/
1658 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1660 struct usb_device *udev;
1664 udev = xfer->xroot->udev;
1666 if (udev->flags.usb_mode != USB_MODE_HOST)
1667 return; /* not supported */
1669 xfer->endpoint->refcount_bw--;
1670 if (xfer->endpoint->refcount_bw != 0)
1671 return; /* still allocated */
1673 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1675 case UE_ISOCHRONOUS:
1677 slot = xfer->endpoint->usb_uframe;
1678 mask = xfer->endpoint->usb_smask;
1680 /* free microframe slot(s): */
1681 usb_hs_bandwidth_adjust(udev,
1682 -xfer->max_frame_size, slot, mask >> slot);
1684 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1685 slot, mask >> slot);
1687 xfer->endpoint->usb_uframe = 0;
1688 xfer->endpoint->usb_cmask = 0;
1689 xfer->endpoint->usb_smask = 0;
1697 /*------------------------------------------------------------------------*
1698 * usb_isoc_time_expand
1700 * This function will expand the time counter from 7-bit to 16-bit.
1703 * 16-bit isochronous time counter.
1704 *------------------------------------------------------------------------*/
1706 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1710 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1712 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1714 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1716 if (isoc_time_curr < rem) {
1717 /* the time counter wrapped around */
1718 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1720 /* update the remainder */
1722 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1723 bus->isoc_time_last |= isoc_time_curr;
1725 return (bus->isoc_time_last);
1728 /*------------------------------------------------------------------------*
1729 * usbd_fs_isoc_schedule_alloc_slot
1731 * This function will allocate bandwidth for an isochronous FULL speed
1732 * transaction in the FULL speed schedule.
1737 *------------------------------------------------------------------------*/
1738 #if USB_HAVE_TT_SUPPORT
1740 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1742 struct usb_xfer *xfer;
1743 struct usb_xfer *pipe_xfer;
1744 struct usb_bus *bus;
1746 usb_frlength_t data_len;
1754 bus = isoc_xfer->xroot->bus;
1756 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1758 /* skip self, if any */
1760 if (xfer == isoc_xfer)
1763 /* check if this USB transfer is going through the same TT */
1765 if (xfer->xroot->udev->parent_hs_hub !=
1766 isoc_xfer->xroot->udev->parent_hs_hub) {
1769 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1770 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1771 (xfer->xroot->udev->hs_port_no !=
1772 isoc_xfer->xroot->udev->hs_port_no)) {
1775 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1778 /* check if isoc_time is part of this transfer */
1780 delta = xfer->isoc_time_complete - isoc_time;
1781 if (delta > 0 && delta <= xfer->nframes) {
1782 delta = xfer->nframes - delta;
1784 len = xfer->frlengths[delta];
1792 /* check double buffered transfers */
1794 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
1797 /* skip self, if any */
1799 if (pipe_xfer == isoc_xfer)
1802 /* check if isoc_time is part of this transfer */
1804 delta = pipe_xfer->isoc_time_complete - isoc_time;
1805 if (delta > 0 && delta <= pipe_xfer->nframes) {
1806 delta = pipe_xfer->nframes - delta;
1808 len = pipe_xfer->frlengths[delta];
1818 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1819 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1823 /* check for overflow */
1825 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1830 delta = isoc_xfer->isoc_time_complete - isoc_time;
1831 if (delta > 0 && delta <= isoc_xfer->nframes) {
1832 delta = isoc_xfer->nframes - delta;
1834 len = isoc_xfer->frlengths[delta];
1842 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1843 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1847 /* check for overflow */
1849 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1856 /*------------------------------------------------------------------------*
1857 * usb_bus_port_get_device
1859 * This function is NULL safe.
1860 *------------------------------------------------------------------------*/
1862 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1864 if ((bus == NULL) || (up == NULL)) {
1868 if (up->device_index == 0) {
1872 return (bus->devices[up->device_index]);
1875 /*------------------------------------------------------------------------*
1876 * usb_bus_port_set_device
1878 * This function is NULL safe.
1879 *------------------------------------------------------------------------*/
1881 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1882 struct usb_device *udev, uint8_t device_index)
1889 * There is only one case where we don't
1890 * have an USB port, and that is the Root Hub!
1894 up->device_index = device_index;
1896 device_index = up->device_index;
1897 up->device_index = 0;
1901 * Make relationships to our new device
1903 if (device_index != 0) {
1905 mtx_lock(&usb_ref_lock);
1907 bus->devices[device_index] = udev;
1909 mtx_unlock(&usb_ref_lock);
1915 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1918 /*------------------------------------------------------------------------*
1921 * This functions is called when the USB event thread needs to run.
1922 *------------------------------------------------------------------------*/
1924 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1931 DPRINTF("No bus pointer!\n");
1934 if ((bus->devices == NULL) ||
1935 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1936 DPRINTF("No root HUB\n");
1939 if (mtx_owned(&bus->bus_mtx)) {
1948 if (usb_proc_msignal(&bus->explore_proc,
1949 &bus->explore_msg[0], &bus->explore_msg[1])) {
1953 USB_BUS_UNLOCK(bus);
1957 /*------------------------------------------------------------------------*
1958 * usb_needs_explore_all
1960 * This function is called whenever a new driver is loaded and will
1961 * cause that all USB busses are re-explored.
1962 *------------------------------------------------------------------------*/
1964 usb_needs_explore_all(void)
1966 struct usb_bus *bus;
1973 dc = usb_devclass_ptr;
1975 DPRINTFN(0, "no devclass\n");
1979 * Explore all USB busses in parallell.
1981 max = devclass_get_maxunit(dc);
1983 dev = devclass_get_device(dc, max);
1985 bus = device_get_softc(dev);
1987 usb_needs_explore(bus, 1);
1994 /*------------------------------------------------------------------------*
1995 * usb_bus_power_update
1997 * This function will ensure that all USB devices on the given bus are
1998 * properly suspended or resumed according to the device transfer
2000 *------------------------------------------------------------------------*/
2003 usb_bus_power_update(struct usb_bus *bus)
2005 usb_needs_explore(bus, 0 /* no probe */ );
2009 /*------------------------------------------------------------------------*
2010 * usbd_transfer_power_ref
2012 * This function will modify the power save reference counts and
2013 * wakeup the USB device associated with the given USB transfer, if
2015 *------------------------------------------------------------------------*/
2018 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2020 static const usb_power_mask_t power_mask[4] = {
2021 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2022 [UE_BULK] = USB_HW_POWER_BULK,
2023 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2024 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2026 struct usb_device *udev;
2027 uint8_t needs_explore;
2028 uint8_t needs_hw_power;
2031 udev = xfer->xroot->udev;
2033 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2034 /* no power save for root HUB */
2037 USB_BUS_LOCK(udev->bus);
2039 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2041 udev->pwr_save.last_xfer_time = ticks;
2042 udev->pwr_save.type_refs[xfer_type] += val;
2044 if (xfer->flags_int.control_xfr) {
2045 udev->pwr_save.read_refs += val;
2046 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2048 * It is not allowed to suspend during a
2051 udev->pwr_save.write_refs += val;
2053 } else if (USB_GET_DATA_ISREAD(xfer)) {
2054 udev->pwr_save.read_refs += val;
2056 udev->pwr_save.write_refs += val;
2060 if (udev->flags.self_suspended)
2061 needs_explore = usb_peer_should_wakeup(udev);
2065 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2066 DPRINTF("Adding type %u to power state\n", xfer_type);
2067 udev->bus->hw_power_state |= power_mask[xfer_type];
2077 USB_BUS_UNLOCK(udev->bus);
2079 if (needs_explore) {
2080 DPRINTF("update\n");
2081 usb_bus_power_update(udev->bus);
2082 } else if (needs_hw_power) {
2083 DPRINTF("needs power\n");
2084 if (udev->bus->methods->set_hw_power != NULL) {
2085 (udev->bus->methods->set_hw_power) (udev->bus);
2091 /*------------------------------------------------------------------------*
2092 * usb_peer_should_wakeup
2094 * This function returns non-zero if the current device should wake up.
2095 *------------------------------------------------------------------------*/
2097 usb_peer_should_wakeup(struct usb_device *udev)
2099 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2100 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2101 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2102 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2103 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2104 (udev->pwr_save.write_refs != 0) ||
2105 ((udev->pwr_save.read_refs != 0) &&
2106 (udev->flags.usb_mode == USB_MODE_HOST) &&
2107 (usb_peer_can_wakeup(udev) == 0)));
2110 /*------------------------------------------------------------------------*
2113 * This function implements the USB power daemon and is called
2114 * regularly from the USB explore thread.
2115 *------------------------------------------------------------------------*/
2118 usb_bus_powerd(struct usb_bus *bus)
2120 struct usb_device *udev;
2123 usb_ticks_t mintime;
2124 usb_size_t type_refs[5];
2127 limit = usb_power_timeout;
2130 else if (limit > 255)
2135 DPRINTF("bus=%p\n", bus);
2140 * The root HUB device is never suspended
2141 * and we simply skip it.
2143 for (x = USB_ROOT_HUB_ADDR + 1;
2144 x != bus->devices_max; x++) {
2146 udev = bus->devices[x];
2150 temp = ticks - udev->pwr_save.last_xfer_time;
2152 if (usb_peer_should_wakeup(udev)) {
2153 /* check if we are suspended */
2154 if (udev->flags.self_suspended != 0) {
2155 USB_BUS_UNLOCK(bus);
2156 usb_dev_resume_peer(udev);
2159 } else if ((temp >= limit) &&
2160 (udev->flags.usb_mode == USB_MODE_HOST) &&
2161 (udev->flags.self_suspended == 0)) {
2162 /* try to do suspend */
2164 USB_BUS_UNLOCK(bus);
2165 usb_dev_suspend_peer(udev);
2170 /* reset counters */
2172 mintime = (usb_ticks_t)-1;
2179 /* Re-loop all the devices to get the actual state */
2181 for (x = USB_ROOT_HUB_ADDR + 1;
2182 x != bus->devices_max; x++) {
2184 udev = bus->devices[x];
2188 /* we found a non-Root-Hub USB device */
2191 /* "last_xfer_time" can be updated by a resume */
2192 temp = ticks - udev->pwr_save.last_xfer_time;
2195 * Compute minimum time since last transfer for the complete
2201 if (udev->flags.self_suspended == 0) {
2202 type_refs[0] += udev->pwr_save.type_refs[0];
2203 type_refs[1] += udev->pwr_save.type_refs[1];
2204 type_refs[2] += udev->pwr_save.type_refs[2];
2205 type_refs[3] += udev->pwr_save.type_refs[3];
2209 if (mintime >= (usb_ticks_t)(1 * hz)) {
2210 /* recompute power masks */
2211 DPRINTF("Recomputing power masks\n");
2212 bus->hw_power_state = 0;
2213 if (type_refs[UE_CONTROL] != 0)
2214 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2215 if (type_refs[UE_BULK] != 0)
2216 bus->hw_power_state |= USB_HW_POWER_BULK;
2217 if (type_refs[UE_INTERRUPT] != 0)
2218 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2219 if (type_refs[UE_ISOCHRONOUS] != 0)
2220 bus->hw_power_state |= USB_HW_POWER_ISOC;
2221 if (type_refs[4] != 0)
2222 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2224 USB_BUS_UNLOCK(bus);
2226 if (bus->methods->set_hw_power != NULL) {
2227 /* always update hardware power! */
2228 (bus->methods->set_hw_power) (bus);
2234 /*------------------------------------------------------------------------*
2235 * usb_dev_resume_peer
2237 * This function will resume an USB peer and do the required USB
2238 * signalling to get an USB device out of the suspended state.
2239 *------------------------------------------------------------------------*/
2241 usb_dev_resume_peer(struct usb_device *udev)
2243 struct usb_bus *bus;
2250 /* check if already resumed */
2251 if (udev->flags.self_suspended == 0)
2254 /* we need a parent HUB to do resume */
2255 if (udev->parent_hub == NULL)
2258 DPRINTF("udev=%p\n", udev);
2260 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2261 (udev->flags.remote_wakeup == 0)) {
2263 * If the host did not set the remote wakeup feature, we can
2264 * not wake it up either!
2266 DPRINTF("remote wakeup is not set!\n");
2269 /* get bus pointer */
2272 /* resume parent hub first */
2273 usb_dev_resume_peer(udev->parent_hub);
2275 /* reduce chance of instant resume failure by waiting a little bit */
2276 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2278 if (usb_device_20_compatible(udev)) {
2279 /* resume current port (Valid in Host and Device Mode) */
2280 err = usbd_req_clear_port_feature(udev->parent_hub,
2281 NULL, udev->port_no, UHF_PORT_SUSPEND);
2283 DPRINTFN(0, "Resuming port failed\n");
2287 /* resume current port (Valid in Host and Device Mode) */
2288 err = usbd_req_set_port_link_state(udev->parent_hub,
2289 NULL, udev->port_no, UPS_PORT_LS_U0);
2291 DPRINTFN(0, "Resuming port failed\n");
2296 /* resume settle time */
2297 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2299 if (bus->methods->device_resume != NULL) {
2300 /* resume USB device on the USB controller */
2301 (bus->methods->device_resume) (udev);
2304 /* set that this device is now resumed */
2305 udev->flags.self_suspended = 0;
2307 /* make sure that we don't go into suspend right away */
2308 udev->pwr_save.last_xfer_time = ticks;
2310 /* make sure the needed power masks are on */
2311 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2312 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2313 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2314 bus->hw_power_state |= USB_HW_POWER_BULK;
2315 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2316 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2317 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2318 bus->hw_power_state |= USB_HW_POWER_ISOC;
2320 USB_BUS_UNLOCK(bus);
2322 if (bus->methods->set_hw_power != NULL) {
2323 /* always update hardware power! */
2324 (bus->methods->set_hw_power) (bus);
2329 /* notify all sub-devices about resume */
2330 err = usb_suspend_resume(udev, 0);
2332 usbd_sr_unlock(udev);
2334 /* check if peer has wakeup capability */
2335 if (usb_peer_can_wakeup(udev)) {
2336 /* clear remote wakeup */
2337 err = usbd_req_clear_device_feature(udev,
2338 NULL, UF_DEVICE_REMOTE_WAKEUP);
2340 DPRINTFN(0, "Clearing device "
2341 "remote wakeup failed: %s\n",
2347 /*------------------------------------------------------------------------*
2348 * usb_dev_suspend_peer
2350 * This function will suspend an USB peer and do the required USB
2351 * signalling to get an USB device into the suspended state.
2352 *------------------------------------------------------------------------*/
2354 usb_dev_suspend_peer(struct usb_device *udev)
2356 struct usb_device *child;
2366 /* check if already suspended */
2367 if (udev->flags.self_suspended)
2370 /* we need a parent HUB to do suspend */
2371 if (udev->parent_hub == NULL)
2374 DPRINTF("udev=%p\n", udev);
2376 /* check if the current device is a HUB */
2377 if (udev->hub != NULL) {
2378 nports = udev->hub->nports;
2380 /* check if all devices on the HUB are suspended */
2381 for (x = 0; x != nports; x++) {
2382 child = usb_bus_port_get_device(udev->bus,
2383 udev->hub->ports + x);
2388 if (child->flags.self_suspended)
2391 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2396 if (usb_peer_can_wakeup(udev)) {
2398 * This request needs to be done before we set
2399 * "udev->flags.self_suspended":
2402 /* allow device to do remote wakeup */
2403 err = usbd_req_set_device_feature(udev,
2404 NULL, UF_DEVICE_REMOTE_WAKEUP);
2406 DPRINTFN(0, "Setting device "
2407 "remote wakeup failed\n");
2411 USB_BUS_LOCK(udev->bus);
2413 * Checking for suspend condition and setting suspended bit
2416 err = usb_peer_should_wakeup(udev);
2419 * Set that this device is suspended. This variable
2420 * must be set before calling USB controller suspend
2423 udev->flags.self_suspended = 1;
2425 USB_BUS_UNLOCK(udev->bus);
2428 if (usb_peer_can_wakeup(udev)) {
2429 /* allow device to do remote wakeup */
2430 err = usbd_req_clear_device_feature(udev,
2431 NULL, UF_DEVICE_REMOTE_WAKEUP);
2433 DPRINTFN(0, "Setting device "
2434 "remote wakeup failed\n");
2438 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2439 /* resume parent HUB first */
2440 usb_dev_resume_peer(udev->parent_hub);
2442 /* reduce chance of instant resume failure by waiting a little bit */
2443 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2445 /* resume current port (Valid in Host and Device Mode) */
2446 err = usbd_req_clear_port_feature(udev->parent_hub,
2447 NULL, udev->port_no, UHF_PORT_SUSPEND);
2449 /* resume settle time */
2450 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2452 DPRINTF("Suspend was cancelled!\n");
2458 /* notify all sub-devices about suspend */
2459 err = usb_suspend_resume(udev, 1);
2461 usbd_sr_unlock(udev);
2463 if (udev->bus->methods->device_suspend != NULL) {
2466 /* suspend device on the USB controller */
2467 (udev->bus->methods->device_suspend) (udev);
2470 temp = usbd_get_dma_delay(udev);
2472 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2476 if (usb_device_20_compatible(udev)) {
2477 /* suspend current port */
2478 err = usbd_req_set_port_feature(udev->parent_hub,
2479 NULL, udev->port_no, UHF_PORT_SUSPEND);
2481 DPRINTFN(0, "Suspending port failed\n");
2485 /* suspend current port */
2486 err = usbd_req_set_port_link_state(udev->parent_hub,
2487 NULL, udev->port_no, UPS_PORT_LS_U3);
2489 DPRINTFN(0, "Suspending port failed\n");
2494 udev = udev->parent_hub;
2498 /*------------------------------------------------------------------------*
2499 * usbd_set_power_mode
2501 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2503 *------------------------------------------------------------------------*/
2505 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2507 /* filter input argument */
2508 if ((power_mode != USB_POWER_MODE_ON) &&
2509 (power_mode != USB_POWER_MODE_OFF))
2510 power_mode = USB_POWER_MODE_SAVE;
2512 power_mode = usbd_filter_power_mode(udev, power_mode);
2514 udev->power_mode = power_mode; /* update copy of power mode */
2517 usb_bus_power_update(udev->bus);
2519 usb_needs_explore(udev->bus, 0 /* no probe */ );
2523 /*------------------------------------------------------------------------*
2524 * usbd_filter_power_mode
2526 * This function filters the power mode based on hardware requirements.
2527 *------------------------------------------------------------------------*/
2529 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2531 struct usb_bus_methods *mtod;
2534 mtod = udev->bus->methods;
2537 if (mtod->get_power_mode != NULL)
2538 (mtod->get_power_mode) (udev, &temp);
2540 /* check if we should not filter */
2542 return (power_mode);
2544 /* use fixed power mode given by hardware driver */
2548 /*------------------------------------------------------------------------*
2549 * usbd_start_re_enumerate
2551 * This function starts re-enumeration of the given USB device. This
2552 * function does not need to be called BUS-locked. This function does
2553 * not wait until the re-enumeration is completed.
2554 *------------------------------------------------------------------------*/
2556 usbd_start_re_enumerate(struct usb_device *udev)
2558 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2559 udev->re_enumerate_wait = USB_RE_ENUM_START;
2560 usb_needs_explore(udev->bus, 0);