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>
56 #include <dev/usb/usbdi_util.h>
58 #define USB_DEBUG_VAR uhub_debug
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_request.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_hub.h>
66 #include <dev/usb/usb_util.h>
67 #include <dev/usb/usb_busdma.h>
68 #include <dev/usb/usb_transfer.h>
69 #include <dev/usb/usb_dynamic.h>
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
74 #define UHUB_INTR_INTERVAL 250 /* ms */
75 #define UHUB_N_TRANSFER 1
78 static int uhub_debug = 0;
80 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
81 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0,
84 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
88 static int usb_power_timeout = 30; /* seconds */
90 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
91 &usb_power_timeout, 0, "USB power timeout");
94 struct uhub_current_state {
100 struct uhub_current_state sc_st;/* current state */
101 device_t sc_dev; /* base device */
102 struct mtx sc_mtx; /* our mutex */
103 struct usb_device *sc_udev; /* USB device */
104 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
106 #define UHUB_FLAG_DID_EXPLORE 0x01
110 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
111 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
112 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
114 /* prototypes for type checking: */
116 static device_probe_t uhub_probe;
117 static device_attach_t uhub_attach;
118 static device_detach_t uhub_detach;
119 static device_suspend_t uhub_suspend;
120 static device_resume_t uhub_resume;
122 static bus_driver_added_t uhub_driver_added;
123 static bus_child_location_str_t uhub_child_location_string;
124 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
126 static usb_callback_t uhub_intr_callback;
128 static void usb_dev_resume_peer(struct usb_device *udev);
129 static void usb_dev_suspend_peer(struct usb_device *udev);
130 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
132 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
135 .type = UE_INTERRUPT,
136 .endpoint = UE_ADDR_ANY,
137 .direction = UE_DIR_ANY,
139 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
140 .bufsize = 0, /* use wMaxPacketSize */
141 .callback = &uhub_intr_callback,
142 .interval = UHUB_INTR_INTERVAL,
147 * driver instance for "hub" connected to "usb"
148 * and "hub" connected to "hub"
150 static devclass_t uhub_devclass;
152 static device_method_t uhub_methods[] = {
153 DEVMETHOD(device_probe, uhub_probe),
154 DEVMETHOD(device_attach, uhub_attach),
155 DEVMETHOD(device_detach, uhub_detach),
157 DEVMETHOD(device_suspend, uhub_suspend),
158 DEVMETHOD(device_resume, uhub_resume),
160 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
161 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
162 DEVMETHOD(bus_driver_added, uhub_driver_added),
166 static driver_t uhub_driver = {
168 .methods = uhub_methods,
169 .size = sizeof(struct uhub_softc)
172 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
173 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
174 MODULE_VERSION(uhub, 1);
177 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
179 struct uhub_softc *sc = usbd_xfer_softc(xfer);
181 switch (USB_GET_STATE(xfer)) {
182 case USB_ST_TRANSFERRED:
185 * This is an indication that some port
186 * has changed status. Notify the bus
187 * event handler thread that we need
188 * to be explored again:
190 usb_needs_explore(sc->sc_udev->bus, 0);
193 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
194 usbd_transfer_submit(xfer);
198 if (xfer->error != USB_ERR_CANCELLED) {
200 * Do a clear-stall. The "stall_pipe" flag
201 * will get cleared before next callback by
204 usbd_xfer_set_stall(xfer);
205 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
206 usbd_transfer_submit(xfer);
212 /*------------------------------------------------------------------------*
213 * uhub_explore_sub - subroutine
217 * Else: A control transaction failed
218 *------------------------------------------------------------------------*/
220 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
223 struct usb_device *child;
227 bus = sc->sc_udev->bus;
230 /* get driver added refcount from USB bus */
231 refcount = bus->driver_added_refcount;
233 /* get device assosiated with the given port */
234 child = usb_bus_port_get_device(bus, up);
239 /* check if probe and attach should be done */
241 if (child->driver_added_refcount != refcount) {
242 child->driver_added_refcount = refcount;
243 err = usb_probe_and_attach(child,
244 USB_IFACE_INDEX_ANY);
249 /* start control transfer, if device mode */
251 if (child->flags.usb_mode == USB_MODE_DEVICE) {
252 usbd_ctrl_transfer_setup(child);
254 /* if a HUB becomes present, do a recursive HUB explore */
257 err = (child->hub->explore) (child);
263 /*------------------------------------------------------------------------*
264 * uhub_read_port_status - factored out code
265 *------------------------------------------------------------------------*/
267 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
269 struct usb_port_status ps;
272 err = usbd_req_get_port_status(
273 sc->sc_udev, NULL, &ps, portno);
275 /* update status regardless of error */
277 sc->sc_st.port_status = UGETW(ps.wPortStatus);
278 sc->sc_st.port_change = UGETW(ps.wPortChange);
280 /* debugging print */
282 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
283 "wPortChange=0x%04x, err=%s\n",
284 portno, sc->sc_st.port_status,
285 sc->sc_st.port_change, usbd_errstr(err));
289 /*------------------------------------------------------------------------*
294 * Else: A control transaction failed
295 *------------------------------------------------------------------------*/
297 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
299 struct usb_device *child;
300 struct usb_device *udev;
301 enum usb_dev_speed speed;
302 enum usb_hc_mode mode;
306 DPRINTF("reattaching port %d\n", portno);
311 child = usb_bus_port_get_device(udev->bus,
312 udev->hub->ports + portno - 1);
316 /* first clear the port connection change bit */
318 err = usbd_req_clear_port_feature(udev, NULL,
319 portno, UHF_C_PORT_CONNECTION);
324 /* check if there is a child */
328 * Free USB device and all subdevices, if any.
330 usb_free_device(child, 0);
333 /* get fresh status */
335 err = uhub_read_port_status(sc, portno);
339 /* check if nothing is connected to the port */
341 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
344 /* check if there is no power on the port and print a warning */
346 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
347 DPRINTF("WARNING: strange, connected port %d "
348 "has no power\n", portno);
350 /* check if the device is in Host Mode */
352 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
354 DPRINTF("Port %d is in Host Mode\n", portno);
356 if (sc->sc_st.port_status & UPS_SUSPEND) {
357 DPRINTF("Port %d was still "
358 "suspended, clearing.\n", portno);
359 err = usbd_req_clear_port_feature(sc->sc_udev,
360 NULL, portno, UHF_PORT_SUSPEND);
364 /* wait for maximum device power up time */
367 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
369 /* reset port, which implies enabling it */
371 err = usbd_req_reset_port(udev, NULL, portno);
374 DPRINTFN(0, "port %d reset "
375 "failed, error=%s\n",
376 portno, usbd_errstr(err));
379 /* get port status again, it might have changed during reset */
381 err = uhub_read_port_status(sc, portno);
385 /* check if something changed during port reset */
387 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
388 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
390 DPRINTFN(0, "giving up port reset "
391 "- device vanished\n");
398 DPRINTF("Port %d is in Device Mode\n", portno);
402 * Figure out the device speed
404 switch (udev->speed) {
406 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
407 speed = USB_SPEED_HIGH;
408 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
409 speed = USB_SPEED_LOW;
411 speed = USB_SPEED_FULL;
414 if (sc->sc_st.port_status & UPS_LOW_SPEED)
415 speed = USB_SPEED_LOW;
417 speed = USB_SPEED_FULL;
420 speed = USB_SPEED_LOW;
423 /* same speed like parent */
428 * Figure out the device mode
430 * NOTE: This part is currently FreeBSD specific.
432 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
433 mode = USB_MODE_DEVICE;
435 mode = USB_MODE_HOST;
437 /* need to create a new child */
438 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
439 udev->depth + 1, portno - 1, portno, speed, mode);
441 DPRINTFN(0, "could not allocate new device\n");
444 return (0); /* success */
449 * Free USB device and all subdevices, if any.
451 usb_free_device(child, 0);
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 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
703 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
704 device_get_nameunit(dev));
706 device_set_usb_desc(dev);
708 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
709 "parent->selfpowered=%d\n",
711 udev->flags.self_powered,
714 parent_hub->flags.self_powered : 0);
716 if (udev->depth > USB_HUB_MAX_DEPTH) {
717 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored\n",
721 if (!udev->flags.self_powered && parent_hub &&
722 (!parent_hub->flags.self_powered)) {
723 DPRINTFN(0, "bus powered HUB connected to "
724 "bus powered HUB. HUB ignored\n");
727 /* get HUB descriptor */
729 DPRINTFN(2, "getting HUB descriptor\n");
731 /* assuming that there is one port */
732 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, 1);
734 nports = hubdesc.bNbrPorts;
736 if (!err && (nports >= 8)) {
737 /* get complete HUB descriptor */
738 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, nports);
741 DPRINTFN(0, "getting hub descriptor failed,"
742 "error=%s\n", usbd_errstr(err));
745 if (hubdesc.bNbrPorts != nports) {
746 DPRINTFN(0, "number of ports changed\n");
750 DPRINTFN(0, "portless HUB\n");
753 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
754 M_USBDEV, M_WAITOK | M_ZERO);
761 #if USB_HAVE_TT_SUPPORT
762 /* init FULL-speed ISOCHRONOUS schedule */
763 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
765 /* initialize HUB structure */
767 hub->explore = &uhub_explore;
768 hub->nports = hubdesc.bNbrPorts;
771 /* if self powered hub, give ports maximum current */
772 if (udev->flags.self_powered) {
773 hub->portpower = USB_MAX_POWER;
775 hub->portpower = USB_MIN_POWER;
778 /* set up interrupt pipe */
780 if (udev->parent_hub == NULL) {
781 /* root HUB is special */
785 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
786 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
789 DPRINTFN(0, "cannot setup interrupt transfer, "
790 "errstr=%s\n", usbd_errstr(err));
793 /* wait with power off for a while */
794 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
797 * To have the best chance of success we do things in the exact same
798 * order as Windoze98. This should not be necessary, but some
799 * devices do not follow the USB specs to the letter.
801 * These are the events on the bus when a hub is attached:
802 * Get device and config descriptors (see attach code)
803 * Get hub descriptor (see above)
806 * wait for power to become stable
807 * (all below happens in explore code)
809 * clear C_PORT_CONNECTION
812 * if device connected
818 * proceed with device attachment
821 /* XXX should check for none, individual, or ganged power? */
824 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
825 USB_EXTRA_POWER_UP_TIME);
827 for (x = 0; x != nports; x++) {
828 /* set up data structures */
829 struct usb_port *up = hub->ports + x;
831 up->device_index = 0;
835 /* check if port is removable */
836 if (!UHD_NOT_REMOV(&hubdesc, portno)) {
840 /* turn the power on */
841 err = usbd_req_set_port_feature(udev, NULL,
842 portno, UHF_PORT_POWER);
845 DPRINTFN(0, "port %d power on failed, %s\n",
846 portno, usbd_errstr(err));
848 DPRINTF("turn on port %d power\n",
851 /* wait for stable power */
852 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
855 device_printf(dev, "%d port%s with %d "
856 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
857 removable, udev->flags.self_powered ? "self" : "bus");
859 /* Start the interrupt endpoint, if any */
861 if (sc->sc_xfer[0] != NULL) {
862 mtx_lock(&sc->sc_mtx);
863 usbd_transfer_start(sc->sc_xfer[0]);
864 mtx_unlock(&sc->sc_mtx);
867 /* Enable automatic power save on all USB HUBs */
869 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
874 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
877 free(udev->hub, M_USBDEV);
881 mtx_destroy(&sc->sc_mtx);
887 * Called from process context when the hub is gone.
888 * Detach all devices on active ports.
891 uhub_detach(device_t dev)
893 struct uhub_softc *sc = device_get_softc(dev);
894 struct usb_hub *hub = sc->sc_udev->hub;
895 struct usb_device *child;
898 if (hub == NULL) { /* must be partially working */
902 /* Make sure interrupt transfer is gone. */
903 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
905 /* Detach all ports */
906 for (x = 0; x != hub->nports; x++) {
908 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
915 * Free USB device and all subdevices, if any.
917 usb_free_device(child, 0);
921 sc->sc_udev->hub = NULL;
923 mtx_destroy(&sc->sc_mtx);
929 uhub_suspend(device_t dev)
932 /* Sub-devices are not suspended here! */
937 uhub_resume(device_t dev)
940 /* Sub-devices are not resumed here! */
945 uhub_driver_added(device_t dev, driver_t *driver)
947 usb_needs_explore_all();
951 struct usb_device *udev;
957 uhub_find_iface_index(struct usb_hub *hub, device_t child,
958 struct hub_result *res)
960 struct usb_interface *iface;
961 struct usb_device *udev;
966 nports = hub->nports;
967 for (x = 0; x != nports; x++) {
968 udev = usb_bus_port_get_device(hub->hubudev->bus,
973 for (i = 0; i != USB_IFACE_MAX; i++) {
974 iface = usbd_get_iface(udev, i);
976 (iface->subdev == child)) {
977 res->iface_index = i;
984 res->iface_index = 0;
990 uhub_child_location_string(device_t parent, device_t child,
991 char *buf, size_t buflen)
993 struct uhub_softc *sc;
995 struct hub_result res;
997 if (!device_is_attached(parent)) {
1003 sc = device_get_softc(parent);
1004 hub = sc->sc_udev->hub;
1007 uhub_find_iface_index(hub, child, &res);
1009 DPRINTF("device not on hub\n");
1015 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1016 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1017 res.portno, device_get_unit(res.udev->bus->bdev),
1018 res.udev->device_index, res.iface_index);
1026 uhub_child_pnpinfo_string(device_t parent, device_t child,
1027 char *buf, size_t buflen)
1029 struct uhub_softc *sc;
1030 struct usb_hub *hub;
1031 struct usb_interface *iface;
1032 struct hub_result res;
1034 if (!device_is_attached(parent)) {
1040 sc = device_get_softc(parent);
1041 hub = sc->sc_udev->hub;
1044 uhub_find_iface_index(hub, child, &res);
1046 DPRINTF("device not on hub\n");
1052 iface = usbd_get_iface(res.udev, res.iface_index);
1053 if (iface && iface->idesc) {
1054 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1055 "devclass=0x%02x devsubclass=0x%02x "
1058 "intclass=0x%02x intsubclass=0x%02x",
1059 UGETW(res.udev->ddesc.idVendor),
1060 UGETW(res.udev->ddesc.idProduct),
1061 res.udev->ddesc.bDeviceClass,
1062 res.udev->ddesc.bDeviceSubClass,
1063 usb_get_serial(res.udev),
1064 UGETW(res.udev->ddesc.bcdDevice),
1065 iface->idesc->bInterfaceClass,
1066 iface->idesc->bInterfaceSubClass);
1080 * The USB Transaction Translator:
1081 * ===============================
1083 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1084 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1085 * USB transfers. To utilize bandwidth dynamically the "scatter and
1086 * gather" principle must be applied. This means that bandwidth must
1087 * be divided into equal parts of bandwidth. With regard to USB all
1088 * data is transferred in smaller packets with length
1089 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1092 * The bandwidth scheduler which I have implemented will simply pack
1093 * the USB transfers back to back until there is no more space in the
1094 * schedule. Out of the 8 microframes which the USB 2.0 standard
1095 * provides, only 6 are available for non-HIGH-speed devices. I have
1096 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1097 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1098 * this division, it is very difficult to allocate and free bandwidth
1101 * NOTE about the Transaction Translator in USB HUBs:
1103 * USB HUBs have a very simple Transaction Translator, that will
1104 * simply pipeline all the SPLIT transactions. That means that the
1105 * transactions will be executed in the order they are queued!
1109 /*------------------------------------------------------------------------*
1110 * usb_intr_find_best_slot
1113 * The best Transaction Translation slot for an interrupt endpoint.
1114 *------------------------------------------------------------------------*/
1116 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1117 uint8_t end, uint8_t mask)
1119 usb_size_t min = 0 - 1;
1127 /* find the last slot with lesser used bandwidth */
1129 for (x = start; x < end; x++) {
1133 /* compute sum of bandwidth */
1134 for (z = x; z < end; z++) {
1135 if (mask & (1U << (z - x)))
1139 /* check if the current multi-slot is more optimal */
1145 /* check if the mask is about to be shifted out */
1146 if (mask & (1U << (end - 1 - x)))
1152 /*------------------------------------------------------------------------*
1153 * usb_hs_bandwidth_adjust
1155 * This function will update the bandwith usage for the microframe
1156 * having index "slot" by "len" bytes. "len" can be negative. If the
1157 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1158 * the "slot" argument will be replaced by the slot having least used
1159 * bandwidth. The "mask" argument is used for multi-slot allocations.
1162 * The slot in which the bandwidth update was done: 0..7
1163 *------------------------------------------------------------------------*/
1165 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1166 uint8_t slot, uint8_t mask)
1168 struct usb_bus *bus = udev->bus;
1169 struct usb_hub *hub;
1170 enum usb_dev_speed speed;
1173 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1175 speed = usbd_get_speed(udev);
1179 case USB_SPEED_FULL:
1180 if (speed == USB_SPEED_LOW) {
1184 * The Host Controller Driver should have
1185 * performed checks so that the lookup
1186 * below does not result in a NULL pointer
1190 hub = udev->parent_hs_hub->hub;
1191 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1192 slot = usb_intr_find_best_slot(hub->uframe_usage,
1193 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1195 for (x = slot; x < 8; x++) {
1196 if (mask & (1U << (x - slot))) {
1197 hub->uframe_usage[x] += len;
1198 bus->uframe_usage[x] += len;
1203 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1204 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1205 USB_HS_MICRO_FRAMES_MAX, mask);
1207 for (x = slot; x < 8; x++) {
1208 if (mask & (1U << (x - slot))) {
1209 bus->uframe_usage[x] += len;
1217 /*------------------------------------------------------------------------*
1218 * usb_hs_bandwidth_alloc
1220 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1221 *------------------------------------------------------------------------*/
1223 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1225 struct usb_device *udev;
1230 udev = xfer->xroot->udev;
1232 if (udev->flags.usb_mode != USB_MODE_HOST)
1233 return; /* not supported */
1235 xfer->endpoint->refcount_bw++;
1236 if (xfer->endpoint->refcount_bw != 1)
1237 return; /* already allocated */
1239 speed = usbd_get_speed(udev);
1241 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1243 /* allocate a microframe slot */
1246 slot = usb_hs_bandwidth_adjust(udev,
1247 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1249 xfer->endpoint->usb_uframe = slot;
1250 xfer->endpoint->usb_smask = mask << slot;
1252 if ((speed != USB_SPEED_FULL) &&
1253 (speed != USB_SPEED_LOW)) {
1254 xfer->endpoint->usb_cmask = 0x00 ;
1256 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1260 case UE_ISOCHRONOUS:
1261 switch (usbd_xfer_get_fps_shift(xfer)) {
1276 /* allocate a microframe multi-slot */
1278 slot = usb_hs_bandwidth_adjust(udev,
1279 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1281 xfer->endpoint->usb_uframe = slot;
1282 xfer->endpoint->usb_cmask = 0;
1283 xfer->endpoint->usb_smask = mask << slot;
1287 xfer->endpoint->usb_uframe = 0;
1288 xfer->endpoint->usb_cmask = 0;
1289 xfer->endpoint->usb_smask = 0;
1293 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1294 xfer->endpoint->usb_uframe,
1295 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1298 /*------------------------------------------------------------------------*
1299 * usb_hs_bandwidth_free
1301 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1302 *------------------------------------------------------------------------*/
1304 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1306 struct usb_device *udev;
1310 udev = xfer->xroot->udev;
1312 if (udev->flags.usb_mode != USB_MODE_HOST)
1313 return; /* not supported */
1315 xfer->endpoint->refcount_bw--;
1316 if (xfer->endpoint->refcount_bw != 0)
1317 return; /* still allocated */
1319 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1321 case UE_ISOCHRONOUS:
1323 slot = xfer->endpoint->usb_uframe;
1324 mask = xfer->endpoint->usb_smask;
1326 /* free microframe slot(s): */
1327 usb_hs_bandwidth_adjust(udev,
1328 -xfer->max_frame_size, slot, mask >> slot);
1330 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1331 slot, mask >> slot);
1333 xfer->endpoint->usb_uframe = 0;
1334 xfer->endpoint->usb_cmask = 0;
1335 xfer->endpoint->usb_smask = 0;
1343 /*------------------------------------------------------------------------*
1344 * usbd_fs_isoc_schedule_init_sub
1346 * This function initialises an USB FULL speed isochronous schedule
1348 *------------------------------------------------------------------------*/
1349 #if USB_HAVE_TT_SUPPORT
1351 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1353 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1354 USB_FS_BYTES_PER_HS_UFRAME);
1355 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1356 fss->frame_slot = 0;
1360 /*------------------------------------------------------------------------*
1361 * usbd_fs_isoc_schedule_init_all
1363 * This function will reset the complete USB FULL speed isochronous
1364 * bandwidth schedule.
1365 *------------------------------------------------------------------------*/
1366 #if USB_HAVE_TT_SUPPORT
1368 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1370 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1372 while (fss != fss_end) {
1373 usbd_fs_isoc_schedule_init_sub(fss);
1379 /*------------------------------------------------------------------------*
1380 * usb_isoc_time_expand
1382 * This function will expand the time counter from 7-bit to 16-bit.
1385 * 16-bit isochronous time counter.
1386 *------------------------------------------------------------------------*/
1388 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1392 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1394 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1396 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1398 if (isoc_time_curr < rem) {
1399 /* the time counter wrapped around */
1400 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1402 /* update the remainder */
1404 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1405 bus->isoc_time_last |= isoc_time_curr;
1407 return (bus->isoc_time_last);
1410 /*------------------------------------------------------------------------*
1411 * usbd_fs_isoc_schedule_isoc_time_expand
1413 * This function does multiple things. First of all it will expand the
1414 * passed isochronous time, which is the return value. Then it will
1415 * store where the current FULL speed isochronous schedule is
1416 * positioned in time and where the end is. See "pp_start" and
1417 * "pp_end" arguments.
1420 * Expanded version of "isoc_time".
1422 * NOTE: This function depends on being called regularly with
1423 * intervals less than "USB_ISOC_TIME_MAX".
1424 *------------------------------------------------------------------------*/
1425 #if USB_HAVE_TT_SUPPORT
1427 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1428 struct usb_fs_isoc_schedule **pp_start,
1429 struct usb_fs_isoc_schedule **pp_end,
1432 struct usb_fs_isoc_schedule *fss_end;
1433 struct usb_fs_isoc_schedule *fss_a;
1434 struct usb_fs_isoc_schedule *fss_b;
1435 struct usb_hub *hs_hub;
1437 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1439 hs_hub = udev->parent_hs_hub->hub;
1441 if (hs_hub != NULL) {
1443 fss_a = hs_hub->fs_isoc_schedule +
1444 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1446 hs_hub->isoc_last_time = isoc_time;
1448 fss_b = hs_hub->fs_isoc_schedule +
1449 (isoc_time % USB_ISOC_TIME_MAX);
1451 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1453 *pp_start = hs_hub->fs_isoc_schedule;
1456 while (fss_a != fss_b) {
1457 if (fss_a == fss_end) {
1458 fss_a = hs_hub->fs_isoc_schedule;
1461 usbd_fs_isoc_schedule_init_sub(fss_a);
1474 /*------------------------------------------------------------------------*
1475 * usbd_fs_isoc_schedule_alloc
1477 * This function will allocate bandwidth for an isochronous FULL speed
1478 * transaction in the FULL speed schedule. The microframe slot where
1479 * the transaction should be started is stored in the byte pointed to
1480 * by "pstart". The "len" argument specifies the length of the
1481 * transaction in bytes.
1486 *------------------------------------------------------------------------*/
1487 #if USB_HAVE_TT_SUPPORT
1489 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1490 uint8_t *pstart, uint16_t len)
1492 uint8_t slot = fss->frame_slot;
1494 /* Compute overhead and bit-stuffing */
1501 if (len > fss->total_bytes) {
1502 *pstart = 0; /* set some dummy value */
1503 return (1); /* error */
1507 fss->total_bytes -= len;
1509 while (len >= fss->frame_bytes) {
1510 len -= fss->frame_bytes;
1511 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1515 fss->frame_bytes -= len;
1518 return (0); /* success */
1522 /*------------------------------------------------------------------------*
1523 * usb_bus_port_get_device
1525 * This function is NULL safe.
1526 *------------------------------------------------------------------------*/
1528 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1530 if ((bus == NULL) || (up == NULL)) {
1534 if (up->device_index == 0) {
1538 return (bus->devices[up->device_index]);
1541 /*------------------------------------------------------------------------*
1542 * usb_bus_port_set_device
1544 * This function is NULL safe.
1545 *------------------------------------------------------------------------*/
1547 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1548 struct usb_device *udev, uint8_t device_index)
1555 * There is only one case where we don't
1556 * have an USB port, and that is the Root Hub!
1560 up->device_index = device_index;
1562 device_index = up->device_index;
1563 up->device_index = 0;
1567 * Make relationships to our new device
1569 if (device_index != 0) {
1571 mtx_lock(&usb_ref_lock);
1573 bus->devices[device_index] = udev;
1575 mtx_unlock(&usb_ref_lock);
1581 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1584 /*------------------------------------------------------------------------*
1587 * This functions is called when the USB event thread needs to run.
1588 *------------------------------------------------------------------------*/
1590 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1597 DPRINTF("No bus pointer!\n");
1600 if ((bus->devices == NULL) ||
1601 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1602 DPRINTF("No root HUB\n");
1605 if (mtx_owned(&bus->bus_mtx)) {
1614 if (usb_proc_msignal(&bus->explore_proc,
1615 &bus->explore_msg[0], &bus->explore_msg[1])) {
1619 USB_BUS_UNLOCK(bus);
1623 /*------------------------------------------------------------------------*
1624 * usb_needs_explore_all
1626 * This function is called whenever a new driver is loaded and will
1627 * cause that all USB busses are re-explored.
1628 *------------------------------------------------------------------------*/
1630 usb_needs_explore_all(void)
1632 struct usb_bus *bus;
1639 dc = usb_devclass_ptr;
1641 DPRINTFN(0, "no devclass\n");
1645 * Explore all USB busses in parallell.
1647 max = devclass_get_maxunit(dc);
1649 dev = devclass_get_device(dc, max);
1651 bus = device_get_softc(dev);
1653 usb_needs_explore(bus, 1);
1660 /*------------------------------------------------------------------------*
1661 * usb_bus_power_update
1663 * This function will ensure that all USB devices on the given bus are
1664 * properly suspended or resumed according to the device transfer
1666 *------------------------------------------------------------------------*/
1669 usb_bus_power_update(struct usb_bus *bus)
1671 usb_needs_explore(bus, 0 /* no probe */ );
1675 /*------------------------------------------------------------------------*
1676 * usbd_transfer_power_ref
1678 * This function will modify the power save reference counts and
1679 * wakeup the USB device associated with the given USB transfer, if
1681 *------------------------------------------------------------------------*/
1684 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1686 static const usb_power_mask_t power_mask[4] = {
1687 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1688 [UE_BULK] = USB_HW_POWER_BULK,
1689 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1690 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1692 struct usb_device *udev;
1693 uint8_t needs_explore;
1694 uint8_t needs_hw_power;
1697 udev = xfer->xroot->udev;
1699 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1700 /* no power save for root HUB */
1703 USB_BUS_LOCK(udev->bus);
1705 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1707 udev->pwr_save.last_xfer_time = ticks;
1708 udev->pwr_save.type_refs[xfer_type] += val;
1710 if (xfer->flags_int.control_xfr) {
1711 udev->pwr_save.read_refs += val;
1712 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1714 * It is not allowed to suspend during a
1717 udev->pwr_save.write_refs += val;
1719 } else if (USB_GET_DATA_ISREAD(xfer)) {
1720 udev->pwr_save.read_refs += val;
1722 udev->pwr_save.write_refs += val;
1726 if (udev->flags.self_suspended)
1727 needs_explore = usb_peer_should_wakeup(udev);
1731 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1732 DPRINTF("Adding type %u to power state\n", xfer_type);
1733 udev->bus->hw_power_state |= power_mask[xfer_type];
1743 USB_BUS_UNLOCK(udev->bus);
1745 if (needs_explore) {
1746 DPRINTF("update\n");
1747 usb_bus_power_update(udev->bus);
1748 } else if (needs_hw_power) {
1749 DPRINTF("needs power\n");
1750 if (udev->bus->methods->set_hw_power != NULL) {
1751 (udev->bus->methods->set_hw_power) (udev->bus);
1757 /*------------------------------------------------------------------------*
1758 * usb_peer_should_wakeup
1760 * This function returns non-zero if the current device should wake up.
1761 *------------------------------------------------------------------------*/
1763 usb_peer_should_wakeup(struct usb_device *udev)
1765 return ((udev->power_mode == USB_POWER_MODE_ON) ||
1766 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1767 (udev->pwr_save.write_refs != 0) ||
1768 ((udev->pwr_save.read_refs != 0) &&
1769 (udev->flags.usb_mode == USB_MODE_HOST) &&
1770 (usb_peer_can_wakeup(udev) == 0)));
1773 /*------------------------------------------------------------------------*
1776 * This function implements the USB power daemon and is called
1777 * regularly from the USB explore thread.
1778 *------------------------------------------------------------------------*/
1781 usb_bus_powerd(struct usb_bus *bus)
1783 struct usb_device *udev;
1786 usb_ticks_t mintime;
1787 usb_size_t type_refs[5];
1790 limit = usb_power_timeout;
1793 else if (limit > 255)
1798 DPRINTF("bus=%p\n", bus);
1803 * The root HUB device is never suspended
1804 * and we simply skip it.
1806 for (x = USB_ROOT_HUB_ADDR + 1;
1807 x != bus->devices_max; x++) {
1809 udev = bus->devices[x];
1813 temp = ticks - udev->pwr_save.last_xfer_time;
1815 if (usb_peer_should_wakeup(udev)) {
1816 /* check if we are suspended */
1817 if (udev->flags.self_suspended != 0) {
1818 USB_BUS_UNLOCK(bus);
1819 usb_dev_resume_peer(udev);
1822 } else if ((temp >= limit) &&
1823 (udev->flags.usb_mode == USB_MODE_HOST) &&
1824 (udev->flags.self_suspended == 0)) {
1825 /* try to do suspend */
1827 USB_BUS_UNLOCK(bus);
1828 usb_dev_suspend_peer(udev);
1833 /* reset counters */
1842 /* Re-loop all the devices to get the actual state */
1844 for (x = USB_ROOT_HUB_ADDR + 1;
1845 x != bus->devices_max; x++) {
1847 udev = bus->devices[x];
1851 /* we found a non-Root-Hub USB device */
1854 /* "last_xfer_time" can be updated by a resume */
1855 temp = ticks - udev->pwr_save.last_xfer_time;
1858 * Compute minimum time since last transfer for the complete
1864 if (udev->flags.self_suspended == 0) {
1865 type_refs[0] += udev->pwr_save.type_refs[0];
1866 type_refs[1] += udev->pwr_save.type_refs[1];
1867 type_refs[2] += udev->pwr_save.type_refs[2];
1868 type_refs[3] += udev->pwr_save.type_refs[3];
1872 if (mintime >= (1 * hz)) {
1873 /* recompute power masks */
1874 DPRINTF("Recomputing power masks\n");
1875 bus->hw_power_state = 0;
1876 if (type_refs[UE_CONTROL] != 0)
1877 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1878 if (type_refs[UE_BULK] != 0)
1879 bus->hw_power_state |= USB_HW_POWER_BULK;
1880 if (type_refs[UE_INTERRUPT] != 0)
1881 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1882 if (type_refs[UE_ISOCHRONOUS] != 0)
1883 bus->hw_power_state |= USB_HW_POWER_ISOC;
1884 if (type_refs[4] != 0)
1885 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1887 USB_BUS_UNLOCK(bus);
1889 if (bus->methods->set_hw_power != NULL) {
1890 /* always update hardware power! */
1891 (bus->methods->set_hw_power) (bus);
1897 /*------------------------------------------------------------------------*
1898 * usb_dev_resume_peer
1900 * This function will resume an USB peer and do the required USB
1901 * signalling to get an USB device out of the suspended state.
1902 *------------------------------------------------------------------------*/
1904 usb_dev_resume_peer(struct usb_device *udev)
1906 struct usb_bus *bus;
1913 /* check if already resumed */
1914 if (udev->flags.self_suspended == 0)
1917 /* we need a parent HUB to do resume */
1918 if (udev->parent_hub == NULL)
1921 DPRINTF("udev=%p\n", udev);
1923 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1924 (udev->flags.remote_wakeup == 0)) {
1926 * If the host did not set the remote wakeup feature, we can
1927 * not wake it up either!
1929 DPRINTF("remote wakeup is not set!\n");
1932 /* get bus pointer */
1935 /* resume parent hub first */
1936 usb_dev_resume_peer(udev->parent_hub);
1938 /* reduce chance of instant resume failure by waiting a little bit */
1939 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
1941 /* resume current port (Valid in Host and Device Mode) */
1942 err = usbd_req_clear_port_feature(udev->parent_hub,
1943 NULL, udev->port_no, UHF_PORT_SUSPEND);
1945 DPRINTFN(0, "Resuming port failed\n");
1948 /* resume settle time */
1949 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1951 if (bus->methods->device_resume != NULL) {
1952 /* resume USB device on the USB controller */
1953 (bus->methods->device_resume) (udev);
1956 /* set that this device is now resumed */
1957 udev->flags.self_suspended = 0;
1959 /* make sure that we don't go into suspend right away */
1960 udev->pwr_save.last_xfer_time = ticks;
1962 /* make sure the needed power masks are on */
1963 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1964 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1965 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1966 bus->hw_power_state |= USB_HW_POWER_BULK;
1967 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1968 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1969 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1970 bus->hw_power_state |= USB_HW_POWER_ISOC;
1972 USB_BUS_UNLOCK(bus);
1974 if (bus->methods->set_hw_power != NULL) {
1975 /* always update hardware power! */
1976 (bus->methods->set_hw_power) (bus);
1981 /* notify all sub-devices about resume */
1982 err = usb_suspend_resume(udev, 0);
1984 usbd_sr_unlock(udev);
1986 /* check if peer has wakeup capability */
1987 if (usb_peer_can_wakeup(udev)) {
1988 /* clear remote wakeup */
1989 err = usbd_req_clear_device_feature(udev,
1990 NULL, UF_DEVICE_REMOTE_WAKEUP);
1992 DPRINTFN(0, "Clearing device "
1993 "remote wakeup failed: %s\n",
2000 /*------------------------------------------------------------------------*
2001 * usb_dev_suspend_peer
2003 * This function will suspend an USB peer and do the required USB
2004 * signalling to get an USB device into the suspended state.
2005 *------------------------------------------------------------------------*/
2007 usb_dev_suspend_peer(struct usb_device *udev)
2009 struct usb_device *child;
2019 /* check if already suspended */
2020 if (udev->flags.self_suspended)
2023 /* we need a parent HUB to do suspend */
2024 if (udev->parent_hub == NULL)
2027 DPRINTF("udev=%p\n", udev);
2029 /* check if the current device is a HUB */
2030 if (udev->hub != NULL) {
2031 nports = udev->hub->nports;
2033 /* check if all devices on the HUB are suspended */
2034 for (x = 0; x != nports; x++) {
2036 child = usb_bus_port_get_device(udev->bus,
2037 udev->hub->ports + x);
2042 if (child->flags.self_suspended)
2045 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2050 USB_BUS_LOCK(udev->bus);
2052 * Checking for suspend condition and setting suspended bit
2055 err = usb_peer_should_wakeup(udev);
2058 * Set that this device is suspended. This variable
2059 * must be set before calling USB controller suspend
2062 udev->flags.self_suspended = 1;
2064 USB_BUS_UNLOCK(udev->bus);
2067 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2068 /* resume parent HUB first */
2069 usb_dev_resume_peer(udev->parent_hub);
2071 /* reduce chance of instant resume failure by waiting a little bit */
2072 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2074 /* resume current port (Valid in Host and Device Mode) */
2075 err = usbd_req_clear_port_feature(udev->parent_hub,
2076 NULL, udev->port_no, UHF_PORT_SUSPEND);
2078 /* resume settle time */
2079 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2081 DPRINTF("Suspend was cancelled!\n");
2087 /* notify all sub-devices about suspend */
2088 err = usb_suspend_resume(udev, 1);
2090 usbd_sr_unlock(udev);
2092 if (usb_peer_can_wakeup(udev)) {
2093 /* allow device to do remote wakeup */
2094 err = usbd_req_set_device_feature(udev,
2095 NULL, UF_DEVICE_REMOTE_WAKEUP);
2097 DPRINTFN(0, "Setting device "
2098 "remote wakeup failed\n");
2102 if (udev->bus->methods->device_suspend != NULL) {
2105 /* suspend device on the USB controller */
2106 (udev->bus->methods->device_suspend) (udev);
2109 temp = usbd_get_dma_delay(udev);
2111 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2114 /* suspend current port */
2115 err = usbd_req_set_port_feature(udev->parent_hub,
2116 NULL, udev->port_no, UHF_PORT_SUSPEND);
2118 DPRINTFN(0, "Suspending port failed\n");
2122 udev = udev->parent_hub;
2126 /*------------------------------------------------------------------------*
2127 * usbd_set_power_mode
2129 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2131 *------------------------------------------------------------------------*/
2133 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2135 /* filter input argument */
2136 if ((power_mode != USB_POWER_MODE_ON) &&
2137 (power_mode != USB_POWER_MODE_OFF))
2138 power_mode = USB_POWER_MODE_SAVE;
2140 power_mode = usbd_filter_power_mode(udev, power_mode);
2142 udev->power_mode = power_mode; /* update copy of power mode */
2145 usb_bus_power_update(udev->bus);
2149 /*------------------------------------------------------------------------*
2150 * usbd_filter_power_mode
2152 * This function filters the power mode based on hardware requirements.
2153 *------------------------------------------------------------------------*/
2155 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2157 struct usb_bus_methods *mtod;
2160 mtod = udev->bus->methods;
2163 if (mtod->get_power_mode != NULL)
2164 (mtod->get_power_mode) (udev, &temp);
2166 /* check if we should not filter */
2168 return (power_mode);
2170 /* use fixed power mode given by hardware driver */