3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #include <sys/stdint.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usb_ioctl.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usbdi_util.h>
55 #define USB_DEBUG_VAR uhub_debug
57 #include <bus/u4b/usb_core.h>
58 #include <bus/u4b/usb_process.h>
59 #include <bus/u4b/usb_device.h>
60 #include <bus/u4b/usb_request.h>
61 #include <bus/u4b/usb_debug.h>
62 #include <bus/u4b/usb_hub.h>
63 #include <bus/u4b/usb_util.h>
64 #include <bus/u4b/usb_busdma.h>
65 #include <bus/u4b/usb_transfer.h>
66 #include <bus/u4b/usb_dynamic.h>
68 #include <bus/u4b/usb_controller.h>
69 #include <bus/u4b/usb_bus.h>
71 #define UHUB_INTR_INTERVAL 250 /* ms */
72 #define UHUB_N_TRANSFER 1
75 static int uhub_debug = 0;
77 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
78 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
81 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
85 static int usb_power_timeout = 30; /* seconds */
87 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
88 &usb_power_timeout, 0, "USB power timeout");
91 struct uhub_current_state {
97 struct uhub_current_state sc_st;/* current state */
98 device_t sc_dev; /* base device */
99 struct lock sc_lock; /* 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
106 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
107 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
108 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
109 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
110 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
112 /* prototypes for type checking: */
114 static device_probe_t uhub_probe;
115 static device_attach_t uhub_attach;
116 static device_detach_t uhub_detach;
117 static device_suspend_t uhub_suspend;
118 static device_resume_t uhub_resume;
120 static bus_driver_added_t uhub_driver_added;
121 static bus_child_location_str_t uhub_child_location_string;
122 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
124 static usb_callback_t uhub_intr_callback;
126 static void usb_dev_resume_peer(struct usb_device *udev);
127 static void usb_dev_suspend_peer(struct usb_device *udev);
128 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
130 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
133 .type = UE_INTERRUPT,
134 .endpoint = UE_ADDR_ANY,
135 .direction = UE_DIR_ANY,
137 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
138 .bufsize = 0, /* use wMaxPacketSize */
139 .callback = &uhub_intr_callback,
140 .interval = UHUB_INTR_INTERVAL,
145 * driver instance for "hub" connected to "usb"
146 * and "hub" connected to "hub"
148 static devclass_t uhub_devclass;
150 static device_method_t uhub_methods[] = {
151 DEVMETHOD(device_probe, uhub_probe),
152 DEVMETHOD(device_attach, uhub_attach),
153 DEVMETHOD(device_detach, uhub_detach),
155 DEVMETHOD(device_suspend, uhub_suspend),
156 DEVMETHOD(device_resume, uhub_resume),
158 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
159 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
160 DEVMETHOD(bus_driver_added, uhub_driver_added),
164 static driver_t uhub_driver = {
166 .methods = uhub_methods,
167 .size = sizeof(struct uhub_softc)
170 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
171 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
172 MODULE_VERSION(uhub, 1);
175 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
177 struct uhub_softc *sc = usbd_xfer_softc(xfer);
179 switch (USB_GET_STATE(xfer)) {
180 case USB_ST_TRANSFERRED:
183 * This is an indication that some port
184 * has changed status. Notify the bus
185 * event handler thread that we need
186 * to be explored again:
188 usb_needs_explore(sc->sc_udev->bus, 0);
191 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
192 usbd_transfer_submit(xfer);
196 if (xfer->error != USB_ERR_CANCELLED) {
198 * Do a clear-stall. The "stall_pipe" flag
199 * will get cleared before next callback by
202 usbd_xfer_set_stall(xfer);
203 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
204 usbd_transfer_submit(xfer);
210 /*------------------------------------------------------------------------*
211 * uhub_explore_sub - subroutine
215 * Else: A control transaction failed
216 *------------------------------------------------------------------------*/
218 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
221 struct usb_device *child;
225 bus = sc->sc_udev->bus;
228 /* get driver added refcount from USB bus */
229 refcount = bus->driver_added_refcount;
231 /* get device assosiated with the given port */
232 child = usb_bus_port_get_device(bus, up);
238 /* check if device should be re-enumerated */
240 if (child->flags.usb_mode == USB_MODE_HOST) {
241 usbd_enum_lock(child);
242 if (child->re_enumerate_wait) {
243 err = usbd_set_config_index(child,
246 DPRINTF("Unconfigure failed: "
250 err = usbd_req_re_enumerate(child, NULL);
252 err = usbd_set_config_index(child, 0);
254 err = usb_probe_and_attach(child,
255 USB_IFACE_INDEX_ANY);
257 child->re_enumerate_wait = 0;
260 usbd_enum_unlock(child);
263 /* check if probe and attach should be done */
265 if (child->driver_added_refcount != refcount) {
266 child->driver_added_refcount = refcount;
267 err = usb_probe_and_attach(child,
268 USB_IFACE_INDEX_ANY);
273 /* start control transfer, if device mode */
275 if (child->flags.usb_mode == USB_MODE_DEVICE)
276 usbd_ctrl_transfer_setup(child);
278 /* if a HUB becomes present, do a recursive HUB explore */
281 err = (child->hub->explore) (child);
287 /*------------------------------------------------------------------------*
288 * uhub_read_port_status - factored out code
289 *------------------------------------------------------------------------*/
291 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
293 struct usb_port_status ps;
296 err = usbd_req_get_port_status(
297 sc->sc_udev, NULL, &ps, portno);
299 /* update status regardless of error */
301 sc->sc_st.port_status = UGETW(ps.wPortStatus);
302 sc->sc_st.port_change = UGETW(ps.wPortChange);
304 /* debugging print */
306 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
307 "wPortChange=0x%04x, err=%s\n",
308 portno, sc->sc_st.port_status,
309 sc->sc_st.port_change, usbd_errstr(err));
313 /*------------------------------------------------------------------------*
318 * Else: A control transaction failed
319 *------------------------------------------------------------------------*/
321 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
323 struct usb_device *child;
324 struct usb_device *udev;
325 enum usb_dev_speed speed;
326 enum usb_hc_mode mode;
331 DPRINTF("reattaching port %d\n", portno);
336 child = usb_bus_port_get_device(udev->bus,
337 udev->hub->ports + portno - 1);
341 /* first clear the port connection change bit */
343 err = usbd_req_clear_port_feature(udev, NULL,
344 portno, UHF_C_PORT_CONNECTION);
349 /* check if there is a child */
353 * Free USB device and all subdevices, if any.
355 usb_free_device(child, 0);
358 /* get fresh status */
360 err = uhub_read_port_status(sc, portno);
364 /* check if nothing is connected to the port */
366 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
369 /* check if there is no power on the port and print a warning */
371 switch (udev->speed) {
375 power_mask = UPS_PORT_POWER;
377 case USB_SPEED_SUPER:
378 if (udev->parent_hub == NULL)
379 power_mask = UPS_PORT_POWER;
381 power_mask = UPS_PORT_POWER_SS;
387 if (!(sc->sc_st.port_status & power_mask)) {
388 DPRINTF("WARNING: strange, connected port %d "
389 "has no power\n", portno);
392 /* check if the device is in Host Mode */
394 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
396 DPRINTF("Port %d is in Host Mode\n", portno);
398 if (sc->sc_st.port_status & UPS_SUSPEND) {
400 * NOTE: Should not get here in SuperSpeed
401 * mode, because the HUB should report this
404 DPRINTF("Port %d was still "
405 "suspended, clearing.\n", portno);
406 err = usbd_req_clear_port_feature(udev,
407 NULL, portno, UHF_PORT_SUSPEND);
412 /* wait for maximum device power up time */
415 USB_MS_TO_TICKS(usb_port_powerup_delay));
417 /* reset port, which implies enabling it */
419 err = usbd_req_reset_port(udev, NULL, portno);
422 DPRINTFN(0, "port %d reset "
423 "failed, error=%s\n",
424 portno, usbd_errstr(err));
427 /* get port status again, it might have changed during reset */
429 err = uhub_read_port_status(sc, portno);
433 /* check if something changed during port reset */
435 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
436 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
438 DPRINTFN(0, "giving up port reset "
439 "- device vanished\n");
446 DPRINTF("Port %d is in Device Mode\n", portno);
450 * Figure out the device speed
452 switch (udev->speed) {
454 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
455 speed = USB_SPEED_HIGH;
456 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
457 speed = USB_SPEED_LOW;
459 speed = USB_SPEED_FULL;
462 if (sc->sc_st.port_status & UPS_LOW_SPEED)
463 speed = USB_SPEED_LOW;
465 speed = USB_SPEED_FULL;
468 speed = USB_SPEED_LOW;
470 case USB_SPEED_SUPER:
471 if (udev->parent_hub == NULL) {
473 /* Root HUB - special case */
474 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
476 speed = USB_SPEED_FULL;
479 speed = USB_SPEED_LOW;
482 speed = USB_SPEED_HIGH;
485 speed = USB_SPEED_SUPER;
489 speed = USB_SPEED_SUPER;
493 /* same speed like parent */
497 if (speed == USB_SPEED_SUPER) {
498 err = usbd_req_set_hub_u1_timeout(udev, NULL,
499 portno, 128 - (2 * udev->depth));
501 DPRINTFN(0, "port %d U1 timeout "
502 "failed, error=%s\n",
503 portno, usbd_errstr(err));
505 err = usbd_req_set_hub_u2_timeout(udev, NULL,
506 portno, 128 - (2 * udev->depth));
508 DPRINTFN(0, "port %d U2 timeout "
509 "failed, error=%s\n",
510 portno, usbd_errstr(err));
515 * Figure out the device mode
517 * NOTE: This part is currently FreeBSD specific.
519 if (udev->parent_hub != NULL) {
520 /* inherit mode from the parent HUB */
521 mode = udev->parent_hub->flags.usb_mode;
522 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
523 mode = USB_MODE_DEVICE;
525 mode = USB_MODE_HOST;
526 /* need to create a new child */
527 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
528 udev->depth + 1, portno - 1, portno, speed, mode);
530 DPRINTFN(0, "could not allocate new device\n");
533 return (0); /* success */
538 * Free USB device and all subdevices, if any.
540 usb_free_device(child, 0);
544 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
545 err = usbd_req_clear_port_feature(
547 portno, UHF_PORT_ENABLE);
551 DPRINTFN(0, "device problem (%s), "
552 "disabling port %d\n", usbd_errstr(err), portno);
557 /*------------------------------------------------------------------------*
558 * usb_device_20_compatible
561 * 0: HUB does not support suspend and resume
562 * Else: HUB supports suspend and resume
563 *------------------------------------------------------------------------*/
565 usb_device_20_compatible(struct usb_device *udev)
569 switch (udev->speed) {
579 /*------------------------------------------------------------------------*
580 * uhub_suspend_resume_port
584 * Else: A control transaction failed
585 *------------------------------------------------------------------------*/
587 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
589 struct usb_device *child;
590 struct usb_device *udev;
594 DPRINTF("port %d\n", portno);
597 child = usb_bus_port_get_device(udev->bus,
598 udev->hub->ports + portno - 1);
600 /* first clear the port suspend change bit */
602 if (usb_device_20_compatible(udev)) {
603 err = usbd_req_clear_port_feature(udev, NULL,
604 portno, UHF_C_PORT_SUSPEND);
606 err = usbd_req_clear_port_feature(udev, NULL,
607 portno, UHF_C_PORT_LINK_STATE);
611 DPRINTF("clearing suspend failed.\n");
614 /* get fresh status */
616 err = uhub_read_port_status(sc, portno);
618 DPRINTF("reading port status failed.\n");
621 /* convert current state */
623 if (usb_device_20_compatible(udev)) {
624 if (sc->sc_st.port_status & UPS_SUSPEND) {
630 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
634 case UPS_PORT_LS_SS_INA:
635 usbd_req_warm_reset_port(udev, NULL, portno);
644 DPRINTF("suspended=%u\n", is_suspend);
646 /* do the suspend or resume */
650 * This code handle two cases: 1) Host Mode - we can only
651 * receive resume here 2) Device Mode - we can receive
652 * suspend and resume here
655 usb_dev_resume_peer(child);
656 else if (child->flags.usb_mode == USB_MODE_DEVICE)
657 usb_dev_suspend_peer(child);
663 /*------------------------------------------------------------------------*
664 * uhub_root_interrupt
666 * This function is called when a Root HUB interrupt has
667 * happened. "ptr" and "len" makes up the Root HUB interrupt
668 * packet. This function is called having the "bus_mtx" locked.
669 *------------------------------------------------------------------------*/
671 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
673 USB_BUS_LOCK_ASSERT(bus);
675 usb_needs_explore(bus, 0);
679 uhub_is_too_deep(struct usb_device *udev)
681 switch (udev->speed) {
685 if (udev->depth > USB_HUB_MAX_DEPTH)
688 case USB_SPEED_SUPER:
689 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
698 /*------------------------------------------------------------------------*
704 *------------------------------------------------------------------------*/
706 uhub_explore(struct usb_device *udev)
709 struct uhub_softc *sc;
718 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
720 /* ignore devices that are too deep */
721 if (uhub_is_too_deep(udev))
722 return (USB_ERR_TOO_DEEP);
724 /* check if device is suspended */
725 if (udev->flags.self_suspended) {
726 /* need to wait until the child signals resume */
727 DPRINTF("Device is suspended!\n");
732 * Make sure we don't race against user-space applications
735 usbd_enum_lock(udev);
737 for (x = 0; x != hub->nports; x++) {
741 err = uhub_read_port_status(sc, portno);
743 /* most likely the HUB is gone */
746 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
747 DPRINTF("Overcurrent on port %u.\n", portno);
748 err = usbd_req_clear_port_feature(
749 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
751 /* most likely the HUB is gone */
755 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
757 * Fake a connect status change so that the
758 * status gets checked initially!
760 sc->sc_st.port_change |=
761 UPS_C_CONNECT_STATUS;
763 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
764 err = usbd_req_clear_port_feature(
765 udev, NULL, portno, UHF_C_PORT_ENABLE);
767 /* most likely the HUB is gone */
770 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
772 * Ignore the port error if the device
775 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
776 DPRINTFN(0, "illegal enable change, "
777 "port %d\n", portno);
780 if (up->restartcnt == USB_RESTART_MAX) {
781 /* XXX could try another speed ? */
782 DPRINTFN(0, "port error, giving up "
783 "port %d\n", portno);
785 sc->sc_st.port_change |=
786 UPS_C_CONNECT_STATUS;
791 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
792 err = uhub_reattach_port(sc, portno);
794 /* most likely the HUB is gone */
798 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
799 UPS_C_PORT_LINK_STATE)) {
800 err = uhub_suspend_resume_port(sc, portno);
802 /* most likely the HUB is gone */
806 err = uhub_explore_sub(sc, up);
808 /* no device(s) present */
811 /* explore succeeded - reset restart counter */
815 usbd_enum_unlock(udev);
817 /* initial status checked */
818 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
821 return (USB_ERR_NORMAL_COMPLETION);
825 uhub_probe(device_t dev)
827 struct usb_attach_arg *uaa = device_get_ivars(dev);
829 if (uaa->usb_mode != USB_MODE_HOST)
833 * The subclass for USB HUBs is currently ignored because it
834 * is 0 for some and 1 for others.
836 if (uaa->info.bConfigIndex == 0 &&
837 uaa->info.bDeviceClass == UDCLASS_HUB)
843 /* NOTE: The information returned by this function can be wrong. */
845 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
847 struct usb_hub_descriptor hubdesc20;
848 struct usb_hub_ss_descriptor hubdesc30;
853 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
854 return (USB_ERR_INVAL);
859 switch (udev->speed) {
863 /* assuming that there is one port */
864 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
866 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
867 "error=%s\n", usbd_errstr(err));
870 nports = hubdesc20.bNbrPorts;
874 if (udev->speed == USB_SPEED_HIGH)
875 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
878 case USB_SPEED_SUPER:
879 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
881 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
882 "error=%s\n", usbd_errstr(err));
885 nports = hubdesc30.bNbrPorts;
905 uhub_attach(device_t dev)
907 struct uhub_softc *sc = device_get_softc(dev);
908 struct usb_attach_arg *uaa = device_get_ivars(dev);
909 struct usb_device *udev = uaa->device;
910 struct usb_device *parent_hub = udev->parent_hub;
912 struct usb_hub_descriptor hubdesc20;
913 struct usb_hub_ss_descriptor hubdesc30;
925 lockinit(&sc->sc_lock, "USB HUB mutex", 0, 0);
927 device_set_usb_desc(dev);
929 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
930 "parent->selfpowered=%d\n",
932 udev->flags.self_powered,
935 parent_hub->flags.self_powered : 0);
937 if (uhub_is_too_deep(udev)) {
938 DPRINTFN(0, "HUB at depth %d, "
939 "exceeds maximum. HUB ignored\n", (int)udev->depth);
943 if (!udev->flags.self_powered && parent_hub &&
944 !parent_hub->flags.self_powered) {
945 DPRINTFN(0, "Bus powered HUB connected to "
946 "bus powered HUB. HUB ignored\n");
950 if (UHUB_IS_MULTI_TT(sc)) {
951 err = usbd_set_alt_interface_index(udev, 0, 1);
953 device_printf(dev, "MTT could not be enabled\n");
956 device_printf(dev, "MTT enabled\n");
959 /* get HUB descriptor */
961 DPRINTFN(2, "Getting HUB descriptor\n");
963 switch (udev->speed) {
967 /* assuming that there is one port */
968 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
970 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
971 "error=%s\n", usbd_errstr(err));
974 /* get number of ports */
975 nports = hubdesc20.bNbrPorts;
977 /* get power delay */
978 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
979 usb_extra_power_up_time);
981 /* get complete HUB descriptor */
983 /* check number of ports */
985 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
986 "error=%s\n", usbd_errstr(err));
989 /* get complete HUB descriptor */
990 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
993 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
994 "error=%s\n", usbd_errstr(err));
997 if (hubdesc20.bNbrPorts != nports) {
998 DPRINTFN(0, "Number of ports changed\n");
1003 case USB_SPEED_SUPER:
1004 if (udev->parent_hub != NULL) {
1005 err = usbd_req_set_hub_depth(udev, NULL,
1008 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1009 "error=%s\n", usbd_errstr(err));
1013 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1015 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1016 "error=%s\n", usbd_errstr(err));
1019 /* get number of ports */
1020 nports = hubdesc30.bNbrPorts;
1022 /* get power delay */
1023 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1024 usb_extra_power_up_time);
1026 /* get complete HUB descriptor */
1028 /* check number of ports */
1029 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1030 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1031 "error=%s\n", usbd_errstr(err));
1034 /* get complete HUB descriptor */
1035 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1038 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1039 "error=%s\n", usbd_errstr(err));
1042 if (hubdesc30.bNbrPorts != nports) {
1043 DPRINTFN(0, "Number of ports changed\n");
1049 DPRINTF("Assuming HUB has only one port\n");
1050 /* default number of ports */
1052 /* default power delay */
1053 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1057 DPRINTFN(0, "portless HUB\n");
1060 hub = kmalloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1061 M_USBDEV, M_WAITOK | M_ZERO);
1065 /* initialize HUB structure */
1067 hub->explore = &uhub_explore;
1068 hub->nports = nports;
1069 hub->hubudev = udev;
1071 /* if self powered hub, give ports maximum current */
1072 if (udev->flags.self_powered) {
1073 hub->portpower = USB_MAX_POWER;
1075 hub->portpower = USB_MIN_POWER;
1078 /* set up interrupt pipe */
1080 if (udev->parent_hub == NULL) {
1081 /* root HUB is special */
1085 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1086 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_lock);
1089 DPRINTFN(0, "cannot setup interrupt transfer, "
1090 "errstr=%s\n", usbd_errstr(err));
1093 /* wait with power off for a while */
1094 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1097 * To have the best chance of success we do things in the exact same
1098 * order as Windoze98. This should not be necessary, but some
1099 * devices do not follow the USB specs to the letter.
1101 * These are the events on the bus when a hub is attached:
1102 * Get device and config descriptors (see attach code)
1103 * Get hub descriptor (see above)
1106 * wait for power to become stable
1107 * (all below happens in explore code)
1109 * clear C_PORT_CONNECTION
1112 * if device connected
1116 * clear C_PORT_RESET
1118 * proceed with device attachment
1121 /* XXX should check for none, individual, or ganged power? */
1125 for (x = 0; x != nports; x++) {
1126 /* set up data structures */
1127 struct usb_port *up = hub->ports + x;
1129 up->device_index = 0;
1133 /* check if port is removable */
1134 switch (udev->speed) {
1136 case USB_SPEED_FULL:
1137 case USB_SPEED_HIGH:
1138 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1141 case USB_SPEED_SUPER:
1142 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1146 DPRINTF("Assuming removable port\n");
1151 /* turn the power on */
1152 err = usbd_req_set_port_feature(udev, NULL,
1153 portno, UHF_PORT_POWER);
1156 DPRINTFN(0, "port %d power on failed, %s\n",
1157 portno, usbd_errstr(err));
1159 DPRINTF("turn on port %d power\n",
1162 /* wait for stable power */
1163 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1166 device_printf(dev, "%d port%s with %d "
1167 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1168 removable, udev->flags.self_powered ? "self" : "bus");
1170 /* Start the interrupt endpoint, if any */
1172 if (sc->sc_xfer[0] != NULL) {
1173 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1174 usbd_transfer_start(sc->sc_xfer[0]);
1175 lockmgr(&sc->sc_lock, LK_RELEASE);
1178 /* Enable automatic power save on all USB HUBs */
1180 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1185 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1188 kfree(udev->hub, M_USBDEV);
1192 lockuninit(&sc->sc_lock);
1198 * Called from process context when the hub is gone.
1199 * Detach all devices on active ports.
1202 uhub_detach(device_t dev)
1204 struct uhub_softc *sc = device_get_softc(dev);
1205 struct usb_hub *hub = sc->sc_udev->hub;
1206 struct usb_device *child;
1209 if (hub == NULL) /* must be partially working */
1212 /* Make sure interrupt transfer is gone. */
1213 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1215 /* Detach all ports */
1216 for (x = 0; x != hub->nports; x++) {
1218 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
1220 if (child == NULL) {
1225 * Free USB device and all subdevices, if any.
1227 usb_free_device(child, 0);
1230 kfree(hub, M_USBDEV);
1231 sc->sc_udev->hub = NULL;
1233 lockuninit(&sc->sc_lock);
1239 uhub_suspend(device_t dev)
1242 /* Sub-devices are not suspended here! */
1247 uhub_resume(device_t dev)
1250 /* Sub-devices are not resumed here! */
1255 uhub_driver_added(device_t dev, driver_t *driver)
1257 usb_needs_explore_all();
1261 struct usb_device *udev;
1263 uint8_t iface_index;
1267 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1268 struct hub_result *res)
1270 struct usb_interface *iface;
1271 struct usb_device *udev;
1276 nports = hub->nports;
1277 for (x = 0; x != nports; x++) {
1278 udev = usb_bus_port_get_device(hub->hubudev->bus,
1283 for (i = 0; i != USB_IFACE_MAX; i++) {
1284 iface = usbd_get_iface(udev, i);
1286 (iface->subdev == child)) {
1287 res->iface_index = i;
1289 res->portno = x + 1;
1294 res->iface_index = 0;
1300 uhub_child_location_string(device_t parent, device_t child,
1301 char *buf, size_t buflen)
1303 struct uhub_softc *sc;
1304 struct usb_hub *hub;
1305 struct hub_result res;
1307 if (!device_is_attached(parent)) {
1313 sc = device_get_softc(parent);
1314 hub = sc->sc_udev->hub;
1319 uhub_find_iface_index(hub, child, &res);
1321 DPRINTF("device not on hub\n");
1327 ksnprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1328 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1329 res.portno, device_get_unit(res.udev->bus->bdev),
1330 res.udev->device_index, res.iface_index);
1340 uhub_child_pnpinfo_string(device_t parent, device_t child,
1341 char *buf, size_t buflen)
1343 struct uhub_softc *sc;
1344 struct usb_hub *hub;
1345 struct usb_interface *iface;
1346 struct hub_result res;
1348 if (!device_is_attached(parent)) {
1354 sc = device_get_softc(parent);
1355 hub = sc->sc_udev->hub;
1360 uhub_find_iface_index(hub, child, &res);
1362 DPRINTF("device not on hub\n");
1368 iface = usbd_get_iface(res.udev, res.iface_index);
1369 if (iface && iface->idesc) {
1370 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1371 "devclass=0x%02x devsubclass=0x%02x "
1375 "intclass=0x%02x intsubclass=0x%02x "
1376 "intprotocol=0x%02x " "%s%s",
1377 UGETW(res.udev->ddesc.idVendor),
1378 UGETW(res.udev->ddesc.idProduct),
1379 res.udev->ddesc.bDeviceClass,
1380 res.udev->ddesc.bDeviceSubClass,
1381 usb_get_serial(res.udev),
1382 UGETW(res.udev->ddesc.bcdDevice),
1383 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1384 iface->idesc->bInterfaceClass,
1385 iface->idesc->bInterfaceSubClass,
1386 iface->idesc->bInterfaceProtocol,
1387 iface->pnpinfo ? " " : "",
1388 iface->pnpinfo ? iface->pnpinfo : "");
1404 * The USB Transaction Translator:
1405 * ===============================
1407 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1408 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1409 * USB transfers. To utilize bandwidth dynamically the "scatter and
1410 * gather" principle must be applied. This means that bandwidth must
1411 * be divided into equal parts of bandwidth. With regard to USB all
1412 * data is transferred in smaller packets with length
1413 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1416 * The bandwidth scheduler which I have implemented will simply pack
1417 * the USB transfers back to back until there is no more space in the
1418 * schedule. Out of the 8 microframes which the USB 2.0 standard
1419 * provides, only 6 are available for non-HIGH-speed devices. I have
1420 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1421 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1422 * this division, it is very difficult to allocate and free bandwidth
1425 * NOTE about the Transaction Translator in USB HUBs:
1427 * USB HUBs have a very simple Transaction Translator, that will
1428 * simply pipeline all the SPLIT transactions. That means that the
1429 * transactions will be executed in the order they are queued!
1433 /*------------------------------------------------------------------------*
1434 * usb_intr_find_best_slot
1437 * The best Transaction Translation slot for an interrupt endpoint.
1438 *------------------------------------------------------------------------*/
1440 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1441 uint8_t end, uint8_t mask)
1443 usb_size_t min = (usb_size_t)-1;
1451 /* find the last slot with lesser used bandwidth */
1453 for (x = start; x < end; x++) {
1457 /* compute sum of bandwidth */
1458 for (z = x; z < end; z++) {
1459 if (mask & (1U << (z - x)))
1463 /* check if the current multi-slot is more optimal */
1469 /* check if the mask is about to be shifted out */
1470 if (mask & (1U << (end - 1 - x)))
1476 /*------------------------------------------------------------------------*
1477 * usb_hs_bandwidth_adjust
1479 * This function will update the bandwith usage for the microframe
1480 * having index "slot" by "len" bytes. "len" can be negative. If the
1481 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1482 * the "slot" argument will be replaced by the slot having least used
1483 * bandwidth. The "mask" argument is used for multi-slot allocations.
1486 * The slot in which the bandwidth update was done: 0..7
1487 *------------------------------------------------------------------------*/
1489 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1490 uint8_t slot, uint8_t mask)
1492 struct usb_bus *bus = udev->bus;
1493 struct usb_hub *hub;
1494 enum usb_dev_speed speed;
1497 USB_BUS_LOCK_ASSERT(bus);
1499 speed = usbd_get_speed(udev);
1503 case USB_SPEED_FULL:
1504 if (speed == USB_SPEED_LOW) {
1508 * The Host Controller Driver should have
1509 * performed checks so that the lookup
1510 * below does not result in a NULL pointer
1514 hub = udev->parent_hs_hub->hub;
1515 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1516 slot = usb_intr_find_best_slot(hub->uframe_usage,
1517 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1519 for (x = slot; x < 8; x++) {
1520 if (mask & (1U << (x - slot))) {
1521 hub->uframe_usage[x] += len;
1522 bus->uframe_usage[x] += len;
1527 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1528 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1529 USB_HS_MICRO_FRAMES_MAX, mask);
1531 for (x = slot; x < 8; x++) {
1532 if (mask & (1U << (x - slot))) {
1533 bus->uframe_usage[x] += len;
1541 /*------------------------------------------------------------------------*
1542 * usb_hs_bandwidth_alloc
1544 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1545 *------------------------------------------------------------------------*/
1547 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1549 struct usb_device *udev;
1554 udev = xfer->xroot->udev;
1556 if (udev->flags.usb_mode != USB_MODE_HOST)
1557 return; /* not supported */
1559 xfer->endpoint->refcount_bw++;
1560 if (xfer->endpoint->refcount_bw != 1)
1561 return; /* already allocated */
1563 speed = usbd_get_speed(udev);
1565 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1567 /* allocate a microframe slot */
1570 slot = usb_hs_bandwidth_adjust(udev,
1571 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1573 xfer->endpoint->usb_uframe = slot;
1574 xfer->endpoint->usb_smask = mask << slot;
1576 if ((speed != USB_SPEED_FULL) &&
1577 (speed != USB_SPEED_LOW)) {
1578 xfer->endpoint->usb_cmask = 0x00 ;
1580 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1584 case UE_ISOCHRONOUS:
1585 switch (usbd_xfer_get_fps_shift(xfer)) {
1600 /* allocate a microframe multi-slot */
1602 slot = usb_hs_bandwidth_adjust(udev,
1603 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1605 xfer->endpoint->usb_uframe = slot;
1606 xfer->endpoint->usb_cmask = 0;
1607 xfer->endpoint->usb_smask = mask << slot;
1611 xfer->endpoint->usb_uframe = 0;
1612 xfer->endpoint->usb_cmask = 0;
1613 xfer->endpoint->usb_smask = 0;
1617 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1618 xfer->endpoint->usb_uframe,
1619 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1622 /*------------------------------------------------------------------------*
1623 * usb_hs_bandwidth_free
1625 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1626 *------------------------------------------------------------------------*/
1628 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1630 struct usb_device *udev;
1634 udev = xfer->xroot->udev;
1636 if (udev->flags.usb_mode != USB_MODE_HOST)
1637 return; /* not supported */
1639 xfer->endpoint->refcount_bw--;
1640 if (xfer->endpoint->refcount_bw != 0)
1641 return; /* still allocated */
1643 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1645 case UE_ISOCHRONOUS:
1647 slot = xfer->endpoint->usb_uframe;
1648 mask = xfer->endpoint->usb_smask;
1650 /* free microframe slot(s): */
1651 usb_hs_bandwidth_adjust(udev,
1652 -xfer->max_frame_size, slot, mask >> slot);
1654 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1655 slot, mask >> slot);
1657 xfer->endpoint->usb_uframe = 0;
1658 xfer->endpoint->usb_cmask = 0;
1659 xfer->endpoint->usb_smask = 0;
1667 /*------------------------------------------------------------------------*
1668 * usb_isoc_time_expand
1670 * This function will expand the time counter from 7-bit to 16-bit.
1673 * 16-bit isochronous time counter.
1674 *------------------------------------------------------------------------*/
1676 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1680 USB_BUS_LOCK_ASSERT(bus);
1682 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1684 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1686 if (isoc_time_curr < rem) {
1687 /* the time counter wrapped around */
1688 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1690 /* update the remainder */
1692 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1693 bus->isoc_time_last |= isoc_time_curr;
1695 return (bus->isoc_time_last);
1698 /*------------------------------------------------------------------------*
1699 * usbd_fs_isoc_schedule_alloc_slot
1701 * This function will allocate bandwidth for an isochronous FULL speed
1702 * transaction in the FULL speed schedule.
1707 *------------------------------------------------------------------------*/
1708 #if USB_HAVE_TT_SUPPORT
1710 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1712 struct usb_xfer *xfer;
1713 struct usb_xfer *pipe_xfer;
1714 struct usb_bus *bus;
1716 usb_frlength_t data_len;
1724 bus = isoc_xfer->xroot->bus;
1726 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1728 /* skip self, if any */
1730 if (xfer == isoc_xfer)
1733 /* check if this USB transfer is going through the same TT */
1735 if (xfer->xroot->udev->parent_hs_hub !=
1736 isoc_xfer->xroot->udev->parent_hs_hub) {
1739 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1740 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1741 (xfer->xroot->udev->hs_port_no !=
1742 isoc_xfer->xroot->udev->hs_port_no)) {
1745 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
1748 /* check if isoc_time is part of this transfer */
1750 delta = xfer->isoc_time_complete - isoc_time;
1751 if (delta > 0 && delta <= xfer->nframes) {
1752 delta = xfer->nframes - delta;
1754 len = xfer->frlengths[delta];
1763 * Check double buffered transfers. Only stream ID
1764 * equal to zero is valid here!
1766 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
1769 /* skip self, if any */
1771 if (pipe_xfer == isoc_xfer)
1774 /* check if isoc_time is part of this transfer */
1776 delta = pipe_xfer->isoc_time_complete - isoc_time;
1777 if (delta > 0 && delta <= pipe_xfer->nframes) {
1778 delta = pipe_xfer->nframes - delta;
1780 len = pipe_xfer->frlengths[delta];
1790 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1791 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1795 /* check for overflow */
1797 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1802 delta = isoc_xfer->isoc_time_complete - isoc_time;
1803 if (delta > 0 && delta <= isoc_xfer->nframes) {
1804 delta = isoc_xfer->nframes - delta;
1806 len = isoc_xfer->frlengths[delta];
1814 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
1815 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
1819 /* check for overflow */
1821 if (slot >= USB_FS_ISOC_UFRAME_MAX)
1828 /*------------------------------------------------------------------------*
1829 * usb_bus_port_get_device
1831 * This function is NULL safe.
1832 *------------------------------------------------------------------------*/
1834 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1836 if ((bus == NULL) || (up == NULL)) {
1840 if (up->device_index == 0) {
1844 return (bus->devices[up->device_index]);
1847 /*------------------------------------------------------------------------*
1848 * usb_bus_port_set_device
1850 * This function is NULL safe.
1851 *------------------------------------------------------------------------*/
1853 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1854 struct usb_device *udev, uint8_t device_index)
1861 * There is only one case where we don't
1862 * have an USB port, and that is the Root Hub!
1866 up->device_index = device_index;
1868 device_index = up->device_index;
1869 up->device_index = 0;
1873 * Make relationships to our new device
1875 if (device_index != 0) {
1877 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1879 bus->devices[device_index] = udev;
1881 lockmgr(&usb_ref_lock, LK_RELEASE);
1887 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1890 /*------------------------------------------------------------------------*
1893 * This functions is called when the USB event thread needs to run.
1894 *------------------------------------------------------------------------*/
1896 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1903 DPRINTF("No bus pointer!\n");
1906 if ((bus->devices == NULL) ||
1907 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1908 DPRINTF("No root HUB\n");
1911 if (lockowned(&bus->bus_lock) != 0) {
1920 if (usb_proc_msignal(&bus->explore_proc,
1921 &bus->explore_msg[0], &bus->explore_msg[1])) {
1925 USB_BUS_UNLOCK(bus);
1929 /*------------------------------------------------------------------------*
1930 * usb_needs_explore_all
1932 * This function is called whenever a new driver is loaded and will
1933 * cause that all USB busses are re-explored.
1934 *------------------------------------------------------------------------*/
1936 usb_needs_explore_all(void)
1938 struct usb_bus *bus;
1945 dc = usb_devclass_ptr;
1947 DPRINTFN(0, "no devclass\n");
1951 * Explore all USB busses in parallell.
1953 max = devclass_get_maxunit(dc);
1955 dev = devclass_get_device(dc, max);
1957 bus = device_get_softc(dev);
1959 usb_needs_explore(bus, 1);
1966 /*------------------------------------------------------------------------*
1967 * usb_bus_power_update
1969 * This function will ensure that all USB devices on the given bus are
1970 * properly suspended or resumed according to the device transfer
1972 *------------------------------------------------------------------------*/
1975 usb_bus_power_update(struct usb_bus *bus)
1977 usb_needs_explore(bus, 0 /* no probe */ );
1981 /*------------------------------------------------------------------------*
1982 * usbd_transfer_power_ref
1984 * This function will modify the power save reference counts and
1985 * wakeup the USB device associated with the given USB transfer, if
1987 *------------------------------------------------------------------------*/
1990 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1992 static const usb_power_mask_t power_mask[4] = {
1993 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1994 [UE_BULK] = USB_HW_POWER_BULK,
1995 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1996 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1998 struct usb_device *udev;
1999 uint8_t needs_explore;
2000 uint8_t needs_hw_power;
2003 udev = xfer->xroot->udev;
2005 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2006 /* no power save for root HUB */
2009 USB_BUS_LOCK(udev->bus);
2011 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2013 udev->pwr_save.last_xfer_time = ticks;
2014 udev->pwr_save.type_refs[xfer_type] += val;
2016 if (xfer->flags_int.control_xfr) {
2017 udev->pwr_save.read_refs += val;
2018 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2020 * It is not allowed to suspend during a
2023 udev->pwr_save.write_refs += val;
2025 } else if (USB_GET_DATA_ISREAD(xfer)) {
2026 udev->pwr_save.read_refs += val;
2028 udev->pwr_save.write_refs += val;
2032 if (udev->flags.self_suspended)
2033 needs_explore = usb_peer_should_wakeup(udev);
2037 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2038 DPRINTF("Adding type %u to power state\n", xfer_type);
2039 udev->bus->hw_power_state |= power_mask[xfer_type];
2049 USB_BUS_UNLOCK(udev->bus);
2051 if (needs_explore) {
2052 DPRINTF("update\n");
2053 usb_bus_power_update(udev->bus);
2054 } else if (needs_hw_power) {
2055 DPRINTF("needs power\n");
2056 if (udev->bus->methods->set_hw_power != NULL) {
2057 (udev->bus->methods->set_hw_power) (udev->bus);
2063 /*------------------------------------------------------------------------*
2064 * usb_peer_should_wakeup
2066 * This function returns non-zero if the current device should wake up.
2067 *------------------------------------------------------------------------*/
2069 usb_peer_should_wakeup(struct usb_device *udev)
2071 return ((udev->power_mode == USB_POWER_MODE_ON) ||
2072 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2073 (udev->re_enumerate_wait != 0) ||
2074 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2075 (udev->pwr_save.write_refs != 0) ||
2076 ((udev->pwr_save.read_refs != 0) &&
2077 (udev->flags.usb_mode == USB_MODE_HOST) &&
2078 (usb_peer_can_wakeup(udev) == 0)));
2081 /*------------------------------------------------------------------------*
2084 * This function implements the USB power daemon and is called
2085 * regularly from the USB explore thread.
2086 *------------------------------------------------------------------------*/
2089 usb_bus_powerd(struct usb_bus *bus)
2091 struct usb_device *udev;
2094 usb_ticks_t mintime;
2095 usb_size_t type_refs[5];
2098 limit = usb_power_timeout;
2101 else if (limit > 255)
2106 DPRINTF("bus=%p\n", bus);
2111 * The root HUB device is never suspended
2112 * and we simply skip it.
2114 for (x = USB_ROOT_HUB_ADDR + 1;
2115 x != bus->devices_max; x++) {
2117 udev = bus->devices[x];
2121 temp = ticks - udev->pwr_save.last_xfer_time;
2123 if (usb_peer_should_wakeup(udev)) {
2124 /* check if we are suspended */
2125 if (udev->flags.self_suspended != 0) {
2126 USB_BUS_UNLOCK(bus);
2127 usb_dev_resume_peer(udev);
2130 } else if ((temp >= limit) &&
2131 (udev->flags.usb_mode == USB_MODE_HOST) &&
2132 (udev->flags.self_suspended == 0)) {
2133 /* try to do suspend */
2135 USB_BUS_UNLOCK(bus);
2136 usb_dev_suspend_peer(udev);
2141 /* reset counters */
2143 mintime = (usb_ticks_t)-1;
2150 /* Re-loop all the devices to get the actual state */
2152 for (x = USB_ROOT_HUB_ADDR + 1;
2153 x != bus->devices_max; x++) {
2155 udev = bus->devices[x];
2159 /* we found a non-Root-Hub USB device */
2162 /* "last_xfer_time" can be updated by a resume */
2163 temp = ticks - udev->pwr_save.last_xfer_time;
2166 * Compute minimum time since last transfer for the complete
2172 if (udev->flags.self_suspended == 0) {
2173 type_refs[0] += udev->pwr_save.type_refs[0];
2174 type_refs[1] += udev->pwr_save.type_refs[1];
2175 type_refs[2] += udev->pwr_save.type_refs[2];
2176 type_refs[3] += udev->pwr_save.type_refs[3];
2180 if (mintime >= (usb_ticks_t)(1 * hz)) {
2181 /* recompute power masks */
2182 DPRINTF("Recomputing power masks\n");
2183 bus->hw_power_state = 0;
2184 if (type_refs[UE_CONTROL] != 0)
2185 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2186 if (type_refs[UE_BULK] != 0)
2187 bus->hw_power_state |= USB_HW_POWER_BULK;
2188 if (type_refs[UE_INTERRUPT] != 0)
2189 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2190 if (type_refs[UE_ISOCHRONOUS] != 0)
2191 bus->hw_power_state |= USB_HW_POWER_ISOC;
2192 if (type_refs[4] != 0)
2193 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2195 USB_BUS_UNLOCK(bus);
2197 if (bus->methods->set_hw_power != NULL) {
2198 /* always update hardware power! */
2199 (bus->methods->set_hw_power) (bus);
2205 /*------------------------------------------------------------------------*
2206 * usb_dev_resume_peer
2208 * This function will resume an USB peer and do the required USB
2209 * signalling to get an USB device out of the suspended state.
2210 *------------------------------------------------------------------------*/
2212 usb_dev_resume_peer(struct usb_device *udev)
2214 struct usb_bus *bus;
2221 /* check if already resumed */
2222 if (udev->flags.self_suspended == 0)
2225 /* we need a parent HUB to do resume */
2226 if (udev->parent_hub == NULL)
2229 DPRINTF("udev=%p\n", udev);
2231 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2232 (udev->flags.remote_wakeup == 0)) {
2234 * If the host did not set the remote wakeup feature, we can
2235 * not wake it up either!
2237 DPRINTF("remote wakeup is not set!\n");
2240 /* get bus pointer */
2243 /* resume parent hub first */
2244 usb_dev_resume_peer(udev->parent_hub);
2246 /* reduce chance of instant resume failure by waiting a little bit */
2247 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2249 if (usb_device_20_compatible(udev)) {
2250 /* resume current port (Valid in Host and Device Mode) */
2251 err = usbd_req_clear_port_feature(udev->parent_hub,
2252 NULL, udev->port_no, UHF_PORT_SUSPEND);
2254 DPRINTFN(0, "Resuming port failed\n");
2258 /* resume current port (Valid in Host and Device Mode) */
2259 err = usbd_req_set_port_link_state(udev->parent_hub,
2260 NULL, udev->port_no, UPS_PORT_LS_U0);
2262 DPRINTFN(0, "Resuming port failed\n");
2267 /* resume settle time */
2268 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2270 if (bus->methods->device_resume != NULL) {
2271 /* resume USB device on the USB controller */
2272 (bus->methods->device_resume) (udev);
2275 /* set that this device is now resumed */
2276 udev->flags.self_suspended = 0;
2278 /* make sure that we don't go into suspend right away */
2279 udev->pwr_save.last_xfer_time = ticks;
2281 /* make sure the needed power masks are on */
2282 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2283 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2284 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2285 bus->hw_power_state |= USB_HW_POWER_BULK;
2286 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2287 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2288 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2289 bus->hw_power_state |= USB_HW_POWER_ISOC;
2291 USB_BUS_UNLOCK(bus);
2293 if (bus->methods->set_hw_power != NULL) {
2294 /* always update hardware power! */
2295 (bus->methods->set_hw_power) (bus);
2300 /* notify all sub-devices about resume */
2301 err = usb_suspend_resume(udev, 0);
2303 usbd_sr_unlock(udev);
2305 /* check if peer has wakeup capability */
2306 if (usb_peer_can_wakeup(udev)) {
2307 /* clear remote wakeup */
2308 err = usbd_req_clear_device_feature(udev,
2309 NULL, UF_DEVICE_REMOTE_WAKEUP);
2311 DPRINTFN(0, "Clearing device "
2312 "remote wakeup failed: %s\n",
2318 /*------------------------------------------------------------------------*
2319 * usb_dev_suspend_peer
2321 * This function will suspend an USB peer and do the required USB
2322 * signalling to get an USB device into the suspended state.
2323 *------------------------------------------------------------------------*/
2325 usb_dev_suspend_peer(struct usb_device *udev)
2327 struct usb_device *child;
2337 /* check if already suspended */
2338 if (udev->flags.self_suspended)
2341 /* we need a parent HUB to do suspend */
2342 if (udev->parent_hub == NULL)
2345 DPRINTF("udev=%p\n", udev);
2347 /* check if the current device is a HUB */
2348 if (udev->hub != NULL) {
2349 nports = udev->hub->nports;
2351 /* check if all devices on the HUB are suspended */
2352 for (x = 0; x != nports; x++) {
2353 child = usb_bus_port_get_device(udev->bus,
2354 udev->hub->ports + x);
2359 if (child->flags.self_suspended)
2362 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2367 if (usb_peer_can_wakeup(udev)) {
2369 * This request needs to be done before we set
2370 * "udev->flags.self_suspended":
2373 /* allow device to do remote wakeup */
2374 err = usbd_req_set_device_feature(udev,
2375 NULL, UF_DEVICE_REMOTE_WAKEUP);
2377 DPRINTFN(0, "Setting device "
2378 "remote wakeup failed\n");
2382 USB_BUS_LOCK(udev->bus);
2384 * Checking for suspend condition and setting suspended bit
2387 err = usb_peer_should_wakeup(udev);
2390 * Set that this device is suspended. This variable
2391 * must be set before calling USB controller suspend
2394 udev->flags.self_suspended = 1;
2396 USB_BUS_UNLOCK(udev->bus);
2399 if (usb_peer_can_wakeup(udev)) {
2400 /* allow device to do remote wakeup */
2401 err = usbd_req_clear_device_feature(udev,
2402 NULL, UF_DEVICE_REMOTE_WAKEUP);
2404 DPRINTFN(0, "Setting device "
2405 "remote wakeup failed\n");
2409 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2410 /* resume parent HUB first */
2411 usb_dev_resume_peer(udev->parent_hub);
2413 /* reduce chance of instant resume failure by waiting a little bit */
2414 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2416 /* resume current port (Valid in Host and Device Mode) */
2417 err = usbd_req_clear_port_feature(udev->parent_hub,
2418 NULL, udev->port_no, UHF_PORT_SUSPEND);
2420 /* resume settle time */
2421 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2423 DPRINTF("Suspend was cancelled!\n");
2429 /* notify all sub-devices about suspend */
2430 err = usb_suspend_resume(udev, 1);
2432 usbd_sr_unlock(udev);
2434 if (udev->bus->methods->device_suspend != NULL) {
2437 /* suspend device on the USB controller */
2438 (udev->bus->methods->device_suspend) (udev);
2441 temp = usbd_get_dma_delay(udev);
2443 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2447 if (usb_device_20_compatible(udev)) {
2448 /* suspend current port */
2449 err = usbd_req_set_port_feature(udev->parent_hub,
2450 NULL, udev->port_no, UHF_PORT_SUSPEND);
2452 DPRINTFN(0, "Suspending port failed\n");
2456 /* suspend current port */
2457 err = usbd_req_set_port_link_state(udev->parent_hub,
2458 NULL, udev->port_no, UPS_PORT_LS_U3);
2460 DPRINTFN(0, "Suspending port failed\n");
2465 udev = udev->parent_hub;
2469 /*------------------------------------------------------------------------*
2470 * usbd_set_power_mode
2472 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2474 *------------------------------------------------------------------------*/
2476 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2478 /* filter input argument */
2479 if ((power_mode != USB_POWER_MODE_ON) &&
2480 (power_mode != USB_POWER_MODE_OFF))
2481 power_mode = USB_POWER_MODE_SAVE;
2483 power_mode = usbd_filter_power_mode(udev, power_mode);
2485 udev->power_mode = power_mode; /* update copy of power mode */
2488 usb_bus_power_update(udev->bus);
2492 /*------------------------------------------------------------------------*
2493 * usbd_filter_power_mode
2495 * This function filters the power mode based on hardware requirements.
2496 *------------------------------------------------------------------------*/
2498 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2500 struct usb_bus_methods *mtod;
2503 mtod = udev->bus->methods;
2506 if (mtod->get_power_mode != NULL)
2507 (mtod->get_power_mode) (udev, &temp);
2509 /* check if we should not filter */
2511 return (power_mode);
2513 /* use fixed power mode given by hardware driver */
2517 /*------------------------------------------------------------------------*
2518 * usbd_start_re_enumerate
2520 * This function starts re-enumeration of the given USB device. This
2521 * function does not need to be called BUS-locked. This function does
2522 * not wait until the re-enumeration is completed.
2523 *------------------------------------------------------------------------*/
2525 usbd_start_re_enumerate(struct usb_device *udev)
2527 if (udev->re_enumerate_wait == 0) {
2528 udev->re_enumerate_wait = 1;
2529 usb_needs_explore(udev->bus, 0);