3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 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 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR uhub_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define UHUB_INTR_INTERVAL 250 /* ms */
79 #if USB_HAVE_TT_SUPPORT
80 UHUB_RESET_TT_TRANSFER,
86 static int uhub_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
91 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
95 static int usb_power_timeout = 30; /* seconds */
97 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
98 &usb_power_timeout, 0, "USB power timeout");
101 #if USB_HAVE_DISABLE_ENUM
102 static int usb_disable_enumeration = 0;
103 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
104 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration.");
105 TUNABLE_INT("hw.usb.disable_enumeration", &usb_disable_enumeration);
107 static int usb_disable_port_power = 0;
108 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
109 &usb_disable_port_power, 0, "Set to disable all USB port power.");
110 TUNABLE_INT("hw.usb.disable_port_power", &usb_disable_port_power);
113 struct uhub_current_state {
114 uint16_t port_change;
115 uint16_t port_status;
119 struct uhub_current_state sc_st;/* current state */
120 #if (USB_HAVE_FIXED_PORT != 0)
121 struct usb_hub sc_hub;
123 device_t sc_dev; /* base device */
124 struct mtx sc_mtx; /* our mutex */
125 struct usb_device *sc_udev; /* USB device */
126 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
127 #if USB_HAVE_DISABLE_ENUM
128 int sc_disable_enumeration;
129 int sc_disable_port_power;
132 #define UHUB_FLAG_DID_EXPLORE 0x01
135 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
136 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
137 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
138 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
139 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
141 /* prototypes for type checking: */
143 static device_probe_t uhub_probe;
144 static device_attach_t uhub_attach;
145 static device_detach_t uhub_detach;
146 static device_suspend_t uhub_suspend;
147 static device_resume_t uhub_resume;
149 static bus_driver_added_t uhub_driver_added;
150 static bus_child_location_str_t uhub_child_location_string;
151 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
153 static usb_callback_t uhub_intr_callback;
154 #if USB_HAVE_TT_SUPPORT
155 static usb_callback_t uhub_reset_tt_callback;
158 static void usb_dev_resume_peer(struct usb_device *udev);
159 static void usb_dev_suspend_peer(struct usb_device *udev);
160 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
162 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
164 [UHUB_INTR_TRANSFER] = {
165 .type = UE_INTERRUPT,
166 .endpoint = UE_ADDR_ANY,
167 .direction = UE_DIR_ANY,
169 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
170 .bufsize = 0, /* use wMaxPacketSize */
171 .callback = &uhub_intr_callback,
172 .interval = UHUB_INTR_INTERVAL,
174 #if USB_HAVE_TT_SUPPORT
175 [UHUB_RESET_TT_TRANSFER] = {
177 .endpoint = 0x00, /* Control pipe */
178 .direction = UE_DIR_ANY,
179 .bufsize = sizeof(struct usb_device_request),
180 .callback = &uhub_reset_tt_callback,
181 .timeout = 1000, /* 1 second */
182 .usb_mode = USB_MODE_HOST,
188 * driver instance for "hub" connected to "usb"
189 * and "hub" connected to "hub"
191 static devclass_t uhub_devclass;
193 static device_method_t uhub_methods[] = {
194 DEVMETHOD(device_probe, uhub_probe),
195 DEVMETHOD(device_attach, uhub_attach),
196 DEVMETHOD(device_detach, uhub_detach),
198 DEVMETHOD(device_suspend, uhub_suspend),
199 DEVMETHOD(device_resume, uhub_resume),
201 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
202 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
203 DEVMETHOD(bus_driver_added, uhub_driver_added),
207 static driver_t uhub_driver = {
209 .methods = uhub_methods,
210 .size = sizeof(struct uhub_softc)
213 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
214 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
215 MODULE_VERSION(uhub, 1);
218 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
220 struct uhub_softc *sc = usbd_xfer_softc(xfer);
222 switch (USB_GET_STATE(xfer)) {
223 case USB_ST_TRANSFERRED:
226 * This is an indication that some port
227 * has changed status. Notify the bus
228 * event handler thread that we need
229 * to be explored again:
231 usb_needs_explore(sc->sc_udev->bus, 0);
234 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
235 usbd_transfer_submit(xfer);
239 if (xfer->error != USB_ERR_CANCELLED) {
241 * Do a clear-stall. The "stall_pipe" flag
242 * will get cleared before next callback by
245 usbd_xfer_set_stall(xfer);
246 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
247 usbd_transfer_submit(xfer);
253 /*------------------------------------------------------------------------*
256 * This function starts the TT reset USB request
257 *------------------------------------------------------------------------*/
258 #if USB_HAVE_TT_SUPPORT
260 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
262 struct usb_udev_msg *pm = (void *)_pm;
263 struct usb_device *udev = pm->udev;
265 struct uhub_softc *sc;
275 USB_BUS_UNLOCK(udev->bus);
276 mtx_lock(&sc->sc_mtx);
278 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
280 mtx_unlock(&sc->sc_mtx);
281 USB_BUS_LOCK(udev->bus);
285 /*------------------------------------------------------------------------*
286 * uhub_tt_buffer_reset_async_locked
288 * This function queues a TT reset for the given USB device and endpoint.
289 *------------------------------------------------------------------------*/
290 #if USB_HAVE_TT_SUPPORT
292 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
294 struct usb_device_request req;
295 struct usb_device *udev;
301 if (child == NULL || ep == NULL)
304 udev = child->parent_hs_hub;
305 port = child->hs_port_no;
312 (udev->speed != USB_SPEED_HIGH) ||
313 (child->speed != USB_SPEED_LOW &&
314 child->speed != USB_SPEED_FULL) ||
315 (child->flags.usb_mode != USB_MODE_HOST) ||
316 (port == 0) || (ep->edesc == NULL)) {
321 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
323 up = hub->ports + port - 1;
325 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
326 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
329 /* if we already received a clear buffer request, reset the whole TT */
330 if (up->req_reset_tt.bRequest != 0) {
331 req.bmRequestType = UT_WRITE_CLASS_OTHER;
332 req.bRequest = UR_RESET_TT;
333 USETW(req.wValue, 0);
334 req.wIndex[0] = port;
336 USETW(req.wLength, 0);
338 wValue = (ep->edesc->bEndpointAddress & 0xF) |
339 ((child->address & 0x7F) << 4) |
340 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
341 ((ep->edesc->bmAttributes & 3) << 12);
343 req.bmRequestType = UT_WRITE_CLASS_OTHER;
344 req.bRequest = UR_CLEAR_TT_BUFFER;
345 USETW(req.wValue, wValue);
346 req.wIndex[0] = port;
348 USETW(req.wLength, 0);
350 up->req_reset_tt = req;
351 /* get reset transfer started */
352 usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev->bus),
353 &hub->tt_msg[0], &hub->tt_msg[1]);
357 #if USB_HAVE_TT_SUPPORT
359 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
361 struct uhub_softc *sc;
362 struct usb_device *udev;
366 DPRINTF("TT buffer reset\n");
368 sc = usbd_xfer_softc(xfer);
371 switch (USB_GET_STATE(xfer)) {
372 case USB_ST_TRANSFERRED:
375 USB_BUS_LOCK(udev->bus);
376 /* find first port which needs a TT reset */
377 for (x = 0; x != udev->hub->nports; x++) {
378 up = udev->hub->ports + x;
380 if (up->req_reset_tt.bRequest == 0)
383 /* copy in the transfer */
384 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
385 sizeof(up->req_reset_tt));
387 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
390 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
392 USB_BUS_UNLOCK(udev->bus);
394 usbd_transfer_submit(xfer);
397 USB_BUS_UNLOCK(udev->bus);
401 if (error == USB_ERR_CANCELLED)
404 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
410 /*------------------------------------------------------------------------*
411 * uhub_count_active_host_ports
413 * This function counts the number of active ports at the given speed.
414 *------------------------------------------------------------------------*/
416 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
418 struct uhub_softc *sc;
419 struct usb_device *child;
434 for (x = 0; x != hub->nports; x++) {
436 child = usb_bus_port_get_device(udev->bus, up);
438 child->flags.usb_mode == USB_MODE_HOST &&
439 child->speed == speed)
447 uhub_explore_handle_re_enumerate(struct usb_device *child)
452 /* check if device should be re-enumerated */
453 if (child->flags.usb_mode != USB_MODE_HOST)
456 do_unlock = usbd_enum_lock(child);
457 switch (child->re_enumerate_wait) {
458 case USB_RE_ENUM_START:
459 err = usbd_set_config_index(child,
462 DPRINTF("Unconfigure failed: %s: Ignored.\n",
465 if (child->parent_hub == NULL) {
466 /* the root HUB cannot be re-enumerated */
467 DPRINTFN(6, "cannot reset root HUB\n");
470 err = usbd_req_re_enumerate(child, NULL);
473 err = usbd_set_config_index(child, 0);
475 err = usb_probe_and_attach(child,
476 USB_IFACE_INDEX_ANY);
478 child->re_enumerate_wait = USB_RE_ENUM_DONE;
481 case USB_RE_ENUM_PWR_OFF:
482 /* get the device unconfigured */
483 err = usbd_set_config_index(child,
486 DPRINTFN(0, "Could not unconfigure "
487 "device (ignored)\n");
489 if (child->parent_hub == NULL) {
490 /* the root HUB cannot be re-enumerated */
491 DPRINTFN(6, "cannot set port feature\n");
494 /* clear port enable */
495 err = usbd_req_clear_port_feature(child->parent_hub,
496 NULL, child->port_no, UHF_PORT_ENABLE);
498 DPRINTFN(0, "Could not disable port "
502 child->re_enumerate_wait = USB_RE_ENUM_DONE;
505 case USB_RE_ENUM_SET_CONFIG:
506 err = usbd_set_config_index(child,
507 child->next_config_index);
509 DPRINTF("Configure failed: %s: Ignored.\n",
512 err = usb_probe_and_attach(child,
513 USB_IFACE_INDEX_ANY);
515 child->re_enumerate_wait = USB_RE_ENUM_DONE;
519 child->re_enumerate_wait = USB_RE_ENUM_DONE;
523 usbd_enum_unlock(child);
526 /*------------------------------------------------------------------------*
527 * uhub_explore_sub - subroutine
531 * Else: A control transaction failed
532 *------------------------------------------------------------------------*/
534 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
537 struct usb_device *child;
541 bus = sc->sc_udev->bus;
544 /* get driver added refcount from USB bus */
545 refcount = bus->driver_added_refcount;
547 /* get device assosiated with the given port */
548 child = usb_bus_port_get_device(bus, up);
554 uhub_explore_handle_re_enumerate(child);
556 /* check if probe and attach should be done */
558 if (child->driver_added_refcount != refcount) {
559 child->driver_added_refcount = refcount;
560 err = usb_probe_and_attach(child,
561 USB_IFACE_INDEX_ANY);
566 /* start control transfer, if device mode */
568 if (child->flags.usb_mode == USB_MODE_DEVICE)
569 usbd_ctrl_transfer_setup(child);
571 /* if a HUB becomes present, do a recursive HUB explore */
574 err = (child->hub->explore) (child);
580 /*------------------------------------------------------------------------*
581 * uhub_read_port_status - factored out code
582 *------------------------------------------------------------------------*/
584 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
586 struct usb_port_status ps;
589 err = usbd_req_get_port_status(
590 sc->sc_udev, NULL, &ps, portno);
592 /* update status regardless of error */
594 sc->sc_st.port_status = UGETW(ps.wPortStatus);
595 sc->sc_st.port_change = UGETW(ps.wPortChange);
597 /* debugging print */
599 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
600 "wPortChange=0x%04x, err=%s\n",
601 portno, sc->sc_st.port_status,
602 sc->sc_st.port_change, usbd_errstr(err));
606 /*------------------------------------------------------------------------*
611 * Else: A control transaction failed
612 *------------------------------------------------------------------------*/
614 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
616 struct usb_device *child;
617 struct usb_device *udev;
618 enum usb_dev_speed speed;
619 enum usb_hc_mode mode;
624 DPRINTF("reattaching port %d\n", portno);
628 child = usb_bus_port_get_device(udev->bus,
629 udev->hub->ports + portno - 1);
633 /* first clear the port connection change bit */
635 err = usbd_req_clear_port_feature(udev, NULL,
636 portno, UHF_C_PORT_CONNECTION);
641 /* check if there is a child */
645 * Free USB device and all subdevices, if any.
647 usb_free_device(child, 0);
650 /* get fresh status */
652 err = uhub_read_port_status(sc, portno);
656 #if USB_HAVE_DISABLE_ENUM
657 /* check if we should skip enumeration from this USB HUB */
658 if (usb_disable_enumeration != 0 ||
659 sc->sc_disable_enumeration != 0) {
660 DPRINTF("Enumeration is disabled!\n");
664 /* check if nothing is connected to the port */
666 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
669 /* check if there is no power on the port and print a warning */
671 switch (udev->speed) {
675 power_mask = UPS_PORT_POWER;
677 case USB_SPEED_SUPER:
678 if (udev->parent_hub == NULL)
679 power_mask = UPS_PORT_POWER;
681 power_mask = UPS_PORT_POWER_SS;
687 if (!(sc->sc_st.port_status & power_mask)) {
688 DPRINTF("WARNING: strange, connected port %d "
689 "has no power\n", portno);
692 /* check if the device is in Host Mode */
694 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
696 DPRINTF("Port %d is in Host Mode\n", portno);
698 if (sc->sc_st.port_status & UPS_SUSPEND) {
700 * NOTE: Should not get here in SuperSpeed
701 * mode, because the HUB should report this
704 DPRINTF("Port %d was still "
705 "suspended, clearing.\n", portno);
706 err = usbd_req_clear_port_feature(udev,
707 NULL, portno, UHF_PORT_SUSPEND);
712 /* wait for maximum device power up time */
715 USB_MS_TO_TICKS(usb_port_powerup_delay));
717 /* reset port, which implies enabling it */
719 err = usbd_req_reset_port(udev, NULL, portno);
722 DPRINTFN(0, "port %d reset "
723 "failed, error=%s\n",
724 portno, usbd_errstr(err));
727 /* get port status again, it might have changed during reset */
729 err = uhub_read_port_status(sc, portno);
733 /* check if something changed during port reset */
735 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
736 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
738 DPRINTFN(0, "giving up port reset "
739 "- device vanished\n");
746 DPRINTF("Port %d is in Device Mode\n", portno);
750 * Figure out the device speed
752 switch (udev->speed) {
754 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
755 speed = USB_SPEED_HIGH;
756 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
757 speed = USB_SPEED_LOW;
759 speed = USB_SPEED_FULL;
762 if (sc->sc_st.port_status & UPS_LOW_SPEED)
763 speed = USB_SPEED_LOW;
765 speed = USB_SPEED_FULL;
768 speed = USB_SPEED_LOW;
770 case USB_SPEED_SUPER:
771 if (udev->parent_hub == NULL) {
772 /* Root HUB - special case */
773 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
775 speed = USB_SPEED_FULL;
778 speed = USB_SPEED_LOW;
781 speed = USB_SPEED_HIGH;
784 speed = USB_SPEED_SUPER;
788 speed = USB_SPEED_SUPER;
792 /* same speed like parent */
796 if (speed == USB_SPEED_SUPER) {
797 err = usbd_req_set_hub_u1_timeout(udev, NULL,
798 portno, 128 - (2 * udev->depth));
800 DPRINTFN(0, "port %d U1 timeout "
801 "failed, error=%s\n",
802 portno, usbd_errstr(err));
804 err = usbd_req_set_hub_u2_timeout(udev, NULL,
805 portno, 128 - (2 * udev->depth));
807 DPRINTFN(0, "port %d U2 timeout "
808 "failed, error=%s\n",
809 portno, usbd_errstr(err));
814 * Figure out the device mode
816 * NOTE: This part is currently FreeBSD specific.
818 if (udev->parent_hub != NULL) {
819 /* inherit mode from the parent HUB */
820 mode = udev->parent_hub->flags.usb_mode;
821 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
822 mode = USB_MODE_DEVICE;
824 mode = USB_MODE_HOST;
826 /* need to create a new child */
827 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
828 udev->depth + 1, portno - 1, portno, speed, mode);
830 DPRINTFN(0, "could not allocate new device\n");
833 return (0); /* success */
838 * Free USB device and all subdevices, if any.
840 usb_free_device(child, 0);
844 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
845 err = usbd_req_clear_port_feature(
847 portno, UHF_PORT_ENABLE);
851 DPRINTFN(0, "device problem (%s), "
852 "disabling port %d\n", usbd_errstr(err), portno);
857 /*------------------------------------------------------------------------*
858 * usb_device_20_compatible
861 * 0: HUB does not support suspend and resume
862 * Else: HUB supports suspend and resume
863 *------------------------------------------------------------------------*/
865 usb_device_20_compatible(struct usb_device *udev)
869 switch (udev->speed) {
879 /*------------------------------------------------------------------------*
880 * uhub_suspend_resume_port
884 * Else: A control transaction failed
885 *------------------------------------------------------------------------*/
887 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
889 struct usb_device *child;
890 struct usb_device *udev;
894 DPRINTF("port %d\n", portno);
897 child = usb_bus_port_get_device(udev->bus,
898 udev->hub->ports + portno - 1);
900 /* first clear the port suspend change bit */
902 if (usb_device_20_compatible(udev)) {
903 err = usbd_req_clear_port_feature(udev, NULL,
904 portno, UHF_C_PORT_SUSPEND);
906 err = usbd_req_clear_port_feature(udev, NULL,
907 portno, UHF_C_PORT_LINK_STATE);
911 DPRINTF("clearing suspend failed.\n");
914 /* get fresh status */
916 err = uhub_read_port_status(sc, portno);
918 DPRINTF("reading port status failed.\n");
921 /* convert current state */
923 if (usb_device_20_compatible(udev)) {
924 if (sc->sc_st.port_status & UPS_SUSPEND) {
930 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
934 case UPS_PORT_LS_SS_INA:
935 usbd_req_warm_reset_port(udev, NULL, portno);
944 DPRINTF("suspended=%u\n", is_suspend);
946 /* do the suspend or resume */
950 * This code handle two cases: 1) Host Mode - we can only
951 * receive resume here 2) Device Mode - we can receive
952 * suspend and resume here
955 usb_dev_resume_peer(child);
956 else if (child->flags.usb_mode == USB_MODE_DEVICE)
957 usb_dev_suspend_peer(child);
963 /*------------------------------------------------------------------------*
964 * uhub_root_interrupt
966 * This function is called when a Root HUB interrupt has
967 * happened. "ptr" and "len" makes up the Root HUB interrupt
968 * packet. This function is called having the "bus_mtx" locked.
969 *------------------------------------------------------------------------*/
971 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
973 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
975 usb_needs_explore(bus, 0);
979 uhub_is_too_deep(struct usb_device *udev)
981 switch (udev->speed) {
985 if (udev->depth > USB_HUB_MAX_DEPTH)
988 case USB_SPEED_SUPER:
989 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
998 /*------------------------------------------------------------------------*
1004 *------------------------------------------------------------------------*/
1006 uhub_explore(struct usb_device *udev)
1008 struct usb_hub *hub;
1009 struct uhub_softc *sc;
1010 struct usb_port *up;
1019 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1021 /* ignore devices that are too deep */
1022 if (uhub_is_too_deep(udev))
1023 return (USB_ERR_TOO_DEEP);
1025 /* check if device is suspended */
1026 if (udev->flags.self_suspended) {
1027 /* need to wait until the child signals resume */
1028 DPRINTF("Device is suspended!\n");
1033 * Make sure we don't race against user-space applications
1036 do_unlock = usbd_enum_lock(udev);
1038 for (x = 0; x != hub->nports; x++) {
1039 up = hub->ports + x;
1042 err = uhub_read_port_status(sc, portno);
1044 /* most likely the HUB is gone */
1047 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1048 DPRINTF("Overcurrent on port %u.\n", portno);
1049 err = usbd_req_clear_port_feature(
1050 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1052 /* most likely the HUB is gone */
1056 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1058 * Fake a connect status change so that the
1059 * status gets checked initially!
1061 sc->sc_st.port_change |=
1062 UPS_C_CONNECT_STATUS;
1064 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1065 err = usbd_req_clear_port_feature(
1066 udev, NULL, portno, UHF_C_PORT_ENABLE);
1068 /* most likely the HUB is gone */
1071 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1073 * Ignore the port error if the device
1076 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1077 DPRINTFN(0, "illegal enable change, "
1078 "port %d\n", portno);
1081 if (up->restartcnt == USB_RESTART_MAX) {
1082 /* XXX could try another speed ? */
1083 DPRINTFN(0, "port error, giving up "
1084 "port %d\n", portno);
1086 sc->sc_st.port_change |=
1087 UPS_C_CONNECT_STATUS;
1092 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1093 err = uhub_reattach_port(sc, portno);
1095 /* most likely the HUB is gone */
1099 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1100 UPS_C_PORT_LINK_STATE)) {
1101 err = uhub_suspend_resume_port(sc, portno);
1103 /* most likely the HUB is gone */
1107 err = uhub_explore_sub(sc, up);
1109 /* no device(s) present */
1112 /* explore succeeded - reset restart counter */
1117 usbd_enum_unlock(udev);
1119 /* initial status checked */
1120 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1122 /* return success */
1123 return (USB_ERR_NORMAL_COMPLETION);
1127 uhub_probe(device_t dev)
1129 struct usb_attach_arg *uaa = device_get_ivars(dev);
1131 if (uaa->usb_mode != USB_MODE_HOST)
1135 * The subclass for USB HUBs is currently ignored because it
1136 * is 0 for some and 1 for others.
1138 if (uaa->info.bConfigIndex == 0 &&
1139 uaa->info.bDeviceClass == UDCLASS_HUB)
1145 /* NOTE: The information returned by this function can be wrong. */
1147 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1149 struct usb_hub_descriptor hubdesc20;
1150 struct usb_hub_ss_descriptor hubdesc30;
1155 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1156 return (USB_ERR_INVAL);
1161 switch (udev->speed) {
1163 case USB_SPEED_FULL:
1164 case USB_SPEED_HIGH:
1165 /* assuming that there is one port */
1166 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1168 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1169 "error=%s\n", usbd_errstr(err));
1172 nports = hubdesc20.bNbrPorts;
1176 if (udev->speed == USB_SPEED_HIGH)
1177 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1180 case USB_SPEED_SUPER:
1181 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1183 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1184 "error=%s\n", usbd_errstr(err));
1187 nports = hubdesc30.bNbrPorts;
1193 err = USB_ERR_INVAL;
1197 if (pnports != NULL)
1207 uhub_attach(device_t dev)
1209 struct uhub_softc *sc = device_get_softc(dev);
1210 struct usb_attach_arg *uaa = device_get_ivars(dev);
1211 struct usb_device *udev = uaa->device;
1212 struct usb_device *parent_hub = udev->parent_hub;
1213 struct usb_hub *hub;
1214 struct usb_hub_descriptor hubdesc20;
1215 struct usb_hub_ss_descriptor hubdesc30;
1216 #if USB_HAVE_DISABLE_ENUM
1217 struct sysctl_ctx_list *sysctl_ctx;
1218 struct sysctl_oid *sysctl_tree;
1225 uint8_t iface_index;
1231 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1233 device_set_usb_desc(dev);
1235 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1236 "parent->selfpowered=%d\n",
1238 udev->flags.self_powered,
1241 parent_hub->flags.self_powered : 0);
1243 if (uhub_is_too_deep(udev)) {
1244 DPRINTFN(0, "HUB at depth %d, "
1245 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1249 if (!udev->flags.self_powered && parent_hub &&
1250 !parent_hub->flags.self_powered) {
1251 DPRINTFN(0, "Bus powered HUB connected to "
1252 "bus powered HUB. HUB ignored\n");
1256 if (UHUB_IS_MULTI_TT(sc)) {
1257 err = usbd_set_alt_interface_index(udev, 0, 1);
1259 device_printf(dev, "MTT could not be enabled\n");
1262 device_printf(dev, "MTT enabled\n");
1265 /* get HUB descriptor */
1267 DPRINTFN(2, "Getting HUB descriptor\n");
1269 switch (udev->speed) {
1271 case USB_SPEED_FULL:
1272 case USB_SPEED_HIGH:
1273 /* assuming that there is one port */
1274 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1276 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1277 "error=%s\n", usbd_errstr(err));
1280 /* get number of ports */
1281 nports = hubdesc20.bNbrPorts;
1283 /* get power delay */
1284 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1285 usb_extra_power_up_time);
1287 /* get complete HUB descriptor */
1289 /* check number of ports */
1291 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1292 "error=%s\n", usbd_errstr(err));
1295 /* get complete HUB descriptor */
1296 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1299 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1300 "error=%s\n", usbd_errstr(err));
1303 if (hubdesc20.bNbrPorts != nports) {
1304 DPRINTFN(0, "Number of ports changed\n");
1309 case USB_SPEED_SUPER:
1310 if (udev->parent_hub != NULL) {
1311 err = usbd_req_set_hub_depth(udev, NULL,
1314 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1315 "error=%s\n", usbd_errstr(err));
1319 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1321 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1322 "error=%s\n", usbd_errstr(err));
1325 /* get number of ports */
1326 nports = hubdesc30.bNbrPorts;
1328 /* get power delay */
1329 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1330 usb_extra_power_up_time);
1332 /* get complete HUB descriptor */
1334 /* check number of ports */
1335 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1336 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1337 "error=%s\n", usbd_errstr(err));
1340 /* get complete HUB descriptor */
1341 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1344 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1345 "error=%s\n", usbd_errstr(err));
1348 if (hubdesc30.bNbrPorts != nports) {
1349 DPRINTFN(0, "Number of ports changed\n");
1355 DPRINTF("Assuming HUB has only one port\n");
1356 /* default number of ports */
1358 /* default power delay */
1359 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1363 DPRINTFN(0, "portless HUB\n");
1366 if (nports > USB_MAX_PORTS) {
1367 DPRINTF("Port limit exceeded\n");
1370 #if (USB_HAVE_FIXED_PORT == 0)
1371 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1372 M_USBDEV, M_WAITOK | M_ZERO);
1381 /* initialize HUB structure */
1383 hub->explore = &uhub_explore;
1384 hub->nports = nports;
1385 hub->hubudev = udev;
1386 #if USB_HAVE_TT_SUPPORT
1387 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1388 hub->tt_msg[0].udev = udev;
1389 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1390 hub->tt_msg[1].udev = udev;
1392 /* if self powered hub, give ports maximum current */
1393 if (udev->flags.self_powered) {
1394 hub->portpower = USB_MAX_POWER;
1396 hub->portpower = USB_MIN_POWER;
1399 /* set up interrupt pipe */
1401 if (udev->parent_hub == NULL) {
1402 /* root HUB is special */
1406 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1407 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1410 DPRINTFN(0, "cannot setup interrupt transfer, "
1411 "errstr=%s\n", usbd_errstr(err));
1414 /* wait with power off for a while */
1415 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1417 #if USB_HAVE_DISABLE_ENUM
1418 /* Add device sysctls */
1420 sysctl_ctx = device_get_sysctl_ctx(dev);
1421 sysctl_tree = device_get_sysctl_tree(dev);
1423 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1426 snprintf(path, sizeof(path), "dev.uhub.%d.disable_enumeration",
1427 device_get_unit(dev));
1428 TUNABLE_INT_FETCH(path, &sc->sc_disable_enumeration);
1430 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1431 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1432 &sc->sc_disable_enumeration, 0,
1433 "Set to disable enumeration on this USB HUB.");
1435 snprintf(path, sizeof(path), "dev.uhub.%d.disable_port_power",
1436 device_get_unit(dev));
1437 TUNABLE_INT_FETCH(path, &sc->sc_disable_port_power);
1439 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1440 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1441 &sc->sc_disable_port_power, 0,
1442 "Set to disable USB port power on this USB HUB.");
1446 * To have the best chance of success we do things in the exact same
1447 * order as Windoze98. This should not be necessary, but some
1448 * devices do not follow the USB specs to the letter.
1450 * These are the events on the bus when a hub is attached:
1451 * Get device and config descriptors (see attach code)
1452 * Get hub descriptor (see above)
1455 * wait for power to become stable
1456 * (all below happens in explore code)
1458 * clear C_PORT_CONNECTION
1461 * if device connected
1465 * clear C_PORT_RESET
1467 * proceed with device attachment
1470 /* XXX should check for none, individual, or ganged power? */
1474 for (x = 0; x != nports; x++) {
1475 /* set up data structures */
1476 struct usb_port *up = hub->ports + x;
1478 up->device_index = 0;
1482 /* check if port is removable */
1483 switch (udev->speed) {
1485 case USB_SPEED_FULL:
1486 case USB_SPEED_HIGH:
1487 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1490 case USB_SPEED_SUPER:
1491 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1495 DPRINTF("Assuming removable port\n");
1500 #if USB_HAVE_DISABLE_ENUM
1501 /* check if we should disable USB port power or not */
1502 if (usb_disable_port_power != 0 ||
1503 sc->sc_disable_port_power != 0) {
1504 /* turn the power off */
1505 DPRINTFN(0, "Turning port %d power off\n", portno);
1506 err = usbd_req_clear_port_feature(udev, NULL,
1507 portno, UHF_PORT_POWER);
1510 /* turn the power on */
1511 DPRINTFN(0, "Turning port %d power on\n", portno);
1512 err = usbd_req_set_port_feature(udev, NULL,
1513 portno, UHF_PORT_POWER);
1514 #if USB_HAVE_DISABLE_ENUM
1519 DPRINTFN(0, "port %d power on or off failed, %s\n",
1520 portno, usbd_errstr(err));
1522 DPRINTF("turn on port %d power\n",
1525 /* wait for stable power */
1526 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1529 device_printf(dev, "%d port%s with %d "
1530 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1531 removable, udev->flags.self_powered ? "self" : "bus");
1533 /* Start the interrupt endpoint, if any */
1535 mtx_lock(&sc->sc_mtx);
1536 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1537 mtx_unlock(&sc->sc_mtx);
1539 /* Enable automatic power save on all USB HUBs */
1541 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1546 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1548 #if (USB_HAVE_FIXED_PORT == 0)
1549 free(udev->hub, M_USBDEV);
1553 mtx_destroy(&sc->sc_mtx);
1559 * Called from process context when the hub is gone.
1560 * Detach all devices on active ports.
1563 uhub_detach(device_t dev)
1565 struct uhub_softc *sc = device_get_softc(dev);
1566 struct usb_hub *hub = sc->sc_udev->hub;
1567 struct usb_bus *bus = sc->sc_udev->bus;
1568 struct usb_device *child;
1571 if (hub == NULL) /* must be partially working */
1574 /* Make sure interrupt transfer is gone. */
1575 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1577 /* Detach all ports */
1578 for (x = 0; x != hub->nports; x++) {
1580 child = usb_bus_port_get_device(bus, hub->ports + x);
1582 if (child == NULL) {
1587 * Free USB device and all subdevices, if any.
1589 usb_free_device(child, 0);
1592 #if USB_HAVE_TT_SUPPORT
1593 /* Make sure our TT messages are not queued anywhere */
1595 usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus),
1596 &hub->tt_msg[0], &hub->tt_msg[1]);
1597 USB_BUS_UNLOCK(bus);
1600 #if (USB_HAVE_FIXED_PORT == 0)
1601 free(hub, M_USBDEV);
1603 sc->sc_udev->hub = NULL;
1605 mtx_destroy(&sc->sc_mtx);
1611 uhub_suspend(device_t dev)
1614 /* Sub-devices are not suspended here! */
1619 uhub_resume(device_t dev)
1622 /* Sub-devices are not resumed here! */
1627 uhub_driver_added(device_t dev, driver_t *driver)
1629 usb_needs_explore_all();
1633 struct usb_device *udev;
1635 uint8_t iface_index;
1639 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1640 struct hub_result *res)
1642 struct usb_interface *iface;
1643 struct usb_device *udev;
1648 nports = hub->nports;
1649 for (x = 0; x != nports; x++) {
1650 udev = usb_bus_port_get_device(hub->hubudev->bus,
1655 for (i = 0; i != USB_IFACE_MAX; i++) {
1656 iface = usbd_get_iface(udev, i);
1658 (iface->subdev == child)) {
1659 res->iface_index = i;
1661 res->portno = x + 1;
1666 res->iface_index = 0;
1672 uhub_child_location_string(device_t parent, device_t child,
1673 char *buf, size_t buflen)
1675 struct uhub_softc *sc;
1676 struct usb_hub *hub;
1677 struct hub_result res;
1679 if (!device_is_attached(parent)) {
1685 sc = device_get_softc(parent);
1686 hub = sc->sc_udev->hub;
1689 uhub_find_iface_index(hub, child, &res);
1691 DPRINTF("device not on hub\n");
1697 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1698 device_get_unit(res.udev->bus->bdev),
1699 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1701 res.udev->device_index, res.iface_index);
1709 uhub_child_pnpinfo_string(device_t parent, device_t child,
1710 char *buf, size_t buflen)
1712 struct uhub_softc *sc;
1713 struct usb_hub *hub;
1714 struct usb_interface *iface;
1715 struct hub_result res;
1717 if (!device_is_attached(parent)) {
1723 sc = device_get_softc(parent);
1724 hub = sc->sc_udev->hub;
1727 uhub_find_iface_index(hub, child, &res);
1729 DPRINTF("device not on hub\n");
1735 iface = usbd_get_iface(res.udev, res.iface_index);
1736 if (iface && iface->idesc) {
1737 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1738 "devclass=0x%02x devsubclass=0x%02x "
1742 "intclass=0x%02x intsubclass=0x%02x "
1743 "intprotocol=0x%02x " "%s%s",
1744 UGETW(res.udev->ddesc.idVendor),
1745 UGETW(res.udev->ddesc.idProduct),
1746 res.udev->ddesc.bDeviceClass,
1747 res.udev->ddesc.bDeviceSubClass,
1748 usb_get_serial(res.udev),
1749 UGETW(res.udev->ddesc.bcdDevice),
1750 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1751 iface->idesc->bInterfaceClass,
1752 iface->idesc->bInterfaceSubClass,
1753 iface->idesc->bInterfaceProtocol,
1754 iface->pnpinfo ? " " : "",
1755 iface->pnpinfo ? iface->pnpinfo : "");
1769 * The USB Transaction Translator:
1770 * ===============================
1772 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1773 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1774 * USB transfers. To utilize bandwidth dynamically the "scatter and
1775 * gather" principle must be applied. This means that bandwidth must
1776 * be divided into equal parts of bandwidth. With regard to USB all
1777 * data is transferred in smaller packets with length
1778 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1781 * The bandwidth scheduler which I have implemented will simply pack
1782 * the USB transfers back to back until there is no more space in the
1783 * schedule. Out of the 8 microframes which the USB 2.0 standard
1784 * provides, only 6 are available for non-HIGH-speed devices. I have
1785 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1786 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1787 * this division, it is very difficult to allocate and free bandwidth
1790 * NOTE about the Transaction Translator in USB HUBs:
1792 * USB HUBs have a very simple Transaction Translator, that will
1793 * simply pipeline all the SPLIT transactions. That means that the
1794 * transactions will be executed in the order they are queued!
1798 /*------------------------------------------------------------------------*
1799 * usb_intr_find_best_slot
1802 * The best Transaction Translation slot for an interrupt endpoint.
1803 *------------------------------------------------------------------------*/
1805 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1806 uint8_t end, uint8_t mask)
1808 usb_size_t min = (usb_size_t)-1;
1816 /* find the last slot with lesser used bandwidth */
1818 for (x = start; x < end; x++) {
1822 /* compute sum of bandwidth */
1823 for (z = x; z < end; z++) {
1824 if (mask & (1U << (z - x)))
1828 /* check if the current multi-slot is more optimal */
1834 /* check if the mask is about to be shifted out */
1835 if (mask & (1U << (end - 1 - x)))
1841 /*------------------------------------------------------------------------*
1842 * usb_hs_bandwidth_adjust
1844 * This function will update the bandwith usage for the microframe
1845 * having index "slot" by "len" bytes. "len" can be negative. If the
1846 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1847 * the "slot" argument will be replaced by the slot having least used
1848 * bandwidth. The "mask" argument is used for multi-slot allocations.
1851 * The slot in which the bandwidth update was done: 0..7
1852 *------------------------------------------------------------------------*/
1854 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1855 uint8_t slot, uint8_t mask)
1857 struct usb_bus *bus = udev->bus;
1858 struct usb_hub *hub;
1859 enum usb_dev_speed speed;
1862 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1864 speed = usbd_get_speed(udev);
1868 case USB_SPEED_FULL:
1869 if (speed == USB_SPEED_LOW) {
1873 * The Host Controller Driver should have
1874 * performed checks so that the lookup
1875 * below does not result in a NULL pointer
1879 hub = udev->parent_hs_hub->hub;
1880 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1881 slot = usb_intr_find_best_slot(hub->uframe_usage,
1882 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1884 for (x = slot; x < 8; x++) {
1885 if (mask & (1U << (x - slot))) {
1886 hub->uframe_usage[x] += len;
1887 bus->uframe_usage[x] += len;
1892 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1893 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1894 USB_HS_MICRO_FRAMES_MAX, mask);
1896 for (x = slot; x < 8; x++) {
1897 if (mask & (1U << (x - slot))) {
1898 bus->uframe_usage[x] += len;
1906 /*------------------------------------------------------------------------*
1907 * usb_hs_bandwidth_alloc
1909 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1910 *------------------------------------------------------------------------*/
1912 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1914 struct usb_device *udev;
1919 udev = xfer->xroot->udev;
1921 if (udev->flags.usb_mode != USB_MODE_HOST)
1922 return; /* not supported */
1924 xfer->endpoint->refcount_bw++;
1925 if (xfer->endpoint->refcount_bw != 1)
1926 return; /* already allocated */
1928 speed = usbd_get_speed(udev);
1930 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1932 /* allocate a microframe slot */
1935 slot = usb_hs_bandwidth_adjust(udev,
1936 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1938 xfer->endpoint->usb_uframe = slot;
1939 xfer->endpoint->usb_smask = mask << slot;
1941 if ((speed != USB_SPEED_FULL) &&
1942 (speed != USB_SPEED_LOW)) {
1943 xfer->endpoint->usb_cmask = 0x00 ;
1945 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1949 case UE_ISOCHRONOUS:
1950 switch (usbd_xfer_get_fps_shift(xfer)) {
1965 /* allocate a microframe multi-slot */
1967 slot = usb_hs_bandwidth_adjust(udev,
1968 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1970 xfer->endpoint->usb_uframe = slot;
1971 xfer->endpoint->usb_cmask = 0;
1972 xfer->endpoint->usb_smask = mask << slot;
1976 xfer->endpoint->usb_uframe = 0;
1977 xfer->endpoint->usb_cmask = 0;
1978 xfer->endpoint->usb_smask = 0;
1982 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1983 xfer->endpoint->usb_uframe,
1984 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1987 /*------------------------------------------------------------------------*
1988 * usb_hs_bandwidth_free
1990 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1991 *------------------------------------------------------------------------*/
1993 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1995 struct usb_device *udev;
1999 udev = xfer->xroot->udev;
2001 if (udev->flags.usb_mode != USB_MODE_HOST)
2002 return; /* not supported */
2004 xfer->endpoint->refcount_bw--;
2005 if (xfer->endpoint->refcount_bw != 0)
2006 return; /* still allocated */
2008 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2010 case UE_ISOCHRONOUS:
2012 slot = xfer->endpoint->usb_uframe;
2013 mask = xfer->endpoint->usb_smask;
2015 /* free microframe slot(s): */
2016 usb_hs_bandwidth_adjust(udev,
2017 -xfer->max_frame_size, slot, mask >> slot);
2019 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2020 slot, mask >> slot);
2022 xfer->endpoint->usb_uframe = 0;
2023 xfer->endpoint->usb_cmask = 0;
2024 xfer->endpoint->usb_smask = 0;
2032 /*------------------------------------------------------------------------*
2033 * usb_isoc_time_expand
2035 * This function will expand the time counter from 7-bit to 16-bit.
2038 * 16-bit isochronous time counter.
2039 *------------------------------------------------------------------------*/
2041 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2045 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2047 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2049 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2051 if (isoc_time_curr < rem) {
2052 /* the time counter wrapped around */
2053 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2055 /* update the remainder */
2057 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2058 bus->isoc_time_last |= isoc_time_curr;
2060 return (bus->isoc_time_last);
2063 /*------------------------------------------------------------------------*
2064 * usbd_fs_isoc_schedule_alloc_slot
2066 * This function will allocate bandwidth for an isochronous FULL speed
2067 * transaction in the FULL speed schedule.
2072 *------------------------------------------------------------------------*/
2073 #if USB_HAVE_TT_SUPPORT
2075 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2077 struct usb_xfer *xfer;
2078 struct usb_xfer *pipe_xfer;
2079 struct usb_bus *bus;
2081 usb_frlength_t data_len;
2089 bus = isoc_xfer->xroot->bus;
2091 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2093 /* skip self, if any */
2095 if (xfer == isoc_xfer)
2098 /* check if this USB transfer is going through the same TT */
2100 if (xfer->xroot->udev->parent_hs_hub !=
2101 isoc_xfer->xroot->udev->parent_hs_hub) {
2104 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2105 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2106 (xfer->xroot->udev->hs_port_no !=
2107 isoc_xfer->xroot->udev->hs_port_no)) {
2110 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2113 /* check if isoc_time is part of this transfer */
2115 delta = xfer->isoc_time_complete - isoc_time;
2116 if (delta > 0 && delta <= xfer->nframes) {
2117 delta = xfer->nframes - delta;
2119 len = xfer->frlengths[delta];
2128 * Check double buffered transfers. Only stream ID
2129 * equal to zero is valid here!
2131 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2134 /* skip self, if any */
2136 if (pipe_xfer == isoc_xfer)
2139 /* check if isoc_time is part of this transfer */
2141 delta = pipe_xfer->isoc_time_complete - isoc_time;
2142 if (delta > 0 && delta <= pipe_xfer->nframes) {
2143 delta = pipe_xfer->nframes - delta;
2145 len = pipe_xfer->frlengths[delta];
2155 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2156 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2160 /* check for overflow */
2162 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2167 delta = isoc_xfer->isoc_time_complete - isoc_time;
2168 if (delta > 0 && delta <= isoc_xfer->nframes) {
2169 delta = isoc_xfer->nframes - delta;
2171 len = isoc_xfer->frlengths[delta];
2179 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2180 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2184 /* check for overflow */
2186 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2193 /*------------------------------------------------------------------------*
2194 * usb_bus_port_get_device
2196 * This function is NULL safe.
2197 *------------------------------------------------------------------------*/
2199 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2201 if ((bus == NULL) || (up == NULL)) {
2205 if (up->device_index == 0) {
2209 return (bus->devices[up->device_index]);
2212 /*------------------------------------------------------------------------*
2213 * usb_bus_port_set_device
2215 * This function is NULL safe.
2216 *------------------------------------------------------------------------*/
2218 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2219 struct usb_device *udev, uint8_t device_index)
2226 * There is only one case where we don't
2227 * have an USB port, and that is the Root Hub!
2231 up->device_index = device_index;
2233 device_index = up->device_index;
2234 up->device_index = 0;
2238 * Make relationships to our new device
2240 if (device_index != 0) {
2242 mtx_lock(&usb_ref_lock);
2244 bus->devices[device_index] = udev;
2246 mtx_unlock(&usb_ref_lock);
2252 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2255 /*------------------------------------------------------------------------*
2258 * This functions is called when the USB event thread needs to run.
2259 *------------------------------------------------------------------------*/
2261 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2268 DPRINTF("No bus pointer!\n");
2271 if ((bus->devices == NULL) ||
2272 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2273 DPRINTF("No root HUB\n");
2276 if (mtx_owned(&bus->bus_mtx)) {
2285 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2286 &bus->explore_msg[0], &bus->explore_msg[1])) {
2290 USB_BUS_UNLOCK(bus);
2294 /*------------------------------------------------------------------------*
2295 * usb_needs_explore_all
2297 * This function is called whenever a new driver is loaded and will
2298 * cause that all USB busses are re-explored.
2299 *------------------------------------------------------------------------*/
2301 usb_needs_explore_all(void)
2303 struct usb_bus *bus;
2310 dc = usb_devclass_ptr;
2312 DPRINTFN(0, "no devclass\n");
2316 * Explore all USB busses in parallell.
2318 max = devclass_get_maxunit(dc);
2320 dev = devclass_get_device(dc, max);
2322 bus = device_get_softc(dev);
2324 usb_needs_explore(bus, 1);
2331 /*------------------------------------------------------------------------*
2332 * usb_bus_power_update
2334 * This function will ensure that all USB devices on the given bus are
2335 * properly suspended or resumed according to the device transfer
2337 *------------------------------------------------------------------------*/
2340 usb_bus_power_update(struct usb_bus *bus)
2342 usb_needs_explore(bus, 0 /* no probe */ );
2346 /*------------------------------------------------------------------------*
2347 * usbd_transfer_power_ref
2349 * This function will modify the power save reference counts and
2350 * wakeup the USB device associated with the given USB transfer, if
2352 *------------------------------------------------------------------------*/
2355 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2357 static const usb_power_mask_t power_mask[4] = {
2358 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2359 [UE_BULK] = USB_HW_POWER_BULK,
2360 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2361 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2363 struct usb_device *udev;
2364 uint8_t needs_explore;
2365 uint8_t needs_hw_power;
2368 udev = xfer->xroot->udev;
2370 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2371 /* no power save for root HUB */
2374 USB_BUS_LOCK(udev->bus);
2376 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2378 udev->pwr_save.last_xfer_time = ticks;
2379 udev->pwr_save.type_refs[xfer_type] += val;
2381 if (xfer->flags_int.control_xfr) {
2382 udev->pwr_save.read_refs += val;
2383 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2385 * It is not allowed to suspend during a
2388 udev->pwr_save.write_refs += val;
2390 } else if (USB_GET_DATA_ISREAD(xfer)) {
2391 udev->pwr_save.read_refs += val;
2393 udev->pwr_save.write_refs += val;
2397 if (udev->flags.self_suspended)
2398 needs_explore = usb_peer_should_wakeup(udev);
2402 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2403 DPRINTF("Adding type %u to power state\n", xfer_type);
2404 udev->bus->hw_power_state |= power_mask[xfer_type];
2414 USB_BUS_UNLOCK(udev->bus);
2416 if (needs_explore) {
2417 DPRINTF("update\n");
2418 usb_bus_power_update(udev->bus);
2419 } else if (needs_hw_power) {
2420 DPRINTF("needs power\n");
2421 if (udev->bus->methods->set_hw_power != NULL) {
2422 (udev->bus->methods->set_hw_power) (udev->bus);
2428 /*------------------------------------------------------------------------*
2429 * usb_peer_should_wakeup
2431 * This function returns non-zero if the current device should wake up.
2432 *------------------------------------------------------------------------*/
2434 usb_peer_should_wakeup(struct usb_device *udev)
2436 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2437 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2438 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2439 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2440 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2441 (udev->pwr_save.write_refs != 0) ||
2442 ((udev->pwr_save.read_refs != 0) &&
2443 (udev->flags.usb_mode == USB_MODE_HOST) &&
2444 (usb_peer_can_wakeup(udev) == 0)));
2447 /*------------------------------------------------------------------------*
2450 * This function implements the USB power daemon and is called
2451 * regularly from the USB explore thread.
2452 *------------------------------------------------------------------------*/
2455 usb_bus_powerd(struct usb_bus *bus)
2457 struct usb_device *udev;
2460 usb_ticks_t mintime;
2461 usb_size_t type_refs[5];
2464 limit = usb_power_timeout;
2467 else if (limit > 255)
2472 DPRINTF("bus=%p\n", bus);
2477 * The root HUB device is never suspended
2478 * and we simply skip it.
2480 for (x = USB_ROOT_HUB_ADDR + 1;
2481 x != bus->devices_max; x++) {
2483 udev = bus->devices[x];
2487 temp = ticks - udev->pwr_save.last_xfer_time;
2489 if (usb_peer_should_wakeup(udev)) {
2490 /* check if we are suspended */
2491 if (udev->flags.self_suspended != 0) {
2492 USB_BUS_UNLOCK(bus);
2493 usb_dev_resume_peer(udev);
2496 } else if ((temp >= limit) &&
2497 (udev->flags.usb_mode == USB_MODE_HOST) &&
2498 (udev->flags.self_suspended == 0)) {
2499 /* try to do suspend */
2501 USB_BUS_UNLOCK(bus);
2502 usb_dev_suspend_peer(udev);
2507 /* reset counters */
2509 mintime = (usb_ticks_t)-1;
2516 /* Re-loop all the devices to get the actual state */
2518 for (x = USB_ROOT_HUB_ADDR + 1;
2519 x != bus->devices_max; x++) {
2521 udev = bus->devices[x];
2525 /* we found a non-Root-Hub USB device */
2528 /* "last_xfer_time" can be updated by a resume */
2529 temp = ticks - udev->pwr_save.last_xfer_time;
2532 * Compute minimum time since last transfer for the complete
2538 if (udev->flags.self_suspended == 0) {
2539 type_refs[0] += udev->pwr_save.type_refs[0];
2540 type_refs[1] += udev->pwr_save.type_refs[1];
2541 type_refs[2] += udev->pwr_save.type_refs[2];
2542 type_refs[3] += udev->pwr_save.type_refs[3];
2546 if (mintime >= (usb_ticks_t)(1 * hz)) {
2547 /* recompute power masks */
2548 DPRINTF("Recomputing power masks\n");
2549 bus->hw_power_state = 0;
2550 if (type_refs[UE_CONTROL] != 0)
2551 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2552 if (type_refs[UE_BULK] != 0)
2553 bus->hw_power_state |= USB_HW_POWER_BULK;
2554 if (type_refs[UE_INTERRUPT] != 0)
2555 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2556 if (type_refs[UE_ISOCHRONOUS] != 0)
2557 bus->hw_power_state |= USB_HW_POWER_ISOC;
2558 if (type_refs[4] != 0)
2559 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2561 USB_BUS_UNLOCK(bus);
2563 if (bus->methods->set_hw_power != NULL) {
2564 /* always update hardware power! */
2565 (bus->methods->set_hw_power) (bus);
2571 /*------------------------------------------------------------------------*
2572 * usb_dev_resume_peer
2574 * This function will resume an USB peer and do the required USB
2575 * signalling to get an USB device out of the suspended state.
2576 *------------------------------------------------------------------------*/
2578 usb_dev_resume_peer(struct usb_device *udev)
2580 struct usb_bus *bus;
2587 /* check if already resumed */
2588 if (udev->flags.self_suspended == 0)
2591 /* we need a parent HUB to do resume */
2592 if (udev->parent_hub == NULL)
2595 DPRINTF("udev=%p\n", udev);
2597 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2598 (udev->flags.remote_wakeup == 0)) {
2600 * If the host did not set the remote wakeup feature, we can
2601 * not wake it up either!
2603 DPRINTF("remote wakeup is not set!\n");
2606 /* get bus pointer */
2609 /* resume parent hub first */
2610 usb_dev_resume_peer(udev->parent_hub);
2612 /* reduce chance of instant resume failure by waiting a little bit */
2613 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2615 if (usb_device_20_compatible(udev)) {
2616 /* resume current port (Valid in Host and Device Mode) */
2617 err = usbd_req_clear_port_feature(udev->parent_hub,
2618 NULL, udev->port_no, UHF_PORT_SUSPEND);
2620 DPRINTFN(0, "Resuming port failed\n");
2624 /* resume current port (Valid in Host and Device Mode) */
2625 err = usbd_req_set_port_link_state(udev->parent_hub,
2626 NULL, udev->port_no, UPS_PORT_LS_U0);
2628 DPRINTFN(0, "Resuming port failed\n");
2633 /* resume settle time */
2634 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2636 if (bus->methods->device_resume != NULL) {
2637 /* resume USB device on the USB controller */
2638 (bus->methods->device_resume) (udev);
2641 /* set that this device is now resumed */
2642 udev->flags.self_suspended = 0;
2644 /* make sure that we don't go into suspend right away */
2645 udev->pwr_save.last_xfer_time = ticks;
2647 /* make sure the needed power masks are on */
2648 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2649 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2650 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2651 bus->hw_power_state |= USB_HW_POWER_BULK;
2652 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2653 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2654 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2655 bus->hw_power_state |= USB_HW_POWER_ISOC;
2657 USB_BUS_UNLOCK(bus);
2659 if (bus->methods->set_hw_power != NULL) {
2660 /* always update hardware power! */
2661 (bus->methods->set_hw_power) (bus);
2666 /* notify all sub-devices about resume */
2667 err = usb_suspend_resume(udev, 0);
2669 usbd_sr_unlock(udev);
2671 /* check if peer has wakeup capability */
2672 if (usb_peer_can_wakeup(udev)) {
2673 /* clear remote wakeup */
2674 err = usbd_req_clear_device_feature(udev,
2675 NULL, UF_DEVICE_REMOTE_WAKEUP);
2677 DPRINTFN(0, "Clearing device "
2678 "remote wakeup failed: %s\n",
2684 /*------------------------------------------------------------------------*
2685 * usb_dev_suspend_peer
2687 * This function will suspend an USB peer and do the required USB
2688 * signalling to get an USB device into the suspended state.
2689 *------------------------------------------------------------------------*/
2691 usb_dev_suspend_peer(struct usb_device *udev)
2693 struct usb_device *child;
2703 /* check if already suspended */
2704 if (udev->flags.self_suspended)
2707 /* we need a parent HUB to do suspend */
2708 if (udev->parent_hub == NULL)
2711 DPRINTF("udev=%p\n", udev);
2713 /* check if the current device is a HUB */
2714 if (udev->hub != NULL) {
2715 nports = udev->hub->nports;
2717 /* check if all devices on the HUB are suspended */
2718 for (x = 0; x != nports; x++) {
2719 child = usb_bus_port_get_device(udev->bus,
2720 udev->hub->ports + x);
2725 if (child->flags.self_suspended)
2728 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2733 if (usb_peer_can_wakeup(udev)) {
2735 * This request needs to be done before we set
2736 * "udev->flags.self_suspended":
2739 /* allow device to do remote wakeup */
2740 err = usbd_req_set_device_feature(udev,
2741 NULL, UF_DEVICE_REMOTE_WAKEUP);
2743 DPRINTFN(0, "Setting device "
2744 "remote wakeup failed\n");
2748 USB_BUS_LOCK(udev->bus);
2750 * Checking for suspend condition and setting suspended bit
2753 err = usb_peer_should_wakeup(udev);
2756 * Set that this device is suspended. This variable
2757 * must be set before calling USB controller suspend
2760 udev->flags.self_suspended = 1;
2762 USB_BUS_UNLOCK(udev->bus);
2765 if (usb_peer_can_wakeup(udev)) {
2766 /* allow device to do remote wakeup */
2767 err = usbd_req_clear_device_feature(udev,
2768 NULL, UF_DEVICE_REMOTE_WAKEUP);
2770 DPRINTFN(0, "Setting device "
2771 "remote wakeup failed\n");
2775 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2776 /* resume parent HUB first */
2777 usb_dev_resume_peer(udev->parent_hub);
2779 /* reduce chance of instant resume failure by waiting a little bit */
2780 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2782 /* resume current port (Valid in Host and Device Mode) */
2783 err = usbd_req_clear_port_feature(udev->parent_hub,
2784 NULL, udev->port_no, UHF_PORT_SUSPEND);
2786 /* resume settle time */
2787 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2789 DPRINTF("Suspend was cancelled!\n");
2795 /* notify all sub-devices about suspend */
2796 err = usb_suspend_resume(udev, 1);
2798 usbd_sr_unlock(udev);
2800 if (udev->bus->methods->device_suspend != NULL) {
2803 /* suspend device on the USB controller */
2804 (udev->bus->methods->device_suspend) (udev);
2807 temp = usbd_get_dma_delay(udev);
2809 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2813 if (usb_device_20_compatible(udev)) {
2814 /* suspend current port */
2815 err = usbd_req_set_port_feature(udev->parent_hub,
2816 NULL, udev->port_no, UHF_PORT_SUSPEND);
2818 DPRINTFN(0, "Suspending port failed\n");
2822 /* suspend current port */
2823 err = usbd_req_set_port_link_state(udev->parent_hub,
2824 NULL, udev->port_no, UPS_PORT_LS_U3);
2826 DPRINTFN(0, "Suspending port failed\n");
2831 udev = udev->parent_hub;
2835 /*------------------------------------------------------------------------*
2836 * usbd_set_power_mode
2838 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2840 *------------------------------------------------------------------------*/
2842 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2844 /* filter input argument */
2845 if ((power_mode != USB_POWER_MODE_ON) &&
2846 (power_mode != USB_POWER_MODE_OFF))
2847 power_mode = USB_POWER_MODE_SAVE;
2849 power_mode = usbd_filter_power_mode(udev, power_mode);
2851 udev->power_mode = power_mode; /* update copy of power mode */
2854 usb_bus_power_update(udev->bus);
2856 usb_needs_explore(udev->bus, 0 /* no probe */ );
2860 /*------------------------------------------------------------------------*
2861 * usbd_filter_power_mode
2863 * This function filters the power mode based on hardware requirements.
2864 *------------------------------------------------------------------------*/
2866 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2868 struct usb_bus_methods *mtod;
2871 mtod = udev->bus->methods;
2874 if (mtod->get_power_mode != NULL)
2875 (mtod->get_power_mode) (udev, &temp);
2877 /* check if we should not filter */
2879 return (power_mode);
2881 /* use fixed power mode given by hardware driver */
2885 /*------------------------------------------------------------------------*
2886 * usbd_start_re_enumerate
2888 * This function starts re-enumeration of the given USB device. This
2889 * function does not need to be called BUS-locked. This function does
2890 * not wait until the re-enumeration is completed.
2891 *------------------------------------------------------------------------*/
2893 usbd_start_re_enumerate(struct usb_device *udev)
2895 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2896 udev->re_enumerate_wait = USB_RE_ENUM_START;
2897 usb_needs_explore(udev->bus, 0);
2901 /*-----------------------------------------------------------------------*
2902 * usbd_start_set_config
2904 * This function starts setting a USB configuration. This function
2905 * does not need to be called BUS-locked. This function does not wait
2906 * until the set USB configuratino is completed.
2907 *------------------------------------------------------------------------*/
2909 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2911 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2912 if (udev->curr_config_index == index) {
2913 /* no change needed */
2916 udev->next_config_index = index;
2917 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2918 usb_needs_explore(udev->bus, 0);
2920 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2921 if (udev->next_config_index == index) {
2922 /* no change needed */
2926 return (USB_ERR_PENDING_REQUESTS);