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;
133 #define UHUB_FLAG_DID_EXPLORE 0x01
136 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
137 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
138 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
139 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
140 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
142 /* prototypes for type checking: */
144 static device_probe_t uhub_probe;
145 static device_attach_t uhub_attach;
146 static device_detach_t uhub_detach;
147 static device_suspend_t uhub_suspend;
148 static device_resume_t uhub_resume;
150 static bus_driver_added_t uhub_driver_added;
151 static bus_child_location_str_t uhub_child_location_string;
152 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
154 static usb_callback_t uhub_intr_callback;
155 #if USB_HAVE_TT_SUPPORT
156 static usb_callback_t uhub_reset_tt_callback;
159 static void usb_dev_resume_peer(struct usb_device *udev);
160 static void usb_dev_suspend_peer(struct usb_device *udev);
161 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
163 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
165 [UHUB_INTR_TRANSFER] = {
166 .type = UE_INTERRUPT,
167 .endpoint = UE_ADDR_ANY,
168 .direction = UE_DIR_ANY,
170 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
171 .bufsize = 0, /* use wMaxPacketSize */
172 .callback = &uhub_intr_callback,
173 .interval = UHUB_INTR_INTERVAL,
175 #if USB_HAVE_TT_SUPPORT
176 [UHUB_RESET_TT_TRANSFER] = {
178 .endpoint = 0x00, /* Control pipe */
179 .direction = UE_DIR_ANY,
180 .bufsize = sizeof(struct usb_device_request),
181 .callback = &uhub_reset_tt_callback,
182 .timeout = 1000, /* 1 second */
183 .usb_mode = USB_MODE_HOST,
189 * driver instance for "hub" connected to "usb"
190 * and "hub" connected to "hub"
192 static devclass_t uhub_devclass;
194 static device_method_t uhub_methods[] = {
195 DEVMETHOD(device_probe, uhub_probe),
196 DEVMETHOD(device_attach, uhub_attach),
197 DEVMETHOD(device_detach, uhub_detach),
199 DEVMETHOD(device_suspend, uhub_suspend),
200 DEVMETHOD(device_resume, uhub_resume),
202 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
203 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
204 DEVMETHOD(bus_driver_added, uhub_driver_added),
208 static driver_t uhub_driver = {
210 .methods = uhub_methods,
211 .size = sizeof(struct uhub_softc)
214 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
215 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
216 MODULE_VERSION(uhub, 1);
219 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
221 struct uhub_softc *sc = usbd_xfer_softc(xfer);
223 switch (USB_GET_STATE(xfer)) {
224 case USB_ST_TRANSFERRED:
227 * This is an indication that some port
228 * has changed status. Notify the bus
229 * event handler thread that we need
230 * to be explored again:
232 usb_needs_explore(sc->sc_udev->bus, 0);
235 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
236 usbd_transfer_submit(xfer);
240 if (xfer->error != USB_ERR_CANCELLED) {
242 * Do a clear-stall. The "stall_pipe" flag
243 * will get cleared before next callback by
246 usbd_xfer_set_stall(xfer);
247 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
248 usbd_transfer_submit(xfer);
254 /*------------------------------------------------------------------------*
257 * This function starts the TT reset USB request
258 *------------------------------------------------------------------------*/
259 #if USB_HAVE_TT_SUPPORT
261 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
263 struct usb_udev_msg *pm = (void *)_pm;
264 struct usb_device *udev = pm->udev;
266 struct uhub_softc *sc;
276 USB_BUS_UNLOCK(udev->bus);
277 USB_MTX_LOCK(&sc->sc_mtx);
279 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
281 USB_MTX_UNLOCK(&sc->sc_mtx);
282 USB_BUS_LOCK(udev->bus);
286 /*------------------------------------------------------------------------*
287 * uhub_tt_buffer_reset_async_locked
289 * This function queues a TT reset for the given USB device and endpoint.
290 *------------------------------------------------------------------------*/
291 #if USB_HAVE_TT_SUPPORT
293 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
295 struct usb_device_request req;
296 struct usb_device *udev;
302 if (child == NULL || ep == NULL)
305 udev = child->parent_hs_hub;
306 port = child->hs_port_no;
313 (udev->speed != USB_SPEED_HIGH) ||
314 (child->speed != USB_SPEED_LOW &&
315 child->speed != USB_SPEED_FULL) ||
316 (child->flags.usb_mode != USB_MODE_HOST) ||
317 (port == 0) || (ep->edesc == NULL)) {
322 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
324 up = hub->ports + port - 1;
326 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
327 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
330 /* if we already received a clear buffer request, reset the whole TT */
331 if (up->req_reset_tt.bRequest != 0) {
332 req.bmRequestType = UT_WRITE_CLASS_OTHER;
333 req.bRequest = UR_RESET_TT;
334 USETW(req.wValue, 0);
335 req.wIndex[0] = port;
337 USETW(req.wLength, 0);
339 wValue = (ep->edesc->bEndpointAddress & 0xF) |
340 ((child->address & 0x7F) << 4) |
341 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
342 ((ep->edesc->bmAttributes & 3) << 12);
344 req.bmRequestType = UT_WRITE_CLASS_OTHER;
345 req.bRequest = UR_CLEAR_TT_BUFFER;
346 USETW(req.wValue, wValue);
347 req.wIndex[0] = port;
349 USETW(req.wLength, 0);
351 up->req_reset_tt = req;
352 /* get reset transfer started */
353 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
354 &hub->tt_msg[0], &hub->tt_msg[1]);
358 #if USB_HAVE_TT_SUPPORT
360 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
362 struct uhub_softc *sc;
363 struct usb_device *udev;
367 DPRINTF("TT buffer reset\n");
369 sc = usbd_xfer_softc(xfer);
372 switch (USB_GET_STATE(xfer)) {
373 case USB_ST_TRANSFERRED:
376 USB_BUS_LOCK(udev->bus);
377 /* find first port which needs a TT reset */
378 for (x = 0; x != udev->hub->nports; x++) {
379 up = udev->hub->ports + x;
381 if (up->req_reset_tt.bRequest == 0)
384 /* copy in the transfer */
385 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
386 sizeof(up->req_reset_tt));
388 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
391 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
393 USB_BUS_UNLOCK(udev->bus);
395 usbd_transfer_submit(xfer);
398 USB_BUS_UNLOCK(udev->bus);
402 if (error == USB_ERR_CANCELLED)
405 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
411 /*------------------------------------------------------------------------*
412 * uhub_count_active_host_ports
414 * This function counts the number of active ports at the given speed.
415 *------------------------------------------------------------------------*/
417 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
419 struct uhub_softc *sc;
420 struct usb_device *child;
435 for (x = 0; x != hub->nports; x++) {
437 child = usb_bus_port_get_device(udev->bus, up);
439 child->flags.usb_mode == USB_MODE_HOST &&
440 child->speed == speed)
448 uhub_explore_handle_re_enumerate(struct usb_device *child)
453 /* check if device should be re-enumerated */
454 if (child->flags.usb_mode != USB_MODE_HOST)
457 do_unlock = usbd_enum_lock(child);
458 switch (child->re_enumerate_wait) {
459 case USB_RE_ENUM_START:
460 err = usbd_set_config_index(child,
463 DPRINTF("Unconfigure failed: %s: Ignored.\n",
466 if (child->parent_hub == NULL) {
467 /* the root HUB cannot be re-enumerated */
468 DPRINTFN(6, "cannot reset root HUB\n");
471 err = usbd_req_re_enumerate(child, NULL);
474 err = usbd_set_config_index(child, 0);
476 err = usb_probe_and_attach(child,
477 USB_IFACE_INDEX_ANY);
479 child->re_enumerate_wait = USB_RE_ENUM_DONE;
482 case USB_RE_ENUM_PWR_OFF:
483 /* get the device unconfigured */
484 err = usbd_set_config_index(child,
487 DPRINTFN(0, "Could not unconfigure "
488 "device (ignored)\n");
490 if (child->parent_hub == NULL) {
491 /* the root HUB cannot be re-enumerated */
492 DPRINTFN(6, "cannot set port feature\n");
495 /* clear port enable */
496 err = usbd_req_clear_port_feature(child->parent_hub,
497 NULL, child->port_no, UHF_PORT_ENABLE);
499 DPRINTFN(0, "Could not disable port "
503 child->re_enumerate_wait = USB_RE_ENUM_DONE;
506 case USB_RE_ENUM_SET_CONFIG:
507 err = usbd_set_config_index(child,
508 child->next_config_index);
510 DPRINTF("Configure failed: %s: Ignored.\n",
513 err = usb_probe_and_attach(child,
514 USB_IFACE_INDEX_ANY);
516 child->re_enumerate_wait = USB_RE_ENUM_DONE;
520 child->re_enumerate_wait = USB_RE_ENUM_DONE;
524 usbd_enum_unlock(child);
527 /*------------------------------------------------------------------------*
528 * uhub_explore_sub - subroutine
532 * Else: A control transaction failed
533 *------------------------------------------------------------------------*/
535 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
538 struct usb_device *child;
542 bus = sc->sc_udev->bus;
545 /* get driver added refcount from USB bus */
546 refcount = bus->driver_added_refcount;
548 /* get device assosiated with the given port */
549 child = usb_bus_port_get_device(bus, up);
555 uhub_explore_handle_re_enumerate(child);
557 /* check if probe and attach should be done */
559 if (child->driver_added_refcount != refcount) {
560 child->driver_added_refcount = refcount;
561 err = usb_probe_and_attach(child,
562 USB_IFACE_INDEX_ANY);
567 /* start control transfer, if device mode */
569 if (child->flags.usb_mode == USB_MODE_DEVICE)
570 usbd_ctrl_transfer_setup(child);
572 /* if a HUB becomes present, do a recursive HUB explore */
575 err = (child->hub->explore) (child);
581 /*------------------------------------------------------------------------*
582 * uhub_read_port_status - factored out code
583 *------------------------------------------------------------------------*/
585 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
587 struct usb_port_status ps;
590 err = usbd_req_get_port_status(
591 sc->sc_udev, NULL, &ps, portno);
593 /* update status regardless of error */
595 sc->sc_st.port_status = UGETW(ps.wPortStatus);
596 sc->sc_st.port_change = UGETW(ps.wPortChange);
598 /* debugging print */
600 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
601 "wPortChange=0x%04x, err=%s\n",
602 portno, sc->sc_st.port_status,
603 sc->sc_st.port_change, usbd_errstr(err));
607 /*------------------------------------------------------------------------*
612 * Else: A control transaction failed
613 *------------------------------------------------------------------------*/
615 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
617 struct usb_device *child;
618 struct usb_device *udev;
619 enum usb_dev_speed speed;
620 enum usb_hc_mode mode;
625 DPRINTF("reattaching port %d\n", portno);
629 child = usb_bus_port_get_device(udev->bus,
630 udev->hub->ports + portno - 1);
634 /* first clear the port connection change bit */
636 err = usbd_req_clear_port_feature(udev, NULL,
637 portno, UHF_C_PORT_CONNECTION);
642 /* check if there is a child */
646 * Free USB device and all subdevices, if any.
648 usb_free_device(child, 0);
651 /* get fresh status */
653 err = uhub_read_port_status(sc, portno);
657 #if USB_HAVE_DISABLE_ENUM
658 /* check if we should skip enumeration from this USB HUB */
659 if (usb_disable_enumeration != 0 ||
660 sc->sc_disable_enumeration != 0) {
661 DPRINTF("Enumeration is disabled!\n");
665 /* check if nothing is connected to the port */
667 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
670 /* check if there is no power on the port and print a warning */
672 switch (udev->speed) {
676 power_mask = UPS_PORT_POWER;
678 case USB_SPEED_SUPER:
679 if (udev->parent_hub == NULL)
680 power_mask = UPS_PORT_POWER;
682 power_mask = UPS_PORT_POWER_SS;
688 if (!(sc->sc_st.port_status & power_mask)) {
689 DPRINTF("WARNING: strange, connected port %d "
690 "has no power\n", portno);
693 /* check if the device is in Host Mode */
695 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
697 DPRINTF("Port %d is in Host Mode\n", portno);
699 if (sc->sc_st.port_status & UPS_SUSPEND) {
701 * NOTE: Should not get here in SuperSpeed
702 * mode, because the HUB should report this
705 DPRINTF("Port %d was still "
706 "suspended, clearing.\n", portno);
707 err = usbd_req_clear_port_feature(udev,
708 NULL, portno, UHF_PORT_SUSPEND);
713 /* wait for maximum device power up time */
716 USB_MS_TO_TICKS(usb_port_powerup_delay));
718 /* reset port, which implies enabling it */
720 err = usbd_req_reset_port(udev, NULL, portno);
723 DPRINTFN(0, "port %d reset "
724 "failed, error=%s\n",
725 portno, usbd_errstr(err));
728 /* get port status again, it might have changed during reset */
730 err = uhub_read_port_status(sc, portno);
734 /* check if something changed during port reset */
736 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
737 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
739 DPRINTFN(0, "giving up port reset "
740 "- device vanished\n");
747 DPRINTF("Port %d is in Device Mode\n", portno);
751 * Figure out the device speed
753 switch (udev->speed) {
755 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
756 speed = USB_SPEED_HIGH;
757 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
758 speed = USB_SPEED_LOW;
760 speed = USB_SPEED_FULL;
763 if (sc->sc_st.port_status & UPS_LOW_SPEED)
764 speed = USB_SPEED_LOW;
766 speed = USB_SPEED_FULL;
769 speed = USB_SPEED_LOW;
771 case USB_SPEED_SUPER:
772 if (udev->parent_hub == NULL) {
773 /* Root HUB - special case */
774 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
776 speed = USB_SPEED_FULL;
779 speed = USB_SPEED_LOW;
782 speed = USB_SPEED_HIGH;
785 speed = USB_SPEED_SUPER;
789 speed = USB_SPEED_SUPER;
793 /* same speed like parent */
797 if (speed == USB_SPEED_SUPER) {
798 err = usbd_req_set_hub_u1_timeout(udev, NULL,
799 portno, 128 - (2 * udev->depth));
801 DPRINTFN(0, "port %d U1 timeout "
802 "failed, error=%s\n",
803 portno, usbd_errstr(err));
805 err = usbd_req_set_hub_u2_timeout(udev, NULL,
806 portno, 128 - (2 * udev->depth));
808 DPRINTFN(0, "port %d U2 timeout "
809 "failed, error=%s\n",
810 portno, usbd_errstr(err));
815 * Figure out the device mode
817 * NOTE: This part is currently FreeBSD specific.
819 if (udev->parent_hub != NULL) {
820 /* inherit mode from the parent HUB */
821 mode = udev->parent_hub->flags.usb_mode;
822 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
823 mode = USB_MODE_DEVICE;
825 mode = USB_MODE_HOST;
827 /* need to create a new child */
828 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
829 udev->depth + 1, portno - 1, portno, speed, mode);
831 DPRINTFN(0, "could not allocate new device\n");
834 return (0); /* success */
839 * Free USB device and all subdevices, if any.
841 usb_free_device(child, 0);
845 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
846 err = usbd_req_clear_port_feature(
848 portno, UHF_PORT_ENABLE);
852 DPRINTFN(0, "device problem (%s), "
853 "disabling port %d\n", usbd_errstr(err), portno);
858 /*------------------------------------------------------------------------*
859 * usb_device_20_compatible
862 * 0: HUB does not support suspend and resume
863 * Else: HUB supports suspend and resume
864 *------------------------------------------------------------------------*/
866 usb_device_20_compatible(struct usb_device *udev)
870 switch (udev->speed) {
880 /*------------------------------------------------------------------------*
881 * uhub_suspend_resume_port
885 * Else: A control transaction failed
886 *------------------------------------------------------------------------*/
888 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
890 struct usb_device *child;
891 struct usb_device *udev;
895 DPRINTF("port %d\n", portno);
898 child = usb_bus_port_get_device(udev->bus,
899 udev->hub->ports + portno - 1);
901 /* first clear the port suspend change bit */
903 if (usb_device_20_compatible(udev)) {
904 err = usbd_req_clear_port_feature(udev, NULL,
905 portno, UHF_C_PORT_SUSPEND);
907 err = usbd_req_clear_port_feature(udev, NULL,
908 portno, UHF_C_PORT_LINK_STATE);
912 DPRINTF("clearing suspend failed.\n");
915 /* get fresh status */
917 err = uhub_read_port_status(sc, portno);
919 DPRINTF("reading port status failed.\n");
922 /* convert current state */
924 if (usb_device_20_compatible(udev)) {
925 if (sc->sc_st.port_status & UPS_SUSPEND) {
931 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
935 case UPS_PORT_LS_SS_INA:
936 usbd_req_warm_reset_port(udev, NULL, portno);
945 DPRINTF("suspended=%u\n", is_suspend);
947 /* do the suspend or resume */
951 * This code handle two cases: 1) Host Mode - we can only
952 * receive resume here 2) Device Mode - we can receive
953 * suspend and resume here
956 usb_dev_resume_peer(child);
957 else if (child->flags.usb_mode == USB_MODE_DEVICE)
958 usb_dev_suspend_peer(child);
964 /*------------------------------------------------------------------------*
965 * uhub_root_interrupt
967 * This function is called when a Root HUB interrupt has
968 * happened. "ptr" and "len" makes up the Root HUB interrupt
969 * packet. This function is called having the "bus_mtx" locked.
970 *------------------------------------------------------------------------*/
972 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
974 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
976 usb_needs_explore(bus, 0);
980 uhub_is_too_deep(struct usb_device *udev)
982 switch (udev->speed) {
986 if (udev->depth > USB_HUB_MAX_DEPTH)
989 case USB_SPEED_SUPER:
990 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
999 /*------------------------------------------------------------------------*
1005 *------------------------------------------------------------------------*/
1007 uhub_explore(struct usb_device *udev)
1009 struct usb_hub *hub;
1010 struct uhub_softc *sc;
1011 struct usb_port *up;
1020 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1022 /* ignore devices that are too deep */
1023 if (uhub_is_too_deep(udev))
1024 return (USB_ERR_TOO_DEEP);
1026 /* check if device is suspended */
1027 if (udev->flags.self_suspended) {
1028 /* need to wait until the child signals resume */
1029 DPRINTF("Device is suspended!\n");
1034 * Make sure we don't race against user-space applications
1037 do_unlock = usbd_enum_lock(udev);
1039 for (x = 0; x != hub->nports; x++) {
1040 up = hub->ports + x;
1043 err = uhub_read_port_status(sc, portno);
1045 /* most likely the HUB is gone */
1048 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1049 DPRINTF("Overcurrent on port %u.\n", portno);
1050 err = usbd_req_clear_port_feature(
1051 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1053 /* most likely the HUB is gone */
1057 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1059 * Fake a connect status change so that the
1060 * status gets checked initially!
1062 sc->sc_st.port_change |=
1063 UPS_C_CONNECT_STATUS;
1065 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1066 err = usbd_req_clear_port_feature(
1067 udev, NULL, portno, UHF_C_PORT_ENABLE);
1069 /* most likely the HUB is gone */
1072 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1074 * Ignore the port error if the device
1077 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1078 DPRINTFN(0, "illegal enable change, "
1079 "port %d\n", portno);
1082 if (up->restartcnt == USB_RESTART_MAX) {
1083 /* XXX could try another speed ? */
1084 DPRINTFN(0, "port error, giving up "
1085 "port %d\n", portno);
1087 sc->sc_st.port_change |=
1088 UPS_C_CONNECT_STATUS;
1093 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1094 err = uhub_reattach_port(sc, portno);
1096 /* most likely the HUB is gone */
1100 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1101 UPS_C_PORT_LINK_STATE)) {
1102 err = uhub_suspend_resume_port(sc, portno);
1104 /* most likely the HUB is gone */
1108 err = uhub_explore_sub(sc, up);
1110 /* no device(s) present */
1113 /* explore succeeded - reset restart counter */
1118 usbd_enum_unlock(udev);
1120 /* initial status checked */
1121 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1123 /* return success */
1124 return (USB_ERR_NORMAL_COMPLETION);
1128 uhub_probe(device_t dev)
1130 struct usb_attach_arg *uaa = device_get_ivars(dev);
1132 if (uaa->usb_mode != USB_MODE_HOST)
1136 * The subclass for USB HUBs is currently ignored because it
1137 * is 0 for some and 1 for others.
1139 if (uaa->info.bConfigIndex == 0 &&
1140 uaa->info.bDeviceClass == UDCLASS_HUB)
1146 /* NOTE: The information returned by this function can be wrong. */
1148 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1150 struct usb_hub_descriptor hubdesc20;
1151 struct usb_hub_ss_descriptor hubdesc30;
1156 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1157 return (USB_ERR_INVAL);
1162 switch (udev->speed) {
1164 case USB_SPEED_FULL:
1165 case USB_SPEED_HIGH:
1166 /* assuming that there is one port */
1167 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1169 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1170 "error=%s\n", usbd_errstr(err));
1173 nports = hubdesc20.bNbrPorts;
1177 if (udev->speed == USB_SPEED_HIGH)
1178 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1181 case USB_SPEED_SUPER:
1182 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1184 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1185 "error=%s\n", usbd_errstr(err));
1188 nports = hubdesc30.bNbrPorts;
1194 err = USB_ERR_INVAL;
1198 if (pnports != NULL)
1208 uhub_attach(device_t dev)
1210 struct uhub_softc *sc = device_get_softc(dev);
1211 struct usb_attach_arg *uaa = device_get_ivars(dev);
1212 struct usb_device *udev = uaa->device;
1213 struct usb_device *parent_hub = udev->parent_hub;
1214 struct usb_hub *hub;
1215 struct usb_hub_descriptor hubdesc20;
1216 struct usb_hub_ss_descriptor hubdesc30;
1217 #if USB_HAVE_DISABLE_ENUM
1218 struct sysctl_ctx_list *sysctl_ctx;
1219 struct sysctl_oid *sysctl_tree;
1226 uint8_t iface_index;
1232 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1234 device_set_usb_desc(dev);
1236 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1237 "parent->selfpowered=%d\n",
1239 udev->flags.self_powered,
1242 parent_hub->flags.self_powered : 0);
1244 if (uhub_is_too_deep(udev)) {
1245 DPRINTFN(0, "HUB at depth %d, "
1246 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1250 if (!udev->flags.self_powered && parent_hub &&
1251 !parent_hub->flags.self_powered) {
1252 DPRINTFN(0, "Bus powered HUB connected to "
1253 "bus powered HUB. HUB ignored\n");
1257 if (UHUB_IS_MULTI_TT(sc)) {
1258 err = usbd_set_alt_interface_index(udev, 0, 1);
1260 device_printf(dev, "MTT could not be enabled\n");
1263 device_printf(dev, "MTT enabled\n");
1266 /* get HUB descriptor */
1268 DPRINTFN(2, "Getting HUB descriptor\n");
1270 switch (udev->speed) {
1272 case USB_SPEED_FULL:
1273 case USB_SPEED_HIGH:
1274 /* assuming that there is one port */
1275 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1277 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1278 "error=%s\n", usbd_errstr(err));
1281 /* get number of ports */
1282 nports = hubdesc20.bNbrPorts;
1284 /* get power delay */
1285 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1286 usb_extra_power_up_time);
1288 /* get complete HUB descriptor */
1290 /* check number of ports */
1292 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1293 "error=%s\n", usbd_errstr(err));
1296 /* get complete HUB descriptor */
1297 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1300 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1301 "error=%s\n", usbd_errstr(err));
1304 if (hubdesc20.bNbrPorts != nports) {
1305 DPRINTFN(0, "Number of ports changed\n");
1310 case USB_SPEED_SUPER:
1311 if (udev->parent_hub != NULL) {
1312 err = usbd_req_set_hub_depth(udev, NULL,
1315 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1316 "error=%s\n", usbd_errstr(err));
1320 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1322 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1323 "error=%s\n", usbd_errstr(err));
1326 /* get number of ports */
1327 nports = hubdesc30.bNbrPorts;
1329 /* get power delay */
1330 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1331 usb_extra_power_up_time);
1333 /* get complete HUB descriptor */
1335 /* check number of ports */
1336 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1337 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1338 "error=%s\n", usbd_errstr(err));
1341 /* get complete HUB descriptor */
1342 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1345 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1346 "error=%s\n", usbd_errstr(err));
1349 if (hubdesc30.bNbrPorts != nports) {
1350 DPRINTFN(0, "Number of ports changed\n");
1356 DPRINTF("Assuming HUB has only one port\n");
1357 /* default number of ports */
1359 /* default power delay */
1360 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1364 DPRINTFN(0, "portless HUB\n");
1367 if (nports > USB_MAX_PORTS) {
1368 DPRINTF("Port limit exceeded\n");
1371 #if (USB_HAVE_FIXED_PORT == 0)
1372 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1373 M_USBDEV, M_WAITOK | M_ZERO);
1382 /* initialize HUB structure */
1384 hub->explore = &uhub_explore;
1385 hub->nports = nports;
1386 hub->hubudev = udev;
1387 #if USB_HAVE_TT_SUPPORT
1388 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1389 hub->tt_msg[0].udev = udev;
1390 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1391 hub->tt_msg[1].udev = udev;
1393 /* if self powered hub, give ports maximum current */
1394 if (udev->flags.self_powered) {
1395 hub->portpower = USB_MAX_POWER;
1397 hub->portpower = USB_MIN_POWER;
1400 /* set up interrupt pipe */
1402 if (udev->parent_hub == NULL) {
1403 /* root HUB is special */
1407 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1408 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1411 DPRINTFN(0, "cannot setup interrupt transfer, "
1412 "errstr=%s\n", usbd_errstr(err));
1415 /* wait with power off for a while */
1416 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1418 #if USB_HAVE_DISABLE_ENUM
1419 /* Add device sysctls */
1421 sysctl_ctx = device_get_sysctl_ctx(dev);
1422 sysctl_tree = device_get_sysctl_tree(dev);
1424 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1425 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1426 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1427 &sc->sc_disable_enumeration, 0,
1428 "Set to disable enumeration on this USB HUB.");
1430 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1431 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1432 &sc->sc_disable_port_power, 0,
1433 "Set to disable USB port power on this USB HUB.");
1437 * To have the best chance of success we do things in the exact same
1438 * order as Windoze98. This should not be necessary, but some
1439 * devices do not follow the USB specs to the letter.
1441 * These are the events on the bus when a hub is attached:
1442 * Get device and config descriptors (see attach code)
1443 * Get hub descriptor (see above)
1446 * wait for power to become stable
1447 * (all below happens in explore code)
1449 * clear C_PORT_CONNECTION
1452 * if device connected
1456 * clear C_PORT_RESET
1458 * proceed with device attachment
1461 /* XXX should check for none, individual, or ganged power? */
1465 for (x = 0; x != nports; x++) {
1466 /* set up data structures */
1467 struct usb_port *up = hub->ports + x;
1469 up->device_index = 0;
1473 /* check if port is removable */
1474 switch (udev->speed) {
1476 case USB_SPEED_FULL:
1477 case USB_SPEED_HIGH:
1478 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1481 case USB_SPEED_SUPER:
1482 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1486 DPRINTF("Assuming removable port\n");
1491 #if USB_HAVE_DISABLE_ENUM
1492 /* check if we should disable USB port power or not */
1493 if (usb_disable_port_power != 0 ||
1494 sc->sc_disable_port_power != 0) {
1495 /* turn the power off */
1496 DPRINTFN(2, "Turning port %d power off\n", portno);
1497 err = usbd_req_clear_port_feature(udev, NULL,
1498 portno, UHF_PORT_POWER);
1501 /* turn the power on */
1502 DPRINTFN(2, "Turning port %d power on\n", portno);
1503 err = usbd_req_set_port_feature(udev, NULL,
1504 portno, UHF_PORT_POWER);
1505 #if USB_HAVE_DISABLE_ENUM
1510 DPRINTFN(0, "port %d power on or off failed, %s\n",
1511 portno, usbd_errstr(err));
1513 DPRINTF("turn on port %d power\n",
1516 /* wait for stable power */
1517 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1520 device_printf(dev, "%d port%s with %d "
1521 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1522 removable, udev->flags.self_powered ? "self" : "bus");
1524 /* Start the interrupt endpoint, if any */
1526 USB_MTX_LOCK(&sc->sc_mtx);
1527 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1528 USB_MTX_UNLOCK(&sc->sc_mtx);
1530 /* Enable automatic power save on all USB HUBs */
1532 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1537 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1539 #if (USB_HAVE_FIXED_PORT == 0)
1540 free(udev->hub, M_USBDEV);
1544 mtx_destroy(&sc->sc_mtx);
1550 * Called from process context when the hub is gone.
1551 * Detach all devices on active ports.
1554 uhub_detach(device_t dev)
1556 struct uhub_softc *sc = device_get_softc(dev);
1557 struct usb_hub *hub = sc->sc_udev->hub;
1558 struct usb_bus *bus = sc->sc_udev->bus;
1559 struct usb_device *child;
1562 if (hub == NULL) /* must be partially working */
1565 /* Make sure interrupt transfer is gone. */
1566 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1568 /* Detach all ports */
1569 for (x = 0; x != hub->nports; x++) {
1571 child = usb_bus_port_get_device(bus, hub->ports + x);
1573 if (child == NULL) {
1578 * Free USB device and all subdevices, if any.
1580 usb_free_device(child, 0);
1583 #if USB_HAVE_TT_SUPPORT
1584 /* Make sure our TT messages are not queued anywhere */
1586 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1587 &hub->tt_msg[0], &hub->tt_msg[1]);
1588 USB_BUS_UNLOCK(bus);
1591 #if (USB_HAVE_FIXED_PORT == 0)
1592 free(hub, M_USBDEV);
1594 sc->sc_udev->hub = NULL;
1596 mtx_destroy(&sc->sc_mtx);
1602 uhub_suspend(device_t dev)
1605 /* Sub-devices are not suspended here! */
1610 uhub_resume(device_t dev)
1613 /* Sub-devices are not resumed here! */
1618 uhub_driver_added(device_t dev, driver_t *driver)
1620 usb_needs_explore_all();
1624 struct usb_device *udev;
1626 uint8_t iface_index;
1630 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1631 struct hub_result *res)
1633 struct usb_interface *iface;
1634 struct usb_device *udev;
1639 nports = hub->nports;
1640 for (x = 0; x != nports; x++) {
1641 udev = usb_bus_port_get_device(hub->hubudev->bus,
1646 for (i = 0; i != USB_IFACE_MAX; i++) {
1647 iface = usbd_get_iface(udev, i);
1649 (iface->subdev == child)) {
1650 res->iface_index = i;
1652 res->portno = x + 1;
1657 res->iface_index = 0;
1663 uhub_child_location_string(device_t parent, device_t child,
1664 char *buf, size_t buflen)
1666 struct uhub_softc *sc;
1667 struct usb_hub *hub;
1668 struct hub_result res;
1670 if (!device_is_attached(parent)) {
1676 sc = device_get_softc(parent);
1677 hub = sc->sc_udev->hub;
1680 uhub_find_iface_index(hub, child, &res);
1682 DPRINTF("device not on hub\n");
1688 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1693 , device_get_unit(res.udev->bus->bdev)
1694 , (res.udev->parent_hub != NULL) ?
1695 res.udev->parent_hub->device_index : 0
1696 , res.portno, res.udev->device_index, res.iface_index
1698 , res.udev->ugen_name
1708 uhub_child_pnpinfo_string(device_t parent, device_t child,
1709 char *buf, size_t buflen)
1711 struct uhub_softc *sc;
1712 struct usb_hub *hub;
1713 struct usb_interface *iface;
1714 struct hub_result res;
1716 if (!device_is_attached(parent)) {
1722 sc = device_get_softc(parent);
1723 hub = sc->sc_udev->hub;
1726 uhub_find_iface_index(hub, child, &res);
1728 DPRINTF("device not on hub\n");
1734 iface = usbd_get_iface(res.udev, res.iface_index);
1735 if (iface && iface->idesc) {
1736 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1737 "devclass=0x%02x devsubclass=0x%02x "
1742 "intclass=0x%02x intsubclass=0x%02x "
1743 "intprotocol=0x%02x" "%s%s",
1744 UGETW(res.udev->ddesc.idVendor),
1745 UGETW(res.udev->ddesc.idProduct),
1746 res.udev->ddesc.bDeviceClass,
1747 res.udev->ddesc.bDeviceSubClass,
1748 res.udev->ddesc.bDeviceProtocol,
1749 usb_get_serial(res.udev),
1750 UGETW(res.udev->ddesc.bcdDevice),
1751 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1752 iface->idesc->bInterfaceClass,
1753 iface->idesc->bInterfaceSubClass,
1754 iface->idesc->bInterfaceProtocol,
1755 iface->pnpinfo ? " " : "",
1756 iface->pnpinfo ? iface->pnpinfo : "");
1770 * The USB Transaction Translator:
1771 * ===============================
1773 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1774 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1775 * USB transfers. To utilize bandwidth dynamically the "scatter and
1776 * gather" principle must be applied. This means that bandwidth must
1777 * be divided into equal parts of bandwidth. With regard to USB all
1778 * data is transferred in smaller packets with length
1779 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1782 * The bandwidth scheduler which I have implemented will simply pack
1783 * the USB transfers back to back until there is no more space in the
1784 * schedule. Out of the 8 microframes which the USB 2.0 standard
1785 * provides, only 6 are available for non-HIGH-speed devices. I have
1786 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1787 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1788 * this division, it is very difficult to allocate and free bandwidth
1791 * NOTE about the Transaction Translator in USB HUBs:
1793 * USB HUBs have a very simple Transaction Translator, that will
1794 * simply pipeline all the SPLIT transactions. That means that the
1795 * transactions will be executed in the order they are queued!
1799 /*------------------------------------------------------------------------*
1800 * usb_intr_find_best_slot
1803 * The best Transaction Translation slot for an interrupt endpoint.
1804 *------------------------------------------------------------------------*/
1806 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1807 uint8_t end, uint8_t mask)
1809 usb_size_t min = (usb_size_t)-1;
1817 /* find the last slot with lesser used bandwidth */
1819 for (x = start; x < end; x++) {
1823 /* compute sum of bandwidth */
1824 for (z = x; z < end; z++) {
1825 if (mask & (1U << (z - x)))
1829 /* check if the current multi-slot is more optimal */
1835 /* check if the mask is about to be shifted out */
1836 if (mask & (1U << (end - 1 - x)))
1842 /*------------------------------------------------------------------------*
1843 * usb_hs_bandwidth_adjust
1845 * This function will update the bandwidth usage for the microframe
1846 * having index "slot" by "len" bytes. "len" can be negative. If the
1847 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1848 * the "slot" argument will be replaced by the slot having least used
1849 * bandwidth. The "mask" argument is used for multi-slot allocations.
1852 * The slot in which the bandwidth update was done: 0..7
1853 *------------------------------------------------------------------------*/
1855 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1856 uint8_t slot, uint8_t mask)
1858 struct usb_bus *bus = udev->bus;
1859 struct usb_hub *hub;
1860 enum usb_dev_speed speed;
1863 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1865 speed = usbd_get_speed(udev);
1869 case USB_SPEED_FULL:
1870 if (speed == USB_SPEED_LOW) {
1874 * The Host Controller Driver should have
1875 * performed checks so that the lookup
1876 * below does not result in a NULL pointer
1880 hub = udev->parent_hs_hub->hub;
1881 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1882 slot = usb_intr_find_best_slot(hub->uframe_usage,
1883 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1885 for (x = slot; x < 8; x++) {
1886 if (mask & (1U << (x - slot))) {
1887 hub->uframe_usage[x] += len;
1888 bus->uframe_usage[x] += len;
1893 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1894 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1895 USB_HS_MICRO_FRAMES_MAX, mask);
1897 for (x = slot; x < 8; x++) {
1898 if (mask & (1U << (x - slot))) {
1899 bus->uframe_usage[x] += len;
1907 /*------------------------------------------------------------------------*
1908 * usb_hs_bandwidth_alloc
1910 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1911 *------------------------------------------------------------------------*/
1913 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1915 struct usb_device *udev;
1920 udev = xfer->xroot->udev;
1922 if (udev->flags.usb_mode != USB_MODE_HOST)
1923 return; /* not supported */
1925 xfer->endpoint->refcount_bw++;
1926 if (xfer->endpoint->refcount_bw != 1)
1927 return; /* already allocated */
1929 speed = usbd_get_speed(udev);
1931 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1933 /* allocate a microframe slot */
1936 slot = usb_hs_bandwidth_adjust(udev,
1937 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1939 xfer->endpoint->usb_uframe = slot;
1940 xfer->endpoint->usb_smask = mask << slot;
1942 if ((speed != USB_SPEED_FULL) &&
1943 (speed != USB_SPEED_LOW)) {
1944 xfer->endpoint->usb_cmask = 0x00 ;
1946 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1950 case UE_ISOCHRONOUS:
1951 switch (usbd_xfer_get_fps_shift(xfer)) {
1966 /* allocate a microframe multi-slot */
1968 slot = usb_hs_bandwidth_adjust(udev,
1969 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1971 xfer->endpoint->usb_uframe = slot;
1972 xfer->endpoint->usb_cmask = 0;
1973 xfer->endpoint->usb_smask = mask << slot;
1977 xfer->endpoint->usb_uframe = 0;
1978 xfer->endpoint->usb_cmask = 0;
1979 xfer->endpoint->usb_smask = 0;
1983 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1984 xfer->endpoint->usb_uframe,
1985 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1988 /*------------------------------------------------------------------------*
1989 * usb_hs_bandwidth_free
1991 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1992 *------------------------------------------------------------------------*/
1994 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1996 struct usb_device *udev;
2000 udev = xfer->xroot->udev;
2002 if (udev->flags.usb_mode != USB_MODE_HOST)
2003 return; /* not supported */
2005 xfer->endpoint->refcount_bw--;
2006 if (xfer->endpoint->refcount_bw != 0)
2007 return; /* still allocated */
2009 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2011 case UE_ISOCHRONOUS:
2013 slot = xfer->endpoint->usb_uframe;
2014 mask = xfer->endpoint->usb_smask;
2016 /* free microframe slot(s): */
2017 usb_hs_bandwidth_adjust(udev,
2018 -xfer->max_frame_size, slot, mask >> slot);
2020 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2021 slot, mask >> slot);
2023 xfer->endpoint->usb_uframe = 0;
2024 xfer->endpoint->usb_cmask = 0;
2025 xfer->endpoint->usb_smask = 0;
2033 /*------------------------------------------------------------------------*
2034 * usb_isoc_time_expand
2036 * This function will expand the time counter from 7-bit to 16-bit.
2039 * 16-bit isochronous time counter.
2040 *------------------------------------------------------------------------*/
2042 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2046 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2048 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2050 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2052 if (isoc_time_curr < rem) {
2053 /* the time counter wrapped around */
2054 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2056 /* update the remainder */
2058 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2059 bus->isoc_time_last |= isoc_time_curr;
2061 return (bus->isoc_time_last);
2064 /*------------------------------------------------------------------------*
2065 * usbd_fs_isoc_schedule_alloc_slot
2067 * This function will allocate bandwidth for an isochronous FULL speed
2068 * transaction in the FULL speed schedule.
2073 *------------------------------------------------------------------------*/
2074 #if USB_HAVE_TT_SUPPORT
2076 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2078 struct usb_xfer *xfer;
2079 struct usb_xfer *pipe_xfer;
2080 struct usb_bus *bus;
2082 usb_frlength_t data_len;
2090 bus = isoc_xfer->xroot->bus;
2092 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2094 /* skip self, if any */
2096 if (xfer == isoc_xfer)
2099 /* check if this USB transfer is going through the same TT */
2101 if (xfer->xroot->udev->parent_hs_hub !=
2102 isoc_xfer->xroot->udev->parent_hs_hub) {
2105 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2106 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2107 (xfer->xroot->udev->hs_port_no !=
2108 isoc_xfer->xroot->udev->hs_port_no)) {
2111 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2114 /* check if isoc_time is part of this transfer */
2116 delta = xfer->isoc_time_complete - isoc_time;
2117 if (delta > 0 && delta <= xfer->nframes) {
2118 delta = xfer->nframes - delta;
2120 len = xfer->frlengths[delta];
2129 * Check double buffered transfers. Only stream ID
2130 * equal to zero is valid here!
2132 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2135 /* skip self, if any */
2137 if (pipe_xfer == isoc_xfer)
2140 /* check if isoc_time is part of this transfer */
2142 delta = pipe_xfer->isoc_time_complete - isoc_time;
2143 if (delta > 0 && delta <= pipe_xfer->nframes) {
2144 delta = pipe_xfer->nframes - delta;
2146 len = pipe_xfer->frlengths[delta];
2156 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2157 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2161 /* check for overflow */
2163 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2168 delta = isoc_xfer->isoc_time_complete - isoc_time;
2169 if (delta > 0 && delta <= isoc_xfer->nframes) {
2170 delta = isoc_xfer->nframes - delta;
2172 len = isoc_xfer->frlengths[delta];
2180 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2181 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2185 /* check for overflow */
2187 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2194 /*------------------------------------------------------------------------*
2195 * usb_bus_port_get_device
2197 * This function is NULL safe.
2198 *------------------------------------------------------------------------*/
2200 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2202 if ((bus == NULL) || (up == NULL)) {
2206 if (up->device_index == 0) {
2210 return (bus->devices[up->device_index]);
2213 /*------------------------------------------------------------------------*
2214 * usb_bus_port_set_device
2216 * This function is NULL safe.
2217 *------------------------------------------------------------------------*/
2219 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2220 struct usb_device *udev, uint8_t device_index)
2227 * There is only one case where we don't
2228 * have an USB port, and that is the Root Hub!
2232 up->device_index = device_index;
2234 device_index = up->device_index;
2235 up->device_index = 0;
2239 * Make relationships to our new device
2241 if (device_index != 0) {
2243 mtx_lock(&usb_ref_lock);
2245 bus->devices[device_index] = udev;
2247 mtx_unlock(&usb_ref_lock);
2253 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2256 /*------------------------------------------------------------------------*
2259 * This functions is called when the USB event thread needs to run.
2260 *------------------------------------------------------------------------*/
2262 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2274 DPRINTF("No bus pointer!\n");
2277 if ((bus->devices == NULL) ||
2278 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2279 DPRINTF("No root HUB\n");
2282 if (mtx_owned(&bus->bus_mtx)) {
2291 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2292 &bus->explore_msg[0], &bus->explore_msg[1])) {
2296 USB_BUS_UNLOCK(bus);
2300 /*------------------------------------------------------------------------*
2301 * usb_needs_explore_all
2303 * This function is called whenever a new driver is loaded and will
2304 * cause that all USB buses are re-explored.
2305 *------------------------------------------------------------------------*/
2307 usb_needs_explore_all(void)
2309 struct usb_bus *bus;
2316 dc = usb_devclass_ptr;
2318 DPRINTFN(0, "no devclass\n");
2322 * Explore all USB buses in parallel.
2324 max = devclass_get_maxunit(dc);
2326 dev = devclass_get_device(dc, max);
2328 bus = device_get_softc(dev);
2330 usb_needs_explore(bus, 1);
2337 /*------------------------------------------------------------------------*
2338 * usb_needs_explore_init
2340 * This function will ensure that the USB controllers are not enumerated
2341 * until the "cold" variable is cleared.
2342 *------------------------------------------------------------------------*/
2344 usb_needs_explore_init(void *arg)
2347 * The cold variable should be cleared prior to this function
2351 usb_needs_explore_all();
2353 DPRINTFN(-1, "Cold variable is still set!\n");
2355 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2357 /*------------------------------------------------------------------------*
2358 * usb_bus_power_update
2360 * This function will ensure that all USB devices on the given bus are
2361 * properly suspended or resumed according to the device transfer
2363 *------------------------------------------------------------------------*/
2366 usb_bus_power_update(struct usb_bus *bus)
2368 usb_needs_explore(bus, 0 /* no probe */ );
2372 /*------------------------------------------------------------------------*
2373 * usbd_transfer_power_ref
2375 * This function will modify the power save reference counts and
2376 * wakeup the USB device associated with the given USB transfer, if
2378 *------------------------------------------------------------------------*/
2381 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2383 static const usb_power_mask_t power_mask[4] = {
2384 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2385 [UE_BULK] = USB_HW_POWER_BULK,
2386 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2387 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2389 struct usb_device *udev;
2390 uint8_t needs_explore;
2391 uint8_t needs_hw_power;
2394 udev = xfer->xroot->udev;
2396 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2397 /* no power save for root HUB */
2400 USB_BUS_LOCK(udev->bus);
2402 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2404 udev->pwr_save.last_xfer_time = ticks;
2405 udev->pwr_save.type_refs[xfer_type] += val;
2407 if (xfer->flags_int.control_xfr) {
2408 udev->pwr_save.read_refs += val;
2409 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2411 * It is not allowed to suspend during a
2414 udev->pwr_save.write_refs += val;
2416 } else if (USB_GET_DATA_ISREAD(xfer)) {
2417 udev->pwr_save.read_refs += val;
2419 udev->pwr_save.write_refs += val;
2423 if (udev->flags.self_suspended)
2424 needs_explore = usb_peer_should_wakeup(udev);
2428 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2429 DPRINTF("Adding type %u to power state\n", xfer_type);
2430 udev->bus->hw_power_state |= power_mask[xfer_type];
2440 USB_BUS_UNLOCK(udev->bus);
2442 if (needs_explore) {
2443 DPRINTF("update\n");
2444 usb_bus_power_update(udev->bus);
2445 } else if (needs_hw_power) {
2446 DPRINTF("needs power\n");
2447 if (udev->bus->methods->set_hw_power != NULL) {
2448 (udev->bus->methods->set_hw_power) (udev->bus);
2454 /*------------------------------------------------------------------------*
2455 * usb_peer_should_wakeup
2457 * This function returns non-zero if the current device should wake up.
2458 *------------------------------------------------------------------------*/
2460 usb_peer_should_wakeup(struct usb_device *udev)
2462 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2463 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2464 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2465 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2466 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2467 (udev->pwr_save.write_refs != 0) ||
2468 ((udev->pwr_save.read_refs != 0) &&
2469 (udev->flags.usb_mode == USB_MODE_HOST) &&
2470 (usb_peer_can_wakeup(udev) == 0)));
2473 /*------------------------------------------------------------------------*
2476 * This function implements the USB power daemon and is called
2477 * regularly from the USB explore thread.
2478 *------------------------------------------------------------------------*/
2481 usb_bus_powerd(struct usb_bus *bus)
2483 struct usb_device *udev;
2486 usb_ticks_t mintime;
2487 usb_size_t type_refs[5];
2490 limit = usb_power_timeout;
2493 else if (limit > 255)
2498 DPRINTF("bus=%p\n", bus);
2503 * The root HUB device is never suspended
2504 * and we simply skip it.
2506 for (x = USB_ROOT_HUB_ADDR + 1;
2507 x != bus->devices_max; x++) {
2509 udev = bus->devices[x];
2513 temp = ticks - udev->pwr_save.last_xfer_time;
2515 if (usb_peer_should_wakeup(udev)) {
2516 /* check if we are suspended */
2517 if (udev->flags.self_suspended != 0) {
2518 USB_BUS_UNLOCK(bus);
2519 usb_dev_resume_peer(udev);
2522 } else if ((temp >= limit) &&
2523 (udev->flags.usb_mode == USB_MODE_HOST) &&
2524 (udev->flags.self_suspended == 0)) {
2525 /* try to do suspend */
2527 USB_BUS_UNLOCK(bus);
2528 usb_dev_suspend_peer(udev);
2533 /* reset counters */
2535 mintime = (usb_ticks_t)-1;
2542 /* Re-loop all the devices to get the actual state */
2544 for (x = USB_ROOT_HUB_ADDR + 1;
2545 x != bus->devices_max; x++) {
2547 udev = bus->devices[x];
2551 /* we found a non-Root-Hub USB device */
2554 /* "last_xfer_time" can be updated by a resume */
2555 temp = ticks - udev->pwr_save.last_xfer_time;
2558 * Compute minimum time since last transfer for the complete
2564 if (udev->flags.self_suspended == 0) {
2565 type_refs[0] += udev->pwr_save.type_refs[0];
2566 type_refs[1] += udev->pwr_save.type_refs[1];
2567 type_refs[2] += udev->pwr_save.type_refs[2];
2568 type_refs[3] += udev->pwr_save.type_refs[3];
2572 if (mintime >= (usb_ticks_t)(1 * hz)) {
2573 /* recompute power masks */
2574 DPRINTF("Recomputing power masks\n");
2575 bus->hw_power_state = 0;
2576 if (type_refs[UE_CONTROL] != 0)
2577 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2578 if (type_refs[UE_BULK] != 0)
2579 bus->hw_power_state |= USB_HW_POWER_BULK;
2580 if (type_refs[UE_INTERRUPT] != 0)
2581 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2582 if (type_refs[UE_ISOCHRONOUS] != 0)
2583 bus->hw_power_state |= USB_HW_POWER_ISOC;
2584 if (type_refs[4] != 0)
2585 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2587 USB_BUS_UNLOCK(bus);
2589 if (bus->methods->set_hw_power != NULL) {
2590 /* always update hardware power! */
2591 (bus->methods->set_hw_power) (bus);
2597 /*------------------------------------------------------------------------*
2598 * usb_dev_resume_peer
2600 * This function will resume an USB peer and do the required USB
2601 * signalling to get an USB device out of the suspended state.
2602 *------------------------------------------------------------------------*/
2604 usb_dev_resume_peer(struct usb_device *udev)
2606 struct usb_bus *bus;
2613 /* check if already resumed */
2614 if (udev->flags.self_suspended == 0)
2617 /* we need a parent HUB to do resume */
2618 if (udev->parent_hub == NULL)
2621 DPRINTF("udev=%p\n", udev);
2623 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2624 (udev->flags.remote_wakeup == 0)) {
2626 * If the host did not set the remote wakeup feature, we can
2627 * not wake it up either!
2629 DPRINTF("remote wakeup is not set!\n");
2632 /* get bus pointer */
2635 /* resume parent hub first */
2636 usb_dev_resume_peer(udev->parent_hub);
2638 /* reduce chance of instant resume failure by waiting a little bit */
2639 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2641 if (usb_device_20_compatible(udev)) {
2642 /* resume current port (Valid in Host and Device Mode) */
2643 err = usbd_req_clear_port_feature(udev->parent_hub,
2644 NULL, udev->port_no, UHF_PORT_SUSPEND);
2646 DPRINTFN(0, "Resuming port failed\n");
2650 /* resume current port (Valid in Host and Device Mode) */
2651 err = usbd_req_set_port_link_state(udev->parent_hub,
2652 NULL, udev->port_no, UPS_PORT_LS_U0);
2654 DPRINTFN(0, "Resuming port failed\n");
2659 /* resume settle time */
2660 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2662 if (bus->methods->device_resume != NULL) {
2663 /* resume USB device on the USB controller */
2664 (bus->methods->device_resume) (udev);
2667 /* set that this device is now resumed */
2668 udev->flags.self_suspended = 0;
2670 /* make sure that we don't go into suspend right away */
2671 udev->pwr_save.last_xfer_time = ticks;
2673 /* make sure the needed power masks are on */
2674 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2675 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2676 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2677 bus->hw_power_state |= USB_HW_POWER_BULK;
2678 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2679 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2680 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2681 bus->hw_power_state |= USB_HW_POWER_ISOC;
2683 USB_BUS_UNLOCK(bus);
2685 if (bus->methods->set_hw_power != NULL) {
2686 /* always update hardware power! */
2687 (bus->methods->set_hw_power) (bus);
2692 /* notify all sub-devices about resume */
2693 err = usb_suspend_resume(udev, 0);
2695 usbd_sr_unlock(udev);
2697 /* check if peer has wakeup capability */
2698 if (usb_peer_can_wakeup(udev)) {
2699 /* clear remote wakeup */
2700 err = usbd_req_clear_device_feature(udev,
2701 NULL, UF_DEVICE_REMOTE_WAKEUP);
2703 DPRINTFN(0, "Clearing device "
2704 "remote wakeup failed: %s\n",
2710 /*------------------------------------------------------------------------*
2711 * usb_dev_suspend_peer
2713 * This function will suspend an USB peer and do the required USB
2714 * signalling to get an USB device into the suspended state.
2715 *------------------------------------------------------------------------*/
2717 usb_dev_suspend_peer(struct usb_device *udev)
2719 struct usb_device *child;
2729 /* check if already suspended */
2730 if (udev->flags.self_suspended)
2733 /* we need a parent HUB to do suspend */
2734 if (udev->parent_hub == NULL)
2737 DPRINTF("udev=%p\n", udev);
2739 /* check if the current device is a HUB */
2740 if (udev->hub != NULL) {
2741 nports = udev->hub->nports;
2743 /* check if all devices on the HUB are suspended */
2744 for (x = 0; x != nports; x++) {
2745 child = usb_bus_port_get_device(udev->bus,
2746 udev->hub->ports + x);
2751 if (child->flags.self_suspended)
2754 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2759 if (usb_peer_can_wakeup(udev)) {
2761 * This request needs to be done before we set
2762 * "udev->flags.self_suspended":
2765 /* allow device to do remote wakeup */
2766 err = usbd_req_set_device_feature(udev,
2767 NULL, UF_DEVICE_REMOTE_WAKEUP);
2769 DPRINTFN(0, "Setting device "
2770 "remote wakeup failed\n");
2774 USB_BUS_LOCK(udev->bus);
2776 * Checking for suspend condition and setting suspended bit
2779 err = usb_peer_should_wakeup(udev);
2782 * Set that this device is suspended. This variable
2783 * must be set before calling USB controller suspend
2786 udev->flags.self_suspended = 1;
2788 USB_BUS_UNLOCK(udev->bus);
2791 if (usb_peer_can_wakeup(udev)) {
2792 /* allow device to do remote wakeup */
2793 err = usbd_req_clear_device_feature(udev,
2794 NULL, UF_DEVICE_REMOTE_WAKEUP);
2796 DPRINTFN(0, "Setting device "
2797 "remote wakeup failed\n");
2801 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2802 /* resume parent HUB first */
2803 usb_dev_resume_peer(udev->parent_hub);
2805 /* reduce chance of instant resume failure by waiting a little bit */
2806 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2808 /* resume current port (Valid in Host and Device Mode) */
2809 err = usbd_req_clear_port_feature(udev->parent_hub,
2810 NULL, udev->port_no, UHF_PORT_SUSPEND);
2812 /* resume settle time */
2813 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2815 DPRINTF("Suspend was cancelled!\n");
2821 /* notify all sub-devices about suspend */
2822 err = usb_suspend_resume(udev, 1);
2824 usbd_sr_unlock(udev);
2826 if (udev->bus->methods->device_suspend != NULL) {
2829 /* suspend device on the USB controller */
2830 (udev->bus->methods->device_suspend) (udev);
2833 temp = usbd_get_dma_delay(udev);
2835 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2839 if (usb_device_20_compatible(udev)) {
2840 /* suspend current port */
2841 err = usbd_req_set_port_feature(udev->parent_hub,
2842 NULL, udev->port_no, UHF_PORT_SUSPEND);
2844 DPRINTFN(0, "Suspending port failed\n");
2848 /* suspend current port */
2849 err = usbd_req_set_port_link_state(udev->parent_hub,
2850 NULL, udev->port_no, UPS_PORT_LS_U3);
2852 DPRINTFN(0, "Suspending port failed\n");
2857 udev = udev->parent_hub;
2861 /*------------------------------------------------------------------------*
2862 * usbd_set_power_mode
2864 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2866 *------------------------------------------------------------------------*/
2868 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2870 /* filter input argument */
2871 if ((power_mode != USB_POWER_MODE_ON) &&
2872 (power_mode != USB_POWER_MODE_OFF))
2873 power_mode = USB_POWER_MODE_SAVE;
2875 power_mode = usbd_filter_power_mode(udev, power_mode);
2877 udev->power_mode = power_mode; /* update copy of power mode */
2880 usb_bus_power_update(udev->bus);
2882 usb_needs_explore(udev->bus, 0 /* no probe */ );
2886 /*------------------------------------------------------------------------*
2887 * usbd_filter_power_mode
2889 * This function filters the power mode based on hardware requirements.
2890 *------------------------------------------------------------------------*/
2892 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2894 const struct usb_bus_methods *mtod;
2897 mtod = udev->bus->methods;
2900 if (mtod->get_power_mode != NULL)
2901 (mtod->get_power_mode) (udev, &temp);
2903 /* check if we should not filter */
2905 return (power_mode);
2907 /* use fixed power mode given by hardware driver */
2911 /*------------------------------------------------------------------------*
2912 * usbd_start_re_enumerate
2914 * This function starts re-enumeration of the given USB device. This
2915 * function does not need to be called BUS-locked. This function does
2916 * not wait until the re-enumeration is completed.
2917 *------------------------------------------------------------------------*/
2919 usbd_start_re_enumerate(struct usb_device *udev)
2921 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2922 udev->re_enumerate_wait = USB_RE_ENUM_START;
2923 usb_needs_explore(udev->bus, 0);
2927 /*-----------------------------------------------------------------------*
2928 * usbd_start_set_config
2930 * This function starts setting a USB configuration. This function
2931 * does not need to be called BUS-locked. This function does not wait
2932 * until the set USB configuratino is completed.
2933 *------------------------------------------------------------------------*/
2935 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2937 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2938 if (udev->curr_config_index == index) {
2939 /* no change needed */
2942 udev->next_config_index = index;
2943 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2944 usb_needs_explore(udev->bus, 0);
2946 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2947 if (udev->next_config_index == index) {
2948 /* no change needed */
2952 return (USB_ERR_PENDING_REQUESTS);