3 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
35 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
61 #define USB_DEBUG_VAR uhub_debug
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_device.h>
66 #include <dev/usb/usb_request.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_hub.h>
69 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_transfer.h>
72 #include <dev/usb/usb_dynamic.h>
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #endif /* USB_GLOBAL_INCLUDE_FILE */
79 #include <dev/usb/usb_hub_private.h>
83 static int uhub_debug = 0;
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
87 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
92 static int usb_power_timeout = 30; /* seconds */
94 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
95 &usb_power_timeout, 0, "USB power timeout");
98 #if USB_HAVE_DISABLE_ENUM
99 static int usb_disable_enumeration = 0;
100 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
101 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
102 "This can secure against USB devices turning evil, "
103 "for example a USB memory stick becoming a USB keyboard.");
105 static int usb_disable_port_power = 0;
106 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
107 &usb_disable_port_power, 0, "Set to disable all USB port power.");
111 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
112 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
113 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
114 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
115 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
117 /* prototypes for type checking: */
119 static device_suspend_t uhub_suspend;
120 static device_resume_t uhub_resume;
122 static bus_driver_added_t uhub_driver_added;
123 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
125 static usb_callback_t uhub_intr_callback;
126 #if USB_HAVE_TT_SUPPORT
127 static usb_callback_t uhub_reset_tt_callback;
130 static void usb_dev_resume_peer(struct usb_device *udev);
131 static void usb_dev_suspend_peer(struct usb_device *udev);
132 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
134 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
136 [UHUB_INTR_TRANSFER] = {
137 .type = UE_INTERRUPT,
138 .endpoint = UE_ADDR_ANY,
139 .direction = UE_DIR_ANY,
141 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
142 .bufsize = 0, /* use wMaxPacketSize */
143 .callback = &uhub_intr_callback,
144 .interval = UHUB_INTR_INTERVAL,
146 #if USB_HAVE_TT_SUPPORT
147 [UHUB_RESET_TT_TRANSFER] = {
149 .endpoint = 0x00, /* Control pipe */
150 .direction = UE_DIR_ANY,
151 .bufsize = sizeof(struct usb_device_request),
152 .callback = &uhub_reset_tt_callback,
153 .timeout = 1000, /* 1 second */
154 .usb_mode = USB_MODE_HOST,
160 * driver instance for "hub" connected to "usb"
161 * and "hub" connected to "hub"
163 static devclass_t uhub_devclass;
165 static device_method_t uhub_methods[] = {
166 DEVMETHOD(device_probe, uhub_probe),
167 DEVMETHOD(device_attach, uhub_attach),
168 DEVMETHOD(device_detach, uhub_detach),
170 DEVMETHOD(device_suspend, uhub_suspend),
171 DEVMETHOD(device_resume, uhub_resume),
173 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
174 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
175 DEVMETHOD(bus_driver_added, uhub_driver_added),
179 driver_t uhub_driver = {
181 .methods = uhub_methods,
182 .size = sizeof(struct uhub_softc)
185 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
186 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
187 MODULE_VERSION(uhub, 1);
190 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
192 struct uhub_softc *sc = usbd_xfer_softc(xfer);
194 switch (USB_GET_STATE(xfer)) {
195 case USB_ST_TRANSFERRED:
198 * This is an indication that some port
199 * has changed status. Notify the bus
200 * event handler thread that we need
201 * to be explored again:
203 usb_needs_explore(sc->sc_udev->bus, 0);
206 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
207 usbd_transfer_submit(xfer);
211 if (xfer->error != USB_ERR_CANCELLED) {
213 * Do a clear-stall. The "stall_pipe" flag
214 * will get cleared before next callback by
217 usbd_xfer_set_stall(xfer);
218 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
219 usbd_transfer_submit(xfer);
225 /*------------------------------------------------------------------------*
228 * This function starts the TT reset USB request
229 *------------------------------------------------------------------------*/
230 #if USB_HAVE_TT_SUPPORT
232 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
234 struct usb_udev_msg *pm = (void *)_pm;
235 struct usb_device *udev = pm->udev;
237 struct uhub_softc *sc;
247 USB_BUS_UNLOCK(udev->bus);
248 USB_MTX_LOCK(&sc->sc_mtx);
250 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
252 USB_MTX_UNLOCK(&sc->sc_mtx);
253 USB_BUS_LOCK(udev->bus);
257 /*------------------------------------------------------------------------*
258 * uhub_tt_buffer_reset_async_locked
260 * This function queues a TT reset for the given USB device and endpoint.
261 *------------------------------------------------------------------------*/
262 #if USB_HAVE_TT_SUPPORT
264 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
266 struct usb_device_request req;
267 struct usb_device *udev;
273 if (child == NULL || ep == NULL)
276 udev = child->parent_hs_hub;
277 port = child->hs_port_no;
284 (udev->speed != USB_SPEED_HIGH) ||
285 (child->speed != USB_SPEED_LOW &&
286 child->speed != USB_SPEED_FULL) ||
287 (child->flags.usb_mode != USB_MODE_HOST) ||
288 (port == 0) || (ep->edesc == NULL)) {
293 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
295 up = hub->ports + port - 1;
297 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
298 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
301 /* if we already received a clear buffer request, reset the whole TT */
302 if (up->req_reset_tt.bRequest != 0) {
303 req.bmRequestType = UT_WRITE_CLASS_OTHER;
304 req.bRequest = UR_RESET_TT;
305 USETW(req.wValue, 0);
306 req.wIndex[0] = port;
308 USETW(req.wLength, 0);
310 wValue = (ep->edesc->bEndpointAddress & 0xF) |
311 ((child->address & 0x7F) << 4) |
312 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
313 ((ep->edesc->bmAttributes & 3) << 12);
315 req.bmRequestType = UT_WRITE_CLASS_OTHER;
316 req.bRequest = UR_CLEAR_TT_BUFFER;
317 USETW(req.wValue, wValue);
318 req.wIndex[0] = port;
320 USETW(req.wLength, 0);
322 up->req_reset_tt = req;
323 /* get reset transfer started */
324 usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
325 &hub->tt_msg[0], &hub->tt_msg[1]);
329 #if USB_HAVE_TT_SUPPORT
331 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
333 struct uhub_softc *sc;
334 struct usb_device *udev;
338 DPRINTF("TT buffer reset\n");
340 sc = usbd_xfer_softc(xfer);
343 switch (USB_GET_STATE(xfer)) {
344 case USB_ST_TRANSFERRED:
347 USB_BUS_LOCK(udev->bus);
348 /* find first port which needs a TT reset */
349 for (x = 0; x != udev->hub->nports; x++) {
350 up = udev->hub->ports + x;
352 if (up->req_reset_tt.bRequest == 0)
355 /* copy in the transfer */
356 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
357 sizeof(up->req_reset_tt));
359 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
362 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
364 USB_BUS_UNLOCK(udev->bus);
366 usbd_transfer_submit(xfer);
369 USB_BUS_UNLOCK(udev->bus);
373 if (error == USB_ERR_CANCELLED)
376 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
382 /*------------------------------------------------------------------------*
383 * uhub_count_active_host_ports
385 * This function counts the number of active ports at the given speed.
386 *------------------------------------------------------------------------*/
388 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
390 struct uhub_softc *sc;
391 struct usb_device *child;
406 for (x = 0; x != hub->nports; x++) {
408 child = usb_bus_port_get_device(udev->bus, up);
410 child->flags.usb_mode == USB_MODE_HOST &&
411 child->speed == speed)
419 uhub_explore_handle_re_enumerate(struct usb_device *child)
424 /* check if device should be re-enumerated */
425 if (child->flags.usb_mode != USB_MODE_HOST)
428 do_unlock = usbd_enum_lock(child);
429 switch (child->re_enumerate_wait) {
430 case USB_RE_ENUM_START:
431 err = usbd_set_config_index(child,
434 DPRINTF("Unconfigure failed: %s: Ignored.\n",
437 if (child->parent_hub == NULL) {
438 /* the root HUB cannot be re-enumerated */
439 DPRINTFN(6, "cannot reset root HUB\n");
442 err = usbd_req_re_enumerate(child, NULL);
445 /* refresh device strings */
446 usb_get_langid(child);
447 usb_set_device_strings(child);
449 /* set default configuration */
450 err = usbd_set_config_index(child, 0);
453 err = usb_probe_and_attach(child,
454 USB_IFACE_INDEX_ANY);
456 child->re_enumerate_wait = USB_RE_ENUM_DONE;
459 case USB_RE_ENUM_PWR_OFF:
460 /* get the device unconfigured */
461 err = usbd_set_config_index(child,
464 DPRINTFN(0, "Could not unconfigure "
465 "device (ignored)\n");
467 if (child->parent_hub == NULL) {
468 /* the root HUB cannot be re-enumerated */
469 DPRINTFN(6, "cannot set port feature\n");
472 /* clear port enable */
473 err = usbd_req_clear_port_feature(child->parent_hub,
474 NULL, child->port_no, UHF_PORT_ENABLE);
476 DPRINTFN(0, "Could not disable port "
480 child->re_enumerate_wait = USB_RE_ENUM_DONE;
483 case USB_RE_ENUM_SET_CONFIG:
484 err = usbd_set_config_index(child,
485 child->next_config_index);
487 DPRINTF("Configure failed: %s: Ignored.\n",
490 err = usb_probe_and_attach(child,
491 USB_IFACE_INDEX_ANY);
493 child->re_enumerate_wait = USB_RE_ENUM_DONE;
497 child->re_enumerate_wait = USB_RE_ENUM_DONE;
501 usbd_enum_unlock(child);
504 /*------------------------------------------------------------------------*
505 * uhub_explore_sub - subroutine
509 * Else: A control transaction failed
510 *------------------------------------------------------------------------*/
512 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
515 struct usb_device *child;
519 bus = sc->sc_udev->bus;
522 /* get driver added refcount from USB bus */
523 refcount = bus->driver_added_refcount;
525 /* get device assosiated with the given port */
526 child = usb_bus_port_get_device(bus, up);
532 uhub_explore_handle_re_enumerate(child);
534 /* check if probe and attach should be done */
536 if (child->driver_added_refcount != refcount) {
537 child->driver_added_refcount = refcount;
538 err = usb_probe_and_attach(child,
539 USB_IFACE_INDEX_ANY);
544 /* start control transfer, if device mode */
546 if (child->flags.usb_mode == USB_MODE_DEVICE)
547 usbd_ctrl_transfer_setup(child);
549 /* if a HUB becomes present, do a recursive HUB explore */
552 err = (child->hub->explore) (child);
558 /*------------------------------------------------------------------------*
559 * uhub_read_port_status - factored out code
560 *------------------------------------------------------------------------*/
562 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
564 struct usb_port_status ps;
567 if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
568 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
569 sc->sc_st.port_status = 0;
570 sc->sc_st.port_change = 0;
571 return (USB_ERR_TIMEOUT);
574 err = usbd_req_get_port_status(
575 sc->sc_udev, NULL, &ps, portno);
578 sc->sc_st.port_status = UGETW(ps.wPortStatus);
579 sc->sc_st.port_change = UGETW(ps.wPortChange);
580 sc->sc_usb_port_errors = 0;
582 sc->sc_st.port_status = 0;
583 sc->sc_st.port_change = 0;
584 sc->sc_usb_port_errors++;
587 /* debugging print */
589 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
590 "wPortChange=0x%04x, err=%s\n",
591 portno, sc->sc_st.port_status,
592 sc->sc_st.port_change, usbd_errstr(err));
596 /*------------------------------------------------------------------------*
601 * Else: A control transaction failed
602 *------------------------------------------------------------------------*/
604 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
606 struct usb_device *child;
607 struct usb_device *udev;
608 enum usb_dev_speed speed;
609 enum usb_hc_mode mode;
614 DPRINTF("reattaching port %d\n", portno);
618 child = usb_bus_port_get_device(udev->bus,
619 udev->hub->ports + portno - 1);
623 /* first clear the port connection change bit */
625 err = usbd_req_clear_port_feature(udev, NULL,
626 portno, UHF_C_PORT_CONNECTION);
631 /* check if there is a child */
635 * Free USB device and all subdevices, if any.
637 usb_free_device(child, 0);
640 /* get fresh status */
642 err = uhub_read_port_status(sc, portno);
646 #if USB_HAVE_DISABLE_ENUM
647 /* check if we should skip enumeration from this USB HUB */
648 if (usb_disable_enumeration != 0 ||
649 sc->sc_disable_enumeration != 0) {
650 DPRINTF("Enumeration is disabled!\n");
654 /* check if nothing is connected to the port */
656 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
659 /* check if there is no power on the port and print a warning */
661 switch (udev->speed) {
665 power_mask = UPS_PORT_POWER;
667 case USB_SPEED_SUPER:
668 if (udev->parent_hub == NULL)
669 power_mask = 0; /* XXX undefined */
671 power_mask = UPS_PORT_POWER_SS;
677 if ((sc->sc_st.port_status & power_mask) != power_mask) {
678 DPRINTF("WARNING: strange, connected port %d "
679 "has no power\n", portno);
682 /* check if the device is in Host Mode */
684 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
686 DPRINTF("Port %d is in Host Mode\n", portno);
688 if (sc->sc_st.port_status & UPS_SUSPEND) {
690 * NOTE: Should not get here in SuperSpeed
691 * mode, because the HUB should report this
694 DPRINTF("Port %d was still "
695 "suspended, clearing.\n", portno);
696 err = usbd_req_clear_port_feature(udev,
697 NULL, portno, UHF_PORT_SUSPEND);
702 /* wait for maximum device power up time */
705 USB_MS_TO_TICKS(usb_port_powerup_delay));
707 /* reset port, which implies enabling it */
709 err = usbd_req_reset_port(udev, NULL, portno);
712 DPRINTFN(0, "port %d reset "
713 "failed, error=%s\n",
714 portno, usbd_errstr(err));
717 /* get port status again, it might have changed during reset */
719 err = uhub_read_port_status(sc, portno);
723 /* check if something changed during port reset */
725 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
726 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
728 DPRINTFN(0, "giving up port reset "
729 "- device vanished\n");
736 DPRINTF("Port %d is in Device Mode\n", portno);
740 * Figure out the device speed
742 switch (udev->speed) {
744 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
745 speed = USB_SPEED_HIGH;
746 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
747 speed = USB_SPEED_LOW;
749 speed = USB_SPEED_FULL;
752 if (sc->sc_st.port_status & UPS_LOW_SPEED)
753 speed = USB_SPEED_LOW;
755 speed = USB_SPEED_FULL;
758 speed = USB_SPEED_LOW;
760 case USB_SPEED_SUPER:
761 if (udev->parent_hub == NULL) {
762 /* Root HUB - special case */
763 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
765 speed = USB_SPEED_FULL;
768 speed = USB_SPEED_LOW;
771 speed = USB_SPEED_HIGH;
774 speed = USB_SPEED_SUPER;
778 speed = USB_SPEED_SUPER;
782 /* same speed like parent */
786 if (speed == USB_SPEED_SUPER) {
787 err = usbd_req_set_hub_u1_timeout(udev, NULL,
788 portno, 128 - (2 * udev->depth));
790 DPRINTFN(0, "port %d U1 timeout "
791 "failed, error=%s\n",
792 portno, usbd_errstr(err));
794 err = usbd_req_set_hub_u2_timeout(udev, NULL,
795 portno, 128 - (2 * udev->depth));
797 DPRINTFN(0, "port %d U2 timeout "
798 "failed, error=%s\n",
799 portno, usbd_errstr(err));
804 * Figure out the device mode
806 * NOTE: This part is currently FreeBSD specific.
808 if (udev->parent_hub != NULL) {
809 /* inherit mode from the parent HUB */
810 mode = udev->parent_hub->flags.usb_mode;
811 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
812 mode = USB_MODE_DEVICE;
814 mode = USB_MODE_HOST;
816 /* need to create a new child */
817 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
818 udev->depth + 1, portno - 1, portno, speed, mode);
820 DPRINTFN(0, "could not allocate new device\n");
823 return (0); /* success */
828 * Free USB device and all subdevices, if any.
830 usb_free_device(child, 0);
834 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
835 err = usbd_req_clear_port_feature(
837 portno, UHF_PORT_ENABLE);
841 DPRINTFN(0, "device problem (%s), "
842 "disabling port %d\n", usbd_errstr(err), portno);
847 /*------------------------------------------------------------------------*
848 * usb_device_20_compatible
851 * 0: HUB does not support suspend and resume
852 * Else: HUB supports suspend and resume
853 *------------------------------------------------------------------------*/
855 usb_device_20_compatible(struct usb_device *udev)
859 switch (udev->speed) {
869 /*------------------------------------------------------------------------*
870 * uhub_suspend_resume_port
874 * Else: A control transaction failed
875 *------------------------------------------------------------------------*/
877 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
879 struct usb_device *child;
880 struct usb_device *udev;
884 DPRINTF("port %d\n", portno);
887 child = usb_bus_port_get_device(udev->bus,
888 udev->hub->ports + portno - 1);
890 /* first clear the port suspend change bit */
892 if (usb_device_20_compatible(udev)) {
893 err = usbd_req_clear_port_feature(udev, NULL,
894 portno, UHF_C_PORT_SUSPEND);
896 err = usbd_req_clear_port_feature(udev, NULL,
897 portno, UHF_C_PORT_LINK_STATE);
901 DPRINTF("clearing suspend failed.\n");
904 /* get fresh status */
906 err = uhub_read_port_status(sc, portno);
908 DPRINTF("reading port status failed.\n");
911 /* convert current state */
913 if (usb_device_20_compatible(udev)) {
914 if (sc->sc_st.port_status & UPS_SUSPEND) {
920 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
924 case UPS_PORT_LS_SS_INA:
925 usbd_req_warm_reset_port(udev, NULL, portno);
934 DPRINTF("suspended=%u\n", is_suspend);
936 /* do the suspend or resume */
940 * This code handle two cases: 1) Host Mode - we can only
941 * receive resume here 2) Device Mode - we can receive
942 * suspend and resume here
945 usb_dev_resume_peer(child);
946 else if (child->flags.usb_mode == USB_MODE_DEVICE)
947 usb_dev_suspend_peer(child);
953 /*------------------------------------------------------------------------*
954 * uhub_root_interrupt
956 * This function is called when a Root HUB interrupt has
957 * happened. "ptr" and "len" makes up the Root HUB interrupt
958 * packet. This function is called having the "bus_mtx" locked.
959 *------------------------------------------------------------------------*/
961 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
963 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
965 usb_needs_explore(bus, 0);
969 uhub_is_too_deep(struct usb_device *udev)
971 switch (udev->speed) {
975 if (udev->depth > USB_HUB_MAX_DEPTH)
978 case USB_SPEED_SUPER:
979 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
988 /*------------------------------------------------------------------------*
994 *------------------------------------------------------------------------*/
996 uhub_explore(struct usb_device *udev)
999 struct uhub_softc *sc;
1000 struct usb_port *up;
1009 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1011 /* ignore devices that are too deep */
1012 if (uhub_is_too_deep(udev))
1013 return (USB_ERR_TOO_DEEP);
1015 /* check if device is suspended */
1016 if (udev->flags.self_suspended) {
1017 /* need to wait until the child signals resume */
1018 DPRINTF("Device is suspended!\n");
1023 * Make sure we don't race against user-space applications
1026 do_unlock = usbd_enum_lock(udev);
1028 for (x = 0; x != hub->nports; x++) {
1029 up = hub->ports + x;
1032 err = uhub_read_port_status(sc, portno);
1034 /* most likely the HUB is gone */
1037 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1038 DPRINTF("Overcurrent on port %u.\n", portno);
1039 err = usbd_req_clear_port_feature(
1040 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1042 /* most likely the HUB is gone */
1046 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1048 * Fake a connect status change so that the
1049 * status gets checked initially!
1051 sc->sc_st.port_change |=
1052 UPS_C_CONNECT_STATUS;
1054 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1055 err = usbd_req_clear_port_feature(
1056 udev, NULL, portno, UHF_C_PORT_ENABLE);
1058 /* most likely the HUB is gone */
1061 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1063 * Ignore the port error if the device
1066 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1067 DPRINTFN(0, "illegal enable change, "
1068 "port %d\n", portno);
1071 if (up->restartcnt == USB_RESTART_MAX) {
1072 /* XXX could try another speed ? */
1073 DPRINTFN(0, "port error, giving up "
1074 "port %d\n", portno);
1076 sc->sc_st.port_change |=
1077 UPS_C_CONNECT_STATUS;
1082 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1083 err = uhub_reattach_port(sc, portno);
1085 /* most likely the HUB is gone */
1089 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1090 UPS_C_PORT_LINK_STATE)) {
1091 err = uhub_suspend_resume_port(sc, portno);
1093 /* most likely the HUB is gone */
1097 err = uhub_explore_sub(sc, up);
1099 /* no device(s) present */
1102 /* explore succeeded - reset restart counter */
1107 usbd_enum_unlock(udev);
1109 /* initial status checked */
1110 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1112 /* return success */
1113 return (USB_ERR_NORMAL_COMPLETION);
1117 uhub_probe(device_t dev)
1119 struct usb_attach_arg *uaa = device_get_ivars(dev);
1121 if (uaa->usb_mode != USB_MODE_HOST)
1125 * The subclass for USB HUBs is currently ignored because it
1126 * is 0 for some and 1 for others.
1128 if (uaa->info.bConfigIndex == 0 &&
1129 uaa->info.bDeviceClass == UDCLASS_HUB)
1130 return (BUS_PROBE_DEFAULT);
1135 /* NOTE: The information returned by this function can be wrong. */
1137 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1139 struct usb_hub_descriptor hubdesc20;
1140 struct usb_hub_ss_descriptor hubdesc30;
1145 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1146 return (USB_ERR_INVAL);
1151 switch (udev->speed) {
1153 case USB_SPEED_FULL:
1154 case USB_SPEED_HIGH:
1155 /* assuming that there is one port */
1156 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1158 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1159 "error=%s\n", usbd_errstr(err));
1162 nports = hubdesc20.bNbrPorts;
1166 if (udev->speed == USB_SPEED_HIGH)
1167 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1170 case USB_SPEED_SUPER:
1171 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1173 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1174 "error=%s\n", usbd_errstr(err));
1177 nports = hubdesc30.bNbrPorts;
1183 err = USB_ERR_INVAL;
1187 if (pnports != NULL)
1197 uhub_attach(device_t dev)
1199 struct uhub_softc *sc = device_get_softc(dev);
1200 struct usb_attach_arg *uaa = device_get_ivars(dev);
1201 struct usb_device *udev = uaa->device;
1202 struct usb_device *parent_hub = udev->parent_hub;
1203 struct usb_hub *hub;
1204 struct usb_hub_descriptor hubdesc20;
1205 struct usb_hub_ss_descriptor hubdesc30;
1206 #if USB_HAVE_DISABLE_ENUM
1207 struct sysctl_ctx_list *sysctl_ctx;
1208 struct sysctl_oid *sysctl_tree;
1215 uint8_t iface_index;
1221 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1223 device_set_usb_desc(dev);
1225 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1226 "parent->selfpowered=%d\n",
1228 udev->flags.self_powered,
1231 parent_hub->flags.self_powered : 0);
1233 if (uhub_is_too_deep(udev)) {
1234 DPRINTFN(0, "HUB at depth %d, "
1235 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1239 if (!udev->flags.self_powered && parent_hub &&
1240 !parent_hub->flags.self_powered) {
1241 DPRINTFN(0, "Bus powered HUB connected to "
1242 "bus powered HUB. HUB ignored\n");
1246 if (UHUB_IS_MULTI_TT(sc)) {
1247 err = usbd_set_alt_interface_index(udev, 0, 1);
1249 device_printf(dev, "MTT could not be enabled\n");
1252 device_printf(dev, "MTT enabled\n");
1255 /* get HUB descriptor */
1257 DPRINTFN(2, "Getting HUB descriptor\n");
1259 switch (udev->speed) {
1261 case USB_SPEED_FULL:
1262 case USB_SPEED_HIGH:
1263 /* assuming that there is one port */
1264 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1266 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1267 "error=%s\n", usbd_errstr(err));
1270 /* get number of ports */
1271 nports = hubdesc20.bNbrPorts;
1273 /* get power delay */
1274 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1275 usb_extra_power_up_time);
1277 /* get complete HUB descriptor */
1279 /* check number of ports */
1281 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1282 "error=%s\n", usbd_errstr(err));
1285 /* get complete HUB descriptor */
1286 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1289 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1290 "error=%s\n", usbd_errstr(err));
1293 if (hubdesc20.bNbrPorts != nports) {
1294 DPRINTFN(0, "Number of ports changed\n");
1299 case USB_SPEED_SUPER:
1300 if (udev->parent_hub != NULL) {
1301 err = usbd_req_set_hub_depth(udev, NULL,
1304 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1305 "error=%s\n", usbd_errstr(err));
1309 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1311 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1312 "error=%s\n", usbd_errstr(err));
1315 /* get number of ports */
1316 nports = hubdesc30.bNbrPorts;
1318 /* get power delay */
1319 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1320 usb_extra_power_up_time);
1322 /* get complete HUB descriptor */
1324 /* check number of ports */
1325 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1326 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1327 "error=%s\n", usbd_errstr(err));
1330 /* get complete HUB descriptor */
1331 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1334 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1335 "error=%s\n", usbd_errstr(err));
1338 if (hubdesc30.bNbrPorts != nports) {
1339 DPRINTFN(0, "Number of ports changed\n");
1345 DPRINTF("Assuming HUB has only one port\n");
1346 /* default number of ports */
1348 /* default power delay */
1349 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1353 DPRINTFN(0, "portless HUB\n");
1356 if (nports > USB_MAX_PORTS) {
1357 DPRINTF("Port limit exceeded\n");
1360 #if (USB_HAVE_FIXED_PORT == 0)
1361 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1362 M_USBDEV, M_WAITOK | M_ZERO);
1371 /* initialize HUB structure */
1373 hub->explore = &uhub_explore;
1374 hub->nports = nports;
1375 hub->hubudev = udev;
1376 #if USB_HAVE_TT_SUPPORT
1377 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1378 hub->tt_msg[0].udev = udev;
1379 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1380 hub->tt_msg[1].udev = udev;
1382 /* if self powered hub, give ports maximum current */
1383 if (udev->flags.self_powered) {
1384 hub->portpower = USB_MAX_POWER;
1386 hub->portpower = USB_MIN_POWER;
1389 /* set up interrupt pipe */
1391 if (udev->parent_hub == NULL) {
1392 /* root HUB is special */
1396 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1397 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1400 DPRINTFN(0, "cannot setup interrupt transfer, "
1401 "errstr=%s\n", usbd_errstr(err));
1404 /* wait with power off for a while */
1405 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1407 #if USB_HAVE_DISABLE_ENUM
1408 /* Add device sysctls */
1410 sysctl_ctx = device_get_sysctl_ctx(dev);
1411 sysctl_tree = device_get_sysctl_tree(dev);
1413 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1414 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1415 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1416 &sc->sc_disable_enumeration, 0,
1417 "Set to disable enumeration on this USB HUB.");
1419 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1420 OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1421 &sc->sc_disable_port_power, 0,
1422 "Set to disable USB port power on this USB HUB.");
1426 * To have the best chance of success we do things in the exact same
1427 * order as Windoze98. This should not be necessary, but some
1428 * devices do not follow the USB specs to the letter.
1430 * These are the events on the bus when a hub is attached:
1431 * Get device and config descriptors (see attach code)
1432 * Get hub descriptor (see above)
1435 * wait for power to become stable
1436 * (all below happens in explore code)
1438 * clear C_PORT_CONNECTION
1441 * if device connected
1445 * clear C_PORT_RESET
1447 * proceed with device attachment
1450 /* XXX should check for none, individual, or ganged power? */
1454 for (x = 0; x != nports; x++) {
1455 /* set up data structures */
1456 struct usb_port *up = hub->ports + x;
1458 up->device_index = 0;
1462 /* check if port is removable */
1463 switch (udev->speed) {
1465 case USB_SPEED_FULL:
1466 case USB_SPEED_HIGH:
1467 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1470 case USB_SPEED_SUPER:
1471 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1475 DPRINTF("Assuming removable port\n");
1480 #if USB_HAVE_DISABLE_ENUM
1481 /* check if we should disable USB port power or not */
1482 if (usb_disable_port_power != 0 ||
1483 sc->sc_disable_port_power != 0) {
1484 /* turn the power off */
1485 DPRINTFN(2, "Turning port %d power off\n", portno);
1486 err = usbd_req_clear_port_feature(udev, NULL,
1487 portno, UHF_PORT_POWER);
1490 /* turn the power on */
1491 DPRINTFN(2, "Turning port %d power on\n", portno);
1492 err = usbd_req_set_port_feature(udev, NULL,
1493 portno, UHF_PORT_POWER);
1494 #if USB_HAVE_DISABLE_ENUM
1499 DPRINTFN(0, "port %d power on or off failed, %s\n",
1500 portno, usbd_errstr(err));
1502 DPRINTF("turn on port %d power\n",
1505 /* wait for stable power */
1506 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1509 device_printf(dev, "%d port%s with %d "
1510 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1511 removable, udev->flags.self_powered ? "self" : "bus");
1513 /* Start the interrupt endpoint, if any */
1515 USB_MTX_LOCK(&sc->sc_mtx);
1516 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1517 USB_MTX_UNLOCK(&sc->sc_mtx);
1519 /* Enable automatic power save on all USB HUBs */
1521 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1526 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1528 #if (USB_HAVE_FIXED_PORT == 0)
1529 free(udev->hub, M_USBDEV);
1533 mtx_destroy(&sc->sc_mtx);
1539 * Called from process context when the hub is gone.
1540 * Detach all devices on active ports.
1543 uhub_detach(device_t dev)
1545 struct uhub_softc *sc = device_get_softc(dev);
1546 struct usb_hub *hub = sc->sc_udev->hub;
1547 struct usb_bus *bus = sc->sc_udev->bus;
1548 struct usb_device *child;
1551 if (hub == NULL) /* must be partially working */
1554 /* Make sure interrupt transfer is gone. */
1555 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1557 /* Detach all ports */
1558 for (x = 0; x != hub->nports; x++) {
1560 child = usb_bus_port_get_device(bus, hub->ports + x);
1562 if (child == NULL) {
1567 * Free USB device and all subdevices, if any.
1569 usb_free_device(child, 0);
1572 #if USB_HAVE_TT_SUPPORT
1573 /* Make sure our TT messages are not queued anywhere */
1575 usb_proc_mwait(USB_BUS_TT_PROC(bus),
1576 &hub->tt_msg[0], &hub->tt_msg[1]);
1577 USB_BUS_UNLOCK(bus);
1580 #if (USB_HAVE_FIXED_PORT == 0)
1581 free(hub, M_USBDEV);
1583 sc->sc_udev->hub = NULL;
1585 mtx_destroy(&sc->sc_mtx);
1591 uhub_suspend(device_t dev)
1594 /* Sub-devices are not suspended here! */
1599 uhub_resume(device_t dev)
1602 /* Sub-devices are not resumed here! */
1607 uhub_driver_added(device_t dev, driver_t *driver)
1609 usb_needs_explore_all();
1613 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1614 struct hub_result *res)
1616 struct usb_interface *iface;
1617 struct usb_device *udev;
1622 nports = hub->nports;
1623 for (x = 0; x != nports; x++) {
1624 udev = usb_bus_port_get_device(hub->hubudev->bus,
1629 for (i = 0; i != USB_IFACE_MAX; i++) {
1630 iface = usbd_get_iface(udev, i);
1632 (iface->subdev == child)) {
1633 res->iface_index = i;
1635 res->portno = x + 1;
1640 res->iface_index = 0;
1646 uhub_child_location_string(device_t parent, device_t child,
1647 char *buf, size_t buflen)
1649 struct uhub_softc *sc;
1650 struct usb_hub *hub;
1651 struct hub_result res;
1653 if (!device_is_attached(parent)) {
1659 sc = device_get_softc(parent);
1660 hub = sc->sc_udev->hub;
1663 uhub_find_iface_index(hub, child, &res);
1665 DPRINTF("device not on hub\n");
1671 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
1676 , device_get_unit(res.udev->bus->bdev)
1677 , (res.udev->parent_hub != NULL) ?
1678 res.udev->parent_hub->device_index : 0
1679 , res.portno, res.udev->device_index, res.iface_index
1681 , res.udev->ugen_name
1691 uhub_child_pnpinfo_string(device_t parent, device_t child,
1692 char *buf, size_t buflen)
1694 struct uhub_softc *sc;
1695 struct usb_hub *hub;
1696 struct usb_interface *iface;
1697 struct hub_result res;
1700 if (!device_is_attached(parent)) {
1706 sc = device_get_softc(parent);
1707 hub = sc->sc_udev->hub;
1710 uhub_find_iface_index(hub, child, &res);
1712 DPRINTF("device not on hub\n");
1718 iface = usbd_get_iface(res.udev, res.iface_index);
1719 if (iface && iface->idesc) {
1720 /* Make sure device information is not changed during the print. */
1721 do_unlock = usbd_ctrl_lock(res.udev);
1723 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1724 "devclass=0x%02x devsubclass=0x%02x "
1729 "intclass=0x%02x intsubclass=0x%02x "
1730 "intprotocol=0x%02x" "%s%s",
1731 UGETW(res.udev->ddesc.idVendor),
1732 UGETW(res.udev->ddesc.idProduct),
1733 res.udev->ddesc.bDeviceClass,
1734 res.udev->ddesc.bDeviceSubClass,
1735 res.udev->ddesc.bDeviceProtocol,
1736 usb_get_serial(res.udev),
1737 UGETW(res.udev->ddesc.bcdDevice),
1738 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1739 iface->idesc->bInterfaceClass,
1740 iface->idesc->bInterfaceSubClass,
1741 iface->idesc->bInterfaceProtocol,
1742 iface->pnpinfo ? " " : "",
1743 iface->pnpinfo ? iface->pnpinfo : "");
1746 usbd_ctrl_unlock(res.udev);
1760 * The USB Transaction Translator:
1761 * ===============================
1763 * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
1764 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1765 * USB transfers. To utilize bandwidth dynamically the "scatter and
1766 * gather" principle must be applied. This means that bandwidth must
1767 * be divided into equal parts of bandwidth. With regard to USB all
1768 * data is transferred in smaller packets with length
1769 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1772 * The bandwidth scheduler which I have implemented will simply pack
1773 * the USB transfers back to back until there is no more space in the
1774 * schedule. Out of the 8 microframes which the USB 2.0 standard
1775 * provides, only 6 are available for non-HIGH-speed devices. I have
1776 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1777 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1778 * this division, it is very difficult to allocate and free bandwidth
1781 * NOTE about the Transaction Translator in USB HUBs:
1783 * USB HUBs have a very simple Transaction Translator, that will
1784 * simply pipeline all the SPLIT transactions. That means that the
1785 * transactions will be executed in the order they are queued!
1789 /*------------------------------------------------------------------------*
1790 * usb_intr_find_best_slot
1793 * The best Transaction Translation slot for an interrupt endpoint.
1794 *------------------------------------------------------------------------*/
1796 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1797 uint8_t end, uint8_t mask)
1799 usb_size_t min = (usb_size_t)-1;
1807 /* find the last slot with lesser used bandwidth */
1809 for (x = start; x < end; x++) {
1813 /* compute sum of bandwidth */
1814 for (z = x; z < end; z++) {
1815 if (mask & (1U << (z - x)))
1819 /* check if the current multi-slot is more optimal */
1825 /* check if the mask is about to be shifted out */
1826 if (mask & (1U << (end - 1 - x)))
1832 /*------------------------------------------------------------------------*
1833 * usb_hs_bandwidth_adjust
1835 * This function will update the bandwidth usage for the microframe
1836 * having index "slot" by "len" bytes. "len" can be negative. If the
1837 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1838 * the "slot" argument will be replaced by the slot having least used
1839 * bandwidth. The "mask" argument is used for multi-slot allocations.
1842 * The slot in which the bandwidth update was done: 0..7
1843 *------------------------------------------------------------------------*/
1845 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1846 uint8_t slot, uint8_t mask)
1848 struct usb_bus *bus = udev->bus;
1849 struct usb_hub *hub;
1850 enum usb_dev_speed speed;
1853 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1855 speed = usbd_get_speed(udev);
1859 case USB_SPEED_FULL:
1860 if (speed == USB_SPEED_LOW) {
1864 * The Host Controller Driver should have
1865 * performed checks so that the lookup
1866 * below does not result in a NULL pointer
1870 hub = udev->parent_hs_hub->hub;
1871 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1872 slot = usb_intr_find_best_slot(hub->uframe_usage,
1873 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1875 for (x = slot; x < 8; x++) {
1876 if (mask & (1U << (x - slot))) {
1877 hub->uframe_usage[x] += len;
1878 bus->uframe_usage[x] += len;
1883 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1884 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1885 USB_HS_MICRO_FRAMES_MAX, mask);
1887 for (x = slot; x < 8; x++) {
1888 if (mask & (1U << (x - slot))) {
1889 bus->uframe_usage[x] += len;
1897 /*------------------------------------------------------------------------*
1898 * usb_hs_bandwidth_alloc
1900 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1901 *------------------------------------------------------------------------*/
1903 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1905 struct usb_device *udev;
1910 udev = xfer->xroot->udev;
1912 if (udev->flags.usb_mode != USB_MODE_HOST)
1913 return; /* not supported */
1915 xfer->endpoint->refcount_bw++;
1916 if (xfer->endpoint->refcount_bw != 1)
1917 return; /* already allocated */
1919 speed = usbd_get_speed(udev);
1921 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1923 /* allocate a microframe slot */
1926 slot = usb_hs_bandwidth_adjust(udev,
1927 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1929 xfer->endpoint->usb_uframe = slot;
1930 xfer->endpoint->usb_smask = mask << slot;
1932 if ((speed != USB_SPEED_FULL) &&
1933 (speed != USB_SPEED_LOW)) {
1934 xfer->endpoint->usb_cmask = 0x00 ;
1936 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1940 case UE_ISOCHRONOUS:
1941 switch (usbd_xfer_get_fps_shift(xfer)) {
1956 /* allocate a microframe multi-slot */
1958 slot = usb_hs_bandwidth_adjust(udev,
1959 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1961 xfer->endpoint->usb_uframe = slot;
1962 xfer->endpoint->usb_cmask = 0;
1963 xfer->endpoint->usb_smask = mask << slot;
1967 xfer->endpoint->usb_uframe = 0;
1968 xfer->endpoint->usb_cmask = 0;
1969 xfer->endpoint->usb_smask = 0;
1973 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1974 xfer->endpoint->usb_uframe,
1975 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1978 /*------------------------------------------------------------------------*
1979 * usb_hs_bandwidth_free
1981 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1982 *------------------------------------------------------------------------*/
1984 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1986 struct usb_device *udev;
1990 udev = xfer->xroot->udev;
1992 if (udev->flags.usb_mode != USB_MODE_HOST)
1993 return; /* not supported */
1995 xfer->endpoint->refcount_bw--;
1996 if (xfer->endpoint->refcount_bw != 0)
1997 return; /* still allocated */
1999 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2001 case UE_ISOCHRONOUS:
2003 slot = xfer->endpoint->usb_uframe;
2004 mask = xfer->endpoint->usb_smask;
2006 /* free microframe slot(s): */
2007 usb_hs_bandwidth_adjust(udev,
2008 -xfer->max_frame_size, slot, mask >> slot);
2010 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2011 slot, mask >> slot);
2013 xfer->endpoint->usb_uframe = 0;
2014 xfer->endpoint->usb_cmask = 0;
2015 xfer->endpoint->usb_smask = 0;
2023 /*------------------------------------------------------------------------*
2024 * usb_isoc_time_expand
2026 * This function will expand the time counter from 7-bit to 16-bit.
2029 * 16-bit isochronous time counter.
2030 *------------------------------------------------------------------------*/
2032 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2036 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2038 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2040 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2042 if (isoc_time_curr < rem) {
2043 /* the time counter wrapped around */
2044 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2046 /* update the remainder */
2048 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2049 bus->isoc_time_last |= isoc_time_curr;
2051 return (bus->isoc_time_last);
2054 /*------------------------------------------------------------------------*
2055 * usbd_fs_isoc_schedule_alloc_slot
2057 * This function will allocate bandwidth for an isochronous FULL speed
2058 * transaction in the FULL speed schedule.
2063 *------------------------------------------------------------------------*/
2064 #if USB_HAVE_TT_SUPPORT
2066 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2068 struct usb_xfer *xfer;
2069 struct usb_xfer *pipe_xfer;
2070 struct usb_bus *bus;
2072 usb_frlength_t data_len;
2080 bus = isoc_xfer->xroot->bus;
2082 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2084 /* skip self, if any */
2086 if (xfer == isoc_xfer)
2089 /* check if this USB transfer is going through the same TT */
2091 if (xfer->xroot->udev->parent_hs_hub !=
2092 isoc_xfer->xroot->udev->parent_hs_hub) {
2095 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2096 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2097 (xfer->xroot->udev->hs_port_no !=
2098 isoc_xfer->xroot->udev->hs_port_no)) {
2101 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2104 /* check if isoc_time is part of this transfer */
2106 delta = xfer->isoc_time_complete - isoc_time;
2107 if (delta > 0 && delta <= xfer->nframes) {
2108 delta = xfer->nframes - delta;
2110 len = xfer->frlengths[delta];
2119 * Check double buffered transfers. Only stream ID
2120 * equal to zero is valid here!
2122 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2125 /* skip self, if any */
2127 if (pipe_xfer == isoc_xfer)
2130 /* check if isoc_time is part of this transfer */
2132 delta = pipe_xfer->isoc_time_complete - isoc_time;
2133 if (delta > 0 && delta <= pipe_xfer->nframes) {
2134 delta = pipe_xfer->nframes - delta;
2136 len = pipe_xfer->frlengths[delta];
2146 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2147 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2151 /* check for overflow */
2153 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2158 delta = isoc_xfer->isoc_time_complete - isoc_time;
2159 if (delta > 0 && delta <= isoc_xfer->nframes) {
2160 delta = isoc_xfer->nframes - delta;
2162 len = isoc_xfer->frlengths[delta];
2170 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2171 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2175 /* check for overflow */
2177 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2184 /*------------------------------------------------------------------------*
2185 * usb_bus_port_get_device
2187 * This function is NULL safe.
2188 *------------------------------------------------------------------------*/
2190 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2192 if ((bus == NULL) || (up == NULL)) {
2196 if (up->device_index == 0) {
2200 return (bus->devices[up->device_index]);
2203 /*------------------------------------------------------------------------*
2204 * usb_bus_port_set_device
2206 * This function is NULL safe.
2207 *------------------------------------------------------------------------*/
2209 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2210 struct usb_device *udev, uint8_t device_index)
2217 * There is only one case where we don't
2218 * have an USB port, and that is the Root Hub!
2222 up->device_index = device_index;
2224 device_index = up->device_index;
2225 up->device_index = 0;
2229 * Make relationships to our new device
2231 if (device_index != 0) {
2233 mtx_lock(&usb_ref_lock);
2235 bus->devices[device_index] = udev;
2237 mtx_unlock(&usb_ref_lock);
2243 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2246 /*------------------------------------------------------------------------*
2249 * This functions is called when the USB event thread needs to run.
2250 *------------------------------------------------------------------------*/
2252 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2264 DPRINTF("No bus pointer!\n");
2267 if ((bus->devices == NULL) ||
2268 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2269 DPRINTF("No root HUB\n");
2272 if (mtx_owned(&bus->bus_mtx)) {
2281 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2282 &bus->explore_msg[0], &bus->explore_msg[1])) {
2286 USB_BUS_UNLOCK(bus);
2290 /*------------------------------------------------------------------------*
2291 * usb_needs_explore_all
2293 * This function is called whenever a new driver is loaded and will
2294 * cause that all USB buses are re-explored.
2295 *------------------------------------------------------------------------*/
2297 usb_needs_explore_all(void)
2299 struct usb_bus *bus;
2306 dc = usb_devclass_ptr;
2308 DPRINTFN(0, "no devclass\n");
2312 * Explore all USB buses in parallel.
2314 max = devclass_get_maxunit(dc);
2316 dev = devclass_get_device(dc, max);
2318 bus = device_get_softc(dev);
2320 usb_needs_explore(bus, 1);
2327 /*------------------------------------------------------------------------*
2328 * usb_needs_explore_init
2330 * This function will ensure that the USB controllers are not enumerated
2331 * until the "cold" variable is cleared.
2332 *------------------------------------------------------------------------*/
2334 usb_needs_explore_init(void *arg)
2337 * The cold variable should be cleared prior to this function
2341 usb_needs_explore_all();
2343 DPRINTFN(-1, "Cold variable is still set!\n");
2345 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2347 /*------------------------------------------------------------------------*
2348 * usb_bus_power_update
2350 * This function will ensure that all USB devices on the given bus are
2351 * properly suspended or resumed according to the device transfer
2353 *------------------------------------------------------------------------*/
2356 usb_bus_power_update(struct usb_bus *bus)
2358 usb_needs_explore(bus, 0 /* no probe */ );
2362 /*------------------------------------------------------------------------*
2363 * usbd_transfer_power_ref
2365 * This function will modify the power save reference counts and
2366 * wakeup the USB device associated with the given USB transfer, if
2368 *------------------------------------------------------------------------*/
2371 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2373 static const usb_power_mask_t power_mask[4] = {
2374 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2375 [UE_BULK] = USB_HW_POWER_BULK,
2376 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2377 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2379 struct usb_device *udev;
2380 uint8_t needs_explore;
2381 uint8_t needs_hw_power;
2384 udev = xfer->xroot->udev;
2386 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2387 /* no power save for root HUB */
2390 USB_BUS_LOCK(udev->bus);
2392 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2394 udev->pwr_save.last_xfer_time = ticks;
2395 udev->pwr_save.type_refs[xfer_type] += val;
2397 if (xfer->flags_int.control_xfr) {
2398 udev->pwr_save.read_refs += val;
2399 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2401 * It is not allowed to suspend during a
2404 udev->pwr_save.write_refs += val;
2406 } else if (USB_GET_DATA_ISREAD(xfer)) {
2407 udev->pwr_save.read_refs += val;
2409 udev->pwr_save.write_refs += val;
2413 if (udev->flags.self_suspended)
2414 needs_explore = usb_peer_should_wakeup(udev);
2418 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2419 DPRINTF("Adding type %u to power state\n", xfer_type);
2420 udev->bus->hw_power_state |= power_mask[xfer_type];
2430 USB_BUS_UNLOCK(udev->bus);
2432 if (needs_explore) {
2433 DPRINTF("update\n");
2434 usb_bus_power_update(udev->bus);
2435 } else if (needs_hw_power) {
2436 DPRINTF("needs power\n");
2437 if (udev->bus->methods->set_hw_power != NULL) {
2438 (udev->bus->methods->set_hw_power) (udev->bus);
2444 /*------------------------------------------------------------------------*
2445 * usb_peer_should_wakeup
2447 * This function returns non-zero if the current device should wake up.
2448 *------------------------------------------------------------------------*/
2450 usb_peer_should_wakeup(struct usb_device *udev)
2452 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2453 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2454 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2455 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2456 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2457 (udev->pwr_save.write_refs != 0) ||
2458 ((udev->pwr_save.read_refs != 0) &&
2459 (udev->flags.usb_mode == USB_MODE_HOST) &&
2460 (usb_peer_can_wakeup(udev) == 0)));
2463 /*------------------------------------------------------------------------*
2466 * This function implements the USB power daemon and is called
2467 * regularly from the USB explore thread.
2468 *------------------------------------------------------------------------*/
2471 usb_bus_powerd(struct usb_bus *bus)
2473 struct usb_device *udev;
2476 usb_ticks_t mintime;
2477 usb_size_t type_refs[5];
2480 limit = usb_power_timeout;
2483 else if (limit > 255)
2488 DPRINTF("bus=%p\n", bus);
2493 * The root HUB device is never suspended
2494 * and we simply skip it.
2496 for (x = USB_ROOT_HUB_ADDR + 1;
2497 x != bus->devices_max; x++) {
2499 udev = bus->devices[x];
2503 temp = ticks - udev->pwr_save.last_xfer_time;
2505 if (usb_peer_should_wakeup(udev)) {
2506 /* check if we are suspended */
2507 if (udev->flags.self_suspended != 0) {
2508 USB_BUS_UNLOCK(bus);
2509 usb_dev_resume_peer(udev);
2512 } else if ((temp >= limit) &&
2513 (udev->flags.usb_mode == USB_MODE_HOST) &&
2514 (udev->flags.self_suspended == 0)) {
2515 /* try to do suspend */
2517 USB_BUS_UNLOCK(bus);
2518 usb_dev_suspend_peer(udev);
2523 /* reset counters */
2525 mintime = (usb_ticks_t)-1;
2532 /* Re-loop all the devices to get the actual state */
2534 for (x = USB_ROOT_HUB_ADDR + 1;
2535 x != bus->devices_max; x++) {
2537 udev = bus->devices[x];
2541 /* we found a non-Root-Hub USB device */
2544 /* "last_xfer_time" can be updated by a resume */
2545 temp = ticks - udev->pwr_save.last_xfer_time;
2548 * Compute minimum time since last transfer for the complete
2554 if (udev->flags.self_suspended == 0) {
2555 type_refs[0] += udev->pwr_save.type_refs[0];
2556 type_refs[1] += udev->pwr_save.type_refs[1];
2557 type_refs[2] += udev->pwr_save.type_refs[2];
2558 type_refs[3] += udev->pwr_save.type_refs[3];
2562 if (mintime >= (usb_ticks_t)(1 * hz)) {
2563 /* recompute power masks */
2564 DPRINTF("Recomputing power masks\n");
2565 bus->hw_power_state = 0;
2566 if (type_refs[UE_CONTROL] != 0)
2567 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2568 if (type_refs[UE_BULK] != 0)
2569 bus->hw_power_state |= USB_HW_POWER_BULK;
2570 if (type_refs[UE_INTERRUPT] != 0)
2571 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2572 if (type_refs[UE_ISOCHRONOUS] != 0)
2573 bus->hw_power_state |= USB_HW_POWER_ISOC;
2574 if (type_refs[4] != 0)
2575 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2577 USB_BUS_UNLOCK(bus);
2579 if (bus->methods->set_hw_power != NULL) {
2580 /* always update hardware power! */
2581 (bus->methods->set_hw_power) (bus);
2588 usbd_device_30_remote_wakeup(struct usb_device *udev, uint8_t bRequest)
2590 struct usb_device_request req = {};
2592 req.bmRequestType = UT_WRITE_INTERFACE;
2593 req.bRequest = bRequest;
2594 USETW(req.wValue, USB_INTERFACE_FUNC_SUSPEND);
2595 USETW(req.wIndex, USB_INTERFACE_FUNC_SUSPEND_LP |
2596 USB_INTERFACE_FUNC_SUSPEND_RW);
2598 return (usbd_do_request(udev, NULL, &req, 0));
2602 usbd_clear_dev_wakeup(struct usb_device *udev)
2606 if (usb_device_20_compatible(udev)) {
2607 err = usbd_req_clear_device_feature(udev,
2608 NULL, UF_DEVICE_REMOTE_WAKEUP);
2610 err = usbd_device_30_remote_wakeup(udev,
2617 usbd_set_dev_wakeup(struct usb_device *udev)
2621 if (usb_device_20_compatible(udev)) {
2622 err = usbd_req_set_device_feature(udev,
2623 NULL, UF_DEVICE_REMOTE_WAKEUP);
2625 err = usbd_device_30_remote_wakeup(udev,
2631 /*------------------------------------------------------------------------*
2632 * usb_dev_resume_peer
2634 * This function will resume an USB peer and do the required USB
2635 * signalling to get an USB device out of the suspended state.
2636 *------------------------------------------------------------------------*/
2638 usb_dev_resume_peer(struct usb_device *udev)
2640 struct usb_bus *bus;
2647 /* check if already resumed */
2648 if (udev->flags.self_suspended == 0)
2651 /* we need a parent HUB to do resume */
2652 if (udev->parent_hub == NULL)
2655 DPRINTF("udev=%p\n", udev);
2657 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2658 (udev->flags.remote_wakeup == 0)) {
2660 * If the host did not set the remote wakeup feature, we can
2661 * not wake it up either!
2663 DPRINTF("remote wakeup is not set!\n");
2666 /* get bus pointer */
2669 /* resume parent hub first */
2670 usb_dev_resume_peer(udev->parent_hub);
2672 /* reduce chance of instant resume failure by waiting a little bit */
2673 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2675 if (usb_device_20_compatible(udev)) {
2676 /* resume current port (Valid in Host and Device Mode) */
2677 err = usbd_req_clear_port_feature(udev->parent_hub,
2678 NULL, udev->port_no, UHF_PORT_SUSPEND);
2680 DPRINTFN(0, "Resuming port failed\n");
2684 /* resume current port (Valid in Host and Device Mode) */
2685 err = usbd_req_set_port_link_state(udev->parent_hub,
2686 NULL, udev->port_no, UPS_PORT_LS_U0);
2688 DPRINTFN(0, "Resuming port failed\n");
2693 /* resume settle time */
2694 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2696 if (bus->methods->device_resume != NULL) {
2697 /* resume USB device on the USB controller */
2698 (bus->methods->device_resume) (udev);
2701 /* set that this device is now resumed */
2702 udev->flags.self_suspended = 0;
2704 /* make sure that we don't go into suspend right away */
2705 udev->pwr_save.last_xfer_time = ticks;
2707 /* make sure the needed power masks are on */
2708 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2709 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2710 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2711 bus->hw_power_state |= USB_HW_POWER_BULK;
2712 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2713 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2714 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2715 bus->hw_power_state |= USB_HW_POWER_ISOC;
2717 USB_BUS_UNLOCK(bus);
2719 if (bus->methods->set_hw_power != NULL) {
2720 /* always update hardware power! */
2721 (bus->methods->set_hw_power) (bus);
2726 /* notify all sub-devices about resume */
2727 err = usb_suspend_resume(udev, 0);
2729 usbd_sr_unlock(udev);
2731 /* check if peer has wakeup capability */
2732 if (usb_peer_can_wakeup(udev)) {
2733 /* clear remote wakeup */
2734 err = usbd_clear_dev_wakeup(udev);
2736 DPRINTFN(0, "Clearing device "
2737 "remote wakeup failed: %s\n",
2743 /*------------------------------------------------------------------------*
2744 * usb_dev_suspend_peer
2746 * This function will suspend an USB peer and do the required USB
2747 * signalling to get an USB device into the suspended state.
2748 *------------------------------------------------------------------------*/
2750 usb_dev_suspend_peer(struct usb_device *udev)
2752 struct usb_device *child;
2762 /* check if already suspended */
2763 if (udev->flags.self_suspended)
2766 /* we need a parent HUB to do suspend */
2767 if (udev->parent_hub == NULL)
2770 DPRINTF("udev=%p\n", udev);
2772 /* check if the current device is a HUB */
2773 if (udev->hub != NULL) {
2774 nports = udev->hub->nports;
2776 /* check if all devices on the HUB are suspended */
2777 for (x = 0; x != nports; x++) {
2778 child = usb_bus_port_get_device(udev->bus,
2779 udev->hub->ports + x);
2784 if (child->flags.self_suspended)
2787 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2792 if (usb_peer_can_wakeup(udev)) {
2794 * This request needs to be done before we set
2795 * "udev->flags.self_suspended":
2798 /* allow device to do remote wakeup */
2799 err = usbd_set_dev_wakeup(udev);
2801 DPRINTFN(0, "Setting device "
2802 "remote wakeup failed\n");
2806 USB_BUS_LOCK(udev->bus);
2808 * Checking for suspend condition and setting suspended bit
2811 err = usb_peer_should_wakeup(udev);
2814 * Set that this device is suspended. This variable
2815 * must be set before calling USB controller suspend
2818 udev->flags.self_suspended = 1;
2820 USB_BUS_UNLOCK(udev->bus);
2823 if (usb_peer_can_wakeup(udev)) {
2824 /* allow device to do remote wakeup */
2825 err = usbd_clear_dev_wakeup(udev);
2827 DPRINTFN(0, "Setting device "
2828 "remote wakeup failed\n");
2832 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2833 /* resume parent HUB first */
2834 usb_dev_resume_peer(udev->parent_hub);
2836 /* reduce chance of instant resume failure by waiting a little bit */
2837 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2839 /* resume current port (Valid in Host and Device Mode) */
2840 err = usbd_req_clear_port_feature(udev->parent_hub,
2841 NULL, udev->port_no, UHF_PORT_SUSPEND);
2843 /* resume settle time */
2844 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2846 DPRINTF("Suspend was cancelled!\n");
2852 /* notify all sub-devices about suspend */
2853 err = usb_suspend_resume(udev, 1);
2855 usbd_sr_unlock(udev);
2857 if (udev->bus->methods->device_suspend != NULL) {
2860 /* suspend device on the USB controller */
2861 (udev->bus->methods->device_suspend) (udev);
2864 temp = usbd_get_dma_delay(udev);
2866 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2870 if (usb_device_20_compatible(udev)) {
2871 /* suspend current port */
2872 err = usbd_req_set_port_feature(udev->parent_hub,
2873 NULL, udev->port_no, UHF_PORT_SUSPEND);
2875 DPRINTFN(0, "Suspending port failed\n");
2879 /* suspend current port */
2880 err = usbd_req_set_port_link_state(udev->parent_hub,
2881 NULL, udev->port_no, UPS_PORT_LS_U3);
2883 DPRINTFN(0, "Suspending port failed\n");
2888 udev = udev->parent_hub;
2892 /*------------------------------------------------------------------------*
2893 * usbd_set_power_mode
2895 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2897 *------------------------------------------------------------------------*/
2899 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2901 /* filter input argument */
2902 if ((power_mode != USB_POWER_MODE_ON) &&
2903 (power_mode != USB_POWER_MODE_OFF))
2904 power_mode = USB_POWER_MODE_SAVE;
2906 power_mode = usbd_filter_power_mode(udev, power_mode);
2908 udev->power_mode = power_mode; /* update copy of power mode */
2911 usb_bus_power_update(udev->bus);
2913 usb_needs_explore(udev->bus, 0 /* no probe */ );
2917 /*------------------------------------------------------------------------*
2918 * usbd_filter_power_mode
2920 * This function filters the power mode based on hardware requirements.
2921 *------------------------------------------------------------------------*/
2923 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2925 const struct usb_bus_methods *mtod;
2928 mtod = udev->bus->methods;
2931 if (mtod->get_power_mode != NULL)
2932 (mtod->get_power_mode) (udev, &temp);
2934 /* check if we should not filter */
2936 return (power_mode);
2938 /* use fixed power mode given by hardware driver */
2942 /*------------------------------------------------------------------------*
2943 * usbd_start_re_enumerate
2945 * This function starts re-enumeration of the given USB device. This
2946 * function does not need to be called BUS-locked. This function does
2947 * not wait until the re-enumeration is completed.
2948 *------------------------------------------------------------------------*/
2950 usbd_start_re_enumerate(struct usb_device *udev)
2952 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2953 udev->re_enumerate_wait = USB_RE_ENUM_START;
2954 usb_needs_explore(udev->bus, 0);
2958 /*-----------------------------------------------------------------------*
2959 * usbd_start_set_config
2961 * This function starts setting a USB configuration. This function
2962 * does not need to be called BUS-locked. This function does not wait
2963 * until the set USB configuratino is completed.
2964 *------------------------------------------------------------------------*/
2966 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2968 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2969 if (udev->curr_config_index == index) {
2970 /* no change needed */
2973 udev->next_config_index = index;
2974 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2975 usb_needs_explore(udev->bus, 0);
2977 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2978 if (udev->next_config_index == index) {
2979 /* no change needed */
2983 return (USB_ERR_PENDING_REQUESTS);