3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR uhub_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define UHUB_INTR_INTERVAL 250 /* ms */
79 #if USB_HAVE_TT_SUPPORT
80 UHUB_RESET_TT_TRANSFER,
86 static int uhub_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
94 static int usb_power_timeout = 30; /* seconds */
96 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
97 &usb_power_timeout, 0, "USB power timeout");
100 #if USB_HAVE_DISABLE_ENUM
101 static int usb_disable_enumeration = 0;
102 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
103 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
104 "This can secure against USB devices turning evil, "
105 "for example a USB memory stick becoming a USB keyboard.");
107 static int usb_disable_port_power = 0;
108 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
109 &usb_disable_port_power, 0, "Set to disable all USB port power.");
112 struct uhub_current_state {
113 uint16_t port_change;
114 uint16_t port_status;
118 struct uhub_current_state sc_st;/* current state */
119 #if (USB_HAVE_FIXED_PORT != 0)
120 struct usb_hub sc_hub;
122 device_t sc_dev; /* base device */
123 struct mtx sc_mtx; /* our mutex */
124 struct usb_device *sc_udev; /* USB device */
125 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
126 #if USB_HAVE_DISABLE_ENUM
127 int sc_disable_enumeration;
128 int sc_disable_port_power;
131 #define UHUB_FLAG_DID_EXPLORE 0x01
134 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
135 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
136 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
137 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
138 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
140 /* prototypes for type checking: */
142 static device_probe_t uhub_probe;
143 static device_attach_t uhub_attach;
144 static device_detach_t uhub_detach;
145 static device_suspend_t uhub_suspend;
146 static device_resume_t uhub_resume;
148 static bus_driver_added_t uhub_driver_added;
149 static bus_child_location_str_t uhub_child_location_string;
150 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
152 static usb_callback_t uhub_intr_callback;
153 #if USB_HAVE_TT_SUPPORT
154 static usb_callback_t uhub_reset_tt_callback;
157 static void usb_dev_resume_peer(struct usb_device *udev);
158 static void usb_dev_suspend_peer(struct usb_device *udev);
159 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
161 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
163 [UHUB_INTR_TRANSFER] = {
164 .type = UE_INTERRUPT,
165 .endpoint = UE_ADDR_ANY,
166 .direction = UE_DIR_ANY,
168 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
169 .bufsize = 0, /* use wMaxPacketSize */
170 .callback = &uhub_intr_callback,
171 .interval = UHUB_INTR_INTERVAL,
173 #if USB_HAVE_TT_SUPPORT
174 [UHUB_RESET_TT_TRANSFER] = {
176 .endpoint = 0x00, /* Control pipe */
177 .direction = UE_DIR_ANY,
178 .bufsize = sizeof(struct usb_device_request),
179 .callback = &uhub_reset_tt_callback,
180 .timeout = 1000, /* 1 second */
181 .usb_mode = USB_MODE_HOST,
187 * driver instance for "hub" connected to "usb"
188 * and "hub" connected to "hub"
190 static devclass_t uhub_devclass;
192 static device_method_t uhub_methods[] = {
193 DEVMETHOD(device_probe, uhub_probe),
194 DEVMETHOD(device_attach, uhub_attach),
195 DEVMETHOD(device_detach, uhub_detach),
197 DEVMETHOD(device_suspend, uhub_suspend),
198 DEVMETHOD(device_resume, uhub_resume),
200 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
201 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
202 DEVMETHOD(bus_driver_added, uhub_driver_added),
206 static driver_t uhub_driver = {
208 .methods = uhub_methods,
209 .size = sizeof(struct uhub_softc)
212 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
213 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
214 MODULE_VERSION(uhub, 1);
217 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
219 struct uhub_softc *sc = usbd_xfer_softc(xfer);
221 switch (USB_GET_STATE(xfer)) {
222 case USB_ST_TRANSFERRED:
225 * This is an indication that some port
226 * has changed status. Notify the bus
227 * event handler thread that we need
228 * to be explored again:
230 usb_needs_explore(sc->sc_udev->bus, 0);
233 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
234 usbd_transfer_submit(xfer);
238 if (xfer->error != USB_ERR_CANCELLED) {
240 * Do a clear-stall. The "stall_pipe" flag
241 * will get cleared before next callback by
244 usbd_xfer_set_stall(xfer);
245 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
246 usbd_transfer_submit(xfer);
252 /*------------------------------------------------------------------------*
255 * This function starts the TT reset USB request
256 *------------------------------------------------------------------------*/
257 #if USB_HAVE_TT_SUPPORT
259 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
261 struct usb_udev_msg *pm = (void *)_pm;
262 struct usb_device *udev = pm->udev;
264 struct uhub_softc *sc;
274 USB_BUS_UNLOCK(udev->bus);
275 USB_MTX_LOCK(&sc->sc_mtx);
277 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
279 USB_MTX_UNLOCK(&sc->sc_mtx);
280 USB_BUS_LOCK(udev->bus);
284 /*------------------------------------------------------------------------*
285 * uhub_tt_buffer_reset_async_locked
287 * This function queues a TT reset for the given USB device and endpoint.
288 *------------------------------------------------------------------------*/
289 #if USB_HAVE_TT_SUPPORT
291 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
293 struct usb_device_request req;
294 struct usb_device *udev;
300 if (child == NULL || ep == NULL)
303 udev = child->parent_hs_hub;
304 port = child->hs_port_no;
311 (udev->speed != USB_SPEED_HIGH) ||
312 (child->speed != USB_SPEED_LOW &&
313 child->speed != USB_SPEED_FULL) ||
314 (child->flags.usb_mode != USB_MODE_HOST) ||
315 (port == 0) || (ep->edesc == NULL)) {
320 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
322 up = hub->ports + port - 1;
324 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
325 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
328 /* if we already received a clear buffer request, reset the whole TT */
329 if (up->req_reset_tt.bRequest != 0) {
330 req.bmRequestType = UT_WRITE_CLASS_OTHER;
331 req.bRequest = UR_RESET_TT;
332 USETW(req.wValue, 0);
333 req.wIndex[0] = port;
335 USETW(req.wLength, 0);
337 wValue = (ep->edesc->bEndpointAddress & 0xF) |
338 ((child->address & 0x7F) << 4) |
339 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
340 ((ep->edesc->bmAttributes & 3) << 12);
342 req.bmRequestType = UT_WRITE_CLASS_OTHER;
343 req.bRequest = UR_CLEAR_TT_BUFFER;
344 USETW(req.wValue, wValue);
345 req.wIndex[0] = port;
347 USETW(req.wLength, 0);
349 up->req_reset_tt = req;
350 /* get reset transfer started */
351 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
352 &hub->tt_msg[0], &hub->tt_msg[1]);
356 #if USB_HAVE_TT_SUPPORT
358 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
360 struct uhub_softc *sc;
361 struct usb_device *udev;
365 DPRINTF("TT buffer reset\n");
367 sc = usbd_xfer_softc(xfer);
370 switch (USB_GET_STATE(xfer)) {
371 case USB_ST_TRANSFERRED:
374 USB_BUS_LOCK(udev->bus);
375 /* find first port which needs a TT reset */
376 for (x = 0; x != udev->hub->nports; x++) {
377 up = udev->hub->ports + x;
379 if (up->req_reset_tt.bRequest == 0)
382 /* copy in the transfer */
383 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
384 sizeof(up->req_reset_tt));
386 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
389 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
391 USB_BUS_UNLOCK(udev->bus);
393 usbd_transfer_submit(xfer);
396 USB_BUS_UNLOCK(udev->bus);
400 if (error == USB_ERR_CANCELLED)
403 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
409 /*------------------------------------------------------------------------*
410 * uhub_count_active_host_ports
412 * This function counts the number of active ports at the given speed.
413 *------------------------------------------------------------------------*/
415 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
417 struct uhub_softc *sc;
418 struct usb_device *child;
433 for (x = 0; x != hub->nports; x++) {
435 child = usb_bus_port_get_device(udev->bus, up);
437 child->flags.usb_mode == USB_MODE_HOST &&
438 child->speed == speed)
446 uhub_explore_handle_re_enumerate(struct usb_device *child)
451 /* check if device should be re-enumerated */
452 if (child->flags.usb_mode != USB_MODE_HOST)
455 do_unlock = usbd_enum_lock(child);
456 switch (child->re_enumerate_wait) {
457 case USB_RE_ENUM_START:
458 err = usbd_set_config_index(child,
461 DPRINTF("Unconfigure failed: %s: Ignored.\n",
464 if (child->parent_hub == NULL) {
465 /* the root HUB cannot be re-enumerated */
466 DPRINTFN(6, "cannot reset root HUB\n");
469 err = usbd_req_re_enumerate(child, NULL);
472 err = usbd_set_config_index(child, 0);
474 err = usb_probe_and_attach(child,
475 USB_IFACE_INDEX_ANY);
477 child->re_enumerate_wait = USB_RE_ENUM_DONE;
480 case USB_RE_ENUM_PWR_OFF:
481 /* get the device unconfigured */
482 err = usbd_set_config_index(child,
485 DPRINTFN(0, "Could not unconfigure "
486 "device (ignored)\n");
488 if (child->parent_hub == NULL) {
489 /* the root HUB cannot be re-enumerated */
490 DPRINTFN(6, "cannot set port feature\n");
493 /* clear port enable */
494 err = usbd_req_clear_port_feature(child->parent_hub,
495 NULL, child->port_no, UHF_PORT_ENABLE);
497 DPRINTFN(0, "Could not disable port "
501 child->re_enumerate_wait = USB_RE_ENUM_DONE;
504 case USB_RE_ENUM_SET_CONFIG:
505 err = usbd_set_config_index(child,
506 child->next_config_index);
508 DPRINTF("Configure failed: %s: Ignored.\n",
511 err = usb_probe_and_attach(child,
512 USB_IFACE_INDEX_ANY);
514 child->re_enumerate_wait = USB_RE_ENUM_DONE;
518 child->re_enumerate_wait = USB_RE_ENUM_DONE;
522 usbd_enum_unlock(child);
525 /*------------------------------------------------------------------------*
526 * uhub_explore_sub - subroutine
530 * Else: A control transaction failed
531 *------------------------------------------------------------------------*/
533 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
536 struct usb_device *child;
540 bus = sc->sc_udev->bus;
543 /* get driver added refcount from USB bus */
544 refcount = bus->driver_added_refcount;
546 /* get device assosiated with the given port */
547 child = usb_bus_port_get_device(bus, up);
553 uhub_explore_handle_re_enumerate(child);
555 /* check if probe and attach should be done */
557 if (child->driver_added_refcount != refcount) {
558 child->driver_added_refcount = refcount;
559 err = usb_probe_and_attach(child,
560 USB_IFACE_INDEX_ANY);
565 /* start control transfer, if device mode */
567 if (child->flags.usb_mode == USB_MODE_DEVICE)
568 usbd_ctrl_transfer_setup(child);
570 /* if a HUB becomes present, do a recursive HUB explore */
573 err = (child->hub->explore) (child);
579 /*------------------------------------------------------------------------*
580 * uhub_read_port_status - factored out code
581 *------------------------------------------------------------------------*/
583 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
585 struct usb_port_status ps;
588 err = usbd_req_get_port_status(
589 sc->sc_udev, NULL, &ps, portno);
591 /* update status regardless of error */
593 sc->sc_st.port_status = UGETW(ps.wPortStatus);
594 sc->sc_st.port_change = UGETW(ps.wPortChange);
596 /* debugging print */
598 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
599 "wPortChange=0x%04x, err=%s\n",
600 portno, sc->sc_st.port_status,
601 sc->sc_st.port_change, usbd_errstr(err));
605 /*------------------------------------------------------------------------*
610 * Else: A control transaction failed
611 *------------------------------------------------------------------------*/
613 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
615 struct usb_device *child;
616 struct usb_device *udev;
617 enum usb_dev_speed speed;
618 enum usb_hc_mode mode;
623 DPRINTF("reattaching port %d\n", portno);
627 child = usb_bus_port_get_device(udev->bus,
628 udev->hub->ports + portno - 1);
632 /* first clear the port connection change bit */
634 err = usbd_req_clear_port_feature(udev, NULL,
635 portno, UHF_C_PORT_CONNECTION);
640 /* check if there is a child */
644 * Free USB device and all subdevices, if any.
646 usb_free_device(child, 0);
649 /* get fresh status */
651 err = uhub_read_port_status(sc, portno);
655 #if USB_HAVE_DISABLE_ENUM
656 /* check if we should skip enumeration from this USB HUB */
657 if (usb_disable_enumeration != 0 ||
658 sc->sc_disable_enumeration != 0) {
659 DPRINTF("Enumeration is disabled!\n");
663 /* check if nothing is connected to the port */
665 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
668 /* check if there is no power on the port and print a warning */
670 switch (udev->speed) {
674 power_mask = UPS_PORT_POWER;
676 case USB_SPEED_SUPER:
677 if (udev->parent_hub == NULL)
678 power_mask = UPS_PORT_POWER;
680 power_mask = UPS_PORT_POWER_SS;
686 if (!(sc->sc_st.port_status & power_mask)) {
687 DPRINTF("WARNING: strange, connected port %d "
688 "has no power\n", portno);
691 /* check if the device is in Host Mode */
693 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
695 DPRINTF("Port %d is in Host Mode\n", portno);
697 if (sc->sc_st.port_status & UPS_SUSPEND) {
699 * NOTE: Should not get here in SuperSpeed
700 * mode, because the HUB should report this
703 DPRINTF("Port %d was still "
704 "suspended, clearing.\n", portno);
705 err = usbd_req_clear_port_feature(udev,
706 NULL, portno, UHF_PORT_SUSPEND);
711 /* wait for maximum device power up time */
714 USB_MS_TO_TICKS(usb_port_powerup_delay));
716 /* reset port, which implies enabling it */
718 err = usbd_req_reset_port(udev, NULL, portno);
721 DPRINTFN(0, "port %d reset "
722 "failed, error=%s\n",
723 portno, usbd_errstr(err));
726 /* get port status again, it might have changed during reset */
728 err = uhub_read_port_status(sc, portno);
732 /* check if something changed during port reset */
734 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
735 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
737 DPRINTFN(0, "giving up port reset "
738 "- device vanished\n");
745 DPRINTF("Port %d is in Device Mode\n", portno);
749 * Figure out the device speed
751 switch (udev->speed) {
753 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
754 speed = USB_SPEED_HIGH;
755 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
756 speed = USB_SPEED_LOW;
758 speed = USB_SPEED_FULL;
761 if (sc->sc_st.port_status & UPS_LOW_SPEED)
762 speed = USB_SPEED_LOW;
764 speed = USB_SPEED_FULL;
767 speed = USB_SPEED_LOW;
769 case USB_SPEED_SUPER:
770 if (udev->parent_hub == NULL) {
771 /* Root HUB - special case */
772 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
774 speed = USB_SPEED_FULL;
777 speed = USB_SPEED_LOW;
780 speed = USB_SPEED_HIGH;
783 speed = USB_SPEED_SUPER;
787 speed = USB_SPEED_SUPER;
791 /* same speed like parent */
795 if (speed == USB_SPEED_SUPER) {
796 err = usbd_req_set_hub_u1_timeout(udev, NULL,
797 portno, 128 - (2 * udev->depth));
799 DPRINTFN(0, "port %d U1 timeout "
800 "failed, error=%s\n",
801 portno, usbd_errstr(err));
803 err = usbd_req_set_hub_u2_timeout(udev, NULL,
804 portno, 128 - (2 * udev->depth));
806 DPRINTFN(0, "port %d U2 timeout "
807 "failed, error=%s\n",
808 portno, usbd_errstr(err));
813 * Figure out the device mode
815 * NOTE: This part is currently FreeBSD specific.
817 if (udev->parent_hub != NULL) {
818 /* inherit mode from the parent HUB */
819 mode = udev->parent_hub->flags.usb_mode;
820 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
821 mode = USB_MODE_DEVICE;
823 mode = USB_MODE_HOST;
825 /* need to create a new child */
826 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
827 udev->depth + 1, portno - 1, portno, speed, mode);
829 DPRINTFN(0, "could not allocate new device\n");
832 return (0); /* success */
837 * Free USB device and all subdevices, if any.
839 usb_free_device(child, 0);
843 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
844 err = usbd_req_clear_port_feature(
846 portno, UHF_PORT_ENABLE);
850 DPRINTFN(0, "device problem (%s), "
851 "disabling port %d\n", usbd_errstr(err), portno);
856 /*------------------------------------------------------------------------*
857 * usb_device_20_compatible
860 * 0: HUB does not support suspend and resume
861 * Else: HUB supports suspend and resume
862 *------------------------------------------------------------------------*/
864 usb_device_20_compatible(struct usb_device *udev)
868 switch (udev->speed) {
878 /*------------------------------------------------------------------------*
879 * uhub_suspend_resume_port
883 * Else: A control transaction failed
884 *------------------------------------------------------------------------*/
886 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
888 struct usb_device *child;
889 struct usb_device *udev;
893 DPRINTF("port %d\n", portno);
896 child = usb_bus_port_get_device(udev->bus,
897 udev->hub->ports + portno - 1);
899 /* first clear the port suspend change bit */
901 if (usb_device_20_compatible(udev)) {
902 err = usbd_req_clear_port_feature(udev, NULL,
903 portno, UHF_C_PORT_SUSPEND);
905 err = usbd_req_clear_port_feature(udev, NULL,
906 portno, UHF_C_PORT_LINK_STATE);
910 DPRINTF("clearing suspend failed.\n");
913 /* get fresh status */
915 err = uhub_read_port_status(sc, portno);
917 DPRINTF("reading port status failed.\n");
920 /* convert current state */
922 if (usb_device_20_compatible(udev)) {
923 if (sc->sc_st.port_status & UPS_SUSPEND) {
929 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
933 case UPS_PORT_LS_SS_INA:
934 usbd_req_warm_reset_port(udev, NULL, portno);
943 DPRINTF("suspended=%u\n", is_suspend);
945 /* do the suspend or resume */
949 * This code handle two cases: 1) Host Mode - we can only
950 * receive resume here 2) Device Mode - we can receive
951 * suspend and resume here
954 usb_dev_resume_peer(child);
955 else if (child->flags.usb_mode == USB_MODE_DEVICE)
956 usb_dev_suspend_peer(child);
962 /*------------------------------------------------------------------------*
963 * uhub_root_interrupt
965 * This function is called when a Root HUB interrupt has
966 * happened. "ptr" and "len" makes up the Root HUB interrupt
967 * packet. This function is called having the "bus_mtx" locked.
968 *------------------------------------------------------------------------*/
970 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
972 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
974 usb_needs_explore(bus, 0);
978 uhub_is_too_deep(struct usb_device *udev)
980 switch (udev->speed) {
984 if (udev->depth > USB_HUB_MAX_DEPTH)
987 case USB_SPEED_SUPER:
988 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
997 /*------------------------------------------------------------------------*
1003 *------------------------------------------------------------------------*/
1005 uhub_explore(struct usb_device *udev)
1007 struct usb_hub *hub;
1008 struct uhub_softc *sc;
1009 struct usb_port *up;
1018 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1020 /* ignore devices that are too deep */
1021 if (uhub_is_too_deep(udev))
1022 return (USB_ERR_TOO_DEEP);
1024 /* check if device is suspended */
1025 if (udev->flags.self_suspended) {
1026 /* need to wait until the child signals resume */
1027 DPRINTF("Device is suspended!\n");
1032 * Make sure we don't race against user-space applications
1035 do_unlock = usbd_enum_lock(udev);
1037 for (x = 0; x != hub->nports; x++) {
1038 up = hub->ports + x;
1041 err = uhub_read_port_status(sc, portno);
1043 /* most likely the HUB is gone */
1046 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1047 DPRINTF("Overcurrent on port %u.\n", portno);
1048 err = usbd_req_clear_port_feature(
1049 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1051 /* most likely the HUB is gone */
1055 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1057 * Fake a connect status change so that the
1058 * status gets checked initially!
1060 sc->sc_st.port_change |=
1061 UPS_C_CONNECT_STATUS;
1063 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1064 err = usbd_req_clear_port_feature(
1065 udev, NULL, portno, UHF_C_PORT_ENABLE);
1067 /* most likely the HUB is gone */
1070 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1072 * Ignore the port error if the device
1075 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1076 DPRINTFN(0, "illegal enable change, "
1077 "port %d\n", portno);
1080 if (up->restartcnt == USB_RESTART_MAX) {
1081 /* XXX could try another speed ? */
1082 DPRINTFN(0, "port error, giving up "
1083 "port %d\n", portno);
1085 sc->sc_st.port_change |=
1086 UPS_C_CONNECT_STATUS;
1091 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1092 err = uhub_reattach_port(sc, portno);
1094 /* most likely the HUB is gone */
1098 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1099 UPS_C_PORT_LINK_STATE)) {
1100 err = uhub_suspend_resume_port(sc, portno);
1102 /* most likely the HUB is gone */
1106 err = uhub_explore_sub(sc, up);
1108 /* no device(s) present */
1111 /* explore succeeded - reset restart counter */
1116 usbd_enum_unlock(udev);
1118 /* initial status checked */
1119 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1121 /* return success */
1122 return (USB_ERR_NORMAL_COMPLETION);
1126 uhub_probe(device_t dev)
1128 struct usb_attach_arg *uaa = device_get_ivars(dev);
1130 if (uaa->usb_mode != USB_MODE_HOST)
1134 * The subclass for USB HUBs is currently ignored because it
1135 * is 0 for some and 1 for others.
1137 if (uaa->info.bConfigIndex == 0 &&
1138 uaa->info.bDeviceClass == UDCLASS_HUB)
1144 /* NOTE: The information returned by this function can be wrong. */
1146 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1148 struct usb_hub_descriptor hubdesc20;
1149 struct usb_hub_ss_descriptor hubdesc30;
1154 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1155 return (USB_ERR_INVAL);
1160 switch (udev->speed) {
1162 case USB_SPEED_FULL:
1163 case USB_SPEED_HIGH:
1164 /* assuming that there is one port */
1165 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1167 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1168 "error=%s\n", usbd_errstr(err));
1171 nports = hubdesc20.bNbrPorts;
1175 if (udev->speed == USB_SPEED_HIGH)
1176 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1179 case USB_SPEED_SUPER:
1180 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1182 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1183 "error=%s\n", usbd_errstr(err));
1186 nports = hubdesc30.bNbrPorts;
1192 err = USB_ERR_INVAL;
1196 if (pnports != NULL)
1206 uhub_attach(device_t dev)
1208 struct uhub_softc *sc = device_get_softc(dev);
1209 struct usb_attach_arg *uaa = device_get_ivars(dev);
1210 struct usb_device *udev = uaa->device;
1211 struct usb_device *parent_hub = udev->parent_hub;
1212 struct usb_hub *hub;
1213 struct usb_hub_descriptor hubdesc20;
1214 struct usb_hub_ss_descriptor hubdesc30;
1215 #if USB_HAVE_DISABLE_ENUM
1216 struct sysctl_ctx_list *sysctl_ctx;
1217 struct sysctl_oid *sysctl_tree;
1224 uint8_t iface_index;
1230 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1232 device_set_usb_desc(dev);
1234 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1235 "parent->selfpowered=%d\n",
1237 udev->flags.self_powered,
1240 parent_hub->flags.self_powered : 0);
1242 if (uhub_is_too_deep(udev)) {
1243 DPRINTFN(0, "HUB at depth %d, "
1244 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1248 if (!udev->flags.self_powered && parent_hub &&
1249 !parent_hub->flags.self_powered) {
1250 DPRINTFN(0, "Bus powered HUB connected to "
1251 "bus powered HUB. HUB ignored\n");
1255 if (UHUB_IS_MULTI_TT(sc)) {
1256 err = usbd_set_alt_interface_index(udev, 0, 1);
1258 device_printf(dev, "MTT could not be enabled\n");
1261 device_printf(dev, "MTT enabled\n");
1264 /* get HUB descriptor */
1266 DPRINTFN(2, "Getting HUB descriptor\n");
1268 switch (udev->speed) {
1270 case USB_SPEED_FULL:
1271 case USB_SPEED_HIGH:
1272 /* assuming that there is one port */
1273 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1275 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1276 "error=%s\n", usbd_errstr(err));
1279 /* get number of ports */
1280 nports = hubdesc20.bNbrPorts;
1282 /* get power delay */
1283 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1284 usb_extra_power_up_time);
1286 /* get complete HUB descriptor */
1288 /* check number of ports */
1290 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1291 "error=%s\n", usbd_errstr(err));
1294 /* get complete HUB descriptor */
1295 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1298 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1299 "error=%s\n", usbd_errstr(err));
1302 if (hubdesc20.bNbrPorts != nports) {
1303 DPRINTFN(0, "Number of ports changed\n");
1308 case USB_SPEED_SUPER:
1309 if (udev->parent_hub != NULL) {
1310 err = usbd_req_set_hub_depth(udev, NULL,
1313 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1314 "error=%s\n", usbd_errstr(err));
1318 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1320 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1321 "error=%s\n", usbd_errstr(err));
1324 /* get number of ports */
1325 nports = hubdesc30.bNbrPorts;
1327 /* get power delay */
1328 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1329 usb_extra_power_up_time);
1331 /* get complete HUB descriptor */
1333 /* check number of ports */
1334 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1335 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1336 "error=%s\n", usbd_errstr(err));
1339 /* get complete HUB descriptor */
1340 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1343 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1344 "error=%s\n", usbd_errstr(err));
1347 if (hubdesc30.bNbrPorts != nports) {
1348 DPRINTFN(0, "Number of ports changed\n");
1354 DPRINTF("Assuming HUB has only one port\n");
1355 /* default number of ports */
1357 /* default power delay */
1358 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1362 DPRINTFN(0, "portless HUB\n");
1365 if (nports > USB_MAX_PORTS) {
1366 DPRINTF("Port limit exceeded\n");
1369 #if (USB_HAVE_FIXED_PORT == 0)
1370 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1371 M_USBDEV, M_WAITOK | M_ZERO);
1380 /* initialize HUB structure */
1382 hub->explore = &uhub_explore;
1383 hub->nports = nports;
1384 hub->hubudev = udev;
1385 #if USB_HAVE_TT_SUPPORT
1386 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1387 hub->tt_msg[0].udev = udev;
1388 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1389 hub->tt_msg[1].udev = udev;
1391 /* if self powered hub, give ports maximum current */
1392 if (udev->flags.self_powered) {
1393 hub->portpower = USB_MAX_POWER;
1395 hub->portpower = USB_MIN_POWER;
1398 /* set up interrupt pipe */
1400 if (udev->parent_hub == NULL) {
1401 /* root HUB is special */
1405 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1406 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1409 DPRINTFN(0, "cannot setup interrupt transfer, "
1410 "errstr=%s\n", usbd_errstr(err));
1413 /* wait with power off for a while */
1414 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1416 #if USB_HAVE_DISABLE_ENUM
1417 /* Add device sysctls */
1419 sysctl_ctx = device_get_sysctl_ctx(dev);
1420 sysctl_tree = device_get_sysctl_tree(dev);
1422 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1423 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1424 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1425 &sc->sc_disable_enumeration, 0,
1426 "Set to disable enumeration on this USB HUB.");
1428 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1429 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1430 &sc->sc_disable_port_power, 0,
1431 "Set to disable USB port power on this USB HUB.");
1435 * To have the best chance of success we do things in the exact same
1436 * order as Windoze98. This should not be necessary, but some
1437 * devices do not follow the USB specs to the letter.
1439 * These are the events on the bus when a hub is attached:
1440 * Get device and config descriptors (see attach code)
1441 * Get hub descriptor (see above)
1444 * wait for power to become stable
1445 * (all below happens in explore code)
1447 * clear C_PORT_CONNECTION
1450 * if device connected
1454 * clear C_PORT_RESET
1456 * proceed with device attachment
1459 /* XXX should check for none, individual, or ganged power? */
1463 for (x = 0; x != nports; x++) {
1464 /* set up data structures */
1465 struct usb_port *up = hub->ports + x;
1467 up->device_index = 0;
1471 /* check if port is removable */
1472 switch (udev->speed) {
1474 case USB_SPEED_FULL:
1475 case USB_SPEED_HIGH:
1476 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1479 case USB_SPEED_SUPER:
1480 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1484 DPRINTF("Assuming removable port\n");
1489 #if USB_HAVE_DISABLE_ENUM
1490 /* check if we should disable USB port power or not */
1491 if (usb_disable_port_power != 0 ||
1492 sc->sc_disable_port_power != 0) {
1493 /* turn the power off */
1494 DPRINTFN(2, "Turning port %d power off\n", portno);
1495 err = usbd_req_clear_port_feature(udev, NULL,
1496 portno, UHF_PORT_POWER);
1499 /* turn the power on */
1500 DPRINTFN(2, "Turning port %d power on\n", portno);
1501 err = usbd_req_set_port_feature(udev, NULL,
1502 portno, UHF_PORT_POWER);
1503 #if USB_HAVE_DISABLE_ENUM
1508 DPRINTFN(0, "port %d power on or off failed, %s\n",
1509 portno, usbd_errstr(err));
1511 DPRINTF("turn on port %d power\n",
1514 /* wait for stable power */
1515 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1518 device_printf(dev, "%d port%s with %d "
1519 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1520 removable, udev->flags.self_powered ? "self" : "bus");
1522 /* Start the interrupt endpoint, if any */
1524 USB_MTX_LOCK(&sc->sc_mtx);
1525 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1526 USB_MTX_UNLOCK(&sc->sc_mtx);
1528 /* Enable automatic power save on all USB HUBs */
1530 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1535 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1537 #if (USB_HAVE_FIXED_PORT == 0)
1538 free(udev->hub, M_USBDEV);
1542 mtx_destroy(&sc->sc_mtx);
1548 * Called from process context when the hub is gone.
1549 * Detach all devices on active ports.
1552 uhub_detach(device_t dev)
1554 struct uhub_softc *sc = device_get_softc(dev);
1555 struct usb_hub *hub = sc->sc_udev->hub;
1556 struct usb_bus *bus = sc->sc_udev->bus;
1557 struct usb_device *child;
1560 if (hub == NULL) /* must be partially working */
1563 /* Make sure interrupt transfer is gone. */
1564 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1566 /* Detach all ports */
1567 for (x = 0; x != hub->nports; x++) {
1569 child = usb_bus_port_get_device(bus, hub->ports + x);
1571 if (child == NULL) {
1576 * Free USB device and all subdevices, if any.
1578 usb_free_device(child, 0);
1581 #if USB_HAVE_TT_SUPPORT
1582 /* Make sure our TT messages are not queued anywhere */
1584 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1585 &hub->tt_msg[0], &hub->tt_msg[1]);
1586 USB_BUS_UNLOCK(bus);
1589 #if (USB_HAVE_FIXED_PORT == 0)
1590 free(hub, M_USBDEV);
1592 sc->sc_udev->hub = NULL;
1594 mtx_destroy(&sc->sc_mtx);
1600 uhub_suspend(device_t dev)
1603 /* Sub-devices are not suspended here! */
1608 uhub_resume(device_t dev)
1611 /* Sub-devices are not resumed here! */
1616 uhub_driver_added(device_t dev, driver_t *driver)
1618 usb_needs_explore_all();
1622 struct usb_device *udev;
1624 uint8_t iface_index;
1628 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1629 struct hub_result *res)
1631 struct usb_interface *iface;
1632 struct usb_device *udev;
1637 nports = hub->nports;
1638 for (x = 0; x != nports; x++) {
1639 udev = usb_bus_port_get_device(hub->hubudev->bus,
1644 for (i = 0; i != USB_IFACE_MAX; i++) {
1645 iface = usbd_get_iface(udev, i);
1647 (iface->subdev == child)) {
1648 res->iface_index = i;
1650 res->portno = x + 1;
1655 res->iface_index = 0;
1661 uhub_child_location_string(device_t parent, device_t child,
1662 char *buf, size_t buflen)
1664 struct uhub_softc *sc;
1665 struct usb_hub *hub;
1666 struct hub_result res;
1668 if (!device_is_attached(parent)) {
1674 sc = device_get_softc(parent);
1675 hub = sc->sc_udev->hub;
1678 uhub_find_iface_index(hub, child, &res);
1680 DPRINTF("device not on hub\n");
1686 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1691 , device_get_unit(res.udev->bus->bdev)
1692 , (res.udev->parent_hub != NULL) ?
1693 res.udev->parent_hub->device_index : 0
1694 , res.portno, res.udev->device_index, res.iface_index
1696 , res.udev->ugen_name
1706 uhub_child_pnpinfo_string(device_t parent, device_t child,
1707 char *buf, size_t buflen)
1709 struct uhub_softc *sc;
1710 struct usb_hub *hub;
1711 struct usb_interface *iface;
1712 struct hub_result res;
1714 if (!device_is_attached(parent)) {
1720 sc = device_get_softc(parent);
1721 hub = sc->sc_udev->hub;
1724 uhub_find_iface_index(hub, child, &res);
1726 DPRINTF("device not on hub\n");
1732 iface = usbd_get_iface(res.udev, res.iface_index);
1733 if (iface && iface->idesc) {
1734 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1735 "devclass=0x%02x devsubclass=0x%02x "
1740 "intclass=0x%02x intsubclass=0x%02x "
1741 "intprotocol=0x%02x" "%s%s",
1742 UGETW(res.udev->ddesc.idVendor),
1743 UGETW(res.udev->ddesc.idProduct),
1744 res.udev->ddesc.bDeviceClass,
1745 res.udev->ddesc.bDeviceSubClass,
1746 res.udev->ddesc.bDeviceProtocol,
1747 usb_get_serial(res.udev),
1748 UGETW(res.udev->ddesc.bcdDevice),
1749 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1750 iface->idesc->bInterfaceClass,
1751 iface->idesc->bInterfaceSubClass,
1752 iface->idesc->bInterfaceProtocol,
1753 iface->pnpinfo ? " " : "",
1754 iface->pnpinfo ? iface->pnpinfo : "");
1768 * The USB Transaction Translator:
1769 * ===============================
1771 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1772 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1773 * USB transfers. To utilize bandwidth dynamically the "scatter and
1774 * gather" principle must be applied. This means that bandwidth must
1775 * be divided into equal parts of bandwidth. With regard to USB all
1776 * data is transferred in smaller packets with length
1777 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1780 * The bandwidth scheduler which I have implemented will simply pack
1781 * the USB transfers back to back until there is no more space in the
1782 * schedule. Out of the 8 microframes which the USB 2.0 standard
1783 * provides, only 6 are available for non-HIGH-speed devices. I have
1784 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1785 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1786 * this division, it is very difficult to allocate and free bandwidth
1789 * NOTE about the Transaction Translator in USB HUBs:
1791 * USB HUBs have a very simple Transaction Translator, that will
1792 * simply pipeline all the SPLIT transactions. That means that the
1793 * transactions will be executed in the order they are queued!
1797 /*------------------------------------------------------------------------*
1798 * usb_intr_find_best_slot
1801 * The best Transaction Translation slot for an interrupt endpoint.
1802 *------------------------------------------------------------------------*/
1804 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1805 uint8_t end, uint8_t mask)
1807 usb_size_t min = (usb_size_t)-1;
1815 /* find the last slot with lesser used bandwidth */
1817 for (x = start; x < end; x++) {
1821 /* compute sum of bandwidth */
1822 for (z = x; z < end; z++) {
1823 if (mask & (1U << (z - x)))
1827 /* check if the current multi-slot is more optimal */
1833 /* check if the mask is about to be shifted out */
1834 if (mask & (1U << (end - 1 - x)))
1840 /*------------------------------------------------------------------------*
1841 * usb_hs_bandwidth_adjust
1843 * This function will update the bandwidth usage for the microframe
1844 * having index "slot" by "len" bytes. "len" can be negative. If the
1845 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1846 * the "slot" argument will be replaced by the slot having least used
1847 * bandwidth. The "mask" argument is used for multi-slot allocations.
1850 * The slot in which the bandwidth update was done: 0..7
1851 *------------------------------------------------------------------------*/
1853 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1854 uint8_t slot, uint8_t mask)
1856 struct usb_bus *bus = udev->bus;
1857 struct usb_hub *hub;
1858 enum usb_dev_speed speed;
1861 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1863 speed = usbd_get_speed(udev);
1867 case USB_SPEED_FULL:
1868 if (speed == USB_SPEED_LOW) {
1872 * The Host Controller Driver should have
1873 * performed checks so that the lookup
1874 * below does not result in a NULL pointer
1878 hub = udev->parent_hs_hub->hub;
1879 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1880 slot = usb_intr_find_best_slot(hub->uframe_usage,
1881 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1883 for (x = slot; x < 8; x++) {
1884 if (mask & (1U << (x - slot))) {
1885 hub->uframe_usage[x] += len;
1886 bus->uframe_usage[x] += len;
1891 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1892 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1893 USB_HS_MICRO_FRAMES_MAX, mask);
1895 for (x = slot; x < 8; x++) {
1896 if (mask & (1U << (x - slot))) {
1897 bus->uframe_usage[x] += len;
1905 /*------------------------------------------------------------------------*
1906 * usb_hs_bandwidth_alloc
1908 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1909 *------------------------------------------------------------------------*/
1911 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1913 struct usb_device *udev;
1918 udev = xfer->xroot->udev;
1920 if (udev->flags.usb_mode != USB_MODE_HOST)
1921 return; /* not supported */
1923 xfer->endpoint->refcount_bw++;
1924 if (xfer->endpoint->refcount_bw != 1)
1925 return; /* already allocated */
1927 speed = usbd_get_speed(udev);
1929 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1931 /* allocate a microframe slot */
1934 slot = usb_hs_bandwidth_adjust(udev,
1935 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1937 xfer->endpoint->usb_uframe = slot;
1938 xfer->endpoint->usb_smask = mask << slot;
1940 if ((speed != USB_SPEED_FULL) &&
1941 (speed != USB_SPEED_LOW)) {
1942 xfer->endpoint->usb_cmask = 0x00 ;
1944 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1948 case UE_ISOCHRONOUS:
1949 switch (usbd_xfer_get_fps_shift(xfer)) {
1964 /* allocate a microframe multi-slot */
1966 slot = usb_hs_bandwidth_adjust(udev,
1967 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1969 xfer->endpoint->usb_uframe = slot;
1970 xfer->endpoint->usb_cmask = 0;
1971 xfer->endpoint->usb_smask = mask << slot;
1975 xfer->endpoint->usb_uframe = 0;
1976 xfer->endpoint->usb_cmask = 0;
1977 xfer->endpoint->usb_smask = 0;
1981 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1982 xfer->endpoint->usb_uframe,
1983 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1986 /*------------------------------------------------------------------------*
1987 * usb_hs_bandwidth_free
1989 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1990 *------------------------------------------------------------------------*/
1992 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1994 struct usb_device *udev;
1998 udev = xfer->xroot->udev;
2000 if (udev->flags.usb_mode != USB_MODE_HOST)
2001 return; /* not supported */
2003 xfer->endpoint->refcount_bw--;
2004 if (xfer->endpoint->refcount_bw != 0)
2005 return; /* still allocated */
2007 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2009 case UE_ISOCHRONOUS:
2011 slot = xfer->endpoint->usb_uframe;
2012 mask = xfer->endpoint->usb_smask;
2014 /* free microframe slot(s): */
2015 usb_hs_bandwidth_adjust(udev,
2016 -xfer->max_frame_size, slot, mask >> slot);
2018 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2019 slot, mask >> slot);
2021 xfer->endpoint->usb_uframe = 0;
2022 xfer->endpoint->usb_cmask = 0;
2023 xfer->endpoint->usb_smask = 0;
2031 /*------------------------------------------------------------------------*
2032 * usb_isoc_time_expand
2034 * This function will expand the time counter from 7-bit to 16-bit.
2037 * 16-bit isochronous time counter.
2038 *------------------------------------------------------------------------*/
2040 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2044 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2046 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2048 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2050 if (isoc_time_curr < rem) {
2051 /* the time counter wrapped around */
2052 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2054 /* update the remainder */
2056 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2057 bus->isoc_time_last |= isoc_time_curr;
2059 return (bus->isoc_time_last);
2062 /*------------------------------------------------------------------------*
2063 * usbd_fs_isoc_schedule_alloc_slot
2065 * This function will allocate bandwidth for an isochronous FULL speed
2066 * transaction in the FULL speed schedule.
2071 *------------------------------------------------------------------------*/
2072 #if USB_HAVE_TT_SUPPORT
2074 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2076 struct usb_xfer *xfer;
2077 struct usb_xfer *pipe_xfer;
2078 struct usb_bus *bus;
2080 usb_frlength_t data_len;
2088 bus = isoc_xfer->xroot->bus;
2090 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2092 /* skip self, if any */
2094 if (xfer == isoc_xfer)
2097 /* check if this USB transfer is going through the same TT */
2099 if (xfer->xroot->udev->parent_hs_hub !=
2100 isoc_xfer->xroot->udev->parent_hs_hub) {
2103 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2104 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2105 (xfer->xroot->udev->hs_port_no !=
2106 isoc_xfer->xroot->udev->hs_port_no)) {
2109 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2112 /* check if isoc_time is part of this transfer */
2114 delta = xfer->isoc_time_complete - isoc_time;
2115 if (delta > 0 && delta <= xfer->nframes) {
2116 delta = xfer->nframes - delta;
2118 len = xfer->frlengths[delta];
2127 * Check double buffered transfers. Only stream ID
2128 * equal to zero is valid here!
2130 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2133 /* skip self, if any */
2135 if (pipe_xfer == isoc_xfer)
2138 /* check if isoc_time is part of this transfer */
2140 delta = pipe_xfer->isoc_time_complete - isoc_time;
2141 if (delta > 0 && delta <= pipe_xfer->nframes) {
2142 delta = pipe_xfer->nframes - delta;
2144 len = pipe_xfer->frlengths[delta];
2154 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2155 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2159 /* check for overflow */
2161 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2166 delta = isoc_xfer->isoc_time_complete - isoc_time;
2167 if (delta > 0 && delta <= isoc_xfer->nframes) {
2168 delta = isoc_xfer->nframes - delta;
2170 len = isoc_xfer->frlengths[delta];
2178 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2179 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2183 /* check for overflow */
2185 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2192 /*------------------------------------------------------------------------*
2193 * usb_bus_port_get_device
2195 * This function is NULL safe.
2196 *------------------------------------------------------------------------*/
2198 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2200 if ((bus == NULL) || (up == NULL)) {
2204 if (up->device_index == 0) {
2208 return (bus->devices[up->device_index]);
2211 /*------------------------------------------------------------------------*
2212 * usb_bus_port_set_device
2214 * This function is NULL safe.
2215 *------------------------------------------------------------------------*/
2217 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2218 struct usb_device *udev, uint8_t device_index)
2225 * There is only one case where we don't
2226 * have an USB port, and that is the Root Hub!
2230 up->device_index = device_index;
2232 device_index = up->device_index;
2233 up->device_index = 0;
2237 * Make relationships to our new device
2239 if (device_index != 0) {
2241 mtx_lock(&usb_ref_lock);
2243 bus->devices[device_index] = udev;
2245 mtx_unlock(&usb_ref_lock);
2251 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2254 /*------------------------------------------------------------------------*
2257 * This functions is called when the USB event thread needs to run.
2258 *------------------------------------------------------------------------*/
2260 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2272 DPRINTF("No bus pointer!\n");
2275 if ((bus->devices == NULL) ||
2276 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2277 DPRINTF("No root HUB\n");
2280 if (mtx_owned(&bus->bus_mtx)) {
2289 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2290 &bus->explore_msg[0], &bus->explore_msg[1])) {
2294 USB_BUS_UNLOCK(bus);
2298 /*------------------------------------------------------------------------*
2299 * usb_needs_explore_all
2301 * This function is called whenever a new driver is loaded and will
2302 * cause that all USB buses are re-explored.
2303 *------------------------------------------------------------------------*/
2305 usb_needs_explore_all(void)
2307 struct usb_bus *bus;
2314 dc = usb_devclass_ptr;
2316 DPRINTFN(0, "no devclass\n");
2320 * Explore all USB buses in parallel.
2322 max = devclass_get_maxunit(dc);
2324 dev = devclass_get_device(dc, max);
2326 bus = device_get_softc(dev);
2328 usb_needs_explore(bus, 1);
2335 /*------------------------------------------------------------------------*
2336 * usb_needs_explore_init
2338 * This function will ensure that the USB controllers are not enumerated
2339 * until the "cold" variable is cleared.
2340 *------------------------------------------------------------------------*/
2342 usb_needs_explore_init(void *arg)
2345 * The cold variable should be cleared prior to this function
2349 usb_needs_explore_all();
2351 DPRINTFN(-1, "Cold variable is still set!\n");
2353 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2355 /*------------------------------------------------------------------------*
2356 * usb_bus_power_update
2358 * This function will ensure that all USB devices on the given bus are
2359 * properly suspended or resumed according to the device transfer
2361 *------------------------------------------------------------------------*/
2364 usb_bus_power_update(struct usb_bus *bus)
2366 usb_needs_explore(bus, 0 /* no probe */ );
2370 /*------------------------------------------------------------------------*
2371 * usbd_transfer_power_ref
2373 * This function will modify the power save reference counts and
2374 * wakeup the USB device associated with the given USB transfer, if
2376 *------------------------------------------------------------------------*/
2379 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2381 static const usb_power_mask_t power_mask[4] = {
2382 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2383 [UE_BULK] = USB_HW_POWER_BULK,
2384 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2385 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2387 struct usb_device *udev;
2388 uint8_t needs_explore;
2389 uint8_t needs_hw_power;
2392 udev = xfer->xroot->udev;
2394 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2395 /* no power save for root HUB */
2398 USB_BUS_LOCK(udev->bus);
2400 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2402 udev->pwr_save.last_xfer_time = ticks;
2403 udev->pwr_save.type_refs[xfer_type] += val;
2405 if (xfer->flags_int.control_xfr) {
2406 udev->pwr_save.read_refs += val;
2407 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2409 * It is not allowed to suspend during a
2412 udev->pwr_save.write_refs += val;
2414 } else if (USB_GET_DATA_ISREAD(xfer)) {
2415 udev->pwr_save.read_refs += val;
2417 udev->pwr_save.write_refs += val;
2421 if (udev->flags.self_suspended)
2422 needs_explore = usb_peer_should_wakeup(udev);
2426 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2427 DPRINTF("Adding type %u to power state\n", xfer_type);
2428 udev->bus->hw_power_state |= power_mask[xfer_type];
2438 USB_BUS_UNLOCK(udev->bus);
2440 if (needs_explore) {
2441 DPRINTF("update\n");
2442 usb_bus_power_update(udev->bus);
2443 } else if (needs_hw_power) {
2444 DPRINTF("needs power\n");
2445 if (udev->bus->methods->set_hw_power != NULL) {
2446 (udev->bus->methods->set_hw_power) (udev->bus);
2452 /*------------------------------------------------------------------------*
2453 * usb_peer_should_wakeup
2455 * This function returns non-zero if the current device should wake up.
2456 *------------------------------------------------------------------------*/
2458 usb_peer_should_wakeup(struct usb_device *udev)
2460 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2461 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2462 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2463 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2464 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2465 (udev->pwr_save.write_refs != 0) ||
2466 ((udev->pwr_save.read_refs != 0) &&
2467 (udev->flags.usb_mode == USB_MODE_HOST) &&
2468 (usb_peer_can_wakeup(udev) == 0)));
2471 /*------------------------------------------------------------------------*
2474 * This function implements the USB power daemon and is called
2475 * regularly from the USB explore thread.
2476 *------------------------------------------------------------------------*/
2479 usb_bus_powerd(struct usb_bus *bus)
2481 struct usb_device *udev;
2484 usb_ticks_t mintime;
2485 usb_size_t type_refs[5];
2488 limit = usb_power_timeout;
2491 else if (limit > 255)
2496 DPRINTF("bus=%p\n", bus);
2501 * The root HUB device is never suspended
2502 * and we simply skip it.
2504 for (x = USB_ROOT_HUB_ADDR + 1;
2505 x != bus->devices_max; x++) {
2507 udev = bus->devices[x];
2511 temp = ticks - udev->pwr_save.last_xfer_time;
2513 if (usb_peer_should_wakeup(udev)) {
2514 /* check if we are suspended */
2515 if (udev->flags.self_suspended != 0) {
2516 USB_BUS_UNLOCK(bus);
2517 usb_dev_resume_peer(udev);
2520 } else if ((temp >= limit) &&
2521 (udev->flags.usb_mode == USB_MODE_HOST) &&
2522 (udev->flags.self_suspended == 0)) {
2523 /* try to do suspend */
2525 USB_BUS_UNLOCK(bus);
2526 usb_dev_suspend_peer(udev);
2531 /* reset counters */
2533 mintime = (usb_ticks_t)-1;
2540 /* Re-loop all the devices to get the actual state */
2542 for (x = USB_ROOT_HUB_ADDR + 1;
2543 x != bus->devices_max; x++) {
2545 udev = bus->devices[x];
2549 /* we found a non-Root-Hub USB device */
2552 /* "last_xfer_time" can be updated by a resume */
2553 temp = ticks - udev->pwr_save.last_xfer_time;
2556 * Compute minimum time since last transfer for the complete
2562 if (udev->flags.self_suspended == 0) {
2563 type_refs[0] += udev->pwr_save.type_refs[0];
2564 type_refs[1] += udev->pwr_save.type_refs[1];
2565 type_refs[2] += udev->pwr_save.type_refs[2];
2566 type_refs[3] += udev->pwr_save.type_refs[3];
2570 if (mintime >= (usb_ticks_t)(1 * hz)) {
2571 /* recompute power masks */
2572 DPRINTF("Recomputing power masks\n");
2573 bus->hw_power_state = 0;
2574 if (type_refs[UE_CONTROL] != 0)
2575 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2576 if (type_refs[UE_BULK] != 0)
2577 bus->hw_power_state |= USB_HW_POWER_BULK;
2578 if (type_refs[UE_INTERRUPT] != 0)
2579 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2580 if (type_refs[UE_ISOCHRONOUS] != 0)
2581 bus->hw_power_state |= USB_HW_POWER_ISOC;
2582 if (type_refs[4] != 0)
2583 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2585 USB_BUS_UNLOCK(bus);
2587 if (bus->methods->set_hw_power != NULL) {
2588 /* always update hardware power! */
2589 (bus->methods->set_hw_power) (bus);
2595 /*------------------------------------------------------------------------*
2596 * usb_dev_resume_peer
2598 * This function will resume an USB peer and do the required USB
2599 * signalling to get an USB device out of the suspended state.
2600 *------------------------------------------------------------------------*/
2602 usb_dev_resume_peer(struct usb_device *udev)
2604 struct usb_bus *bus;
2611 /* check if already resumed */
2612 if (udev->flags.self_suspended == 0)
2615 /* we need a parent HUB to do resume */
2616 if (udev->parent_hub == NULL)
2619 DPRINTF("udev=%p\n", udev);
2621 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2622 (udev->flags.remote_wakeup == 0)) {
2624 * If the host did not set the remote wakeup feature, we can
2625 * not wake it up either!
2627 DPRINTF("remote wakeup is not set!\n");
2630 /* get bus pointer */
2633 /* resume parent hub first */
2634 usb_dev_resume_peer(udev->parent_hub);
2636 /* reduce chance of instant resume failure by waiting a little bit */
2637 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2639 if (usb_device_20_compatible(udev)) {
2640 /* resume current port (Valid in Host and Device Mode) */
2641 err = usbd_req_clear_port_feature(udev->parent_hub,
2642 NULL, udev->port_no, UHF_PORT_SUSPEND);
2644 DPRINTFN(0, "Resuming port failed\n");
2648 /* resume current port (Valid in Host and Device Mode) */
2649 err = usbd_req_set_port_link_state(udev->parent_hub,
2650 NULL, udev->port_no, UPS_PORT_LS_U0);
2652 DPRINTFN(0, "Resuming port failed\n");
2657 /* resume settle time */
2658 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2660 if (bus->methods->device_resume != NULL) {
2661 /* resume USB device on the USB controller */
2662 (bus->methods->device_resume) (udev);
2665 /* set that this device is now resumed */
2666 udev->flags.self_suspended = 0;
2668 /* make sure that we don't go into suspend right away */
2669 udev->pwr_save.last_xfer_time = ticks;
2671 /* make sure the needed power masks are on */
2672 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2673 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2674 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2675 bus->hw_power_state |= USB_HW_POWER_BULK;
2676 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2677 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2678 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2679 bus->hw_power_state |= USB_HW_POWER_ISOC;
2681 USB_BUS_UNLOCK(bus);
2683 if (bus->methods->set_hw_power != NULL) {
2684 /* always update hardware power! */
2685 (bus->methods->set_hw_power) (bus);
2690 /* notify all sub-devices about resume */
2691 err = usb_suspend_resume(udev, 0);
2693 usbd_sr_unlock(udev);
2695 /* check if peer has wakeup capability */
2696 if (usb_peer_can_wakeup(udev)) {
2697 /* clear remote wakeup */
2698 err = usbd_req_clear_device_feature(udev,
2699 NULL, UF_DEVICE_REMOTE_WAKEUP);
2701 DPRINTFN(0, "Clearing device "
2702 "remote wakeup failed: %s\n",
2708 /*------------------------------------------------------------------------*
2709 * usb_dev_suspend_peer
2711 * This function will suspend an USB peer and do the required USB
2712 * signalling to get an USB device into the suspended state.
2713 *------------------------------------------------------------------------*/
2715 usb_dev_suspend_peer(struct usb_device *udev)
2717 struct usb_device *child;
2727 /* check if already suspended */
2728 if (udev->flags.self_suspended)
2731 /* we need a parent HUB to do suspend */
2732 if (udev->parent_hub == NULL)
2735 DPRINTF("udev=%p\n", udev);
2737 /* check if the current device is a HUB */
2738 if (udev->hub != NULL) {
2739 nports = udev->hub->nports;
2741 /* check if all devices on the HUB are suspended */
2742 for (x = 0; x != nports; x++) {
2743 child = usb_bus_port_get_device(udev->bus,
2744 udev->hub->ports + x);
2749 if (child->flags.self_suspended)
2752 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2757 if (usb_peer_can_wakeup(udev)) {
2759 * This request needs to be done before we set
2760 * "udev->flags.self_suspended":
2763 /* allow device to do remote wakeup */
2764 err = usbd_req_set_device_feature(udev,
2765 NULL, UF_DEVICE_REMOTE_WAKEUP);
2767 DPRINTFN(0, "Setting device "
2768 "remote wakeup failed\n");
2772 USB_BUS_LOCK(udev->bus);
2774 * Checking for suspend condition and setting suspended bit
2777 err = usb_peer_should_wakeup(udev);
2780 * Set that this device is suspended. This variable
2781 * must be set before calling USB controller suspend
2784 udev->flags.self_suspended = 1;
2786 USB_BUS_UNLOCK(udev->bus);
2789 if (usb_peer_can_wakeup(udev)) {
2790 /* allow device to do remote wakeup */
2791 err = usbd_req_clear_device_feature(udev,
2792 NULL, UF_DEVICE_REMOTE_WAKEUP);
2794 DPRINTFN(0, "Setting device "
2795 "remote wakeup failed\n");
2799 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2800 /* resume parent HUB first */
2801 usb_dev_resume_peer(udev->parent_hub);
2803 /* reduce chance of instant resume failure by waiting a little bit */
2804 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2806 /* resume current port (Valid in Host and Device Mode) */
2807 err = usbd_req_clear_port_feature(udev->parent_hub,
2808 NULL, udev->port_no, UHF_PORT_SUSPEND);
2810 /* resume settle time */
2811 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2813 DPRINTF("Suspend was cancelled!\n");
2819 /* notify all sub-devices about suspend */
2820 err = usb_suspend_resume(udev, 1);
2822 usbd_sr_unlock(udev);
2824 if (udev->bus->methods->device_suspend != NULL) {
2827 /* suspend device on the USB controller */
2828 (udev->bus->methods->device_suspend) (udev);
2831 temp = usbd_get_dma_delay(udev);
2833 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2837 if (usb_device_20_compatible(udev)) {
2838 /* suspend current port */
2839 err = usbd_req_set_port_feature(udev->parent_hub,
2840 NULL, udev->port_no, UHF_PORT_SUSPEND);
2842 DPRINTFN(0, "Suspending port failed\n");
2846 /* suspend current port */
2847 err = usbd_req_set_port_link_state(udev->parent_hub,
2848 NULL, udev->port_no, UPS_PORT_LS_U3);
2850 DPRINTFN(0, "Suspending port failed\n");
2855 udev = udev->parent_hub;
2859 /*------------------------------------------------------------------------*
2860 * usbd_set_power_mode
2862 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2864 *------------------------------------------------------------------------*/
2866 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2868 /* filter input argument */
2869 if ((power_mode != USB_POWER_MODE_ON) &&
2870 (power_mode != USB_POWER_MODE_OFF))
2871 power_mode = USB_POWER_MODE_SAVE;
2873 power_mode = usbd_filter_power_mode(udev, power_mode);
2875 udev->power_mode = power_mode; /* update copy of power mode */
2878 usb_bus_power_update(udev->bus);
2880 usb_needs_explore(udev->bus, 0 /* no probe */ );
2884 /*------------------------------------------------------------------------*
2885 * usbd_filter_power_mode
2887 * This function filters the power mode based on hardware requirements.
2888 *------------------------------------------------------------------------*/
2890 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2892 const struct usb_bus_methods *mtod;
2895 mtod = udev->bus->methods;
2898 if (mtod->get_power_mode != NULL)
2899 (mtod->get_power_mode) (udev, &temp);
2901 /* check if we should not filter */
2903 return (power_mode);
2905 /* use fixed power mode given by hardware driver */
2909 /*------------------------------------------------------------------------*
2910 * usbd_start_re_enumerate
2912 * This function starts re-enumeration of the given USB device. This
2913 * function does not need to be called BUS-locked. This function does
2914 * not wait until the re-enumeration is completed.
2915 *------------------------------------------------------------------------*/
2917 usbd_start_re_enumerate(struct usb_device *udev)
2919 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2920 udev->re_enumerate_wait = USB_RE_ENUM_START;
2921 usb_needs_explore(udev->bus, 0);
2925 /*-----------------------------------------------------------------------*
2926 * usbd_start_set_config
2928 * This function starts setting a USB configuration. This function
2929 * does not need to be called BUS-locked. This function does not wait
2930 * until the set USB configuratino is completed.
2931 *------------------------------------------------------------------------*/
2933 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2935 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2936 if (udev->curr_config_index == index) {
2937 /* no change needed */
2940 udev->next_config_index = index;
2941 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2942 usb_needs_explore(udev->bus, 0);
2944 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2945 if (udev->next_config_index == index) {
2946 /* no change needed */
2950 return (USB_ERR_PENDING_REQUESTS);