3 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
35 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
61 #define USB_DEBUG_VAR uhub_debug
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_device.h>
66 #include <dev/usb/usb_request.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_hub.h>
69 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_transfer.h>
72 #include <dev/usb/usb_dynamic.h>
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #endif /* USB_GLOBAL_INCLUDE_FILE */
78 #define UHUB_INTR_INTERVAL 250 /* ms */
81 #if USB_HAVE_TT_SUPPORT
82 UHUB_RESET_TT_TRANSFER,
88 static int uhub_debug = 0;
90 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
91 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
96 static int usb_power_timeout = 30; /* seconds */
98 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
99 &usb_power_timeout, 0, "USB power timeout");
102 #if USB_HAVE_DISABLE_ENUM
103 static int usb_disable_enumeration = 0;
104 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
105 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
106 "This can secure against USB devices turning evil, "
107 "for example a USB memory stick becoming a USB keyboard.");
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.");
114 struct uhub_current_state {
115 uint16_t port_change;
116 uint16_t port_status;
120 struct uhub_current_state sc_st;/* current state */
121 #if (USB_HAVE_FIXED_PORT != 0)
122 struct usb_hub sc_hub;
124 device_t sc_dev; /* base device */
125 struct mtx sc_mtx; /* our mutex */
126 struct usb_device *sc_udev; /* USB device */
127 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
128 #if USB_HAVE_DISABLE_ENUM
129 int sc_disable_enumeration;
130 int sc_disable_port_power;
132 uint8_t sc_usb_port_errors; /* error counter */
133 #define UHUB_USB_PORT_ERRORS_MAX 4
135 #define UHUB_FLAG_DID_EXPLORE 0x01
138 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
139 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
140 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
141 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
142 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
144 /* prototypes for type checking: */
146 static device_probe_t uhub_probe;
147 static device_attach_t uhub_attach;
148 static device_detach_t uhub_detach;
149 static device_suspend_t uhub_suspend;
150 static device_resume_t uhub_resume;
152 static bus_driver_added_t uhub_driver_added;
153 static bus_child_location_str_t uhub_child_location_string;
154 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
156 static usb_callback_t uhub_intr_callback;
157 #if USB_HAVE_TT_SUPPORT
158 static usb_callback_t uhub_reset_tt_callback;
161 static void usb_dev_resume_peer(struct usb_device *udev);
162 static void usb_dev_suspend_peer(struct usb_device *udev);
163 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
165 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
167 [UHUB_INTR_TRANSFER] = {
168 .type = UE_INTERRUPT,
169 .endpoint = UE_ADDR_ANY,
170 .direction = UE_DIR_ANY,
172 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
173 .bufsize = 0, /* use wMaxPacketSize */
174 .callback = &uhub_intr_callback,
175 .interval = UHUB_INTR_INTERVAL,
177 #if USB_HAVE_TT_SUPPORT
178 [UHUB_RESET_TT_TRANSFER] = {
180 .endpoint = 0x00, /* Control pipe */
181 .direction = UE_DIR_ANY,
182 .bufsize = sizeof(struct usb_device_request),
183 .callback = &uhub_reset_tt_callback,
184 .timeout = 1000, /* 1 second */
185 .usb_mode = USB_MODE_HOST,
191 * driver instance for "hub" connected to "usb"
192 * and "hub" connected to "hub"
194 static devclass_t uhub_devclass;
196 static device_method_t uhub_methods[] = {
197 DEVMETHOD(device_probe, uhub_probe),
198 DEVMETHOD(device_attach, uhub_attach),
199 DEVMETHOD(device_detach, uhub_detach),
201 DEVMETHOD(device_suspend, uhub_suspend),
202 DEVMETHOD(device_resume, uhub_resume),
204 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
205 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
206 DEVMETHOD(bus_driver_added, uhub_driver_added),
210 static driver_t uhub_driver = {
212 .methods = uhub_methods,
213 .size = sizeof(struct uhub_softc)
216 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
217 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
218 MODULE_VERSION(uhub, 1);
221 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
223 struct uhub_softc *sc = usbd_xfer_softc(xfer);
225 switch (USB_GET_STATE(xfer)) {
226 case USB_ST_TRANSFERRED:
229 * This is an indication that some port
230 * has changed status. Notify the bus
231 * event handler thread that we need
232 * to be explored again:
234 usb_needs_explore(sc->sc_udev->bus, 0);
237 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
238 usbd_transfer_submit(xfer);
242 if (xfer->error != USB_ERR_CANCELLED) {
244 * Do a clear-stall. The "stall_pipe" flag
245 * will get cleared before next callback by
248 usbd_xfer_set_stall(xfer);
249 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
250 usbd_transfer_submit(xfer);
256 /*------------------------------------------------------------------------*
259 * This function starts the TT reset USB request
260 *------------------------------------------------------------------------*/
261 #if USB_HAVE_TT_SUPPORT
263 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
265 struct usb_udev_msg *pm = (void *)_pm;
266 struct usb_device *udev = pm->udev;
268 struct uhub_softc *sc;
278 USB_BUS_UNLOCK(udev->bus);
279 USB_MTX_LOCK(&sc->sc_mtx);
281 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
283 USB_MTX_UNLOCK(&sc->sc_mtx);
284 USB_BUS_LOCK(udev->bus);
288 /*------------------------------------------------------------------------*
289 * uhub_tt_buffer_reset_async_locked
291 * This function queues a TT reset for the given USB device and endpoint.
292 *------------------------------------------------------------------------*/
293 #if USB_HAVE_TT_SUPPORT
295 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
297 struct usb_device_request req;
298 struct usb_device *udev;
304 if (child == NULL || ep == NULL)
307 udev = child->parent_hs_hub;
308 port = child->hs_port_no;
315 (udev->speed != USB_SPEED_HIGH) ||
316 (child->speed != USB_SPEED_LOW &&
317 child->speed != USB_SPEED_FULL) ||
318 (child->flags.usb_mode != USB_MODE_HOST) ||
319 (port == 0) || (ep->edesc == NULL)) {
324 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
326 up = hub->ports + port - 1;
328 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
329 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
332 /* if we already received a clear buffer request, reset the whole TT */
333 if (up->req_reset_tt.bRequest != 0) {
334 req.bmRequestType = UT_WRITE_CLASS_OTHER;
335 req.bRequest = UR_RESET_TT;
336 USETW(req.wValue, 0);
337 req.wIndex[0] = port;
339 USETW(req.wLength, 0);
341 wValue = (ep->edesc->bEndpointAddress & 0xF) |
342 ((child->address & 0x7F) << 4) |
343 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
344 ((ep->edesc->bmAttributes & 3) << 12);
346 req.bmRequestType = UT_WRITE_CLASS_OTHER;
347 req.bRequest = UR_CLEAR_TT_BUFFER;
348 USETW(req.wValue, wValue);
349 req.wIndex[0] = port;
351 USETW(req.wLength, 0);
353 up->req_reset_tt = req;
354 /* get reset transfer started */
355 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
356 &hub->tt_msg[0], &hub->tt_msg[1]);
360 #if USB_HAVE_TT_SUPPORT
362 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
364 struct uhub_softc *sc;
365 struct usb_device *udev;
369 DPRINTF("TT buffer reset\n");
371 sc = usbd_xfer_softc(xfer);
374 switch (USB_GET_STATE(xfer)) {
375 case USB_ST_TRANSFERRED:
378 USB_BUS_LOCK(udev->bus);
379 /* find first port which needs a TT reset */
380 for (x = 0; x != udev->hub->nports; x++) {
381 up = udev->hub->ports + x;
383 if (up->req_reset_tt.bRequest == 0)
386 /* copy in the transfer */
387 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
388 sizeof(up->req_reset_tt));
390 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
393 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
395 USB_BUS_UNLOCK(udev->bus);
397 usbd_transfer_submit(xfer);
400 USB_BUS_UNLOCK(udev->bus);
404 if (error == USB_ERR_CANCELLED)
407 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
413 /*------------------------------------------------------------------------*
414 * uhub_count_active_host_ports
416 * This function counts the number of active ports at the given speed.
417 *------------------------------------------------------------------------*/
419 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
421 struct uhub_softc *sc;
422 struct usb_device *child;
437 for (x = 0; x != hub->nports; x++) {
439 child = usb_bus_port_get_device(udev->bus, up);
441 child->flags.usb_mode == USB_MODE_HOST &&
442 child->speed == speed)
450 uhub_explore_handle_re_enumerate(struct usb_device *child)
455 /* check if device should be re-enumerated */
456 if (child->flags.usb_mode != USB_MODE_HOST)
459 do_unlock = usbd_enum_lock(child);
460 switch (child->re_enumerate_wait) {
461 case USB_RE_ENUM_START:
462 err = usbd_set_config_index(child,
465 DPRINTF("Unconfigure failed: %s: Ignored.\n",
468 if (child->parent_hub == NULL) {
469 /* the root HUB cannot be re-enumerated */
470 DPRINTFN(6, "cannot reset root HUB\n");
473 err = usbd_req_re_enumerate(child, NULL);
476 err = usbd_set_config_index(child, 0);
478 err = usb_probe_and_attach(child,
479 USB_IFACE_INDEX_ANY);
481 child->re_enumerate_wait = USB_RE_ENUM_DONE;
484 case USB_RE_ENUM_PWR_OFF:
485 /* get the device unconfigured */
486 err = usbd_set_config_index(child,
489 DPRINTFN(0, "Could not unconfigure "
490 "device (ignored)\n");
492 if (child->parent_hub == NULL) {
493 /* the root HUB cannot be re-enumerated */
494 DPRINTFN(6, "cannot set port feature\n");
497 /* clear port enable */
498 err = usbd_req_clear_port_feature(child->parent_hub,
499 NULL, child->port_no, UHF_PORT_ENABLE);
501 DPRINTFN(0, "Could not disable port "
505 child->re_enumerate_wait = USB_RE_ENUM_DONE;
508 case USB_RE_ENUM_SET_CONFIG:
509 err = usbd_set_config_index(child,
510 child->next_config_index);
512 DPRINTF("Configure failed: %s: Ignored.\n",
515 err = usb_probe_and_attach(child,
516 USB_IFACE_INDEX_ANY);
518 child->re_enumerate_wait = USB_RE_ENUM_DONE;
522 child->re_enumerate_wait = USB_RE_ENUM_DONE;
526 usbd_enum_unlock(child);
529 /*------------------------------------------------------------------------*
530 * uhub_explore_sub - subroutine
534 * Else: A control transaction failed
535 *------------------------------------------------------------------------*/
537 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
540 struct usb_device *child;
544 bus = sc->sc_udev->bus;
547 /* get driver added refcount from USB bus */
548 refcount = bus->driver_added_refcount;
550 /* get device assosiated with the given port */
551 child = usb_bus_port_get_device(bus, up);
557 uhub_explore_handle_re_enumerate(child);
559 /* check if probe and attach should be done */
561 if (child->driver_added_refcount != refcount) {
562 child->driver_added_refcount = refcount;
563 err = usb_probe_and_attach(child,
564 USB_IFACE_INDEX_ANY);
569 /* start control transfer, if device mode */
571 if (child->flags.usb_mode == USB_MODE_DEVICE)
572 usbd_ctrl_transfer_setup(child);
574 /* if a HUB becomes present, do a recursive HUB explore */
577 err = (child->hub->explore) (child);
583 /*------------------------------------------------------------------------*
584 * uhub_read_port_status - factored out code
585 *------------------------------------------------------------------------*/
587 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
589 struct usb_port_status ps;
592 if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
593 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
594 sc->sc_st.port_status = 0;
595 sc->sc_st.port_change = 0;
596 return (USB_ERR_TIMEOUT);
599 err = usbd_req_get_port_status(
600 sc->sc_udev, NULL, &ps, portno);
603 sc->sc_st.port_status = UGETW(ps.wPortStatus);
604 sc->sc_st.port_change = UGETW(ps.wPortChange);
605 sc->sc_usb_port_errors = 0;
607 sc->sc_st.port_status = 0;
608 sc->sc_st.port_change = 0;
609 sc->sc_usb_port_errors++;
612 /* debugging print */
614 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
615 "wPortChange=0x%04x, err=%s\n",
616 portno, sc->sc_st.port_status,
617 sc->sc_st.port_change, usbd_errstr(err));
621 /*------------------------------------------------------------------------*
626 * Else: A control transaction failed
627 *------------------------------------------------------------------------*/
629 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
631 struct usb_device *child;
632 struct usb_device *udev;
633 enum usb_dev_speed speed;
634 enum usb_hc_mode mode;
639 DPRINTF("reattaching port %d\n", portno);
643 child = usb_bus_port_get_device(udev->bus,
644 udev->hub->ports + portno - 1);
648 /* first clear the port connection change bit */
650 err = usbd_req_clear_port_feature(udev, NULL,
651 portno, UHF_C_PORT_CONNECTION);
656 /* check if there is a child */
660 * Free USB device and all subdevices, if any.
662 usb_free_device(child, 0);
665 /* get fresh status */
667 err = uhub_read_port_status(sc, portno);
671 #if USB_HAVE_DISABLE_ENUM
672 /* check if we should skip enumeration from this USB HUB */
673 if (usb_disable_enumeration != 0 ||
674 sc->sc_disable_enumeration != 0) {
675 DPRINTF("Enumeration is disabled!\n");
679 /* check if nothing is connected to the port */
681 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
684 /* check if there is no power on the port and print a warning */
686 switch (udev->speed) {
690 power_mask = UPS_PORT_POWER;
692 case USB_SPEED_SUPER:
693 if (udev->parent_hub == NULL)
694 power_mask = UPS_PORT_POWER;
696 power_mask = UPS_PORT_POWER_SS;
702 if (!(sc->sc_st.port_status & power_mask)) {
703 DPRINTF("WARNING: strange, connected port %d "
704 "has no power\n", portno);
707 /* check if the device is in Host Mode */
709 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
711 DPRINTF("Port %d is in Host Mode\n", portno);
713 if (sc->sc_st.port_status & UPS_SUSPEND) {
715 * NOTE: Should not get here in SuperSpeed
716 * mode, because the HUB should report this
719 DPRINTF("Port %d was still "
720 "suspended, clearing.\n", portno);
721 err = usbd_req_clear_port_feature(udev,
722 NULL, portno, UHF_PORT_SUSPEND);
727 /* wait for maximum device power up time */
730 USB_MS_TO_TICKS(usb_port_powerup_delay));
732 /* reset port, which implies enabling it */
734 err = usbd_req_reset_port(udev, NULL, portno);
737 DPRINTFN(0, "port %d reset "
738 "failed, error=%s\n",
739 portno, usbd_errstr(err));
742 /* get port status again, it might have changed during reset */
744 err = uhub_read_port_status(sc, portno);
748 /* check if something changed during port reset */
750 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
751 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
753 DPRINTFN(0, "giving up port reset "
754 "- device vanished\n");
761 DPRINTF("Port %d is in Device Mode\n", portno);
765 * Figure out the device speed
767 switch (udev->speed) {
769 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
770 speed = USB_SPEED_HIGH;
771 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
772 speed = USB_SPEED_LOW;
774 speed = USB_SPEED_FULL;
777 if (sc->sc_st.port_status & UPS_LOW_SPEED)
778 speed = USB_SPEED_LOW;
780 speed = USB_SPEED_FULL;
783 speed = USB_SPEED_LOW;
785 case USB_SPEED_SUPER:
786 if (udev->parent_hub == NULL) {
787 /* Root HUB - special case */
788 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
790 speed = USB_SPEED_FULL;
793 speed = USB_SPEED_LOW;
796 speed = USB_SPEED_HIGH;
799 speed = USB_SPEED_SUPER;
803 speed = USB_SPEED_SUPER;
807 /* same speed like parent */
811 if (speed == USB_SPEED_SUPER) {
812 err = usbd_req_set_hub_u1_timeout(udev, NULL,
813 portno, 128 - (2 * udev->depth));
815 DPRINTFN(0, "port %d U1 timeout "
816 "failed, error=%s\n",
817 portno, usbd_errstr(err));
819 err = usbd_req_set_hub_u2_timeout(udev, NULL,
820 portno, 128 - (2 * udev->depth));
822 DPRINTFN(0, "port %d U2 timeout "
823 "failed, error=%s\n",
824 portno, usbd_errstr(err));
829 * Figure out the device mode
831 * NOTE: This part is currently FreeBSD specific.
833 if (udev->parent_hub != NULL) {
834 /* inherit mode from the parent HUB */
835 mode = udev->parent_hub->flags.usb_mode;
836 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
837 mode = USB_MODE_DEVICE;
839 mode = USB_MODE_HOST;
841 /* need to create a new child */
842 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
843 udev->depth + 1, portno - 1, portno, speed, mode);
845 DPRINTFN(0, "could not allocate new device\n");
848 return (0); /* success */
853 * Free USB device and all subdevices, if any.
855 usb_free_device(child, 0);
859 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
860 err = usbd_req_clear_port_feature(
862 portno, UHF_PORT_ENABLE);
866 DPRINTFN(0, "device problem (%s), "
867 "disabling port %d\n", usbd_errstr(err), portno);
872 /*------------------------------------------------------------------------*
873 * usb_device_20_compatible
876 * 0: HUB does not support suspend and resume
877 * Else: HUB supports suspend and resume
878 *------------------------------------------------------------------------*/
880 usb_device_20_compatible(struct usb_device *udev)
884 switch (udev->speed) {
894 /*------------------------------------------------------------------------*
895 * uhub_suspend_resume_port
899 * Else: A control transaction failed
900 *------------------------------------------------------------------------*/
902 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
904 struct usb_device *child;
905 struct usb_device *udev;
909 DPRINTF("port %d\n", portno);
912 child = usb_bus_port_get_device(udev->bus,
913 udev->hub->ports + portno - 1);
915 /* first clear the port suspend change bit */
917 if (usb_device_20_compatible(udev)) {
918 err = usbd_req_clear_port_feature(udev, NULL,
919 portno, UHF_C_PORT_SUSPEND);
921 err = usbd_req_clear_port_feature(udev, NULL,
922 portno, UHF_C_PORT_LINK_STATE);
926 DPRINTF("clearing suspend failed.\n");
929 /* get fresh status */
931 err = uhub_read_port_status(sc, portno);
933 DPRINTF("reading port status failed.\n");
936 /* convert current state */
938 if (usb_device_20_compatible(udev)) {
939 if (sc->sc_st.port_status & UPS_SUSPEND) {
945 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
949 case UPS_PORT_LS_SS_INA:
950 usbd_req_warm_reset_port(udev, NULL, portno);
959 DPRINTF("suspended=%u\n", is_suspend);
961 /* do the suspend or resume */
965 * This code handle two cases: 1) Host Mode - we can only
966 * receive resume here 2) Device Mode - we can receive
967 * suspend and resume here
970 usb_dev_resume_peer(child);
971 else if (child->flags.usb_mode == USB_MODE_DEVICE)
972 usb_dev_suspend_peer(child);
978 /*------------------------------------------------------------------------*
979 * uhub_root_interrupt
981 * This function is called when a Root HUB interrupt has
982 * happened. "ptr" and "len" makes up the Root HUB interrupt
983 * packet. This function is called having the "bus_mtx" locked.
984 *------------------------------------------------------------------------*/
986 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
988 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
990 usb_needs_explore(bus, 0);
994 uhub_is_too_deep(struct usb_device *udev)
996 switch (udev->speed) {
1000 if (udev->depth > USB_HUB_MAX_DEPTH)
1003 case USB_SPEED_SUPER:
1004 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
1013 /*------------------------------------------------------------------------*
1019 *------------------------------------------------------------------------*/
1021 uhub_explore(struct usb_device *udev)
1023 struct usb_hub *hub;
1024 struct uhub_softc *sc;
1025 struct usb_port *up;
1034 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1036 /* ignore devices that are too deep */
1037 if (uhub_is_too_deep(udev))
1038 return (USB_ERR_TOO_DEEP);
1040 /* check if device is suspended */
1041 if (udev->flags.self_suspended) {
1042 /* need to wait until the child signals resume */
1043 DPRINTF("Device is suspended!\n");
1048 * Make sure we don't race against user-space applications
1051 do_unlock = usbd_enum_lock(udev);
1053 for (x = 0; x != hub->nports; x++) {
1054 up = hub->ports + x;
1057 err = uhub_read_port_status(sc, portno);
1059 /* most likely the HUB is gone */
1062 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1063 DPRINTF("Overcurrent on port %u.\n", portno);
1064 err = usbd_req_clear_port_feature(
1065 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1067 /* most likely the HUB is gone */
1071 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1073 * Fake a connect status change so that the
1074 * status gets checked initially!
1076 sc->sc_st.port_change |=
1077 UPS_C_CONNECT_STATUS;
1079 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1080 err = usbd_req_clear_port_feature(
1081 udev, NULL, portno, UHF_C_PORT_ENABLE);
1083 /* most likely the HUB is gone */
1086 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1088 * Ignore the port error if the device
1091 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1092 DPRINTFN(0, "illegal enable change, "
1093 "port %d\n", portno);
1096 if (up->restartcnt == USB_RESTART_MAX) {
1097 /* XXX could try another speed ? */
1098 DPRINTFN(0, "port error, giving up "
1099 "port %d\n", portno);
1101 sc->sc_st.port_change |=
1102 UPS_C_CONNECT_STATUS;
1107 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1108 err = uhub_reattach_port(sc, portno);
1110 /* most likely the HUB is gone */
1114 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1115 UPS_C_PORT_LINK_STATE)) {
1116 err = uhub_suspend_resume_port(sc, portno);
1118 /* most likely the HUB is gone */
1122 err = uhub_explore_sub(sc, up);
1124 /* no device(s) present */
1127 /* explore succeeded - reset restart counter */
1132 usbd_enum_unlock(udev);
1134 /* initial status checked */
1135 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1137 /* return success */
1138 return (USB_ERR_NORMAL_COMPLETION);
1142 uhub_probe(device_t dev)
1144 struct usb_attach_arg *uaa = device_get_ivars(dev);
1146 if (uaa->usb_mode != USB_MODE_HOST)
1150 * The subclass for USB HUBs is currently ignored because it
1151 * is 0 for some and 1 for others.
1153 if (uaa->info.bConfigIndex == 0 &&
1154 uaa->info.bDeviceClass == UDCLASS_HUB)
1160 /* NOTE: The information returned by this function can be wrong. */
1162 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1164 struct usb_hub_descriptor hubdesc20;
1165 struct usb_hub_ss_descriptor hubdesc30;
1170 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1171 return (USB_ERR_INVAL);
1176 switch (udev->speed) {
1178 case USB_SPEED_FULL:
1179 case USB_SPEED_HIGH:
1180 /* assuming that there is one port */
1181 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1183 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1184 "error=%s\n", usbd_errstr(err));
1187 nports = hubdesc20.bNbrPorts;
1191 if (udev->speed == USB_SPEED_HIGH)
1192 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1195 case USB_SPEED_SUPER:
1196 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1198 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1199 "error=%s\n", usbd_errstr(err));
1202 nports = hubdesc30.bNbrPorts;
1208 err = USB_ERR_INVAL;
1212 if (pnports != NULL)
1222 uhub_attach(device_t dev)
1224 struct uhub_softc *sc = device_get_softc(dev);
1225 struct usb_attach_arg *uaa = device_get_ivars(dev);
1226 struct usb_device *udev = uaa->device;
1227 struct usb_device *parent_hub = udev->parent_hub;
1228 struct usb_hub *hub;
1229 struct usb_hub_descriptor hubdesc20;
1230 struct usb_hub_ss_descriptor hubdesc30;
1231 #if USB_HAVE_DISABLE_ENUM
1232 struct sysctl_ctx_list *sysctl_ctx;
1233 struct sysctl_oid *sysctl_tree;
1240 uint8_t iface_index;
1246 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1248 device_set_usb_desc(dev);
1250 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1251 "parent->selfpowered=%d\n",
1253 udev->flags.self_powered,
1256 parent_hub->flags.self_powered : 0);
1258 if (uhub_is_too_deep(udev)) {
1259 DPRINTFN(0, "HUB at depth %d, "
1260 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1264 if (!udev->flags.self_powered && parent_hub &&
1265 !parent_hub->flags.self_powered) {
1266 DPRINTFN(0, "Bus powered HUB connected to "
1267 "bus powered HUB. HUB ignored\n");
1271 if (UHUB_IS_MULTI_TT(sc)) {
1272 err = usbd_set_alt_interface_index(udev, 0, 1);
1274 device_printf(dev, "MTT could not be enabled\n");
1277 device_printf(dev, "MTT enabled\n");
1280 /* get HUB descriptor */
1282 DPRINTFN(2, "Getting HUB descriptor\n");
1284 switch (udev->speed) {
1286 case USB_SPEED_FULL:
1287 case USB_SPEED_HIGH:
1288 /* assuming that there is one port */
1289 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1291 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1292 "error=%s\n", usbd_errstr(err));
1295 /* get number of ports */
1296 nports = hubdesc20.bNbrPorts;
1298 /* get power delay */
1299 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1300 usb_extra_power_up_time);
1302 /* get complete HUB descriptor */
1304 /* check number of ports */
1306 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1307 "error=%s\n", usbd_errstr(err));
1310 /* get complete HUB descriptor */
1311 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1314 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1315 "error=%s\n", usbd_errstr(err));
1318 if (hubdesc20.bNbrPorts != nports) {
1319 DPRINTFN(0, "Number of ports changed\n");
1324 case USB_SPEED_SUPER:
1325 if (udev->parent_hub != NULL) {
1326 err = usbd_req_set_hub_depth(udev, NULL,
1329 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1330 "error=%s\n", usbd_errstr(err));
1334 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1336 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1337 "error=%s\n", usbd_errstr(err));
1340 /* get number of ports */
1341 nports = hubdesc30.bNbrPorts;
1343 /* get power delay */
1344 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1345 usb_extra_power_up_time);
1347 /* get complete HUB descriptor */
1349 /* check number of ports */
1350 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1351 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1352 "error=%s\n", usbd_errstr(err));
1355 /* get complete HUB descriptor */
1356 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1359 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1360 "error=%s\n", usbd_errstr(err));
1363 if (hubdesc30.bNbrPorts != nports) {
1364 DPRINTFN(0, "Number of ports changed\n");
1370 DPRINTF("Assuming HUB has only one port\n");
1371 /* default number of ports */
1373 /* default power delay */
1374 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1378 DPRINTFN(0, "portless HUB\n");
1381 if (nports > USB_MAX_PORTS) {
1382 DPRINTF("Port limit exceeded\n");
1385 #if (USB_HAVE_FIXED_PORT == 0)
1386 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1387 M_USBDEV, M_WAITOK | M_ZERO);
1396 /* initialize HUB structure */
1398 hub->explore = &uhub_explore;
1399 hub->nports = nports;
1400 hub->hubudev = udev;
1401 #if USB_HAVE_TT_SUPPORT
1402 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1403 hub->tt_msg[0].udev = udev;
1404 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1405 hub->tt_msg[1].udev = udev;
1407 /* if self powered hub, give ports maximum current */
1408 if (udev->flags.self_powered) {
1409 hub->portpower = USB_MAX_POWER;
1411 hub->portpower = USB_MIN_POWER;
1414 /* set up interrupt pipe */
1416 if (udev->parent_hub == NULL) {
1417 /* root HUB is special */
1421 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1422 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1425 DPRINTFN(0, "cannot setup interrupt transfer, "
1426 "errstr=%s\n", usbd_errstr(err));
1429 /* wait with power off for a while */
1430 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1432 #if USB_HAVE_DISABLE_ENUM
1433 /* Add device sysctls */
1435 sysctl_ctx = device_get_sysctl_ctx(dev);
1436 sysctl_tree = device_get_sysctl_tree(dev);
1438 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1439 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1440 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1441 &sc->sc_disable_enumeration, 0,
1442 "Set to disable enumeration on this USB HUB.");
1444 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1445 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1446 &sc->sc_disable_port_power, 0,
1447 "Set to disable USB port power on this USB HUB.");
1451 * To have the best chance of success we do things in the exact same
1452 * order as Windoze98. This should not be necessary, but some
1453 * devices do not follow the USB specs to the letter.
1455 * These are the events on the bus when a hub is attached:
1456 * Get device and config descriptors (see attach code)
1457 * Get hub descriptor (see above)
1460 * wait for power to become stable
1461 * (all below happens in explore code)
1463 * clear C_PORT_CONNECTION
1466 * if device connected
1470 * clear C_PORT_RESET
1472 * proceed with device attachment
1475 /* XXX should check for none, individual, or ganged power? */
1479 for (x = 0; x != nports; x++) {
1480 /* set up data structures */
1481 struct usb_port *up = hub->ports + x;
1483 up->device_index = 0;
1487 /* check if port is removable */
1488 switch (udev->speed) {
1490 case USB_SPEED_FULL:
1491 case USB_SPEED_HIGH:
1492 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1495 case USB_SPEED_SUPER:
1496 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1500 DPRINTF("Assuming removable port\n");
1505 #if USB_HAVE_DISABLE_ENUM
1506 /* check if we should disable USB port power or not */
1507 if (usb_disable_port_power != 0 ||
1508 sc->sc_disable_port_power != 0) {
1509 /* turn the power off */
1510 DPRINTFN(2, "Turning port %d power off\n", portno);
1511 err = usbd_req_clear_port_feature(udev, NULL,
1512 portno, UHF_PORT_POWER);
1515 /* turn the power on */
1516 DPRINTFN(2, "Turning port %d power on\n", portno);
1517 err = usbd_req_set_port_feature(udev, NULL,
1518 portno, UHF_PORT_POWER);
1519 #if USB_HAVE_DISABLE_ENUM
1524 DPRINTFN(0, "port %d power on or off failed, %s\n",
1525 portno, usbd_errstr(err));
1527 DPRINTF("turn on port %d power\n",
1530 /* wait for stable power */
1531 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1534 device_printf(dev, "%d port%s with %d "
1535 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1536 removable, udev->flags.self_powered ? "self" : "bus");
1538 /* Start the interrupt endpoint, if any */
1540 USB_MTX_LOCK(&sc->sc_mtx);
1541 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1542 USB_MTX_UNLOCK(&sc->sc_mtx);
1544 /* Enable automatic power save on all USB HUBs */
1546 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1551 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1553 #if (USB_HAVE_FIXED_PORT == 0)
1554 free(udev->hub, M_USBDEV);
1558 mtx_destroy(&sc->sc_mtx);
1564 * Called from process context when the hub is gone.
1565 * Detach all devices on active ports.
1568 uhub_detach(device_t dev)
1570 struct uhub_softc *sc = device_get_softc(dev);
1571 struct usb_hub *hub = sc->sc_udev->hub;
1572 struct usb_bus *bus = sc->sc_udev->bus;
1573 struct usb_device *child;
1576 if (hub == NULL) /* must be partially working */
1579 /* Make sure interrupt transfer is gone. */
1580 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1582 /* Detach all ports */
1583 for (x = 0; x != hub->nports; x++) {
1585 child = usb_bus_port_get_device(bus, hub->ports + x);
1587 if (child == NULL) {
1592 * Free USB device and all subdevices, if any.
1594 usb_free_device(child, 0);
1597 #if USB_HAVE_TT_SUPPORT
1598 /* Make sure our TT messages are not queued anywhere */
1600 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1601 &hub->tt_msg[0], &hub->tt_msg[1]);
1602 USB_BUS_UNLOCK(bus);
1605 #if (USB_HAVE_FIXED_PORT == 0)
1606 free(hub, M_USBDEV);
1608 sc->sc_udev->hub = NULL;
1610 mtx_destroy(&sc->sc_mtx);
1616 uhub_suspend(device_t dev)
1619 /* Sub-devices are not suspended here! */
1624 uhub_resume(device_t dev)
1627 /* Sub-devices are not resumed here! */
1632 uhub_driver_added(device_t dev, driver_t *driver)
1634 usb_needs_explore_all();
1638 struct usb_device *udev;
1640 uint8_t iface_index;
1644 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1645 struct hub_result *res)
1647 struct usb_interface *iface;
1648 struct usb_device *udev;
1653 nports = hub->nports;
1654 for (x = 0; x != nports; x++) {
1655 udev = usb_bus_port_get_device(hub->hubudev->bus,
1660 for (i = 0; i != USB_IFACE_MAX; i++) {
1661 iface = usbd_get_iface(udev, i);
1663 (iface->subdev == child)) {
1664 res->iface_index = i;
1666 res->portno = x + 1;
1671 res->iface_index = 0;
1677 uhub_child_location_string(device_t parent, device_t child,
1678 char *buf, size_t buflen)
1680 struct uhub_softc *sc;
1681 struct usb_hub *hub;
1682 struct hub_result res;
1684 if (!device_is_attached(parent)) {
1690 sc = device_get_softc(parent);
1691 hub = sc->sc_udev->hub;
1694 uhub_find_iface_index(hub, child, &res);
1696 DPRINTF("device not on hub\n");
1702 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1707 , device_get_unit(res.udev->bus->bdev)
1708 , (res.udev->parent_hub != NULL) ?
1709 res.udev->parent_hub->device_index : 0
1710 , res.portno, res.udev->device_index, res.iface_index
1712 , res.udev->ugen_name
1722 uhub_child_pnpinfo_string(device_t parent, device_t child,
1723 char *buf, size_t buflen)
1725 struct uhub_softc *sc;
1726 struct usb_hub *hub;
1727 struct usb_interface *iface;
1728 struct hub_result res;
1730 if (!device_is_attached(parent)) {
1736 sc = device_get_softc(parent);
1737 hub = sc->sc_udev->hub;
1740 uhub_find_iface_index(hub, child, &res);
1742 DPRINTF("device not on hub\n");
1748 iface = usbd_get_iface(res.udev, res.iface_index);
1749 if (iface && iface->idesc) {
1750 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1751 "devclass=0x%02x devsubclass=0x%02x "
1756 "intclass=0x%02x intsubclass=0x%02x "
1757 "intprotocol=0x%02x" "%s%s",
1758 UGETW(res.udev->ddesc.idVendor),
1759 UGETW(res.udev->ddesc.idProduct),
1760 res.udev->ddesc.bDeviceClass,
1761 res.udev->ddesc.bDeviceSubClass,
1762 res.udev->ddesc.bDeviceProtocol,
1763 usb_get_serial(res.udev),
1764 UGETW(res.udev->ddesc.bcdDevice),
1765 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1766 iface->idesc->bInterfaceClass,
1767 iface->idesc->bInterfaceSubClass,
1768 iface->idesc->bInterfaceProtocol,
1769 iface->pnpinfo ? " " : "",
1770 iface->pnpinfo ? iface->pnpinfo : "");
1784 * The USB Transaction Translator:
1785 * ===============================
1787 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1788 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1789 * USB transfers. To utilize bandwidth dynamically the "scatter and
1790 * gather" principle must be applied. This means that bandwidth must
1791 * be divided into equal parts of bandwidth. With regard to USB all
1792 * data is transferred in smaller packets with length
1793 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1796 * The bandwidth scheduler which I have implemented will simply pack
1797 * the USB transfers back to back until there is no more space in the
1798 * schedule. Out of the 8 microframes which the USB 2.0 standard
1799 * provides, only 6 are available for non-HIGH-speed devices. I have
1800 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1801 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1802 * this division, it is very difficult to allocate and free bandwidth
1805 * NOTE about the Transaction Translator in USB HUBs:
1807 * USB HUBs have a very simple Transaction Translator, that will
1808 * simply pipeline all the SPLIT transactions. That means that the
1809 * transactions will be executed in the order they are queued!
1813 /*------------------------------------------------------------------------*
1814 * usb_intr_find_best_slot
1817 * The best Transaction Translation slot for an interrupt endpoint.
1818 *------------------------------------------------------------------------*/
1820 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1821 uint8_t end, uint8_t mask)
1823 usb_size_t min = (usb_size_t)-1;
1831 /* find the last slot with lesser used bandwidth */
1833 for (x = start; x < end; x++) {
1837 /* compute sum of bandwidth */
1838 for (z = x; z < end; z++) {
1839 if (mask & (1U << (z - x)))
1843 /* check if the current multi-slot is more optimal */
1849 /* check if the mask is about to be shifted out */
1850 if (mask & (1U << (end - 1 - x)))
1856 /*------------------------------------------------------------------------*
1857 * usb_hs_bandwidth_adjust
1859 * This function will update the bandwidth usage for the microframe
1860 * having index "slot" by "len" bytes. "len" can be negative. If the
1861 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1862 * the "slot" argument will be replaced by the slot having least used
1863 * bandwidth. The "mask" argument is used for multi-slot allocations.
1866 * The slot in which the bandwidth update was done: 0..7
1867 *------------------------------------------------------------------------*/
1869 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1870 uint8_t slot, uint8_t mask)
1872 struct usb_bus *bus = udev->bus;
1873 struct usb_hub *hub;
1874 enum usb_dev_speed speed;
1877 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1879 speed = usbd_get_speed(udev);
1883 case USB_SPEED_FULL:
1884 if (speed == USB_SPEED_LOW) {
1888 * The Host Controller Driver should have
1889 * performed checks so that the lookup
1890 * below does not result in a NULL pointer
1894 hub = udev->parent_hs_hub->hub;
1895 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1896 slot = usb_intr_find_best_slot(hub->uframe_usage,
1897 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1899 for (x = slot; x < 8; x++) {
1900 if (mask & (1U << (x - slot))) {
1901 hub->uframe_usage[x] += len;
1902 bus->uframe_usage[x] += len;
1907 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1908 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1909 USB_HS_MICRO_FRAMES_MAX, mask);
1911 for (x = slot; x < 8; x++) {
1912 if (mask & (1U << (x - slot))) {
1913 bus->uframe_usage[x] += len;
1921 /*------------------------------------------------------------------------*
1922 * usb_hs_bandwidth_alloc
1924 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1925 *------------------------------------------------------------------------*/
1927 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1929 struct usb_device *udev;
1934 udev = xfer->xroot->udev;
1936 if (udev->flags.usb_mode != USB_MODE_HOST)
1937 return; /* not supported */
1939 xfer->endpoint->refcount_bw++;
1940 if (xfer->endpoint->refcount_bw != 1)
1941 return; /* already allocated */
1943 speed = usbd_get_speed(udev);
1945 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1947 /* allocate a microframe slot */
1950 slot = usb_hs_bandwidth_adjust(udev,
1951 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1953 xfer->endpoint->usb_uframe = slot;
1954 xfer->endpoint->usb_smask = mask << slot;
1956 if ((speed != USB_SPEED_FULL) &&
1957 (speed != USB_SPEED_LOW)) {
1958 xfer->endpoint->usb_cmask = 0x00 ;
1960 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1964 case UE_ISOCHRONOUS:
1965 switch (usbd_xfer_get_fps_shift(xfer)) {
1980 /* allocate a microframe multi-slot */
1982 slot = usb_hs_bandwidth_adjust(udev,
1983 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1985 xfer->endpoint->usb_uframe = slot;
1986 xfer->endpoint->usb_cmask = 0;
1987 xfer->endpoint->usb_smask = mask << slot;
1991 xfer->endpoint->usb_uframe = 0;
1992 xfer->endpoint->usb_cmask = 0;
1993 xfer->endpoint->usb_smask = 0;
1997 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1998 xfer->endpoint->usb_uframe,
1999 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
2002 /*------------------------------------------------------------------------*
2003 * usb_hs_bandwidth_free
2005 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
2006 *------------------------------------------------------------------------*/
2008 usb_hs_bandwidth_free(struct usb_xfer *xfer)
2010 struct usb_device *udev;
2014 udev = xfer->xroot->udev;
2016 if (udev->flags.usb_mode != USB_MODE_HOST)
2017 return; /* not supported */
2019 xfer->endpoint->refcount_bw--;
2020 if (xfer->endpoint->refcount_bw != 0)
2021 return; /* still allocated */
2023 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2025 case UE_ISOCHRONOUS:
2027 slot = xfer->endpoint->usb_uframe;
2028 mask = xfer->endpoint->usb_smask;
2030 /* free microframe slot(s): */
2031 usb_hs_bandwidth_adjust(udev,
2032 -xfer->max_frame_size, slot, mask >> slot);
2034 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2035 slot, mask >> slot);
2037 xfer->endpoint->usb_uframe = 0;
2038 xfer->endpoint->usb_cmask = 0;
2039 xfer->endpoint->usb_smask = 0;
2047 /*------------------------------------------------------------------------*
2048 * usb_isoc_time_expand
2050 * This function will expand the time counter from 7-bit to 16-bit.
2053 * 16-bit isochronous time counter.
2054 *------------------------------------------------------------------------*/
2056 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2060 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2062 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2064 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2066 if (isoc_time_curr < rem) {
2067 /* the time counter wrapped around */
2068 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2070 /* update the remainder */
2072 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2073 bus->isoc_time_last |= isoc_time_curr;
2075 return (bus->isoc_time_last);
2078 /*------------------------------------------------------------------------*
2079 * usbd_fs_isoc_schedule_alloc_slot
2081 * This function will allocate bandwidth for an isochronous FULL speed
2082 * transaction in the FULL speed schedule.
2087 *------------------------------------------------------------------------*/
2088 #if USB_HAVE_TT_SUPPORT
2090 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2092 struct usb_xfer *xfer;
2093 struct usb_xfer *pipe_xfer;
2094 struct usb_bus *bus;
2096 usb_frlength_t data_len;
2104 bus = isoc_xfer->xroot->bus;
2106 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2108 /* skip self, if any */
2110 if (xfer == isoc_xfer)
2113 /* check if this USB transfer is going through the same TT */
2115 if (xfer->xroot->udev->parent_hs_hub !=
2116 isoc_xfer->xroot->udev->parent_hs_hub) {
2119 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2120 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2121 (xfer->xroot->udev->hs_port_no !=
2122 isoc_xfer->xroot->udev->hs_port_no)) {
2125 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2128 /* check if isoc_time is part of this transfer */
2130 delta = xfer->isoc_time_complete - isoc_time;
2131 if (delta > 0 && delta <= xfer->nframes) {
2132 delta = xfer->nframes - delta;
2134 len = xfer->frlengths[delta];
2143 * Check double buffered transfers. Only stream ID
2144 * equal to zero is valid here!
2146 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2149 /* skip self, if any */
2151 if (pipe_xfer == isoc_xfer)
2154 /* check if isoc_time is part of this transfer */
2156 delta = pipe_xfer->isoc_time_complete - isoc_time;
2157 if (delta > 0 && delta <= pipe_xfer->nframes) {
2158 delta = pipe_xfer->nframes - delta;
2160 len = pipe_xfer->frlengths[delta];
2170 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2171 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2175 /* check for overflow */
2177 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2182 delta = isoc_xfer->isoc_time_complete - isoc_time;
2183 if (delta > 0 && delta <= isoc_xfer->nframes) {
2184 delta = isoc_xfer->nframes - delta;
2186 len = isoc_xfer->frlengths[delta];
2194 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2195 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2199 /* check for overflow */
2201 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2208 /*------------------------------------------------------------------------*
2209 * usb_bus_port_get_device
2211 * This function is NULL safe.
2212 *------------------------------------------------------------------------*/
2214 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2216 if ((bus == NULL) || (up == NULL)) {
2220 if (up->device_index == 0) {
2224 return (bus->devices[up->device_index]);
2227 /*------------------------------------------------------------------------*
2228 * usb_bus_port_set_device
2230 * This function is NULL safe.
2231 *------------------------------------------------------------------------*/
2233 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2234 struct usb_device *udev, uint8_t device_index)
2241 * There is only one case where we don't
2242 * have an USB port, and that is the Root Hub!
2246 up->device_index = device_index;
2248 device_index = up->device_index;
2249 up->device_index = 0;
2253 * Make relationships to our new device
2255 if (device_index != 0) {
2257 mtx_lock(&usb_ref_lock);
2259 bus->devices[device_index] = udev;
2261 mtx_unlock(&usb_ref_lock);
2267 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2270 /*------------------------------------------------------------------------*
2273 * This functions is called when the USB event thread needs to run.
2274 *------------------------------------------------------------------------*/
2276 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2288 DPRINTF("No bus pointer!\n");
2291 if ((bus->devices == NULL) ||
2292 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2293 DPRINTF("No root HUB\n");
2296 if (mtx_owned(&bus->bus_mtx)) {
2305 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2306 &bus->explore_msg[0], &bus->explore_msg[1])) {
2310 USB_BUS_UNLOCK(bus);
2314 /*------------------------------------------------------------------------*
2315 * usb_needs_explore_all
2317 * This function is called whenever a new driver is loaded and will
2318 * cause that all USB buses are re-explored.
2319 *------------------------------------------------------------------------*/
2321 usb_needs_explore_all(void)
2323 struct usb_bus *bus;
2330 dc = usb_devclass_ptr;
2332 DPRINTFN(0, "no devclass\n");
2336 * Explore all USB buses in parallel.
2338 max = devclass_get_maxunit(dc);
2340 dev = devclass_get_device(dc, max);
2342 bus = device_get_softc(dev);
2344 usb_needs_explore(bus, 1);
2351 /*------------------------------------------------------------------------*
2352 * usb_needs_explore_init
2354 * This function will ensure that the USB controllers are not enumerated
2355 * until the "cold" variable is cleared.
2356 *------------------------------------------------------------------------*/
2358 usb_needs_explore_init(void *arg)
2361 * The cold variable should be cleared prior to this function
2365 usb_needs_explore_all();
2367 DPRINTFN(-1, "Cold variable is still set!\n");
2369 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2371 /*------------------------------------------------------------------------*
2372 * usb_bus_power_update
2374 * This function will ensure that all USB devices on the given bus are
2375 * properly suspended or resumed according to the device transfer
2377 *------------------------------------------------------------------------*/
2380 usb_bus_power_update(struct usb_bus *bus)
2382 usb_needs_explore(bus, 0 /* no probe */ );
2386 /*------------------------------------------------------------------------*
2387 * usbd_transfer_power_ref
2389 * This function will modify the power save reference counts and
2390 * wakeup the USB device associated with the given USB transfer, if
2392 *------------------------------------------------------------------------*/
2395 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2397 static const usb_power_mask_t power_mask[4] = {
2398 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2399 [UE_BULK] = USB_HW_POWER_BULK,
2400 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2401 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2403 struct usb_device *udev;
2404 uint8_t needs_explore;
2405 uint8_t needs_hw_power;
2408 udev = xfer->xroot->udev;
2410 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2411 /* no power save for root HUB */
2414 USB_BUS_LOCK(udev->bus);
2416 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2418 udev->pwr_save.last_xfer_time = ticks;
2419 udev->pwr_save.type_refs[xfer_type] += val;
2421 if (xfer->flags_int.control_xfr) {
2422 udev->pwr_save.read_refs += val;
2423 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2425 * It is not allowed to suspend during a
2428 udev->pwr_save.write_refs += val;
2430 } else if (USB_GET_DATA_ISREAD(xfer)) {
2431 udev->pwr_save.read_refs += val;
2433 udev->pwr_save.write_refs += val;
2437 if (udev->flags.self_suspended)
2438 needs_explore = usb_peer_should_wakeup(udev);
2442 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2443 DPRINTF("Adding type %u to power state\n", xfer_type);
2444 udev->bus->hw_power_state |= power_mask[xfer_type];
2454 USB_BUS_UNLOCK(udev->bus);
2456 if (needs_explore) {
2457 DPRINTF("update\n");
2458 usb_bus_power_update(udev->bus);
2459 } else if (needs_hw_power) {
2460 DPRINTF("needs power\n");
2461 if (udev->bus->methods->set_hw_power != NULL) {
2462 (udev->bus->methods->set_hw_power) (udev->bus);
2468 /*------------------------------------------------------------------------*
2469 * usb_peer_should_wakeup
2471 * This function returns non-zero if the current device should wake up.
2472 *------------------------------------------------------------------------*/
2474 usb_peer_should_wakeup(struct usb_device *udev)
2476 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2477 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2478 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2479 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2480 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2481 (udev->pwr_save.write_refs != 0) ||
2482 ((udev->pwr_save.read_refs != 0) &&
2483 (udev->flags.usb_mode == USB_MODE_HOST) &&
2484 (usb_peer_can_wakeup(udev) == 0)));
2487 /*------------------------------------------------------------------------*
2490 * This function implements the USB power daemon and is called
2491 * regularly from the USB explore thread.
2492 *------------------------------------------------------------------------*/
2495 usb_bus_powerd(struct usb_bus *bus)
2497 struct usb_device *udev;
2500 usb_ticks_t mintime;
2501 usb_size_t type_refs[5];
2504 limit = usb_power_timeout;
2507 else if (limit > 255)
2512 DPRINTF("bus=%p\n", bus);
2517 * The root HUB device is never suspended
2518 * and we simply skip it.
2520 for (x = USB_ROOT_HUB_ADDR + 1;
2521 x != bus->devices_max; x++) {
2523 udev = bus->devices[x];
2527 temp = ticks - udev->pwr_save.last_xfer_time;
2529 if (usb_peer_should_wakeup(udev)) {
2530 /* check if we are suspended */
2531 if (udev->flags.self_suspended != 0) {
2532 USB_BUS_UNLOCK(bus);
2533 usb_dev_resume_peer(udev);
2536 } else if ((temp >= limit) &&
2537 (udev->flags.usb_mode == USB_MODE_HOST) &&
2538 (udev->flags.self_suspended == 0)) {
2539 /* try to do suspend */
2541 USB_BUS_UNLOCK(bus);
2542 usb_dev_suspend_peer(udev);
2547 /* reset counters */
2549 mintime = (usb_ticks_t)-1;
2556 /* Re-loop all the devices to get the actual state */
2558 for (x = USB_ROOT_HUB_ADDR + 1;
2559 x != bus->devices_max; x++) {
2561 udev = bus->devices[x];
2565 /* we found a non-Root-Hub USB device */
2568 /* "last_xfer_time" can be updated by a resume */
2569 temp = ticks - udev->pwr_save.last_xfer_time;
2572 * Compute minimum time since last transfer for the complete
2578 if (udev->flags.self_suspended == 0) {
2579 type_refs[0] += udev->pwr_save.type_refs[0];
2580 type_refs[1] += udev->pwr_save.type_refs[1];
2581 type_refs[2] += udev->pwr_save.type_refs[2];
2582 type_refs[3] += udev->pwr_save.type_refs[3];
2586 if (mintime >= (usb_ticks_t)(1 * hz)) {
2587 /* recompute power masks */
2588 DPRINTF("Recomputing power masks\n");
2589 bus->hw_power_state = 0;
2590 if (type_refs[UE_CONTROL] != 0)
2591 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2592 if (type_refs[UE_BULK] != 0)
2593 bus->hw_power_state |= USB_HW_POWER_BULK;
2594 if (type_refs[UE_INTERRUPT] != 0)
2595 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2596 if (type_refs[UE_ISOCHRONOUS] != 0)
2597 bus->hw_power_state |= USB_HW_POWER_ISOC;
2598 if (type_refs[4] != 0)
2599 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2601 USB_BUS_UNLOCK(bus);
2603 if (bus->methods->set_hw_power != NULL) {
2604 /* always update hardware power! */
2605 (bus->methods->set_hw_power) (bus);
2611 /*------------------------------------------------------------------------*
2612 * usb_dev_resume_peer
2614 * This function will resume an USB peer and do the required USB
2615 * signalling to get an USB device out of the suspended state.
2616 *------------------------------------------------------------------------*/
2618 usb_dev_resume_peer(struct usb_device *udev)
2620 struct usb_bus *bus;
2627 /* check if already resumed */
2628 if (udev->flags.self_suspended == 0)
2631 /* we need a parent HUB to do resume */
2632 if (udev->parent_hub == NULL)
2635 DPRINTF("udev=%p\n", udev);
2637 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2638 (udev->flags.remote_wakeup == 0)) {
2640 * If the host did not set the remote wakeup feature, we can
2641 * not wake it up either!
2643 DPRINTF("remote wakeup is not set!\n");
2646 /* get bus pointer */
2649 /* resume parent hub first */
2650 usb_dev_resume_peer(udev->parent_hub);
2652 /* reduce chance of instant resume failure by waiting a little bit */
2653 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2655 if (usb_device_20_compatible(udev)) {
2656 /* resume current port (Valid in Host and Device Mode) */
2657 err = usbd_req_clear_port_feature(udev->parent_hub,
2658 NULL, udev->port_no, UHF_PORT_SUSPEND);
2660 DPRINTFN(0, "Resuming port failed\n");
2664 /* resume current port (Valid in Host and Device Mode) */
2665 err = usbd_req_set_port_link_state(udev->parent_hub,
2666 NULL, udev->port_no, UPS_PORT_LS_U0);
2668 DPRINTFN(0, "Resuming port failed\n");
2673 /* resume settle time */
2674 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2676 if (bus->methods->device_resume != NULL) {
2677 /* resume USB device on the USB controller */
2678 (bus->methods->device_resume) (udev);
2681 /* set that this device is now resumed */
2682 udev->flags.self_suspended = 0;
2684 /* make sure that we don't go into suspend right away */
2685 udev->pwr_save.last_xfer_time = ticks;
2687 /* make sure the needed power masks are on */
2688 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2689 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2690 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2691 bus->hw_power_state |= USB_HW_POWER_BULK;
2692 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2693 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2694 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2695 bus->hw_power_state |= USB_HW_POWER_ISOC;
2697 USB_BUS_UNLOCK(bus);
2699 if (bus->methods->set_hw_power != NULL) {
2700 /* always update hardware power! */
2701 (bus->methods->set_hw_power) (bus);
2706 /* notify all sub-devices about resume */
2707 err = usb_suspend_resume(udev, 0);
2709 usbd_sr_unlock(udev);
2711 /* check if peer has wakeup capability */
2712 if (usb_peer_can_wakeup(udev)) {
2713 /* clear remote wakeup */
2714 err = usbd_req_clear_device_feature(udev,
2715 NULL, UF_DEVICE_REMOTE_WAKEUP);
2717 DPRINTFN(0, "Clearing device "
2718 "remote wakeup failed: %s\n",
2724 /*------------------------------------------------------------------------*
2725 * usb_dev_suspend_peer
2727 * This function will suspend an USB peer and do the required USB
2728 * signalling to get an USB device into the suspended state.
2729 *------------------------------------------------------------------------*/
2731 usb_dev_suspend_peer(struct usb_device *udev)
2733 struct usb_device *child;
2743 /* check if already suspended */
2744 if (udev->flags.self_suspended)
2747 /* we need a parent HUB to do suspend */
2748 if (udev->parent_hub == NULL)
2751 DPRINTF("udev=%p\n", udev);
2753 /* check if the current device is a HUB */
2754 if (udev->hub != NULL) {
2755 nports = udev->hub->nports;
2757 /* check if all devices on the HUB are suspended */
2758 for (x = 0; x != nports; x++) {
2759 child = usb_bus_port_get_device(udev->bus,
2760 udev->hub->ports + x);
2765 if (child->flags.self_suspended)
2768 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2773 if (usb_peer_can_wakeup(udev)) {
2775 * This request needs to be done before we set
2776 * "udev->flags.self_suspended":
2779 /* allow device to do remote wakeup */
2780 err = usbd_req_set_device_feature(udev,
2781 NULL, UF_DEVICE_REMOTE_WAKEUP);
2783 DPRINTFN(0, "Setting device "
2784 "remote wakeup failed\n");
2788 USB_BUS_LOCK(udev->bus);
2790 * Checking for suspend condition and setting suspended bit
2793 err = usb_peer_should_wakeup(udev);
2796 * Set that this device is suspended. This variable
2797 * must be set before calling USB controller suspend
2800 udev->flags.self_suspended = 1;
2802 USB_BUS_UNLOCK(udev->bus);
2805 if (usb_peer_can_wakeup(udev)) {
2806 /* allow device to do remote wakeup */
2807 err = usbd_req_clear_device_feature(udev,
2808 NULL, UF_DEVICE_REMOTE_WAKEUP);
2810 DPRINTFN(0, "Setting device "
2811 "remote wakeup failed\n");
2815 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2816 /* resume parent HUB first */
2817 usb_dev_resume_peer(udev->parent_hub);
2819 /* reduce chance of instant resume failure by waiting a little bit */
2820 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2822 /* resume current port (Valid in Host and Device Mode) */
2823 err = usbd_req_clear_port_feature(udev->parent_hub,
2824 NULL, udev->port_no, UHF_PORT_SUSPEND);
2826 /* resume settle time */
2827 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2829 DPRINTF("Suspend was cancelled!\n");
2835 /* notify all sub-devices about suspend */
2836 err = usb_suspend_resume(udev, 1);
2838 usbd_sr_unlock(udev);
2840 if (udev->bus->methods->device_suspend != NULL) {
2843 /* suspend device on the USB controller */
2844 (udev->bus->methods->device_suspend) (udev);
2847 temp = usbd_get_dma_delay(udev);
2849 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2853 if (usb_device_20_compatible(udev)) {
2854 /* suspend current port */
2855 err = usbd_req_set_port_feature(udev->parent_hub,
2856 NULL, udev->port_no, UHF_PORT_SUSPEND);
2858 DPRINTFN(0, "Suspending port failed\n");
2862 /* suspend current port */
2863 err = usbd_req_set_port_link_state(udev->parent_hub,
2864 NULL, udev->port_no, UPS_PORT_LS_U3);
2866 DPRINTFN(0, "Suspending port failed\n");
2871 udev = udev->parent_hub;
2875 /*------------------------------------------------------------------------*
2876 * usbd_set_power_mode
2878 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2880 *------------------------------------------------------------------------*/
2882 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2884 /* filter input argument */
2885 if ((power_mode != USB_POWER_MODE_ON) &&
2886 (power_mode != USB_POWER_MODE_OFF))
2887 power_mode = USB_POWER_MODE_SAVE;
2889 power_mode = usbd_filter_power_mode(udev, power_mode);
2891 udev->power_mode = power_mode; /* update copy of power mode */
2894 usb_bus_power_update(udev->bus);
2896 usb_needs_explore(udev->bus, 0 /* no probe */ );
2900 /*------------------------------------------------------------------------*
2901 * usbd_filter_power_mode
2903 * This function filters the power mode based on hardware requirements.
2904 *------------------------------------------------------------------------*/
2906 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2908 const struct usb_bus_methods *mtod;
2911 mtod = udev->bus->methods;
2914 if (mtod->get_power_mode != NULL)
2915 (mtod->get_power_mode) (udev, &temp);
2917 /* check if we should not filter */
2919 return (power_mode);
2921 /* use fixed power mode given by hardware driver */
2925 /*------------------------------------------------------------------------*
2926 * usbd_start_re_enumerate
2928 * This function starts re-enumeration of the given USB device. This
2929 * function does not need to be called BUS-locked. This function does
2930 * not wait until the re-enumeration is completed.
2931 *------------------------------------------------------------------------*/
2933 usbd_start_re_enumerate(struct usb_device *udev)
2935 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2936 udev->re_enumerate_wait = USB_RE_ENUM_START;
2937 usb_needs_explore(udev->bus, 0);
2941 /*-----------------------------------------------------------------------*
2942 * usbd_start_set_config
2944 * This function starts setting a USB configuration. This function
2945 * does not need to be called BUS-locked. This function does not wait
2946 * until the set USB configuratino is completed.
2947 *------------------------------------------------------------------------*/
2949 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2951 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2952 if (udev->curr_config_index == index) {
2953 /* no change needed */
2956 udev->next_config_index = index;
2957 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2958 usb_needs_explore(udev->bus, 0);
2960 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2961 if (udev->next_config_index == index) {
2962 /* no change needed */
2966 return (USB_ERR_PENDING_REQUESTS);