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 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR uhub_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define UHUB_INTR_INTERVAL 250 /* ms */
79 #if USB_HAVE_TT_SUPPORT
80 UHUB_RESET_TT_TRANSFER,
86 static int uhub_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
91 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
95 static int usb_power_timeout = 30; /* seconds */
97 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
98 &usb_power_timeout, 0, "USB power timeout");
101 #if USB_HAVE_DISABLE_ENUM
102 static int usb_disable_enumeration = 0;
103 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
104 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
105 "This can secure against USB devices turning evil, "
106 "for example a USB memory stick becoming a USB keyboard.");
107 TUNABLE_INT("hw.usb.disable_enumeration", &usb_disable_enumeration);
109 static int usb_disable_port_power = 0;
110 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
111 &usb_disable_port_power, 0, "Set to disable all USB port power.");
112 TUNABLE_INT("hw.usb.disable_port_power", &usb_disable_port_power);
115 struct uhub_current_state {
116 uint16_t port_change;
117 uint16_t port_status;
121 struct uhub_current_state sc_st;/* current state */
122 #if (USB_HAVE_FIXED_PORT != 0)
123 struct usb_hub sc_hub;
125 device_t sc_dev; /* base device */
126 struct mtx sc_mtx; /* our mutex */
127 struct usb_device *sc_udev; /* USB device */
128 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
129 #if USB_HAVE_DISABLE_ENUM
130 int sc_disable_enumeration;
131 int sc_disable_port_power;
133 uint8_t sc_usb_port_errors; /* error counter */
134 #define UHUB_USB_PORT_ERRORS_MAX 4
136 #define UHUB_FLAG_DID_EXPLORE 0x01
139 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
140 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
141 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
142 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
143 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
145 /* prototypes for type checking: */
147 static device_probe_t uhub_probe;
148 static device_attach_t uhub_attach;
149 static device_detach_t uhub_detach;
150 static device_suspend_t uhub_suspend;
151 static device_resume_t uhub_resume;
153 static bus_driver_added_t uhub_driver_added;
154 static bus_child_location_str_t uhub_child_location_string;
155 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
157 static usb_callback_t uhub_intr_callback;
158 #if USB_HAVE_TT_SUPPORT
159 static usb_callback_t uhub_reset_tt_callback;
162 static void usb_dev_resume_peer(struct usb_device *udev);
163 static void usb_dev_suspend_peer(struct usb_device *udev);
164 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
166 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
168 [UHUB_INTR_TRANSFER] = {
169 .type = UE_INTERRUPT,
170 .endpoint = UE_ADDR_ANY,
171 .direction = UE_DIR_ANY,
173 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
174 .bufsize = 0, /* use wMaxPacketSize */
175 .callback = &uhub_intr_callback,
176 .interval = UHUB_INTR_INTERVAL,
178 #if USB_HAVE_TT_SUPPORT
179 [UHUB_RESET_TT_TRANSFER] = {
181 .endpoint = 0x00, /* Control pipe */
182 .direction = UE_DIR_ANY,
183 .bufsize = sizeof(struct usb_device_request),
184 .callback = &uhub_reset_tt_callback,
185 .timeout = 1000, /* 1 second */
186 .usb_mode = USB_MODE_HOST,
192 * driver instance for "hub" connected to "usb"
193 * and "hub" connected to "hub"
195 static devclass_t uhub_devclass;
197 static device_method_t uhub_methods[] = {
198 DEVMETHOD(device_probe, uhub_probe),
199 DEVMETHOD(device_attach, uhub_attach),
200 DEVMETHOD(device_detach, uhub_detach),
202 DEVMETHOD(device_suspend, uhub_suspend),
203 DEVMETHOD(device_resume, uhub_resume),
205 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
206 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
207 DEVMETHOD(bus_driver_added, uhub_driver_added),
211 static driver_t uhub_driver = {
213 .methods = uhub_methods,
214 .size = sizeof(struct uhub_softc)
217 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
218 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
219 MODULE_VERSION(uhub, 1);
222 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
224 struct uhub_softc *sc = usbd_xfer_softc(xfer);
226 switch (USB_GET_STATE(xfer)) {
227 case USB_ST_TRANSFERRED:
230 * This is an indication that some port
231 * has changed status. Notify the bus
232 * event handler thread that we need
233 * to be explored again:
235 usb_needs_explore(sc->sc_udev->bus, 0);
238 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
239 usbd_transfer_submit(xfer);
243 if (xfer->error != USB_ERR_CANCELLED) {
245 * Do a clear-stall. The "stall_pipe" flag
246 * will get cleared before next callback by
249 usbd_xfer_set_stall(xfer);
250 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
251 usbd_transfer_submit(xfer);
257 /*------------------------------------------------------------------------*
260 * This function starts the TT reset USB request
261 *------------------------------------------------------------------------*/
262 #if USB_HAVE_TT_SUPPORT
264 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
266 struct usb_udev_msg *pm = (void *)_pm;
267 struct usb_device *udev = pm->udev;
269 struct uhub_softc *sc;
279 USB_BUS_UNLOCK(udev->bus);
280 mtx_lock(&sc->sc_mtx);
282 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
284 mtx_unlock(&sc->sc_mtx);
285 USB_BUS_LOCK(udev->bus);
289 /*------------------------------------------------------------------------*
290 * uhub_tt_buffer_reset_async_locked
292 * This function queues a TT reset for the given USB device and endpoint.
293 *------------------------------------------------------------------------*/
294 #if USB_HAVE_TT_SUPPORT
296 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
298 struct usb_device_request req;
299 struct usb_device *udev;
305 if (child == NULL || ep == NULL)
308 udev = child->parent_hs_hub;
309 port = child->hs_port_no;
316 (udev->speed != USB_SPEED_HIGH) ||
317 (child->speed != USB_SPEED_LOW &&
318 child->speed != USB_SPEED_FULL) ||
319 (child->flags.usb_mode != USB_MODE_HOST) ||
320 (port == 0) || (ep->edesc == NULL)) {
325 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
327 up = hub->ports + port - 1;
329 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
330 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
333 /* if we already received a clear buffer request, reset the whole TT */
334 if (up->req_reset_tt.bRequest != 0) {
335 req.bmRequestType = UT_WRITE_CLASS_OTHER;
336 req.bRequest = UR_RESET_TT;
337 USETW(req.wValue, 0);
338 req.wIndex[0] = port;
340 USETW(req.wLength, 0);
342 wValue = (ep->edesc->bEndpointAddress & 0xF) |
343 ((child->address & 0x7F) << 4) |
344 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
345 ((ep->edesc->bmAttributes & 3) << 12);
347 req.bmRequestType = UT_WRITE_CLASS_OTHER;
348 req.bRequest = UR_CLEAR_TT_BUFFER;
349 USETW(req.wValue, wValue);
350 req.wIndex[0] = port;
352 USETW(req.wLength, 0);
354 up->req_reset_tt = req;
355 /* get reset transfer started */
356 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
357 &hub->tt_msg[0], &hub->tt_msg[1]);
361 #if USB_HAVE_TT_SUPPORT
363 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
365 struct uhub_softc *sc;
366 struct usb_device *udev;
370 DPRINTF("TT buffer reset\n");
372 sc = usbd_xfer_softc(xfer);
375 switch (USB_GET_STATE(xfer)) {
376 case USB_ST_TRANSFERRED:
379 USB_BUS_LOCK(udev->bus);
380 /* find first port which needs a TT reset */
381 for (x = 0; x != udev->hub->nports; x++) {
382 up = udev->hub->ports + x;
384 if (up->req_reset_tt.bRequest == 0)
387 /* copy in the transfer */
388 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
389 sizeof(up->req_reset_tt));
391 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
394 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
396 USB_BUS_UNLOCK(udev->bus);
398 usbd_transfer_submit(xfer);
401 USB_BUS_UNLOCK(udev->bus);
405 if (error == USB_ERR_CANCELLED)
408 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
414 /*------------------------------------------------------------------------*
415 * uhub_count_active_host_ports
417 * This function counts the number of active ports at the given speed.
418 *------------------------------------------------------------------------*/
420 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
422 struct uhub_softc *sc;
423 struct usb_device *child;
438 for (x = 0; x != hub->nports; x++) {
440 child = usb_bus_port_get_device(udev->bus, up);
442 child->flags.usb_mode == USB_MODE_HOST &&
443 child->speed == speed)
451 uhub_explore_handle_re_enumerate(struct usb_device *child)
456 /* check if device should be re-enumerated */
457 if (child->flags.usb_mode != USB_MODE_HOST)
460 do_unlock = usbd_enum_lock(child);
461 switch (child->re_enumerate_wait) {
462 case USB_RE_ENUM_START:
463 err = usbd_set_config_index(child,
466 DPRINTF("Unconfigure failed: %s: Ignored.\n",
469 if (child->parent_hub == NULL) {
470 /* the root HUB cannot be re-enumerated */
471 DPRINTFN(6, "cannot reset root HUB\n");
474 err = usbd_req_re_enumerate(child, NULL);
477 err = usbd_set_config_index(child, 0);
479 err = usb_probe_and_attach(child,
480 USB_IFACE_INDEX_ANY);
482 child->re_enumerate_wait = USB_RE_ENUM_DONE;
485 case USB_RE_ENUM_PWR_OFF:
486 /* get the device unconfigured */
487 err = usbd_set_config_index(child,
490 DPRINTFN(0, "Could not unconfigure "
491 "device (ignored)\n");
493 if (child->parent_hub == NULL) {
494 /* the root HUB cannot be re-enumerated */
495 DPRINTFN(6, "cannot set port feature\n");
498 /* clear port enable */
499 err = usbd_req_clear_port_feature(child->parent_hub,
500 NULL, child->port_no, UHF_PORT_ENABLE);
502 DPRINTFN(0, "Could not disable port "
506 child->re_enumerate_wait = USB_RE_ENUM_DONE;
509 case USB_RE_ENUM_SET_CONFIG:
510 err = usbd_set_config_index(child,
511 child->next_config_index);
513 DPRINTF("Configure failed: %s: Ignored.\n",
516 err = usb_probe_and_attach(child,
517 USB_IFACE_INDEX_ANY);
519 child->re_enumerate_wait = USB_RE_ENUM_DONE;
523 child->re_enumerate_wait = USB_RE_ENUM_DONE;
527 usbd_enum_unlock(child);
530 /*------------------------------------------------------------------------*
531 * uhub_explore_sub - subroutine
535 * Else: A control transaction failed
536 *------------------------------------------------------------------------*/
538 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
541 struct usb_device *child;
545 bus = sc->sc_udev->bus;
548 /* get driver added refcount from USB bus */
549 refcount = bus->driver_added_refcount;
551 /* get device assosiated with the given port */
552 child = usb_bus_port_get_device(bus, up);
558 uhub_explore_handle_re_enumerate(child);
560 /* check if probe and attach should be done */
562 if (child->driver_added_refcount != refcount) {
563 child->driver_added_refcount = refcount;
564 err = usb_probe_and_attach(child,
565 USB_IFACE_INDEX_ANY);
570 /* start control transfer, if device mode */
572 if (child->flags.usb_mode == USB_MODE_DEVICE)
573 usbd_ctrl_transfer_setup(child);
575 /* if a HUB becomes present, do a recursive HUB explore */
578 err = (child->hub->explore) (child);
584 /*------------------------------------------------------------------------*
585 * uhub_read_port_status - factored out code
586 *------------------------------------------------------------------------*/
588 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
590 struct usb_port_status ps;
593 if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
594 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
595 sc->sc_st.port_status = 0;
596 sc->sc_st.port_change = 0;
597 return (USB_ERR_TIMEOUT);
600 err = usbd_req_get_port_status(
601 sc->sc_udev, NULL, &ps, portno);
604 sc->sc_st.port_status = UGETW(ps.wPortStatus);
605 sc->sc_st.port_change = UGETW(ps.wPortChange);
606 sc->sc_usb_port_errors = 0;
608 sc->sc_st.port_status = 0;
609 sc->sc_st.port_change = 0;
610 sc->sc_usb_port_errors++;
613 /* debugging print */
615 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
616 "wPortChange=0x%04x, err=%s\n",
617 portno, sc->sc_st.port_status,
618 sc->sc_st.port_change, usbd_errstr(err));
622 /*------------------------------------------------------------------------*
627 * Else: A control transaction failed
628 *------------------------------------------------------------------------*/
630 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
632 struct usb_device *child;
633 struct usb_device *udev;
634 enum usb_dev_speed speed;
635 enum usb_hc_mode mode;
640 DPRINTF("reattaching port %d\n", portno);
644 child = usb_bus_port_get_device(udev->bus,
645 udev->hub->ports + portno - 1);
649 /* first clear the port connection change bit */
651 err = usbd_req_clear_port_feature(udev, NULL,
652 portno, UHF_C_PORT_CONNECTION);
657 /* check if there is a child */
661 * Free USB device and all subdevices, if any.
663 usb_free_device(child, 0);
666 /* get fresh status */
668 err = uhub_read_port_status(sc, portno);
672 #if USB_HAVE_DISABLE_ENUM
673 /* check if we should skip enumeration from this USB HUB */
674 if (usb_disable_enumeration != 0 ||
675 sc->sc_disable_enumeration != 0) {
676 DPRINTF("Enumeration is disabled!\n");
680 /* check if nothing is connected to the port */
682 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
685 /* check if there is no power on the port and print a warning */
687 switch (udev->speed) {
691 power_mask = UPS_PORT_POWER;
693 case USB_SPEED_SUPER:
694 if (udev->parent_hub == NULL)
695 power_mask = 0; /* XXX undefined */
697 power_mask = UPS_PORT_POWER_SS;
703 if ((sc->sc_st.port_status & power_mask) != power_mask) {
704 DPRINTF("WARNING: strange, connected port %d "
705 "has no power\n", portno);
708 /* check if the device is in Host Mode */
710 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
712 DPRINTF("Port %d is in Host Mode\n", portno);
714 if (sc->sc_st.port_status & UPS_SUSPEND) {
716 * NOTE: Should not get here in SuperSpeed
717 * mode, because the HUB should report this
720 DPRINTF("Port %d was still "
721 "suspended, clearing.\n", portno);
722 err = usbd_req_clear_port_feature(udev,
723 NULL, portno, UHF_PORT_SUSPEND);
728 /* wait for maximum device power up time */
731 USB_MS_TO_TICKS(usb_port_powerup_delay));
733 /* reset port, which implies enabling it */
735 err = usbd_req_reset_port(udev, NULL, portno);
738 DPRINTFN(0, "port %d reset "
739 "failed, error=%s\n",
740 portno, usbd_errstr(err));
743 /* get port status again, it might have changed during reset */
745 err = uhub_read_port_status(sc, portno);
749 /* check if something changed during port reset */
751 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
752 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
754 DPRINTFN(0, "giving up port reset "
755 "- device vanished\n");
762 DPRINTF("Port %d is in Device Mode\n", portno);
766 * Figure out the device speed
768 switch (udev->speed) {
770 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
771 speed = USB_SPEED_HIGH;
772 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
773 speed = USB_SPEED_LOW;
775 speed = USB_SPEED_FULL;
778 if (sc->sc_st.port_status & UPS_LOW_SPEED)
779 speed = USB_SPEED_LOW;
781 speed = USB_SPEED_FULL;
784 speed = USB_SPEED_LOW;
786 case USB_SPEED_SUPER:
787 if (udev->parent_hub == NULL) {
788 /* Root HUB - special case */
789 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
791 speed = USB_SPEED_FULL;
794 speed = USB_SPEED_LOW;
797 speed = USB_SPEED_HIGH;
800 speed = USB_SPEED_SUPER;
804 speed = USB_SPEED_SUPER;
808 /* same speed like parent */
812 if (speed == USB_SPEED_SUPER) {
813 err = usbd_req_set_hub_u1_timeout(udev, NULL,
814 portno, 128 - (2 * udev->depth));
816 DPRINTFN(0, "port %d U1 timeout "
817 "failed, error=%s\n",
818 portno, usbd_errstr(err));
820 err = usbd_req_set_hub_u2_timeout(udev, NULL,
821 portno, 128 - (2 * udev->depth));
823 DPRINTFN(0, "port %d U2 timeout "
824 "failed, error=%s\n",
825 portno, usbd_errstr(err));
830 * Figure out the device mode
832 * NOTE: This part is currently FreeBSD specific.
834 if (udev->parent_hub != NULL) {
835 /* inherit mode from the parent HUB */
836 mode = udev->parent_hub->flags.usb_mode;
837 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
838 mode = USB_MODE_DEVICE;
840 mode = USB_MODE_HOST;
842 /* need to create a new child */
843 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
844 udev->depth + 1, portno - 1, portno, speed, mode);
846 DPRINTFN(0, "could not allocate new device\n");
849 return (0); /* success */
854 * Free USB device and all subdevices, if any.
856 usb_free_device(child, 0);
860 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
861 err = usbd_req_clear_port_feature(
863 portno, UHF_PORT_ENABLE);
867 DPRINTFN(0, "device problem (%s), "
868 "disabling port %d\n", usbd_errstr(err), portno);
873 /*------------------------------------------------------------------------*
874 * usb_device_20_compatible
877 * 0: HUB does not support suspend and resume
878 * Else: HUB supports suspend and resume
879 *------------------------------------------------------------------------*/
881 usb_device_20_compatible(struct usb_device *udev)
885 switch (udev->speed) {
895 /*------------------------------------------------------------------------*
896 * uhub_suspend_resume_port
900 * Else: A control transaction failed
901 *------------------------------------------------------------------------*/
903 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
905 struct usb_device *child;
906 struct usb_device *udev;
910 DPRINTF("port %d\n", portno);
913 child = usb_bus_port_get_device(udev->bus,
914 udev->hub->ports + portno - 1);
916 /* first clear the port suspend change bit */
918 if (usb_device_20_compatible(udev)) {
919 err = usbd_req_clear_port_feature(udev, NULL,
920 portno, UHF_C_PORT_SUSPEND);
922 err = usbd_req_clear_port_feature(udev, NULL,
923 portno, UHF_C_PORT_LINK_STATE);
927 DPRINTF("clearing suspend failed.\n");
930 /* get fresh status */
932 err = uhub_read_port_status(sc, portno);
934 DPRINTF("reading port status failed.\n");
937 /* convert current state */
939 if (usb_device_20_compatible(udev)) {
940 if (sc->sc_st.port_status & UPS_SUSPEND) {
946 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
950 case UPS_PORT_LS_SS_INA:
951 usbd_req_warm_reset_port(udev, NULL, portno);
960 DPRINTF("suspended=%u\n", is_suspend);
962 /* do the suspend or resume */
966 * This code handle two cases: 1) Host Mode - we can only
967 * receive resume here 2) Device Mode - we can receive
968 * suspend and resume here
971 usb_dev_resume_peer(child);
972 else if (child->flags.usb_mode == USB_MODE_DEVICE)
973 usb_dev_suspend_peer(child);
979 /*------------------------------------------------------------------------*
980 * uhub_root_interrupt
982 * This function is called when a Root HUB interrupt has
983 * happened. "ptr" and "len" makes up the Root HUB interrupt
984 * packet. This function is called having the "bus_mtx" locked.
985 *------------------------------------------------------------------------*/
987 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
989 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
991 usb_needs_explore(bus, 0);
995 uhub_is_too_deep(struct usb_device *udev)
997 switch (udev->speed) {
1000 case USB_SPEED_HIGH:
1001 if (udev->depth > USB_HUB_MAX_DEPTH)
1004 case USB_SPEED_SUPER:
1005 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
1014 /*------------------------------------------------------------------------*
1020 *------------------------------------------------------------------------*/
1022 uhub_explore(struct usb_device *udev)
1024 struct usb_hub *hub;
1025 struct uhub_softc *sc;
1026 struct usb_port *up;
1035 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1037 /* ignore devices that are too deep */
1038 if (uhub_is_too_deep(udev))
1039 return (USB_ERR_TOO_DEEP);
1041 /* check if device is suspended */
1042 if (udev->flags.self_suspended) {
1043 /* need to wait until the child signals resume */
1044 DPRINTF("Device is suspended!\n");
1049 * Make sure we don't race against user-space applications
1052 do_unlock = usbd_enum_lock(udev);
1054 for (x = 0; x != hub->nports; x++) {
1055 up = hub->ports + x;
1058 err = uhub_read_port_status(sc, portno);
1060 /* most likely the HUB is gone */
1063 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1064 DPRINTF("Overcurrent on port %u.\n", portno);
1065 err = usbd_req_clear_port_feature(
1066 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1068 /* most likely the HUB is gone */
1072 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1074 * Fake a connect status change so that the
1075 * status gets checked initially!
1077 sc->sc_st.port_change |=
1078 UPS_C_CONNECT_STATUS;
1080 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1081 err = usbd_req_clear_port_feature(
1082 udev, NULL, portno, UHF_C_PORT_ENABLE);
1084 /* most likely the HUB is gone */
1087 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1089 * Ignore the port error if the device
1092 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1093 DPRINTFN(0, "illegal enable change, "
1094 "port %d\n", portno);
1097 if (up->restartcnt == USB_RESTART_MAX) {
1098 /* XXX could try another speed ? */
1099 DPRINTFN(0, "port error, giving up "
1100 "port %d\n", portno);
1102 sc->sc_st.port_change |=
1103 UPS_C_CONNECT_STATUS;
1108 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1109 err = uhub_reattach_port(sc, portno);
1111 /* most likely the HUB is gone */
1115 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1116 UPS_C_PORT_LINK_STATE)) {
1117 err = uhub_suspend_resume_port(sc, portno);
1119 /* most likely the HUB is gone */
1123 err = uhub_explore_sub(sc, up);
1125 /* no device(s) present */
1128 /* explore succeeded - reset restart counter */
1133 usbd_enum_unlock(udev);
1135 /* initial status checked */
1136 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1138 /* return success */
1139 return (USB_ERR_NORMAL_COMPLETION);
1143 uhub_probe(device_t dev)
1145 struct usb_attach_arg *uaa = device_get_ivars(dev);
1147 if (uaa->usb_mode != USB_MODE_HOST)
1151 * The subclass for USB HUBs is currently ignored because it
1152 * is 0 for some and 1 for others.
1154 if (uaa->info.bConfigIndex == 0 &&
1155 uaa->info.bDeviceClass == UDCLASS_HUB)
1161 /* NOTE: The information returned by this function can be wrong. */
1163 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1165 struct usb_hub_descriptor hubdesc20;
1166 struct usb_hub_ss_descriptor hubdesc30;
1171 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1172 return (USB_ERR_INVAL);
1177 switch (udev->speed) {
1179 case USB_SPEED_FULL:
1180 case USB_SPEED_HIGH:
1181 /* assuming that there is one port */
1182 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1184 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1185 "error=%s\n", usbd_errstr(err));
1188 nports = hubdesc20.bNbrPorts;
1192 if (udev->speed == USB_SPEED_HIGH)
1193 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1196 case USB_SPEED_SUPER:
1197 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1199 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1200 "error=%s\n", usbd_errstr(err));
1203 nports = hubdesc30.bNbrPorts;
1209 err = USB_ERR_INVAL;
1213 if (pnports != NULL)
1223 uhub_attach(device_t dev)
1225 struct uhub_softc *sc = device_get_softc(dev);
1226 struct usb_attach_arg *uaa = device_get_ivars(dev);
1227 struct usb_device *udev = uaa->device;
1228 struct usb_device *parent_hub = udev->parent_hub;
1229 struct usb_hub *hub;
1230 struct usb_hub_descriptor hubdesc20;
1231 struct usb_hub_ss_descriptor hubdesc30;
1232 #if USB_HAVE_DISABLE_ENUM
1233 struct sysctl_ctx_list *sysctl_ctx;
1234 struct sysctl_oid *sysctl_tree;
1241 uint8_t iface_index;
1247 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1249 device_set_usb_desc(dev);
1251 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1252 "parent->selfpowered=%d\n",
1254 udev->flags.self_powered,
1257 parent_hub->flags.self_powered : 0);
1259 if (uhub_is_too_deep(udev)) {
1260 DPRINTFN(0, "HUB at depth %d, "
1261 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1265 if (!udev->flags.self_powered && parent_hub &&
1266 !parent_hub->flags.self_powered) {
1267 DPRINTFN(0, "Bus powered HUB connected to "
1268 "bus powered HUB. HUB ignored\n");
1272 if (UHUB_IS_MULTI_TT(sc)) {
1273 err = usbd_set_alt_interface_index(udev, 0, 1);
1275 device_printf(dev, "MTT could not be enabled\n");
1278 device_printf(dev, "MTT enabled\n");
1281 /* get HUB descriptor */
1283 DPRINTFN(2, "Getting HUB descriptor\n");
1285 switch (udev->speed) {
1287 case USB_SPEED_FULL:
1288 case USB_SPEED_HIGH:
1289 /* assuming that there is one port */
1290 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1292 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1293 "error=%s\n", usbd_errstr(err));
1296 /* get number of ports */
1297 nports = hubdesc20.bNbrPorts;
1299 /* get power delay */
1300 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1301 usb_extra_power_up_time);
1303 /* get complete HUB descriptor */
1305 /* check number of ports */
1307 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1308 "error=%s\n", usbd_errstr(err));
1311 /* get complete HUB descriptor */
1312 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1315 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1316 "error=%s\n", usbd_errstr(err));
1319 if (hubdesc20.bNbrPorts != nports) {
1320 DPRINTFN(0, "Number of ports changed\n");
1325 case USB_SPEED_SUPER:
1326 if (udev->parent_hub != NULL) {
1327 err = usbd_req_set_hub_depth(udev, NULL,
1330 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1331 "error=%s\n", usbd_errstr(err));
1335 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1337 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1338 "error=%s\n", usbd_errstr(err));
1341 /* get number of ports */
1342 nports = hubdesc30.bNbrPorts;
1344 /* get power delay */
1345 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1346 usb_extra_power_up_time);
1348 /* get complete HUB descriptor */
1350 /* check number of ports */
1351 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1352 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1353 "error=%s\n", usbd_errstr(err));
1356 /* get complete HUB descriptor */
1357 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1360 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1361 "error=%s\n", usbd_errstr(err));
1364 if (hubdesc30.bNbrPorts != nports) {
1365 DPRINTFN(0, "Number of ports changed\n");
1371 DPRINTF("Assuming HUB has only one port\n");
1372 /* default number of ports */
1374 /* default power delay */
1375 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1379 DPRINTFN(0, "portless HUB\n");
1382 if (nports > USB_MAX_PORTS) {
1383 DPRINTF("Port limit exceeded\n");
1386 #if (USB_HAVE_FIXED_PORT == 0)
1387 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1388 M_USBDEV, M_WAITOK | M_ZERO);
1397 /* initialize HUB structure */
1399 hub->explore = &uhub_explore;
1400 hub->nports = nports;
1401 hub->hubudev = udev;
1402 #if USB_HAVE_TT_SUPPORT
1403 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1404 hub->tt_msg[0].udev = udev;
1405 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1406 hub->tt_msg[1].udev = udev;
1408 /* if self powered hub, give ports maximum current */
1409 if (udev->flags.self_powered) {
1410 hub->portpower = USB_MAX_POWER;
1412 hub->portpower = USB_MIN_POWER;
1415 /* set up interrupt pipe */
1417 if (udev->parent_hub == NULL) {
1418 /* root HUB is special */
1422 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1423 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1426 DPRINTFN(0, "cannot setup interrupt transfer, "
1427 "errstr=%s\n", usbd_errstr(err));
1430 /* wait with power off for a while */
1431 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1433 #if USB_HAVE_DISABLE_ENUM
1434 /* Add device sysctls */
1436 sysctl_ctx = device_get_sysctl_ctx(dev);
1437 sysctl_tree = device_get_sysctl_tree(dev);
1439 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1442 snprintf(path, sizeof(path), "dev.uhub.%d.disable_enumeration",
1443 device_get_unit(dev));
1444 TUNABLE_INT_FETCH(path, &sc->sc_disable_enumeration);
1446 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1447 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1448 &sc->sc_disable_enumeration, 0,
1449 "Set to disable enumeration on this USB HUB.");
1451 snprintf(path, sizeof(path), "dev.uhub.%d.disable_port_power",
1452 device_get_unit(dev));
1453 TUNABLE_INT_FETCH(path, &sc->sc_disable_port_power);
1455 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1456 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1457 &sc->sc_disable_port_power, 0,
1458 "Set to disable USB port power on this USB HUB.");
1462 * To have the best chance of success we do things in the exact same
1463 * order as Windoze98. This should not be necessary, but some
1464 * devices do not follow the USB specs to the letter.
1466 * These are the events on the bus when a hub is attached:
1467 * Get device and config descriptors (see attach code)
1468 * Get hub descriptor (see above)
1471 * wait for power to become stable
1472 * (all below happens in explore code)
1474 * clear C_PORT_CONNECTION
1477 * if device connected
1481 * clear C_PORT_RESET
1483 * proceed with device attachment
1486 /* XXX should check for none, individual, or ganged power? */
1490 for (x = 0; x != nports; x++) {
1491 /* set up data structures */
1492 struct usb_port *up = hub->ports + x;
1494 up->device_index = 0;
1498 /* check if port is removable */
1499 switch (udev->speed) {
1501 case USB_SPEED_FULL:
1502 case USB_SPEED_HIGH:
1503 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1506 case USB_SPEED_SUPER:
1507 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1511 DPRINTF("Assuming removable port\n");
1516 #if USB_HAVE_DISABLE_ENUM
1517 /* check if we should disable USB port power or not */
1518 if (usb_disable_port_power != 0 ||
1519 sc->sc_disable_port_power != 0) {
1520 /* turn the power off */
1521 DPRINTFN(2, "Turning port %d power off\n", portno);
1522 err = usbd_req_clear_port_feature(udev, NULL,
1523 portno, UHF_PORT_POWER);
1526 /* turn the power on */
1527 DPRINTFN(2, "Turning port %d power on\n", portno);
1528 err = usbd_req_set_port_feature(udev, NULL,
1529 portno, UHF_PORT_POWER);
1530 #if USB_HAVE_DISABLE_ENUM
1535 DPRINTFN(0, "port %d power on or off failed, %s\n",
1536 portno, usbd_errstr(err));
1538 DPRINTF("turn on port %d power\n",
1541 /* wait for stable power */
1542 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1545 device_printf(dev, "%d port%s with %d "
1546 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1547 removable, udev->flags.self_powered ? "self" : "bus");
1549 /* Start the interrupt endpoint, if any */
1551 mtx_lock(&sc->sc_mtx);
1552 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1553 mtx_unlock(&sc->sc_mtx);
1555 /* Enable automatic power save on all USB HUBs */
1557 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1562 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1564 #if (USB_HAVE_FIXED_PORT == 0)
1565 free(udev->hub, M_USBDEV);
1569 mtx_destroy(&sc->sc_mtx);
1575 * Called from process context when the hub is gone.
1576 * Detach all devices on active ports.
1579 uhub_detach(device_t dev)
1581 struct uhub_softc *sc = device_get_softc(dev);
1582 struct usb_hub *hub = sc->sc_udev->hub;
1583 struct usb_bus *bus = sc->sc_udev->bus;
1584 struct usb_device *child;
1587 if (hub == NULL) /* must be partially working */
1590 /* Make sure interrupt transfer is gone. */
1591 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1593 /* Detach all ports */
1594 for (x = 0; x != hub->nports; x++) {
1596 child = usb_bus_port_get_device(bus, hub->ports + x);
1598 if (child == NULL) {
1603 * Free USB device and all subdevices, if any.
1605 usb_free_device(child, 0);
1608 #if USB_HAVE_TT_SUPPORT
1609 /* Make sure our TT messages are not queued anywhere */
1611 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1612 &hub->tt_msg[0], &hub->tt_msg[1]);
1613 USB_BUS_UNLOCK(bus);
1616 #if (USB_HAVE_FIXED_PORT == 0)
1617 free(hub, M_USBDEV);
1619 sc->sc_udev->hub = NULL;
1621 mtx_destroy(&sc->sc_mtx);
1627 uhub_suspend(device_t dev)
1630 /* Sub-devices are not suspended here! */
1635 uhub_resume(device_t dev)
1638 /* Sub-devices are not resumed here! */
1643 uhub_driver_added(device_t dev, driver_t *driver)
1645 usb_needs_explore_all();
1649 struct usb_device *udev;
1651 uint8_t iface_index;
1655 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1656 struct hub_result *res)
1658 struct usb_interface *iface;
1659 struct usb_device *udev;
1664 nports = hub->nports;
1665 for (x = 0; x != nports; x++) {
1666 udev = usb_bus_port_get_device(hub->hubudev->bus,
1671 for (i = 0; i != USB_IFACE_MAX; i++) {
1672 iface = usbd_get_iface(udev, i);
1674 (iface->subdev == child)) {
1675 res->iface_index = i;
1677 res->portno = x + 1;
1682 res->iface_index = 0;
1688 uhub_child_location_string(device_t parent, device_t child,
1689 char *buf, size_t buflen)
1691 struct uhub_softc *sc;
1692 struct usb_hub *hub;
1693 struct hub_result res;
1695 if (!device_is_attached(parent)) {
1701 sc = device_get_softc(parent);
1702 hub = sc->sc_udev->hub;
1705 uhub_find_iface_index(hub, child, &res);
1707 DPRINTF("device not on hub\n");
1713 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1718 , device_get_unit(res.udev->bus->bdev)
1719 , (res.udev->parent_hub != NULL) ?
1720 res.udev->parent_hub->device_index : 0
1721 , res.portno, res.udev->device_index, res.iface_index
1723 , res.udev->ugen_name
1733 uhub_child_pnpinfo_string(device_t parent, device_t child,
1734 char *buf, size_t buflen)
1736 struct uhub_softc *sc;
1737 struct usb_hub *hub;
1738 struct usb_interface *iface;
1739 struct hub_result res;
1741 if (!device_is_attached(parent)) {
1747 sc = device_get_softc(parent);
1748 hub = sc->sc_udev->hub;
1751 uhub_find_iface_index(hub, child, &res);
1753 DPRINTF("device not on hub\n");
1759 iface = usbd_get_iface(res.udev, res.iface_index);
1760 if (iface && iface->idesc) {
1761 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1762 "devclass=0x%02x devsubclass=0x%02x "
1766 "intclass=0x%02x intsubclass=0x%02x "
1767 "intprotocol=0x%02x" "%s%s",
1768 UGETW(res.udev->ddesc.idVendor),
1769 UGETW(res.udev->ddesc.idProduct),
1770 res.udev->ddesc.bDeviceClass,
1771 res.udev->ddesc.bDeviceSubClass,
1772 usb_get_serial(res.udev),
1773 UGETW(res.udev->ddesc.bcdDevice),
1774 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1775 iface->idesc->bInterfaceClass,
1776 iface->idesc->bInterfaceSubClass,
1777 iface->idesc->bInterfaceProtocol,
1778 iface->pnpinfo ? " " : "",
1779 iface->pnpinfo ? iface->pnpinfo : "");
1793 * The USB Transaction Translator:
1794 * ===============================
1796 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1797 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1798 * USB transfers. To utilize bandwidth dynamically the "scatter and
1799 * gather" principle must be applied. This means that bandwidth must
1800 * be divided into equal parts of bandwidth. With regard to USB all
1801 * data is transferred in smaller packets with length
1802 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1805 * The bandwidth scheduler which I have implemented will simply pack
1806 * the USB transfers back to back until there is no more space in the
1807 * schedule. Out of the 8 microframes which the USB 2.0 standard
1808 * provides, only 6 are available for non-HIGH-speed devices. I have
1809 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1810 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1811 * this division, it is very difficult to allocate and free bandwidth
1814 * NOTE about the Transaction Translator in USB HUBs:
1816 * USB HUBs have a very simple Transaction Translator, that will
1817 * simply pipeline all the SPLIT transactions. That means that the
1818 * transactions will be executed in the order they are queued!
1822 /*------------------------------------------------------------------------*
1823 * usb_intr_find_best_slot
1826 * The best Transaction Translation slot for an interrupt endpoint.
1827 *------------------------------------------------------------------------*/
1829 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1830 uint8_t end, uint8_t mask)
1832 usb_size_t min = (usb_size_t)-1;
1840 /* find the last slot with lesser used bandwidth */
1842 for (x = start; x < end; x++) {
1846 /* compute sum of bandwidth */
1847 for (z = x; z < end; z++) {
1848 if (mask & (1U << (z - x)))
1852 /* check if the current multi-slot is more optimal */
1858 /* check if the mask is about to be shifted out */
1859 if (mask & (1U << (end - 1 - x)))
1865 /*------------------------------------------------------------------------*
1866 * usb_hs_bandwidth_adjust
1868 * This function will update the bandwith usage for the microframe
1869 * having index "slot" by "len" bytes. "len" can be negative. If the
1870 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1871 * the "slot" argument will be replaced by the slot having least used
1872 * bandwidth. The "mask" argument is used for multi-slot allocations.
1875 * The slot in which the bandwidth update was done: 0..7
1876 *------------------------------------------------------------------------*/
1878 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1879 uint8_t slot, uint8_t mask)
1881 struct usb_bus *bus = udev->bus;
1882 struct usb_hub *hub;
1883 enum usb_dev_speed speed;
1886 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1888 speed = usbd_get_speed(udev);
1892 case USB_SPEED_FULL:
1893 if (speed == USB_SPEED_LOW) {
1897 * The Host Controller Driver should have
1898 * performed checks so that the lookup
1899 * below does not result in a NULL pointer
1903 hub = udev->parent_hs_hub->hub;
1904 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1905 slot = usb_intr_find_best_slot(hub->uframe_usage,
1906 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1908 for (x = slot; x < 8; x++) {
1909 if (mask & (1U << (x - slot))) {
1910 hub->uframe_usage[x] += len;
1911 bus->uframe_usage[x] += len;
1916 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1917 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1918 USB_HS_MICRO_FRAMES_MAX, mask);
1920 for (x = slot; x < 8; x++) {
1921 if (mask & (1U << (x - slot))) {
1922 bus->uframe_usage[x] += len;
1930 /*------------------------------------------------------------------------*
1931 * usb_hs_bandwidth_alloc
1933 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1934 *------------------------------------------------------------------------*/
1936 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1938 struct usb_device *udev;
1943 udev = xfer->xroot->udev;
1945 if (udev->flags.usb_mode != USB_MODE_HOST)
1946 return; /* not supported */
1948 xfer->endpoint->refcount_bw++;
1949 if (xfer->endpoint->refcount_bw != 1)
1950 return; /* already allocated */
1952 speed = usbd_get_speed(udev);
1954 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1956 /* allocate a microframe slot */
1959 slot = usb_hs_bandwidth_adjust(udev,
1960 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1962 xfer->endpoint->usb_uframe = slot;
1963 xfer->endpoint->usb_smask = mask << slot;
1965 if ((speed != USB_SPEED_FULL) &&
1966 (speed != USB_SPEED_LOW)) {
1967 xfer->endpoint->usb_cmask = 0x00 ;
1969 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1973 case UE_ISOCHRONOUS:
1974 switch (usbd_xfer_get_fps_shift(xfer)) {
1989 /* allocate a microframe multi-slot */
1991 slot = usb_hs_bandwidth_adjust(udev,
1992 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1994 xfer->endpoint->usb_uframe = slot;
1995 xfer->endpoint->usb_cmask = 0;
1996 xfer->endpoint->usb_smask = mask << slot;
2000 xfer->endpoint->usb_uframe = 0;
2001 xfer->endpoint->usb_cmask = 0;
2002 xfer->endpoint->usb_smask = 0;
2006 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2007 xfer->endpoint->usb_uframe,
2008 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
2011 /*------------------------------------------------------------------------*
2012 * usb_hs_bandwidth_free
2014 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
2015 *------------------------------------------------------------------------*/
2017 usb_hs_bandwidth_free(struct usb_xfer *xfer)
2019 struct usb_device *udev;
2023 udev = xfer->xroot->udev;
2025 if (udev->flags.usb_mode != USB_MODE_HOST)
2026 return; /* not supported */
2028 xfer->endpoint->refcount_bw--;
2029 if (xfer->endpoint->refcount_bw != 0)
2030 return; /* still allocated */
2032 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2034 case UE_ISOCHRONOUS:
2036 slot = xfer->endpoint->usb_uframe;
2037 mask = xfer->endpoint->usb_smask;
2039 /* free microframe slot(s): */
2040 usb_hs_bandwidth_adjust(udev,
2041 -xfer->max_frame_size, slot, mask >> slot);
2043 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2044 slot, mask >> slot);
2046 xfer->endpoint->usb_uframe = 0;
2047 xfer->endpoint->usb_cmask = 0;
2048 xfer->endpoint->usb_smask = 0;
2056 /*------------------------------------------------------------------------*
2057 * usb_isoc_time_expand
2059 * This function will expand the time counter from 7-bit to 16-bit.
2062 * 16-bit isochronous time counter.
2063 *------------------------------------------------------------------------*/
2065 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2069 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2071 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2073 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2075 if (isoc_time_curr < rem) {
2076 /* the time counter wrapped around */
2077 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2079 /* update the remainder */
2081 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2082 bus->isoc_time_last |= isoc_time_curr;
2084 return (bus->isoc_time_last);
2087 /*------------------------------------------------------------------------*
2088 * usbd_fs_isoc_schedule_alloc_slot
2090 * This function will allocate bandwidth for an isochronous FULL speed
2091 * transaction in the FULL speed schedule.
2096 *------------------------------------------------------------------------*/
2097 #if USB_HAVE_TT_SUPPORT
2099 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2101 struct usb_xfer *xfer;
2102 struct usb_xfer *pipe_xfer;
2103 struct usb_bus *bus;
2105 usb_frlength_t data_len;
2113 bus = isoc_xfer->xroot->bus;
2115 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2117 /* skip self, if any */
2119 if (xfer == isoc_xfer)
2122 /* check if this USB transfer is going through the same TT */
2124 if (xfer->xroot->udev->parent_hs_hub !=
2125 isoc_xfer->xroot->udev->parent_hs_hub) {
2128 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2129 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2130 (xfer->xroot->udev->hs_port_no !=
2131 isoc_xfer->xroot->udev->hs_port_no)) {
2134 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2137 /* check if isoc_time is part of this transfer */
2139 delta = xfer->isoc_time_complete - isoc_time;
2140 if (delta > 0 && delta <= xfer->nframes) {
2141 delta = xfer->nframes - delta;
2143 len = xfer->frlengths[delta];
2152 * Check double buffered transfers. Only stream ID
2153 * equal to zero is valid here!
2155 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2158 /* skip self, if any */
2160 if (pipe_xfer == isoc_xfer)
2163 /* check if isoc_time is part of this transfer */
2165 delta = pipe_xfer->isoc_time_complete - isoc_time;
2166 if (delta > 0 && delta <= pipe_xfer->nframes) {
2167 delta = pipe_xfer->nframes - delta;
2169 len = pipe_xfer->frlengths[delta];
2179 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2180 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2184 /* check for overflow */
2186 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2191 delta = isoc_xfer->isoc_time_complete - isoc_time;
2192 if (delta > 0 && delta <= isoc_xfer->nframes) {
2193 delta = isoc_xfer->nframes - delta;
2195 len = isoc_xfer->frlengths[delta];
2203 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2204 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2208 /* check for overflow */
2210 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2217 /*------------------------------------------------------------------------*
2218 * usb_bus_port_get_device
2220 * This function is NULL safe.
2221 *------------------------------------------------------------------------*/
2223 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2225 if ((bus == NULL) || (up == NULL)) {
2229 if (up->device_index == 0) {
2233 return (bus->devices[up->device_index]);
2236 /*------------------------------------------------------------------------*
2237 * usb_bus_port_set_device
2239 * This function is NULL safe.
2240 *------------------------------------------------------------------------*/
2242 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2243 struct usb_device *udev, uint8_t device_index)
2250 * There is only one case where we don't
2251 * have an USB port, and that is the Root Hub!
2255 up->device_index = device_index;
2257 device_index = up->device_index;
2258 up->device_index = 0;
2262 * Make relationships to our new device
2264 if (device_index != 0) {
2266 mtx_lock(&usb_ref_lock);
2268 bus->devices[device_index] = udev;
2270 mtx_unlock(&usb_ref_lock);
2276 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2279 /*------------------------------------------------------------------------*
2282 * This functions is called when the USB event thread needs to run.
2283 *------------------------------------------------------------------------*/
2285 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2297 DPRINTF("No bus pointer!\n");
2300 if ((bus->devices == NULL) ||
2301 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2302 DPRINTF("No root HUB\n");
2305 if (mtx_owned(&bus->bus_mtx)) {
2314 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2315 &bus->explore_msg[0], &bus->explore_msg[1])) {
2319 USB_BUS_UNLOCK(bus);
2323 /*------------------------------------------------------------------------*
2324 * usb_needs_explore_all
2326 * This function is called whenever a new driver is loaded and will
2327 * cause that all USB busses are re-explored.
2328 *------------------------------------------------------------------------*/
2330 usb_needs_explore_all(void)
2332 struct usb_bus *bus;
2339 dc = usb_devclass_ptr;
2341 DPRINTFN(0, "no devclass\n");
2345 * Explore all USB busses in parallell.
2347 max = devclass_get_maxunit(dc);
2349 dev = devclass_get_device(dc, max);
2351 bus = device_get_softc(dev);
2353 usb_needs_explore(bus, 1);
2360 /*------------------------------------------------------------------------*
2361 * usb_needs_explore_init
2363 * This function will ensure that the USB controllers are not enumerated
2364 * until the "cold" variable is cleared.
2365 *------------------------------------------------------------------------*/
2367 usb_needs_explore_init(void *arg)
2370 * The cold variable should be cleared prior to this function
2374 usb_needs_explore_all();
2376 DPRINTFN(-1, "Cold variable is still set!\n");
2378 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2380 /*------------------------------------------------------------------------*
2381 * usb_bus_power_update
2383 * This function will ensure that all USB devices on the given bus are
2384 * properly suspended or resumed according to the device transfer
2386 *------------------------------------------------------------------------*/
2389 usb_bus_power_update(struct usb_bus *bus)
2391 usb_needs_explore(bus, 0 /* no probe */ );
2395 /*------------------------------------------------------------------------*
2396 * usbd_transfer_power_ref
2398 * This function will modify the power save reference counts and
2399 * wakeup the USB device associated with the given USB transfer, if
2401 *------------------------------------------------------------------------*/
2404 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2406 static const usb_power_mask_t power_mask[4] = {
2407 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2408 [UE_BULK] = USB_HW_POWER_BULK,
2409 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2410 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2412 struct usb_device *udev;
2413 uint8_t needs_explore;
2414 uint8_t needs_hw_power;
2417 udev = xfer->xroot->udev;
2419 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2420 /* no power save for root HUB */
2423 USB_BUS_LOCK(udev->bus);
2425 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2427 udev->pwr_save.last_xfer_time = ticks;
2428 udev->pwr_save.type_refs[xfer_type] += val;
2430 if (xfer->flags_int.control_xfr) {
2431 udev->pwr_save.read_refs += val;
2432 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2434 * It is not allowed to suspend during a
2437 udev->pwr_save.write_refs += val;
2439 } else if (USB_GET_DATA_ISREAD(xfer)) {
2440 udev->pwr_save.read_refs += val;
2442 udev->pwr_save.write_refs += val;
2446 if (udev->flags.self_suspended)
2447 needs_explore = usb_peer_should_wakeup(udev);
2451 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2452 DPRINTF("Adding type %u to power state\n", xfer_type);
2453 udev->bus->hw_power_state |= power_mask[xfer_type];
2463 USB_BUS_UNLOCK(udev->bus);
2465 if (needs_explore) {
2466 DPRINTF("update\n");
2467 usb_bus_power_update(udev->bus);
2468 } else if (needs_hw_power) {
2469 DPRINTF("needs power\n");
2470 if (udev->bus->methods->set_hw_power != NULL) {
2471 (udev->bus->methods->set_hw_power) (udev->bus);
2477 /*------------------------------------------------------------------------*
2478 * usb_peer_should_wakeup
2480 * This function returns non-zero if the current device should wake up.
2481 *------------------------------------------------------------------------*/
2483 usb_peer_should_wakeup(struct usb_device *udev)
2485 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2486 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2487 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2488 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2489 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2490 (udev->pwr_save.write_refs != 0) ||
2491 ((udev->pwr_save.read_refs != 0) &&
2492 (udev->flags.usb_mode == USB_MODE_HOST) &&
2493 (usb_peer_can_wakeup(udev) == 0)));
2496 /*------------------------------------------------------------------------*
2499 * This function implements the USB power daemon and is called
2500 * regularly from the USB explore thread.
2501 *------------------------------------------------------------------------*/
2504 usb_bus_powerd(struct usb_bus *bus)
2506 struct usb_device *udev;
2509 usb_ticks_t mintime;
2510 usb_size_t type_refs[5];
2513 limit = usb_power_timeout;
2516 else if (limit > 255)
2521 DPRINTF("bus=%p\n", bus);
2526 * The root HUB device is never suspended
2527 * and we simply skip it.
2529 for (x = USB_ROOT_HUB_ADDR + 1;
2530 x != bus->devices_max; x++) {
2532 udev = bus->devices[x];
2536 temp = ticks - udev->pwr_save.last_xfer_time;
2538 if (usb_peer_should_wakeup(udev)) {
2539 /* check if we are suspended */
2540 if (udev->flags.self_suspended != 0) {
2541 USB_BUS_UNLOCK(bus);
2542 usb_dev_resume_peer(udev);
2545 } else if ((temp >= limit) &&
2546 (udev->flags.usb_mode == USB_MODE_HOST) &&
2547 (udev->flags.self_suspended == 0)) {
2548 /* try to do suspend */
2550 USB_BUS_UNLOCK(bus);
2551 usb_dev_suspend_peer(udev);
2556 /* reset counters */
2558 mintime = (usb_ticks_t)-1;
2565 /* Re-loop all the devices to get the actual state */
2567 for (x = USB_ROOT_HUB_ADDR + 1;
2568 x != bus->devices_max; x++) {
2570 udev = bus->devices[x];
2574 /* we found a non-Root-Hub USB device */
2577 /* "last_xfer_time" can be updated by a resume */
2578 temp = ticks - udev->pwr_save.last_xfer_time;
2581 * Compute minimum time since last transfer for the complete
2587 if (udev->flags.self_suspended == 0) {
2588 type_refs[0] += udev->pwr_save.type_refs[0];
2589 type_refs[1] += udev->pwr_save.type_refs[1];
2590 type_refs[2] += udev->pwr_save.type_refs[2];
2591 type_refs[3] += udev->pwr_save.type_refs[3];
2595 if (mintime >= (usb_ticks_t)(1 * hz)) {
2596 /* recompute power masks */
2597 DPRINTF("Recomputing power masks\n");
2598 bus->hw_power_state = 0;
2599 if (type_refs[UE_CONTROL] != 0)
2600 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2601 if (type_refs[UE_BULK] != 0)
2602 bus->hw_power_state |= USB_HW_POWER_BULK;
2603 if (type_refs[UE_INTERRUPT] != 0)
2604 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2605 if (type_refs[UE_ISOCHRONOUS] != 0)
2606 bus->hw_power_state |= USB_HW_POWER_ISOC;
2607 if (type_refs[4] != 0)
2608 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2610 USB_BUS_UNLOCK(bus);
2612 if (bus->methods->set_hw_power != NULL) {
2613 /* always update hardware power! */
2614 (bus->methods->set_hw_power) (bus);
2620 /*------------------------------------------------------------------------*
2621 * usb_dev_resume_peer
2623 * This function will resume an USB peer and do the required USB
2624 * signalling to get an USB device out of the suspended state.
2625 *------------------------------------------------------------------------*/
2627 usb_dev_resume_peer(struct usb_device *udev)
2629 struct usb_bus *bus;
2636 /* check if already resumed */
2637 if (udev->flags.self_suspended == 0)
2640 /* we need a parent HUB to do resume */
2641 if (udev->parent_hub == NULL)
2644 DPRINTF("udev=%p\n", udev);
2646 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2647 (udev->flags.remote_wakeup == 0)) {
2649 * If the host did not set the remote wakeup feature, we can
2650 * not wake it up either!
2652 DPRINTF("remote wakeup is not set!\n");
2655 /* get bus pointer */
2658 /* resume parent hub first */
2659 usb_dev_resume_peer(udev->parent_hub);
2661 /* reduce chance of instant resume failure by waiting a little bit */
2662 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2664 if (usb_device_20_compatible(udev)) {
2665 /* resume current port (Valid in Host and Device Mode) */
2666 err = usbd_req_clear_port_feature(udev->parent_hub,
2667 NULL, udev->port_no, UHF_PORT_SUSPEND);
2669 DPRINTFN(0, "Resuming port failed\n");
2673 /* resume current port (Valid in Host and Device Mode) */
2674 err = usbd_req_set_port_link_state(udev->parent_hub,
2675 NULL, udev->port_no, UPS_PORT_LS_U0);
2677 DPRINTFN(0, "Resuming port failed\n");
2682 /* resume settle time */
2683 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2685 if (bus->methods->device_resume != NULL) {
2686 /* resume USB device on the USB controller */
2687 (bus->methods->device_resume) (udev);
2690 /* set that this device is now resumed */
2691 udev->flags.self_suspended = 0;
2693 /* make sure that we don't go into suspend right away */
2694 udev->pwr_save.last_xfer_time = ticks;
2696 /* make sure the needed power masks are on */
2697 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2698 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2699 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2700 bus->hw_power_state |= USB_HW_POWER_BULK;
2701 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2702 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2703 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2704 bus->hw_power_state |= USB_HW_POWER_ISOC;
2706 USB_BUS_UNLOCK(bus);
2708 if (bus->methods->set_hw_power != NULL) {
2709 /* always update hardware power! */
2710 (bus->methods->set_hw_power) (bus);
2715 /* notify all sub-devices about resume */
2716 err = usb_suspend_resume(udev, 0);
2718 usbd_sr_unlock(udev);
2720 /* check if peer has wakeup capability */
2721 if (usb_peer_can_wakeup(udev)) {
2722 /* clear remote wakeup */
2723 err = usbd_req_clear_device_feature(udev,
2724 NULL, UF_DEVICE_REMOTE_WAKEUP);
2726 DPRINTFN(0, "Clearing device "
2727 "remote wakeup failed: %s\n",
2733 /*------------------------------------------------------------------------*
2734 * usb_dev_suspend_peer
2736 * This function will suspend an USB peer and do the required USB
2737 * signalling to get an USB device into the suspended state.
2738 *------------------------------------------------------------------------*/
2740 usb_dev_suspend_peer(struct usb_device *udev)
2742 struct usb_device *child;
2752 /* check if already suspended */
2753 if (udev->flags.self_suspended)
2756 /* we need a parent HUB to do suspend */
2757 if (udev->parent_hub == NULL)
2760 DPRINTF("udev=%p\n", udev);
2762 /* check if the current device is a HUB */
2763 if (udev->hub != NULL) {
2764 nports = udev->hub->nports;
2766 /* check if all devices on the HUB are suspended */
2767 for (x = 0; x != nports; x++) {
2768 child = usb_bus_port_get_device(udev->bus,
2769 udev->hub->ports + x);
2774 if (child->flags.self_suspended)
2777 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2782 if (usb_peer_can_wakeup(udev)) {
2784 * This request needs to be done before we set
2785 * "udev->flags.self_suspended":
2788 /* allow device to do remote wakeup */
2789 err = usbd_req_set_device_feature(udev,
2790 NULL, UF_DEVICE_REMOTE_WAKEUP);
2792 DPRINTFN(0, "Setting device "
2793 "remote wakeup failed\n");
2797 USB_BUS_LOCK(udev->bus);
2799 * Checking for suspend condition and setting suspended bit
2802 err = usb_peer_should_wakeup(udev);
2805 * Set that this device is suspended. This variable
2806 * must be set before calling USB controller suspend
2809 udev->flags.self_suspended = 1;
2811 USB_BUS_UNLOCK(udev->bus);
2814 if (usb_peer_can_wakeup(udev)) {
2815 /* allow device to do remote wakeup */
2816 err = usbd_req_clear_device_feature(udev,
2817 NULL, UF_DEVICE_REMOTE_WAKEUP);
2819 DPRINTFN(0, "Setting device "
2820 "remote wakeup failed\n");
2824 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2825 /* resume parent HUB first */
2826 usb_dev_resume_peer(udev->parent_hub);
2828 /* reduce chance of instant resume failure by waiting a little bit */
2829 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2831 /* resume current port (Valid in Host and Device Mode) */
2832 err = usbd_req_clear_port_feature(udev->parent_hub,
2833 NULL, udev->port_no, UHF_PORT_SUSPEND);
2835 /* resume settle time */
2836 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2838 DPRINTF("Suspend was cancelled!\n");
2844 /* notify all sub-devices about suspend */
2845 err = usb_suspend_resume(udev, 1);
2847 usbd_sr_unlock(udev);
2849 if (udev->bus->methods->device_suspend != NULL) {
2852 /* suspend device on the USB controller */
2853 (udev->bus->methods->device_suspend) (udev);
2856 temp = usbd_get_dma_delay(udev);
2858 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2862 if (usb_device_20_compatible(udev)) {
2863 /* suspend current port */
2864 err = usbd_req_set_port_feature(udev->parent_hub,
2865 NULL, udev->port_no, UHF_PORT_SUSPEND);
2867 DPRINTFN(0, "Suspending port failed\n");
2871 /* suspend current port */
2872 err = usbd_req_set_port_link_state(udev->parent_hub,
2873 NULL, udev->port_no, UPS_PORT_LS_U3);
2875 DPRINTFN(0, "Suspending port failed\n");
2880 udev = udev->parent_hub;
2884 /*------------------------------------------------------------------------*
2885 * usbd_set_power_mode
2887 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2889 *------------------------------------------------------------------------*/
2891 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2893 /* filter input argument */
2894 if ((power_mode != USB_POWER_MODE_ON) &&
2895 (power_mode != USB_POWER_MODE_OFF))
2896 power_mode = USB_POWER_MODE_SAVE;
2898 power_mode = usbd_filter_power_mode(udev, power_mode);
2900 udev->power_mode = power_mode; /* update copy of power mode */
2903 usb_bus_power_update(udev->bus);
2905 usb_needs_explore(udev->bus, 0 /* no probe */ );
2909 /*------------------------------------------------------------------------*
2910 * usbd_filter_power_mode
2912 * This function filters the power mode based on hardware requirements.
2913 *------------------------------------------------------------------------*/
2915 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2917 struct usb_bus_methods *mtod;
2920 mtod = udev->bus->methods;
2923 if (mtod->get_power_mode != NULL)
2924 (mtod->get_power_mode) (udev, &temp);
2926 /* check if we should not filter */
2928 return (power_mode);
2930 /* use fixed power mode given by hardware driver */
2934 /*------------------------------------------------------------------------*
2935 * usbd_start_re_enumerate
2937 * This function starts re-enumeration of the given USB device. This
2938 * function does not need to be called BUS-locked. This function does
2939 * not wait until the re-enumeration is completed.
2940 *------------------------------------------------------------------------*/
2942 usbd_start_re_enumerate(struct usb_device *udev)
2944 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2945 udev->re_enumerate_wait = USB_RE_ENUM_START;
2946 usb_needs_explore(udev->bus, 0);
2950 /*-----------------------------------------------------------------------*
2951 * usbd_start_set_config
2953 * This function starts setting a USB configuration. This function
2954 * does not need to be called BUS-locked. This function does not wait
2955 * until the set USB configuratino is completed.
2956 *------------------------------------------------------------------------*/
2958 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2960 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2961 if (udev->curr_config_index == index) {
2962 /* no change needed */
2965 udev->next_config_index = index;
2966 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2967 usb_needs_explore(udev->bus, 0);
2969 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2970 if (udev->next_config_index == index) {
2971 /* no change needed */
2975 return (USB_ERR_PENDING_REQUESTS);