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 mtx sc_mtx; /* our mutex */
100 struct usb_device *sc_udev; /* USB device */
101 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
103 #define UHUB_FLAG_DID_EXPLORE 0x01
107 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
108 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
109 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
111 /* prototypes for type checking: */
113 static device_probe_t uhub_probe;
114 static device_attach_t uhub_attach;
115 static device_detach_t uhub_detach;
116 static device_suspend_t uhub_suspend;
117 static device_resume_t uhub_resume;
119 static bus_driver_added_t uhub_driver_added;
120 static bus_child_location_str_t uhub_child_location_string;
121 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
123 static usb_callback_t uhub_intr_callback;
125 static void usb_dev_resume_peer(struct usb_device *udev);
126 static void usb_dev_suspend_peer(struct usb_device *udev);
128 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
131 .type = UE_INTERRUPT,
132 .endpoint = UE_ADDR_ANY,
133 .direction = UE_DIR_ANY,
135 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
136 .bufsize = 0, /* use wMaxPacketSize */
137 .callback = &uhub_intr_callback,
138 .interval = UHUB_INTR_INTERVAL,
143 * driver instance for "hub" connected to "usb"
144 * and "hub" connected to "hub"
146 static devclass_t uhub_devclass;
148 static device_method_t uhub_methods[] = {
149 DEVMETHOD(device_probe, uhub_probe),
150 DEVMETHOD(device_attach, uhub_attach),
151 DEVMETHOD(device_detach, uhub_detach),
153 DEVMETHOD(device_suspend, uhub_suspend),
154 DEVMETHOD(device_resume, uhub_resume),
156 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
157 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
158 DEVMETHOD(bus_driver_added, uhub_driver_added),
162 static driver_t uhub_driver = {
164 .methods = uhub_methods,
165 .size = sizeof(struct uhub_softc)
168 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
169 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
172 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
174 struct uhub_softc *sc = usbd_xfer_softc(xfer);
176 switch (USB_GET_STATE(xfer)) {
177 case USB_ST_TRANSFERRED:
180 * This is an indication that some port
181 * has changed status. Notify the bus
182 * event handler thread that we need
183 * to be explored again:
185 usb_needs_explore(sc->sc_udev->bus, 0);
188 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
189 usbd_transfer_submit(xfer);
193 if (xfer->error != USB_ERR_CANCELLED) {
195 * Do a clear-stall. The "stall_pipe" flag
196 * will get cleared before next callback by
199 usbd_xfer_set_stall(xfer);
200 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
201 usbd_transfer_submit(xfer);
207 /*------------------------------------------------------------------------*
208 * uhub_explore_sub - subroutine
212 * Else: A control transaction failed
213 *------------------------------------------------------------------------*/
215 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
218 struct usb_device *child;
222 bus = sc->sc_udev->bus;
225 /* get driver added refcount from USB bus */
226 refcount = bus->driver_added_refcount;
228 /* get device assosiated with the given port */
229 child = usb_bus_port_get_device(bus, up);
234 /* check if probe and attach should be done */
236 if (child->driver_added_refcount != refcount) {
237 child->driver_added_refcount = refcount;
238 err = usb_probe_and_attach(child,
239 USB_IFACE_INDEX_ANY);
244 /* start control transfer, if device mode */
246 if (child->flags.usb_mode == USB_MODE_DEVICE) {
247 usbd_default_transfer_setup(child);
249 /* if a HUB becomes present, do a recursive HUB explore */
252 err = (child->hub->explore) (child);
258 /*------------------------------------------------------------------------*
259 * uhub_read_port_status - factored out code
260 *------------------------------------------------------------------------*/
262 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
264 struct usb_port_status ps;
267 err = usbd_req_get_port_status(
268 sc->sc_udev, NULL, &ps, portno);
270 /* update status regardless of error */
272 sc->sc_st.port_status = UGETW(ps.wPortStatus);
273 sc->sc_st.port_change = UGETW(ps.wPortChange);
275 /* debugging print */
277 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
278 "wPortChange=0x%04x, err=%s\n",
279 portno, sc->sc_st.port_status,
280 sc->sc_st.port_change, usbd_errstr(err));
284 /*------------------------------------------------------------------------*
289 * Else: A control transaction failed
290 *------------------------------------------------------------------------*/
292 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
294 struct usb_device *child;
295 struct usb_device *udev;
296 enum usb_dev_speed speed;
297 enum usb_hc_mode mode;
301 DPRINTF("reattaching port %d\n", portno);
306 child = usb_bus_port_get_device(udev->bus,
307 udev->hub->ports + portno - 1);
311 /* first clear the port connection change bit */
313 err = usbd_req_clear_port_feature(udev, NULL,
314 portno, UHF_C_PORT_CONNECTION);
319 /* detach any existing devices */
322 usb_free_device(child,
323 USB_UNCFG_FLAG_FREE_SUBDEV |
324 USB_UNCFG_FLAG_FREE_EP0);
327 /* get fresh status */
329 err = uhub_read_port_status(sc, portno);
333 /* check if nothing is connected to the port */
335 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
338 /* check if there is no power on the port and print a warning */
340 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
341 DPRINTF("WARNING: strange, connected port %d "
342 "has no power\n", portno);
344 /* check if the device is in Host Mode */
346 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
348 DPRINTF("Port %d is in Host Mode\n", portno);
350 if (sc->sc_st.port_status & UPS_SUSPEND) {
351 DPRINTF("Port %d was still "
352 "suspended, clearing.\n", portno);
353 err = usbd_req_clear_port_feature(sc->sc_udev,
354 NULL, portno, UHF_PORT_SUSPEND);
358 /* wait for maximum device power up time */
361 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
363 /* reset port, which implies enabling it */
365 err = usbd_req_reset_port(udev, NULL, portno);
368 DPRINTFN(0, "port %d reset "
369 "failed, error=%s\n",
370 portno, usbd_errstr(err));
373 /* get port status again, it might have changed during reset */
375 err = uhub_read_port_status(sc, portno);
379 /* check if something changed during port reset */
381 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
382 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
384 DPRINTFN(0, "giving up port reset "
385 "- device vanished!\n");
392 DPRINTF("Port %d is in Device Mode\n", portno);
396 * Figure out the device speed
398 switch (udev->speed) {
400 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
401 speed = USB_SPEED_HIGH;
402 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
403 speed = USB_SPEED_LOW;
405 speed = USB_SPEED_FULL;
408 if (sc->sc_st.port_status & UPS_LOW_SPEED)
409 speed = USB_SPEED_LOW;
411 speed = USB_SPEED_FULL;
414 speed = USB_SPEED_LOW;
417 /* same speed like parent */
422 * Figure out the device mode
424 * NOTE: This part is currently FreeBSD specific.
426 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
427 mode = USB_MODE_DEVICE;
429 mode = USB_MODE_HOST;
431 /* 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 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
696 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
697 device_get_nameunit(dev));
699 device_set_usb_desc(dev);
701 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
702 "parent->selfpowered=%d\n",
704 udev->flags.self_powered,
707 parent_hub->flags.self_powered : 0);
709 if (udev->depth > USB_HUB_MAX_DEPTH) {
710 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored!\n",
714 if (!udev->flags.self_powered && parent_hub &&
715 (!parent_hub->flags.self_powered)) {
716 DPRINTFN(0, "bus powered HUB connected to "
717 "bus powered HUB. HUB ignored!\n");
720 /* get HUB descriptor */
722 DPRINTFN(2, "getting HUB descriptor\n");
724 /* assuming that there is one port */
725 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, 1);
727 nports = hubdesc.bNbrPorts;
729 if (!err && (nports >= 8)) {
730 /* get complete HUB descriptor */
731 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, nports);
734 DPRINTFN(0, "getting hub descriptor failed,"
735 "error=%s\n", usbd_errstr(err));
738 if (hubdesc.bNbrPorts != nports) {
739 DPRINTFN(0, "number of ports changed!\n");
743 DPRINTFN(0, "portless HUB!\n");
746 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
747 M_USBDEV, M_WAITOK | M_ZERO);
754 #if USB_HAVE_TT_SUPPORT
755 /* init FULL-speed ISOCHRONOUS schedule */
756 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
758 /* initialize HUB structure */
760 hub->explore = &uhub_explore;
761 hub->nports = hubdesc.bNbrPorts;
764 /* if self powered hub, give ports maximum current */
765 if (udev->flags.self_powered) {
766 hub->portpower = USB_MAX_POWER;
768 hub->portpower = USB_MIN_POWER;
771 /* set up interrupt pipe */
773 if (udev->parent_hub == NULL) {
774 /* root HUB is special */
778 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
779 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
782 DPRINTFN(0, "cannot setup interrupt transfer, "
783 "errstr=%s!\n", usbd_errstr(err));
786 /* wait with power off for a while */
787 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
790 * To have the best chance of success we do things in the exact same
791 * order as Windoze98. This should not be necessary, but some
792 * devices do not follow the USB specs to the letter.
794 * These are the events on the bus when a hub is attached:
795 * Get device and config descriptors (see attach code)
796 * Get hub descriptor (see above)
799 * wait for power to become stable
800 * (all below happens in explore code)
802 * clear C_PORT_CONNECTION
805 * if device connected
811 * proceed with device attachment
814 /* XXX should check for none, individual, or ganged power? */
817 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
818 USB_EXTRA_POWER_UP_TIME);
820 for (x = 0; x != nports; x++) {
821 /* set up data structures */
822 struct usb_port *up = hub->ports + x;
824 up->device_index = 0;
828 /* check if port is removable */
829 if (!UHD_NOT_REMOV(&hubdesc, portno)) {
833 /* turn the power on */
834 err = usbd_req_set_port_feature(udev, NULL,
835 portno, UHF_PORT_POWER);
838 DPRINTFN(0, "port %d power on failed, %s\n",
839 portno, usbd_errstr(err));
841 DPRINTF("turn on port %d power\n",
844 /* wait for stable power */
845 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
848 device_printf(dev, "%d port%s with %d "
849 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
850 removable, udev->flags.self_powered ? "self" : "bus");
852 /* Start the interrupt endpoint, if any */
854 if (sc->sc_xfer[0] != NULL) {
855 mtx_lock(&sc->sc_mtx);
856 usbd_transfer_start(sc->sc_xfer[0]);
857 mtx_unlock(&sc->sc_mtx);
860 /* Enable automatic power save on all USB HUBs */
862 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
867 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
870 free(udev->hub, M_USBDEV);
874 mtx_destroy(&sc->sc_mtx);
880 * Called from process context when the hub is gone.
881 * Detach all devices on active ports.
884 uhub_detach(device_t dev)
886 struct uhub_softc *sc = device_get_softc(dev);
887 struct usb_hub *hub = sc->sc_udev->hub;
888 struct usb_device *child;
891 /* detach all children first */
892 bus_generic_detach(dev);
894 if (hub == NULL) { /* must be partially working */
897 for (x = 0; x != hub->nports; x++) {
899 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
905 * Subdevices are not freed, because the caller of
906 * uhub_detach() will do that.
908 usb_free_device(child,
909 USB_UNCFG_FLAG_FREE_EP0);
912 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
915 sc->sc_udev->hub = NULL;
917 mtx_destroy(&sc->sc_mtx);
923 uhub_suspend(device_t dev)
926 /* Sub-devices are not suspended here! */
931 uhub_resume(device_t dev)
934 /* Sub-devices are not resumed here! */
939 uhub_driver_added(device_t dev, driver_t *driver)
941 usb_needs_explore_all();
945 struct usb_device *udev;
951 uhub_find_iface_index(struct usb_hub *hub, device_t child,
952 struct hub_result *res)
954 struct usb_interface *iface;
955 struct usb_device *udev;
960 nports = hub->nports;
961 for (x = 0; x != nports; x++) {
962 udev = usb_bus_port_get_device(hub->hubudev->bus,
967 for (i = 0; i != USB_IFACE_MAX; i++) {
968 iface = usbd_get_iface(udev, i);
970 (iface->subdev == child)) {
971 res->iface_index = i;
978 res->iface_index = 0;
984 uhub_child_location_string(device_t parent, device_t child,
985 char *buf, size_t buflen)
987 struct uhub_softc *sc = device_get_softc(parent);
988 struct usb_hub *hub = sc->sc_udev->hub;
989 struct hub_result res;
992 uhub_find_iface_index(hub, child, &res);
994 DPRINTF("device not on hub\n");
1000 snprintf(buf, buflen, "port=%u interface=%u",
1001 res.portno, res.iface_index);
1009 uhub_child_pnpinfo_string(device_t parent, device_t child,
1010 char *buf, size_t buflen)
1012 struct uhub_softc *sc = device_get_softc(parent);
1013 struct usb_hub *hub = sc->sc_udev->hub;
1014 struct usb_interface *iface;
1015 struct hub_result res;
1018 uhub_find_iface_index(hub, child, &res);
1020 DPRINTF("device not on hub\n");
1026 iface = usbd_get_iface(res.udev, res.iface_index);
1027 if (iface && iface->idesc) {
1028 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1029 "devclass=0x%02x devsubclass=0x%02x "
1032 "intclass=0x%02x intsubclass=0x%02x",
1033 UGETW(res.udev->ddesc.idVendor),
1034 UGETW(res.udev->ddesc.idProduct),
1035 res.udev->ddesc.bDeviceClass,
1036 res.udev->ddesc.bDeviceSubClass,
1038 UGETW(res.udev->ddesc.bcdDevice),
1039 iface->idesc->bInterfaceClass,
1040 iface->idesc->bInterfaceSubClass);
1054 * The USB Transaction Translator:
1055 * ===============================
1057 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1058 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1059 * USB transfers. To utilize bandwidth dynamically the "scatter and
1060 * gather" principle must be applied. This means that bandwidth must
1061 * be divided into equal parts of bandwidth. With regard to USB all
1062 * data is transferred in smaller packets with length
1063 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1066 * The bandwidth scheduler which I have implemented will simply pack
1067 * the USB transfers back to back until there is no more space in the
1068 * schedule. Out of the 8 microframes which the USB 2.0 standard
1069 * provides, only 6 are available for non-HIGH-speed devices. I have
1070 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1071 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1072 * this division, it is very difficult to allocate and free bandwidth
1075 * NOTE about the Transaction Translator in USB HUBs:
1077 * USB HUBs have a very simple Transaction Translator, that will
1078 * simply pipeline all the SPLIT transactions. That means that the
1079 * transactions will be executed in the order they are queued!
1083 /*------------------------------------------------------------------------*
1084 * usb_intr_find_best_slot
1087 * The best Transaction Translation slot for an interrupt endpoint.
1088 *------------------------------------------------------------------------*/
1090 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start, uint8_t end)
1092 usb_size_t max = 0 - 1;
1098 /* find the last slot with lesser used bandwidth */
1100 for (x = start; x < end; x++) {
1101 if (max >= ptr[x]) {
1109 /*------------------------------------------------------------------------*
1110 * usb_intr_schedule_adjust
1112 * This function will update the bandwith usage for the microframe
1113 * having index "slot" by "len" bytes. "len" can be negative. If the
1114 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1115 * the "slot" argument will be replaced by the slot having least used
1119 * The slot on which the bandwidth update was done.
1120 *------------------------------------------------------------------------*/
1122 usb_intr_schedule_adjust(struct usb_device *udev, int16_t len, uint8_t slot)
1124 struct usb_bus *bus = udev->bus;
1125 struct usb_hub *hub;
1126 enum usb_dev_speed speed;
1128 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1130 speed = usbd_get_speed(udev);
1134 case USB_SPEED_FULL:
1135 if (speed == USB_SPEED_LOW) {
1139 * The Host Controller Driver should have
1140 * performed checks so that the lookup
1141 * below does not result in a NULL pointer
1145 hub = udev->parent_hs_hub->hub;
1146 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1147 slot = usb_intr_find_best_slot(hub->uframe_usage,
1148 USB_FS_ISOC_UFRAME_MAX, 6);
1150 hub->uframe_usage[slot] += len;
1151 bus->uframe_usage[slot] += len;
1154 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1155 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1156 USB_HS_MICRO_FRAMES_MAX);
1158 bus->uframe_usage[slot] += len;
1164 /*------------------------------------------------------------------------*
1165 * usbd_fs_isoc_schedule_init_sub
1167 * This function initialises an USB FULL speed isochronous schedule
1169 *------------------------------------------------------------------------*/
1170 #if USB_HAVE_TT_SUPPORT
1172 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1174 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1175 USB_FS_BYTES_PER_HS_UFRAME);
1176 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1177 fss->frame_slot = 0;
1181 /*------------------------------------------------------------------------*
1182 * usbd_fs_isoc_schedule_init_all
1184 * This function will reset the complete USB FULL speed isochronous
1185 * bandwidth schedule.
1186 *------------------------------------------------------------------------*/
1187 #if USB_HAVE_TT_SUPPORT
1189 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1191 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1193 while (fss != fss_end) {
1194 usbd_fs_isoc_schedule_init_sub(fss);
1200 /*------------------------------------------------------------------------*
1201 * usb_isoc_time_expand
1203 * This function will expand the time counter from 7-bit to 16-bit.
1206 * 16-bit isochronous time counter.
1207 *------------------------------------------------------------------------*/
1209 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1213 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1215 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1217 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1219 if (isoc_time_curr < rem) {
1220 /* the time counter wrapped around */
1221 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1223 /* update the remainder */
1225 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1226 bus->isoc_time_last |= isoc_time_curr;
1228 return (bus->isoc_time_last);
1231 /*------------------------------------------------------------------------*
1232 * usbd_fs_isoc_schedule_isoc_time_expand
1234 * This function does multiple things. First of all it will expand the
1235 * passed isochronous time, which is the return value. Then it will
1236 * store where the current FULL speed isochronous schedule is
1237 * positioned in time and where the end is. See "pp_start" and
1238 * "pp_end" arguments.
1241 * Expanded version of "isoc_time".
1243 * NOTE: This function depends on being called regularly with
1244 * intervals less than "USB_ISOC_TIME_MAX".
1245 *------------------------------------------------------------------------*/
1246 #if USB_HAVE_TT_SUPPORT
1248 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1249 struct usb_fs_isoc_schedule **pp_start,
1250 struct usb_fs_isoc_schedule **pp_end,
1253 struct usb_fs_isoc_schedule *fss_end;
1254 struct usb_fs_isoc_schedule *fss_a;
1255 struct usb_fs_isoc_schedule *fss_b;
1256 struct usb_hub *hs_hub;
1258 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1260 hs_hub = udev->parent_hs_hub->hub;
1262 if (hs_hub != NULL) {
1264 fss_a = hs_hub->fs_isoc_schedule +
1265 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1267 hs_hub->isoc_last_time = isoc_time;
1269 fss_b = hs_hub->fs_isoc_schedule +
1270 (isoc_time % USB_ISOC_TIME_MAX);
1272 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1274 *pp_start = hs_hub->fs_isoc_schedule;
1277 while (fss_a != fss_b) {
1278 if (fss_a == fss_end) {
1279 fss_a = hs_hub->fs_isoc_schedule;
1282 usbd_fs_isoc_schedule_init_sub(fss_a);
1295 /*------------------------------------------------------------------------*
1296 * usbd_fs_isoc_schedule_alloc
1298 * This function will allocate bandwidth for an isochronous FULL speed
1299 * transaction in the FULL speed schedule. The microframe slot where
1300 * the transaction should be started is stored in the byte pointed to
1301 * by "pstart". The "len" argument specifies the length of the
1302 * transaction in bytes.
1307 *------------------------------------------------------------------------*/
1308 #if USB_HAVE_TT_SUPPORT
1310 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1311 uint8_t *pstart, uint16_t len)
1313 uint8_t slot = fss->frame_slot;
1315 /* Compute overhead and bit-stuffing */
1322 if (len > fss->total_bytes) {
1323 *pstart = 0; /* set some dummy value */
1324 return (1); /* error */
1328 fss->total_bytes -= len;
1330 while (len >= fss->frame_bytes) {
1331 len -= fss->frame_bytes;
1332 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1336 fss->frame_bytes -= len;
1339 return (0); /* success */
1343 /*------------------------------------------------------------------------*
1344 * usb_bus_port_get_device
1346 * This function is NULL safe.
1347 *------------------------------------------------------------------------*/
1349 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1351 if ((bus == NULL) || (up == NULL)) {
1355 if (up->device_index == 0) {
1359 return (bus->devices[up->device_index]);
1362 /*------------------------------------------------------------------------*
1363 * usb_bus_port_set_device
1365 * This function is NULL safe.
1366 *------------------------------------------------------------------------*/
1368 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1369 struct usb_device *udev, uint8_t device_index)
1376 * There is only one case where we don't
1377 * have an USB port, and that is the Root Hub!
1381 up->device_index = device_index;
1383 device_index = up->device_index;
1384 up->device_index = 0;
1388 * Make relationships to our new device
1390 if (device_index != 0) {
1392 mtx_lock(&usb_ref_lock);
1394 bus->devices[device_index] = udev;
1396 mtx_unlock(&usb_ref_lock);
1402 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1405 /*------------------------------------------------------------------------*
1408 * This functions is called when the USB event thread needs to run.
1409 *------------------------------------------------------------------------*/
1411 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1418 DPRINTF("No bus pointer!\n");
1421 if ((bus->devices == NULL) ||
1422 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1423 DPRINTF("No root HUB\n");
1426 if (mtx_owned(&bus->bus_mtx)) {
1435 if (usb_proc_msignal(&bus->explore_proc,
1436 &bus->explore_msg[0], &bus->explore_msg[1])) {
1440 USB_BUS_UNLOCK(bus);
1444 /*------------------------------------------------------------------------*
1445 * usb_needs_explore_all
1447 * This function is called whenever a new driver is loaded and will
1448 * cause that all USB busses are re-explored.
1449 *------------------------------------------------------------------------*/
1451 usb_needs_explore_all(void)
1453 struct usb_bus *bus;
1460 dc = usb_devclass_ptr;
1462 DPRINTFN(0, "no devclass\n");
1466 * Explore all USB busses in parallell.
1468 max = devclass_get_maxunit(dc);
1470 dev = devclass_get_device(dc, max);
1472 bus = device_get_softc(dev);
1474 usb_needs_explore(bus, 1);
1481 /*------------------------------------------------------------------------*
1482 * usb_bus_power_update
1484 * This function will ensure that all USB devices on the given bus are
1485 * properly suspended or resumed according to the device transfer
1487 *------------------------------------------------------------------------*/
1490 usb_bus_power_update(struct usb_bus *bus)
1492 usb_needs_explore(bus, 0 /* no probe */ );
1496 /*------------------------------------------------------------------------*
1497 * usbd_transfer_power_ref
1499 * This function will modify the power save reference counts and
1500 * wakeup the USB device associated with the given USB transfer, if
1502 *------------------------------------------------------------------------*/
1505 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1507 static const usb_power_mask_t power_mask[4] = {
1508 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1509 [UE_BULK] = USB_HW_POWER_BULK,
1510 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1511 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1513 struct usb_device *udev;
1514 uint8_t needs_explore;
1515 uint8_t needs_hw_power;
1518 udev = xfer->xroot->udev;
1520 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1521 /* no power save for root HUB */
1524 USB_BUS_LOCK(udev->bus);
1526 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1528 udev->pwr_save.last_xfer_time = ticks;
1529 udev->pwr_save.type_refs[xfer_type] += val;
1531 if (xfer->flags_int.control_xfr) {
1532 udev->pwr_save.read_refs += val;
1533 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1535 * it is not allowed to suspend during a control
1538 udev->pwr_save.write_refs += val;
1540 } else if (USB_GET_DATA_ISREAD(xfer)) {
1541 udev->pwr_save.read_refs += val;
1543 udev->pwr_save.write_refs += val;
1546 if (udev->flags.self_suspended)
1548 (udev->pwr_save.write_refs != 0) ||
1549 ((udev->pwr_save.read_refs != 0) &&
1550 (usb_peer_can_wakeup(udev) == 0));
1554 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1555 DPRINTF("Adding type %u to power state\n", xfer_type);
1556 udev->bus->hw_power_state |= power_mask[xfer_type];
1562 USB_BUS_UNLOCK(udev->bus);
1564 if (needs_explore) {
1565 DPRINTF("update\n");
1566 usb_bus_power_update(udev->bus);
1567 } else if (needs_hw_power) {
1568 DPRINTF("needs power\n");
1569 if (udev->bus->methods->set_hw_power != NULL) {
1570 (udev->bus->methods->set_hw_power) (udev->bus);
1576 /*------------------------------------------------------------------------*
1579 * This function implements the USB power daemon and is called
1580 * regularly from the USB explore thread.
1581 *------------------------------------------------------------------------*/
1584 usb_bus_powerd(struct usb_bus *bus)
1586 struct usb_device *udev;
1589 usb_ticks_t mintime;
1590 usb_size_t type_refs[5];
1594 limit = usb_power_timeout;
1597 else if (limit > 255)
1602 DPRINTF("bus=%p\n", bus);
1607 * The root HUB device is never suspended
1608 * and we simply skip it.
1610 for (x = USB_ROOT_HUB_ADDR + 1;
1611 x != bus->devices_max; x++) {
1613 udev = bus->devices[x];
1617 rem_wakeup = usb_peer_can_wakeup(udev);
1619 temp = ticks - udev->pwr_save.last_xfer_time;
1621 if ((udev->power_mode == USB_POWER_MODE_ON) ||
1622 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1623 (udev->pwr_save.write_refs != 0) ||
1624 ((udev->pwr_save.read_refs != 0) &&
1625 (rem_wakeup == 0))) {
1627 /* check if we are suspended */
1628 if (udev->flags.self_suspended != 0) {
1629 USB_BUS_UNLOCK(bus);
1630 usb_dev_resume_peer(udev);
1633 } else if (temp >= limit) {
1635 /* check if we are not suspended */
1636 if (udev->flags.self_suspended == 0) {
1637 USB_BUS_UNLOCK(bus);
1638 usb_dev_suspend_peer(udev);
1644 /* reset counters */
1653 /* Re-loop all the devices to get the actual state */
1655 for (x = USB_ROOT_HUB_ADDR + 1;
1656 x != bus->devices_max; x++) {
1658 udev = bus->devices[x];
1662 /* we found a non-Root-Hub USB device */
1665 /* "last_xfer_time" can be updated by a resume */
1666 temp = ticks - udev->pwr_save.last_xfer_time;
1669 * Compute minimum time since last transfer for the complete
1675 if (udev->flags.self_suspended == 0) {
1676 type_refs[0] += udev->pwr_save.type_refs[0];
1677 type_refs[1] += udev->pwr_save.type_refs[1];
1678 type_refs[2] += udev->pwr_save.type_refs[2];
1679 type_refs[3] += udev->pwr_save.type_refs[3];
1683 if (mintime >= (1 * hz)) {
1684 /* recompute power masks */
1685 DPRINTF("Recomputing power masks\n");
1686 bus->hw_power_state = 0;
1687 if (type_refs[UE_CONTROL] != 0)
1688 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1689 if (type_refs[UE_BULK] != 0)
1690 bus->hw_power_state |= USB_HW_POWER_BULK;
1691 if (type_refs[UE_INTERRUPT] != 0)
1692 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1693 if (type_refs[UE_ISOCHRONOUS] != 0)
1694 bus->hw_power_state |= USB_HW_POWER_ISOC;
1695 if (type_refs[4] != 0)
1696 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1698 USB_BUS_UNLOCK(bus);
1700 if (bus->methods->set_hw_power != NULL) {
1701 /* always update hardware power! */
1702 (bus->methods->set_hw_power) (bus);
1708 /*------------------------------------------------------------------------*
1709 * usb_dev_resume_peer
1711 * This function will resume an USB peer and do the required USB
1712 * signalling to get an USB device out of the suspended state.
1713 *------------------------------------------------------------------------*/
1715 usb_dev_resume_peer(struct usb_device *udev)
1717 struct usb_bus *bus;
1724 /* check if already resumed */
1725 if (udev->flags.self_suspended == 0)
1728 /* we need a parent HUB to do resume */
1729 if (udev->parent_hub == NULL)
1732 DPRINTF("udev=%p\n", udev);
1734 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1735 (udev->flags.remote_wakeup == 0)) {
1737 * If the host did not set the remote wakeup feature, we can
1738 * not wake it up either!
1740 DPRINTF("remote wakeup is not set!\n");
1743 /* get bus pointer */
1746 /* resume parent hub first */
1747 usb_dev_resume_peer(udev->parent_hub);
1749 /* resume current port (Valid in Host and Device Mode) */
1750 err = usbd_req_clear_port_feature(udev->parent_hub,
1751 NULL, udev->port_no, UHF_PORT_SUSPEND);
1753 DPRINTFN(0, "Resuming port failed!\n");
1756 /* resume settle time */
1757 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1759 if (bus->methods->device_resume != NULL) {
1760 /* resume USB device on the USB controller */
1761 (bus->methods->device_resume) (udev);
1764 /* set that this device is now resumed */
1765 udev->flags.self_suspended = 0;
1767 /* make sure that we don't go into suspend right away */
1768 udev->pwr_save.last_xfer_time = ticks;
1770 /* make sure the needed power masks are on */
1771 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1772 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1773 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1774 bus->hw_power_state |= USB_HW_POWER_BULK;
1775 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1776 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1777 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1778 bus->hw_power_state |= USB_HW_POWER_ISOC;
1780 USB_BUS_UNLOCK(bus);
1782 if (bus->methods->set_hw_power != NULL) {
1783 /* always update hardware power! */
1784 (bus->methods->set_hw_power) (bus);
1787 usbd_enum_lock(udev);
1789 /* notify all sub-devices about resume */
1790 err = usb_suspend_resume(udev, 0);
1792 usbd_enum_unlock(udev);
1794 /* check if peer has wakeup capability */
1795 if (usb_peer_can_wakeup(udev)) {
1796 /* clear remote wakeup */
1797 err = usbd_req_clear_device_feature(udev,
1798 NULL, UF_DEVICE_REMOTE_WAKEUP);
1800 DPRINTFN(0, "Clearing device "
1801 "remote wakeup failed: %s!\n",
1808 /*------------------------------------------------------------------------*
1809 * usb_dev_suspend_peer
1811 * This function will suspend an USB peer and do the required USB
1812 * signalling to get an USB device into the suspended state.
1813 *------------------------------------------------------------------------*/
1815 usb_dev_suspend_peer(struct usb_device *udev)
1817 struct usb_device *child;
1827 /* check if already suspended */
1828 if (udev->flags.self_suspended)
1831 /* we need a parent HUB to do suspend */
1832 if (udev->parent_hub == NULL)
1835 DPRINTF("udev=%p\n", udev);
1837 /* check if the current device is a HUB */
1838 if (udev->hub != NULL) {
1839 nports = udev->hub->nports;
1841 /* check if all devices on the HUB are suspended */
1842 for (x = 0; x != nports; x++) {
1844 child = usb_bus_port_get_device(udev->bus,
1845 udev->hub->ports + x);
1850 if (child->flags.self_suspended)
1853 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
1858 usbd_enum_lock(udev);
1860 /* notify all sub-devices about suspend */
1861 err = usb_suspend_resume(udev, 1);
1863 usbd_enum_unlock(udev);
1865 if (usb_peer_can_wakeup(udev)) {
1866 /* allow device to do remote wakeup */
1867 err = usbd_req_set_device_feature(udev,
1868 NULL, UF_DEVICE_REMOTE_WAKEUP);
1870 DPRINTFN(0, "Setting device "
1871 "remote wakeup failed!\n");
1874 USB_BUS_LOCK(udev->bus);
1876 * Set that this device is suspended. This variable must be set
1877 * before calling USB controller suspend callbacks.
1879 udev->flags.self_suspended = 1;
1880 USB_BUS_UNLOCK(udev->bus);
1882 if (udev->bus->methods->device_suspend != NULL) {
1885 /* suspend device on the USB controller */
1886 (udev->bus->methods->device_suspend) (udev);
1889 temp = usbd_get_dma_delay(udev->bus);
1890 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
1893 /* suspend current port */
1894 err = usbd_req_set_port_feature(udev->parent_hub,
1895 NULL, udev->port_no, UHF_PORT_SUSPEND);
1897 DPRINTFN(0, "Suspending port failed\n");
1901 udev = udev->parent_hub;
1905 /*------------------------------------------------------------------------*
1906 * usbd_set_power_mode
1908 * This function will set the power mode, see USB_POWER_MODE_XXX for a
1910 *------------------------------------------------------------------------*/
1912 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
1914 /* filter input argument */
1915 if ((power_mode != USB_POWER_MODE_ON) &&
1916 (power_mode != USB_POWER_MODE_OFF)) {
1917 power_mode = USB_POWER_MODE_SAVE;
1919 udev->power_mode = power_mode; /* update copy of power mode */
1922 usb_bus_power_update(udev->bus);