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,
83 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
87 static int usb_power_timeout = 30; /* seconds */
89 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
90 &usb_power_timeout, 0, "USB power timeout");
93 struct uhub_current_state {
99 struct uhub_current_state sc_st;/* current state */
100 device_t sc_dev; /* base device */
101 struct mtx sc_mtx; /* our mutex */
102 struct usb_device *sc_udev; /* USB device */
103 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
105 #define UHUB_FLAG_DID_EXPLORE 0x01
109 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
110 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
111 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
113 /* prototypes for type checking: */
115 static device_probe_t uhub_probe;
116 static device_attach_t uhub_attach;
117 static device_detach_t uhub_detach;
118 static device_suspend_t uhub_suspend;
119 static device_resume_t uhub_resume;
121 static bus_driver_added_t uhub_driver_added;
122 static bus_child_location_str_t uhub_child_location_string;
123 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
125 static usb_callback_t uhub_intr_callback;
127 static void usb_dev_resume_peer(struct usb_device *udev);
128 static void usb_dev_suspend_peer(struct usb_device *udev);
129 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
131 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
134 .type = UE_INTERRUPT,
135 .endpoint = UE_ADDR_ANY,
136 .direction = UE_DIR_ANY,
138 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
139 .bufsize = 0, /* use wMaxPacketSize */
140 .callback = &uhub_intr_callback,
141 .interval = UHUB_INTR_INTERVAL,
146 * driver instance for "hub" connected to "usb"
147 * and "hub" connected to "hub"
149 static devclass_t uhub_devclass;
151 static device_method_t uhub_methods[] = {
152 DEVMETHOD(device_probe, uhub_probe),
153 DEVMETHOD(device_attach, uhub_attach),
154 DEVMETHOD(device_detach, uhub_detach),
156 DEVMETHOD(device_suspend, uhub_suspend),
157 DEVMETHOD(device_resume, uhub_resume),
159 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
160 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
161 DEVMETHOD(bus_driver_added, uhub_driver_added),
165 static driver_t uhub_driver = {
167 .methods = uhub_methods,
168 .size = sizeof(struct uhub_softc)
171 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
172 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
175 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
177 struct uhub_softc *sc = usbd_xfer_softc(xfer);
179 switch (USB_GET_STATE(xfer)) {
180 case USB_ST_TRANSFERRED:
183 * This is an indication that some port
184 * has changed status. Notify the bus
185 * event handler thread that we need
186 * to be explored again:
188 usb_needs_explore(sc->sc_udev->bus, 0);
191 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
192 usbd_transfer_submit(xfer);
196 if (xfer->error != USB_ERR_CANCELLED) {
198 * Do a clear-stall. The "stall_pipe" flag
199 * will get cleared before next callback by
202 usbd_xfer_set_stall(xfer);
203 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
204 usbd_transfer_submit(xfer);
210 /*------------------------------------------------------------------------*
211 * uhub_explore_sub - subroutine
215 * Else: A control transaction failed
216 *------------------------------------------------------------------------*/
218 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
221 struct usb_device *child;
225 bus = sc->sc_udev->bus;
228 /* get driver added refcount from USB bus */
229 refcount = bus->driver_added_refcount;
231 /* get device assosiated with the given port */
232 child = usb_bus_port_get_device(bus, up);
237 /* check if probe and attach should be done */
239 if (child->driver_added_refcount != refcount) {
240 child->driver_added_refcount = refcount;
241 err = usb_probe_and_attach(child,
242 USB_IFACE_INDEX_ANY);
247 /* start control transfer, if device mode */
249 if (child->flags.usb_mode == USB_MODE_DEVICE) {
250 usbd_ctrl_transfer_setup(child);
252 /* if a HUB becomes present, do a recursive HUB explore */
255 err = (child->hub->explore) (child);
261 /*------------------------------------------------------------------------*
262 * uhub_read_port_status - factored out code
263 *------------------------------------------------------------------------*/
265 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
267 struct usb_port_status ps;
270 err = usbd_req_get_port_status(
271 sc->sc_udev, NULL, &ps, portno);
273 /* update status regardless of error */
275 sc->sc_st.port_status = UGETW(ps.wPortStatus);
276 sc->sc_st.port_change = UGETW(ps.wPortChange);
278 /* debugging print */
280 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
281 "wPortChange=0x%04x, err=%s\n",
282 portno, sc->sc_st.port_status,
283 sc->sc_st.port_change, usbd_errstr(err));
287 /*------------------------------------------------------------------------*
292 * Else: A control transaction failed
293 *------------------------------------------------------------------------*/
295 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
297 struct usb_device *child;
298 struct usb_device *udev;
299 enum usb_dev_speed speed;
300 enum usb_hc_mode mode;
304 DPRINTF("reattaching port %d\n", portno);
309 child = usb_bus_port_get_device(udev->bus,
310 udev->hub->ports + portno - 1);
314 /* first clear the port connection change bit */
316 err = usbd_req_clear_port_feature(udev, NULL,
317 portno, UHF_C_PORT_CONNECTION);
322 /* check if there is a child */
326 * Free USB device and all subdevices, if any.
328 usb_free_device(child, 0);
331 /* get fresh status */
333 err = uhub_read_port_status(sc, portno);
337 /* check if nothing is connected to the port */
339 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
342 /* check if there is no power on the port and print a warning */
344 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) {
345 DPRINTF("WARNING: strange, connected port %d "
346 "has no power\n", portno);
348 /* check if the device is in Host Mode */
350 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
352 DPRINTF("Port %d is in Host Mode\n", portno);
354 if (sc->sc_st.port_status & UPS_SUSPEND) {
355 DPRINTF("Port %d was still "
356 "suspended, clearing.\n", portno);
357 err = usbd_req_clear_port_feature(sc->sc_udev,
358 NULL, portno, UHF_PORT_SUSPEND);
362 /* wait for maximum device power up time */
365 USB_MS_TO_TICKS(USB_PORT_POWERUP_DELAY));
367 /* reset port, which implies enabling it */
369 err = usbd_req_reset_port(udev, NULL, portno);
372 DPRINTFN(0, "port %d reset "
373 "failed, error=%s\n",
374 portno, usbd_errstr(err));
377 /* get port status again, it might have changed during reset */
379 err = uhub_read_port_status(sc, portno);
383 /* check if something changed during port reset */
385 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
386 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
388 DPRINTFN(0, "giving up port reset "
389 "- device vanished\n");
396 DPRINTF("Port %d is in Device Mode\n", portno);
400 * Figure out the device speed
402 switch (udev->speed) {
404 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
405 speed = USB_SPEED_HIGH;
406 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
407 speed = USB_SPEED_LOW;
409 speed = USB_SPEED_FULL;
412 if (sc->sc_st.port_status & UPS_LOW_SPEED)
413 speed = USB_SPEED_LOW;
415 speed = USB_SPEED_FULL;
418 speed = USB_SPEED_LOW;
421 /* same speed like parent */
426 * Figure out the device mode
428 * NOTE: This part is currently FreeBSD specific.
430 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
431 mode = USB_MODE_DEVICE;
433 mode = USB_MODE_HOST;
435 /* need to create a new child */
436 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
437 udev->depth + 1, portno - 1, portno, speed, mode);
439 DPRINTFN(0, "could not allocate new device\n");
442 return (0); /* success */
447 * Free USB device and all subdevices, if any.
449 usb_free_device(child, 0);
453 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
454 err = usbd_req_clear_port_feature(
456 portno, UHF_PORT_ENABLE);
460 DPRINTFN(0, "device problem (%s), "
461 "disabling port %d\n", usbd_errstr(err), portno);
466 /*------------------------------------------------------------------------*
467 * uhub_suspend_resume_port
471 * Else: A control transaction failed
472 *------------------------------------------------------------------------*/
474 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
476 struct usb_device *child;
477 struct usb_device *udev;
481 DPRINTF("port %d\n", portno);
484 child = usb_bus_port_get_device(udev->bus,
485 udev->hub->ports + portno - 1);
487 /* first clear the port suspend change bit */
489 err = usbd_req_clear_port_feature(udev, NULL,
490 portno, UHF_C_PORT_SUSPEND);
492 DPRINTF("clearing suspend failed.\n");
495 /* get fresh status */
497 err = uhub_read_port_status(sc, portno);
499 DPRINTF("reading port status failed.\n");
502 /* get current state */
504 if (sc->sc_st.port_status & UPS_SUSPEND) {
510 DPRINTF("suspended=%u\n", is_suspend);
512 /* do the suspend or resume */
516 * This code handle two cases: 1) Host Mode - we can only
517 * receive resume here 2) Device Mode - we can receive
518 * suspend and resume here
521 usb_dev_resume_peer(child);
522 else if (child->flags.usb_mode == USB_MODE_DEVICE)
523 usb_dev_suspend_peer(child);
529 /*------------------------------------------------------------------------*
530 * uhub_root_interrupt
532 * This function is called when a Root HUB interrupt has
533 * happened. "ptr" and "len" makes up the Root HUB interrupt
534 * packet. This function is called having the "bus_mtx" locked.
535 *------------------------------------------------------------------------*/
537 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
539 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
541 usb_needs_explore(bus, 0);
544 /*------------------------------------------------------------------------*
550 *------------------------------------------------------------------------*/
552 uhub_explore(struct usb_device *udev)
555 struct uhub_softc *sc;
564 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
566 /* ignore hubs that are too deep */
567 if (udev->depth > USB_HUB_MAX_DEPTH) {
568 return (USB_ERR_TOO_DEEP);
571 if (udev->flags.self_suspended) {
572 /* need to wait until the child signals resume */
573 DPRINTF("Device is suspended!\n");
576 for (x = 0; x != hub->nports; x++) {
580 err = uhub_read_port_status(sc, portno);
582 /* most likely the HUB is gone */
585 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
586 DPRINTF("Overcurrent on port %u.\n", portno);
587 err = usbd_req_clear_port_feature(
588 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
590 /* most likely the HUB is gone */
594 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
596 * Fake a connect status change so that the
597 * status gets checked initially!
599 sc->sc_st.port_change |=
600 UPS_C_CONNECT_STATUS;
602 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
603 err = usbd_req_clear_port_feature(
604 udev, NULL, portno, UHF_C_PORT_ENABLE);
606 /* most likely the HUB is gone */
609 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
611 * Ignore the port error if the device
614 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
615 DPRINTFN(0, "illegal enable change, "
616 "port %d\n", portno);
619 if (up->restartcnt == USB_RESTART_MAX) {
620 /* XXX could try another speed ? */
621 DPRINTFN(0, "port error, giving up "
622 "port %d\n", portno);
624 sc->sc_st.port_change |=
625 UPS_C_CONNECT_STATUS;
630 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
631 err = uhub_reattach_port(sc, portno);
633 /* most likely the HUB is gone */
637 if (sc->sc_st.port_change & UPS_C_SUSPEND) {
638 err = uhub_suspend_resume_port(sc, portno);
640 /* most likely the HUB is gone */
644 err = uhub_explore_sub(sc, up);
646 /* no device(s) present */
649 /* explore succeeded - reset restart counter */
653 /* initial status checked */
654 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
657 return (USB_ERR_NORMAL_COMPLETION);
661 uhub_probe(device_t dev)
663 struct usb_attach_arg *uaa = device_get_ivars(dev);
665 if (uaa->usb_mode != USB_MODE_HOST) {
669 * The subclass for USB HUBs is ignored because it is 0 for
670 * some and 1 for others.
672 if ((uaa->info.bConfigIndex == 0) &&
673 (uaa->info.bDeviceClass == UDCLASS_HUB)) {
680 uhub_attach(device_t dev)
682 struct uhub_softc *sc = device_get_softc(dev);
683 struct usb_attach_arg *uaa = device_get_ivars(dev);
684 struct usb_device *udev = uaa->device;
685 struct usb_device *parent_hub = udev->parent_hub;
687 struct usb_hub_descriptor hubdesc;
699 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
701 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
702 device_get_nameunit(dev));
704 device_set_usb_desc(dev);
706 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
707 "parent->selfpowered=%d\n",
709 udev->flags.self_powered,
712 parent_hub->flags.self_powered : 0);
714 if (udev->depth > USB_HUB_MAX_DEPTH) {
715 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored\n",
719 if (!udev->flags.self_powered && parent_hub &&
720 (!parent_hub->flags.self_powered)) {
721 DPRINTFN(0, "bus powered HUB connected to "
722 "bus powered HUB. HUB ignored\n");
725 /* get HUB descriptor */
727 DPRINTFN(2, "getting HUB descriptor\n");
729 /* assuming that there is one port */
730 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, 1);
732 nports = hubdesc.bNbrPorts;
734 if (!err && (nports >= 8)) {
735 /* get complete HUB descriptor */
736 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc, nports);
739 DPRINTFN(0, "getting hub descriptor failed,"
740 "error=%s\n", usbd_errstr(err));
743 if (hubdesc.bNbrPorts != nports) {
744 DPRINTFN(0, "number of ports changed\n");
748 DPRINTFN(0, "portless HUB\n");
751 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
752 M_USBDEV, M_WAITOK | M_ZERO);
759 #if USB_HAVE_TT_SUPPORT
760 /* init FULL-speed ISOCHRONOUS schedule */
761 usbd_fs_isoc_schedule_init_all(hub->fs_isoc_schedule);
763 /* initialize HUB structure */
765 hub->explore = &uhub_explore;
766 hub->nports = hubdesc.bNbrPorts;
769 /* if self powered hub, give ports maximum current */
770 if (udev->flags.self_powered) {
771 hub->portpower = USB_MAX_POWER;
773 hub->portpower = USB_MIN_POWER;
776 /* set up interrupt pipe */
778 if (udev->parent_hub == NULL) {
779 /* root HUB is special */
783 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
784 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
787 DPRINTFN(0, "cannot setup interrupt transfer, "
788 "errstr=%s\n", usbd_errstr(err));
791 /* wait with power off for a while */
792 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
795 * To have the best chance of success we do things in the exact same
796 * order as Windoze98. This should not be necessary, but some
797 * devices do not follow the USB specs to the letter.
799 * These are the events on the bus when a hub is attached:
800 * Get device and config descriptors (see attach code)
801 * Get hub descriptor (see above)
804 * wait for power to become stable
805 * (all below happens in explore code)
807 * clear C_PORT_CONNECTION
810 * if device connected
816 * proceed with device attachment
819 /* XXX should check for none, individual, or ganged power? */
822 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
823 USB_EXTRA_POWER_UP_TIME);
825 for (x = 0; x != nports; x++) {
826 /* set up data structures */
827 struct usb_port *up = hub->ports + x;
829 up->device_index = 0;
833 /* check if port is removable */
834 if (!UHD_NOT_REMOV(&hubdesc, portno)) {
838 /* turn the power on */
839 err = usbd_req_set_port_feature(udev, NULL,
840 portno, UHF_PORT_POWER);
843 DPRINTFN(0, "port %d power on failed, %s\n",
844 portno, usbd_errstr(err));
846 DPRINTF("turn on port %d power\n",
849 /* wait for stable power */
850 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
853 device_printf(dev, "%d port%s with %d "
854 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
855 removable, udev->flags.self_powered ? "self" : "bus");
857 /* Start the interrupt endpoint, if any */
859 if (sc->sc_xfer[0] != NULL) {
860 mtx_lock(&sc->sc_mtx);
861 usbd_transfer_start(sc->sc_xfer[0]);
862 mtx_unlock(&sc->sc_mtx);
865 /* Enable automatic power save on all USB HUBs */
867 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
872 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
875 free(udev->hub, M_USBDEV);
879 mtx_destroy(&sc->sc_mtx);
885 * Called from process context when the hub is gone.
886 * Detach all devices on active ports.
889 uhub_detach(device_t dev)
891 struct uhub_softc *sc = device_get_softc(dev);
892 struct usb_hub *hub = sc->sc_udev->hub;
893 struct usb_device *child;
896 if (hub == NULL) { /* must be partially working */
900 /* Make sure interrupt transfer is gone. */
901 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
903 /* Detach all ports */
904 for (x = 0; x != hub->nports; x++) {
906 child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + x);
913 * Free USB device and all subdevices, if any.
915 usb_free_device(child, 0);
919 sc->sc_udev->hub = NULL;
921 mtx_destroy(&sc->sc_mtx);
927 uhub_suspend(device_t dev)
930 /* Sub-devices are not suspended here! */
935 uhub_resume(device_t dev)
938 /* Sub-devices are not resumed here! */
943 uhub_driver_added(device_t dev, driver_t *driver)
945 usb_needs_explore_all();
949 struct usb_device *udev;
955 uhub_find_iface_index(struct usb_hub *hub, device_t child,
956 struct hub_result *res)
958 struct usb_interface *iface;
959 struct usb_device *udev;
964 nports = hub->nports;
965 for (x = 0; x != nports; x++) {
966 udev = usb_bus_port_get_device(hub->hubudev->bus,
971 for (i = 0; i != USB_IFACE_MAX; i++) {
972 iface = usbd_get_iface(udev, i);
974 (iface->subdev == child)) {
975 res->iface_index = i;
982 res->iface_index = 0;
988 uhub_child_location_string(device_t parent, device_t child,
989 char *buf, size_t buflen)
991 struct uhub_softc *sc;
993 struct hub_result res;
995 if (!device_is_attached(parent)) {
1001 sc = device_get_softc(parent);
1002 hub = sc->sc_udev->hub;
1005 uhub_find_iface_index(hub, child, &res);
1007 DPRINTF("device not on hub\n");
1013 snprintf(buf, buflen, "port=%u interface=%u",
1014 res.portno, res.iface_index);
1022 uhub_child_pnpinfo_string(device_t parent, device_t child,
1023 char *buf, size_t buflen)
1025 struct uhub_softc *sc;
1026 struct usb_hub *hub;
1027 struct usb_interface *iface;
1028 struct hub_result res;
1030 if (!device_is_attached(parent)) {
1036 sc = device_get_softc(parent);
1037 hub = sc->sc_udev->hub;
1040 uhub_find_iface_index(hub, child, &res);
1042 DPRINTF("device not on hub\n");
1048 iface = usbd_get_iface(res.udev, res.iface_index);
1049 if (iface && iface->idesc) {
1050 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1051 "devclass=0x%02x devsubclass=0x%02x "
1054 "intclass=0x%02x intsubclass=0x%02x",
1055 UGETW(res.udev->ddesc.idVendor),
1056 UGETW(res.udev->ddesc.idProduct),
1057 res.udev->ddesc.bDeviceClass,
1058 res.udev->ddesc.bDeviceSubClass,
1060 UGETW(res.udev->ddesc.bcdDevice),
1061 iface->idesc->bInterfaceClass,
1062 iface->idesc->bInterfaceSubClass);
1076 * The USB Transaction Translator:
1077 * ===============================
1079 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1080 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1081 * USB transfers. To utilize bandwidth dynamically the "scatter and
1082 * gather" principle must be applied. This means that bandwidth must
1083 * be divided into equal parts of bandwidth. With regard to USB all
1084 * data is transferred in smaller packets with length
1085 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1088 * The bandwidth scheduler which I have implemented will simply pack
1089 * the USB transfers back to back until there is no more space in the
1090 * schedule. Out of the 8 microframes which the USB 2.0 standard
1091 * provides, only 6 are available for non-HIGH-speed devices. I have
1092 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1093 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1094 * this division, it is very difficult to allocate and free bandwidth
1097 * NOTE about the Transaction Translator in USB HUBs:
1099 * USB HUBs have a very simple Transaction Translator, that will
1100 * simply pipeline all the SPLIT transactions. That means that the
1101 * transactions will be executed in the order they are queued!
1105 /*------------------------------------------------------------------------*
1106 * usb_intr_find_best_slot
1109 * The best Transaction Translation slot for an interrupt endpoint.
1110 *------------------------------------------------------------------------*/
1112 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1113 uint8_t end, uint8_t mask)
1115 usb_size_t min = 0 - 1;
1123 /* find the last slot with lesser used bandwidth */
1125 for (x = start; x < end; x++) {
1129 /* compute sum of bandwidth */
1130 for (z = x; z < end; z++) {
1131 if (mask & (1U << (z - x)))
1135 /* check if the current multi-slot is more optimal */
1141 /* check if the mask is about to be shifted out */
1142 if (mask & (1U << (end - 1 - x)))
1148 /*------------------------------------------------------------------------*
1149 * usb_hs_bandwidth_adjust
1151 * This function will update the bandwith usage for the microframe
1152 * having index "slot" by "len" bytes. "len" can be negative. If the
1153 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1154 * the "slot" argument will be replaced by the slot having least used
1155 * bandwidth. The "mask" argument is used for multi-slot allocations.
1158 * The slot in which the bandwidth update was done: 0..7
1159 *------------------------------------------------------------------------*/
1161 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1162 uint8_t slot, uint8_t mask)
1164 struct usb_bus *bus = udev->bus;
1165 struct usb_hub *hub;
1166 enum usb_dev_speed speed;
1169 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1171 speed = usbd_get_speed(udev);
1175 case USB_SPEED_FULL:
1176 if (speed == USB_SPEED_LOW) {
1180 * The Host Controller Driver should have
1181 * performed checks so that the lookup
1182 * below does not result in a NULL pointer
1186 hub = udev->parent_hs_hub->hub;
1187 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1188 slot = usb_intr_find_best_slot(hub->uframe_usage,
1189 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1191 for (x = slot; x < 8; x++) {
1192 if (mask & (1U << (x - slot))) {
1193 hub->uframe_usage[x] += len;
1194 bus->uframe_usage[x] += len;
1199 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1200 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1201 USB_HS_MICRO_FRAMES_MAX, mask);
1203 for (x = slot; x < 8; x++) {
1204 if (mask & (1U << (x - slot))) {
1205 bus->uframe_usage[x] += len;
1213 /*------------------------------------------------------------------------*
1214 * usb_hs_bandwidth_alloc
1216 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1217 *------------------------------------------------------------------------*/
1219 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1221 struct usb_device *udev;
1226 udev = xfer->xroot->udev;
1228 if (udev->flags.usb_mode != USB_MODE_HOST)
1229 return; /* not supported */
1231 xfer->endpoint->refcount_bw++;
1232 if (xfer->endpoint->refcount_bw != 1)
1233 return; /* already allocated */
1235 speed = usbd_get_speed(udev);
1237 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1239 /* allocate a microframe slot */
1242 slot = usb_hs_bandwidth_adjust(udev,
1243 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1245 xfer->endpoint->usb_uframe = slot;
1246 xfer->endpoint->usb_smask = mask << slot;
1248 if ((speed != USB_SPEED_FULL) &&
1249 (speed != USB_SPEED_LOW)) {
1250 xfer->endpoint->usb_cmask = 0x00 ;
1252 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1256 case UE_ISOCHRONOUS:
1257 switch (usbd_xfer_get_fps_shift(xfer)) {
1272 /* allocate a microframe multi-slot */
1274 slot = usb_hs_bandwidth_adjust(udev,
1275 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1277 xfer->endpoint->usb_uframe = slot;
1278 xfer->endpoint->usb_cmask = 0;
1279 xfer->endpoint->usb_smask = mask << slot;
1283 xfer->endpoint->usb_uframe = 0;
1284 xfer->endpoint->usb_cmask = 0;
1285 xfer->endpoint->usb_smask = 0;
1289 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1290 xfer->endpoint->usb_uframe,
1291 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1294 /*------------------------------------------------------------------------*
1295 * usb_hs_bandwidth_free
1297 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1298 *------------------------------------------------------------------------*/
1300 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1302 struct usb_device *udev;
1306 udev = xfer->xroot->udev;
1308 if (udev->flags.usb_mode != USB_MODE_HOST)
1309 return; /* not supported */
1311 xfer->endpoint->refcount_bw--;
1312 if (xfer->endpoint->refcount_bw != 0)
1313 return; /* still allocated */
1315 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1317 case UE_ISOCHRONOUS:
1319 slot = xfer->endpoint->usb_uframe;
1320 mask = xfer->endpoint->usb_smask;
1322 /* free microframe slot(s): */
1323 usb_hs_bandwidth_adjust(udev,
1324 -xfer->max_frame_size, slot, mask >> slot);
1326 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1327 slot, mask >> slot);
1329 xfer->endpoint->usb_uframe = 0;
1330 xfer->endpoint->usb_cmask = 0;
1331 xfer->endpoint->usb_smask = 0;
1339 /*------------------------------------------------------------------------*
1340 * usbd_fs_isoc_schedule_init_sub
1342 * This function initialises an USB FULL speed isochronous schedule
1344 *------------------------------------------------------------------------*/
1345 #if USB_HAVE_TT_SUPPORT
1347 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1349 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1350 USB_FS_BYTES_PER_HS_UFRAME);
1351 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1352 fss->frame_slot = 0;
1356 /*------------------------------------------------------------------------*
1357 * usbd_fs_isoc_schedule_init_all
1359 * This function will reset the complete USB FULL speed isochronous
1360 * bandwidth schedule.
1361 *------------------------------------------------------------------------*/
1362 #if USB_HAVE_TT_SUPPORT
1364 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1366 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1368 while (fss != fss_end) {
1369 usbd_fs_isoc_schedule_init_sub(fss);
1375 /*------------------------------------------------------------------------*
1376 * usb_isoc_time_expand
1378 * This function will expand the time counter from 7-bit to 16-bit.
1381 * 16-bit isochronous time counter.
1382 *------------------------------------------------------------------------*/
1384 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1388 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1390 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1392 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1394 if (isoc_time_curr < rem) {
1395 /* the time counter wrapped around */
1396 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1398 /* update the remainder */
1400 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1401 bus->isoc_time_last |= isoc_time_curr;
1403 return (bus->isoc_time_last);
1406 /*------------------------------------------------------------------------*
1407 * usbd_fs_isoc_schedule_isoc_time_expand
1409 * This function does multiple things. First of all it will expand the
1410 * passed isochronous time, which is the return value. Then it will
1411 * store where the current FULL speed isochronous schedule is
1412 * positioned in time and where the end is. See "pp_start" and
1413 * "pp_end" arguments.
1416 * Expanded version of "isoc_time".
1418 * NOTE: This function depends on being called regularly with
1419 * intervals less than "USB_ISOC_TIME_MAX".
1420 *------------------------------------------------------------------------*/
1421 #if USB_HAVE_TT_SUPPORT
1423 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1424 struct usb_fs_isoc_schedule **pp_start,
1425 struct usb_fs_isoc_schedule **pp_end,
1428 struct usb_fs_isoc_schedule *fss_end;
1429 struct usb_fs_isoc_schedule *fss_a;
1430 struct usb_fs_isoc_schedule *fss_b;
1431 struct usb_hub *hs_hub;
1433 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1435 hs_hub = udev->parent_hs_hub->hub;
1437 if (hs_hub != NULL) {
1439 fss_a = hs_hub->fs_isoc_schedule +
1440 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1442 hs_hub->isoc_last_time = isoc_time;
1444 fss_b = hs_hub->fs_isoc_schedule +
1445 (isoc_time % USB_ISOC_TIME_MAX);
1447 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1449 *pp_start = hs_hub->fs_isoc_schedule;
1452 while (fss_a != fss_b) {
1453 if (fss_a == fss_end) {
1454 fss_a = hs_hub->fs_isoc_schedule;
1457 usbd_fs_isoc_schedule_init_sub(fss_a);
1470 /*------------------------------------------------------------------------*
1471 * usbd_fs_isoc_schedule_alloc
1473 * This function will allocate bandwidth for an isochronous FULL speed
1474 * transaction in the FULL speed schedule. The microframe slot where
1475 * the transaction should be started is stored in the byte pointed to
1476 * by "pstart". The "len" argument specifies the length of the
1477 * transaction in bytes.
1482 *------------------------------------------------------------------------*/
1483 #if USB_HAVE_TT_SUPPORT
1485 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1486 uint8_t *pstart, uint16_t len)
1488 uint8_t slot = fss->frame_slot;
1490 /* Compute overhead and bit-stuffing */
1497 if (len > fss->total_bytes) {
1498 *pstart = 0; /* set some dummy value */
1499 return (1); /* error */
1503 fss->total_bytes -= len;
1505 while (len >= fss->frame_bytes) {
1506 len -= fss->frame_bytes;
1507 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1511 fss->frame_bytes -= len;
1514 return (0); /* success */
1518 /*------------------------------------------------------------------------*
1519 * usb_bus_port_get_device
1521 * This function is NULL safe.
1522 *------------------------------------------------------------------------*/
1524 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1526 if ((bus == NULL) || (up == NULL)) {
1530 if (up->device_index == 0) {
1534 return (bus->devices[up->device_index]);
1537 /*------------------------------------------------------------------------*
1538 * usb_bus_port_set_device
1540 * This function is NULL safe.
1541 *------------------------------------------------------------------------*/
1543 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1544 struct usb_device *udev, uint8_t device_index)
1551 * There is only one case where we don't
1552 * have an USB port, and that is the Root Hub!
1556 up->device_index = device_index;
1558 device_index = up->device_index;
1559 up->device_index = 0;
1563 * Make relationships to our new device
1565 if (device_index != 0) {
1567 mtx_lock(&usb_ref_lock);
1569 bus->devices[device_index] = udev;
1571 mtx_unlock(&usb_ref_lock);
1577 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1580 /*------------------------------------------------------------------------*
1583 * This functions is called when the USB event thread needs to run.
1584 *------------------------------------------------------------------------*/
1586 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1593 DPRINTF("No bus pointer!\n");
1596 if ((bus->devices == NULL) ||
1597 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1598 DPRINTF("No root HUB\n");
1601 if (mtx_owned(&bus->bus_mtx)) {
1610 if (usb_proc_msignal(&bus->explore_proc,
1611 &bus->explore_msg[0], &bus->explore_msg[1])) {
1615 USB_BUS_UNLOCK(bus);
1619 /*------------------------------------------------------------------------*
1620 * usb_needs_explore_all
1622 * This function is called whenever a new driver is loaded and will
1623 * cause that all USB busses are re-explored.
1624 *------------------------------------------------------------------------*/
1626 usb_needs_explore_all(void)
1628 struct usb_bus *bus;
1635 dc = usb_devclass_ptr;
1637 DPRINTFN(0, "no devclass\n");
1641 * Explore all USB busses in parallell.
1643 max = devclass_get_maxunit(dc);
1645 dev = devclass_get_device(dc, max);
1647 bus = device_get_softc(dev);
1649 usb_needs_explore(bus, 1);
1656 /*------------------------------------------------------------------------*
1657 * usb_bus_power_update
1659 * This function will ensure that all USB devices on the given bus are
1660 * properly suspended or resumed according to the device transfer
1662 *------------------------------------------------------------------------*/
1665 usb_bus_power_update(struct usb_bus *bus)
1667 usb_needs_explore(bus, 0 /* no probe */ );
1671 /*------------------------------------------------------------------------*
1672 * usbd_transfer_power_ref
1674 * This function will modify the power save reference counts and
1675 * wakeup the USB device associated with the given USB transfer, if
1677 *------------------------------------------------------------------------*/
1680 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1682 static const usb_power_mask_t power_mask[4] = {
1683 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1684 [UE_BULK] = USB_HW_POWER_BULK,
1685 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1686 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1688 struct usb_device *udev;
1689 uint8_t needs_explore;
1690 uint8_t needs_hw_power;
1693 udev = xfer->xroot->udev;
1695 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1696 /* no power save for root HUB */
1699 USB_BUS_LOCK(udev->bus);
1701 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1703 udev->pwr_save.last_xfer_time = ticks;
1704 udev->pwr_save.type_refs[xfer_type] += val;
1706 if (xfer->flags_int.control_xfr) {
1707 udev->pwr_save.read_refs += val;
1708 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1710 * It is not allowed to suspend during a
1713 udev->pwr_save.write_refs += val;
1715 } else if (USB_GET_DATA_ISREAD(xfer)) {
1716 udev->pwr_save.read_refs += val;
1718 udev->pwr_save.write_refs += val;
1722 if (udev->flags.self_suspended)
1723 needs_explore = usb_peer_should_wakeup(udev);
1727 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1728 DPRINTF("Adding type %u to power state\n", xfer_type);
1729 udev->bus->hw_power_state |= power_mask[xfer_type];
1739 USB_BUS_UNLOCK(udev->bus);
1741 if (needs_explore) {
1742 DPRINTF("update\n");
1743 usb_bus_power_update(udev->bus);
1744 } else if (needs_hw_power) {
1745 DPRINTF("needs power\n");
1746 if (udev->bus->methods->set_hw_power != NULL) {
1747 (udev->bus->methods->set_hw_power) (udev->bus);
1753 /*------------------------------------------------------------------------*
1754 * usb_peer_should_wakeup
1756 * This function returns non-zero if the current device should wake up.
1757 *------------------------------------------------------------------------*/
1759 usb_peer_should_wakeup(struct usb_device *udev)
1761 return ((udev->power_mode == USB_POWER_MODE_ON) ||
1762 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1763 (udev->pwr_save.write_refs != 0) ||
1764 ((udev->pwr_save.read_refs != 0) &&
1765 (udev->flags.usb_mode == USB_MODE_HOST) &&
1766 (usb_peer_can_wakeup(udev) == 0)));
1769 /*------------------------------------------------------------------------*
1772 * This function implements the USB power daemon and is called
1773 * regularly from the USB explore thread.
1774 *------------------------------------------------------------------------*/
1777 usb_bus_powerd(struct usb_bus *bus)
1779 struct usb_device *udev;
1782 usb_ticks_t mintime;
1783 usb_size_t type_refs[5];
1786 limit = usb_power_timeout;
1789 else if (limit > 255)
1794 DPRINTF("bus=%p\n", bus);
1799 * The root HUB device is never suspended
1800 * and we simply skip it.
1802 for (x = USB_ROOT_HUB_ADDR + 1;
1803 x != bus->devices_max; x++) {
1805 udev = bus->devices[x];
1809 temp = ticks - udev->pwr_save.last_xfer_time;
1811 if (usb_peer_should_wakeup(udev)) {
1812 /* check if we are suspended */
1813 if (udev->flags.self_suspended != 0) {
1814 USB_BUS_UNLOCK(bus);
1815 usb_dev_resume_peer(udev);
1818 } else if ((temp >= limit) &&
1819 (udev->flags.usb_mode == USB_MODE_HOST) &&
1820 (udev->flags.self_suspended == 0)) {
1821 /* try to do suspend */
1823 USB_BUS_UNLOCK(bus);
1824 usb_dev_suspend_peer(udev);
1829 /* reset counters */
1838 /* Re-loop all the devices to get the actual state */
1840 for (x = USB_ROOT_HUB_ADDR + 1;
1841 x != bus->devices_max; x++) {
1843 udev = bus->devices[x];
1847 /* we found a non-Root-Hub USB device */
1850 /* "last_xfer_time" can be updated by a resume */
1851 temp = ticks - udev->pwr_save.last_xfer_time;
1854 * Compute minimum time since last transfer for the complete
1860 if (udev->flags.self_suspended == 0) {
1861 type_refs[0] += udev->pwr_save.type_refs[0];
1862 type_refs[1] += udev->pwr_save.type_refs[1];
1863 type_refs[2] += udev->pwr_save.type_refs[2];
1864 type_refs[3] += udev->pwr_save.type_refs[3];
1868 if (mintime >= (1 * hz)) {
1869 /* recompute power masks */
1870 DPRINTF("Recomputing power masks\n");
1871 bus->hw_power_state = 0;
1872 if (type_refs[UE_CONTROL] != 0)
1873 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1874 if (type_refs[UE_BULK] != 0)
1875 bus->hw_power_state |= USB_HW_POWER_BULK;
1876 if (type_refs[UE_INTERRUPT] != 0)
1877 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1878 if (type_refs[UE_ISOCHRONOUS] != 0)
1879 bus->hw_power_state |= USB_HW_POWER_ISOC;
1880 if (type_refs[4] != 0)
1881 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1883 USB_BUS_UNLOCK(bus);
1885 if (bus->methods->set_hw_power != NULL) {
1886 /* always update hardware power! */
1887 (bus->methods->set_hw_power) (bus);
1893 /*------------------------------------------------------------------------*
1894 * usb_dev_resume_peer
1896 * This function will resume an USB peer and do the required USB
1897 * signalling to get an USB device out of the suspended state.
1898 *------------------------------------------------------------------------*/
1900 usb_dev_resume_peer(struct usb_device *udev)
1902 struct usb_bus *bus;
1909 /* check if already resumed */
1910 if (udev->flags.self_suspended == 0)
1913 /* we need a parent HUB to do resume */
1914 if (udev->parent_hub == NULL)
1917 DPRINTF("udev=%p\n", udev);
1919 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1920 (udev->flags.remote_wakeup == 0)) {
1922 * If the host did not set the remote wakeup feature, we can
1923 * not wake it up either!
1925 DPRINTF("remote wakeup is not set!\n");
1928 /* get bus pointer */
1931 /* resume parent hub first */
1932 usb_dev_resume_peer(udev->parent_hub);
1934 /* reduce chance of instant resume failure by waiting a little bit */
1935 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
1937 /* resume current port (Valid in Host and Device Mode) */
1938 err = usbd_req_clear_port_feature(udev->parent_hub,
1939 NULL, udev->port_no, UHF_PORT_SUSPEND);
1941 DPRINTFN(0, "Resuming port failed\n");
1944 /* resume settle time */
1945 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1947 if (bus->methods->device_resume != NULL) {
1948 /* resume USB device on the USB controller */
1949 (bus->methods->device_resume) (udev);
1952 /* set that this device is now resumed */
1953 udev->flags.self_suspended = 0;
1955 /* make sure that we don't go into suspend right away */
1956 udev->pwr_save.last_xfer_time = ticks;
1958 /* make sure the needed power masks are on */
1959 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1960 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1961 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1962 bus->hw_power_state |= USB_HW_POWER_BULK;
1963 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1964 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1965 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1966 bus->hw_power_state |= USB_HW_POWER_ISOC;
1968 USB_BUS_UNLOCK(bus);
1970 if (bus->methods->set_hw_power != NULL) {
1971 /* always update hardware power! */
1972 (bus->methods->set_hw_power) (bus);
1977 /* notify all sub-devices about resume */
1978 err = usb_suspend_resume(udev, 0);
1980 usbd_sr_unlock(udev);
1982 /* check if peer has wakeup capability */
1983 if (usb_peer_can_wakeup(udev)) {
1984 /* clear remote wakeup */
1985 err = usbd_req_clear_device_feature(udev,
1986 NULL, UF_DEVICE_REMOTE_WAKEUP);
1988 DPRINTFN(0, "Clearing device "
1989 "remote wakeup failed: %s\n",
1996 /*------------------------------------------------------------------------*
1997 * usb_dev_suspend_peer
1999 * This function will suspend an USB peer and do the required USB
2000 * signalling to get an USB device into the suspended state.
2001 *------------------------------------------------------------------------*/
2003 usb_dev_suspend_peer(struct usb_device *udev)
2005 struct usb_device *child;
2015 /* check if already suspended */
2016 if (udev->flags.self_suspended)
2019 /* we need a parent HUB to do suspend */
2020 if (udev->parent_hub == NULL)
2023 DPRINTF("udev=%p\n", udev);
2025 /* check if the current device is a HUB */
2026 if (udev->hub != NULL) {
2027 nports = udev->hub->nports;
2029 /* check if all devices on the HUB are suspended */
2030 for (x = 0; x != nports; x++) {
2032 child = usb_bus_port_get_device(udev->bus,
2033 udev->hub->ports + x);
2038 if (child->flags.self_suspended)
2041 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2046 USB_BUS_LOCK(udev->bus);
2048 * Checking for suspend condition and setting suspended bit
2051 err = usb_peer_should_wakeup(udev);
2054 * Set that this device is suspended. This variable
2055 * must be set before calling USB controller suspend
2058 udev->flags.self_suspended = 1;
2060 USB_BUS_UNLOCK(udev->bus);
2063 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2064 /* resume parent HUB first */
2065 usb_dev_resume_peer(udev->parent_hub);
2067 /* reduce chance of instant resume failure by waiting a little bit */
2068 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2070 /* resume current port (Valid in Host and Device Mode) */
2071 err = usbd_req_clear_port_feature(udev->parent_hub,
2072 NULL, udev->port_no, UHF_PORT_SUSPEND);
2074 /* resume settle time */
2075 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2077 DPRINTF("Suspend was cancelled!\n");
2083 /* notify all sub-devices about suspend */
2084 err = usb_suspend_resume(udev, 1);
2086 usbd_sr_unlock(udev);
2088 if (usb_peer_can_wakeup(udev)) {
2089 /* allow device to do remote wakeup */
2090 err = usbd_req_set_device_feature(udev,
2091 NULL, UF_DEVICE_REMOTE_WAKEUP);
2093 DPRINTFN(0, "Setting device "
2094 "remote wakeup failed\n");
2098 if (udev->bus->methods->device_suspend != NULL) {
2101 /* suspend device on the USB controller */
2102 (udev->bus->methods->device_suspend) (udev);
2105 temp = usbd_get_dma_delay(udev->bus);
2106 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2109 /* suspend current port */
2110 err = usbd_req_set_port_feature(udev->parent_hub,
2111 NULL, udev->port_no, UHF_PORT_SUSPEND);
2113 DPRINTFN(0, "Suspending port failed\n");
2117 udev = udev->parent_hub;
2121 /*------------------------------------------------------------------------*
2122 * usbd_set_power_mode
2124 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2126 *------------------------------------------------------------------------*/
2128 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2130 /* filter input argument */
2131 if ((power_mode != USB_POWER_MODE_ON) &&
2132 (power_mode != USB_POWER_MODE_OFF)) {
2133 power_mode = USB_POWER_MODE_SAVE;
2135 udev->power_mode = power_mode; /* update copy of power mode */
2138 usb_bus_power_update(udev->bus);