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 #include <dev/usb/usb_hub_private.h>
81 static int uhub_debug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
85 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
90 static int usb_power_timeout = 30; /* seconds */
92 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
93 &usb_power_timeout, 0, "USB power timeout");
96 #if USB_HAVE_DISABLE_ENUM
97 static int usb_disable_enumeration = 0;
98 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
99 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
100 "This can secure against USB devices turning evil, "
101 "for example a USB memory stick becoming a USB keyboard.");
103 static int usb_disable_port_power = 0;
104 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
105 &usb_disable_port_power, 0, "Set to disable all USB port power.");
108 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
109 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
110 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
111 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
112 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
114 /* prototypes for type checking: */
116 static device_suspend_t uhub_suspend;
117 static device_resume_t uhub_resume;
119 static bus_driver_added_t uhub_driver_added;
120 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
122 static usb_callback_t uhub_intr_callback;
123 #if USB_HAVE_TT_SUPPORT
124 static usb_callback_t uhub_reset_tt_callback;
127 static void usb_dev_resume_peer(struct usb_device *udev);
128 static void usb_dev_suspend_peer(struct usb_device *udev);
129 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
131 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
132 [UHUB_INTR_TRANSFER] = {
133 .type = UE_INTERRUPT,
134 .endpoint = UE_ADDR_ANY,
135 .direction = UE_DIR_ANY,
137 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
138 .bufsize = 0, /* use wMaxPacketSize */
139 .callback = &uhub_intr_callback,
140 .interval = UHUB_INTR_INTERVAL,
142 #if USB_HAVE_TT_SUPPORT
143 [UHUB_RESET_TT_TRANSFER] = {
145 .endpoint = 0x00, /* Control pipe */
146 .direction = UE_DIR_ANY,
147 .bufsize = sizeof(struct usb_device_request),
148 .callback = &uhub_reset_tt_callback,
149 .timeout = 1000, /* 1 second */
150 .usb_mode = USB_MODE_HOST,
156 * driver instance for "hub" connected to "usb"
157 * and "hub" connected to "hub"
159 static devclass_t uhub_devclass;
161 static device_method_t uhub_methods[] = {
162 DEVMETHOD(device_probe, uhub_probe),
163 DEVMETHOD(device_attach, uhub_attach),
164 DEVMETHOD(device_detach, uhub_detach),
166 DEVMETHOD(device_suspend, uhub_suspend),
167 DEVMETHOD(device_resume, uhub_resume),
169 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
170 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
171 DEVMETHOD(bus_driver_added, uhub_driver_added),
175 driver_t uhub_driver = {
177 .methods = uhub_methods,
178 .size = sizeof(struct uhub_softc)
181 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
182 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
183 MODULE_VERSION(uhub, 1);
186 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
188 struct uhub_softc *sc = usbd_xfer_softc(xfer);
190 switch (USB_GET_STATE(xfer)) {
191 case USB_ST_TRANSFERRED:
194 * This is an indication that some port
195 * has changed status. Notify the bus
196 * event handler thread that we need
197 * to be explored again:
199 usb_needs_explore(sc->sc_udev->bus, 0);
202 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
203 usbd_transfer_submit(xfer);
207 if (xfer->error != USB_ERR_CANCELLED) {
209 * Do a clear-stall. The "stall_pipe" flag
210 * will get cleared before next callback by
213 usbd_xfer_set_stall(xfer);
214 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
215 usbd_transfer_submit(xfer);
221 /*------------------------------------------------------------------------*
224 * This function starts the TT reset USB request
225 *------------------------------------------------------------------------*/
226 #if USB_HAVE_TT_SUPPORT
228 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
230 struct usb_udev_msg *pm = (void *)_pm;
231 struct usb_device *udev = pm->udev;
233 struct uhub_softc *sc;
243 USB_BUS_UNLOCK(udev->bus);
244 USB_MTX_LOCK(&sc->sc_mtx);
246 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
248 USB_MTX_UNLOCK(&sc->sc_mtx);
249 USB_BUS_LOCK(udev->bus);
253 /*------------------------------------------------------------------------*
254 * uhub_tt_buffer_reset_async_locked
256 * This function queues a TT reset for the given USB device and endpoint.
257 *------------------------------------------------------------------------*/
258 #if USB_HAVE_TT_SUPPORT
260 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
262 struct usb_device_request req;
263 struct usb_device *udev;
269 if (child == NULL || ep == NULL)
272 udev = child->parent_hs_hub;
273 port = child->hs_port_no;
280 (udev->speed != USB_SPEED_HIGH) ||
281 (child->speed != USB_SPEED_LOW &&
282 child->speed != USB_SPEED_FULL) ||
283 (child->flags.usb_mode != USB_MODE_HOST) ||
284 (port == 0) || (ep->edesc == NULL)) {
289 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
291 up = hub->ports + port - 1;
293 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
294 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
297 /* if we already received a clear buffer request, reset the whole TT */
298 if (up->req_reset_tt.bRequest != 0) {
299 req.bmRequestType = UT_WRITE_CLASS_OTHER;
300 req.bRequest = UR_RESET_TT;
301 USETW(req.wValue, 0);
302 req.wIndex[0] = port;
304 USETW(req.wLength, 0);
306 wValue = (ep->edesc->bEndpointAddress & 0xF) |
307 ((child->address & 0x7F) << 4) |
308 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
309 ((ep->edesc->bmAttributes & 3) << 12);
311 req.bmRequestType = UT_WRITE_CLASS_OTHER;
312 req.bRequest = UR_CLEAR_TT_BUFFER;
313 USETW(req.wValue, wValue);
314 req.wIndex[0] = port;
316 USETW(req.wLength, 0);
318 up->req_reset_tt = req;
319 /* get reset transfer started */
320 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
321 &hub->tt_msg[0], &hub->tt_msg[1]);
325 #if USB_HAVE_TT_SUPPORT
327 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
329 struct uhub_softc *sc;
330 struct usb_device *udev;
334 DPRINTF("TT buffer reset\n");
336 sc = usbd_xfer_softc(xfer);
339 switch (USB_GET_STATE(xfer)) {
340 case USB_ST_TRANSFERRED:
343 USB_BUS_LOCK(udev->bus);
344 /* find first port which needs a TT reset */
345 for (x = 0; x != udev->hub->nports; x++) {
346 up = udev->hub->ports + x;
348 if (up->req_reset_tt.bRequest == 0)
351 /* copy in the transfer */
352 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
353 sizeof(up->req_reset_tt));
355 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
358 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
360 USB_BUS_UNLOCK(udev->bus);
362 usbd_transfer_submit(xfer);
365 USB_BUS_UNLOCK(udev->bus);
369 if (error == USB_ERR_CANCELLED)
372 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
378 /*------------------------------------------------------------------------*
379 * uhub_count_active_host_ports
381 * This function counts the number of active ports at the given speed.
382 *------------------------------------------------------------------------*/
384 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
386 struct uhub_softc *sc;
387 struct usb_device *child;
402 for (x = 0; x != hub->nports; x++) {
404 child = usb_bus_port_get_device(udev->bus, up);
406 child->flags.usb_mode == USB_MODE_HOST &&
407 child->speed == speed)
415 uhub_explore_handle_re_enumerate(struct usb_device *child)
420 /* check if device should be re-enumerated */
421 if (child->flags.usb_mode != USB_MODE_HOST)
424 do_unlock = usbd_enum_lock(child);
425 switch (child->re_enumerate_wait) {
426 case USB_RE_ENUM_START:
427 err = usbd_set_config_index(child,
430 DPRINTF("Unconfigure failed: %s: Ignored.\n",
433 if (child->parent_hub == NULL) {
434 /* the root HUB cannot be re-enumerated */
435 DPRINTFN(6, "cannot reset root HUB\n");
438 err = usbd_req_re_enumerate(child, NULL);
441 /* refresh device strings */
442 usb_get_langid(child);
443 usb_set_device_strings(child);
445 /* set default configuration */
446 err = usbd_set_config_index(child, 0);
449 err = usb_probe_and_attach(child,
450 USB_IFACE_INDEX_ANY);
452 child->re_enumerate_wait = USB_RE_ENUM_DONE;
455 case USB_RE_ENUM_PWR_OFF:
456 /* get the device unconfigured */
457 err = usbd_set_config_index(child,
460 DPRINTFN(0, "Could not unconfigure "
461 "device (ignored)\n");
463 if (child->parent_hub == NULL) {
464 /* the root HUB cannot be re-enumerated */
465 DPRINTFN(6, "cannot set port feature\n");
468 /* clear port enable */
469 err = usbd_req_clear_port_feature(child->parent_hub,
470 NULL, child->port_no, UHF_PORT_ENABLE);
472 DPRINTFN(0, "Could not disable port "
476 child->re_enumerate_wait = USB_RE_ENUM_DONE;
479 case USB_RE_ENUM_SET_CONFIG:
480 err = usbd_set_config_index(child,
481 child->next_config_index);
483 DPRINTF("Configure failed: %s: Ignored.\n",
486 err = usb_probe_and_attach(child,
487 USB_IFACE_INDEX_ANY);
489 child->re_enumerate_wait = USB_RE_ENUM_DONE;
493 child->re_enumerate_wait = USB_RE_ENUM_DONE;
497 usbd_enum_unlock(child);
500 /*------------------------------------------------------------------------*
501 * uhub_explore_sub - subroutine
505 * Else: A control transaction failed
506 *------------------------------------------------------------------------*/
508 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
511 struct usb_device *child;
515 bus = sc->sc_udev->bus;
516 err = USB_ERR_NORMAL_COMPLETION;
518 /* get driver added refcount from USB bus */
519 refcount = bus->driver_added_refcount;
521 /* get device assosiated with the given port */
522 child = usb_bus_port_get_device(bus, up);
528 uhub_explore_handle_re_enumerate(child);
530 /* check if probe and attach should be done */
532 if (child->driver_added_refcount != refcount) {
533 child->driver_added_refcount = refcount;
534 err = usb_probe_and_attach(child,
535 USB_IFACE_INDEX_ANY);
540 /* start control transfer, if device mode */
542 if (child->flags.usb_mode == USB_MODE_DEVICE)
543 usbd_ctrl_transfer_setup(child);
545 /* if a HUB becomes present, do a recursive HUB explore */
548 err = (child->hub->explore) (child);
554 /*------------------------------------------------------------------------*
555 * uhub_read_port_status - factored out code
556 *------------------------------------------------------------------------*/
558 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
560 struct usb_port_status ps;
563 if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
564 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
565 sc->sc_st.port_status = 0;
566 sc->sc_st.port_change = 0;
567 return (USB_ERR_TIMEOUT);
570 err = usbd_req_get_port_status(
571 sc->sc_udev, NULL, &ps, portno);
574 sc->sc_st.port_status = UGETW(ps.wPortStatus);
575 sc->sc_st.port_change = UGETW(ps.wPortChange);
576 sc->sc_usb_port_errors = 0;
578 sc->sc_st.port_status = 0;
579 sc->sc_st.port_change = 0;
580 sc->sc_usb_port_errors++;
583 /* debugging print */
585 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
586 "wPortChange=0x%04x, err=%s\n",
587 portno, sc->sc_st.port_status,
588 sc->sc_st.port_change, usbd_errstr(err));
592 /*------------------------------------------------------------------------*
597 * Else: A control transaction failed
598 *------------------------------------------------------------------------*/
600 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
602 struct usb_device *child;
603 struct usb_device *udev;
604 enum usb_dev_speed speed;
605 enum usb_hc_mode mode;
610 DPRINTF("reattaching port %d\n", portno);
614 child = usb_bus_port_get_device(udev->bus,
615 udev->hub->ports + portno - 1);
619 /* first clear the port connection change bit */
621 err = usbd_req_clear_port_feature(udev, NULL,
622 portno, UHF_C_PORT_CONNECTION);
627 /* check if there is a child */
631 * Free USB device and all subdevices, if any.
633 usb_free_device(child, 0);
636 /* get fresh status */
638 err = uhub_read_port_status(sc, portno);
642 #if USB_HAVE_DISABLE_ENUM
643 /* check if we should skip enumeration from this USB HUB */
644 if (usb_disable_enumeration != 0 ||
645 sc->sc_disable_enumeration != 0) {
646 DPRINTF("Enumeration is disabled!\n");
650 /* check if nothing is connected to the port */
652 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
655 /* check if there is no power on the port and print a warning */
657 switch (udev->speed) {
661 power_mask = UPS_PORT_POWER;
663 case USB_SPEED_SUPER:
664 if (udev->parent_hub == NULL)
665 power_mask = 0; /* XXX undefined */
667 power_mask = UPS_PORT_POWER_SS;
673 if ((sc->sc_st.port_status & power_mask) != power_mask) {
674 DPRINTF("WARNING: strange, connected port %d "
675 "has no power\n", portno);
678 /* check if the device is in Host Mode */
680 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
681 DPRINTF("Port %d is in Host Mode\n", portno);
683 if (sc->sc_st.port_status & UPS_SUSPEND) {
685 * NOTE: Should not get here in SuperSpeed
686 * mode, because the HUB should report this
689 DPRINTF("Port %d was still "
690 "suspended, clearing.\n", portno);
691 err = usbd_req_clear_port_feature(udev,
692 NULL, portno, UHF_PORT_SUSPEND);
697 /* wait for maximum device power up time */
700 USB_MS_TO_TICKS(usb_port_powerup_delay));
702 /* reset port, which implies enabling it */
704 err = usbd_req_reset_port(udev, NULL, portno);
707 DPRINTFN(0, "port %d reset "
708 "failed, error=%s\n",
709 portno, usbd_errstr(err));
712 /* get port status again, it might have changed during reset */
714 err = uhub_read_port_status(sc, portno);
718 /* check if something changed during port reset */
720 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
721 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
723 DPRINTFN(0, "giving up port %d reset - "
724 "device vanished: change %#x status %#x\n",
725 portno, sc->sc_st.port_change,
726 sc->sc_st.port_status);
733 DPRINTF("Port %d is in Device Mode\n", portno);
737 * Figure out the device speed
739 switch (udev->speed) {
741 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
742 speed = USB_SPEED_HIGH;
743 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
744 speed = USB_SPEED_LOW;
746 speed = USB_SPEED_FULL;
749 if (sc->sc_st.port_status & UPS_LOW_SPEED)
750 speed = USB_SPEED_LOW;
752 speed = USB_SPEED_FULL;
755 speed = USB_SPEED_LOW;
757 case USB_SPEED_SUPER:
758 if (udev->parent_hub == NULL) {
759 /* Root HUB - special case */
760 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
762 speed = USB_SPEED_FULL;
765 speed = USB_SPEED_LOW;
768 speed = USB_SPEED_HIGH;
771 speed = USB_SPEED_SUPER;
775 speed = USB_SPEED_SUPER;
779 /* same speed like parent */
783 if (speed == USB_SPEED_SUPER) {
784 err = usbd_req_set_hub_u1_timeout(udev, NULL,
785 portno, 128 - (2 * udev->depth));
787 DPRINTFN(0, "port %d U1 timeout "
788 "failed, error=%s\n",
789 portno, usbd_errstr(err));
791 err = usbd_req_set_hub_u2_timeout(udev, NULL,
792 portno, 128 - (2 * udev->depth));
794 DPRINTFN(0, "port %d U2 timeout "
795 "failed, error=%s\n",
796 portno, usbd_errstr(err));
801 * Figure out the device mode
803 * NOTE: This part is currently FreeBSD specific.
805 if (udev->parent_hub != NULL) {
806 /* inherit mode from the parent HUB */
807 mode = udev->parent_hub->flags.usb_mode;
808 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
809 mode = USB_MODE_DEVICE;
811 mode = USB_MODE_HOST;
813 /* need to create a new child */
814 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
815 udev->depth + 1, portno - 1, portno, speed, mode);
817 DPRINTFN(0, "could not allocate new device\n");
820 return (0); /* success */
825 * Free USB device and all subdevices, if any.
827 usb_free_device(child, 0);
831 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
832 err = usbd_req_clear_port_feature(
834 portno, UHF_PORT_ENABLE);
838 DPRINTFN(0, "device problem (%s), "
839 "disabling port %d\n", usbd_errstr(err), portno);
844 /*------------------------------------------------------------------------*
845 * usb_device_20_compatible
848 * 0: HUB does not support suspend and resume
849 * Else: HUB supports suspend and resume
850 *------------------------------------------------------------------------*/
852 usb_device_20_compatible(struct usb_device *udev)
856 switch (udev->speed) {
866 /*------------------------------------------------------------------------*
867 * uhub_suspend_resume_port
871 * Else: A control transaction failed
872 *------------------------------------------------------------------------*/
874 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
876 struct usb_device *child;
877 struct usb_device *udev;
881 DPRINTF("port %d\n", portno);
884 child = usb_bus_port_get_device(udev->bus,
885 udev->hub->ports + portno - 1);
887 /* first clear the port suspend change bit */
889 if (usb_device_20_compatible(udev)) {
890 err = usbd_req_clear_port_feature(udev, NULL,
891 portno, UHF_C_PORT_SUSPEND);
893 err = usbd_req_clear_port_feature(udev, NULL,
894 portno, UHF_C_PORT_LINK_STATE);
898 DPRINTF("clearing suspend failed.\n");
901 /* get fresh status */
903 err = uhub_read_port_status(sc, portno);
905 DPRINTF("reading port status failed.\n");
908 /* convert current state */
910 if (usb_device_20_compatible(udev)) {
911 if (sc->sc_st.port_status & UPS_SUSPEND) {
917 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
921 case UPS_PORT_LS_SS_INA:
922 usbd_req_warm_reset_port(udev, NULL, portno);
931 DPRINTF("suspended=%u\n", is_suspend);
933 /* do the suspend or resume */
937 * This code handle two cases: 1) Host Mode - we can only
938 * receive resume here 2) Device Mode - we can receive
939 * suspend and resume here
942 usb_dev_resume_peer(child);
943 else if (child->flags.usb_mode == USB_MODE_DEVICE)
944 usb_dev_suspend_peer(child);
950 /*------------------------------------------------------------------------*
951 * uhub_root_interrupt
953 * This function is called when a Root HUB interrupt has
954 * happened. "ptr" and "len" makes up the Root HUB interrupt
955 * packet. This function is called having the "bus_mtx" locked.
956 *------------------------------------------------------------------------*/
958 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
960 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
962 usb_needs_explore(bus, 0);
966 uhub_is_too_deep(struct usb_device *udev)
968 switch (udev->speed) {
972 if (udev->depth > USB_HUB_MAX_DEPTH)
975 case USB_SPEED_SUPER:
976 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
985 /*------------------------------------------------------------------------*
991 *------------------------------------------------------------------------*/
993 uhub_explore(struct usb_device *udev)
996 struct uhub_softc *sc;
1006 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1008 /* ignore devices that are too deep */
1009 if (uhub_is_too_deep(udev))
1010 return (USB_ERR_TOO_DEEP);
1012 /* check if device is suspended */
1013 if (udev->flags.self_suspended) {
1014 /* need to wait until the child signals resume */
1015 DPRINTF("Device is suspended!\n");
1016 return (USB_ERR_NORMAL_COMPLETION);
1020 * Make sure we don't race against user-space applications
1023 do_unlock = usbd_enum_lock(udev);
1026 * Set default error code to avoid compiler warnings.
1027 * Note that hub->nports cannot be zero.
1029 err = USB_ERR_NORMAL_COMPLETION;
1031 for (x = 0; x != hub->nports; x++) {
1032 up = hub->ports + x;
1035 err = uhub_read_port_status(sc, portno);
1037 /* most likely the HUB is gone */
1040 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1041 DPRINTF("Overcurrent on port %u.\n", portno);
1042 err = usbd_req_clear_port_feature(
1043 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1045 /* most likely the HUB is gone */
1049 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1051 * Fake a connect status change so that the
1052 * status gets checked initially!
1054 sc->sc_st.port_change |=
1055 UPS_C_CONNECT_STATUS;
1057 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1058 err = usbd_req_clear_port_feature(
1059 udev, NULL, portno, UHF_C_PORT_ENABLE);
1061 /* most likely the HUB is gone */
1064 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1066 * Ignore the port error if the device
1069 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1070 DPRINTFN(0, "illegal enable change, "
1071 "port %d\n", portno);
1073 if (up->restartcnt == USB_RESTART_MAX) {
1074 /* XXX could try another speed ? */
1075 DPRINTFN(0, "port error, giving up "
1076 "port %d\n", portno);
1078 sc->sc_st.port_change |=
1079 UPS_C_CONNECT_STATUS;
1084 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1085 err = uhub_reattach_port(sc, portno);
1087 /* most likely the HUB is gone */
1091 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1092 UPS_C_PORT_LINK_STATE)) {
1093 err = uhub_suspend_resume_port(sc, portno);
1095 /* most likely the HUB is gone */
1100 if (uhub_explore_sub(sc, up) == USB_ERR_NORMAL_COMPLETION) {
1101 /* explore succeeded - reset restart counter */
1107 usbd_enum_unlock(udev);
1109 /* initial status checked */
1110 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1116 uhub_probe(device_t dev)
1118 struct usb_attach_arg *uaa = device_get_ivars(dev);
1120 if (uaa->usb_mode != USB_MODE_HOST)
1124 * The subclass for USB HUBs is currently ignored because it
1125 * is 0 for some and 1 for others.
1127 if (uaa->info.bConfigIndex == 0 &&
1128 uaa->info.bDeviceClass == UDCLASS_HUB)
1129 return (BUS_PROBE_DEFAULT);
1134 /* NOTE: The information returned by this function can be wrong. */
1136 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1138 struct usb_hub_descriptor hubdesc20;
1139 struct usb_hub_ss_descriptor hubdesc30;
1144 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1145 return (USB_ERR_INVAL);
1150 switch (udev->speed) {
1152 case USB_SPEED_FULL:
1153 case USB_SPEED_HIGH:
1154 /* assuming that there is one port */
1155 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1157 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1158 "error=%s\n", usbd_errstr(err));
1161 nports = hubdesc20.bNbrPorts;
1165 if (udev->speed == USB_SPEED_HIGH)
1166 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1169 case USB_SPEED_SUPER:
1170 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1172 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1173 "error=%s\n", usbd_errstr(err));
1176 nports = hubdesc30.bNbrPorts;
1182 err = USB_ERR_INVAL;
1186 if (pnports != NULL)
1196 uhub_attach(device_t dev)
1198 struct uhub_softc *sc = device_get_softc(dev);
1199 struct usb_attach_arg *uaa = device_get_ivars(dev);
1200 struct usb_device *udev = uaa->device;
1201 struct usb_device *parent_hub = udev->parent_hub;
1202 struct usb_hub *hub;
1203 struct usb_hub_descriptor hubdesc20;
1204 struct usb_hub_ss_descriptor hubdesc30;
1205 #if USB_HAVE_DISABLE_ENUM
1206 struct sysctl_ctx_list *sysctl_ctx;
1207 struct sysctl_oid *sysctl_tree;
1214 uint8_t iface_index;
1220 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1222 device_set_usb_desc(dev);
1224 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1225 "parent->selfpowered=%d\n",
1227 udev->flags.self_powered,
1230 parent_hub->flags.self_powered : 0);
1232 if (uhub_is_too_deep(udev)) {
1233 DPRINTFN(0, "HUB at depth %d, "
1234 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1238 if (!udev->flags.self_powered && parent_hub &&
1239 !parent_hub->flags.self_powered) {
1240 DPRINTFN(0, "Bus powered HUB connected to "
1241 "bus powered HUB. HUB ignored\n");
1245 if (UHUB_IS_MULTI_TT(sc)) {
1246 err = usbd_set_alt_interface_index(udev, 0, 1);
1248 device_printf(dev, "MTT could not be enabled\n");
1251 device_printf(dev, "MTT enabled\n");
1254 /* get HUB descriptor */
1256 DPRINTFN(2, "Getting HUB descriptor\n");
1258 switch (udev->speed) {
1260 case USB_SPEED_FULL:
1261 case USB_SPEED_HIGH:
1262 /* assuming that there is one port */
1263 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1265 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1266 "error=%s\n", usbd_errstr(err));
1269 /* get number of ports */
1270 nports = hubdesc20.bNbrPorts;
1272 /* get power delay */
1273 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1274 usb_extra_power_up_time);
1276 /* get complete HUB descriptor */
1278 /* check number of ports */
1280 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1281 "error=%s\n", usbd_errstr(err));
1284 /* get complete HUB descriptor */
1285 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1288 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1289 "error=%s\n", usbd_errstr(err));
1292 if (hubdesc20.bNbrPorts != nports) {
1293 DPRINTFN(0, "Number of ports changed\n");
1298 case USB_SPEED_SUPER:
1299 if (udev->parent_hub != NULL) {
1300 err = usbd_req_set_hub_depth(udev, NULL,
1303 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1304 "error=%s\n", usbd_errstr(err));
1308 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1310 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1311 "error=%s\n", usbd_errstr(err));
1314 /* get number of ports */
1315 nports = hubdesc30.bNbrPorts;
1317 /* get power delay */
1318 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1319 usb_extra_power_up_time);
1321 /* get complete HUB descriptor */
1323 /* check number of ports */
1324 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1325 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1326 "error=%s\n", usbd_errstr(err));
1329 /* get complete HUB descriptor */
1330 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1333 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1334 "error=%s\n", usbd_errstr(err));
1337 if (hubdesc30.bNbrPorts != nports) {
1338 DPRINTFN(0, "Number of ports changed\n");
1344 DPRINTF("Assuming HUB has only one port\n");
1345 /* default number of ports */
1347 /* default power delay */
1348 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1352 DPRINTFN(0, "portless HUB\n");
1355 if (nports > USB_MAX_PORTS) {
1356 DPRINTF("Port limit exceeded\n");
1359 #if (USB_HAVE_FIXED_PORT == 0)
1360 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1361 M_USBDEV, M_WAITOK | M_ZERO);
1370 /* initialize HUB structure */
1372 hub->explore = &uhub_explore;
1373 hub->nports = nports;
1374 hub->hubudev = udev;
1375 #if USB_HAVE_TT_SUPPORT
1376 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1377 hub->tt_msg[0].udev = udev;
1378 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1379 hub->tt_msg[1].udev = udev;
1381 /* if self powered hub, give ports maximum current */
1382 if (udev->flags.self_powered) {
1383 hub->portpower = USB_MAX_POWER;
1385 hub->portpower = USB_MIN_POWER;
1388 /* set up interrupt pipe */
1390 if (udev->parent_hub == NULL) {
1391 /* root HUB is special */
1395 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1396 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1399 DPRINTFN(0, "cannot setup interrupt transfer, "
1400 "errstr=%s\n", usbd_errstr(err));
1403 /* wait with power off for a while */
1404 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1406 #if USB_HAVE_DISABLE_ENUM
1407 /* Add device sysctls */
1409 sysctl_ctx = device_get_sysctl_ctx(dev);
1410 sysctl_tree = device_get_sysctl_tree(dev);
1412 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1413 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1414 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1415 &sc->sc_disable_enumeration, 0,
1416 "Set to disable enumeration on this USB HUB.");
1418 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1419 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1420 &sc->sc_disable_port_power, 0,
1421 "Set to disable USB port power on this USB HUB.");
1425 * To have the best chance of success we do things in the exact same
1426 * order as Windoze98. This should not be necessary, but some
1427 * devices do not follow the USB specs to the letter.
1429 * These are the events on the bus when a hub is attached:
1430 * Get device and config descriptors (see attach code)
1431 * Get hub descriptor (see above)
1434 * wait for power to become stable
1435 * (all below happens in explore code)
1437 * clear C_PORT_CONNECTION
1440 * if device connected
1444 * clear C_PORT_RESET
1446 * proceed with device attachment
1449 /* XXX should check for none, individual, or ganged power? */
1453 for (x = 0; x != nports; x++) {
1454 /* set up data structures */
1455 struct usb_port *up = hub->ports + x;
1457 up->device_index = 0;
1461 /* check if port is removable */
1462 switch (udev->speed) {
1464 case USB_SPEED_FULL:
1465 case USB_SPEED_HIGH:
1466 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1469 case USB_SPEED_SUPER:
1470 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1474 DPRINTF("Assuming removable port\n");
1479 #if USB_HAVE_DISABLE_ENUM
1480 /* check if we should disable USB port power or not */
1481 if (usb_disable_port_power != 0 ||
1482 sc->sc_disable_port_power != 0) {
1483 /* turn the power off */
1484 DPRINTFN(2, "Turning port %d power off\n", portno);
1485 err = usbd_req_clear_port_feature(udev, NULL,
1486 portno, UHF_PORT_POWER);
1489 /* turn the power on */
1490 DPRINTFN(2, "Turning port %d power on\n", portno);
1491 err = usbd_req_set_port_feature(udev, NULL,
1492 portno, UHF_PORT_POWER);
1493 #if USB_HAVE_DISABLE_ENUM
1498 DPRINTFN(0, "port %d power on or off failed, %s\n",
1499 portno, usbd_errstr(err));
1501 DPRINTF("turn on port %d power\n",
1504 /* wait for stable power */
1505 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1508 device_printf(dev, "%d port%s with %d "
1509 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1510 removable, udev->flags.self_powered ? "self" : "bus");
1512 /* Start the interrupt endpoint, if any */
1514 USB_MTX_LOCK(&sc->sc_mtx);
1515 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1516 USB_MTX_UNLOCK(&sc->sc_mtx);
1518 /* Enable automatic power save on all USB HUBs */
1520 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1525 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1527 #if (USB_HAVE_FIXED_PORT == 0)
1528 free(udev->hub, M_USBDEV);
1532 mtx_destroy(&sc->sc_mtx);
1538 * Called from process context when the hub is gone.
1539 * Detach all devices on active ports.
1542 uhub_detach(device_t dev)
1544 struct uhub_softc *sc = device_get_softc(dev);
1545 struct usb_hub *hub = sc->sc_udev->hub;
1546 struct usb_bus *bus = sc->sc_udev->bus;
1547 struct usb_device *child;
1550 if (hub == NULL) /* must be partially working */
1553 /* Make sure interrupt transfer is gone. */
1554 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1556 /* Detach all ports */
1557 for (x = 0; x != hub->nports; x++) {
1558 child = usb_bus_port_get_device(bus, hub->ports + x);
1560 if (child == NULL) {
1565 * Free USB device and all subdevices, if any.
1567 usb_free_device(child, 0);
1570 #if USB_HAVE_TT_SUPPORT
1571 /* Make sure our TT messages are not queued anywhere */
1573 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1574 &hub->tt_msg[0], &hub->tt_msg[1]);
1575 USB_BUS_UNLOCK(bus);
1578 #if (USB_HAVE_FIXED_PORT == 0)
1579 free(hub, M_USBDEV);
1581 sc->sc_udev->hub = NULL;
1583 mtx_destroy(&sc->sc_mtx);
1589 uhub_suspend(device_t dev)
1592 /* Sub-devices are not suspended here! */
1597 uhub_resume(device_t dev)
1600 /* Sub-devices are not resumed here! */
1605 uhub_driver_added(device_t dev, driver_t *driver)
1607 usb_needs_explore_all();
1611 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1612 struct hub_result *res)
1614 struct usb_interface *iface;
1615 struct usb_device *udev;
1620 nports = hub->nports;
1621 for (x = 0; x != nports; x++) {
1622 udev = usb_bus_port_get_device(hub->hubudev->bus,
1627 for (i = 0; i != USB_IFACE_MAX; i++) {
1628 iface = usbd_get_iface(udev, i);
1630 (iface->subdev == child)) {
1631 res->iface_index = i;
1633 res->portno = x + 1;
1638 res->iface_index = 0;
1644 uhub_child_location_string(device_t parent, device_t child,
1645 char *buf, size_t buflen)
1647 struct uhub_softc *sc;
1648 struct usb_hub *hub;
1649 struct hub_result res;
1651 if (!device_is_attached(parent)) {
1657 sc = device_get_softc(parent);
1658 hub = sc->sc_udev->hub;
1661 uhub_find_iface_index(hub, child, &res);
1663 DPRINTF("device not on hub\n");
1669 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1674 , device_get_unit(res.udev->bus->bdev)
1675 , (res.udev->parent_hub != NULL) ?
1676 res.udev->parent_hub->device_index : 0
1677 , res.portno, res.udev->device_index, res.iface_index
1679 , res.udev->ugen_name
1689 uhub_child_pnpinfo_string(device_t parent, device_t child,
1690 char *buf, size_t buflen)
1692 struct uhub_softc *sc;
1693 struct usb_hub *hub;
1694 struct usb_interface *iface;
1695 struct hub_result res;
1698 if (!device_is_attached(parent)) {
1704 sc = device_get_softc(parent);
1705 hub = sc->sc_udev->hub;
1708 uhub_find_iface_index(hub, child, &res);
1710 DPRINTF("device not on hub\n");
1716 iface = usbd_get_iface(res.udev, res.iface_index);
1717 if (iface && iface->idesc) {
1718 /* Make sure device information is not changed during the print. */
1719 do_unlock = usbd_ctrl_lock(res.udev);
1721 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1722 "devclass=0x%02x devsubclass=0x%02x "
1727 "intclass=0x%02x intsubclass=0x%02x "
1728 "intprotocol=0x%02x" "%s%s",
1729 UGETW(res.udev->ddesc.idVendor),
1730 UGETW(res.udev->ddesc.idProduct),
1731 res.udev->ddesc.bDeviceClass,
1732 res.udev->ddesc.bDeviceSubClass,
1733 res.udev->ddesc.bDeviceProtocol,
1734 usb_get_serial(res.udev),
1735 UGETW(res.udev->ddesc.bcdDevice),
1736 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1737 iface->idesc->bInterfaceClass,
1738 iface->idesc->bInterfaceSubClass,
1739 iface->idesc->bInterfaceProtocol,
1740 iface->pnpinfo ? " " : "",
1741 iface->pnpinfo ? iface->pnpinfo : "");
1744 usbd_ctrl_unlock(res.udev);
1758 * The USB Transaction Translator:
1759 * ===============================
1761 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1762 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1763 * USB transfers. To utilize bandwidth dynamically the "scatter and
1764 * gather" principle must be applied. This means that bandwidth must
1765 * be divided into equal parts of bandwidth. With regard to USB all
1766 * data is transferred in smaller packets with length
1767 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1770 * The bandwidth scheduler which I have implemented will simply pack
1771 * the USB transfers back to back until there is no more space in the
1772 * schedule. Out of the 8 microframes which the USB 2.0 standard
1773 * provides, only 6 are available for non-HIGH-speed devices. I have
1774 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1775 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1776 * this division, it is very difficult to allocate and free bandwidth
1779 * NOTE about the Transaction Translator in USB HUBs:
1781 * USB HUBs have a very simple Transaction Translator, that will
1782 * simply pipeline all the SPLIT transactions. That means that the
1783 * transactions will be executed in the order they are queued!
1787 /*------------------------------------------------------------------------*
1788 * usb_intr_find_best_slot
1791 * The best Transaction Translation slot for an interrupt endpoint.
1792 *------------------------------------------------------------------------*/
1794 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1795 uint8_t end, uint8_t mask)
1797 usb_size_t min = (usb_size_t)-1;
1805 /* find the last slot with lesser used bandwidth */
1807 for (x = start; x < end; x++) {
1810 /* compute sum of bandwidth */
1811 for (z = x; z < end; z++) {
1812 if (mask & (1U << (z - x)))
1816 /* check if the current multi-slot is more optimal */
1822 /* check if the mask is about to be shifted out */
1823 if (mask & (1U << (end - 1 - x)))
1829 /*------------------------------------------------------------------------*
1830 * usb_hs_bandwidth_adjust
1832 * This function will update the bandwidth usage for the microframe
1833 * having index "slot" by "len" bytes. "len" can be negative. If the
1834 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1835 * the "slot" argument will be replaced by the slot having least used
1836 * bandwidth. The "mask" argument is used for multi-slot allocations.
1839 * The slot in which the bandwidth update was done: 0..7
1840 *------------------------------------------------------------------------*/
1842 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1843 uint8_t slot, uint8_t mask)
1845 struct usb_bus *bus = udev->bus;
1846 struct usb_hub *hub;
1847 enum usb_dev_speed speed;
1850 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1852 speed = usbd_get_speed(udev);
1856 case USB_SPEED_FULL:
1857 if (speed == USB_SPEED_LOW) {
1861 * The Host Controller Driver should have
1862 * performed checks so that the lookup
1863 * below does not result in a NULL pointer
1867 hub = udev->parent_hs_hub->hub;
1868 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1869 slot = usb_intr_find_best_slot(hub->uframe_usage,
1870 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1872 for (x = slot; x < 8; x++) {
1873 if (mask & (1U << (x - slot))) {
1874 hub->uframe_usage[x] += len;
1875 bus->uframe_usage[x] += len;
1880 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1881 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1882 USB_HS_MICRO_FRAMES_MAX, mask);
1884 for (x = slot; x < 8; x++) {
1885 if (mask & (1U << (x - slot))) {
1886 bus->uframe_usage[x] += len;
1894 /*------------------------------------------------------------------------*
1895 * usb_hs_bandwidth_alloc
1897 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1898 *------------------------------------------------------------------------*/
1900 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1902 struct usb_device *udev;
1907 udev = xfer->xroot->udev;
1909 if (udev->flags.usb_mode != USB_MODE_HOST)
1910 return; /* not supported */
1912 xfer->endpoint->refcount_bw++;
1913 if (xfer->endpoint->refcount_bw != 1)
1914 return; /* already allocated */
1916 speed = usbd_get_speed(udev);
1918 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1920 /* allocate a microframe slot */
1923 slot = usb_hs_bandwidth_adjust(udev,
1924 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1926 xfer->endpoint->usb_uframe = slot;
1927 xfer->endpoint->usb_smask = mask << slot;
1929 if ((speed != USB_SPEED_FULL) &&
1930 (speed != USB_SPEED_LOW)) {
1931 xfer->endpoint->usb_cmask = 0x00 ;
1933 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1937 case UE_ISOCHRONOUS:
1938 switch (usbd_xfer_get_fps_shift(xfer)) {
1953 /* allocate a microframe multi-slot */
1955 slot = usb_hs_bandwidth_adjust(udev,
1956 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1958 xfer->endpoint->usb_uframe = slot;
1959 xfer->endpoint->usb_cmask = 0;
1960 xfer->endpoint->usb_smask = mask << slot;
1964 xfer->endpoint->usb_uframe = 0;
1965 xfer->endpoint->usb_cmask = 0;
1966 xfer->endpoint->usb_smask = 0;
1970 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1971 xfer->endpoint->usb_uframe,
1972 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1975 /*------------------------------------------------------------------------*
1976 * usb_hs_bandwidth_free
1978 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1979 *------------------------------------------------------------------------*/
1981 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1983 struct usb_device *udev;
1987 udev = xfer->xroot->udev;
1989 if (udev->flags.usb_mode != USB_MODE_HOST)
1990 return; /* not supported */
1992 xfer->endpoint->refcount_bw--;
1993 if (xfer->endpoint->refcount_bw != 0)
1994 return; /* still allocated */
1996 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1998 case UE_ISOCHRONOUS:
2000 slot = xfer->endpoint->usb_uframe;
2001 mask = xfer->endpoint->usb_smask;
2003 /* free microframe slot(s): */
2004 usb_hs_bandwidth_adjust(udev,
2005 -xfer->max_frame_size, slot, mask >> slot);
2007 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2008 slot, mask >> slot);
2010 xfer->endpoint->usb_uframe = 0;
2011 xfer->endpoint->usb_cmask = 0;
2012 xfer->endpoint->usb_smask = 0;
2020 /*------------------------------------------------------------------------*
2021 * usb_isoc_time_expand
2023 * This function will expand the time counter from 7-bit to 16-bit.
2026 * 16-bit isochronous time counter.
2027 *------------------------------------------------------------------------*/
2029 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2033 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2035 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2037 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2039 if (isoc_time_curr < rem) {
2040 /* the time counter wrapped around */
2041 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2043 /* update the remainder */
2045 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2046 bus->isoc_time_last |= isoc_time_curr;
2048 return (bus->isoc_time_last);
2051 /*------------------------------------------------------------------------*
2052 * usbd_fs_isoc_schedule_alloc_slot
2054 * This function will allocate bandwidth for an isochronous FULL speed
2055 * transaction in the FULL speed schedule.
2060 *------------------------------------------------------------------------*/
2061 #if USB_HAVE_TT_SUPPORT
2063 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2065 struct usb_xfer *xfer;
2066 struct usb_xfer *pipe_xfer;
2067 struct usb_bus *bus;
2069 usb_frlength_t data_len;
2077 bus = isoc_xfer->xroot->bus;
2079 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2080 /* skip self, if any */
2082 if (xfer == isoc_xfer)
2085 /* check if this USB transfer is going through the same TT */
2087 if (xfer->xroot->udev->parent_hs_hub !=
2088 isoc_xfer->xroot->udev->parent_hs_hub) {
2091 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2092 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2093 (xfer->xroot->udev->hs_port_no !=
2094 isoc_xfer->xroot->udev->hs_port_no)) {
2097 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2100 /* check if isoc_time is part of this transfer */
2102 delta = xfer->isoc_time_complete - isoc_time;
2103 if (delta > 0 && delta <= xfer->nframes) {
2104 delta = xfer->nframes - delta;
2106 len = xfer->frlengths[delta];
2115 * Check double buffered transfers. Only stream ID
2116 * equal to zero is valid here!
2118 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2120 /* skip self, if any */
2122 if (pipe_xfer == isoc_xfer)
2125 /* check if isoc_time is part of this transfer */
2127 delta = pipe_xfer->isoc_time_complete - isoc_time;
2128 if (delta > 0 && delta <= pipe_xfer->nframes) {
2129 delta = pipe_xfer->nframes - delta;
2131 len = pipe_xfer->frlengths[delta];
2141 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2142 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2146 /* check for overflow */
2148 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2153 delta = isoc_xfer->isoc_time_complete - isoc_time;
2154 if (delta > 0 && delta <= isoc_xfer->nframes) {
2155 delta = isoc_xfer->nframes - delta;
2157 len = isoc_xfer->frlengths[delta];
2165 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2166 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2170 /* check for overflow */
2172 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2179 /*------------------------------------------------------------------------*
2180 * usb_bus_port_get_device
2182 * This function is NULL safe.
2183 *------------------------------------------------------------------------*/
2185 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2187 if ((bus == NULL) || (up == NULL)) {
2191 if (up->device_index == 0) {
2195 return (bus->devices[up->device_index]);
2198 /*------------------------------------------------------------------------*
2199 * usb_bus_port_set_device
2201 * This function is NULL safe.
2202 *------------------------------------------------------------------------*/
2204 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2205 struct usb_device *udev, uint8_t device_index)
2212 * There is only one case where we don't
2213 * have an USB port, and that is the Root Hub!
2217 up->device_index = device_index;
2219 device_index = up->device_index;
2220 up->device_index = 0;
2224 * Make relationships to our new device
2226 if (device_index != 0) {
2228 mtx_lock(&usb_ref_lock);
2230 bus->devices[device_index] = udev;
2232 mtx_unlock(&usb_ref_lock);
2238 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2241 /*------------------------------------------------------------------------*
2244 * This functions is called when the USB event thread needs to run.
2245 *------------------------------------------------------------------------*/
2247 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2259 DPRINTF("No bus pointer!\n");
2262 if ((bus->devices == NULL) ||
2263 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2264 DPRINTF("No root HUB\n");
2267 if (mtx_owned(&bus->bus_mtx)) {
2276 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2277 &bus->explore_msg[0], &bus->explore_msg[1])) {
2281 USB_BUS_UNLOCK(bus);
2285 /*------------------------------------------------------------------------*
2286 * usb_needs_explore_all
2288 * This function is called whenever a new driver is loaded and will
2289 * cause that all USB buses are re-explored.
2290 *------------------------------------------------------------------------*/
2292 usb_needs_explore_all(void)
2294 struct usb_bus *bus;
2301 dc = usb_devclass_ptr;
2303 DPRINTFN(0, "no devclass\n");
2307 * Explore all USB buses in parallel.
2309 max = devclass_get_maxunit(dc);
2311 dev = devclass_get_device(dc, max);
2313 bus = device_get_softc(dev);
2315 usb_needs_explore(bus, 1);
2322 /*------------------------------------------------------------------------*
2323 * usb_needs_explore_init
2325 * This function will ensure that the USB controllers are not enumerated
2326 * until the "cold" variable is cleared.
2327 *------------------------------------------------------------------------*/
2329 usb_needs_explore_init(void *arg)
2332 * The cold variable should be cleared prior to this function
2336 usb_needs_explore_all();
2338 DPRINTFN(-1, "Cold variable is still set!\n");
2340 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2342 /*------------------------------------------------------------------------*
2343 * usb_bus_power_update
2345 * This function will ensure that all USB devices on the given bus are
2346 * properly suspended or resumed according to the device transfer
2348 *------------------------------------------------------------------------*/
2351 usb_bus_power_update(struct usb_bus *bus)
2353 usb_needs_explore(bus, 0 /* no probe */ );
2357 /*------------------------------------------------------------------------*
2358 * usbd_transfer_power_ref
2360 * This function will modify the power save reference counts and
2361 * wakeup the USB device associated with the given USB transfer, if
2363 *------------------------------------------------------------------------*/
2366 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2368 static const usb_power_mask_t power_mask[4] = {
2369 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2370 [UE_BULK] = USB_HW_POWER_BULK,
2371 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2372 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2374 struct usb_device *udev;
2375 uint8_t needs_explore;
2376 uint8_t needs_hw_power;
2379 udev = xfer->xroot->udev;
2381 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2382 /* no power save for root HUB */
2385 USB_BUS_LOCK(udev->bus);
2387 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2389 udev->pwr_save.last_xfer_time = ticks;
2390 udev->pwr_save.type_refs[xfer_type] += val;
2392 if (xfer->flags_int.control_xfr) {
2393 udev->pwr_save.read_refs += val;
2394 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2396 * It is not allowed to suspend during a
2399 udev->pwr_save.write_refs += val;
2401 } else if (USB_GET_DATA_ISREAD(xfer)) {
2402 udev->pwr_save.read_refs += val;
2404 udev->pwr_save.write_refs += val;
2408 if (udev->flags.self_suspended)
2409 needs_explore = usb_peer_should_wakeup(udev);
2413 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2414 DPRINTF("Adding type %u to power state\n", xfer_type);
2415 udev->bus->hw_power_state |= power_mask[xfer_type];
2425 USB_BUS_UNLOCK(udev->bus);
2427 if (needs_explore) {
2428 DPRINTF("update\n");
2429 usb_bus_power_update(udev->bus);
2430 } else if (needs_hw_power) {
2431 DPRINTF("needs power\n");
2432 if (udev->bus->methods->set_hw_power != NULL) {
2433 (udev->bus->methods->set_hw_power) (udev->bus);
2439 /*------------------------------------------------------------------------*
2440 * usb_peer_should_wakeup
2442 * This function returns non-zero if the current device should wake up.
2443 *------------------------------------------------------------------------*/
2445 usb_peer_should_wakeup(struct usb_device *udev)
2447 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2448 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2449 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2450 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2451 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2452 (udev->pwr_save.write_refs != 0) ||
2453 ((udev->pwr_save.read_refs != 0) &&
2454 (udev->flags.usb_mode == USB_MODE_HOST) &&
2455 (usb_peer_can_wakeup(udev) == 0)));
2458 /*------------------------------------------------------------------------*
2461 * This function implements the USB power daemon and is called
2462 * regularly from the USB explore thread.
2463 *------------------------------------------------------------------------*/
2466 usb_bus_powerd(struct usb_bus *bus)
2468 struct usb_device *udev;
2471 usb_ticks_t mintime;
2472 usb_size_t type_refs[5];
2475 limit = usb_power_timeout;
2478 else if (limit > 255)
2483 DPRINTF("bus=%p\n", bus);
2488 * The root HUB device is never suspended
2489 * and we simply skip it.
2491 for (x = USB_ROOT_HUB_ADDR + 1;
2492 x != bus->devices_max; x++) {
2493 udev = bus->devices[x];
2497 temp = ticks - udev->pwr_save.last_xfer_time;
2499 if (usb_peer_should_wakeup(udev)) {
2500 /* check if we are suspended */
2501 if (udev->flags.self_suspended != 0) {
2502 USB_BUS_UNLOCK(bus);
2503 usb_dev_resume_peer(udev);
2506 } else if ((temp >= limit) &&
2507 (udev->flags.usb_mode == USB_MODE_HOST) &&
2508 (udev->flags.self_suspended == 0)) {
2509 /* try to do suspend */
2511 USB_BUS_UNLOCK(bus);
2512 usb_dev_suspend_peer(udev);
2517 /* reset counters */
2519 mintime = (usb_ticks_t)-1;
2526 /* Re-loop all the devices to get the actual state */
2528 for (x = USB_ROOT_HUB_ADDR + 1;
2529 x != bus->devices_max; x++) {
2530 udev = bus->devices[x];
2534 /* we found a non-Root-Hub USB device */
2537 /* "last_xfer_time" can be updated by a resume */
2538 temp = ticks - udev->pwr_save.last_xfer_time;
2541 * Compute minimum time since last transfer for the complete
2547 if (udev->flags.self_suspended == 0) {
2548 type_refs[0] += udev->pwr_save.type_refs[0];
2549 type_refs[1] += udev->pwr_save.type_refs[1];
2550 type_refs[2] += udev->pwr_save.type_refs[2];
2551 type_refs[3] += udev->pwr_save.type_refs[3];
2555 if (mintime >= (usb_ticks_t)(1 * hz)) {
2556 /* recompute power masks */
2557 DPRINTF("Recomputing power masks\n");
2558 bus->hw_power_state = 0;
2559 if (type_refs[UE_CONTROL] != 0)
2560 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2561 if (type_refs[UE_BULK] != 0)
2562 bus->hw_power_state |= USB_HW_POWER_BULK;
2563 if (type_refs[UE_INTERRUPT] != 0)
2564 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2565 if (type_refs[UE_ISOCHRONOUS] != 0)
2566 bus->hw_power_state |= USB_HW_POWER_ISOC;
2567 if (type_refs[4] != 0)
2568 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2570 USB_BUS_UNLOCK(bus);
2572 if (bus->methods->set_hw_power != NULL) {
2573 /* always update hardware power! */
2574 (bus->methods->set_hw_power) (bus);
2581 usbd_device_30_remote_wakeup(struct usb_device *udev, uint8_t bRequest)
2583 struct usb_device_request req = {};
2585 req.bmRequestType = UT_WRITE_INTERFACE;
2586 req.bRequest = bRequest;
2587 USETW(req.wValue, USB_INTERFACE_FUNC_SUSPEND);
2588 USETW(req.wIndex, USB_INTERFACE_FUNC_SUSPEND_LP |
2589 USB_INTERFACE_FUNC_SUSPEND_RW);
2591 return (usbd_do_request(udev, NULL, &req, 0));
2595 usbd_clear_dev_wakeup(struct usb_device *udev)
2599 if (usb_device_20_compatible(udev)) {
2600 err = usbd_req_clear_device_feature(udev,
2601 NULL, UF_DEVICE_REMOTE_WAKEUP);
2603 err = usbd_device_30_remote_wakeup(udev,
2610 usbd_set_dev_wakeup(struct usb_device *udev)
2614 if (usb_device_20_compatible(udev)) {
2615 err = usbd_req_set_device_feature(udev,
2616 NULL, UF_DEVICE_REMOTE_WAKEUP);
2618 err = usbd_device_30_remote_wakeup(udev,
2624 /*------------------------------------------------------------------------*
2625 * usb_dev_resume_peer
2627 * This function will resume an USB peer and do the required USB
2628 * signalling to get an USB device out of the suspended state.
2629 *------------------------------------------------------------------------*/
2631 usb_dev_resume_peer(struct usb_device *udev)
2633 struct usb_bus *bus;
2640 /* check if already resumed */
2641 if (udev->flags.self_suspended == 0)
2644 /* we need a parent HUB to do resume */
2645 if (udev->parent_hub == NULL)
2648 DPRINTF("udev=%p\n", udev);
2650 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2651 (udev->flags.remote_wakeup == 0)) {
2653 * If the host did not set the remote wakeup feature, we can
2654 * not wake it up either!
2656 DPRINTF("remote wakeup is not set!\n");
2659 /* get bus pointer */
2662 /* resume parent hub first */
2663 usb_dev_resume_peer(udev->parent_hub);
2665 /* reduce chance of instant resume failure by waiting a little bit */
2666 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2668 if (usb_device_20_compatible(udev)) {
2669 /* resume current port (Valid in Host and Device Mode) */
2670 err = usbd_req_clear_port_feature(udev->parent_hub,
2671 NULL, udev->port_no, UHF_PORT_SUSPEND);
2673 DPRINTFN(0, "Resuming port failed\n");
2677 /* resume current port (Valid in Host and Device Mode) */
2678 err = usbd_req_set_port_link_state(udev->parent_hub,
2679 NULL, udev->port_no, UPS_PORT_LS_U0);
2681 DPRINTFN(0, "Resuming port failed\n");
2686 /* resume settle time */
2687 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2689 if (bus->methods->device_resume != NULL) {
2690 /* resume USB device on the USB controller */
2691 (bus->methods->device_resume) (udev);
2694 /* set that this device is now resumed */
2695 udev->flags.self_suspended = 0;
2697 /* make sure that we don't go into suspend right away */
2698 udev->pwr_save.last_xfer_time = ticks;
2700 /* make sure the needed power masks are on */
2701 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2702 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2703 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2704 bus->hw_power_state |= USB_HW_POWER_BULK;
2705 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2706 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2707 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2708 bus->hw_power_state |= USB_HW_POWER_ISOC;
2710 USB_BUS_UNLOCK(bus);
2712 if (bus->methods->set_hw_power != NULL) {
2713 /* always update hardware power! */
2714 (bus->methods->set_hw_power) (bus);
2719 /* notify all sub-devices about resume */
2720 err = usb_suspend_resume(udev, 0);
2722 usbd_sr_unlock(udev);
2724 /* check if peer has wakeup capability */
2725 if (usb_peer_can_wakeup(udev)) {
2726 /* clear remote wakeup */
2727 err = usbd_clear_dev_wakeup(udev);
2729 DPRINTFN(0, "Clearing device "
2730 "remote wakeup failed: %s\n",
2736 /*------------------------------------------------------------------------*
2737 * usb_dev_suspend_peer
2739 * This function will suspend an USB peer and do the required USB
2740 * signalling to get an USB device into the suspended state.
2741 *------------------------------------------------------------------------*/
2743 usb_dev_suspend_peer(struct usb_device *udev)
2745 struct usb_device *child;
2755 /* check if already suspended */
2756 if (udev->flags.self_suspended)
2759 /* we need a parent HUB to do suspend */
2760 if (udev->parent_hub == NULL)
2763 DPRINTF("udev=%p\n", udev);
2765 /* check if the current device is a HUB */
2766 if (udev->hub != NULL) {
2767 nports = udev->hub->nports;
2769 /* check if all devices on the HUB are suspended */
2770 for (x = 0; x != nports; x++) {
2771 child = usb_bus_port_get_device(udev->bus,
2772 udev->hub->ports + x);
2777 if (child->flags.self_suspended)
2780 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2785 if (usb_peer_can_wakeup(udev)) {
2787 * This request needs to be done before we set
2788 * "udev->flags.self_suspended":
2791 /* allow device to do remote wakeup */
2792 err = usbd_set_dev_wakeup(udev);
2794 DPRINTFN(0, "Setting device "
2795 "remote wakeup failed\n");
2799 USB_BUS_LOCK(udev->bus);
2801 * Checking for suspend condition and setting suspended bit
2804 err = usb_peer_should_wakeup(udev);
2807 * Set that this device is suspended. This variable
2808 * must be set before calling USB controller suspend
2811 udev->flags.self_suspended = 1;
2813 USB_BUS_UNLOCK(udev->bus);
2816 if (usb_peer_can_wakeup(udev)) {
2817 /* allow device to do remote wakeup */
2818 err = usbd_clear_dev_wakeup(udev);
2820 DPRINTFN(0, "Setting device "
2821 "remote wakeup failed\n");
2825 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2826 /* resume parent HUB first */
2827 usb_dev_resume_peer(udev->parent_hub);
2829 /* reduce chance of instant resume failure by waiting a little bit */
2830 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2832 /* resume current port (Valid in Host and Device Mode) */
2833 err = usbd_req_clear_port_feature(udev->parent_hub,
2834 NULL, udev->port_no, UHF_PORT_SUSPEND);
2836 /* resume settle time */
2837 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2839 DPRINTF("Suspend was cancelled!\n");
2845 /* notify all sub-devices about suspend */
2846 err = usb_suspend_resume(udev, 1);
2848 usbd_sr_unlock(udev);
2850 if (udev->bus->methods->device_suspend != NULL) {
2853 /* suspend device on the USB controller */
2854 (udev->bus->methods->device_suspend) (udev);
2857 temp = usbd_get_dma_delay(udev);
2859 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2862 if (usb_device_20_compatible(udev)) {
2863 /* suspend current port */
2864 err = usbd_req_set_port_feature(udev->parent_hub,
2865 NULL, udev->port_no, UHF_PORT_SUSPEND);
2867 DPRINTFN(0, "Suspending port failed\n");
2871 /* suspend current port */
2872 err = usbd_req_set_port_link_state(udev->parent_hub,
2873 NULL, udev->port_no, UPS_PORT_LS_U3);
2875 DPRINTFN(0, "Suspending port failed\n");
2880 udev = udev->parent_hub;
2884 /*------------------------------------------------------------------------*
2885 * usbd_set_power_mode
2887 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2889 *------------------------------------------------------------------------*/
2891 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2893 /* filter input argument */
2894 if ((power_mode != USB_POWER_MODE_ON) &&
2895 (power_mode != USB_POWER_MODE_OFF))
2896 power_mode = USB_POWER_MODE_SAVE;
2898 power_mode = usbd_filter_power_mode(udev, power_mode);
2900 udev->power_mode = power_mode; /* update copy of power mode */
2903 usb_bus_power_update(udev->bus);
2905 usb_needs_explore(udev->bus, 0 /* no probe */ );
2909 /*------------------------------------------------------------------------*
2910 * usbd_filter_power_mode
2912 * This function filters the power mode based on hardware requirements.
2913 *------------------------------------------------------------------------*/
2915 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2917 const struct usb_bus_methods *mtod;
2920 mtod = udev->bus->methods;
2923 if (mtod->get_power_mode != NULL)
2924 (mtod->get_power_mode) (udev, &temp);
2926 /* check if we should not filter */
2928 return (power_mode);
2930 /* use fixed power mode given by hardware driver */
2934 /*------------------------------------------------------------------------*
2935 * usbd_start_re_enumerate
2937 * This function starts re-enumeration of the given USB device. This
2938 * function does not need to be called BUS-locked. This function does
2939 * not wait until the re-enumeration is completed.
2940 *------------------------------------------------------------------------*/
2942 usbd_start_re_enumerate(struct usb_device *udev)
2944 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2945 udev->re_enumerate_wait = USB_RE_ENUM_START;
2946 usb_needs_explore(udev->bus, 0);
2950 /*-----------------------------------------------------------------------*
2951 * usbd_start_set_config
2953 * This function starts setting a USB configuration. This function
2954 * does not need to be called BUS-locked. This function does not wait
2955 * until the set USB configuratino is completed.
2956 *------------------------------------------------------------------------*/
2958 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2960 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2961 if (udev->curr_config_index == index) {
2962 /* no change needed */
2965 udev->next_config_index = index;
2966 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2967 usb_needs_explore(udev->bus, 0);
2969 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2970 if (udev->next_config_index == index) {
2971 /* no change needed */
2975 return (USB_ERR_PENDING_REQUESTS);