3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 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 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/linker_set.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usb_ioctl.h>
55 #include <dev/usb/usbdi.h>
57 #define USB_DEBUG_VAR uhub_debug
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/usb_device.h>
62 #include <dev/usb/usb_request.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_hub.h>
65 #include <dev/usb/usb_util.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_dynamic.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
73 #define UHUB_INTR_INTERVAL 250 /* ms */
74 #define UHUB_N_TRANSFER 1
77 static int uhub_debug = 0;
79 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
80 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
85 static int usb_power_timeout = 30; /* seconds */
87 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
88 &usb_power_timeout, 0, "USB power timeout");
91 struct uhub_current_state {
97 struct uhub_current_state sc_st;/* current state */
98 device_t sc_dev; /* base device */
99 struct usb_device *sc_udev; /* USB device */
100 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
102 #define UHUB_FLAG_DID_EXPLORE 0x01
106 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
107 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
108 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
110 /* prototypes for type checking: */
112 static device_probe_t uhub_probe;
113 static device_attach_t uhub_attach;
114 static device_detach_t uhub_detach;
115 static device_suspend_t uhub_suspend;
116 static device_resume_t uhub_resume;
118 static bus_driver_added_t uhub_driver_added;
119 static bus_child_location_str_t uhub_child_location_string;
120 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
122 static usb_callback_t uhub_intr_callback;
124 static void usb_dev_resume_peer(struct usb_device *udev);
125 static void usb_dev_suspend_peer(struct usb_device *udev);
127 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
130 .type = UE_INTERRUPT,
131 .endpoint = UE_ADDR_ANY,
132 .direction = UE_DIR_ANY,
134 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
135 .bufsize = 0, /* use wMaxPacketSize */
136 .callback = &uhub_intr_callback,
137 .interval = UHUB_INTR_INTERVAL,
142 * driver instance for "hub" connected to "usb"
143 * and "hub" connected to "hub"
145 static devclass_t uhub_devclass;
147 static device_method_t uhub_methods[] = {
148 DEVMETHOD(device_probe, uhub_probe),
149 DEVMETHOD(device_attach, uhub_attach),
150 DEVMETHOD(device_detach, uhub_detach),
152 DEVMETHOD(device_suspend, uhub_suspend),
153 DEVMETHOD(device_resume, uhub_resume),
155 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
156 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
157 DEVMETHOD(bus_driver_added, uhub_driver_added),
161 static driver_t uhub_driver = {
163 .methods = uhub_methods,
164 .size = sizeof(struct uhub_softc)
167 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
168 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
171 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
173 struct uhub_softc *sc = usbd_xfer_softc(xfer);
175 switch (USB_GET_STATE(xfer)) {
176 case USB_ST_TRANSFERRED:
179 * This is an indication that some port
180 * has changed status. Notify the bus
181 * event handler thread that we need
182 * to be explored again:
184 usb_needs_explore(sc->sc_udev->bus, 0);
187 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
188 usbd_transfer_submit(xfer);
192 if (xfer->error != USB_ERR_CANCELLED) {
194 * Do a clear-stall. The "stall_pipe" flag
195 * will get cleared before next callback by
198 usbd_xfer_set_stall(xfer);
199 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
200 usbd_transfer_submit(xfer);
206 /*------------------------------------------------------------------------*
207 * uhub_explore_sub - subroutine
211 * Else: A control transaction failed
212 *------------------------------------------------------------------------*/
214 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
217 struct usb_device *child;
221 bus = sc->sc_udev->bus;
224 /* get driver added refcount from USB bus */
225 refcount = bus->driver_added_refcount;
227 /* get device assosiated with the given port */
228 child = usb_bus_port_get_device(bus, up);
233 /* check if probe and attach should be done */
235 if (child->driver_added_refcount != refcount) {
236 child->driver_added_refcount = refcount;
237 err = usb_probe_and_attach(child,
238 USB_IFACE_INDEX_ANY);
243 /* start control transfer, if device mode */
245 if (child->flags.usb_mode == USB_MODE_DEVICE) {
246 usbd_default_transfer_setup(child);
248 /* if a HUB becomes present, do a recursive HUB explore */
251 err = (child->hub->explore) (child);
257 /*------------------------------------------------------------------------*
258 * uhub_read_port_status - factored out code
259 *------------------------------------------------------------------------*/
261 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
263 struct usb_port_status ps;
266 err = usbd_req_get_port_status(
267 sc->sc_udev, NULL, &ps, portno);
269 /* update status regardless of error */
271 sc->sc_st.port_status = UGETW(ps.wPortStatus);
272 sc->sc_st.port_change = UGETW(ps.wPortChange);
274 /* debugging print */
276 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
277 "wPortChange=0x%04x, err=%s\n",
278 portno, sc->sc_st.port_status,
279 sc->sc_st.port_change, usbd_errstr(err));
283 /*------------------------------------------------------------------------*
288 * Else: A control transaction failed
289 *------------------------------------------------------------------------*/
291 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
293 struct usb_device *child;
294 struct usb_device *udev;
295 enum usb_dev_speed speed;
296 enum usb_hc_mode mode;
300 DPRINTF("reattaching port %d\n", portno);
305 child = usb_bus_port_get_device(udev->bus,
306 udev->hub->ports + portno - 1);
310 /* first clear the port connection change bit */
312 err = usbd_req_clear_port_feature(udev, NULL,
313 portno, UHF_C_PORT_CONNECTION);
318 /* detach any existing devices */
321 usb_free_device(child,
322 USB_UNCFG_FLAG_FREE_SUBDEV |
323 USB_UNCFG_FLAG_FREE_EP0);
326 /* get fresh status */
328 err = uhub_read_port_status(sc, portno);
332 /* check if nothing is connected to the port */
334 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
337 /* check if there is no power on the port and print a warning */
339 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
340 DPRINTF("WARNING: strange, connected port %d "
341 "has no power\n", portno);
343 /* check if the device is in Host Mode */
345 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
347 DPRINTF("Port %d is in Host Mode\n", portno);
349 if (sc->sc_st.port_status & UPS_SUSPEND) {
350 DPRINTF("Port %d was still "
351 "suspended, clearing.\n", portno);
352 err = usbd_req_clear_port_feature(sc->sc_udev,
353 NULL, portno, UHF_PORT_SUSPEND);
357 /* wait for maximum device power up time */
360 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
362 /* reset port, which implies enabling it */
364 err = usbd_req_reset_port(udev, NULL, portno);
367 DPRINTFN(0, "port %d reset "
368 "failed, error=%s\n",
369 portno, usbd_errstr(err));
372 /* get port status again, it might have changed during reset */
374 err = uhub_read_port_status(sc, portno);
378 /* check if something changed during port reset */
380 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
381 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
383 DPRINTFN(0, "giving up port reset "
384 "- device vanished!\n");
391 DPRINTF("Port %d is in Device Mode\n", portno);
395 * Figure out the device speed
397 switch (udev->speed) {
399 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
400 speed = USB_SPEED_HIGH;
401 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
402 speed = USB_SPEED_LOW;
404 speed = USB_SPEED_FULL;
407 if (sc->sc_st.port_status & UPS_LOW_SPEED)
408 speed = USB_SPEED_LOW;
410 speed = USB_SPEED_FULL;
413 speed = USB_SPEED_LOW;
416 /* same speed like parent */
421 * Figure out the device mode
423 * NOTE: This part is currently FreeBSD specific.
425 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
426 mode = USB_MODE_DEVICE;
428 mode = USB_MODE_HOST;
430 /* need to create a new child */
432 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
433 udev->depth + 1, portno - 1, portno, speed, mode);
435 DPRINTFN(0, "could not allocate new device!\n");
438 return (0); /* success */
442 usb_free_device(child,
443 USB_UNCFG_FLAG_FREE_SUBDEV |
444 USB_UNCFG_FLAG_FREE_EP0);
448 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
449 err = usbd_req_clear_port_feature(
451 portno, UHF_PORT_ENABLE);
455 DPRINTFN(0, "device problem (%s), "
456 "disabling port %d\n", usbd_errstr(err), portno);
461 /*------------------------------------------------------------------------*
462 * uhub_suspend_resume_port
466 * Else: A control transaction failed
467 *------------------------------------------------------------------------*/
469 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
471 struct usb_device *child;
472 struct usb_device *udev;
476 DPRINTF("port %d\n", portno);
479 child = usb_bus_port_get_device(udev->bus,
480 udev->hub->ports + portno - 1);
482 /* first clear the port suspend change bit */
484 err = usbd_req_clear_port_feature(udev, NULL,
485 portno, UHF_C_PORT_SUSPEND);
487 DPRINTF("clearing suspend failed.\n");
490 /* get fresh status */
492 err = uhub_read_port_status(sc, portno);
494 DPRINTF("reading port status failed.\n");
497 /* get current state */
499 if (sc->sc_st.port_status & UPS_SUSPEND) {
505 DPRINTF("suspended=%u\n", is_suspend);
507 /* do the suspend or resume */
511 * This code handle two cases: 1) Host Mode - we can only
512 * receive resume here 2) Device Mode - we can receive
513 * suspend and resume here
516 usb_dev_resume_peer(child);
517 else if (child->flags.usb_mode == USB_MODE_DEVICE)
518 usb_dev_suspend_peer(child);
524 /*------------------------------------------------------------------------*
525 * uhub_root_interrupt
527 * This function is called when a Root HUB interrupt has
528 * happened. "ptr" and "len" makes up the Root HUB interrupt
529 * packet. This function is called having the "bus_mtx" locked.
530 *------------------------------------------------------------------------*/
532 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
534 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
536 usb_needs_explore(bus, 0);
539 /*------------------------------------------------------------------------*
545 *------------------------------------------------------------------------*/
547 uhub_explore(struct usb_device *udev)
550 struct uhub_softc *sc;
559 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
561 /* ignore hubs that are too deep */
562 if (udev->depth > USB_HUB_MAX_DEPTH) {
563 return (USB_ERR_TOO_DEEP);
566 if (udev->flags.self_suspended) {
567 /* need to wait until the child signals resume */
568 DPRINTF("Device is suspended!\n");
571 for (x = 0; x != hub->nports; x++) {
575 err = uhub_read_port_status(sc, portno);
577 /* most likely the HUB is gone */
580 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
581 DPRINTF("Overcurrent on port %u.\n", portno);
582 err = usbd_req_clear_port_feature(
583 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
585 /* most likely the HUB is gone */
589 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
591 * Fake a connect status change so that the
592 * status gets checked initially!
594 sc->sc_st.port_change |=
595 UPS_C_CONNECT_STATUS;
597 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
598 err = usbd_req_clear_port_feature(
599 udev, NULL, portno, UHF_C_PORT_ENABLE);
601 /* most likely the HUB is gone */
604 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
606 * Ignore the port error if the device
609 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
610 DPRINTFN(0, "illegal enable change, "
611 "port %d\n", portno);
614 if (up->restartcnt == USB_RESTART_MAX) {
615 /* XXX could try another speed ? */
616 DPRINTFN(0, "port error, giving up "
617 "port %d\n", portno);
619 sc->sc_st.port_change |=
620 UPS_C_CONNECT_STATUS;
625 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
626 err = uhub_reattach_port(sc, portno);
628 /* most likely the HUB is gone */
632 if (sc->sc_st.port_change & UPS_C_SUSPEND) {
633 err = uhub_suspend_resume_port(sc, portno);
635 /* most likely the HUB is gone */
639 err = uhub_explore_sub(sc, up);
641 /* no device(s) present */
644 /* explore succeeded - reset restart counter */
648 /* initial status checked */
649 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
652 return (USB_ERR_NORMAL_COMPLETION);
656 uhub_probe(device_t dev)
658 struct usb_attach_arg *uaa = device_get_ivars(dev);
660 if (uaa->usb_mode != USB_MODE_HOST) {
664 * The subclass for USB HUBs is ignored because it is 0 for
665 * some and 1 for others.
667 if ((uaa->info.bConfigIndex == 0) &&
668 (uaa->info.bDeviceClass == UDCLASS_HUB)) {
675 uhub_attach(device_t dev)
677 struct uhub_softc *sc = device_get_softc(dev);
678 struct usb_attach_arg *uaa = device_get_ivars(dev);
679 struct usb_device *udev = uaa->device;
680 struct usb_device *parent_hub = udev->parent_hub;
682 struct usb_hub_descriptor hubdesc;
694 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
695 device_get_nameunit(dev));
697 device_set_usb_desc(dev);
699 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
700 "parent->selfpowered=%d\n",
702 udev->flags.self_powered,
705 parent_hub->flags.self_powered : 0);
707 if (udev->depth > USB_HUB_MAX_DEPTH) {
708 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored!\n",
712 if (!udev->flags.self_powered && parent_hub &&
713 (!parent_hub->flags.self_powered)) {
714 DPRINTFN(0, "bus powered HUB connected to "
715 "bus powered HUB. HUB ignored!\n");
718 /* get HUB descriptor */
720 DPRINTFN(2, "getting HUB descriptor\n");
722 /* assuming that there is one port */
723 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, 1);
725 nports = hubdesc.bNbrPorts;
727 if (!err && (nports >= 8)) {
728 /* get complete HUB descriptor */
729 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, nports);
732 DPRINTFN(0, "getting hub descriptor failed,"
733 "error=%s\n", usbd_errstr(err));
736 if (hubdesc.bNbrPorts != nports) {
737 DPRINTFN(0, "number of ports changed!\n");
741 DPRINTFN(0, "portless HUB!\n");
744 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
745 M_USBDEV, M_WAITOK | M_ZERO);
752 #if USB_HAVE_TT_SUPPORT
753 /* init FULL-speed ISOCHRONOUS schedule */
754 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
756 /* initialize HUB structure */
758 hub->explore = &uhub_explore;
759 hub->nports = hubdesc.bNbrPorts;
762 /* if self powered hub, give ports maximum current */
763 if (udev->flags.self_powered) {
764 hub->portpower = USB_MAX_POWER;
766 hub->portpower = USB_MIN_POWER;
769 /* set up interrupt pipe */
771 if (udev->parent_hub == NULL) {
772 /* root HUB is special */
776 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
777 uhub_config, UHUB_N_TRANSFER, sc, &Giant);
780 DPRINTFN(0, "cannot setup interrupt transfer, "
781 "errstr=%s!\n", usbd_errstr(err));
784 /* wait with power off for a while */
785 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
788 * To have the best chance of success we do things in the exact same
789 * order as Windoze98. This should not be necessary, but some
790 * devices do not follow the USB specs to the letter.
792 * These are the events on the bus when a hub is attached:
793 * Get device and config descriptors (see attach code)
794 * Get hub descriptor (see above)
797 * wait for power to become stable
798 * (all below happens in explore code)
800 * clear C_PORT_CONNECTION
803 * if device connected
809 * proceed with device attachment
812 /* XXX should check for none, individual, or ganged power? */
815 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
816 USB_EXTRA_POWER_UP_TIME);
818 for (x = 0; x != nports; x++) {
819 /* set up data structures */
820 struct usb_port *up = hub->ports + x;
822 up->device_index = 0;
826 /* check if port is removable */
827 if (!UHD_NOT_REMOV(&hubdesc, portno)) {
831 /* turn the power on */
832 err = usbd_req_set_port_feature(udev, NULL,
833 portno, UHF_PORT_POWER);
836 DPRINTFN(0, "port %d power on failed, %s\n",
837 portno, usbd_errstr(err));
839 DPRINTF("turn on port %d power\n",
842 /* wait for stable power */
843 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
846 device_printf(dev, "%d port%s with %d "
847 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
848 removable, udev->flags.self_powered ? "self" : "bus");
850 /* Start the interrupt endpoint, if any */
852 if (sc->sc_xfer[0] != NULL) {
853 USB_XFER_LOCK(sc->sc_xfer[0]);
854 usbd_transfer_start(sc->sc_xfer[0]);
855 USB_XFER_UNLOCK(sc->sc_xfer[0]);
858 /* Enable automatic power save on all USB HUBs */
860 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
865 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
868 free(udev->hub, M_USBDEV);
875 * Called from process context when the hub is gone.
876 * Detach all devices on active ports.
879 uhub_detach(device_t dev)
881 struct uhub_softc *sc = device_get_softc(dev);
882 struct usb_hub *hub = sc->sc_udev->hub;
883 struct usb_device *child;
886 /* detach all children first */
887 bus_generic_detach(dev);
889 if (hub == NULL) { /* must be partially working */
892 for (x = 0; x != hub->nports; x++) {
894 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
900 * Subdevices are not freed, because the caller of
901 * uhub_detach() will do that.
903 usb_free_device(child,
904 USB_UNCFG_FLAG_FREE_EP0);
907 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
910 sc->sc_udev->hub = NULL;
915 uhub_suspend(device_t dev)
918 /* Sub-devices are not suspended here! */
923 uhub_resume(device_t dev)
926 /* Sub-devices are not resumed here! */
931 uhub_driver_added(device_t dev, driver_t *driver)
933 usb_needs_explore_all();
937 struct usb_device *udev;
943 uhub_find_iface_index(struct usb_hub *hub, device_t child,
944 struct hub_result *res)
946 struct usb_interface *iface;
947 struct usb_device *udev;
952 nports = hub->nports;
953 for (x = 0; x != nports; x++) {
954 udev = usb_bus_port_get_device(hub->hubudev->bus,
959 for (i = 0; i != USB_IFACE_MAX; i++) {
960 iface = usbd_get_iface(udev, i);
962 (iface->subdev == child)) {
963 res->iface_index = i;
970 res->iface_index = 0;
976 uhub_child_location_string(device_t parent, device_t child,
977 char *buf, size_t buflen)
979 struct uhub_softc *sc = device_get_softc(parent);
980 struct usb_hub *hub = sc->sc_udev->hub;
981 struct hub_result res;
984 uhub_find_iface_index(hub, child, &res);
986 DPRINTF("device not on hub\n");
992 snprintf(buf, buflen, "port=%u interface=%u",
993 res.portno, res.iface_index);
1001 uhub_child_pnpinfo_string(device_t parent, device_t child,
1002 char *buf, size_t buflen)
1004 struct uhub_softc *sc = device_get_softc(parent);
1005 struct usb_hub *hub = sc->sc_udev->hub;
1006 struct usb_interface *iface;
1007 struct hub_result res;
1010 uhub_find_iface_index(hub, child, &res);
1012 DPRINTF("device not on hub\n");
1018 iface = usbd_get_iface(res.udev, res.iface_index);
1019 if (iface && iface->idesc) {
1020 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1021 "devclass=0x%02x devsubclass=0x%02x "
1024 "intclass=0x%02x intsubclass=0x%02x",
1025 UGETW(res.udev->ddesc.idVendor),
1026 UGETW(res.udev->ddesc.idProduct),
1027 res.udev->ddesc.bDeviceClass,
1028 res.udev->ddesc.bDeviceSubClass,
1030 UGETW(res.udev->ddesc.bcdDevice),
1031 iface->idesc->bInterfaceClass,
1032 iface->idesc->bInterfaceSubClass);
1046 * The USB Transaction Translator:
1047 * ===============================
1049 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1050 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1051 * USB transfers. To utilize bandwidth dynamically the "scatter and
1052 * gather" principle must be applied. This means that bandwidth must
1053 * be divided into equal parts of bandwidth. With regard to USB all
1054 * data is transferred in smaller packets with length
1055 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1058 * The bandwidth scheduler which I have implemented will simply pack
1059 * the USB transfers back to back until there is no more space in the
1060 * schedule. Out of the 8 microframes which the USB 2.0 standard
1061 * provides, only 6 are available for non-HIGH-speed devices. I have
1062 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1063 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1064 * this division, it is very difficult to allocate and free bandwidth
1067 * NOTE about the Transaction Translator in USB HUBs:
1069 * USB HUBs have a very simple Transaction Translator, that will
1070 * simply pipeline all the SPLIT transactions. That means that the
1071 * transactions will be executed in the order they are queued!
1075 /*------------------------------------------------------------------------*
1076 * usb_intr_find_best_slot
1079 * The best Transaction Translation slot for an interrupt endpoint.
1080 *------------------------------------------------------------------------*/
1082 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start, uint8_t end)
1084 usb_size_t max = 0 - 1;
1090 /* find the last slot with lesser used bandwidth */
1092 for (x = start; x < end; x++) {
1093 if (max >= ptr[x]) {
1101 /*------------------------------------------------------------------------*
1102 * usb_intr_schedule_adjust
1104 * This function will update the bandwith usage for the microframe
1105 * having index "slot" by "len" bytes. "len" can be negative. If the
1106 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1107 * the "slot" argument will be replaced by the slot having least used
1111 * The slot on which the bandwidth update was done.
1112 *------------------------------------------------------------------------*/
1114 usb_intr_schedule_adjust(struct usb_device *udev, int16_t len, uint8_t slot)
1116 struct usb_bus *bus = udev->bus;
1117 struct usb_hub *hub;
1118 enum usb_dev_speed speed;
1120 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1122 speed = usbd_get_speed(udev);
1126 case USB_SPEED_FULL:
1127 if (speed == USB_SPEED_LOW) {
1131 * The Host Controller Driver should have
1132 * performed checks so that the lookup
1133 * below does not result in a NULL pointer
1137 hub = udev->parent_hs_hub->hub;
1138 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1139 slot = usb_intr_find_best_slot(hub->uframe_usage,
1140 USB_FS_ISOC_UFRAME_MAX, 6);
1142 hub->uframe_usage[slot] += len;
1143 bus->uframe_usage[slot] += len;
1146 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1147 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1148 USB_HS_MICRO_FRAMES_MAX);
1150 bus->uframe_usage[slot] += len;
1156 /*------------------------------------------------------------------------*
1157 * usbd_fs_isoc_schedule_init_sub
1159 * This function initialises an USB FULL speed isochronous schedule
1161 *------------------------------------------------------------------------*/
1162 #if USB_HAVE_TT_SUPPORT
1164 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1166 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1167 USB_FS_BYTES_PER_HS_UFRAME);
1168 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1169 fss->frame_slot = 0;
1173 /*------------------------------------------------------------------------*
1174 * usbd_fs_isoc_schedule_init_all
1176 * This function will reset the complete USB FULL speed isochronous
1177 * bandwidth schedule.
1178 *------------------------------------------------------------------------*/
1179 #if USB_HAVE_TT_SUPPORT
1181 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1183 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1185 while (fss != fss_end) {
1186 usbd_fs_isoc_schedule_init_sub(fss);
1192 /*------------------------------------------------------------------------*
1193 * usb_isoc_time_expand
1195 * This function will expand the time counter from 7-bit to 16-bit.
1198 * 16-bit isochronous time counter.
1199 *------------------------------------------------------------------------*/
1201 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1205 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1207 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1209 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1211 if (isoc_time_curr < rem) {
1212 /* the time counter wrapped around */
1213 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1215 /* update the remainder */
1217 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1218 bus->isoc_time_last |= isoc_time_curr;
1220 return (bus->isoc_time_last);
1223 /*------------------------------------------------------------------------*
1224 * usbd_fs_isoc_schedule_isoc_time_expand
1226 * This function does multiple things. First of all it will expand the
1227 * passed isochronous time, which is the return value. Then it will
1228 * store where the current FULL speed isochronous schedule is
1229 * positioned in time and where the end is. See "pp_start" and
1230 * "pp_end" arguments.
1233 * Expanded version of "isoc_time".
1235 * NOTE: This function depends on being called regularly with
1236 * intervals less than "USB_ISOC_TIME_MAX".
1237 *------------------------------------------------------------------------*/
1238 #if USB_HAVE_TT_SUPPORT
1240 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1241 struct usb_fs_isoc_schedule **pp_start,
1242 struct usb_fs_isoc_schedule **pp_end,
1245 struct usb_fs_isoc_schedule *fss_end;
1246 struct usb_fs_isoc_schedule *fss_a;
1247 struct usb_fs_isoc_schedule *fss_b;
1248 struct usb_hub *hs_hub;
1250 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1252 hs_hub = udev->parent_hs_hub->hub;
1254 if (hs_hub != NULL) {
1256 fss_a = hs_hub->fs_isoc_schedule +
1257 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1259 hs_hub->isoc_last_time = isoc_time;
1261 fss_b = hs_hub->fs_isoc_schedule +
1262 (isoc_time % USB_ISOC_TIME_MAX);
1264 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1266 *pp_start = hs_hub->fs_isoc_schedule;
1269 while (fss_a != fss_b) {
1270 if (fss_a == fss_end) {
1271 fss_a = hs_hub->fs_isoc_schedule;
1274 usbd_fs_isoc_schedule_init_sub(fss_a);
1287 /*------------------------------------------------------------------------*
1288 * usbd_fs_isoc_schedule_alloc
1290 * This function will allocate bandwidth for an isochronous FULL speed
1291 * transaction in the FULL speed schedule. The microframe slot where
1292 * the transaction should be started is stored in the byte pointed to
1293 * by "pstart". The "len" argument specifies the length of the
1294 * transaction in bytes.
1299 *------------------------------------------------------------------------*/
1300 #if USB_HAVE_TT_SUPPORT
1302 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1303 uint8_t *pstart, uint16_t len)
1305 uint8_t slot = fss->frame_slot;
1307 /* Compute overhead and bit-stuffing */
1314 if (len > fss->total_bytes) {
1315 *pstart = 0; /* set some dummy value */
1316 return (1); /* error */
1320 fss->total_bytes -= len;
1322 while (len >= fss->frame_bytes) {
1323 len -= fss->frame_bytes;
1324 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1328 fss->frame_bytes -= len;
1331 return (0); /* success */
1335 /*------------------------------------------------------------------------*
1336 * usb_bus_port_get_device
1338 * This function is NULL safe.
1339 *------------------------------------------------------------------------*/
1341 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1343 if ((bus == NULL) || (up == NULL)) {
1347 if (up->device_index == 0) {
1351 return (bus->devices[up->device_index]);
1354 /*------------------------------------------------------------------------*
1355 * usb_bus_port_set_device
1357 * This function is NULL safe.
1358 *------------------------------------------------------------------------*/
1360 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1361 struct usb_device *udev, uint8_t device_index)
1368 * There is only one case where we don't
1369 * have an USB port, and that is the Root Hub!
1373 up->device_index = device_index;
1375 device_index = up->device_index;
1376 up->device_index = 0;
1380 * Make relationships to our new device
1382 if (device_index != 0) {
1384 mtx_lock(&usb_ref_lock);
1386 bus->devices[device_index] = udev;
1388 mtx_unlock(&usb_ref_lock);
1394 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1397 /*------------------------------------------------------------------------*
1400 * This functions is called when the USB event thread needs to run.
1401 *------------------------------------------------------------------------*/
1403 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1410 DPRINTF("No bus pointer!\n");
1413 if ((bus->devices == NULL) ||
1414 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1415 DPRINTF("No root HUB\n");
1418 if (mtx_owned(&bus->bus_mtx)) {
1427 if (usb_proc_msignal(&bus->explore_proc,
1428 &bus->explore_msg[0], &bus->explore_msg[1])) {
1432 USB_BUS_UNLOCK(bus);
1436 /*------------------------------------------------------------------------*
1437 * usb_needs_explore_all
1439 * This function is called whenever a new driver is loaded and will
1440 * cause that all USB busses are re-explored.
1441 *------------------------------------------------------------------------*/
1443 usb_needs_explore_all(void)
1445 struct usb_bus *bus;
1452 dc = usb_devclass_ptr;
1454 DPRINTFN(0, "no devclass\n");
1458 * Explore all USB busses in parallell.
1460 max = devclass_get_maxunit(dc);
1462 dev = devclass_get_device(dc, max);
1464 bus = device_get_softc(dev);
1466 usb_needs_explore(bus, 1);
1473 /*------------------------------------------------------------------------*
1474 * usb_bus_power_update
1476 * This function will ensure that all USB devices on the given bus are
1477 * properly suspended or resumed according to the device transfer
1479 *------------------------------------------------------------------------*/
1482 usb_bus_power_update(struct usb_bus *bus)
1484 usb_needs_explore(bus, 0 /* no probe */ );
1488 /*------------------------------------------------------------------------*
1489 * usbd_transfer_power_ref
1491 * This function will modify the power save reference counts and
1492 * wakeup the USB device associated with the given USB transfer, if
1494 *------------------------------------------------------------------------*/
1497 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1499 static const usb_power_mask_t power_mask[4] = {
1500 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1501 [UE_BULK] = USB_HW_POWER_BULK,
1502 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1503 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1505 struct usb_device *udev;
1506 uint8_t needs_explore;
1507 uint8_t needs_hw_power;
1510 udev = xfer->xroot->udev;
1512 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1513 /* no power save for root HUB */
1516 USB_BUS_LOCK(udev->bus);
1518 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1520 udev->pwr_save.last_xfer_time = ticks;
1521 udev->pwr_save.type_refs[xfer_type] += val;
1523 if (xfer->flags_int.control_xfr) {
1524 udev->pwr_save.read_refs += val;
1525 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1527 * it is not allowed to suspend during a control
1530 udev->pwr_save.write_refs += val;
1532 } else if (USB_GET_DATA_ISREAD(xfer)) {
1533 udev->pwr_save.read_refs += val;
1535 udev->pwr_save.write_refs += val;
1538 if (udev->flags.self_suspended)
1540 (udev->pwr_save.write_refs != 0) ||
1541 ((udev->pwr_save.read_refs != 0) &&
1542 (usb_peer_can_wakeup(udev) == 0));
1546 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1547 DPRINTF("Adding type %u to power state\n", xfer_type);
1548 udev->bus->hw_power_state |= power_mask[xfer_type];
1554 USB_BUS_UNLOCK(udev->bus);
1556 if (needs_explore) {
1557 DPRINTF("update\n");
1558 usb_bus_power_update(udev->bus);
1559 } else if (needs_hw_power) {
1560 DPRINTF("needs power\n");
1561 if (udev->bus->methods->set_hw_power != NULL) {
1562 (udev->bus->methods->set_hw_power) (udev->bus);
1568 /*------------------------------------------------------------------------*
1571 * This function implements the USB power daemon and is called
1572 * regularly from the USB explore thread.
1573 *------------------------------------------------------------------------*/
1576 usb_bus_powerd(struct usb_bus *bus)
1578 struct usb_device *udev;
1581 usb_ticks_t mintime;
1582 usb_size_t type_refs[5];
1586 limit = usb_power_timeout;
1589 else if (limit > 255)
1594 DPRINTF("bus=%p\n", bus);
1599 * The root HUB device is never suspended
1600 * and we simply skip it.
1602 for (x = USB_ROOT_HUB_ADDR + 1;
1603 x != bus->devices_max; x++) {
1605 udev = bus->devices[x];
1609 rem_wakeup = usb_peer_can_wakeup(udev);
1611 temp = ticks - udev->pwr_save.last_xfer_time;
1613 if ((udev->power_mode == USB_POWER_MODE_ON) ||
1614 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1615 (udev->pwr_save.write_refs != 0) ||
1616 ((udev->pwr_save.read_refs != 0) &&
1617 (rem_wakeup == 0))) {
1619 /* check if we are suspended */
1620 if (udev->flags.self_suspended != 0) {
1621 USB_BUS_UNLOCK(bus);
1622 usb_dev_resume_peer(udev);
1625 } else if (temp >= limit) {
1627 /* check if we are not suspended */
1628 if (udev->flags.self_suspended == 0) {
1629 USB_BUS_UNLOCK(bus);
1630 usb_dev_suspend_peer(udev);
1636 /* reset counters */
1645 /* Re-loop all the devices to get the actual state */
1647 for (x = USB_ROOT_HUB_ADDR + 1;
1648 x != bus->devices_max; x++) {
1650 udev = bus->devices[x];
1654 /* we found a non-Root-Hub USB device */
1657 /* "last_xfer_time" can be updated by a resume */
1658 temp = ticks - udev->pwr_save.last_xfer_time;
1661 * Compute minimum time since last transfer for the complete
1667 if (udev->flags.self_suspended == 0) {
1668 type_refs[0] += udev->pwr_save.type_refs[0];
1669 type_refs[1] += udev->pwr_save.type_refs[1];
1670 type_refs[2] += udev->pwr_save.type_refs[2];
1671 type_refs[3] += udev->pwr_save.type_refs[3];
1675 if (mintime >= (1 * hz)) {
1676 /* recompute power masks */
1677 DPRINTF("Recomputing power masks\n");
1678 bus->hw_power_state = 0;
1679 if (type_refs[UE_CONTROL] != 0)
1680 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1681 if (type_refs[UE_BULK] != 0)
1682 bus->hw_power_state |= USB_HW_POWER_BULK;
1683 if (type_refs[UE_INTERRUPT] != 0)
1684 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1685 if (type_refs[UE_ISOCHRONOUS] != 0)
1686 bus->hw_power_state |= USB_HW_POWER_ISOC;
1687 if (type_refs[4] != 0)
1688 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1690 USB_BUS_UNLOCK(bus);
1692 if (bus->methods->set_hw_power != NULL) {
1693 /* always update hardware power! */
1694 (bus->methods->set_hw_power) (bus);
1700 /*------------------------------------------------------------------------*
1701 * usb_dev_resume_peer
1703 * This function will resume an USB peer and do the required USB
1704 * signalling to get an USB device out of the suspended state.
1705 *------------------------------------------------------------------------*/
1707 usb_dev_resume_peer(struct usb_device *udev)
1709 struct usb_bus *bus;
1716 /* check if already resumed */
1717 if (udev->flags.self_suspended == 0)
1720 /* we need a parent HUB to do resume */
1721 if (udev->parent_hub == NULL)
1724 DPRINTF("udev=%p\n", udev);
1726 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1727 (udev->flags.remote_wakeup == 0)) {
1729 * If the host did not set the remote wakeup feature, we can
1730 * not wake it up either!
1732 DPRINTF("remote wakeup is not set!\n");
1735 /* get bus pointer */
1738 /* resume parent hub first */
1739 usb_dev_resume_peer(udev->parent_hub);
1741 /* resume current port (Valid in Host and Device Mode) */
1742 err = usbd_req_clear_port_feature(udev->parent_hub,
1743 NULL, udev->port_no, UHF_PORT_SUSPEND);
1745 DPRINTFN(0, "Resuming port failed!\n");
1748 /* resume settle time */
1749 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1751 if (bus->methods->device_resume != NULL) {
1752 /* resume USB device on the USB controller */
1753 (bus->methods->device_resume) (udev);
1756 /* set that this device is now resumed */
1757 udev->flags.self_suspended = 0;
1759 /* make sure that we don't go into suspend right away */
1760 udev->pwr_save.last_xfer_time = ticks;
1762 /* make sure the needed power masks are on */
1763 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1764 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1765 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1766 bus->hw_power_state |= USB_HW_POWER_BULK;
1767 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1768 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1769 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1770 bus->hw_power_state |= USB_HW_POWER_ISOC;
1772 USB_BUS_UNLOCK(bus);
1774 if (bus->methods->set_hw_power != NULL) {
1775 /* always update hardware power! */
1776 (bus->methods->set_hw_power) (bus);
1778 sx_xlock(udev->default_sx + 1);
1779 /* notify all sub-devices about resume */
1780 err = usb_suspend_resume(udev, 0);
1781 sx_unlock(udev->default_sx + 1);
1783 /* check if peer has wakeup capability */
1784 if (usb_peer_can_wakeup(udev)) {
1785 /* clear remote wakeup */
1786 err = usbd_req_clear_device_feature(udev,
1787 NULL, UF_DEVICE_REMOTE_WAKEUP);
1789 DPRINTFN(0, "Clearing device "
1790 "remote wakeup failed: %s!\n",
1797 /*------------------------------------------------------------------------*
1798 * usb_dev_suspend_peer
1800 * This function will suspend an USB peer and do the required USB
1801 * signalling to get an USB device into the suspended state.
1802 *------------------------------------------------------------------------*/
1804 usb_dev_suspend_peer(struct usb_device *udev)
1806 struct usb_device *child;
1816 /* check if already suspended */
1817 if (udev->flags.self_suspended)
1820 /* we need a parent HUB to do suspend */
1821 if (udev->parent_hub == NULL)
1824 DPRINTF("udev=%p\n", udev);
1826 /* check if the current device is a HUB */
1827 if (udev->hub != NULL) {
1828 nports = udev->hub->nports;
1830 /* check if all devices on the HUB are suspended */
1831 for (x = 0; x != nports; x++) {
1833 child = usb_bus_port_get_device(udev->bus,
1834 udev->hub->ports + x);
1839 if (child->flags.self_suspended)
1842 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
1847 sx_xlock(udev->default_sx + 1);
1848 /* notify all sub-devices about suspend */
1849 err = usb_suspend_resume(udev, 1);
1850 sx_unlock(udev->default_sx + 1);
1852 if (usb_peer_can_wakeup(udev)) {
1853 /* allow device to do remote wakeup */
1854 err = usbd_req_set_device_feature(udev,
1855 NULL, UF_DEVICE_REMOTE_WAKEUP);
1857 DPRINTFN(0, "Setting device "
1858 "remote wakeup failed!\n");
1861 USB_BUS_LOCK(udev->bus);
1863 * Set that this device is suspended. This variable must be set
1864 * before calling USB controller suspend callbacks.
1866 udev->flags.self_suspended = 1;
1867 USB_BUS_UNLOCK(udev->bus);
1869 if (udev->bus->methods->device_suspend != NULL) {
1872 /* suspend device on the USB controller */
1873 (udev->bus->methods->device_suspend) (udev);
1876 temp = usbd_get_dma_delay(udev->bus);
1877 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
1880 /* suspend current port */
1881 err = usbd_req_set_port_feature(udev->parent_hub,
1882 NULL, udev->port_no, UHF_PORT_SUSPEND);
1884 DPRINTFN(0, "Suspending port failed\n");
1888 udev = udev->parent_hub;
1892 /*------------------------------------------------------------------------*
1893 * usbd_set_power_mode
1895 * This function will set the power mode, see USB_POWER_MODE_XXX for a
1897 *------------------------------------------------------------------------*/
1899 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
1901 /* filter input argument */
1902 if ((power_mode != USB_POWER_MODE_ON) &&
1903 (power_mode != USB_POWER_MODE_OFF)) {
1904 power_mode = USB_POWER_MODE_SAVE;
1906 udev->power_mode = power_mode; /* update copy of power mode */
1909 usb_bus_power_update(udev->bus);