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 /* check if there is a child */
323 * Free USB device and all subdevices, if any.
325 usb_free_device(child, 0);
328 /* get fresh status */
330 err = uhub_read_port_status(sc, portno);
334 /* check if nothing is connected to the port */
336 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
339 /* check if there is no power on the port and print a warning */
341 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
342 DPRINTF("WARNING: strange, connected port %d "
343 "has no power\n", portno);
345 /* check if the device is in Host Mode */
347 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
349 DPRINTF("Port %d is in Host Mode\n", portno);
351 if (sc->sc_st.port_status & UPS_SUSPEND) {
352 DPRINTF("Port %d was still "
353 "suspended, clearing.\n", portno);
354 err = usbd_req_clear_port_feature(sc->sc_udev,
355 NULL, portno, UHF_PORT_SUSPEND);
359 /* wait for maximum device power up time */
362 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
364 /* reset port, which implies enabling it */
366 err = usbd_req_reset_port(udev, NULL, portno);
369 DPRINTFN(0, "port %d reset "
370 "failed, error=%s\n",
371 portno, usbd_errstr(err));
374 /* get port status again, it might have changed during reset */
376 err = uhub_read_port_status(sc, portno);
380 /* check if something changed during port reset */
382 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
383 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
385 DPRINTFN(0, "giving up port reset "
386 "- device vanished!\n");
393 DPRINTF("Port %d is in Device Mode\n", portno);
397 * Figure out the device speed
399 switch (udev->speed) {
401 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
402 speed = USB_SPEED_HIGH;
403 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
404 speed = USB_SPEED_LOW;
406 speed = USB_SPEED_FULL;
409 if (sc->sc_st.port_status & UPS_LOW_SPEED)
410 speed = USB_SPEED_LOW;
412 speed = USB_SPEED_FULL;
415 speed = USB_SPEED_LOW;
418 /* same speed like parent */
423 * Figure out the device mode
425 * NOTE: This part is currently FreeBSD specific.
427 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
428 mode = USB_MODE_DEVICE;
430 mode = USB_MODE_HOST;
432 /* need to create a new child */
433 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
434 udev->depth + 1, portno - 1, portno, speed, mode);
436 DPRINTFN(0, "could not allocate new device!\n");
439 return (0); /* success */
444 * Free USB device and all subdevices, if any.
446 usb_free_device(child, 0);
450 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
451 err = usbd_req_clear_port_feature(
453 portno, UHF_PORT_ENABLE);
457 DPRINTFN(0, "device problem (%s), "
458 "disabling port %d\n", usbd_errstr(err), portno);
463 /*------------------------------------------------------------------------*
464 * uhub_suspend_resume_port
468 * Else: A control transaction failed
469 *------------------------------------------------------------------------*/
471 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
473 struct usb_device *child;
474 struct usb_device *udev;
478 DPRINTF("port %d\n", portno);
481 child = usb_bus_port_get_device(udev->bus,
482 udev->hub->ports + portno - 1);
484 /* first clear the port suspend change bit */
486 err = usbd_req_clear_port_feature(udev, NULL,
487 portno, UHF_C_PORT_SUSPEND);
489 DPRINTF("clearing suspend failed.\n");
492 /* get fresh status */
494 err = uhub_read_port_status(sc, portno);
496 DPRINTF("reading port status failed.\n");
499 /* get current state */
501 if (sc->sc_st.port_status & UPS_SUSPEND) {
507 DPRINTF("suspended=%u\n", is_suspend);
509 /* do the suspend or resume */
513 * This code handle two cases: 1) Host Mode - we can only
514 * receive resume here 2) Device Mode - we can receive
515 * suspend and resume here
518 usb_dev_resume_peer(child);
519 else if (child->flags.usb_mode == USB_MODE_DEVICE)
520 usb_dev_suspend_peer(child);
526 /*------------------------------------------------------------------------*
527 * uhub_root_interrupt
529 * This function is called when a Root HUB interrupt has
530 * happened. "ptr" and "len" makes up the Root HUB interrupt
531 * packet. This function is called having the "bus_mtx" locked.
532 *------------------------------------------------------------------------*/
534 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
536 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
538 usb_needs_explore(bus, 0);
541 /*------------------------------------------------------------------------*
547 *------------------------------------------------------------------------*/
549 uhub_explore(struct usb_device *udev)
552 struct uhub_softc *sc;
561 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
563 /* ignore hubs that are too deep */
564 if (udev->depth > USB_HUB_MAX_DEPTH) {
565 return (USB_ERR_TOO_DEEP);
568 if (udev->flags.self_suspended) {
569 /* need to wait until the child signals resume */
570 DPRINTF("Device is suspended!\n");
573 for (x = 0; x != hub->nports; x++) {
577 err = uhub_read_port_status(sc, portno);
579 /* most likely the HUB is gone */
582 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
583 DPRINTF("Overcurrent on port %u.\n", portno);
584 err = usbd_req_clear_port_feature(
585 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
587 /* most likely the HUB is gone */
591 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
593 * Fake a connect status change so that the
594 * status gets checked initially!
596 sc->sc_st.port_change |=
597 UPS_C_CONNECT_STATUS;
599 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
600 err = usbd_req_clear_port_feature(
601 udev, NULL, portno, UHF_C_PORT_ENABLE);
603 /* most likely the HUB is gone */
606 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
608 * Ignore the port error if the device
611 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
612 DPRINTFN(0, "illegal enable change, "
613 "port %d\n", portno);
616 if (up->restartcnt == USB_RESTART_MAX) {
617 /* XXX could try another speed ? */
618 DPRINTFN(0, "port error, giving up "
619 "port %d\n", portno);
621 sc->sc_st.port_change |=
622 UPS_C_CONNECT_STATUS;
627 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
628 err = uhub_reattach_port(sc, portno);
630 /* most likely the HUB is gone */
634 if (sc->sc_st.port_change & UPS_C_SUSPEND) {
635 err = uhub_suspend_resume_port(sc, portno);
637 /* most likely the HUB is gone */
641 err = uhub_explore_sub(sc, up);
643 /* no device(s) present */
646 /* explore succeeded - reset restart counter */
650 /* initial status checked */
651 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
654 return (USB_ERR_NORMAL_COMPLETION);
658 uhub_probe(device_t dev)
660 struct usb_attach_arg *uaa = device_get_ivars(dev);
662 if (uaa->usb_mode != USB_MODE_HOST) {
666 * The subclass for USB HUBs is ignored because it is 0 for
667 * some and 1 for others.
669 if ((uaa->info.bConfigIndex == 0) &&
670 (uaa->info.bDeviceClass == UDCLASS_HUB)) {
677 uhub_attach(device_t dev)
679 struct uhub_softc *sc = device_get_softc(dev);
680 struct usb_attach_arg *uaa = device_get_ivars(dev);
681 struct usb_device *udev = uaa->device;
682 struct usb_device *parent_hub = udev->parent_hub;
684 struct usb_hub_descriptor hubdesc;
696 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
698 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
699 device_get_nameunit(dev));
701 device_set_usb_desc(dev);
703 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
704 "parent->selfpowered=%d\n",
706 udev->flags.self_powered,
709 parent_hub->flags.self_powered : 0);
711 if (udev->depth > USB_HUB_MAX_DEPTH) {
712 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored!\n",
716 if (!udev->flags.self_powered && parent_hub &&
717 (!parent_hub->flags.self_powered)) {
718 DPRINTFN(0, "bus powered HUB connected to "
719 "bus powered HUB. HUB ignored!\n");
722 /* get HUB descriptor */
724 DPRINTFN(2, "getting HUB descriptor\n");
726 /* assuming that there is one port */
727 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, 1);
729 nports = hubdesc.bNbrPorts;
731 if (!err && (nports >= 8)) {
732 /* get complete HUB descriptor */
733 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, nports);
736 DPRINTFN(0, "getting hub descriptor failed,"
737 "error=%s\n", usbd_errstr(err));
740 if (hubdesc.bNbrPorts != nports) {
741 DPRINTFN(0, "number of ports changed!\n");
745 DPRINTFN(0, "portless HUB!\n");
748 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
749 M_USBDEV, M_WAITOK | M_ZERO);
756 #if USB_HAVE_TT_SUPPORT
757 /* init FULL-speed ISOCHRONOUS schedule */
758 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
760 /* initialize HUB structure */
762 hub->explore = &uhub_explore;
763 hub->nports = hubdesc.bNbrPorts;
766 /* if self powered hub, give ports maximum current */
767 if (udev->flags.self_powered) {
768 hub->portpower = USB_MAX_POWER;
770 hub->portpower = USB_MIN_POWER;
773 /* set up interrupt pipe */
775 if (udev->parent_hub == NULL) {
776 /* root HUB is special */
780 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
781 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
784 DPRINTFN(0, "cannot setup interrupt transfer, "
785 "errstr=%s!\n", usbd_errstr(err));
788 /* wait with power off for a while */
789 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
792 * To have the best chance of success we do things in the exact same
793 * order as Windoze98. This should not be necessary, but some
794 * devices do not follow the USB specs to the letter.
796 * These are the events on the bus when a hub is attached:
797 * Get device and config descriptors (see attach code)
798 * Get hub descriptor (see above)
801 * wait for power to become stable
802 * (all below happens in explore code)
804 * clear C_PORT_CONNECTION
807 * if device connected
813 * proceed with device attachment
816 /* XXX should check for none, individual, or ganged power? */
819 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
820 USB_EXTRA_POWER_UP_TIME);
822 for (x = 0; x != nports; x++) {
823 /* set up data structures */
824 struct usb_port *up = hub->ports + x;
826 up->device_index = 0;
830 /* check if port is removable */
831 if (!UHD_NOT_REMOV(&hubdesc, portno)) {
835 /* turn the power on */
836 err = usbd_req_set_port_feature(udev, NULL,
837 portno, UHF_PORT_POWER);
840 DPRINTFN(0, "port %d power on failed, %s\n",
841 portno, usbd_errstr(err));
843 DPRINTF("turn on port %d power\n",
846 /* wait for stable power */
847 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
850 device_printf(dev, "%d port%s with %d "
851 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
852 removable, udev->flags.self_powered ? "self" : "bus");
854 /* Start the interrupt endpoint, if any */
856 if (sc->sc_xfer[0] != NULL) {
857 mtx_lock(&sc->sc_mtx);
858 usbd_transfer_start(sc->sc_xfer[0]);
859 mtx_unlock(&sc->sc_mtx);
862 /* Enable automatic power save on all USB HUBs */
864 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
869 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
872 free(udev->hub, M_USBDEV);
876 mtx_destroy(&sc->sc_mtx);
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 if (hub == NULL) { /* must be partially working */
897 /* Make sure interrupt transfer is gone. */
898 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
900 /* Detach all ports */
901 for (x = 0; x != hub->nports; x++) {
903 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
910 * Free USB device and all subdevices, if any.
912 usb_free_device(child, 0);
916 sc->sc_udev->hub = NULL;
918 mtx_destroy(&sc->sc_mtx);
924 uhub_suspend(device_t dev)
927 /* Sub-devices are not suspended here! */
932 uhub_resume(device_t dev)
935 /* Sub-devices are not resumed here! */
940 uhub_driver_added(device_t dev, driver_t *driver)
942 usb_needs_explore_all();
946 struct usb_device *udev;
952 uhub_find_iface_index(struct usb_hub *hub, device_t child,
953 struct hub_result *res)
955 struct usb_interface *iface;
956 struct usb_device *udev;
961 nports = hub->nports;
962 for (x = 0; x != nports; x++) {
963 udev = usb_bus_port_get_device(hub->hubudev->bus,
968 for (i = 0; i != USB_IFACE_MAX; i++) {
969 iface = usbd_get_iface(udev, i);
971 (iface->subdev == child)) {
972 res->iface_index = i;
979 res->iface_index = 0;
985 uhub_child_location_string(device_t parent, device_t child,
986 char *buf, size_t buflen)
988 struct uhub_softc *sc;
990 struct hub_result res;
992 if (!device_is_attached(parent)) {
998 sc = device_get_softc(parent);
999 hub = sc->sc_udev->hub;
1002 uhub_find_iface_index(hub, child, &res);
1004 DPRINTF("device not on hub\n");
1010 snprintf(buf, buflen, "port=%u interface=%u",
1011 res.portno, res.iface_index);
1019 uhub_child_pnpinfo_string(device_t parent, device_t child,
1020 char *buf, size_t buflen)
1022 struct uhub_softc *sc;
1023 struct usb_hub *hub;
1024 struct usb_interface *iface;
1025 struct hub_result res;
1027 if (!device_is_attached(parent)) {
1033 sc = device_get_softc(parent);
1034 hub = sc->sc_udev->hub;
1037 uhub_find_iface_index(hub, child, &res);
1039 DPRINTF("device not on hub\n");
1045 iface = usbd_get_iface(res.udev, res.iface_index);
1046 if (iface && iface->idesc) {
1047 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1048 "devclass=0x%02x devsubclass=0x%02x "
1051 "intclass=0x%02x intsubclass=0x%02x",
1052 UGETW(res.udev->ddesc.idVendor),
1053 UGETW(res.udev->ddesc.idProduct),
1054 res.udev->ddesc.bDeviceClass,
1055 res.udev->ddesc.bDeviceSubClass,
1057 UGETW(res.udev->ddesc.bcdDevice),
1058 iface->idesc->bInterfaceClass,
1059 iface->idesc->bInterfaceSubClass);
1073 * The USB Transaction Translator:
1074 * ===============================
1076 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1077 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1078 * USB transfers. To utilize bandwidth dynamically the "scatter and
1079 * gather" principle must be applied. This means that bandwidth must
1080 * be divided into equal parts of bandwidth. With regard to USB all
1081 * data is transferred in smaller packets with length
1082 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1085 * The bandwidth scheduler which I have implemented will simply pack
1086 * the USB transfers back to back until there is no more space in the
1087 * schedule. Out of the 8 microframes which the USB 2.0 standard
1088 * provides, only 6 are available for non-HIGH-speed devices. I have
1089 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1090 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1091 * this division, it is very difficult to allocate and free bandwidth
1094 * NOTE about the Transaction Translator in USB HUBs:
1096 * USB HUBs have a very simple Transaction Translator, that will
1097 * simply pipeline all the SPLIT transactions. That means that the
1098 * transactions will be executed in the order they are queued!
1102 /*------------------------------------------------------------------------*
1103 * usb_intr_find_best_slot
1106 * The best Transaction Translation slot for an interrupt endpoint.
1107 *------------------------------------------------------------------------*/
1109 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1110 uint8_t end, uint8_t mask)
1112 usb_size_t min = 0 - 1;
1120 /* find the last slot with lesser used bandwidth */
1122 for (x = start; x < end; x++) {
1126 /* compute sum of bandwidth */
1127 for (z = x; z < end; z++) {
1128 if (mask & (1U << (z - x)))
1132 /* check if the current multi-slot is more optimal */
1138 /* check if the mask is about to be shifted out */
1139 if (mask & (1U << (end - 1 - x)))
1145 /*------------------------------------------------------------------------*
1146 * usb_hs_bandwidth_adjust
1148 * This function will update the bandwith usage for the microframe
1149 * having index "slot" by "len" bytes. "len" can be negative. If the
1150 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1151 * the "slot" argument will be replaced by the slot having least used
1152 * bandwidth. The "mask" argument is used for multi-slot allocations.
1155 * The slot in which the bandwidth update was done: 0..7
1156 *------------------------------------------------------------------------*/
1158 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1159 uint8_t slot, uint8_t mask)
1161 struct usb_bus *bus = udev->bus;
1162 struct usb_hub *hub;
1163 enum usb_dev_speed speed;
1166 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1168 speed = usbd_get_speed(udev);
1172 case USB_SPEED_FULL:
1173 if (speed == USB_SPEED_LOW) {
1177 * The Host Controller Driver should have
1178 * performed checks so that the lookup
1179 * below does not result in a NULL pointer
1183 hub = udev->parent_hs_hub->hub;
1184 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1185 slot = usb_intr_find_best_slot(hub->uframe_usage,
1186 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1188 for (x = slot; x < 8; x++) {
1189 if (mask & (1U << (x - slot))) {
1190 hub->uframe_usage[x] += len;
1191 bus->uframe_usage[x] += len;
1196 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1197 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1198 USB_HS_MICRO_FRAMES_MAX, mask);
1200 for (x = slot; x < 8; x++) {
1201 if (mask & (1U << (x - slot))) {
1202 bus->uframe_usage[x] += len;
1210 /*------------------------------------------------------------------------*
1211 * usb_hs_bandwidth_alloc
1213 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1214 *------------------------------------------------------------------------*/
1216 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1218 struct usb_device *udev;
1223 udev = xfer->xroot->udev;
1225 if (udev->flags.usb_mode != USB_MODE_HOST)
1226 return; /* not supported */
1228 xfer->endpoint->refcount_bw++;
1229 if (xfer->endpoint->refcount_bw != 1)
1230 return; /* already allocated */
1232 speed = usbd_get_speed(udev);
1234 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1236 /* allocate a microframe slot */
1239 slot = usb_hs_bandwidth_adjust(udev,
1240 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1242 xfer->endpoint->usb_uframe = slot;
1243 xfer->endpoint->usb_smask = mask << slot;
1245 if ((speed != USB_SPEED_FULL) &&
1246 (speed != USB_SPEED_LOW)) {
1247 xfer->endpoint->usb_cmask = 0x00 ;
1249 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1253 case UE_ISOCHRONOUS:
1254 switch (usbd_xfer_get_fps_shift(xfer)) {
1269 /* allocate a microframe multi-slot */
1271 slot = usb_hs_bandwidth_adjust(udev,
1272 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1274 xfer->endpoint->usb_uframe = slot;
1275 xfer->endpoint->usb_cmask = 0;
1276 xfer->endpoint->usb_smask = mask << slot;
1280 xfer->endpoint->usb_uframe = 0;
1281 xfer->endpoint->usb_cmask = 0;
1282 xfer->endpoint->usb_smask = 0;
1286 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1287 xfer->endpoint->usb_uframe,
1288 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1291 /*------------------------------------------------------------------------*
1292 * usb_hs_bandwidth_free
1294 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1295 *------------------------------------------------------------------------*/
1297 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1299 struct usb_device *udev;
1303 udev = xfer->xroot->udev;
1305 if (udev->flags.usb_mode != USB_MODE_HOST)
1306 return; /* not supported */
1308 xfer->endpoint->refcount_bw--;
1309 if (xfer->endpoint->refcount_bw != 0)
1310 return; /* still allocated */
1312 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1314 case UE_ISOCHRONOUS:
1316 slot = xfer->endpoint->usb_uframe;
1317 mask = xfer->endpoint->usb_smask;
1319 /* free microframe slot(s): */
1320 usb_hs_bandwidth_adjust(udev,
1321 -xfer->max_frame_size, slot, mask >> slot);
1323 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1324 slot, mask >> slot);
1326 xfer->endpoint->usb_uframe = 0;
1327 xfer->endpoint->usb_cmask = 0;
1328 xfer->endpoint->usb_smask = 0;
1336 /*------------------------------------------------------------------------*
1337 * usbd_fs_isoc_schedule_init_sub
1339 * This function initialises an USB FULL speed isochronous schedule
1341 *------------------------------------------------------------------------*/
1342 #if USB_HAVE_TT_SUPPORT
1344 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1346 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1347 USB_FS_BYTES_PER_HS_UFRAME);
1348 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1349 fss->frame_slot = 0;
1353 /*------------------------------------------------------------------------*
1354 * usbd_fs_isoc_schedule_init_all
1356 * This function will reset the complete USB FULL speed isochronous
1357 * bandwidth schedule.
1358 *------------------------------------------------------------------------*/
1359 #if USB_HAVE_TT_SUPPORT
1361 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1363 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1365 while (fss != fss_end) {
1366 usbd_fs_isoc_schedule_init_sub(fss);
1372 /*------------------------------------------------------------------------*
1373 * usb_isoc_time_expand
1375 * This function will expand the time counter from 7-bit to 16-bit.
1378 * 16-bit isochronous time counter.
1379 *------------------------------------------------------------------------*/
1381 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1385 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1387 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1389 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1391 if (isoc_time_curr < rem) {
1392 /* the time counter wrapped around */
1393 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1395 /* update the remainder */
1397 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1398 bus->isoc_time_last |= isoc_time_curr;
1400 return (bus->isoc_time_last);
1403 /*------------------------------------------------------------------------*
1404 * usbd_fs_isoc_schedule_isoc_time_expand
1406 * This function does multiple things. First of all it will expand the
1407 * passed isochronous time, which is the return value. Then it will
1408 * store where the current FULL speed isochronous schedule is
1409 * positioned in time and where the end is. See "pp_start" and
1410 * "pp_end" arguments.
1413 * Expanded version of "isoc_time".
1415 * NOTE: This function depends on being called regularly with
1416 * intervals less than "USB_ISOC_TIME_MAX".
1417 *------------------------------------------------------------------------*/
1418 #if USB_HAVE_TT_SUPPORT
1420 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1421 struct usb_fs_isoc_schedule **pp_start,
1422 struct usb_fs_isoc_schedule **pp_end,
1425 struct usb_fs_isoc_schedule *fss_end;
1426 struct usb_fs_isoc_schedule *fss_a;
1427 struct usb_fs_isoc_schedule *fss_b;
1428 struct usb_hub *hs_hub;
1430 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1432 hs_hub = udev->parent_hs_hub->hub;
1434 if (hs_hub != NULL) {
1436 fss_a = hs_hub->fs_isoc_schedule +
1437 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1439 hs_hub->isoc_last_time = isoc_time;
1441 fss_b = hs_hub->fs_isoc_schedule +
1442 (isoc_time % USB_ISOC_TIME_MAX);
1444 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1446 *pp_start = hs_hub->fs_isoc_schedule;
1449 while (fss_a != fss_b) {
1450 if (fss_a == fss_end) {
1451 fss_a = hs_hub->fs_isoc_schedule;
1454 usbd_fs_isoc_schedule_init_sub(fss_a);
1467 /*------------------------------------------------------------------------*
1468 * usbd_fs_isoc_schedule_alloc
1470 * This function will allocate bandwidth for an isochronous FULL speed
1471 * transaction in the FULL speed schedule. The microframe slot where
1472 * the transaction should be started is stored in the byte pointed to
1473 * by "pstart". The "len" argument specifies the length of the
1474 * transaction in bytes.
1479 *------------------------------------------------------------------------*/
1480 #if USB_HAVE_TT_SUPPORT
1482 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1483 uint8_t *pstart, uint16_t len)
1485 uint8_t slot = fss->frame_slot;
1487 /* Compute overhead and bit-stuffing */
1494 if (len > fss->total_bytes) {
1495 *pstart = 0; /* set some dummy value */
1496 return (1); /* error */
1500 fss->total_bytes -= len;
1502 while (len >= fss->frame_bytes) {
1503 len -= fss->frame_bytes;
1504 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1508 fss->frame_bytes -= len;
1511 return (0); /* success */
1515 /*------------------------------------------------------------------------*
1516 * usb_bus_port_get_device
1518 * This function is NULL safe.
1519 *------------------------------------------------------------------------*/
1521 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1523 if ((bus == NULL) || (up == NULL)) {
1527 if (up->device_index == 0) {
1531 return (bus->devices[up->device_index]);
1534 /*------------------------------------------------------------------------*
1535 * usb_bus_port_set_device
1537 * This function is NULL safe.
1538 *------------------------------------------------------------------------*/
1540 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1541 struct usb_device *udev, uint8_t device_index)
1548 * There is only one case where we don't
1549 * have an USB port, and that is the Root Hub!
1553 up->device_index = device_index;
1555 device_index = up->device_index;
1556 up->device_index = 0;
1560 * Make relationships to our new device
1562 if (device_index != 0) {
1564 mtx_lock(&usb_ref_lock);
1566 bus->devices[device_index] = udev;
1568 mtx_unlock(&usb_ref_lock);
1574 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1577 /*------------------------------------------------------------------------*
1580 * This functions is called when the USB event thread needs to run.
1581 *------------------------------------------------------------------------*/
1583 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1590 DPRINTF("No bus pointer!\n");
1593 if ((bus->devices == NULL) ||
1594 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1595 DPRINTF("No root HUB\n");
1598 if (mtx_owned(&bus->bus_mtx)) {
1607 if (usb_proc_msignal(&bus->explore_proc,
1608 &bus->explore_msg[0], &bus->explore_msg[1])) {
1612 USB_BUS_UNLOCK(bus);
1616 /*------------------------------------------------------------------------*
1617 * usb_needs_explore_all
1619 * This function is called whenever a new driver is loaded and will
1620 * cause that all USB busses are re-explored.
1621 *------------------------------------------------------------------------*/
1623 usb_needs_explore_all(void)
1625 struct usb_bus *bus;
1632 dc = usb_devclass_ptr;
1634 DPRINTFN(0, "no devclass\n");
1638 * Explore all USB busses in parallell.
1640 max = devclass_get_maxunit(dc);
1642 dev = devclass_get_device(dc, max);
1644 bus = device_get_softc(dev);
1646 usb_needs_explore(bus, 1);
1653 /*------------------------------------------------------------------------*
1654 * usb_bus_power_update
1656 * This function will ensure that all USB devices on the given bus are
1657 * properly suspended or resumed according to the device transfer
1659 *------------------------------------------------------------------------*/
1662 usb_bus_power_update(struct usb_bus *bus)
1664 usb_needs_explore(bus, 0 /* no probe */ );
1668 /*------------------------------------------------------------------------*
1669 * usbd_transfer_power_ref
1671 * This function will modify the power save reference counts and
1672 * wakeup the USB device associated with the given USB transfer, if
1674 *------------------------------------------------------------------------*/
1677 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1679 static const usb_power_mask_t power_mask[4] = {
1680 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1681 [UE_BULK] = USB_HW_POWER_BULK,
1682 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1683 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1685 struct usb_device *udev;
1686 uint8_t needs_explore;
1687 uint8_t needs_hw_power;
1690 udev = xfer->xroot->udev;
1692 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1693 /* no power save for root HUB */
1696 USB_BUS_LOCK(udev->bus);
1698 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1700 udev->pwr_save.last_xfer_time = ticks;
1701 udev->pwr_save.type_refs[xfer_type] += val;
1703 if (xfer->flags_int.control_xfr) {
1704 udev->pwr_save.read_refs += val;
1705 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1707 * it is not allowed to suspend during a control
1710 udev->pwr_save.write_refs += val;
1712 } else if (USB_GET_DATA_ISREAD(xfer)) {
1713 udev->pwr_save.read_refs += val;
1715 udev->pwr_save.write_refs += val;
1718 if (udev->flags.self_suspended)
1720 (udev->pwr_save.write_refs != 0) ||
1721 ((udev->pwr_save.read_refs != 0) &&
1722 (usb_peer_can_wakeup(udev) == 0));
1726 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1727 DPRINTF("Adding type %u to power state\n", xfer_type);
1728 udev->bus->hw_power_state |= power_mask[xfer_type];
1734 USB_BUS_UNLOCK(udev->bus);
1736 if (needs_explore) {
1737 DPRINTF("update\n");
1738 usb_bus_power_update(udev->bus);
1739 } else if (needs_hw_power) {
1740 DPRINTF("needs power\n");
1741 if (udev->bus->methods->set_hw_power != NULL) {
1742 (udev->bus->methods->set_hw_power) (udev->bus);
1748 /*------------------------------------------------------------------------*
1751 * This function implements the USB power daemon and is called
1752 * regularly from the USB explore thread.
1753 *------------------------------------------------------------------------*/
1756 usb_bus_powerd(struct usb_bus *bus)
1758 struct usb_device *udev;
1761 usb_ticks_t mintime;
1762 usb_size_t type_refs[5];
1766 limit = usb_power_timeout;
1769 else if (limit > 255)
1774 DPRINTF("bus=%p\n", bus);
1779 * The root HUB device is never suspended
1780 * and we simply skip it.
1782 for (x = USB_ROOT_HUB_ADDR + 1;
1783 x != bus->devices_max; x++) {
1785 udev = bus->devices[x];
1789 rem_wakeup = usb_peer_can_wakeup(udev);
1791 temp = ticks - udev->pwr_save.last_xfer_time;
1793 if ((udev->power_mode == USB_POWER_MODE_ON) ||
1794 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1795 (udev->pwr_save.write_refs != 0) ||
1796 ((udev->pwr_save.read_refs != 0) &&
1797 (rem_wakeup == 0))) {
1799 /* check if we are suspended */
1800 if (udev->flags.self_suspended != 0) {
1801 USB_BUS_UNLOCK(bus);
1802 usb_dev_resume_peer(udev);
1805 } else if (temp >= limit) {
1807 /* check if we are not suspended */
1808 if (udev->flags.self_suspended == 0) {
1809 USB_BUS_UNLOCK(bus);
1810 usb_dev_suspend_peer(udev);
1816 /* reset counters */
1825 /* Re-loop all the devices to get the actual state */
1827 for (x = USB_ROOT_HUB_ADDR + 1;
1828 x != bus->devices_max; x++) {
1830 udev = bus->devices[x];
1834 /* we found a non-Root-Hub USB device */
1837 /* "last_xfer_time" can be updated by a resume */
1838 temp = ticks - udev->pwr_save.last_xfer_time;
1841 * Compute minimum time since last transfer for the complete
1847 if (udev->flags.self_suspended == 0) {
1848 type_refs[0] += udev->pwr_save.type_refs[0];
1849 type_refs[1] += udev->pwr_save.type_refs[1];
1850 type_refs[2] += udev->pwr_save.type_refs[2];
1851 type_refs[3] += udev->pwr_save.type_refs[3];
1855 if (mintime >= (1 * hz)) {
1856 /* recompute power masks */
1857 DPRINTF("Recomputing power masks\n");
1858 bus->hw_power_state = 0;
1859 if (type_refs[UE_CONTROL] != 0)
1860 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1861 if (type_refs[UE_BULK] != 0)
1862 bus->hw_power_state |= USB_HW_POWER_BULK;
1863 if (type_refs[UE_INTERRUPT] != 0)
1864 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1865 if (type_refs[UE_ISOCHRONOUS] != 0)
1866 bus->hw_power_state |= USB_HW_POWER_ISOC;
1867 if (type_refs[4] != 0)
1868 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1870 USB_BUS_UNLOCK(bus);
1872 if (bus->methods->set_hw_power != NULL) {
1873 /* always update hardware power! */
1874 (bus->methods->set_hw_power) (bus);
1880 /*------------------------------------------------------------------------*
1881 * usb_dev_resume_peer
1883 * This function will resume an USB peer and do the required USB
1884 * signalling to get an USB device out of the suspended state.
1885 *------------------------------------------------------------------------*/
1887 usb_dev_resume_peer(struct usb_device *udev)
1889 struct usb_bus *bus;
1896 /* check if already resumed */
1897 if (udev->flags.self_suspended == 0)
1900 /* we need a parent HUB to do resume */
1901 if (udev->parent_hub == NULL)
1904 DPRINTF("udev=%p\n", udev);
1906 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1907 (udev->flags.remote_wakeup == 0)) {
1909 * If the host did not set the remote wakeup feature, we can
1910 * not wake it up either!
1912 DPRINTF("remote wakeup is not set!\n");
1915 /* get bus pointer */
1918 /* resume parent hub first */
1919 usb_dev_resume_peer(udev->parent_hub);
1921 /* resume current port (Valid in Host and Device Mode) */
1922 err = usbd_req_clear_port_feature(udev->parent_hub,
1923 NULL, udev->port_no, UHF_PORT_SUSPEND);
1925 DPRINTFN(0, "Resuming port failed!\n");
1928 /* resume settle time */
1929 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1931 if (bus->methods->device_resume != NULL) {
1932 /* resume USB device on the USB controller */
1933 (bus->methods->device_resume) (udev);
1936 /* set that this device is now resumed */
1937 udev->flags.self_suspended = 0;
1939 /* make sure that we don't go into suspend right away */
1940 udev->pwr_save.last_xfer_time = ticks;
1942 /* make sure the needed power masks are on */
1943 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1944 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1945 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1946 bus->hw_power_state |= USB_HW_POWER_BULK;
1947 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1948 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1949 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1950 bus->hw_power_state |= USB_HW_POWER_ISOC;
1952 USB_BUS_UNLOCK(bus);
1954 if (bus->methods->set_hw_power != NULL) {
1955 /* always update hardware power! */
1956 (bus->methods->set_hw_power) (bus);
1959 usbd_enum_lock(udev);
1961 /* notify all sub-devices about resume */
1962 err = usb_suspend_resume(udev, 0);
1964 usbd_enum_unlock(udev);
1966 /* check if peer has wakeup capability */
1967 if (usb_peer_can_wakeup(udev)) {
1968 /* clear remote wakeup */
1969 err = usbd_req_clear_device_feature(udev,
1970 NULL, UF_DEVICE_REMOTE_WAKEUP);
1972 DPRINTFN(0, "Clearing device "
1973 "remote wakeup failed: %s!\n",
1980 /*------------------------------------------------------------------------*
1981 * usb_dev_suspend_peer
1983 * This function will suspend an USB peer and do the required USB
1984 * signalling to get an USB device into the suspended state.
1985 *------------------------------------------------------------------------*/
1987 usb_dev_suspend_peer(struct usb_device *udev)
1989 struct usb_device *child;
1999 /* check if already suspended */
2000 if (udev->flags.self_suspended)
2003 /* we need a parent HUB to do suspend */
2004 if (udev->parent_hub == NULL)
2007 DPRINTF("udev=%p\n", udev);
2009 /* check if the current device is a HUB */
2010 if (udev->hub != NULL) {
2011 nports = udev->hub->nports;
2013 /* check if all devices on the HUB are suspended */
2014 for (x = 0; x != nports; x++) {
2016 child = usb_bus_port_get_device(udev->bus,
2017 udev->hub->ports + x);
2022 if (child->flags.self_suspended)
2025 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2030 usbd_enum_lock(udev);
2032 /* notify all sub-devices about suspend */
2033 err = usb_suspend_resume(udev, 1);
2035 usbd_enum_unlock(udev);
2037 if (usb_peer_can_wakeup(udev)) {
2038 /* allow device to do remote wakeup */
2039 err = usbd_req_set_device_feature(udev,
2040 NULL, UF_DEVICE_REMOTE_WAKEUP);
2042 DPRINTFN(0, "Setting device "
2043 "remote wakeup failed!\n");
2046 USB_BUS_LOCK(udev->bus);
2048 * Set that this device is suspended. This variable must be set
2049 * before calling USB controller suspend callbacks.
2051 udev->flags.self_suspended = 1;
2052 USB_BUS_UNLOCK(udev->bus);
2054 if (udev->bus->methods->device_suspend != NULL) {
2057 /* suspend device on the USB controller */
2058 (udev->bus->methods->device_suspend) (udev);
2061 temp = usbd_get_dma_delay(udev->bus);
2062 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2065 /* suspend current port */
2066 err = usbd_req_set_port_feature(udev->parent_hub,
2067 NULL, udev->port_no, UHF_PORT_SUSPEND);
2069 DPRINTFN(0, "Suspending port failed\n");
2073 udev = udev->parent_hub;
2077 /*------------------------------------------------------------------------*
2078 * usbd_set_power_mode
2080 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2082 *------------------------------------------------------------------------*/
2084 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2086 /* filter input argument */
2087 if ((power_mode != USB_POWER_MODE_ON) &&
2088 (power_mode != USB_POWER_MODE_OFF)) {
2089 power_mode = USB_POWER_MODE_SAVE;
2091 udev->power_mode = power_mode; /* update copy of power mode */
2094 usb_bus_power_update(udev->bus);