3 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
35 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
61 #define USB_DEBUG_VAR uhub_debug
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_device.h>
66 #include <dev/usb/usb_request.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_hub.h>
69 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_transfer.h>
72 #include <dev/usb/usb_dynamic.h>
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #endif /* USB_GLOBAL_INCLUDE_FILE */
78 #define UHUB_INTR_INTERVAL 250 /* ms */
81 #if USB_HAVE_TT_SUPPORT
82 UHUB_RESET_TT_TRANSFER,
88 static int uhub_debug = 0;
90 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
91 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
96 static int usb_power_timeout = 30; /* seconds */
98 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
99 &usb_power_timeout, 0, "USB power timeout");
102 #if USB_HAVE_DISABLE_ENUM
103 static int usb_disable_enumeration = 0;
104 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
105 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
106 "This can secure against USB devices turning evil, "
107 "for example a USB memory stick becoming a USB keyboard.");
109 static int usb_disable_port_power = 0;
110 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
111 &usb_disable_port_power, 0, "Set to disable all USB port power.");
114 struct uhub_current_state {
115 uint16_t port_change;
116 uint16_t port_status;
120 struct uhub_current_state sc_st;/* current state */
121 #if (USB_HAVE_FIXED_PORT != 0)
122 struct usb_hub sc_hub;
124 device_t sc_dev; /* base device */
125 struct mtx sc_mtx; /* our mutex */
126 struct usb_device *sc_udev; /* USB device */
127 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
128 #if USB_HAVE_DISABLE_ENUM
129 int sc_disable_enumeration;
130 int sc_disable_port_power;
132 uint8_t sc_usb_port_errors; /* error counter */
133 #define UHUB_USB_PORT_ERRORS_MAX 4
135 #define UHUB_FLAG_DID_EXPLORE 0x01
138 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
139 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
140 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
141 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
142 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
144 /* prototypes for type checking: */
146 static device_probe_t uhub_probe;
147 static device_attach_t uhub_attach;
148 static device_detach_t uhub_detach;
149 static device_suspend_t uhub_suspend;
150 static device_resume_t uhub_resume;
152 static bus_driver_added_t uhub_driver_added;
153 static bus_child_location_str_t uhub_child_location_string;
154 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
156 static usb_callback_t uhub_intr_callback;
157 #if USB_HAVE_TT_SUPPORT
158 static usb_callback_t uhub_reset_tt_callback;
161 static void usb_dev_resume_peer(struct usb_device *udev);
162 static void usb_dev_suspend_peer(struct usb_device *udev);
163 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
165 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
167 [UHUB_INTR_TRANSFER] = {
168 .type = UE_INTERRUPT,
169 .endpoint = UE_ADDR_ANY,
170 .direction = UE_DIR_ANY,
172 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
173 .bufsize = 0, /* use wMaxPacketSize */
174 .callback = &uhub_intr_callback,
175 .interval = UHUB_INTR_INTERVAL,
177 #if USB_HAVE_TT_SUPPORT
178 [UHUB_RESET_TT_TRANSFER] = {
180 .endpoint = 0x00, /* Control pipe */
181 .direction = UE_DIR_ANY,
182 .bufsize = sizeof(struct usb_device_request),
183 .callback = &uhub_reset_tt_callback,
184 .timeout = 1000, /* 1 second */
185 .usb_mode = USB_MODE_HOST,
191 * driver instance for "hub" connected to "usb"
192 * and "hub" connected to "hub"
194 static devclass_t uhub_devclass;
196 static device_method_t uhub_methods[] = {
197 DEVMETHOD(device_probe, uhub_probe),
198 DEVMETHOD(device_attach, uhub_attach),
199 DEVMETHOD(device_detach, uhub_detach),
201 DEVMETHOD(device_suspend, uhub_suspend),
202 DEVMETHOD(device_resume, uhub_resume),
204 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
205 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
206 DEVMETHOD(bus_driver_added, uhub_driver_added),
210 static driver_t uhub_driver = {
212 .methods = uhub_methods,
213 .size = sizeof(struct uhub_softc)
216 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
217 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
218 MODULE_VERSION(uhub, 1);
221 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
223 struct uhub_softc *sc = usbd_xfer_softc(xfer);
225 switch (USB_GET_STATE(xfer)) {
226 case USB_ST_TRANSFERRED:
229 * This is an indication that some port
230 * has changed status. Notify the bus
231 * event handler thread that we need
232 * to be explored again:
234 usb_needs_explore(sc->sc_udev->bus, 0);
237 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
238 usbd_transfer_submit(xfer);
242 if (xfer->error != USB_ERR_CANCELLED) {
244 * Do a clear-stall. The "stall_pipe" flag
245 * will get cleared before next callback by
248 usbd_xfer_set_stall(xfer);
249 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
250 usbd_transfer_submit(xfer);
256 /*------------------------------------------------------------------------*
259 * This function starts the TT reset USB request
260 *------------------------------------------------------------------------*/
261 #if USB_HAVE_TT_SUPPORT
263 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
265 struct usb_udev_msg *pm = (void *)_pm;
266 struct usb_device *udev = pm->udev;
268 struct uhub_softc *sc;
278 USB_BUS_UNLOCK(udev->bus);
279 USB_MTX_LOCK(&sc->sc_mtx);
281 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
283 USB_MTX_UNLOCK(&sc->sc_mtx);
284 USB_BUS_LOCK(udev->bus);
288 /*------------------------------------------------------------------------*
289 * uhub_tt_buffer_reset_async_locked
291 * This function queues a TT reset for the given USB device and endpoint.
292 *------------------------------------------------------------------------*/
293 #if USB_HAVE_TT_SUPPORT
295 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
297 struct usb_device_request req;
298 struct usb_device *udev;
304 if (child == NULL || ep == NULL)
307 udev = child->parent_hs_hub;
308 port = child->hs_port_no;
315 (udev->speed != USB_SPEED_HIGH) ||
316 (child->speed != USB_SPEED_LOW &&
317 child->speed != USB_SPEED_FULL) ||
318 (child->flags.usb_mode != USB_MODE_HOST) ||
319 (port == 0) || (ep->edesc == NULL)) {
324 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
326 up = hub->ports + port - 1;
328 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
329 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
332 /* if we already received a clear buffer request, reset the whole TT */
333 if (up->req_reset_tt.bRequest != 0) {
334 req.bmRequestType = UT_WRITE_CLASS_OTHER;
335 req.bRequest = UR_RESET_TT;
336 USETW(req.wValue, 0);
337 req.wIndex[0] = port;
339 USETW(req.wLength, 0);
341 wValue = (ep->edesc->bEndpointAddress & 0xF) |
342 ((child->address & 0x7F) << 4) |
343 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
344 ((ep->edesc->bmAttributes & 3) << 12);
346 req.bmRequestType = UT_WRITE_CLASS_OTHER;
347 req.bRequest = UR_CLEAR_TT_BUFFER;
348 USETW(req.wValue, wValue);
349 req.wIndex[0] = port;
351 USETW(req.wLength, 0);
353 up->req_reset_tt = req;
354 /* get reset transfer started */
355 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
356 &hub->tt_msg[0], &hub->tt_msg[1]);
360 #if USB_HAVE_TT_SUPPORT
362 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
364 struct uhub_softc *sc;
365 struct usb_device *udev;
369 DPRINTF("TT buffer reset\n");
371 sc = usbd_xfer_softc(xfer);
374 switch (USB_GET_STATE(xfer)) {
375 case USB_ST_TRANSFERRED:
378 USB_BUS_LOCK(udev->bus);
379 /* find first port which needs a TT reset */
380 for (x = 0; x != udev->hub->nports; x++) {
381 up = udev->hub->ports + x;
383 if (up->req_reset_tt.bRequest == 0)
386 /* copy in the transfer */
387 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
388 sizeof(up->req_reset_tt));
390 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
393 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
395 USB_BUS_UNLOCK(udev->bus);
397 usbd_transfer_submit(xfer);
400 USB_BUS_UNLOCK(udev->bus);
404 if (error == USB_ERR_CANCELLED)
407 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
413 /*------------------------------------------------------------------------*
414 * uhub_count_active_host_ports
416 * This function counts the number of active ports at the given speed.
417 *------------------------------------------------------------------------*/
419 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
421 struct uhub_softc *sc;
422 struct usb_device *child;
437 for (x = 0; x != hub->nports; x++) {
439 child = usb_bus_port_get_device(udev->bus, up);
441 child->flags.usb_mode == USB_MODE_HOST &&
442 child->speed == speed)
450 uhub_explore_handle_re_enumerate(struct usb_device *child)
455 /* check if device should be re-enumerated */
456 if (child->flags.usb_mode != USB_MODE_HOST)
459 do_unlock = usbd_enum_lock(child);
460 switch (child->re_enumerate_wait) {
461 case USB_RE_ENUM_START:
462 err = usbd_set_config_index(child,
465 DPRINTF("Unconfigure failed: %s: Ignored.\n",
468 if (child->parent_hub == NULL) {
469 /* the root HUB cannot be re-enumerated */
470 DPRINTFN(6, "cannot reset root HUB\n");
473 err = usbd_req_re_enumerate(child, NULL);
476 /* refresh device strings */
477 usb_get_langid(child);
478 usb_set_device_strings(child);
480 /* set default configuration */
481 err = usbd_set_config_index(child, 0);
484 err = usb_probe_and_attach(child,
485 USB_IFACE_INDEX_ANY);
487 child->re_enumerate_wait = USB_RE_ENUM_DONE;
490 case USB_RE_ENUM_PWR_OFF:
491 /* get the device unconfigured */
492 err = usbd_set_config_index(child,
495 DPRINTFN(0, "Could not unconfigure "
496 "device (ignored)\n");
498 if (child->parent_hub == NULL) {
499 /* the root HUB cannot be re-enumerated */
500 DPRINTFN(6, "cannot set port feature\n");
503 /* clear port enable */
504 err = usbd_req_clear_port_feature(child->parent_hub,
505 NULL, child->port_no, UHF_PORT_ENABLE);
507 DPRINTFN(0, "Could not disable port "
511 child->re_enumerate_wait = USB_RE_ENUM_DONE;
514 case USB_RE_ENUM_SET_CONFIG:
515 err = usbd_set_config_index(child,
516 child->next_config_index);
518 DPRINTF("Configure failed: %s: Ignored.\n",
521 err = usb_probe_and_attach(child,
522 USB_IFACE_INDEX_ANY);
524 child->re_enumerate_wait = USB_RE_ENUM_DONE;
528 child->re_enumerate_wait = USB_RE_ENUM_DONE;
532 usbd_enum_unlock(child);
535 /*------------------------------------------------------------------------*
536 * uhub_explore_sub - subroutine
540 * Else: A control transaction failed
541 *------------------------------------------------------------------------*/
543 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
546 struct usb_device *child;
550 bus = sc->sc_udev->bus;
553 /* get driver added refcount from USB bus */
554 refcount = bus->driver_added_refcount;
556 /* get device assosiated with the given port */
557 child = usb_bus_port_get_device(bus, up);
563 uhub_explore_handle_re_enumerate(child);
565 /* check if probe and attach should be done */
567 if (child->driver_added_refcount != refcount) {
568 child->driver_added_refcount = refcount;
569 err = usb_probe_and_attach(child,
570 USB_IFACE_INDEX_ANY);
575 /* start control transfer, if device mode */
577 if (child->flags.usb_mode == USB_MODE_DEVICE)
578 usbd_ctrl_transfer_setup(child);
580 /* if a HUB becomes present, do a recursive HUB explore */
583 err = (child->hub->explore) (child);
589 /*------------------------------------------------------------------------*
590 * uhub_read_port_status - factored out code
591 *------------------------------------------------------------------------*/
593 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
595 struct usb_port_status ps;
598 if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
599 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
600 sc->sc_st.port_status = 0;
601 sc->sc_st.port_change = 0;
602 return (USB_ERR_TIMEOUT);
605 err = usbd_req_get_port_status(
606 sc->sc_udev, NULL, &ps, portno);
609 sc->sc_st.port_status = UGETW(ps.wPortStatus);
610 sc->sc_st.port_change = UGETW(ps.wPortChange);
611 sc->sc_usb_port_errors = 0;
613 sc->sc_st.port_status = 0;
614 sc->sc_st.port_change = 0;
615 sc->sc_usb_port_errors++;
618 /* debugging print */
620 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
621 "wPortChange=0x%04x, err=%s\n",
622 portno, sc->sc_st.port_status,
623 sc->sc_st.port_change, usbd_errstr(err));
627 /*------------------------------------------------------------------------*
632 * Else: A control transaction failed
633 *------------------------------------------------------------------------*/
635 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
637 struct usb_device *child;
638 struct usb_device *udev;
639 enum usb_dev_speed speed;
640 enum usb_hc_mode mode;
645 DPRINTF("reattaching port %d\n", portno);
649 child = usb_bus_port_get_device(udev->bus,
650 udev->hub->ports + portno - 1);
654 /* first clear the port connection change bit */
656 err = usbd_req_clear_port_feature(udev, NULL,
657 portno, UHF_C_PORT_CONNECTION);
662 /* check if there is a child */
666 * Free USB device and all subdevices, if any.
668 usb_free_device(child, 0);
671 /* get fresh status */
673 err = uhub_read_port_status(sc, portno);
677 #if USB_HAVE_DISABLE_ENUM
678 /* check if we should skip enumeration from this USB HUB */
679 if (usb_disable_enumeration != 0 ||
680 sc->sc_disable_enumeration != 0) {
681 DPRINTF("Enumeration is disabled!\n");
685 /* check if nothing is connected to the port */
687 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
690 /* check if there is no power on the port and print a warning */
692 switch (udev->speed) {
696 power_mask = UPS_PORT_POWER;
698 case USB_SPEED_SUPER:
699 if (udev->parent_hub == NULL)
700 power_mask = 0; /* XXX undefined */
702 power_mask = UPS_PORT_POWER_SS;
708 if ((sc->sc_st.port_status & power_mask) != power_mask) {
709 DPRINTF("WARNING: strange, connected port %d "
710 "has no power\n", portno);
713 /* check if the device is in Host Mode */
715 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
717 DPRINTF("Port %d is in Host Mode\n", portno);
719 if (sc->sc_st.port_status & UPS_SUSPEND) {
721 * NOTE: Should not get here in SuperSpeed
722 * mode, because the HUB should report this
725 DPRINTF("Port %d was still "
726 "suspended, clearing.\n", portno);
727 err = usbd_req_clear_port_feature(udev,
728 NULL, portno, UHF_PORT_SUSPEND);
733 /* wait for maximum device power up time */
736 USB_MS_TO_TICKS(usb_port_powerup_delay));
738 /* reset port, which implies enabling it */
740 err = usbd_req_reset_port(udev, NULL, portno);
743 DPRINTFN(0, "port %d reset "
744 "failed, error=%s\n",
745 portno, usbd_errstr(err));
748 /* get port status again, it might have changed during reset */
750 err = uhub_read_port_status(sc, portno);
754 /* check if something changed during port reset */
756 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
757 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
759 DPRINTFN(0, "giving up port %d reset - "
760 "device vanished: change %#x status %#x\n",
761 portno, sc->sc_st.port_change,
762 sc->sc_st.port_status);
769 DPRINTF("Port %d is in Device Mode\n", portno);
773 * Figure out the device speed
775 switch (udev->speed) {
777 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
778 speed = USB_SPEED_HIGH;
779 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
780 speed = USB_SPEED_LOW;
782 speed = USB_SPEED_FULL;
785 if (sc->sc_st.port_status & UPS_LOW_SPEED)
786 speed = USB_SPEED_LOW;
788 speed = USB_SPEED_FULL;
791 speed = USB_SPEED_LOW;
793 case USB_SPEED_SUPER:
794 if (udev->parent_hub == NULL) {
795 /* Root HUB - special case */
796 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
798 speed = USB_SPEED_FULL;
801 speed = USB_SPEED_LOW;
804 speed = USB_SPEED_HIGH;
807 speed = USB_SPEED_SUPER;
811 speed = USB_SPEED_SUPER;
815 /* same speed like parent */
819 if (speed == USB_SPEED_SUPER) {
820 err = usbd_req_set_hub_u1_timeout(udev, NULL,
821 portno, 128 - (2 * udev->depth));
823 DPRINTFN(0, "port %d U1 timeout "
824 "failed, error=%s\n",
825 portno, usbd_errstr(err));
827 err = usbd_req_set_hub_u2_timeout(udev, NULL,
828 portno, 128 - (2 * udev->depth));
830 DPRINTFN(0, "port %d U2 timeout "
831 "failed, error=%s\n",
832 portno, usbd_errstr(err));
837 * Figure out the device mode
839 * NOTE: This part is currently FreeBSD specific.
841 if (udev->parent_hub != NULL) {
842 /* inherit mode from the parent HUB */
843 mode = udev->parent_hub->flags.usb_mode;
844 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
845 mode = USB_MODE_DEVICE;
847 mode = USB_MODE_HOST;
849 /* need to create a new child */
850 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
851 udev->depth + 1, portno - 1, portno, speed, mode);
853 DPRINTFN(0, "could not allocate new device\n");
856 return (0); /* success */
861 * Free USB device and all subdevices, if any.
863 usb_free_device(child, 0);
867 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
868 err = usbd_req_clear_port_feature(
870 portno, UHF_PORT_ENABLE);
874 DPRINTFN(0, "device problem (%s), "
875 "disabling port %d\n", usbd_errstr(err), portno);
880 /*------------------------------------------------------------------------*
881 * usb_device_20_compatible
884 * 0: HUB does not support suspend and resume
885 * Else: HUB supports suspend and resume
886 *------------------------------------------------------------------------*/
888 usb_device_20_compatible(struct usb_device *udev)
892 switch (udev->speed) {
902 /*------------------------------------------------------------------------*
903 * uhub_suspend_resume_port
907 * Else: A control transaction failed
908 *------------------------------------------------------------------------*/
910 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
912 struct usb_device *child;
913 struct usb_device *udev;
917 DPRINTF("port %d\n", portno);
920 child = usb_bus_port_get_device(udev->bus,
921 udev->hub->ports + portno - 1);
923 /* first clear the port suspend change bit */
925 if (usb_device_20_compatible(udev)) {
926 err = usbd_req_clear_port_feature(udev, NULL,
927 portno, UHF_C_PORT_SUSPEND);
929 err = usbd_req_clear_port_feature(udev, NULL,
930 portno, UHF_C_PORT_LINK_STATE);
934 DPRINTF("clearing suspend failed.\n");
937 /* get fresh status */
939 err = uhub_read_port_status(sc, portno);
941 DPRINTF("reading port status failed.\n");
944 /* convert current state */
946 if (usb_device_20_compatible(udev)) {
947 if (sc->sc_st.port_status & UPS_SUSPEND) {
953 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
957 case UPS_PORT_LS_SS_INA:
958 usbd_req_warm_reset_port(udev, NULL, portno);
967 DPRINTF("suspended=%u\n", is_suspend);
969 /* do the suspend or resume */
973 * This code handle two cases: 1) Host Mode - we can only
974 * receive resume here 2) Device Mode - we can receive
975 * suspend and resume here
978 usb_dev_resume_peer(child);
979 else if (child->flags.usb_mode == USB_MODE_DEVICE)
980 usb_dev_suspend_peer(child);
986 /*------------------------------------------------------------------------*
987 * uhub_root_interrupt
989 * This function is called when a Root HUB interrupt has
990 * happened. "ptr" and "len" makes up the Root HUB interrupt
991 * packet. This function is called having the "bus_mtx" locked.
992 *------------------------------------------------------------------------*/
994 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
996 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
998 usb_needs_explore(bus, 0);
1002 uhub_is_too_deep(struct usb_device *udev)
1004 switch (udev->speed) {
1005 case USB_SPEED_FULL:
1007 case USB_SPEED_HIGH:
1008 if (udev->depth > USB_HUB_MAX_DEPTH)
1011 case USB_SPEED_SUPER:
1012 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
1021 /*------------------------------------------------------------------------*
1027 *------------------------------------------------------------------------*/
1029 uhub_explore(struct usb_device *udev)
1031 struct usb_hub *hub;
1032 struct uhub_softc *sc;
1033 struct usb_port *up;
1042 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1044 /* ignore devices that are too deep */
1045 if (uhub_is_too_deep(udev))
1046 return (USB_ERR_TOO_DEEP);
1048 /* check if device is suspended */
1049 if (udev->flags.self_suspended) {
1050 /* need to wait until the child signals resume */
1051 DPRINTF("Device is suspended!\n");
1056 * Make sure we don't race against user-space applications
1059 do_unlock = usbd_enum_lock(udev);
1061 for (x = 0; x != hub->nports; x++) {
1062 up = hub->ports + x;
1065 err = uhub_read_port_status(sc, portno);
1067 /* most likely the HUB is gone */
1070 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1071 DPRINTF("Overcurrent on port %u.\n", portno);
1072 err = usbd_req_clear_port_feature(
1073 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1075 /* most likely the HUB is gone */
1079 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1081 * Fake a connect status change so that the
1082 * status gets checked initially!
1084 sc->sc_st.port_change |=
1085 UPS_C_CONNECT_STATUS;
1087 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1088 err = usbd_req_clear_port_feature(
1089 udev, NULL, portno, UHF_C_PORT_ENABLE);
1091 /* most likely the HUB is gone */
1094 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1096 * Ignore the port error if the device
1099 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1100 DPRINTFN(0, "illegal enable change, "
1101 "port %d\n", portno);
1104 if (up->restartcnt == USB_RESTART_MAX) {
1105 /* XXX could try another speed ? */
1106 DPRINTFN(0, "port error, giving up "
1107 "port %d\n", portno);
1109 sc->sc_st.port_change |=
1110 UPS_C_CONNECT_STATUS;
1115 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1116 err = uhub_reattach_port(sc, portno);
1118 /* most likely the HUB is gone */
1122 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1123 UPS_C_PORT_LINK_STATE)) {
1124 err = uhub_suspend_resume_port(sc, portno);
1126 /* most likely the HUB is gone */
1130 err = uhub_explore_sub(sc, up);
1132 /* no device(s) present */
1135 /* explore succeeded - reset restart counter */
1140 usbd_enum_unlock(udev);
1142 /* initial status checked */
1143 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1145 /* return success */
1146 return (USB_ERR_NORMAL_COMPLETION);
1150 uhub_probe(device_t dev)
1152 struct usb_attach_arg *uaa = device_get_ivars(dev);
1154 if (uaa->usb_mode != USB_MODE_HOST)
1158 * The subclass for USB HUBs is currently ignored because it
1159 * is 0 for some and 1 for others.
1161 if (uaa->info.bConfigIndex == 0 &&
1162 uaa->info.bDeviceClass == UDCLASS_HUB)
1168 /* NOTE: The information returned by this function can be wrong. */
1170 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1172 struct usb_hub_descriptor hubdesc20;
1173 struct usb_hub_ss_descriptor hubdesc30;
1178 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1179 return (USB_ERR_INVAL);
1184 switch (udev->speed) {
1186 case USB_SPEED_FULL:
1187 case USB_SPEED_HIGH:
1188 /* assuming that there is one port */
1189 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1191 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1192 "error=%s\n", usbd_errstr(err));
1195 nports = hubdesc20.bNbrPorts;
1199 if (udev->speed == USB_SPEED_HIGH)
1200 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1203 case USB_SPEED_SUPER:
1204 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1206 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1207 "error=%s\n", usbd_errstr(err));
1210 nports = hubdesc30.bNbrPorts;
1216 err = USB_ERR_INVAL;
1220 if (pnports != NULL)
1230 uhub_attach(device_t dev)
1232 struct uhub_softc *sc = device_get_softc(dev);
1233 struct usb_attach_arg *uaa = device_get_ivars(dev);
1234 struct usb_device *udev = uaa->device;
1235 struct usb_device *parent_hub = udev->parent_hub;
1236 struct usb_hub *hub;
1237 struct usb_hub_descriptor hubdesc20;
1238 struct usb_hub_ss_descriptor hubdesc30;
1239 #if USB_HAVE_DISABLE_ENUM
1240 struct sysctl_ctx_list *sysctl_ctx;
1241 struct sysctl_oid *sysctl_tree;
1248 uint8_t iface_index;
1254 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1256 device_set_usb_desc(dev);
1258 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1259 "parent->selfpowered=%d\n",
1261 udev->flags.self_powered,
1264 parent_hub->flags.self_powered : 0);
1266 if (uhub_is_too_deep(udev)) {
1267 DPRINTFN(0, "HUB at depth %d, "
1268 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1272 if (!udev->flags.self_powered && parent_hub &&
1273 !parent_hub->flags.self_powered) {
1274 DPRINTFN(0, "Bus powered HUB connected to "
1275 "bus powered HUB. HUB ignored\n");
1279 if (UHUB_IS_MULTI_TT(sc)) {
1280 err = usbd_set_alt_interface_index(udev, 0, 1);
1282 device_printf(dev, "MTT could not be enabled\n");
1285 device_printf(dev, "MTT enabled\n");
1288 /* get HUB descriptor */
1290 DPRINTFN(2, "Getting HUB descriptor\n");
1292 switch (udev->speed) {
1294 case USB_SPEED_FULL:
1295 case USB_SPEED_HIGH:
1296 /* assuming that there is one port */
1297 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1299 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1300 "error=%s\n", usbd_errstr(err));
1303 /* get number of ports */
1304 nports = hubdesc20.bNbrPorts;
1306 /* get power delay */
1307 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1308 usb_extra_power_up_time);
1310 /* get complete HUB descriptor */
1312 /* check number of ports */
1314 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1315 "error=%s\n", usbd_errstr(err));
1318 /* get complete HUB descriptor */
1319 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1322 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1323 "error=%s\n", usbd_errstr(err));
1326 if (hubdesc20.bNbrPorts != nports) {
1327 DPRINTFN(0, "Number of ports changed\n");
1332 case USB_SPEED_SUPER:
1333 if (udev->parent_hub != NULL) {
1334 err = usbd_req_set_hub_depth(udev, NULL,
1337 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1338 "error=%s\n", usbd_errstr(err));
1342 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1344 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1345 "error=%s\n", usbd_errstr(err));
1348 /* get number of ports */
1349 nports = hubdesc30.bNbrPorts;
1351 /* get power delay */
1352 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1353 usb_extra_power_up_time);
1355 /* get complete HUB descriptor */
1357 /* check number of ports */
1358 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1359 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1360 "error=%s\n", usbd_errstr(err));
1363 /* get complete HUB descriptor */
1364 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1367 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1368 "error=%s\n", usbd_errstr(err));
1371 if (hubdesc30.bNbrPorts != nports) {
1372 DPRINTFN(0, "Number of ports changed\n");
1378 DPRINTF("Assuming HUB has only one port\n");
1379 /* default number of ports */
1381 /* default power delay */
1382 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1386 DPRINTFN(0, "portless HUB\n");
1389 if (nports > USB_MAX_PORTS) {
1390 DPRINTF("Port limit exceeded\n");
1393 #if (USB_HAVE_FIXED_PORT == 0)
1394 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1395 M_USBDEV, M_WAITOK | M_ZERO);
1404 /* initialize HUB structure */
1406 hub->explore = &uhub_explore;
1407 hub->nports = nports;
1408 hub->hubudev = udev;
1409 #if USB_HAVE_TT_SUPPORT
1410 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1411 hub->tt_msg[0].udev = udev;
1412 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1413 hub->tt_msg[1].udev = udev;
1415 /* if self powered hub, give ports maximum current */
1416 if (udev->flags.self_powered) {
1417 hub->portpower = USB_MAX_POWER;
1419 hub->portpower = USB_MIN_POWER;
1422 /* set up interrupt pipe */
1424 if (udev->parent_hub == NULL) {
1425 /* root HUB is special */
1429 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1430 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1433 DPRINTFN(0, "cannot setup interrupt transfer, "
1434 "errstr=%s\n", usbd_errstr(err));
1437 /* wait with power off for a while */
1438 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1440 #if USB_HAVE_DISABLE_ENUM
1441 /* Add device sysctls */
1443 sysctl_ctx = device_get_sysctl_ctx(dev);
1444 sysctl_tree = device_get_sysctl_tree(dev);
1446 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1447 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1448 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1449 &sc->sc_disable_enumeration, 0,
1450 "Set to disable enumeration on this USB HUB.");
1452 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1453 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1454 &sc->sc_disable_port_power, 0,
1455 "Set to disable USB port power on this USB HUB.");
1459 * To have the best chance of success we do things in the exact same
1460 * order as Windoze98. This should not be necessary, but some
1461 * devices do not follow the USB specs to the letter.
1463 * These are the events on the bus when a hub is attached:
1464 * Get device and config descriptors (see attach code)
1465 * Get hub descriptor (see above)
1468 * wait for power to become stable
1469 * (all below happens in explore code)
1471 * clear C_PORT_CONNECTION
1474 * if device connected
1478 * clear C_PORT_RESET
1480 * proceed with device attachment
1483 /* XXX should check for none, individual, or ganged power? */
1487 for (x = 0; x != nports; x++) {
1488 /* set up data structures */
1489 struct usb_port *up = hub->ports + x;
1491 up->device_index = 0;
1495 /* check if port is removable */
1496 switch (udev->speed) {
1498 case USB_SPEED_FULL:
1499 case USB_SPEED_HIGH:
1500 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1503 case USB_SPEED_SUPER:
1504 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1508 DPRINTF("Assuming removable port\n");
1513 #if USB_HAVE_DISABLE_ENUM
1514 /* check if we should disable USB port power or not */
1515 if (usb_disable_port_power != 0 ||
1516 sc->sc_disable_port_power != 0) {
1517 /* turn the power off */
1518 DPRINTFN(2, "Turning port %d power off\n", portno);
1519 err = usbd_req_clear_port_feature(udev, NULL,
1520 portno, UHF_PORT_POWER);
1523 /* turn the power on */
1524 DPRINTFN(2, "Turning port %d power on\n", portno);
1525 err = usbd_req_set_port_feature(udev, NULL,
1526 portno, UHF_PORT_POWER);
1527 #if USB_HAVE_DISABLE_ENUM
1532 DPRINTFN(0, "port %d power on or off failed, %s\n",
1533 portno, usbd_errstr(err));
1535 DPRINTF("turn on port %d power\n",
1538 /* wait for stable power */
1539 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1542 device_printf(dev, "%d port%s with %d "
1543 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1544 removable, udev->flags.self_powered ? "self" : "bus");
1546 /* Start the interrupt endpoint, if any */
1548 USB_MTX_LOCK(&sc->sc_mtx);
1549 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1550 USB_MTX_UNLOCK(&sc->sc_mtx);
1552 /* Enable automatic power save on all USB HUBs */
1554 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1559 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1561 #if (USB_HAVE_FIXED_PORT == 0)
1562 free(udev->hub, M_USBDEV);
1566 mtx_destroy(&sc->sc_mtx);
1572 * Called from process context when the hub is gone.
1573 * Detach all devices on active ports.
1576 uhub_detach(device_t dev)
1578 struct uhub_softc *sc = device_get_softc(dev);
1579 struct usb_hub *hub = sc->sc_udev->hub;
1580 struct usb_bus *bus = sc->sc_udev->bus;
1581 struct usb_device *child;
1584 if (hub == NULL) /* must be partially working */
1587 /* Make sure interrupt transfer is gone. */
1588 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1590 /* Detach all ports */
1591 for (x = 0; x != hub->nports; x++) {
1593 child = usb_bus_port_get_device(bus, hub->ports + x);
1595 if (child == NULL) {
1600 * Free USB device and all subdevices, if any.
1602 usb_free_device(child, 0);
1605 #if USB_HAVE_TT_SUPPORT
1606 /* Make sure our TT messages are not queued anywhere */
1608 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1609 &hub->tt_msg[0], &hub->tt_msg[1]);
1610 USB_BUS_UNLOCK(bus);
1613 #if (USB_HAVE_FIXED_PORT == 0)
1614 free(hub, M_USBDEV);
1616 sc->sc_udev->hub = NULL;
1618 mtx_destroy(&sc->sc_mtx);
1624 uhub_suspend(device_t dev)
1627 /* Sub-devices are not suspended here! */
1632 uhub_resume(device_t dev)
1635 /* Sub-devices are not resumed here! */
1640 uhub_driver_added(device_t dev, driver_t *driver)
1642 usb_needs_explore_all();
1646 struct usb_device *udev;
1648 uint8_t iface_index;
1652 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1653 struct hub_result *res)
1655 struct usb_interface *iface;
1656 struct usb_device *udev;
1661 nports = hub->nports;
1662 for (x = 0; x != nports; x++) {
1663 udev = usb_bus_port_get_device(hub->hubudev->bus,
1668 for (i = 0; i != USB_IFACE_MAX; i++) {
1669 iface = usbd_get_iface(udev, i);
1671 (iface->subdev == child)) {
1672 res->iface_index = i;
1674 res->portno = x + 1;
1679 res->iface_index = 0;
1685 uhub_child_location_string(device_t parent, device_t child,
1686 char *buf, size_t buflen)
1688 struct uhub_softc *sc;
1689 struct usb_hub *hub;
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 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1715 , device_get_unit(res.udev->bus->bdev)
1716 , (res.udev->parent_hub != NULL) ?
1717 res.udev->parent_hub->device_index : 0
1718 , res.portno, res.udev->device_index, res.iface_index
1720 , res.udev->ugen_name
1730 uhub_child_pnpinfo_string(device_t parent, device_t child,
1731 char *buf, size_t buflen)
1733 struct uhub_softc *sc;
1734 struct usb_hub *hub;
1735 struct usb_interface *iface;
1736 struct hub_result res;
1739 if (!device_is_attached(parent)) {
1745 sc = device_get_softc(parent);
1746 hub = sc->sc_udev->hub;
1749 uhub_find_iface_index(hub, child, &res);
1751 DPRINTF("device not on hub\n");
1757 iface = usbd_get_iface(res.udev, res.iface_index);
1758 if (iface && iface->idesc) {
1759 /* Make sure device information is not changed during the print. */
1760 do_unlock = usbd_ctrl_lock(res.udev);
1762 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1763 "devclass=0x%02x devsubclass=0x%02x "
1768 "intclass=0x%02x intsubclass=0x%02x "
1769 "intprotocol=0x%02x" "%s%s",
1770 UGETW(res.udev->ddesc.idVendor),
1771 UGETW(res.udev->ddesc.idProduct),
1772 res.udev->ddesc.bDeviceClass,
1773 res.udev->ddesc.bDeviceSubClass,
1774 res.udev->ddesc.bDeviceProtocol,
1775 usb_get_serial(res.udev),
1776 UGETW(res.udev->ddesc.bcdDevice),
1777 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1778 iface->idesc->bInterfaceClass,
1779 iface->idesc->bInterfaceSubClass,
1780 iface->idesc->bInterfaceProtocol,
1781 iface->pnpinfo ? " " : "",
1782 iface->pnpinfo ? iface->pnpinfo : "");
1785 usbd_ctrl_unlock(res.udev);
1799 * The USB Transaction Translator:
1800 * ===============================
1802 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1803 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1804 * USB transfers. To utilize bandwidth dynamically the "scatter and
1805 * gather" principle must be applied. This means that bandwidth must
1806 * be divided into equal parts of bandwidth. With regard to USB all
1807 * data is transferred in smaller packets with length
1808 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1811 * The bandwidth scheduler which I have implemented will simply pack
1812 * the USB transfers back to back until there is no more space in the
1813 * schedule. Out of the 8 microframes which the USB 2.0 standard
1814 * provides, only 6 are available for non-HIGH-speed devices. I have
1815 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1816 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1817 * this division, it is very difficult to allocate and free bandwidth
1820 * NOTE about the Transaction Translator in USB HUBs:
1822 * USB HUBs have a very simple Transaction Translator, that will
1823 * simply pipeline all the SPLIT transactions. That means that the
1824 * transactions will be executed in the order they are queued!
1828 /*------------------------------------------------------------------------*
1829 * usb_intr_find_best_slot
1832 * The best Transaction Translation slot for an interrupt endpoint.
1833 *------------------------------------------------------------------------*/
1835 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1836 uint8_t end, uint8_t mask)
1838 usb_size_t min = (usb_size_t)-1;
1846 /* find the last slot with lesser used bandwidth */
1848 for (x = start; x < end; x++) {
1852 /* compute sum of bandwidth */
1853 for (z = x; z < end; z++) {
1854 if (mask & (1U << (z - x)))
1858 /* check if the current multi-slot is more optimal */
1864 /* check if the mask is about to be shifted out */
1865 if (mask & (1U << (end - 1 - x)))
1871 /*------------------------------------------------------------------------*
1872 * usb_hs_bandwidth_adjust
1874 * This function will update the bandwidth usage for the microframe
1875 * having index "slot" by "len" bytes. "len" can be negative. If the
1876 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1877 * the "slot" argument will be replaced by the slot having least used
1878 * bandwidth. The "mask" argument is used for multi-slot allocations.
1881 * The slot in which the bandwidth update was done: 0..7
1882 *------------------------------------------------------------------------*/
1884 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1885 uint8_t slot, uint8_t mask)
1887 struct usb_bus *bus = udev->bus;
1888 struct usb_hub *hub;
1889 enum usb_dev_speed speed;
1892 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1894 speed = usbd_get_speed(udev);
1898 case USB_SPEED_FULL:
1899 if (speed == USB_SPEED_LOW) {
1903 * The Host Controller Driver should have
1904 * performed checks so that the lookup
1905 * below does not result in a NULL pointer
1909 hub = udev->parent_hs_hub->hub;
1910 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1911 slot = usb_intr_find_best_slot(hub->uframe_usage,
1912 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1914 for (x = slot; x < 8; x++) {
1915 if (mask & (1U << (x - slot))) {
1916 hub->uframe_usage[x] += len;
1917 bus->uframe_usage[x] += len;
1922 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1923 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1924 USB_HS_MICRO_FRAMES_MAX, mask);
1926 for (x = slot; x < 8; x++) {
1927 if (mask & (1U << (x - slot))) {
1928 bus->uframe_usage[x] += len;
1936 /*------------------------------------------------------------------------*
1937 * usb_hs_bandwidth_alloc
1939 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1940 *------------------------------------------------------------------------*/
1942 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1944 struct usb_device *udev;
1949 udev = xfer->xroot->udev;
1951 if (udev->flags.usb_mode != USB_MODE_HOST)
1952 return; /* not supported */
1954 xfer->endpoint->refcount_bw++;
1955 if (xfer->endpoint->refcount_bw != 1)
1956 return; /* already allocated */
1958 speed = usbd_get_speed(udev);
1960 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1962 /* allocate a microframe slot */
1965 slot = usb_hs_bandwidth_adjust(udev,
1966 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1968 xfer->endpoint->usb_uframe = slot;
1969 xfer->endpoint->usb_smask = mask << slot;
1971 if ((speed != USB_SPEED_FULL) &&
1972 (speed != USB_SPEED_LOW)) {
1973 xfer->endpoint->usb_cmask = 0x00 ;
1975 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1979 case UE_ISOCHRONOUS:
1980 switch (usbd_xfer_get_fps_shift(xfer)) {
1995 /* allocate a microframe multi-slot */
1997 slot = usb_hs_bandwidth_adjust(udev,
1998 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
2000 xfer->endpoint->usb_uframe = slot;
2001 xfer->endpoint->usb_cmask = 0;
2002 xfer->endpoint->usb_smask = mask << slot;
2006 xfer->endpoint->usb_uframe = 0;
2007 xfer->endpoint->usb_cmask = 0;
2008 xfer->endpoint->usb_smask = 0;
2012 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2013 xfer->endpoint->usb_uframe,
2014 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
2017 /*------------------------------------------------------------------------*
2018 * usb_hs_bandwidth_free
2020 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
2021 *------------------------------------------------------------------------*/
2023 usb_hs_bandwidth_free(struct usb_xfer *xfer)
2025 struct usb_device *udev;
2029 udev = xfer->xroot->udev;
2031 if (udev->flags.usb_mode != USB_MODE_HOST)
2032 return; /* not supported */
2034 xfer->endpoint->refcount_bw--;
2035 if (xfer->endpoint->refcount_bw != 0)
2036 return; /* still allocated */
2038 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2040 case UE_ISOCHRONOUS:
2042 slot = xfer->endpoint->usb_uframe;
2043 mask = xfer->endpoint->usb_smask;
2045 /* free microframe slot(s): */
2046 usb_hs_bandwidth_adjust(udev,
2047 -xfer->max_frame_size, slot, mask >> slot);
2049 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2050 slot, mask >> slot);
2052 xfer->endpoint->usb_uframe = 0;
2053 xfer->endpoint->usb_cmask = 0;
2054 xfer->endpoint->usb_smask = 0;
2062 /*------------------------------------------------------------------------*
2063 * usb_isoc_time_expand
2065 * This function will expand the time counter from 7-bit to 16-bit.
2068 * 16-bit isochronous time counter.
2069 *------------------------------------------------------------------------*/
2071 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2075 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2077 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2079 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2081 if (isoc_time_curr < rem) {
2082 /* the time counter wrapped around */
2083 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2085 /* update the remainder */
2087 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2088 bus->isoc_time_last |= isoc_time_curr;
2090 return (bus->isoc_time_last);
2093 /*------------------------------------------------------------------------*
2094 * usbd_fs_isoc_schedule_alloc_slot
2096 * This function will allocate bandwidth for an isochronous FULL speed
2097 * transaction in the FULL speed schedule.
2102 *------------------------------------------------------------------------*/
2103 #if USB_HAVE_TT_SUPPORT
2105 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2107 struct usb_xfer *xfer;
2108 struct usb_xfer *pipe_xfer;
2109 struct usb_bus *bus;
2111 usb_frlength_t data_len;
2119 bus = isoc_xfer->xroot->bus;
2121 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2123 /* skip self, if any */
2125 if (xfer == isoc_xfer)
2128 /* check if this USB transfer is going through the same TT */
2130 if (xfer->xroot->udev->parent_hs_hub !=
2131 isoc_xfer->xroot->udev->parent_hs_hub) {
2134 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2135 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2136 (xfer->xroot->udev->hs_port_no !=
2137 isoc_xfer->xroot->udev->hs_port_no)) {
2140 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2143 /* check if isoc_time is part of this transfer */
2145 delta = xfer->isoc_time_complete - isoc_time;
2146 if (delta > 0 && delta <= xfer->nframes) {
2147 delta = xfer->nframes - delta;
2149 len = xfer->frlengths[delta];
2158 * Check double buffered transfers. Only stream ID
2159 * equal to zero is valid here!
2161 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2164 /* skip self, if any */
2166 if (pipe_xfer == isoc_xfer)
2169 /* check if isoc_time is part of this transfer */
2171 delta = pipe_xfer->isoc_time_complete - isoc_time;
2172 if (delta > 0 && delta <= pipe_xfer->nframes) {
2173 delta = pipe_xfer->nframes - delta;
2175 len = pipe_xfer->frlengths[delta];
2185 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2186 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2190 /* check for overflow */
2192 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2197 delta = isoc_xfer->isoc_time_complete - isoc_time;
2198 if (delta > 0 && delta <= isoc_xfer->nframes) {
2199 delta = isoc_xfer->nframes - delta;
2201 len = isoc_xfer->frlengths[delta];
2209 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2210 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2214 /* check for overflow */
2216 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2223 /*------------------------------------------------------------------------*
2224 * usb_bus_port_get_device
2226 * This function is NULL safe.
2227 *------------------------------------------------------------------------*/
2229 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2231 if ((bus == NULL) || (up == NULL)) {
2235 if (up->device_index == 0) {
2239 return (bus->devices[up->device_index]);
2242 /*------------------------------------------------------------------------*
2243 * usb_bus_port_set_device
2245 * This function is NULL safe.
2246 *------------------------------------------------------------------------*/
2248 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2249 struct usb_device *udev, uint8_t device_index)
2256 * There is only one case where we don't
2257 * have an USB port, and that is the Root Hub!
2261 up->device_index = device_index;
2263 device_index = up->device_index;
2264 up->device_index = 0;
2268 * Make relationships to our new device
2270 if (device_index != 0) {
2272 mtx_lock(&usb_ref_lock);
2274 bus->devices[device_index] = udev;
2276 mtx_unlock(&usb_ref_lock);
2282 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2285 /*------------------------------------------------------------------------*
2288 * This functions is called when the USB event thread needs to run.
2289 *------------------------------------------------------------------------*/
2291 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2303 DPRINTF("No bus pointer!\n");
2306 if ((bus->devices == NULL) ||
2307 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2308 DPRINTF("No root HUB\n");
2311 if (mtx_owned(&bus->bus_mtx)) {
2320 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2321 &bus->explore_msg[0], &bus->explore_msg[1])) {
2325 USB_BUS_UNLOCK(bus);
2329 /*------------------------------------------------------------------------*
2330 * usb_needs_explore_all
2332 * This function is called whenever a new driver is loaded and will
2333 * cause that all USB buses are re-explored.
2334 *------------------------------------------------------------------------*/
2336 usb_needs_explore_all(void)
2338 struct usb_bus *bus;
2345 dc = usb_devclass_ptr;
2347 DPRINTFN(0, "no devclass\n");
2351 * Explore all USB buses in parallel.
2353 max = devclass_get_maxunit(dc);
2355 dev = devclass_get_device(dc, max);
2357 bus = device_get_softc(dev);
2359 usb_needs_explore(bus, 1);
2366 /*------------------------------------------------------------------------*
2367 * usb_needs_explore_init
2369 * This function will ensure that the USB controllers are not enumerated
2370 * until the "cold" variable is cleared.
2371 *------------------------------------------------------------------------*/
2373 usb_needs_explore_init(void *arg)
2376 * The cold variable should be cleared prior to this function
2380 usb_needs_explore_all();
2382 DPRINTFN(-1, "Cold variable is still set!\n");
2384 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2386 /*------------------------------------------------------------------------*
2387 * usb_bus_power_update
2389 * This function will ensure that all USB devices on the given bus are
2390 * properly suspended or resumed according to the device transfer
2392 *------------------------------------------------------------------------*/
2395 usb_bus_power_update(struct usb_bus *bus)
2397 usb_needs_explore(bus, 0 /* no probe */ );
2401 /*------------------------------------------------------------------------*
2402 * usbd_transfer_power_ref
2404 * This function will modify the power save reference counts and
2405 * wakeup the USB device associated with the given USB transfer, if
2407 *------------------------------------------------------------------------*/
2410 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2412 static const usb_power_mask_t power_mask[4] = {
2413 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2414 [UE_BULK] = USB_HW_POWER_BULK,
2415 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2416 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2418 struct usb_device *udev;
2419 uint8_t needs_explore;
2420 uint8_t needs_hw_power;
2423 udev = xfer->xroot->udev;
2425 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2426 /* no power save for root HUB */
2429 USB_BUS_LOCK(udev->bus);
2431 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2433 udev->pwr_save.last_xfer_time = ticks;
2434 udev->pwr_save.type_refs[xfer_type] += val;
2436 if (xfer->flags_int.control_xfr) {
2437 udev->pwr_save.read_refs += val;
2438 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2440 * It is not allowed to suspend during a
2443 udev->pwr_save.write_refs += val;
2445 } else if (USB_GET_DATA_ISREAD(xfer)) {
2446 udev->pwr_save.read_refs += val;
2448 udev->pwr_save.write_refs += val;
2452 if (udev->flags.self_suspended)
2453 needs_explore = usb_peer_should_wakeup(udev);
2457 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2458 DPRINTF("Adding type %u to power state\n", xfer_type);
2459 udev->bus->hw_power_state |= power_mask[xfer_type];
2469 USB_BUS_UNLOCK(udev->bus);
2471 if (needs_explore) {
2472 DPRINTF("update\n");
2473 usb_bus_power_update(udev->bus);
2474 } else if (needs_hw_power) {
2475 DPRINTF("needs power\n");
2476 if (udev->bus->methods->set_hw_power != NULL) {
2477 (udev->bus->methods->set_hw_power) (udev->bus);
2483 /*------------------------------------------------------------------------*
2484 * usb_peer_should_wakeup
2486 * This function returns non-zero if the current device should wake up.
2487 *------------------------------------------------------------------------*/
2489 usb_peer_should_wakeup(struct usb_device *udev)
2491 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2492 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2493 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2494 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2495 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2496 (udev->pwr_save.write_refs != 0) ||
2497 ((udev->pwr_save.read_refs != 0) &&
2498 (udev->flags.usb_mode == USB_MODE_HOST) &&
2499 (usb_peer_can_wakeup(udev) == 0)));
2502 /*------------------------------------------------------------------------*
2505 * This function implements the USB power daemon and is called
2506 * regularly from the USB explore thread.
2507 *------------------------------------------------------------------------*/
2510 usb_bus_powerd(struct usb_bus *bus)
2512 struct usb_device *udev;
2515 usb_ticks_t mintime;
2516 usb_size_t type_refs[5];
2519 limit = usb_power_timeout;
2522 else if (limit > 255)
2527 DPRINTF("bus=%p\n", bus);
2532 * The root HUB device is never suspended
2533 * and we simply skip it.
2535 for (x = USB_ROOT_HUB_ADDR + 1;
2536 x != bus->devices_max; x++) {
2538 udev = bus->devices[x];
2542 temp = ticks - udev->pwr_save.last_xfer_time;
2544 if (usb_peer_should_wakeup(udev)) {
2545 /* check if we are suspended */
2546 if (udev->flags.self_suspended != 0) {
2547 USB_BUS_UNLOCK(bus);
2548 usb_dev_resume_peer(udev);
2551 } else if ((temp >= limit) &&
2552 (udev->flags.usb_mode == USB_MODE_HOST) &&
2553 (udev->flags.self_suspended == 0)) {
2554 /* try to do suspend */
2556 USB_BUS_UNLOCK(bus);
2557 usb_dev_suspend_peer(udev);
2562 /* reset counters */
2564 mintime = (usb_ticks_t)-1;
2571 /* Re-loop all the devices to get the actual state */
2573 for (x = USB_ROOT_HUB_ADDR + 1;
2574 x != bus->devices_max; x++) {
2576 udev = bus->devices[x];
2580 /* we found a non-Root-Hub USB device */
2583 /* "last_xfer_time" can be updated by a resume */
2584 temp = ticks - udev->pwr_save.last_xfer_time;
2587 * Compute minimum time since last transfer for the complete
2593 if (udev->flags.self_suspended == 0) {
2594 type_refs[0] += udev->pwr_save.type_refs[0];
2595 type_refs[1] += udev->pwr_save.type_refs[1];
2596 type_refs[2] += udev->pwr_save.type_refs[2];
2597 type_refs[3] += udev->pwr_save.type_refs[3];
2601 if (mintime >= (usb_ticks_t)(1 * hz)) {
2602 /* recompute power masks */
2603 DPRINTF("Recomputing power masks\n");
2604 bus->hw_power_state = 0;
2605 if (type_refs[UE_CONTROL] != 0)
2606 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2607 if (type_refs[UE_BULK] != 0)
2608 bus->hw_power_state |= USB_HW_POWER_BULK;
2609 if (type_refs[UE_INTERRUPT] != 0)
2610 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2611 if (type_refs[UE_ISOCHRONOUS] != 0)
2612 bus->hw_power_state |= USB_HW_POWER_ISOC;
2613 if (type_refs[4] != 0)
2614 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2616 USB_BUS_UNLOCK(bus);
2618 if (bus->methods->set_hw_power != NULL) {
2619 /* always update hardware power! */
2620 (bus->methods->set_hw_power) (bus);
2627 usbd_device_30_remote_wakeup(struct usb_device *udev, uint8_t bRequest)
2629 struct usb_device_request req = {};
2631 req.bmRequestType = UT_WRITE_INTERFACE;
2632 req.bRequest = bRequest;
2633 USETW(req.wValue, USB_INTERFACE_FUNC_SUSPEND);
2634 USETW(req.wIndex, USB_INTERFACE_FUNC_SUSPEND_LP |
2635 USB_INTERFACE_FUNC_SUSPEND_RW);
2637 return (usbd_do_request(udev, NULL, &req, 0));
2641 usbd_clear_dev_wakeup(struct usb_device *udev)
2645 if (usb_device_20_compatible(udev)) {
2646 err = usbd_req_clear_device_feature(udev,
2647 NULL, UF_DEVICE_REMOTE_WAKEUP);
2649 err = usbd_device_30_remote_wakeup(udev,
2656 usbd_set_dev_wakeup(struct usb_device *udev)
2660 if (usb_device_20_compatible(udev)) {
2661 err = usbd_req_set_device_feature(udev,
2662 NULL, UF_DEVICE_REMOTE_WAKEUP);
2664 err = usbd_device_30_remote_wakeup(udev,
2670 /*------------------------------------------------------------------------*
2671 * usb_dev_resume_peer
2673 * This function will resume an USB peer and do the required USB
2674 * signalling to get an USB device out of the suspended state.
2675 *------------------------------------------------------------------------*/
2677 usb_dev_resume_peer(struct usb_device *udev)
2679 struct usb_bus *bus;
2686 /* check if already resumed */
2687 if (udev->flags.self_suspended == 0)
2690 /* we need a parent HUB to do resume */
2691 if (udev->parent_hub == NULL)
2694 DPRINTF("udev=%p\n", udev);
2696 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2697 (udev->flags.remote_wakeup == 0)) {
2699 * If the host did not set the remote wakeup feature, we can
2700 * not wake it up either!
2702 DPRINTF("remote wakeup is not set!\n");
2705 /* get bus pointer */
2708 /* resume parent hub first */
2709 usb_dev_resume_peer(udev->parent_hub);
2711 /* reduce chance of instant resume failure by waiting a little bit */
2712 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2714 if (usb_device_20_compatible(udev)) {
2715 /* resume current port (Valid in Host and Device Mode) */
2716 err = usbd_req_clear_port_feature(udev->parent_hub,
2717 NULL, udev->port_no, UHF_PORT_SUSPEND);
2719 DPRINTFN(0, "Resuming port failed\n");
2723 /* resume current port (Valid in Host and Device Mode) */
2724 err = usbd_req_set_port_link_state(udev->parent_hub,
2725 NULL, udev->port_no, UPS_PORT_LS_U0);
2727 DPRINTFN(0, "Resuming port failed\n");
2732 /* resume settle time */
2733 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2735 if (bus->methods->device_resume != NULL) {
2736 /* resume USB device on the USB controller */
2737 (bus->methods->device_resume) (udev);
2740 /* set that this device is now resumed */
2741 udev->flags.self_suspended = 0;
2743 /* make sure that we don't go into suspend right away */
2744 udev->pwr_save.last_xfer_time = ticks;
2746 /* make sure the needed power masks are on */
2747 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2748 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2749 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2750 bus->hw_power_state |= USB_HW_POWER_BULK;
2751 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2752 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2753 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2754 bus->hw_power_state |= USB_HW_POWER_ISOC;
2756 USB_BUS_UNLOCK(bus);
2758 if (bus->methods->set_hw_power != NULL) {
2759 /* always update hardware power! */
2760 (bus->methods->set_hw_power) (bus);
2765 /* notify all sub-devices about resume */
2766 err = usb_suspend_resume(udev, 0);
2768 usbd_sr_unlock(udev);
2770 /* check if peer has wakeup capability */
2771 if (usb_peer_can_wakeup(udev)) {
2772 /* clear remote wakeup */
2773 err = usbd_clear_dev_wakeup(udev);
2775 DPRINTFN(0, "Clearing device "
2776 "remote wakeup failed: %s\n",
2782 /*------------------------------------------------------------------------*
2783 * usb_dev_suspend_peer
2785 * This function will suspend an USB peer and do the required USB
2786 * signalling to get an USB device into the suspended state.
2787 *------------------------------------------------------------------------*/
2789 usb_dev_suspend_peer(struct usb_device *udev)
2791 struct usb_device *child;
2801 /* check if already suspended */
2802 if (udev->flags.self_suspended)
2805 /* we need a parent HUB to do suspend */
2806 if (udev->parent_hub == NULL)
2809 DPRINTF("udev=%p\n", udev);
2811 /* check if the current device is a HUB */
2812 if (udev->hub != NULL) {
2813 nports = udev->hub->nports;
2815 /* check if all devices on the HUB are suspended */
2816 for (x = 0; x != nports; x++) {
2817 child = usb_bus_port_get_device(udev->bus,
2818 udev->hub->ports + x);
2823 if (child->flags.self_suspended)
2826 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2831 if (usb_peer_can_wakeup(udev)) {
2833 * This request needs to be done before we set
2834 * "udev->flags.self_suspended":
2837 /* allow device to do remote wakeup */
2838 err = usbd_set_dev_wakeup(udev);
2840 DPRINTFN(0, "Setting device "
2841 "remote wakeup failed\n");
2845 USB_BUS_LOCK(udev->bus);
2847 * Checking for suspend condition and setting suspended bit
2850 err = usb_peer_should_wakeup(udev);
2853 * Set that this device is suspended. This variable
2854 * must be set before calling USB controller suspend
2857 udev->flags.self_suspended = 1;
2859 USB_BUS_UNLOCK(udev->bus);
2862 if (usb_peer_can_wakeup(udev)) {
2863 /* allow device to do remote wakeup */
2864 err = usbd_clear_dev_wakeup(udev);
2866 DPRINTFN(0, "Setting device "
2867 "remote wakeup failed\n");
2871 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2872 /* resume parent HUB first */
2873 usb_dev_resume_peer(udev->parent_hub);
2875 /* reduce chance of instant resume failure by waiting a little bit */
2876 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2878 /* resume current port (Valid in Host and Device Mode) */
2879 err = usbd_req_clear_port_feature(udev->parent_hub,
2880 NULL, udev->port_no, UHF_PORT_SUSPEND);
2882 /* resume settle time */
2883 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2885 DPRINTF("Suspend was cancelled!\n");
2891 /* notify all sub-devices about suspend */
2892 err = usb_suspend_resume(udev, 1);
2894 usbd_sr_unlock(udev);
2896 if (udev->bus->methods->device_suspend != NULL) {
2899 /* suspend device on the USB controller */
2900 (udev->bus->methods->device_suspend) (udev);
2903 temp = usbd_get_dma_delay(udev);
2905 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2909 if (usb_device_20_compatible(udev)) {
2910 /* suspend current port */
2911 err = usbd_req_set_port_feature(udev->parent_hub,
2912 NULL, udev->port_no, UHF_PORT_SUSPEND);
2914 DPRINTFN(0, "Suspending port failed\n");
2918 /* suspend current port */
2919 err = usbd_req_set_port_link_state(udev->parent_hub,
2920 NULL, udev->port_no, UPS_PORT_LS_U3);
2922 DPRINTFN(0, "Suspending port failed\n");
2927 udev = udev->parent_hub;
2931 /*------------------------------------------------------------------------*
2932 * usbd_set_power_mode
2934 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2936 *------------------------------------------------------------------------*/
2938 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2940 /* filter input argument */
2941 if ((power_mode != USB_POWER_MODE_ON) &&
2942 (power_mode != USB_POWER_MODE_OFF))
2943 power_mode = USB_POWER_MODE_SAVE;
2945 power_mode = usbd_filter_power_mode(udev, power_mode);
2947 udev->power_mode = power_mode; /* update copy of power mode */
2950 usb_bus_power_update(udev->bus);
2952 usb_needs_explore(udev->bus, 0 /* no probe */ );
2956 /*------------------------------------------------------------------------*
2957 * usbd_filter_power_mode
2959 * This function filters the power mode based on hardware requirements.
2960 *------------------------------------------------------------------------*/
2962 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2964 const struct usb_bus_methods *mtod;
2967 mtod = udev->bus->methods;
2970 if (mtod->get_power_mode != NULL)
2971 (mtod->get_power_mode) (udev, &temp);
2973 /* check if we should not filter */
2975 return (power_mode);
2977 /* use fixed power mode given by hardware driver */
2981 /*------------------------------------------------------------------------*
2982 * usbd_start_re_enumerate
2984 * This function starts re-enumeration of the given USB device. This
2985 * function does not need to be called BUS-locked. This function does
2986 * not wait until the re-enumeration is completed.
2987 *------------------------------------------------------------------------*/
2989 usbd_start_re_enumerate(struct usb_device *udev)
2991 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2992 udev->re_enumerate_wait = USB_RE_ENUM_START;
2993 usb_needs_explore(udev->bus, 0);
2997 /*-----------------------------------------------------------------------*
2998 * usbd_start_set_config
3000 * This function starts setting a USB configuration. This function
3001 * does not need to be called BUS-locked. This function does not wait
3002 * until the set USB configuratino is completed.
3003 *------------------------------------------------------------------------*/
3005 usbd_start_set_config(struct usb_device *udev, uint8_t index)
3007 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
3008 if (udev->curr_config_index == index) {
3009 /* no change needed */
3012 udev->next_config_index = index;
3013 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
3014 usb_needs_explore(udev->bus, 0);
3016 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
3017 if (udev->next_config_index == index) {
3018 /* no change needed */
3022 return (USB_ERR_PENDING_REQUESTS);