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, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1014 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1015 res.portno, device_get_unit(res.udev->bus->bdev),
1016 res.udev->device_index, res.iface_index);
1024 uhub_child_pnpinfo_string(device_t parent, device_t child,
1025 char *buf, size_t buflen)
1027 struct uhub_softc *sc;
1028 struct usb_hub *hub;
1029 struct usb_interface *iface;
1030 struct hub_result res;
1032 if (!device_is_attached(parent)) {
1038 sc = device_get_softc(parent);
1039 hub = sc->sc_udev->hub;
1042 uhub_find_iface_index(hub, child, &res);
1044 DPRINTF("device not on hub\n");
1050 iface = usbd_get_iface(res.udev, res.iface_index);
1051 if (iface && iface->idesc) {
1052 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1053 "devclass=0x%02x devsubclass=0x%02x "
1056 "intclass=0x%02x intsubclass=0x%02x",
1057 UGETW(res.udev->ddesc.idVendor),
1058 UGETW(res.udev->ddesc.idProduct),
1059 res.udev->ddesc.bDeviceClass,
1060 res.udev->ddesc.bDeviceSubClass,
1062 UGETW(res.udev->ddesc.bcdDevice),
1063 iface->idesc->bInterfaceClass,
1064 iface->idesc->bInterfaceSubClass);
1078 * The USB Transaction Translator:
1079 * ===============================
1081 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1082 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1083 * USB transfers. To utilize bandwidth dynamically the "scatter and
1084 * gather" principle must be applied. This means that bandwidth must
1085 * be divided into equal parts of bandwidth. With regard to USB all
1086 * data is transferred in smaller packets with length
1087 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1090 * The bandwidth scheduler which I have implemented will simply pack
1091 * the USB transfers back to back until there is no more space in the
1092 * schedule. Out of the 8 microframes which the USB 2.0 standard
1093 * provides, only 6 are available for non-HIGH-speed devices. I have
1094 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1095 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1096 * this division, it is very difficult to allocate and free bandwidth
1099 * NOTE about the Transaction Translator in USB HUBs:
1101 * USB HUBs have a very simple Transaction Translator, that will
1102 * simply pipeline all the SPLIT transactions. That means that the
1103 * transactions will be executed in the order they are queued!
1107 /*------------------------------------------------------------------------*
1108 * usb_intr_find_best_slot
1111 * The best Transaction Translation slot for an interrupt endpoint.
1112 *------------------------------------------------------------------------*/
1114 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1115 uint8_t end, uint8_t mask)
1117 usb_size_t min = 0 - 1;
1125 /* find the last slot with lesser used bandwidth */
1127 for (x = start; x < end; x++) {
1131 /* compute sum of bandwidth */
1132 for (z = x; z < end; z++) {
1133 if (mask & (1U << (z - x)))
1137 /* check if the current multi-slot is more optimal */
1143 /* check if the mask is about to be shifted out */
1144 if (mask & (1U << (end - 1 - x)))
1150 /*------------------------------------------------------------------------*
1151 * usb_hs_bandwidth_adjust
1153 * This function will update the bandwith usage for the microframe
1154 * having index "slot" by "len" bytes. "len" can be negative. If the
1155 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1156 * the "slot" argument will be replaced by the slot having least used
1157 * bandwidth. The "mask" argument is used for multi-slot allocations.
1160 * The slot in which the bandwidth update was done: 0..7
1161 *------------------------------------------------------------------------*/
1163 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1164 uint8_t slot, uint8_t mask)
1166 struct usb_bus *bus = udev->bus;
1167 struct usb_hub *hub;
1168 enum usb_dev_speed speed;
1171 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1173 speed = usbd_get_speed(udev);
1177 case USB_SPEED_FULL:
1178 if (speed == USB_SPEED_LOW) {
1182 * The Host Controller Driver should have
1183 * performed checks so that the lookup
1184 * below does not result in a NULL pointer
1188 hub = udev->parent_hs_hub->hub;
1189 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1190 slot = usb_intr_find_best_slot(hub->uframe_usage,
1191 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1193 for (x = slot; x < 8; x++) {
1194 if (mask & (1U << (x - slot))) {
1195 hub->uframe_usage[x] += len;
1196 bus->uframe_usage[x] += len;
1201 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1202 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1203 USB_HS_MICRO_FRAMES_MAX, mask);
1205 for (x = slot; x < 8; x++) {
1206 if (mask & (1U << (x - slot))) {
1207 bus->uframe_usage[x] += len;
1215 /*------------------------------------------------------------------------*
1216 * usb_hs_bandwidth_alloc
1218 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1219 *------------------------------------------------------------------------*/
1221 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1223 struct usb_device *udev;
1228 udev = xfer->xroot->udev;
1230 if (udev->flags.usb_mode != USB_MODE_HOST)
1231 return; /* not supported */
1233 xfer->endpoint->refcount_bw++;
1234 if (xfer->endpoint->refcount_bw != 1)
1235 return; /* already allocated */
1237 speed = usbd_get_speed(udev);
1239 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1241 /* allocate a microframe slot */
1244 slot = usb_hs_bandwidth_adjust(udev,
1245 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1247 xfer->endpoint->usb_uframe = slot;
1248 xfer->endpoint->usb_smask = mask << slot;
1250 if ((speed != USB_SPEED_FULL) &&
1251 (speed != USB_SPEED_LOW)) {
1252 xfer->endpoint->usb_cmask = 0x00 ;
1254 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1258 case UE_ISOCHRONOUS:
1259 switch (usbd_xfer_get_fps_shift(xfer)) {
1274 /* allocate a microframe multi-slot */
1276 slot = usb_hs_bandwidth_adjust(udev,
1277 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1279 xfer->endpoint->usb_uframe = slot;
1280 xfer->endpoint->usb_cmask = 0;
1281 xfer->endpoint->usb_smask = mask << slot;
1285 xfer->endpoint->usb_uframe = 0;
1286 xfer->endpoint->usb_cmask = 0;
1287 xfer->endpoint->usb_smask = 0;
1291 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1292 xfer->endpoint->usb_uframe,
1293 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1296 /*------------------------------------------------------------------------*
1297 * usb_hs_bandwidth_free
1299 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1300 *------------------------------------------------------------------------*/
1302 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1304 struct usb_device *udev;
1308 udev = xfer->xroot->udev;
1310 if (udev->flags.usb_mode != USB_MODE_HOST)
1311 return; /* not supported */
1313 xfer->endpoint->refcount_bw--;
1314 if (xfer->endpoint->refcount_bw != 0)
1315 return; /* still allocated */
1317 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1319 case UE_ISOCHRONOUS:
1321 slot = xfer->endpoint->usb_uframe;
1322 mask = xfer->endpoint->usb_smask;
1324 /* free microframe slot(s): */
1325 usb_hs_bandwidth_adjust(udev,
1326 -xfer->max_frame_size, slot, mask >> slot);
1328 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1329 slot, mask >> slot);
1331 xfer->endpoint->usb_uframe = 0;
1332 xfer->endpoint->usb_cmask = 0;
1333 xfer->endpoint->usb_smask = 0;
1341 /*------------------------------------------------------------------------*
1342 * usbd_fs_isoc_schedule_init_sub
1344 * This function initialises an USB FULL speed isochronous schedule
1346 *------------------------------------------------------------------------*/
1347 #if USB_HAVE_TT_SUPPORT
1349 usbd_fs_isoc_schedule_init_sub(struct usb_fs_isoc_schedule *fss)
1351 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX *
1352 USB_FS_BYTES_PER_HS_UFRAME);
1353 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME);
1354 fss->frame_slot = 0;
1358 /*------------------------------------------------------------------------*
1359 * usbd_fs_isoc_schedule_init_all
1361 * This function will reset the complete USB FULL speed isochronous
1362 * bandwidth schedule.
1363 *------------------------------------------------------------------------*/
1364 #if USB_HAVE_TT_SUPPORT
1366 usbd_fs_isoc_schedule_init_all(struct usb_fs_isoc_schedule *fss)
1368 struct usb_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX;
1370 while (fss != fss_end) {
1371 usbd_fs_isoc_schedule_init_sub(fss);
1377 /*------------------------------------------------------------------------*
1378 * usb_isoc_time_expand
1380 * This function will expand the time counter from 7-bit to 16-bit.
1383 * 16-bit isochronous time counter.
1384 *------------------------------------------------------------------------*/
1386 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1390 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1392 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1394 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1396 if (isoc_time_curr < rem) {
1397 /* the time counter wrapped around */
1398 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1400 /* update the remainder */
1402 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1403 bus->isoc_time_last |= isoc_time_curr;
1405 return (bus->isoc_time_last);
1408 /*------------------------------------------------------------------------*
1409 * usbd_fs_isoc_schedule_isoc_time_expand
1411 * This function does multiple things. First of all it will expand the
1412 * passed isochronous time, which is the return value. Then it will
1413 * store where the current FULL speed isochronous schedule is
1414 * positioned in time and where the end is. See "pp_start" and
1415 * "pp_end" arguments.
1418 * Expanded version of "isoc_time".
1420 * NOTE: This function depends on being called regularly with
1421 * intervals less than "USB_ISOC_TIME_MAX".
1422 *------------------------------------------------------------------------*/
1423 #if USB_HAVE_TT_SUPPORT
1425 usbd_fs_isoc_schedule_isoc_time_expand(struct usb_device *udev,
1426 struct usb_fs_isoc_schedule **pp_start,
1427 struct usb_fs_isoc_schedule **pp_end,
1430 struct usb_fs_isoc_schedule *fss_end;
1431 struct usb_fs_isoc_schedule *fss_a;
1432 struct usb_fs_isoc_schedule *fss_b;
1433 struct usb_hub *hs_hub;
1435 isoc_time = usb_isoc_time_expand(udev->bus, isoc_time);
1437 hs_hub = udev->parent_hs_hub->hub;
1439 if (hs_hub != NULL) {
1441 fss_a = hs_hub->fs_isoc_schedule +
1442 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX);
1444 hs_hub->isoc_last_time = isoc_time;
1446 fss_b = hs_hub->fs_isoc_schedule +
1447 (isoc_time % USB_ISOC_TIME_MAX);
1449 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX;
1451 *pp_start = hs_hub->fs_isoc_schedule;
1454 while (fss_a != fss_b) {
1455 if (fss_a == fss_end) {
1456 fss_a = hs_hub->fs_isoc_schedule;
1459 usbd_fs_isoc_schedule_init_sub(fss_a);
1472 /*------------------------------------------------------------------------*
1473 * usbd_fs_isoc_schedule_alloc
1475 * This function will allocate bandwidth for an isochronous FULL speed
1476 * transaction in the FULL speed schedule. The microframe slot where
1477 * the transaction should be started is stored in the byte pointed to
1478 * by "pstart". The "len" argument specifies the length of the
1479 * transaction in bytes.
1484 *------------------------------------------------------------------------*/
1485 #if USB_HAVE_TT_SUPPORT
1487 usbd_fs_isoc_schedule_alloc(struct usb_fs_isoc_schedule *fss,
1488 uint8_t *pstart, uint16_t len)
1490 uint8_t slot = fss->frame_slot;
1492 /* Compute overhead and bit-stuffing */
1499 if (len > fss->total_bytes) {
1500 *pstart = 0; /* set some dummy value */
1501 return (1); /* error */
1505 fss->total_bytes -= len;
1507 while (len >= fss->frame_bytes) {
1508 len -= fss->frame_bytes;
1509 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME;
1513 fss->frame_bytes -= len;
1516 return (0); /* success */
1520 /*------------------------------------------------------------------------*
1521 * usb_bus_port_get_device
1523 * This function is NULL safe.
1524 *------------------------------------------------------------------------*/
1526 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
1528 if ((bus == NULL) || (up == NULL)) {
1532 if (up->device_index == 0) {
1536 return (bus->devices[up->device_index]);
1539 /*------------------------------------------------------------------------*
1540 * usb_bus_port_set_device
1542 * This function is NULL safe.
1543 *------------------------------------------------------------------------*/
1545 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
1546 struct usb_device *udev, uint8_t device_index)
1553 * There is only one case where we don't
1554 * have an USB port, and that is the Root Hub!
1558 up->device_index = device_index;
1560 device_index = up->device_index;
1561 up->device_index = 0;
1565 * Make relationships to our new device
1567 if (device_index != 0) {
1569 mtx_lock(&usb_ref_lock);
1571 bus->devices[device_index] = udev;
1573 mtx_unlock(&usb_ref_lock);
1579 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
1582 /*------------------------------------------------------------------------*
1585 * This functions is called when the USB event thread needs to run.
1586 *------------------------------------------------------------------------*/
1588 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
1595 DPRINTF("No bus pointer!\n");
1598 if ((bus->devices == NULL) ||
1599 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
1600 DPRINTF("No root HUB\n");
1603 if (mtx_owned(&bus->bus_mtx)) {
1612 if (usb_proc_msignal(&bus->explore_proc,
1613 &bus->explore_msg[0], &bus->explore_msg[1])) {
1617 USB_BUS_UNLOCK(bus);
1621 /*------------------------------------------------------------------------*
1622 * usb_needs_explore_all
1624 * This function is called whenever a new driver is loaded and will
1625 * cause that all USB busses are re-explored.
1626 *------------------------------------------------------------------------*/
1628 usb_needs_explore_all(void)
1630 struct usb_bus *bus;
1637 dc = usb_devclass_ptr;
1639 DPRINTFN(0, "no devclass\n");
1643 * Explore all USB busses in parallell.
1645 max = devclass_get_maxunit(dc);
1647 dev = devclass_get_device(dc, max);
1649 bus = device_get_softc(dev);
1651 usb_needs_explore(bus, 1);
1658 /*------------------------------------------------------------------------*
1659 * usb_bus_power_update
1661 * This function will ensure that all USB devices on the given bus are
1662 * properly suspended or resumed according to the device transfer
1664 *------------------------------------------------------------------------*/
1667 usb_bus_power_update(struct usb_bus *bus)
1669 usb_needs_explore(bus, 0 /* no probe */ );
1673 /*------------------------------------------------------------------------*
1674 * usbd_transfer_power_ref
1676 * This function will modify the power save reference counts and
1677 * wakeup the USB device associated with the given USB transfer, if
1679 *------------------------------------------------------------------------*/
1682 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
1684 static const usb_power_mask_t power_mask[4] = {
1685 [UE_CONTROL] = USB_HW_POWER_CONTROL,
1686 [UE_BULK] = USB_HW_POWER_BULK,
1687 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
1688 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
1690 struct usb_device *udev;
1691 uint8_t needs_explore;
1692 uint8_t needs_hw_power;
1695 udev = xfer->xroot->udev;
1697 if (udev->device_index == USB_ROOT_HUB_ADDR) {
1698 /* no power save for root HUB */
1701 USB_BUS_LOCK(udev->bus);
1703 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
1705 udev->pwr_save.last_xfer_time = ticks;
1706 udev->pwr_save.type_refs[xfer_type] += val;
1708 if (xfer->flags_int.control_xfr) {
1709 udev->pwr_save.read_refs += val;
1710 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
1712 * It is not allowed to suspend during a
1715 udev->pwr_save.write_refs += val;
1717 } else if (USB_GET_DATA_ISREAD(xfer)) {
1718 udev->pwr_save.read_refs += val;
1720 udev->pwr_save.write_refs += val;
1724 if (udev->flags.self_suspended)
1725 needs_explore = usb_peer_should_wakeup(udev);
1729 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
1730 DPRINTF("Adding type %u to power state\n", xfer_type);
1731 udev->bus->hw_power_state |= power_mask[xfer_type];
1741 USB_BUS_UNLOCK(udev->bus);
1743 if (needs_explore) {
1744 DPRINTF("update\n");
1745 usb_bus_power_update(udev->bus);
1746 } else if (needs_hw_power) {
1747 DPRINTF("needs power\n");
1748 if (udev->bus->methods->set_hw_power != NULL) {
1749 (udev->bus->methods->set_hw_power) (udev->bus);
1755 /*------------------------------------------------------------------------*
1756 * usb_peer_should_wakeup
1758 * This function returns non-zero if the current device should wake up.
1759 *------------------------------------------------------------------------*/
1761 usb_peer_should_wakeup(struct usb_device *udev)
1763 return ((udev->power_mode == USB_POWER_MODE_ON) ||
1764 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
1765 (udev->pwr_save.write_refs != 0) ||
1766 ((udev->pwr_save.read_refs != 0) &&
1767 (udev->flags.usb_mode == USB_MODE_HOST) &&
1768 (usb_peer_can_wakeup(udev) == 0)));
1771 /*------------------------------------------------------------------------*
1774 * This function implements the USB power daemon and is called
1775 * regularly from the USB explore thread.
1776 *------------------------------------------------------------------------*/
1779 usb_bus_powerd(struct usb_bus *bus)
1781 struct usb_device *udev;
1784 usb_ticks_t mintime;
1785 usb_size_t type_refs[5];
1788 limit = usb_power_timeout;
1791 else if (limit > 255)
1796 DPRINTF("bus=%p\n", bus);
1801 * The root HUB device is never suspended
1802 * and we simply skip it.
1804 for (x = USB_ROOT_HUB_ADDR + 1;
1805 x != bus->devices_max; x++) {
1807 udev = bus->devices[x];
1811 temp = ticks - udev->pwr_save.last_xfer_time;
1813 if (usb_peer_should_wakeup(udev)) {
1814 /* check if we are suspended */
1815 if (udev->flags.self_suspended != 0) {
1816 USB_BUS_UNLOCK(bus);
1817 usb_dev_resume_peer(udev);
1820 } else if ((temp >= limit) &&
1821 (udev->flags.usb_mode == USB_MODE_HOST) &&
1822 (udev->flags.self_suspended == 0)) {
1823 /* try to do suspend */
1825 USB_BUS_UNLOCK(bus);
1826 usb_dev_suspend_peer(udev);
1831 /* reset counters */
1840 /* Re-loop all the devices to get the actual state */
1842 for (x = USB_ROOT_HUB_ADDR + 1;
1843 x != bus->devices_max; x++) {
1845 udev = bus->devices[x];
1849 /* we found a non-Root-Hub USB device */
1852 /* "last_xfer_time" can be updated by a resume */
1853 temp = ticks - udev->pwr_save.last_xfer_time;
1856 * Compute minimum time since last transfer for the complete
1862 if (udev->flags.self_suspended == 0) {
1863 type_refs[0] += udev->pwr_save.type_refs[0];
1864 type_refs[1] += udev->pwr_save.type_refs[1];
1865 type_refs[2] += udev->pwr_save.type_refs[2];
1866 type_refs[3] += udev->pwr_save.type_refs[3];
1870 if (mintime >= (1 * hz)) {
1871 /* recompute power masks */
1872 DPRINTF("Recomputing power masks\n");
1873 bus->hw_power_state = 0;
1874 if (type_refs[UE_CONTROL] != 0)
1875 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1876 if (type_refs[UE_BULK] != 0)
1877 bus->hw_power_state |= USB_HW_POWER_BULK;
1878 if (type_refs[UE_INTERRUPT] != 0)
1879 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1880 if (type_refs[UE_ISOCHRONOUS] != 0)
1881 bus->hw_power_state |= USB_HW_POWER_ISOC;
1882 if (type_refs[4] != 0)
1883 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
1885 USB_BUS_UNLOCK(bus);
1887 if (bus->methods->set_hw_power != NULL) {
1888 /* always update hardware power! */
1889 (bus->methods->set_hw_power) (bus);
1895 /*------------------------------------------------------------------------*
1896 * usb_dev_resume_peer
1898 * This function will resume an USB peer and do the required USB
1899 * signalling to get an USB device out of the suspended state.
1900 *------------------------------------------------------------------------*/
1902 usb_dev_resume_peer(struct usb_device *udev)
1904 struct usb_bus *bus;
1911 /* check if already resumed */
1912 if (udev->flags.self_suspended == 0)
1915 /* we need a parent HUB to do resume */
1916 if (udev->parent_hub == NULL)
1919 DPRINTF("udev=%p\n", udev);
1921 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
1922 (udev->flags.remote_wakeup == 0)) {
1924 * If the host did not set the remote wakeup feature, we can
1925 * not wake it up either!
1927 DPRINTF("remote wakeup is not set!\n");
1930 /* get bus pointer */
1933 /* resume parent hub first */
1934 usb_dev_resume_peer(udev->parent_hub);
1936 /* reduce chance of instant resume failure by waiting a little bit */
1937 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
1939 /* resume current port (Valid in Host and Device Mode) */
1940 err = usbd_req_clear_port_feature(udev->parent_hub,
1941 NULL, udev->port_no, UHF_PORT_SUSPEND);
1943 DPRINTFN(0, "Resuming port failed\n");
1946 /* resume settle time */
1947 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
1949 if (bus->methods->device_resume != NULL) {
1950 /* resume USB device on the USB controller */
1951 (bus->methods->device_resume) (udev);
1954 /* set that this device is now resumed */
1955 udev->flags.self_suspended = 0;
1957 /* make sure that we don't go into suspend right away */
1958 udev->pwr_save.last_xfer_time = ticks;
1960 /* make sure the needed power masks are on */
1961 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
1962 bus->hw_power_state |= USB_HW_POWER_CONTROL;
1963 if (udev->pwr_save.type_refs[UE_BULK] != 0)
1964 bus->hw_power_state |= USB_HW_POWER_BULK;
1965 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
1966 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
1967 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
1968 bus->hw_power_state |= USB_HW_POWER_ISOC;
1970 USB_BUS_UNLOCK(bus);
1972 if (bus->methods->set_hw_power != NULL) {
1973 /* always update hardware power! */
1974 (bus->methods->set_hw_power) (bus);
1979 /* notify all sub-devices about resume */
1980 err = usb_suspend_resume(udev, 0);
1982 usbd_sr_unlock(udev);
1984 /* check if peer has wakeup capability */
1985 if (usb_peer_can_wakeup(udev)) {
1986 /* clear remote wakeup */
1987 err = usbd_req_clear_device_feature(udev,
1988 NULL, UF_DEVICE_REMOTE_WAKEUP);
1990 DPRINTFN(0, "Clearing device "
1991 "remote wakeup failed: %s\n",
1998 /*------------------------------------------------------------------------*
1999 * usb_dev_suspend_peer
2001 * This function will suspend an USB peer and do the required USB
2002 * signalling to get an USB device into the suspended state.
2003 *------------------------------------------------------------------------*/
2005 usb_dev_suspend_peer(struct usb_device *udev)
2007 struct usb_device *child;
2017 /* check if already suspended */
2018 if (udev->flags.self_suspended)
2021 /* we need a parent HUB to do suspend */
2022 if (udev->parent_hub == NULL)
2025 DPRINTF("udev=%p\n", udev);
2027 /* check if the current device is a HUB */
2028 if (udev->hub != NULL) {
2029 nports = udev->hub->nports;
2031 /* check if all devices on the HUB are suspended */
2032 for (x = 0; x != nports; x++) {
2034 child = usb_bus_port_get_device(udev->bus,
2035 udev->hub->ports + x);
2040 if (child->flags.self_suspended)
2043 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2048 USB_BUS_LOCK(udev->bus);
2050 * Checking for suspend condition and setting suspended bit
2053 err = usb_peer_should_wakeup(udev);
2056 * Set that this device is suspended. This variable
2057 * must be set before calling USB controller suspend
2060 udev->flags.self_suspended = 1;
2062 USB_BUS_UNLOCK(udev->bus);
2065 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2066 /* resume parent HUB first */
2067 usb_dev_resume_peer(udev->parent_hub);
2069 /* reduce chance of instant resume failure by waiting a little bit */
2070 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2072 /* resume current port (Valid in Host and Device Mode) */
2073 err = usbd_req_clear_port_feature(udev->parent_hub,
2074 NULL, udev->port_no, UHF_PORT_SUSPEND);
2076 /* resume settle time */
2077 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_PORT_RESUME_DELAY));
2079 DPRINTF("Suspend was cancelled!\n");
2085 /* notify all sub-devices about suspend */
2086 err = usb_suspend_resume(udev, 1);
2088 usbd_sr_unlock(udev);
2090 if (usb_peer_can_wakeup(udev)) {
2091 /* allow device to do remote wakeup */
2092 err = usbd_req_set_device_feature(udev,
2093 NULL, UF_DEVICE_REMOTE_WAKEUP);
2095 DPRINTFN(0, "Setting device "
2096 "remote wakeup failed\n");
2100 if (udev->bus->methods->device_suspend != NULL) {
2103 /* suspend device on the USB controller */
2104 (udev->bus->methods->device_suspend) (udev);
2107 temp = usbd_get_dma_delay(udev->bus);
2108 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2111 /* suspend current port */
2112 err = usbd_req_set_port_feature(udev->parent_hub,
2113 NULL, udev->port_no, UHF_PORT_SUSPEND);
2115 DPRINTFN(0, "Suspending port failed\n");
2119 udev = udev->parent_hub;
2123 /*------------------------------------------------------------------------*
2124 * usbd_set_power_mode
2126 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2128 *------------------------------------------------------------------------*/
2130 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2132 /* filter input argument */
2133 if ((power_mode != USB_POWER_MODE_ON) &&
2134 (power_mode != USB_POWER_MODE_OFF)) {
2135 power_mode = USB_POWER_MODE_SAVE;
2137 udev->power_mode = power_mode; /* update copy of power mode */
2140 usb_bus_power_update(udev->bus);