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 */
79 #include <dev/usb/usb_hub_private.h>
83 static int uhub_debug = 0;
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
86 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
91 static int usb_power_timeout = 30; /* seconds */
93 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
94 &usb_power_timeout, 0, "USB power timeout");
97 #if USB_HAVE_DISABLE_ENUM
98 static int usb_disable_enumeration = 0;
99 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
100 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
101 "This can secure against USB devices turning evil, "
102 "for example a USB memory stick becoming a USB keyboard.");
104 static int usb_disable_port_power = 0;
105 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
106 &usb_disable_port_power, 0, "Set to disable all USB port power.");
110 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
111 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
112 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
113 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
114 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
116 /* prototypes for type checking: */
118 static device_suspend_t uhub_suspend;
119 static device_resume_t uhub_resume;
121 static bus_driver_added_t uhub_driver_added;
122 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
124 static usb_callback_t uhub_intr_callback;
125 #if USB_HAVE_TT_SUPPORT
126 static usb_callback_t uhub_reset_tt_callback;
129 static void usb_dev_resume_peer(struct usb_device *udev);
130 static void usb_dev_suspend_peer(struct usb_device *udev);
131 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
133 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
135 [UHUB_INTR_TRANSFER] = {
136 .type = UE_INTERRUPT,
137 .endpoint = UE_ADDR_ANY,
138 .direction = UE_DIR_ANY,
140 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
141 .bufsize = 0, /* use wMaxPacketSize */
142 .callback = &uhub_intr_callback,
143 .interval = UHUB_INTR_INTERVAL,
145 #if USB_HAVE_TT_SUPPORT
146 [UHUB_RESET_TT_TRANSFER] = {
148 .endpoint = 0x00, /* Control pipe */
149 .direction = UE_DIR_ANY,
150 .bufsize = sizeof(struct usb_device_request),
151 .callback = &uhub_reset_tt_callback,
152 .timeout = 1000, /* 1 second */
153 .usb_mode = USB_MODE_HOST,
159 * driver instance for "hub" connected to "usb"
160 * and "hub" connected to "hub"
162 static devclass_t uhub_devclass;
164 static device_method_t uhub_methods[] = {
165 DEVMETHOD(device_probe, uhub_probe),
166 DEVMETHOD(device_attach, uhub_attach),
167 DEVMETHOD(device_detach, uhub_detach),
169 DEVMETHOD(device_suspend, uhub_suspend),
170 DEVMETHOD(device_resume, uhub_resume),
172 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
173 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
174 DEVMETHOD(bus_driver_added, uhub_driver_added),
178 driver_t uhub_driver = {
180 .methods = uhub_methods,
181 .size = sizeof(struct uhub_softc)
184 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
185 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
186 MODULE_VERSION(uhub, 1);
189 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
191 struct uhub_softc *sc = usbd_xfer_softc(xfer);
193 switch (USB_GET_STATE(xfer)) {
194 case USB_ST_TRANSFERRED:
197 * This is an indication that some port
198 * has changed status. Notify the bus
199 * event handler thread that we need
200 * to be explored again:
202 usb_needs_explore(sc->sc_udev->bus, 0);
205 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
206 usbd_transfer_submit(xfer);
210 if (xfer->error != USB_ERR_CANCELLED) {
212 * Do a clear-stall. The "stall_pipe" flag
213 * will get cleared before next callback by
216 usbd_xfer_set_stall(xfer);
217 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
218 usbd_transfer_submit(xfer);
224 /*------------------------------------------------------------------------*
227 * This function starts the TT reset USB request
228 *------------------------------------------------------------------------*/
229 #if USB_HAVE_TT_SUPPORT
231 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
233 struct usb_udev_msg *pm = (void *)_pm;
234 struct usb_device *udev = pm->udev;
236 struct uhub_softc *sc;
246 USB_BUS_UNLOCK(udev->bus);
247 USB_MTX_LOCK(&sc->sc_mtx);
249 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
251 USB_MTX_UNLOCK(&sc->sc_mtx);
252 USB_BUS_LOCK(udev->bus);
256 /*------------------------------------------------------------------------*
257 * uhub_tt_buffer_reset_async_locked
259 * This function queues a TT reset for the given USB device and endpoint.
260 *------------------------------------------------------------------------*/
261 #if USB_HAVE_TT_SUPPORT
263 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
265 struct usb_device_request req;
266 struct usb_device *udev;
272 if (child == NULL || ep == NULL)
275 udev = child->parent_hs_hub;
276 port = child->hs_port_no;
283 (udev->speed != USB_SPEED_HIGH) ||
284 (child->speed != USB_SPEED_LOW &&
285 child->speed != USB_SPEED_FULL) ||
286 (child->flags.usb_mode != USB_MODE_HOST) ||
287 (port == 0) || (ep->edesc == NULL)) {
292 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
294 up = hub->ports + port - 1;
296 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
297 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
300 /* if we already received a clear buffer request, reset the whole TT */
301 if (up->req_reset_tt.bRequest != 0) {
302 req.bmRequestType = UT_WRITE_CLASS_OTHER;
303 req.bRequest = UR_RESET_TT;
304 USETW(req.wValue, 0);
305 req.wIndex[0] = port;
307 USETW(req.wLength, 0);
309 wValue = (ep->edesc->bEndpointAddress & 0xF) |
310 ((child->address & 0x7F) << 4) |
311 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
312 ((ep->edesc->bmAttributes & 3) << 12);
314 req.bmRequestType = UT_WRITE_CLASS_OTHER;
315 req.bRequest = UR_CLEAR_TT_BUFFER;
316 USETW(req.wValue, wValue);
317 req.wIndex[0] = port;
319 USETW(req.wLength, 0);
321 up->req_reset_tt = req;
322 /* get reset transfer started */
323 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
324 &hub->tt_msg[0], &hub->tt_msg[1]);
328 #if USB_HAVE_TT_SUPPORT
330 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
332 struct uhub_softc *sc;
333 struct usb_device *udev;
337 DPRINTF("TT buffer reset\n");
339 sc = usbd_xfer_softc(xfer);
342 switch (USB_GET_STATE(xfer)) {
343 case USB_ST_TRANSFERRED:
346 USB_BUS_LOCK(udev->bus);
347 /* find first port which needs a TT reset */
348 for (x = 0; x != udev->hub->nports; x++) {
349 up = udev->hub->ports + x;
351 if (up->req_reset_tt.bRequest == 0)
354 /* copy in the transfer */
355 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
356 sizeof(up->req_reset_tt));
358 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
361 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
363 USB_BUS_UNLOCK(udev->bus);
365 usbd_transfer_submit(xfer);
368 USB_BUS_UNLOCK(udev->bus);
372 if (error == USB_ERR_CANCELLED)
375 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
381 /*------------------------------------------------------------------------*
382 * uhub_count_active_host_ports
384 * This function counts the number of active ports at the given speed.
385 *------------------------------------------------------------------------*/
387 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
389 struct uhub_softc *sc;
390 struct usb_device *child;
405 for (x = 0; x != hub->nports; x++) {
407 child = usb_bus_port_get_device(udev->bus, up);
409 child->flags.usb_mode == USB_MODE_HOST &&
410 child->speed == speed)
418 uhub_explore_handle_re_enumerate(struct usb_device *child)
423 /* check if device should be re-enumerated */
424 if (child->flags.usb_mode != USB_MODE_HOST)
427 do_unlock = usbd_enum_lock(child);
428 switch (child->re_enumerate_wait) {
429 case USB_RE_ENUM_START:
430 err = usbd_set_config_index(child,
433 DPRINTF("Unconfigure failed: %s: Ignored.\n",
436 if (child->parent_hub == NULL) {
437 /* the root HUB cannot be re-enumerated */
438 DPRINTFN(6, "cannot reset root HUB\n");
441 err = usbd_req_re_enumerate(child, NULL);
444 err = usbd_set_config_index(child, 0);
446 err = usb_probe_and_attach(child,
447 USB_IFACE_INDEX_ANY);
449 child->re_enumerate_wait = USB_RE_ENUM_DONE;
452 case USB_RE_ENUM_PWR_OFF:
453 /* get the device unconfigured */
454 err = usbd_set_config_index(child,
457 DPRINTFN(0, "Could not unconfigure "
458 "device (ignored)\n");
460 if (child->parent_hub == NULL) {
461 /* the root HUB cannot be re-enumerated */
462 DPRINTFN(6, "cannot set port feature\n");
465 /* clear port enable */
466 err = usbd_req_clear_port_feature(child->parent_hub,
467 NULL, child->port_no, UHF_PORT_ENABLE);
469 DPRINTFN(0, "Could not disable port "
473 child->re_enumerate_wait = USB_RE_ENUM_DONE;
476 case USB_RE_ENUM_SET_CONFIG:
477 err = usbd_set_config_index(child,
478 child->next_config_index);
480 DPRINTF("Configure failed: %s: Ignored.\n",
483 err = usb_probe_and_attach(child,
484 USB_IFACE_INDEX_ANY);
486 child->re_enumerate_wait = USB_RE_ENUM_DONE;
490 child->re_enumerate_wait = USB_RE_ENUM_DONE;
494 usbd_enum_unlock(child);
497 /*------------------------------------------------------------------------*
498 * uhub_explore_sub - subroutine
502 * Else: A control transaction failed
503 *------------------------------------------------------------------------*/
505 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
508 struct usb_device *child;
512 bus = sc->sc_udev->bus;
515 /* get driver added refcount from USB bus */
516 refcount = bus->driver_added_refcount;
518 /* get device assosiated with the given port */
519 child = usb_bus_port_get_device(bus, up);
525 uhub_explore_handle_re_enumerate(child);
527 /* check if probe and attach should be done */
529 if (child->driver_added_refcount != refcount) {
530 child->driver_added_refcount = refcount;
531 err = usb_probe_and_attach(child,
532 USB_IFACE_INDEX_ANY);
537 /* start control transfer, if device mode */
539 if (child->flags.usb_mode == USB_MODE_DEVICE)
540 usbd_ctrl_transfer_setup(child);
542 /* if a HUB becomes present, do a recursive HUB explore */
545 err = (child->hub->explore) (child);
551 /*------------------------------------------------------------------------*
552 * uhub_read_port_status - factored out code
553 *------------------------------------------------------------------------*/
555 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
557 struct usb_port_status ps;
560 if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
561 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
562 sc->sc_st.port_status = 0;
563 sc->sc_st.port_change = 0;
564 return (USB_ERR_TIMEOUT);
567 err = usbd_req_get_port_status(
568 sc->sc_udev, NULL, &ps, portno);
571 sc->sc_st.port_status = UGETW(ps.wPortStatus);
572 sc->sc_st.port_change = UGETW(ps.wPortChange);
573 sc->sc_usb_port_errors = 0;
575 sc->sc_st.port_status = 0;
576 sc->sc_st.port_change = 0;
577 sc->sc_usb_port_errors++;
580 /* debugging print */
582 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
583 "wPortChange=0x%04x, err=%s\n",
584 portno, sc->sc_st.port_status,
585 sc->sc_st.port_change, usbd_errstr(err));
589 /*------------------------------------------------------------------------*
594 * Else: A control transaction failed
595 *------------------------------------------------------------------------*/
597 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
599 struct usb_device *child;
600 struct usb_device *udev;
601 enum usb_dev_speed speed;
602 enum usb_hc_mode mode;
607 DPRINTF("reattaching port %d\n", portno);
611 child = usb_bus_port_get_device(udev->bus,
612 udev->hub->ports + portno - 1);
616 /* first clear the port connection change bit */
618 err = usbd_req_clear_port_feature(udev, NULL,
619 portno, UHF_C_PORT_CONNECTION);
624 /* check if there is a child */
628 * Free USB device and all subdevices, if any.
630 usb_free_device(child, 0);
633 /* get fresh status */
635 err = uhub_read_port_status(sc, portno);
639 #if USB_HAVE_DISABLE_ENUM
640 /* check if we should skip enumeration from this USB HUB */
641 if (usb_disable_enumeration != 0 ||
642 sc->sc_disable_enumeration != 0) {
643 DPRINTF("Enumeration is disabled!\n");
647 /* check if nothing is connected to the port */
649 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
652 /* check if there is no power on the port and print a warning */
654 switch (udev->speed) {
658 power_mask = UPS_PORT_POWER;
660 case USB_SPEED_SUPER:
661 if (udev->parent_hub == NULL)
662 power_mask = UPS_PORT_POWER;
664 power_mask = UPS_PORT_POWER_SS;
670 if (!(sc->sc_st.port_status & power_mask)) {
671 DPRINTF("WARNING: strange, connected port %d "
672 "has no power\n", portno);
675 /* check if the device is in Host Mode */
677 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
679 DPRINTF("Port %d is in Host Mode\n", portno);
681 if (sc->sc_st.port_status & UPS_SUSPEND) {
683 * NOTE: Should not get here in SuperSpeed
684 * mode, because the HUB should report this
687 DPRINTF("Port %d was still "
688 "suspended, clearing.\n", portno);
689 err = usbd_req_clear_port_feature(udev,
690 NULL, portno, UHF_PORT_SUSPEND);
695 /* wait for maximum device power up time */
698 USB_MS_TO_TICKS(usb_port_powerup_delay));
700 /* reset port, which implies enabling it */
702 err = usbd_req_reset_port(udev, NULL, portno);
705 DPRINTFN(0, "port %d reset "
706 "failed, error=%s\n",
707 portno, usbd_errstr(err));
710 /* get port status again, it might have changed during reset */
712 err = uhub_read_port_status(sc, portno);
716 /* check if something changed during port reset */
718 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
719 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
721 DPRINTFN(0, "giving up port reset "
722 "- device vanished\n");
729 DPRINTF("Port %d is in Device Mode\n", portno);
733 * Figure out the device speed
735 switch (udev->speed) {
737 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
738 speed = USB_SPEED_HIGH;
739 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
740 speed = USB_SPEED_LOW;
742 speed = USB_SPEED_FULL;
745 if (sc->sc_st.port_status & UPS_LOW_SPEED)
746 speed = USB_SPEED_LOW;
748 speed = USB_SPEED_FULL;
751 speed = USB_SPEED_LOW;
753 case USB_SPEED_SUPER:
754 if (udev->parent_hub == NULL) {
755 /* Root HUB - special case */
756 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
758 speed = USB_SPEED_FULL;
761 speed = USB_SPEED_LOW;
764 speed = USB_SPEED_HIGH;
767 speed = USB_SPEED_SUPER;
771 speed = USB_SPEED_SUPER;
775 /* same speed like parent */
779 if (speed == USB_SPEED_SUPER) {
780 err = usbd_req_set_hub_u1_timeout(udev, NULL,
781 portno, 128 - (2 * udev->depth));
783 DPRINTFN(0, "port %d U1 timeout "
784 "failed, error=%s\n",
785 portno, usbd_errstr(err));
787 err = usbd_req_set_hub_u2_timeout(udev, NULL,
788 portno, 128 - (2 * udev->depth));
790 DPRINTFN(0, "port %d U2 timeout "
791 "failed, error=%s\n",
792 portno, usbd_errstr(err));
797 * Figure out the device mode
799 * NOTE: This part is currently FreeBSD specific.
801 if (udev->parent_hub != NULL) {
802 /* inherit mode from the parent HUB */
803 mode = udev->parent_hub->flags.usb_mode;
804 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
805 mode = USB_MODE_DEVICE;
807 mode = USB_MODE_HOST;
809 /* need to create a new child */
810 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
811 udev->depth + 1, portno - 1, portno, speed, mode);
813 DPRINTFN(0, "could not allocate new device\n");
816 return (0); /* success */
821 * Free USB device and all subdevices, if any.
823 usb_free_device(child, 0);
827 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
828 err = usbd_req_clear_port_feature(
830 portno, UHF_PORT_ENABLE);
834 DPRINTFN(0, "device problem (%s), "
835 "disabling port %d\n", usbd_errstr(err), portno);
840 /*------------------------------------------------------------------------*
841 * usb_device_20_compatible
844 * 0: HUB does not support suspend and resume
845 * Else: HUB supports suspend and resume
846 *------------------------------------------------------------------------*/
848 usb_device_20_compatible(struct usb_device *udev)
852 switch (udev->speed) {
862 /*------------------------------------------------------------------------*
863 * uhub_suspend_resume_port
867 * Else: A control transaction failed
868 *------------------------------------------------------------------------*/
870 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
872 struct usb_device *child;
873 struct usb_device *udev;
877 DPRINTF("port %d\n", portno);
880 child = usb_bus_port_get_device(udev->bus,
881 udev->hub->ports + portno - 1);
883 /* first clear the port suspend change bit */
885 if (usb_device_20_compatible(udev)) {
886 err = usbd_req_clear_port_feature(udev, NULL,
887 portno, UHF_C_PORT_SUSPEND);
889 err = usbd_req_clear_port_feature(udev, NULL,
890 portno, UHF_C_PORT_LINK_STATE);
894 DPRINTF("clearing suspend failed.\n");
897 /* get fresh status */
899 err = uhub_read_port_status(sc, portno);
901 DPRINTF("reading port status failed.\n");
904 /* convert current state */
906 if (usb_device_20_compatible(udev)) {
907 if (sc->sc_st.port_status & UPS_SUSPEND) {
913 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
917 case UPS_PORT_LS_SS_INA:
918 usbd_req_warm_reset_port(udev, NULL, portno);
927 DPRINTF("suspended=%u\n", is_suspend);
929 /* do the suspend or resume */
933 * This code handle two cases: 1) Host Mode - we can only
934 * receive resume here 2) Device Mode - we can receive
935 * suspend and resume here
938 usb_dev_resume_peer(child);
939 else if (child->flags.usb_mode == USB_MODE_DEVICE)
940 usb_dev_suspend_peer(child);
946 /*------------------------------------------------------------------------*
947 * uhub_root_interrupt
949 * This function is called when a Root HUB interrupt has
950 * happened. "ptr" and "len" makes up the Root HUB interrupt
951 * packet. This function is called having the "bus_mtx" locked.
952 *------------------------------------------------------------------------*/
954 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
956 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
958 usb_needs_explore(bus, 0);
962 uhub_is_too_deep(struct usb_device *udev)
964 switch (udev->speed) {
968 if (udev->depth > USB_HUB_MAX_DEPTH)
971 case USB_SPEED_SUPER:
972 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
981 /*------------------------------------------------------------------------*
987 *------------------------------------------------------------------------*/
989 uhub_explore(struct usb_device *udev)
992 struct uhub_softc *sc;
1002 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1004 /* ignore devices that are too deep */
1005 if (uhub_is_too_deep(udev))
1006 return (USB_ERR_TOO_DEEP);
1008 /* check if device is suspended */
1009 if (udev->flags.self_suspended) {
1010 /* need to wait until the child signals resume */
1011 DPRINTF("Device is suspended!\n");
1016 * Make sure we don't race against user-space applications
1019 do_unlock = usbd_enum_lock(udev);
1021 for (x = 0; x != hub->nports; x++) {
1022 up = hub->ports + x;
1025 err = uhub_read_port_status(sc, portno);
1027 /* most likely the HUB is gone */
1030 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1031 DPRINTF("Overcurrent on port %u.\n", portno);
1032 err = usbd_req_clear_port_feature(
1033 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1035 /* most likely the HUB is gone */
1039 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1041 * Fake a connect status change so that the
1042 * status gets checked initially!
1044 sc->sc_st.port_change |=
1045 UPS_C_CONNECT_STATUS;
1047 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1048 err = usbd_req_clear_port_feature(
1049 udev, NULL, portno, UHF_C_PORT_ENABLE);
1051 /* most likely the HUB is gone */
1054 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1056 * Ignore the port error if the device
1059 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1060 DPRINTFN(0, "illegal enable change, "
1061 "port %d\n", portno);
1064 if (up->restartcnt == USB_RESTART_MAX) {
1065 /* XXX could try another speed ? */
1066 DPRINTFN(0, "port error, giving up "
1067 "port %d\n", portno);
1069 sc->sc_st.port_change |=
1070 UPS_C_CONNECT_STATUS;
1075 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1076 err = uhub_reattach_port(sc, portno);
1078 /* most likely the HUB is gone */
1082 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1083 UPS_C_PORT_LINK_STATE)) {
1084 err = uhub_suspend_resume_port(sc, portno);
1086 /* most likely the HUB is gone */
1090 err = uhub_explore_sub(sc, up);
1092 /* no device(s) present */
1095 /* explore succeeded - reset restart counter */
1100 usbd_enum_unlock(udev);
1102 /* initial status checked */
1103 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1105 /* return success */
1106 return (USB_ERR_NORMAL_COMPLETION);
1110 uhub_probe(device_t dev)
1112 struct usb_attach_arg *uaa = device_get_ivars(dev);
1114 if (uaa->usb_mode != USB_MODE_HOST)
1118 * The subclass for USB HUBs is currently ignored because it
1119 * is 0 for some and 1 for others.
1121 if (uaa->info.bConfigIndex == 0 &&
1122 uaa->info.bDeviceClass == UDCLASS_HUB)
1123 return (BUS_PROBE_DEFAULT);
1128 /* NOTE: The information returned by this function can be wrong. */
1130 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1132 struct usb_hub_descriptor hubdesc20;
1133 struct usb_hub_ss_descriptor hubdesc30;
1138 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1139 return (USB_ERR_INVAL);
1144 switch (udev->speed) {
1146 case USB_SPEED_FULL:
1147 case USB_SPEED_HIGH:
1148 /* assuming that there is one port */
1149 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1151 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1152 "error=%s\n", usbd_errstr(err));
1155 nports = hubdesc20.bNbrPorts;
1159 if (udev->speed == USB_SPEED_HIGH)
1160 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1163 case USB_SPEED_SUPER:
1164 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1166 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1167 "error=%s\n", usbd_errstr(err));
1170 nports = hubdesc30.bNbrPorts;
1176 err = USB_ERR_INVAL;
1180 if (pnports != NULL)
1190 uhub_attach(device_t dev)
1192 struct uhub_softc *sc = device_get_softc(dev);
1193 struct usb_attach_arg *uaa = device_get_ivars(dev);
1194 struct usb_device *udev = uaa->device;
1195 struct usb_device *parent_hub = udev->parent_hub;
1196 struct usb_hub *hub;
1197 struct usb_hub_descriptor hubdesc20;
1198 struct usb_hub_ss_descriptor hubdesc30;
1199 #if USB_HAVE_DISABLE_ENUM
1200 struct sysctl_ctx_list *sysctl_ctx;
1201 struct sysctl_oid *sysctl_tree;
1208 uint8_t iface_index;
1214 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1216 device_set_usb_desc(dev);
1218 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1219 "parent->selfpowered=%d\n",
1221 udev->flags.self_powered,
1224 parent_hub->flags.self_powered : 0);
1226 if (uhub_is_too_deep(udev)) {
1227 DPRINTFN(0, "HUB at depth %d, "
1228 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1232 if (!udev->flags.self_powered && parent_hub &&
1233 !parent_hub->flags.self_powered) {
1234 DPRINTFN(0, "Bus powered HUB connected to "
1235 "bus powered HUB. HUB ignored\n");
1239 if (UHUB_IS_MULTI_TT(sc)) {
1240 err = usbd_set_alt_interface_index(udev, 0, 1);
1242 device_printf(dev, "MTT could not be enabled\n");
1245 device_printf(dev, "MTT enabled\n");
1248 /* get HUB descriptor */
1250 DPRINTFN(2, "Getting HUB descriptor\n");
1252 switch (udev->speed) {
1254 case USB_SPEED_FULL:
1255 case USB_SPEED_HIGH:
1256 /* assuming that there is one port */
1257 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1259 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1260 "error=%s\n", usbd_errstr(err));
1263 /* get number of ports */
1264 nports = hubdesc20.bNbrPorts;
1266 /* get power delay */
1267 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1268 usb_extra_power_up_time);
1270 /* get complete HUB descriptor */
1272 /* check number of ports */
1274 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1275 "error=%s\n", usbd_errstr(err));
1278 /* get complete HUB descriptor */
1279 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1282 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1283 "error=%s\n", usbd_errstr(err));
1286 if (hubdesc20.bNbrPorts != nports) {
1287 DPRINTFN(0, "Number of ports changed\n");
1292 case USB_SPEED_SUPER:
1293 if (udev->parent_hub != NULL) {
1294 err = usbd_req_set_hub_depth(udev, NULL,
1297 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1298 "error=%s\n", usbd_errstr(err));
1302 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1304 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1305 "error=%s\n", usbd_errstr(err));
1308 /* get number of ports */
1309 nports = hubdesc30.bNbrPorts;
1311 /* get power delay */
1312 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1313 usb_extra_power_up_time);
1315 /* get complete HUB descriptor */
1317 /* check number of ports */
1318 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1319 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1320 "error=%s\n", usbd_errstr(err));
1323 /* get complete HUB descriptor */
1324 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1327 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1328 "error=%s\n", usbd_errstr(err));
1331 if (hubdesc30.bNbrPorts != nports) {
1332 DPRINTFN(0, "Number of ports changed\n");
1338 DPRINTF("Assuming HUB has only one port\n");
1339 /* default number of ports */
1341 /* default power delay */
1342 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1346 DPRINTFN(0, "portless HUB\n");
1349 if (nports > USB_MAX_PORTS) {
1350 DPRINTF("Port limit exceeded\n");
1353 #if (USB_HAVE_FIXED_PORT == 0)
1354 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1355 M_USBDEV, M_WAITOK | M_ZERO);
1364 /* initialize HUB structure */
1366 hub->explore = &uhub_explore;
1367 hub->nports = nports;
1368 hub->hubudev = udev;
1369 #if USB_HAVE_TT_SUPPORT
1370 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1371 hub->tt_msg[0].udev = udev;
1372 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1373 hub->tt_msg[1].udev = udev;
1375 /* if self powered hub, give ports maximum current */
1376 if (udev->flags.self_powered) {
1377 hub->portpower = USB_MAX_POWER;
1379 hub->portpower = USB_MIN_POWER;
1382 /* set up interrupt pipe */
1384 if (udev->parent_hub == NULL) {
1385 /* root HUB is special */
1389 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1390 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1393 DPRINTFN(0, "cannot setup interrupt transfer, "
1394 "errstr=%s\n", usbd_errstr(err));
1397 /* wait with power off for a while */
1398 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1400 #if USB_HAVE_DISABLE_ENUM
1401 /* Add device sysctls */
1403 sysctl_ctx = device_get_sysctl_ctx(dev);
1404 sysctl_tree = device_get_sysctl_tree(dev);
1406 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1407 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1408 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1409 &sc->sc_disable_enumeration, 0,
1410 "Set to disable enumeration on this USB HUB.");
1412 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1413 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1414 &sc->sc_disable_port_power, 0,
1415 "Set to disable USB port power on this USB HUB.");
1419 * To have the best chance of success we do things in the exact same
1420 * order as Windoze98. This should not be necessary, but some
1421 * devices do not follow the USB specs to the letter.
1423 * These are the events on the bus when a hub is attached:
1424 * Get device and config descriptors (see attach code)
1425 * Get hub descriptor (see above)
1428 * wait for power to become stable
1429 * (all below happens in explore code)
1431 * clear C_PORT_CONNECTION
1434 * if device connected
1438 * clear C_PORT_RESET
1440 * proceed with device attachment
1443 /* XXX should check for none, individual, or ganged power? */
1447 for (x = 0; x != nports; x++) {
1448 /* set up data structures */
1449 struct usb_port *up = hub->ports + x;
1451 up->device_index = 0;
1455 /* check if port is removable */
1456 switch (udev->speed) {
1458 case USB_SPEED_FULL:
1459 case USB_SPEED_HIGH:
1460 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1463 case USB_SPEED_SUPER:
1464 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1468 DPRINTF("Assuming removable port\n");
1473 #if USB_HAVE_DISABLE_ENUM
1474 /* check if we should disable USB port power or not */
1475 if (usb_disable_port_power != 0 ||
1476 sc->sc_disable_port_power != 0) {
1477 /* turn the power off */
1478 DPRINTFN(2, "Turning port %d power off\n", portno);
1479 err = usbd_req_clear_port_feature(udev, NULL,
1480 portno, UHF_PORT_POWER);
1483 /* turn the power on */
1484 DPRINTFN(2, "Turning port %d power on\n", portno);
1485 err = usbd_req_set_port_feature(udev, NULL,
1486 portno, UHF_PORT_POWER);
1487 #if USB_HAVE_DISABLE_ENUM
1492 DPRINTFN(0, "port %d power on or off failed, %s\n",
1493 portno, usbd_errstr(err));
1495 DPRINTF("turn on port %d power\n",
1498 /* wait for stable power */
1499 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1502 device_printf(dev, "%d port%s with %d "
1503 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1504 removable, udev->flags.self_powered ? "self" : "bus");
1506 /* Start the interrupt endpoint, if any */
1508 USB_MTX_LOCK(&sc->sc_mtx);
1509 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1510 USB_MTX_UNLOCK(&sc->sc_mtx);
1512 /* Enable automatic power save on all USB HUBs */
1514 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1519 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1521 #if (USB_HAVE_FIXED_PORT == 0)
1522 free(udev->hub, M_USBDEV);
1526 mtx_destroy(&sc->sc_mtx);
1532 * Called from process context when the hub is gone.
1533 * Detach all devices on active ports.
1536 uhub_detach(device_t dev)
1538 struct uhub_softc *sc = device_get_softc(dev);
1539 struct usb_hub *hub = sc->sc_udev->hub;
1540 struct usb_bus *bus = sc->sc_udev->bus;
1541 struct usb_device *child;
1544 if (hub == NULL) /* must be partially working */
1547 /* Make sure interrupt transfer is gone. */
1548 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1550 /* Detach all ports */
1551 for (x = 0; x != hub->nports; x++) {
1553 child = usb_bus_port_get_device(bus, hub->ports + x);
1555 if (child == NULL) {
1560 * Free USB device and all subdevices, if any.
1562 usb_free_device(child, 0);
1565 #if USB_HAVE_TT_SUPPORT
1566 /* Make sure our TT messages are not queued anywhere */
1568 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1569 &hub->tt_msg[0], &hub->tt_msg[1]);
1570 USB_BUS_UNLOCK(bus);
1573 #if (USB_HAVE_FIXED_PORT == 0)
1574 free(hub, M_USBDEV);
1576 sc->sc_udev->hub = NULL;
1578 mtx_destroy(&sc->sc_mtx);
1584 uhub_suspend(device_t dev)
1587 /* Sub-devices are not suspended here! */
1592 uhub_resume(device_t dev)
1595 /* Sub-devices are not resumed here! */
1600 uhub_driver_added(device_t dev, driver_t *driver)
1602 usb_needs_explore_all();
1606 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1607 struct hub_result *res)
1609 struct usb_interface *iface;
1610 struct usb_device *udev;
1615 nports = hub->nports;
1616 for (x = 0; x != nports; x++) {
1617 udev = usb_bus_port_get_device(hub->hubudev->bus,
1622 for (i = 0; i != USB_IFACE_MAX; i++) {
1623 iface = usbd_get_iface(udev, i);
1625 (iface->subdev == child)) {
1626 res->iface_index = i;
1628 res->portno = x + 1;
1633 res->iface_index = 0;
1639 uhub_child_location_string(device_t parent, device_t child,
1640 char *buf, size_t buflen)
1642 struct uhub_softc *sc;
1643 struct usb_hub *hub;
1644 struct hub_result res;
1646 if (!device_is_attached(parent)) {
1652 sc = device_get_softc(parent);
1653 hub = sc->sc_udev->hub;
1656 uhub_find_iface_index(hub, child, &res);
1658 DPRINTF("device not on hub\n");
1664 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1669 , device_get_unit(res.udev->bus->bdev)
1670 , (res.udev->parent_hub != NULL) ?
1671 res.udev->parent_hub->device_index : 0
1672 , res.portno, res.udev->device_index, res.iface_index
1674 , res.udev->ugen_name
1684 uhub_child_pnpinfo_string(device_t parent, device_t child,
1685 char *buf, size_t buflen)
1687 struct uhub_softc *sc;
1688 struct usb_hub *hub;
1689 struct usb_interface *iface;
1690 struct hub_result res;
1692 if (!device_is_attached(parent)) {
1698 sc = device_get_softc(parent);
1699 hub = sc->sc_udev->hub;
1702 uhub_find_iface_index(hub, child, &res);
1704 DPRINTF("device not on hub\n");
1710 iface = usbd_get_iface(res.udev, res.iface_index);
1711 if (iface && iface->idesc) {
1712 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1713 "devclass=0x%02x devsubclass=0x%02x "
1718 "intclass=0x%02x intsubclass=0x%02x "
1719 "intprotocol=0x%02x" "%s%s",
1720 UGETW(res.udev->ddesc.idVendor),
1721 UGETW(res.udev->ddesc.idProduct),
1722 res.udev->ddesc.bDeviceClass,
1723 res.udev->ddesc.bDeviceSubClass,
1724 res.udev->ddesc.bDeviceProtocol,
1725 usb_get_serial(res.udev),
1726 UGETW(res.udev->ddesc.bcdDevice),
1727 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1728 iface->idesc->bInterfaceClass,
1729 iface->idesc->bInterfaceSubClass,
1730 iface->idesc->bInterfaceProtocol,
1731 iface->pnpinfo ? " " : "",
1732 iface->pnpinfo ? iface->pnpinfo : "");
1746 * The USB Transaction Translator:
1747 * ===============================
1749 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1750 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1751 * USB transfers. To utilize bandwidth dynamically the "scatter and
1752 * gather" principle must be applied. This means that bandwidth must
1753 * be divided into equal parts of bandwidth. With regard to USB all
1754 * data is transferred in smaller packets with length
1755 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1758 * The bandwidth scheduler which I have implemented will simply pack
1759 * the USB transfers back to back until there is no more space in the
1760 * schedule. Out of the 8 microframes which the USB 2.0 standard
1761 * provides, only 6 are available for non-HIGH-speed devices. I have
1762 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1763 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1764 * this division, it is very difficult to allocate and free bandwidth
1767 * NOTE about the Transaction Translator in USB HUBs:
1769 * USB HUBs have a very simple Transaction Translator, that will
1770 * simply pipeline all the SPLIT transactions. That means that the
1771 * transactions will be executed in the order they are queued!
1775 /*------------------------------------------------------------------------*
1776 * usb_intr_find_best_slot
1779 * The best Transaction Translation slot for an interrupt endpoint.
1780 *------------------------------------------------------------------------*/
1782 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1783 uint8_t end, uint8_t mask)
1785 usb_size_t min = (usb_size_t)-1;
1793 /* find the last slot with lesser used bandwidth */
1795 for (x = start; x < end; x++) {
1799 /* compute sum of bandwidth */
1800 for (z = x; z < end; z++) {
1801 if (mask & (1U << (z - x)))
1805 /* check if the current multi-slot is more optimal */
1811 /* check if the mask is about to be shifted out */
1812 if (mask & (1U << (end - 1 - x)))
1818 /*------------------------------------------------------------------------*
1819 * usb_hs_bandwidth_adjust
1821 * This function will update the bandwidth usage for the microframe
1822 * having index "slot" by "len" bytes. "len" can be negative. If the
1823 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1824 * the "slot" argument will be replaced by the slot having least used
1825 * bandwidth. The "mask" argument is used for multi-slot allocations.
1828 * The slot in which the bandwidth update was done: 0..7
1829 *------------------------------------------------------------------------*/
1831 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1832 uint8_t slot, uint8_t mask)
1834 struct usb_bus *bus = udev->bus;
1835 struct usb_hub *hub;
1836 enum usb_dev_speed speed;
1839 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1841 speed = usbd_get_speed(udev);
1845 case USB_SPEED_FULL:
1846 if (speed == USB_SPEED_LOW) {
1850 * The Host Controller Driver should have
1851 * performed checks so that the lookup
1852 * below does not result in a NULL pointer
1856 hub = udev->parent_hs_hub->hub;
1857 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1858 slot = usb_intr_find_best_slot(hub->uframe_usage,
1859 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1861 for (x = slot; x < 8; x++) {
1862 if (mask & (1U << (x - slot))) {
1863 hub->uframe_usage[x] += len;
1864 bus->uframe_usage[x] += len;
1869 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1870 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1871 USB_HS_MICRO_FRAMES_MAX, mask);
1873 for (x = slot; x < 8; x++) {
1874 if (mask & (1U << (x - slot))) {
1875 bus->uframe_usage[x] += len;
1883 /*------------------------------------------------------------------------*
1884 * usb_hs_bandwidth_alloc
1886 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1887 *------------------------------------------------------------------------*/
1889 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1891 struct usb_device *udev;
1896 udev = xfer->xroot->udev;
1898 if (udev->flags.usb_mode != USB_MODE_HOST)
1899 return; /* not supported */
1901 xfer->endpoint->refcount_bw++;
1902 if (xfer->endpoint->refcount_bw != 1)
1903 return; /* already allocated */
1905 speed = usbd_get_speed(udev);
1907 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1909 /* allocate a microframe slot */
1912 slot = usb_hs_bandwidth_adjust(udev,
1913 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1915 xfer->endpoint->usb_uframe = slot;
1916 xfer->endpoint->usb_smask = mask << slot;
1918 if ((speed != USB_SPEED_FULL) &&
1919 (speed != USB_SPEED_LOW)) {
1920 xfer->endpoint->usb_cmask = 0x00 ;
1922 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1926 case UE_ISOCHRONOUS:
1927 switch (usbd_xfer_get_fps_shift(xfer)) {
1942 /* allocate a microframe multi-slot */
1944 slot = usb_hs_bandwidth_adjust(udev,
1945 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1947 xfer->endpoint->usb_uframe = slot;
1948 xfer->endpoint->usb_cmask = 0;
1949 xfer->endpoint->usb_smask = mask << slot;
1953 xfer->endpoint->usb_uframe = 0;
1954 xfer->endpoint->usb_cmask = 0;
1955 xfer->endpoint->usb_smask = 0;
1959 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1960 xfer->endpoint->usb_uframe,
1961 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1964 /*------------------------------------------------------------------------*
1965 * usb_hs_bandwidth_free
1967 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1968 *------------------------------------------------------------------------*/
1970 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1972 struct usb_device *udev;
1976 udev = xfer->xroot->udev;
1978 if (udev->flags.usb_mode != USB_MODE_HOST)
1979 return; /* not supported */
1981 xfer->endpoint->refcount_bw--;
1982 if (xfer->endpoint->refcount_bw != 0)
1983 return; /* still allocated */
1985 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1987 case UE_ISOCHRONOUS:
1989 slot = xfer->endpoint->usb_uframe;
1990 mask = xfer->endpoint->usb_smask;
1992 /* free microframe slot(s): */
1993 usb_hs_bandwidth_adjust(udev,
1994 -xfer->max_frame_size, slot, mask >> slot);
1996 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1997 slot, mask >> slot);
1999 xfer->endpoint->usb_uframe = 0;
2000 xfer->endpoint->usb_cmask = 0;
2001 xfer->endpoint->usb_smask = 0;
2009 /*------------------------------------------------------------------------*
2010 * usb_isoc_time_expand
2012 * This function will expand the time counter from 7-bit to 16-bit.
2015 * 16-bit isochronous time counter.
2016 *------------------------------------------------------------------------*/
2018 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2022 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2024 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2026 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2028 if (isoc_time_curr < rem) {
2029 /* the time counter wrapped around */
2030 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2032 /* update the remainder */
2034 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2035 bus->isoc_time_last |= isoc_time_curr;
2037 return (bus->isoc_time_last);
2040 /*------------------------------------------------------------------------*
2041 * usbd_fs_isoc_schedule_alloc_slot
2043 * This function will allocate bandwidth for an isochronous FULL speed
2044 * transaction in the FULL speed schedule.
2049 *------------------------------------------------------------------------*/
2050 #if USB_HAVE_TT_SUPPORT
2052 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2054 struct usb_xfer *xfer;
2055 struct usb_xfer *pipe_xfer;
2056 struct usb_bus *bus;
2058 usb_frlength_t data_len;
2066 bus = isoc_xfer->xroot->bus;
2068 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2070 /* skip self, if any */
2072 if (xfer == isoc_xfer)
2075 /* check if this USB transfer is going through the same TT */
2077 if (xfer->xroot->udev->parent_hs_hub !=
2078 isoc_xfer->xroot->udev->parent_hs_hub) {
2081 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2082 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2083 (xfer->xroot->udev->hs_port_no !=
2084 isoc_xfer->xroot->udev->hs_port_no)) {
2087 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2090 /* check if isoc_time is part of this transfer */
2092 delta = xfer->isoc_time_complete - isoc_time;
2093 if (delta > 0 && delta <= xfer->nframes) {
2094 delta = xfer->nframes - delta;
2096 len = xfer->frlengths[delta];
2105 * Check double buffered transfers. Only stream ID
2106 * equal to zero is valid here!
2108 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2111 /* skip self, if any */
2113 if (pipe_xfer == isoc_xfer)
2116 /* check if isoc_time is part of this transfer */
2118 delta = pipe_xfer->isoc_time_complete - isoc_time;
2119 if (delta > 0 && delta <= pipe_xfer->nframes) {
2120 delta = pipe_xfer->nframes - delta;
2122 len = pipe_xfer->frlengths[delta];
2132 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2133 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2137 /* check for overflow */
2139 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2144 delta = isoc_xfer->isoc_time_complete - isoc_time;
2145 if (delta > 0 && delta <= isoc_xfer->nframes) {
2146 delta = isoc_xfer->nframes - delta;
2148 len = isoc_xfer->frlengths[delta];
2156 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2157 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2161 /* check for overflow */
2163 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2170 /*------------------------------------------------------------------------*
2171 * usb_bus_port_get_device
2173 * This function is NULL safe.
2174 *------------------------------------------------------------------------*/
2176 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2178 if ((bus == NULL) || (up == NULL)) {
2182 if (up->device_index == 0) {
2186 return (bus->devices[up->device_index]);
2189 /*------------------------------------------------------------------------*
2190 * usb_bus_port_set_device
2192 * This function is NULL safe.
2193 *------------------------------------------------------------------------*/
2195 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2196 struct usb_device *udev, uint8_t device_index)
2203 * There is only one case where we don't
2204 * have an USB port, and that is the Root Hub!
2208 up->device_index = device_index;
2210 device_index = up->device_index;
2211 up->device_index = 0;
2215 * Make relationships to our new device
2217 if (device_index != 0) {
2219 mtx_lock(&usb_ref_lock);
2221 bus->devices[device_index] = udev;
2223 mtx_unlock(&usb_ref_lock);
2229 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2232 /*------------------------------------------------------------------------*
2235 * This functions is called when the USB event thread needs to run.
2236 *------------------------------------------------------------------------*/
2238 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2250 DPRINTF("No bus pointer!\n");
2253 if ((bus->devices == NULL) ||
2254 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2255 DPRINTF("No root HUB\n");
2258 if (mtx_owned(&bus->bus_mtx)) {
2267 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2268 &bus->explore_msg[0], &bus->explore_msg[1])) {
2272 USB_BUS_UNLOCK(bus);
2276 /*------------------------------------------------------------------------*
2277 * usb_needs_explore_all
2279 * This function is called whenever a new driver is loaded and will
2280 * cause that all USB buses are re-explored.
2281 *------------------------------------------------------------------------*/
2283 usb_needs_explore_all(void)
2285 struct usb_bus *bus;
2292 dc = usb_devclass_ptr;
2294 DPRINTFN(0, "no devclass\n");
2298 * Explore all USB buses in parallel.
2300 max = devclass_get_maxunit(dc);
2302 dev = devclass_get_device(dc, max);
2304 bus = device_get_softc(dev);
2306 usb_needs_explore(bus, 1);
2313 /*------------------------------------------------------------------------*
2314 * usb_needs_explore_init
2316 * This function will ensure that the USB controllers are not enumerated
2317 * until the "cold" variable is cleared.
2318 *------------------------------------------------------------------------*/
2320 usb_needs_explore_init(void *arg)
2323 * The cold variable should be cleared prior to this function
2327 usb_needs_explore_all();
2329 DPRINTFN(-1, "Cold variable is still set!\n");
2331 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2333 /*------------------------------------------------------------------------*
2334 * usb_bus_power_update
2336 * This function will ensure that all USB devices on the given bus are
2337 * properly suspended or resumed according to the device transfer
2339 *------------------------------------------------------------------------*/
2342 usb_bus_power_update(struct usb_bus *bus)
2344 usb_needs_explore(bus, 0 /* no probe */ );
2348 /*------------------------------------------------------------------------*
2349 * usbd_transfer_power_ref
2351 * This function will modify the power save reference counts and
2352 * wakeup the USB device associated with the given USB transfer, if
2354 *------------------------------------------------------------------------*/
2357 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2359 static const usb_power_mask_t power_mask[4] = {
2360 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2361 [UE_BULK] = USB_HW_POWER_BULK,
2362 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2363 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2365 struct usb_device *udev;
2366 uint8_t needs_explore;
2367 uint8_t needs_hw_power;
2370 udev = xfer->xroot->udev;
2372 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2373 /* no power save for root HUB */
2376 USB_BUS_LOCK(udev->bus);
2378 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2380 udev->pwr_save.last_xfer_time = ticks;
2381 udev->pwr_save.type_refs[xfer_type] += val;
2383 if (xfer->flags_int.control_xfr) {
2384 udev->pwr_save.read_refs += val;
2385 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2387 * It is not allowed to suspend during a
2390 udev->pwr_save.write_refs += val;
2392 } else if (USB_GET_DATA_ISREAD(xfer)) {
2393 udev->pwr_save.read_refs += val;
2395 udev->pwr_save.write_refs += val;
2399 if (udev->flags.self_suspended)
2400 needs_explore = usb_peer_should_wakeup(udev);
2404 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2405 DPRINTF("Adding type %u to power state\n", xfer_type);
2406 udev->bus->hw_power_state |= power_mask[xfer_type];
2416 USB_BUS_UNLOCK(udev->bus);
2418 if (needs_explore) {
2419 DPRINTF("update\n");
2420 usb_bus_power_update(udev->bus);
2421 } else if (needs_hw_power) {
2422 DPRINTF("needs power\n");
2423 if (udev->bus->methods->set_hw_power != NULL) {
2424 (udev->bus->methods->set_hw_power) (udev->bus);
2430 /*------------------------------------------------------------------------*
2431 * usb_peer_should_wakeup
2433 * This function returns non-zero if the current device should wake up.
2434 *------------------------------------------------------------------------*/
2436 usb_peer_should_wakeup(struct usb_device *udev)
2438 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2439 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2440 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2441 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2442 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2443 (udev->pwr_save.write_refs != 0) ||
2444 ((udev->pwr_save.read_refs != 0) &&
2445 (udev->flags.usb_mode == USB_MODE_HOST) &&
2446 (usb_peer_can_wakeup(udev) == 0)));
2449 /*------------------------------------------------------------------------*
2452 * This function implements the USB power daemon and is called
2453 * regularly from the USB explore thread.
2454 *------------------------------------------------------------------------*/
2457 usb_bus_powerd(struct usb_bus *bus)
2459 struct usb_device *udev;
2462 usb_ticks_t mintime;
2463 usb_size_t type_refs[5];
2466 limit = usb_power_timeout;
2469 else if (limit > 255)
2474 DPRINTF("bus=%p\n", bus);
2479 * The root HUB device is never suspended
2480 * and we simply skip it.
2482 for (x = USB_ROOT_HUB_ADDR + 1;
2483 x != bus->devices_max; x++) {
2485 udev = bus->devices[x];
2489 temp = ticks - udev->pwr_save.last_xfer_time;
2491 if (usb_peer_should_wakeup(udev)) {
2492 /* check if we are suspended */
2493 if (udev->flags.self_suspended != 0) {
2494 USB_BUS_UNLOCK(bus);
2495 usb_dev_resume_peer(udev);
2498 } else if ((temp >= limit) &&
2499 (udev->flags.usb_mode == USB_MODE_HOST) &&
2500 (udev->flags.self_suspended == 0)) {
2501 /* try to do suspend */
2503 USB_BUS_UNLOCK(bus);
2504 usb_dev_suspend_peer(udev);
2509 /* reset counters */
2511 mintime = (usb_ticks_t)-1;
2518 /* Re-loop all the devices to get the actual state */
2520 for (x = USB_ROOT_HUB_ADDR + 1;
2521 x != bus->devices_max; x++) {
2523 udev = bus->devices[x];
2527 /* we found a non-Root-Hub USB device */
2530 /* "last_xfer_time" can be updated by a resume */
2531 temp = ticks - udev->pwr_save.last_xfer_time;
2534 * Compute minimum time since last transfer for the complete
2540 if (udev->flags.self_suspended == 0) {
2541 type_refs[0] += udev->pwr_save.type_refs[0];
2542 type_refs[1] += udev->pwr_save.type_refs[1];
2543 type_refs[2] += udev->pwr_save.type_refs[2];
2544 type_refs[3] += udev->pwr_save.type_refs[3];
2548 if (mintime >= (usb_ticks_t)(1 * hz)) {
2549 /* recompute power masks */
2550 DPRINTF("Recomputing power masks\n");
2551 bus->hw_power_state = 0;
2552 if (type_refs[UE_CONTROL] != 0)
2553 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2554 if (type_refs[UE_BULK] != 0)
2555 bus->hw_power_state |= USB_HW_POWER_BULK;
2556 if (type_refs[UE_INTERRUPT] != 0)
2557 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2558 if (type_refs[UE_ISOCHRONOUS] != 0)
2559 bus->hw_power_state |= USB_HW_POWER_ISOC;
2560 if (type_refs[4] != 0)
2561 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2563 USB_BUS_UNLOCK(bus);
2565 if (bus->methods->set_hw_power != NULL) {
2566 /* always update hardware power! */
2567 (bus->methods->set_hw_power) (bus);
2573 /*------------------------------------------------------------------------*
2574 * usb_dev_resume_peer
2576 * This function will resume an USB peer and do the required USB
2577 * signalling to get an USB device out of the suspended state.
2578 *------------------------------------------------------------------------*/
2580 usb_dev_resume_peer(struct usb_device *udev)
2582 struct usb_bus *bus;
2589 /* check if already resumed */
2590 if (udev->flags.self_suspended == 0)
2593 /* we need a parent HUB to do resume */
2594 if (udev->parent_hub == NULL)
2597 DPRINTF("udev=%p\n", udev);
2599 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2600 (udev->flags.remote_wakeup == 0)) {
2602 * If the host did not set the remote wakeup feature, we can
2603 * not wake it up either!
2605 DPRINTF("remote wakeup is not set!\n");
2608 /* get bus pointer */
2611 /* resume parent hub first */
2612 usb_dev_resume_peer(udev->parent_hub);
2614 /* reduce chance of instant resume failure by waiting a little bit */
2615 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2617 if (usb_device_20_compatible(udev)) {
2618 /* resume current port (Valid in Host and Device Mode) */
2619 err = usbd_req_clear_port_feature(udev->parent_hub,
2620 NULL, udev->port_no, UHF_PORT_SUSPEND);
2622 DPRINTFN(0, "Resuming port failed\n");
2626 /* resume current port (Valid in Host and Device Mode) */
2627 err = usbd_req_set_port_link_state(udev->parent_hub,
2628 NULL, udev->port_no, UPS_PORT_LS_U0);
2630 DPRINTFN(0, "Resuming port failed\n");
2635 /* resume settle time */
2636 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2638 if (bus->methods->device_resume != NULL) {
2639 /* resume USB device on the USB controller */
2640 (bus->methods->device_resume) (udev);
2643 /* set that this device is now resumed */
2644 udev->flags.self_suspended = 0;
2646 /* make sure that we don't go into suspend right away */
2647 udev->pwr_save.last_xfer_time = ticks;
2649 /* make sure the needed power masks are on */
2650 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2651 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2652 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2653 bus->hw_power_state |= USB_HW_POWER_BULK;
2654 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2655 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2656 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2657 bus->hw_power_state |= USB_HW_POWER_ISOC;
2659 USB_BUS_UNLOCK(bus);
2661 if (bus->methods->set_hw_power != NULL) {
2662 /* always update hardware power! */
2663 (bus->methods->set_hw_power) (bus);
2668 /* notify all sub-devices about resume */
2669 err = usb_suspend_resume(udev, 0);
2671 usbd_sr_unlock(udev);
2673 /* check if peer has wakeup capability */
2674 if (usb_peer_can_wakeup(udev)) {
2675 /* clear remote wakeup */
2676 err = usbd_req_clear_device_feature(udev,
2677 NULL, UF_DEVICE_REMOTE_WAKEUP);
2679 DPRINTFN(0, "Clearing device "
2680 "remote wakeup failed: %s\n",
2686 /*------------------------------------------------------------------------*
2687 * usb_dev_suspend_peer
2689 * This function will suspend an USB peer and do the required USB
2690 * signalling to get an USB device into the suspended state.
2691 *------------------------------------------------------------------------*/
2693 usb_dev_suspend_peer(struct usb_device *udev)
2695 struct usb_device *child;
2705 /* check if already suspended */
2706 if (udev->flags.self_suspended)
2709 /* we need a parent HUB to do suspend */
2710 if (udev->parent_hub == NULL)
2713 DPRINTF("udev=%p\n", udev);
2715 /* check if the current device is a HUB */
2716 if (udev->hub != NULL) {
2717 nports = udev->hub->nports;
2719 /* check if all devices on the HUB are suspended */
2720 for (x = 0; x != nports; x++) {
2721 child = usb_bus_port_get_device(udev->bus,
2722 udev->hub->ports + x);
2727 if (child->flags.self_suspended)
2730 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2735 if (usb_peer_can_wakeup(udev)) {
2737 * This request needs to be done before we set
2738 * "udev->flags.self_suspended":
2741 /* allow device to do remote wakeup */
2742 err = usbd_req_set_device_feature(udev,
2743 NULL, UF_DEVICE_REMOTE_WAKEUP);
2745 DPRINTFN(0, "Setting device "
2746 "remote wakeup failed\n");
2750 USB_BUS_LOCK(udev->bus);
2752 * Checking for suspend condition and setting suspended bit
2755 err = usb_peer_should_wakeup(udev);
2758 * Set that this device is suspended. This variable
2759 * must be set before calling USB controller suspend
2762 udev->flags.self_suspended = 1;
2764 USB_BUS_UNLOCK(udev->bus);
2767 if (usb_peer_can_wakeup(udev)) {
2768 /* allow device to do remote wakeup */
2769 err = usbd_req_clear_device_feature(udev,
2770 NULL, UF_DEVICE_REMOTE_WAKEUP);
2772 DPRINTFN(0, "Setting device "
2773 "remote wakeup failed\n");
2777 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2778 /* resume parent HUB first */
2779 usb_dev_resume_peer(udev->parent_hub);
2781 /* reduce chance of instant resume failure by waiting a little bit */
2782 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2784 /* resume current port (Valid in Host and Device Mode) */
2785 err = usbd_req_clear_port_feature(udev->parent_hub,
2786 NULL, udev->port_no, UHF_PORT_SUSPEND);
2788 /* resume settle time */
2789 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2791 DPRINTF("Suspend was cancelled!\n");
2797 /* notify all sub-devices about suspend */
2798 err = usb_suspend_resume(udev, 1);
2800 usbd_sr_unlock(udev);
2802 if (udev->bus->methods->device_suspend != NULL) {
2805 /* suspend device on the USB controller */
2806 (udev->bus->methods->device_suspend) (udev);
2809 temp = usbd_get_dma_delay(udev);
2811 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2815 if (usb_device_20_compatible(udev)) {
2816 /* suspend current port */
2817 err = usbd_req_set_port_feature(udev->parent_hub,
2818 NULL, udev->port_no, UHF_PORT_SUSPEND);
2820 DPRINTFN(0, "Suspending port failed\n");
2824 /* suspend current port */
2825 err = usbd_req_set_port_link_state(udev->parent_hub,
2826 NULL, udev->port_no, UPS_PORT_LS_U3);
2828 DPRINTFN(0, "Suspending port failed\n");
2833 udev = udev->parent_hub;
2837 /*------------------------------------------------------------------------*
2838 * usbd_set_power_mode
2840 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2842 *------------------------------------------------------------------------*/
2844 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2846 /* filter input argument */
2847 if ((power_mode != USB_POWER_MODE_ON) &&
2848 (power_mode != USB_POWER_MODE_OFF))
2849 power_mode = USB_POWER_MODE_SAVE;
2851 power_mode = usbd_filter_power_mode(udev, power_mode);
2853 udev->power_mode = power_mode; /* update copy of power mode */
2856 usb_bus_power_update(udev->bus);
2858 usb_needs_explore(udev->bus, 0 /* no probe */ );
2862 /*------------------------------------------------------------------------*
2863 * usbd_filter_power_mode
2865 * This function filters the power mode based on hardware requirements.
2866 *------------------------------------------------------------------------*/
2868 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2870 const struct usb_bus_methods *mtod;
2873 mtod = udev->bus->methods;
2876 if (mtod->get_power_mode != NULL)
2877 (mtod->get_power_mode) (udev, &temp);
2879 /* check if we should not filter */
2881 return (power_mode);
2883 /* use fixed power mode given by hardware driver */
2887 /*------------------------------------------------------------------------*
2888 * usbd_start_re_enumerate
2890 * This function starts re-enumeration of the given USB device. This
2891 * function does not need to be called BUS-locked. This function does
2892 * not wait until the re-enumeration is completed.
2893 *------------------------------------------------------------------------*/
2895 usbd_start_re_enumerate(struct usb_device *udev)
2897 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2898 udev->re_enumerate_wait = USB_RE_ENUM_START;
2899 usb_needs_explore(udev->bus, 0);
2903 /*-----------------------------------------------------------------------*
2904 * usbd_start_set_config
2906 * This function starts setting a USB configuration. This function
2907 * does not need to be called BUS-locked. This function does not wait
2908 * until the set USB configuratino is completed.
2909 *------------------------------------------------------------------------*/
2911 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2913 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2914 if (udev->curr_config_index == index) {
2915 /* no change needed */
2918 udev->next_config_index = index;
2919 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2920 usb_needs_explore(udev->bus, 0);
2922 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2923 if (udev->next_config_index == index) {
2924 /* no change needed */
2928 return (USB_ERR_PENDING_REQUESTS);