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;
238 err = usb_probe_and_attach(child,
239 USB_IFACE_INDEX_ANY);
245 /* start control transfer, if device mode */
247 if (child->flags.usb_mode == USB_MODE_DEVICE) {
248 usbd_default_transfer_setup(child);
250 /* if a HUB becomes present, do a recursive HUB explore */
253 err = (child->hub->explore) (child);
259 /*------------------------------------------------------------------------*
260 * uhub_read_port_status - factored out code
261 *------------------------------------------------------------------------*/
263 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
265 struct usb_port_status ps;
268 err = usbd_req_get_port_status(
269 sc->sc_udev, NULL, &ps, portno);
271 /* update status regardless of error */
273 sc->sc_st.port_status = UGETW(ps.wPortStatus);
274 sc->sc_st.port_change = UGETW(ps.wPortChange);
276 /* debugging print */
278 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
279 "wPortChange=0x%04x, err=%s\n",
280 portno, sc->sc_st.port_status,
281 sc->sc_st.port_change, usbd_errstr(err));
285 /*------------------------------------------------------------------------*
290 * Else: A control transaction failed
291 *------------------------------------------------------------------------*/
293 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
295 struct usb_device *child;
296 struct usb_device *udev;
297 enum usb_dev_speed speed;
298 enum usb_hc_mode mode;
302 DPRINTF("reattaching port %d\n", portno);
307 child = usb_bus_port_get_device(udev->bus,
308 udev->hub->ports + portno - 1);
312 /* first clear the port connection change bit */
314 err = usbd_req_clear_port_feature(udev, NULL,
315 portno, UHF_C_PORT_CONNECTION);
320 /* detach any existing devices */
324 usb_free_device(child,
325 USB_UNCFG_FLAG_FREE_SUBDEV |
326 USB_UNCFG_FLAG_FREE_EP0);
330 /* get fresh status */
332 err = uhub_read_port_status(sc, portno);
336 /* check if nothing is connected to the port */
338 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
341 /* check if there is no power on the port and print a warning */
343 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
344 DPRINTF("WARNING: strange, connected port %d "
345 "has no power\n", portno);
347 /* check if the device is in Host Mode */
349 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
351 DPRINTF("Port %d is in Host Mode\n", portno);
353 if (sc->sc_st.port_status & UPS_SUSPEND) {
354 DPRINTF("Port %d was still "
355 "suspended, clearing.\n", portno);
356 err = usbd_req_clear_port_feature(sc->sc_udev,
357 NULL, portno, UHF_PORT_SUSPEND);
361 /* wait for maximum device power up time */
364 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
366 /* reset port, which implies enabling it */
368 err = usbd_req_reset_port(udev, NULL, portno);
371 DPRINTFN(0, "port %d reset "
372 "failed, error=%s\n",
373 portno, usbd_errstr(err));
376 /* get port status again, it might have changed during reset */
378 err = uhub_read_port_status(sc, portno);
382 /* check if something changed during port reset */
384 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
385 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
387 DPRINTFN(0, "giving up port reset "
388 "- device vanished!\n");
395 DPRINTF("Port %d is in Device Mode\n", portno);
399 * Figure out the device speed
401 switch (udev->speed) {
403 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
404 speed = USB_SPEED_HIGH;
405 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
406 speed = USB_SPEED_LOW;
408 speed = USB_SPEED_FULL;
411 if (sc->sc_st.port_status & UPS_LOW_SPEED)
412 speed = USB_SPEED_LOW;
414 speed = USB_SPEED_FULL;
417 speed = USB_SPEED_LOW;
420 /* same speed like parent */
425 * Figure out the device mode
427 * NOTE: This part is currently FreeBSD specific.
429 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
430 mode = USB_MODE_DEVICE;
432 mode = USB_MODE_HOST;
434 /* need to create a new child */
436 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
437 udev->depth + 1, portno - 1, portno, speed, mode);
440 DPRINTFN(0, "could not allocate new device!\n");
443 return (0); /* success */
448 usb_free_device(child,
449 USB_UNCFG_FLAG_FREE_SUBDEV |
450 USB_UNCFG_FLAG_FREE_EP0);
455 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
456 err = usbd_req_clear_port_feature(
458 portno, UHF_PORT_ENABLE);
462 DPRINTFN(0, "device problem (%s), "
463 "disabling port %d\n", usbd_errstr(err), portno);
468 /*------------------------------------------------------------------------*
469 * uhub_suspend_resume_port
473 * Else: A control transaction failed
474 *------------------------------------------------------------------------*/
476 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
478 struct usb_device *child;
479 struct usb_device *udev;
483 DPRINTF("port %d\n", portno);
486 child = usb_bus_port_get_device(udev->bus,
487 udev->hub->ports + portno - 1);
489 /* first clear the port suspend change bit */
491 err = usbd_req_clear_port_feature(udev, NULL,
492 portno, UHF_C_PORT_SUSPEND);
494 DPRINTF("clearing suspend failed.\n");
497 /* get fresh status */
499 err = uhub_read_port_status(sc, portno);
501 DPRINTF("reading port status failed.\n");
504 /* get current state */
506 if (sc->sc_st.port_status & UPS_SUSPEND) {
512 DPRINTF("suspended=%u\n", is_suspend);
514 /* do the suspend or resume */
518 * This code handle two cases: 1) Host Mode - we can only
519 * receive resume here 2) Device Mode - we can receive
520 * suspend and resume here
523 usb_dev_resume_peer(child);
524 else if (child->flags.usb_mode == USB_MODE_DEVICE)
525 usb_dev_suspend_peer(child);
531 /*------------------------------------------------------------------------*
532 * uhub_root_interrupt
534 * This function is called when a Root HUB interrupt has
535 * happened. "ptr" and "len" makes up the Root HUB interrupt
536 * packet. This function is called having the "bus_mtx" locked.
537 *------------------------------------------------------------------------*/
539 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
541 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
543 usb_needs_explore(bus, 0);
546 /*------------------------------------------------------------------------*
552 *------------------------------------------------------------------------*/
554 uhub_explore(struct usb_device *udev)
557 struct uhub_softc *sc;
566 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
568 /* ignore hubs that are too deep */
569 if (udev->depth > USB_HUB_MAX_DEPTH) {
570 return (USB_ERR_TOO_DEEP);
573 if (udev->flags.self_suspended) {
574 /* need to wait until the child signals resume */
575 DPRINTF("Device is suspended!\n");
578 for (x = 0; x != hub->nports; x++) {
582 err = uhub_read_port_status(sc, portno);
584 /* most likely the HUB is gone */
587 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
588 DPRINTF("Overcurrent on port %u.\n", portno);
589 err = usbd_req_clear_port_feature(
590 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
592 /* most likely the HUB is gone */
596 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
598 * Fake a connect status change so that the
599 * status gets checked initially!
601 sc->sc_st.port_change |=
602 UPS_C_CONNECT_STATUS;
604 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
605 err = usbd_req_clear_port_feature(
606 udev, NULL, portno, UHF_C_PORT_ENABLE);
608 /* most likely the HUB is gone */
611 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
613 * Ignore the port error if the device
616 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
617 DPRINTFN(0, "illegal enable change, "
618 "port %d\n", portno);
621 if (up->restartcnt == USB_RESTART_MAX) {
622 /* XXX could try another speed ? */
623 DPRINTFN(0, "port error, giving up "
624 "port %d\n", portno);
626 sc->sc_st.port_change |=
627 UPS_C_CONNECT_STATUS;
632 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
633 err = uhub_reattach_port(sc, portno);
635 /* most likely the HUB is gone */
639 if (sc->sc_st.port_change & UPS_C_SUSPEND) {
640 err = uhub_suspend_resume_port(sc, portno);
642 /* most likely the HUB is gone */
646 err = uhub_explore_sub(sc, up);
648 /* no device(s) present */
651 /* explore succeeded - reset restart counter */
655 /* initial status checked */
656 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
659 return (USB_ERR_NORMAL_COMPLETION);
663 uhub_probe(device_t dev)
665 struct usb_attach_arg *uaa = device_get_ivars(dev);
667 if (uaa->usb_mode != USB_MODE_HOST) {
671 * The subclass for USB HUBs is ignored because it is 0 for
672 * some and 1 for others.
674 if ((uaa->info.bConfigIndex == 0) &&
675 (uaa->info.bDeviceClass == UDCLASS_HUB)) {
682 uhub_attach(device_t dev)
684 struct uhub_softc *sc = device_get_softc(dev);
685 struct usb_attach_arg *uaa = device_get_ivars(dev);
686 struct usb_device *udev = uaa->device;
687 struct usb_device *parent_hub = udev->parent_hub;
689 struct usb_hub_descriptor hubdesc;
701 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
702 device_get_nameunit(dev));
704 device_set_usb_desc(dev);
706 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
707 "parent->selfpowered=%d\n",
709 udev->flags.self_powered,
712 parent_hub->flags.self_powered : 0);
714 if (udev->depth > USB_HUB_MAX_DEPTH) {
715 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored!\n",
719 if (!udev->flags.self_powered && parent_hub &&
720 (!parent_hub->flags.self_powered)) {
721 DPRINTFN(0, "bus powered HUB connected to "
722 "bus powered HUB. HUB ignored!\n");
725 /* get HUB descriptor */
727 DPRINTFN(2, "getting HUB descriptor\n");
729 /* assuming that there is one port */
730 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, 1);
732 nports = hubdesc.bNbrPorts;
734 if (!err && (nports >= 8)) {
735 /* get complete HUB descriptor */
736 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, nports);
739 DPRINTFN(0, "getting hub descriptor failed,"
740 "error=%s\n", usbd_errstr(err));
743 if (hubdesc.bNbrPorts != nports) {
744 DPRINTFN(0, "number of ports changed!\n");
748 DPRINTFN(0, "portless HUB!\n");
751 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
752 M_USBDEV, M_WAITOK | M_ZERO);
759 #if USB_HAVE_TT_SUPPORT
760 /* init FULL-speed ISOCHRONOUS schedule */
761 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
763 /* initialize HUB structure */
765 hub->explore = &uhub_explore;
766 hub->nports = hubdesc.bNbrPorts;
769 /* if self powered hub, give ports maximum current */
770 if (udev->flags.self_powered) {
771 hub->portpower = USB_MAX_POWER;
773 hub->portpower = USB_MIN_POWER;
776 /* set up interrupt pipe */
778 if (udev->parent_hub == NULL) {
779 /* root HUB is special */
783 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
784 uhub_config, UHUB_N_TRANSFER, sc, &Giant);
787 DPRINTFN(0, "cannot setup interrupt transfer, "
788 "errstr=%s!\n", usbd_errstr(err));
791 /* wait with power off for a while */
792 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
795 * To have the best chance of success we do things in the exact same
796 * order as Windoze98. This should not be necessary, but some
797 * devices do not follow the USB specs to the letter.
799 * These are the events on the bus when a hub is attached:
800 * Get device and config descriptors (see attach code)
801 * Get hub descriptor (see above)
804 * wait for power to become stable
805 * (all below happens in explore code)
807 * clear C_PORT_CONNECTION
810 * if device connected
816 * proceed with device attachment
819 /* XXX should check for none, individual, or ganged power? */
822 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
823 USB_EXTRA_POWER_UP_TIME);
825 for (x = 0; x != nports; x++) {
826 /* set up data structures */
827 struct usb_port *up = hub->ports + x;
829 up->device_index = 0;
833 /* check if port is removable */
834 if (!UHD_NOT_REMOV(&hubdesc, portno)) {
838 /* turn the power on */
839 err = usbd_req_set_port_feature(udev, NULL,
840 portno, UHF_PORT_POWER);
843 DPRINTFN(0, "port %d power on failed, %s\n",
844 portno, usbd_errstr(err));
846 DPRINTF("turn on port %d power\n",
849 /* wait for stable power */
850 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
853 device_printf(dev, "%d port%s with %d "
854 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
855 removable, udev->flags.self_powered ? "self" : "bus");
857 /* Start the interrupt endpoint, if any */
859 if (sc->sc_xfer[0] != NULL) {
860 USB_XFER_LOCK(sc->sc_xfer[0]);
861 usbd_transfer_start(sc->sc_xfer[0]);
862 USB_XFER_UNLOCK(sc->sc_xfer[0]);
865 /* Enable automatic power save on all USB HUBs */
867 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
872 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
875 free(udev->hub, M_USBDEV);
882 * Called from process context when the hub is gone.
883 * Detach all devices on active ports.
886 uhub_detach(device_t dev)
888 struct uhub_softc *sc = device_get_softc(dev);
889 struct usb_hub *hub = sc->sc_udev->hub;
890 struct usb_device *child;
893 /* detach all children first */
894 bus_generic_detach(dev);
896 if (hub == NULL) { /* must be partially working */
899 for (x = 0; x != hub->nports; x++) {
901 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
907 * Subdevices are not freed, because the caller of
908 * uhub_detach() will do that.
910 usb_free_device(child,
911 USB_UNCFG_FLAG_FREE_EP0);
914 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
917 sc->sc_udev->hub = NULL;
922 uhub_suspend(device_t dev)
925 /* Sub-devices are not suspended here! */
930 uhub_resume(device_t dev)
933 /* Sub-devices are not resumed here! */
938 uhub_driver_added(device_t dev, driver_t *driver)
940 usb_needs_explore_all();
944 struct usb_device *udev;
950 uhub_find_iface_index(struct usb_hub *hub, device_t child,
951 struct hub_result *res)
953 struct usb_interface *iface;
954 struct usb_device *udev;
959 nports = hub->nports;
960 for (x = 0; x != nports; x++) {
961 udev = usb_bus_port_get_device(hub->hubudev->bus,
966 for (i = 0; i != USB_IFACE_MAX; i++) {
967 iface = usbd_get_iface(udev, i);
969 (iface->subdev == child)) {
970 res->iface_index = i;
977 res->iface_index = 0;
983 uhub_child_location_string(device_t parent, device_t child,
984 char *buf, size_t buflen)
986 struct uhub_softc *sc = device_get_softc(parent);
987 struct usb_hub *hub = sc->sc_udev->hub;
988 struct hub_result res;
990 uhub_find_iface_index(hub, child, &res);
992 DPRINTF("device not on hub\n");
998 snprintf(buf, buflen, "port=%u interface=%u",
999 res.portno, res.iface_index);
1006 uhub_child_pnpinfo_string(device_t parent, device_t child,
1007 char *buf, size_t buflen)
1009 struct uhub_softc *sc = device_get_softc(parent);
1010 struct usb_hub *hub = sc->sc_udev->hub;
1011 struct usb_interface *iface;
1012 struct hub_result res;
1014 uhub_find_iface_index(hub, child, &res);
1016 DPRINTF("device not on hub\n");
1022 iface = usbd_get_iface(res.udev, res.iface_index);
1023 if (iface && iface->idesc) {
1024 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1025 "devclass=0x%02x devsubclass=0x%02x "
1028 "intclass=0x%02x intsubclass=0x%02x",
1029 UGETW(res.udev->ddesc.idVendor),
1030 UGETW(res.udev->ddesc.idProduct),
1031 res.udev->ddesc.bDeviceClass,
1032 res.udev->ddesc.bDeviceSubClass,
1034 UGETW(res.udev->ddesc.bcdDevice),
1035 iface->idesc->bInterfaceClass,
1036 iface->idesc->bInterfaceSubClass);
1049 * The USB Transaction Translator:
1050 * ===============================
1052 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1053 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1054 * USB transfers. To utilize bandwidth dynamically the "scatter and
1055 * gather" principle must be applied. This means that bandwidth must
1056 * be divided into equal parts of bandwidth. With regard to USB all
1057 * data is transferred in smaller packets with length
1058 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1061 * The bandwidth scheduler which I have implemented will simply pack
1062 * the USB transfers back to back until there is no more space in the
1063 * schedule. Out of the 8 microframes which the USB 2.0 standard
1064 * provides, only 6 are available for non-HIGH-speed devices. I have
1065 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1066 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1067 * this division, it is very difficult to allocate and free bandwidth
1070 * NOTE about the Transaction Translator in USB HUBs:
1072 * USB HUBs have a very simple Transaction Translator, that will
1073 * simply pipeline all the SPLIT transactions. That means that the
1074 * transactions will be executed in the order they are queued!
1078 /*------------------------------------------------------------------------*
1079 * usb_intr_find_best_slot
1082 * The best Transaction Translation slot for an interrupt endpoint.
1083 *------------------------------------------------------------------------*/
1085 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start, uint8_t end)
1087 usb_size_t max = 0 - 1;
1093 /* find the last slot with lesser used bandwidth */
1095 for (x = start; x < end; x++) {
1096 if (max >= ptr[x]) {
1104 /*------------------------------------------------------------------------*
1105 * usb_intr_schedule_adjust
1107 * This function will update the bandwith usage for the microframe
1108 * having index "slot" by "len" bytes. "len" can be negative. If the
1109 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1110 * the "slot" argument will be replaced by the slot having least used
1114 * The slot on which the bandwidth update was done.
1115 *------------------------------------------------------------------------*/
1117 usb_intr_schedule_adjust(struct usb_device *udev, int16_t len, uint8_t slot)
1119 struct usb_bus *bus = udev->bus;
1120 struct usb_hub *hub;
1121 enum usb_dev_speed speed;
1123 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1125 speed = usbd_get_speed(udev);
1129 case USB_SPEED_FULL:
1130 if (speed == USB_SPEED_LOW) {
1134 * The Host Controller Driver should have
1135 * performed checks so that the lookup
1136 * below does not result in a NULL pointer
1140 hub = udev->parent_hs_hub->hub;
1141 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1142 slot = usb_intr_find_best_slot(hub->uframe_usage,
1143 USB_FS_ISOC_UFRAME_MAX, 6);
1145 hub->uframe_usage[slot] += len;
1146 bus->uframe_usage[slot] += len;
1149 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1150 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1151 USB_HS_MICRO_FRAMES_MAX);
1153 bus->uframe_usage[slot] += len;
1159 /*------------------------------------------------------------------------*
1160 * usbd_fs_isoc_schedule_init_sub
1162 * This function initialises an USB FULL speed isochronous schedule
1164 *------------------------------------------------------------------------*/
1165 #if USB_HAVE_TT_SUPPORT
1167 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1169 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1170 USB_FS_BYTES_PER_HS_UFRAME);
1171 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1172 fss->frame_slot = 0;
1176 /*------------------------------------------------------------------------*
1177 * usbd_fs_isoc_schedule_init_all
1179 * This function will reset the complete USB FULL speed isochronous
1180 * bandwidth schedule.
1181 *------------------------------------------------------------------------*/
1182 #if USB_HAVE_TT_SUPPORT
1184 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1186 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1188 while (fss != fss_end) {
1189 usbd_fs_isoc_schedule_init_sub(fss);
1195 /*------------------------------------------------------------------------*
1196 * usb_isoc_time_expand
1198 * This function will expand the time counter from 7-bit to 16-bit.
1201 * 16-bit isochronous time counter.
1202 *------------------------------------------------------------------------*/
1204 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1208 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1210 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1212 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1214 if (isoc_time_curr < rem) {
1215 /* the time counter wrapped around */
1216 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1218 /* update the remainder */
1220 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1221 bus->isoc_time_last |= isoc_time_curr;
1223 return (bus->isoc_time_last);
1226 /*------------------------------------------------------------------------*
1227 * usbd_fs_isoc_schedule_isoc_time_expand
1229 * This function does multiple things. First of all it will expand the
1230 * passed isochronous time, which is the return value. Then it will
1231 * store where the current FULL speed isochronous schedule is
1232 * positioned in time and where the end is. See "pp_start" and
1233 * "pp_end" arguments.
1236 * Expanded version of "isoc_time".
1238 * NOTE: This function depends on being called regularly with
1239 * intervals less than "USB_ISOC_TIME_MAX".
1240 *------------------------------------------------------------------------*/
1241 #if USB_HAVE_TT_SUPPORT
1243 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1244 struct usb_fs_isoc_schedule **pp_start,
1245 struct usb_fs_isoc_schedule **pp_end,
1248 struct usb_fs_isoc_schedule *fss_end;
1249 struct usb_fs_isoc_schedule *fss_a;
1250 struct usb_fs_isoc_schedule *fss_b;
1251 struct usb_hub *hs_hub;
1253 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1255 hs_hub = udev->parent_hs_hub->hub;
1257 if (hs_hub != NULL) {
1259 fss_a = hs_hub->fs_isoc_schedule +
1260 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1262 hs_hub->isoc_last_time = isoc_time;
1264 fss_b = hs_hub->fs_isoc_schedule +
1265 (isoc_time % USB_ISOC_TIME_MAX);
1267 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1269 *pp_start = hs_hub->fs_isoc_schedule;
1272 while (fss_a != fss_b) {
1273 if (fss_a == fss_end) {
1274 fss_a = hs_hub->fs_isoc_schedule;
1277 usbd_fs_isoc_schedule_init_sub(fss_a);
1290 /*------------------------------------------------------------------------*
1291 * usbd_fs_isoc_schedule_alloc
1293 * This function will allocate bandwidth for an isochronous FULL speed
1294 * transaction in the FULL speed schedule. The microframe slot where
1295 * the transaction should be started is stored in the byte pointed to
1296 * by "pstart". The "len" argument specifies the length of the
1297 * transaction in bytes.
1302 *------------------------------------------------------------------------*/
1303 #if USB_HAVE_TT_SUPPORT
1305 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1306 uint8_t *pstart, uint16_t len)
1308 uint8_t slot = fss->frame_slot;
1310 /* Compute overhead and bit-stuffing */
1317 if (len > fss->total_bytes) {
1318 *pstart = 0; /* set some dummy value */
1319 return (1); /* error */
1323 fss->total_bytes -= len;
1325 while (len >= fss->frame_bytes) {
1326 len -= fss->frame_bytes;
1327 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1331 fss->frame_bytes -= len;
1334 return (0); /* success */
1338 /*------------------------------------------------------------------------*
1339 * usb_bus_port_get_device
1341 * This function is NULL safe.
1342 *------------------------------------------------------------------------*/
1344 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1346 if ((bus == NULL) || (up == NULL)) {
1350 if (up->device_index == 0) {
1354 return (bus->devices[up->device_index]);
1357 /*------------------------------------------------------------------------*
1358 * usb_bus_port_set_device
1360 * This function is NULL safe.
1361 *------------------------------------------------------------------------*/
1363 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1364 struct usb_device *udev, uint8_t device_index)
1371 * There is only one case where we don't
1372 * have an USB port, and that is the Root Hub!
1376 up->device_index = device_index;
1378 device_index = up->device_index;
1379 up->device_index = 0;
1383 * Make relationships to our new device
1385 if (device_index != 0) {
1387 mtx_lock(&usb_ref_lock);
1389 bus->devices[device_index] = udev;
1391 mtx_unlock(&usb_ref_lock);
1397 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1400 /*------------------------------------------------------------------------*
1403 * This functions is called when the USB event thread needs to run.
1404 *------------------------------------------------------------------------*/
1406 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1413 DPRINTF("No bus pointer!\n");
1416 if ((bus->devices == NULL) ||
1417 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1418 DPRINTF("No root HUB\n");
1421 if (mtx_owned(&bus->bus_mtx)) {
1430 if (usb_proc_msignal(&bus->explore_proc,
1431 &bus->explore_msg[0], &bus->explore_msg[1])) {
1435 USB_BUS_UNLOCK(bus);
1439 /*------------------------------------------------------------------------*
1440 * usb_needs_explore_all
1442 * This function is called whenever a new driver is loaded and will
1443 * cause that all USB busses are re-explored.
1444 *------------------------------------------------------------------------*/
1446 usb_needs_explore_all(void)
1448 struct usb_bus *bus;
1455 dc = usb_devclass_ptr;
1457 DPRINTFN(0, "no devclass\n");
1461 * Explore all USB busses in parallell.
1463 max = devclass_get_maxunit(dc);
1465 dev = devclass_get_device(dc, max);
1467 bus = device_get_softc(dev);
1469 usb_needs_explore(bus, 1);
1476 /*------------------------------------------------------------------------*
1477 * usb_bus_power_update
1479 * This function will ensure that all USB devices on the given bus are
1480 * properly suspended or resumed according to the device transfer
1482 *------------------------------------------------------------------------*/
1485 usb_bus_power_update(struct usb_bus *bus)
1487 usb_needs_explore(bus, 0 /* no probe */ );
1491 /*------------------------------------------------------------------------*
1492 * usbd_transfer_power_ref
1494 * This function will modify the power save reference counts and
1495 * wakeup the USB device associated with the given USB transfer, if
1497 *------------------------------------------------------------------------*/
1500 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1502 static const usb_power_mask_t power_mask[4] = {
1503 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1504 [UE_BULK] = USB_HW_POWER_BULK,
1505 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1506 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1508 struct usb_device *udev;
1509 uint8_t needs_explore;
1510 uint8_t needs_hw_power;
1513 udev = xfer->xroot->udev;
1515 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1516 /* no power save for root HUB */
1519 USB_BUS_LOCK(udev->bus);
1521 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1523 udev->pwr_save.last_xfer_time = ticks;
1524 udev->pwr_save.type_refs[xfer_type] += val;
1526 if (xfer->flags_int.control_xfr) {
1527 udev->pwr_save.read_refs += val;
1528 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1530 * it is not allowed to suspend during a control
1533 udev->pwr_save.write_refs += val;
1535 } else if (USB_GET_DATA_ISREAD(xfer)) {
1536 udev->pwr_save.read_refs += val;
1538 udev->pwr_save.write_refs += val;
1541 if (udev->flags.self_suspended)
1543 (udev->pwr_save.write_refs != 0) ||
1544 ((udev->pwr_save.read_refs != 0) &&
1545 (usb_peer_can_wakeup(udev) == 0));
1549 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1550 DPRINTF("Adding type %u to power state\n", xfer_type);
1551 udev->bus->hw_power_state |= power_mask[xfer_type];
1557 USB_BUS_UNLOCK(udev->bus);
1559 if (needs_explore) {
1560 DPRINTF("update\n");
1561 usb_bus_power_update(udev->bus);
1562 } else if (needs_hw_power) {
1563 DPRINTF("needs power\n");
1564 if (udev->bus->methods->set_hw_power != NULL) {
1565 (udev->bus->methods->set_hw_power) (udev->bus);
1571 /*------------------------------------------------------------------------*
1574 * This function implements the USB power daemon and is called
1575 * regularly from the USB explore thread.
1576 *------------------------------------------------------------------------*/
1579 usb_bus_powerd(struct usb_bus *bus)
1581 struct usb_device *udev;
1584 usb_ticks_t mintime;
1585 usb_size_t type_refs[5];
1589 limit = usb_power_timeout;
1592 else if (limit > 255)
1597 DPRINTF("bus=%p\n", bus);
1602 * The root HUB device is never suspended
1603 * and we simply skip it.
1605 for (x = USB_ROOT_HUB_ADDR + 1;
1606 x != bus->devices_max; x++) {
1608 udev = bus->devices[x];
1612 rem_wakeup = usb_peer_can_wakeup(udev);
1614 temp = ticks - udev->pwr_save.last_xfer_time;
1616 if ((udev->power_mode == USB_POWER_MODE_ON) ||
1617 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1618 (udev->pwr_save.write_refs != 0) ||
1619 ((udev->pwr_save.read_refs != 0) &&
1620 (rem_wakeup == 0))) {
1622 /* check if we are suspended */
1623 if (udev->flags.self_suspended != 0) {
1624 USB_BUS_UNLOCK(bus);
1625 usb_dev_resume_peer(udev);
1628 } else if (temp >= limit) {
1630 /* check if we are not suspended */
1631 if (udev->flags.self_suspended == 0) {
1632 USB_BUS_UNLOCK(bus);
1633 usb_dev_suspend_peer(udev);
1639 /* reset counters */
1648 /* Re-loop all the devices to get the actual state */
1650 for (x = USB_ROOT_HUB_ADDR + 1;
1651 x != bus->devices_max; x++) {
1653 udev = bus->devices[x];
1657 /* we found a non-Root-Hub USB device */
1660 /* "last_xfer_time" can be updated by a resume */
1661 temp = ticks - udev->pwr_save.last_xfer_time;
1664 * Compute minimum time since last transfer for the complete
1670 if (udev->flags.self_suspended == 0) {
1671 type_refs[0] += udev->pwr_save.type_refs[0];
1672 type_refs[1] += udev->pwr_save.type_refs[1];
1673 type_refs[2] += udev->pwr_save.type_refs[2];
1674 type_refs[3] += udev->pwr_save.type_refs[3];
1678 if (mintime >= (1 * hz)) {
1679 /* recompute power masks */
1680 DPRINTF("Recomputing power masks\n");
1681 bus->hw_power_state = 0;
1682 if (type_refs[UE_CONTROL] != 0)
1683 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1684 if (type_refs[UE_BULK] != 0)
1685 bus->hw_power_state |= USB_HW_POWER_BULK;
1686 if (type_refs[UE_INTERRUPT] != 0)
1687 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1688 if (type_refs[UE_ISOCHRONOUS] != 0)
1689 bus->hw_power_state |= USB_HW_POWER_ISOC;
1690 if (type_refs[4] != 0)
1691 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1693 USB_BUS_UNLOCK(bus);
1695 if (bus->methods->set_hw_power != NULL) {
1696 /* always update hardware power! */
1697 (bus->methods->set_hw_power) (bus);
1703 /*------------------------------------------------------------------------*
1704 * usb_dev_resume_peer
1706 * This function will resume an USB peer and do the required USB
1707 * signalling to get an USB device out of the suspended state.
1708 *------------------------------------------------------------------------*/
1710 usb_dev_resume_peer(struct usb_device *udev)
1712 struct usb_bus *bus;
1719 /* check if already resumed */
1720 if (udev->flags.self_suspended == 0)
1723 /* we need a parent HUB to do resume */
1724 if (udev->parent_hub == NULL)
1727 DPRINTF("udev=%p\n", udev);
1729 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1730 (udev->flags.remote_wakeup == 0)) {
1732 * If the host did not set the remote wakeup feature, we can
1733 * not wake it up either!
1735 DPRINTF("remote wakeup is not set!\n");
1738 /* get bus pointer */
1741 /* resume parent hub first */
1742 usb_dev_resume_peer(udev->parent_hub);
1744 /* resume current port (Valid in Host and Device Mode) */
1745 err = usbd_req_clear_port_feature(udev->parent_hub,
1746 NULL, udev->port_no, UHF_PORT_SUSPEND);
1748 DPRINTFN(0, "Resuming port failed!\n");
1751 /* resume settle time */
1752 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1754 if (bus->methods->device_resume != NULL) {
1755 /* resume USB device on the USB controller */
1756 (bus->methods->device_resume) (udev);
1759 /* set that this device is now resumed */
1760 udev->flags.self_suspended = 0;
1762 /* make sure that we don't go into suspend right away */
1763 udev->pwr_save.last_xfer_time = ticks;
1765 /* make sure the needed power masks are on */
1766 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1767 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1768 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1769 bus->hw_power_state |= USB_HW_POWER_BULK;
1770 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1771 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1772 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1773 bus->hw_power_state |= USB_HW_POWER_ISOC;
1775 USB_BUS_UNLOCK(bus);
1777 if (bus->methods->set_hw_power != NULL) {
1778 /* always update hardware power! */
1779 (bus->methods->set_hw_power) (bus);
1782 sx_xlock(udev->default_sx + 1);
1784 /* notify all sub-devices about resume */
1785 err = usb_suspend_resume(udev, 0);
1786 sx_unlock(udev->default_sx + 1);
1789 /* check if peer has wakeup capability */
1790 if (usb_peer_can_wakeup(udev)) {
1791 /* clear remote wakeup */
1792 err = usbd_req_clear_device_feature(udev,
1793 NULL, UF_DEVICE_REMOTE_WAKEUP);
1795 DPRINTFN(0, "Clearing device "
1796 "remote wakeup failed: %s!\n",
1803 /*------------------------------------------------------------------------*
1804 * usb_dev_suspend_peer
1806 * This function will suspend an USB peer and do the required USB
1807 * signalling to get an USB device into the suspended state.
1808 *------------------------------------------------------------------------*/
1810 usb_dev_suspend_peer(struct usb_device *udev)
1812 struct usb_device *child;
1822 /* check if already suspended */
1823 if (udev->flags.self_suspended)
1826 /* we need a parent HUB to do suspend */
1827 if (udev->parent_hub == NULL)
1830 DPRINTF("udev=%p\n", udev);
1832 /* check if the current device is a HUB */
1833 if (udev->hub != NULL) {
1834 nports = udev->hub->nports;
1836 /* check if all devices on the HUB are suspended */
1837 for (x = 0; x != nports; x++) {
1839 child = usb_bus_port_get_device(udev->bus,
1840 udev->hub->ports + x);
1845 if (child->flags.self_suspended)
1848 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
1854 sx_xlock(udev->default_sx + 1);
1856 /* notify all sub-devices about suspend */
1857 err = usb_suspend_resume(udev, 1);
1858 sx_unlock(udev->default_sx + 1);
1861 if (usb_peer_can_wakeup(udev)) {
1862 /* allow device to do remote wakeup */
1863 err = usbd_req_set_device_feature(udev,
1864 NULL, UF_DEVICE_REMOTE_WAKEUP);
1866 DPRINTFN(0, "Setting device "
1867 "remote wakeup failed!\n");
1870 USB_BUS_LOCK(udev->bus);
1872 * Set that this device is suspended. This variable must be set
1873 * before calling USB controller suspend callbacks.
1875 udev->flags.self_suspended = 1;
1876 USB_BUS_UNLOCK(udev->bus);
1878 if (udev->bus->methods->device_suspend != NULL) {
1881 /* suspend device on the USB controller */
1882 (udev->bus->methods->device_suspend) (udev);
1885 temp = usbd_get_dma_delay(udev->bus);
1886 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
1889 /* suspend current port */
1890 err = usbd_req_set_port_feature(udev->parent_hub,
1891 NULL, udev->port_no, UHF_PORT_SUSPEND);
1893 DPRINTFN(0, "Suspending port failed\n");
1897 udev = udev->parent_hub;
1901 /*------------------------------------------------------------------------*
1902 * usbd_set_power_mode
1904 * This function will set the power mode, see USB_POWER_MODE_XXX for a
1906 *------------------------------------------------------------------------*/
1908 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
1910 /* filter input argument */
1911 if ((power_mode != USB_POWER_MODE_ON) &&
1912 (power_mode != USB_POWER_MODE_OFF)) {
1913 power_mode = USB_POWER_MODE_SAVE;
1915 udev->power_mode = power_mode; /* update copy of power mode */
1918 usb_bus_power_update(udev->bus);