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 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR uhub_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_request.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_hub.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_controller.h>
70 #include <dev/usb/usb_bus.h>
72 #define UHUB_INTR_INTERVAL 250 /* ms */
75 #if USB_HAVE_TT_SUPPORT
76 UHUB_RESET_TT_TRANSFER,
82 static int uhub_debug = 0;
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
85 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
87 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
91 static int usb_power_timeout = 30; /* seconds */
93 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
94 &usb_power_timeout, 0, "USB power timeout");
97 struct uhub_current_state {
103 struct uhub_current_state sc_st;/* current state */
104 device_t sc_dev; /* base device */
105 struct mtx sc_mtx; /* our mutex */
106 struct usb_device *sc_udev; /* USB device */
107 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
109 #define UHUB_FLAG_DID_EXPLORE 0x01
113 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
114 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
115 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
116 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
117 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
119 /* prototypes for type checking: */
121 static device_probe_t uhub_probe;
122 static device_attach_t uhub_attach;
123 static device_detach_t uhub_detach;
124 static device_suspend_t uhub_suspend;
125 static device_resume_t uhub_resume;
127 static bus_driver_added_t uhub_driver_added;
128 static bus_child_location_str_t uhub_child_location_string;
129 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
131 static usb_callback_t uhub_intr_callback;
132 #if USB_HAVE_TT_SUPPORT
133 static usb_callback_t uhub_reset_tt_callback;
136 static void usb_dev_resume_peer(struct usb_device *udev);
137 static void usb_dev_suspend_peer(struct usb_device *udev);
138 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
140 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
142 [UHUB_INTR_TRANSFER] = {
143 .type = UE_INTERRUPT,
144 .endpoint = UE_ADDR_ANY,
145 .direction = UE_DIR_ANY,
147 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
148 .bufsize = 0, /* use wMaxPacketSize */
149 .callback = &uhub_intr_callback,
150 .interval = UHUB_INTR_INTERVAL,
152 #if USB_HAVE_TT_SUPPORT
153 [UHUB_RESET_TT_TRANSFER] = {
155 .endpoint = 0x00, /* Control pipe */
156 .direction = UE_DIR_ANY,
157 .bufsize = sizeof(struct usb_device_request),
158 .callback = &uhub_reset_tt_callback,
159 .timeout = 1000, /* 1 second */
160 .usb_mode = USB_MODE_HOST,
166 * driver instance for "hub" connected to "usb"
167 * and "hub" connected to "hub"
169 static devclass_t uhub_devclass;
171 static device_method_t uhub_methods[] = {
172 DEVMETHOD(device_probe, uhub_probe),
173 DEVMETHOD(device_attach, uhub_attach),
174 DEVMETHOD(device_detach, uhub_detach),
176 DEVMETHOD(device_suspend, uhub_suspend),
177 DEVMETHOD(device_resume, uhub_resume),
179 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
180 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
181 DEVMETHOD(bus_driver_added, uhub_driver_added),
185 static driver_t uhub_driver = {
187 .methods = uhub_methods,
188 .size = sizeof(struct uhub_softc)
191 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
192 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
193 MODULE_VERSION(uhub, 1);
196 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
198 struct uhub_softc *sc = usbd_xfer_softc(xfer);
200 switch (USB_GET_STATE(xfer)) {
201 case USB_ST_TRANSFERRED:
204 * This is an indication that some port
205 * has changed status. Notify the bus
206 * event handler thread that we need
207 * to be explored again:
209 usb_needs_explore(sc->sc_udev->bus, 0);
212 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
213 usbd_transfer_submit(xfer);
217 if (xfer->error != USB_ERR_CANCELLED) {
219 * Do a clear-stall. The "stall_pipe" flag
220 * will get cleared before next callback by
223 usbd_xfer_set_stall(xfer);
224 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
225 usbd_transfer_submit(xfer);
231 /*------------------------------------------------------------------------*
234 * This function starts the TT reset USB request
235 *------------------------------------------------------------------------*/
236 #if USB_HAVE_TT_SUPPORT
238 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
240 struct usb_udev_msg *pm = (void *)_pm;
241 struct usb_device *udev = pm->udev;
243 struct uhub_softc *sc;
253 USB_BUS_UNLOCK(udev->bus);
254 mtx_lock(&sc->sc_mtx);
256 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
258 mtx_unlock(&sc->sc_mtx);
259 USB_BUS_LOCK(udev->bus);
263 /*------------------------------------------------------------------------*
264 * uhub_tt_buffer_reset_async_locked
266 * This function queues a TT reset for the given USB device and endpoint.
267 *------------------------------------------------------------------------*/
268 #if USB_HAVE_TT_SUPPORT
270 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
272 struct usb_device_request req;
273 struct usb_device *udev;
279 if (child == NULL || ep == NULL)
282 udev = child->parent_hs_hub;
283 port = child->hs_port_no;
290 (udev->speed != USB_SPEED_HIGH) ||
291 (child->speed != USB_SPEED_LOW &&
292 child->speed != USB_SPEED_FULL) ||
293 (child->flags.usb_mode != USB_MODE_HOST) ||
294 (port == 0) || (ep->edesc == NULL)) {
299 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
301 up = hub->ports + port - 1;
303 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
304 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
307 /* if we already received a clear buffer request, reset the whole TT */
308 if (up->req_reset_tt.bRequest != 0) {
309 req.bmRequestType = UT_WRITE_CLASS_OTHER;
310 req.bRequest = UR_RESET_TT;
311 USETW(req.wValue, 0);
312 req.wIndex[0] = port;
314 USETW(req.wLength, 0);
316 wValue = (ep->edesc->bEndpointAddress & 0xF) |
317 ((child->address & 0x7F) << 4) |
318 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
319 ((ep->edesc->bmAttributes & 3) << 12);
321 req.bmRequestType = UT_WRITE_CLASS_OTHER;
322 req.bRequest = UR_CLEAR_TT_BUFFER;
323 USETW(req.wValue, wValue);
324 req.wIndex[0] = port;
326 USETW(req.wLength, 0);
328 up->req_reset_tt = req;
329 /* get reset transfer started */
330 usb_proc_msignal(&udev->bus->non_giant_callback_proc,
331 &hub->tt_msg[0], &hub->tt_msg[1]);
335 #if USB_HAVE_TT_SUPPORT
337 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
339 struct uhub_softc *sc;
340 struct usb_device *udev;
344 DPRINTF("TT buffer reset\n");
346 sc = usbd_xfer_softc(xfer);
349 switch (USB_GET_STATE(xfer)) {
350 case USB_ST_TRANSFERRED:
353 USB_BUS_LOCK(udev->bus);
354 /* find first port which needs a TT reset */
355 for (x = 0; x != udev->hub->nports; x++) {
356 up = udev->hub->ports + x;
358 if (up->req_reset_tt.bRequest == 0)
361 /* copy in the transfer */
362 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
363 sizeof(up->req_reset_tt));
365 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
368 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
370 USB_BUS_UNLOCK(udev->bus);
372 usbd_transfer_submit(xfer);
375 USB_BUS_UNLOCK(udev->bus);
379 if (error == USB_ERR_CANCELLED)
382 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
388 /*------------------------------------------------------------------------*
389 * uhub_count_active_host_ports
391 * This function counts the number of active ports at the given speed.
392 *------------------------------------------------------------------------*/
394 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
396 struct uhub_softc *sc;
397 struct usb_device *child;
412 for (x = 0; x != hub->nports; x++) {
414 child = usb_bus_port_get_device(udev->bus, up);
416 child->flags.usb_mode == USB_MODE_HOST &&
417 child->speed == speed)
425 uhub_explore_handle_re_enumerate(struct usb_device *child)
430 /* check if device should be re-enumerated */
431 if (child->flags.usb_mode != USB_MODE_HOST)
434 do_unlock = usbd_enum_lock(child);
435 switch (child->re_enumerate_wait) {
436 case USB_RE_ENUM_START:
437 err = usbd_set_config_index(child,
440 DPRINTF("Unconfigure failed: %s: Ignored.\n",
443 if (child->parent_hub == NULL) {
444 /* the root HUB cannot be re-enumerated */
445 DPRINTFN(6, "cannot reset root HUB\n");
448 err = usbd_req_re_enumerate(child, NULL);
451 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 err = usbd_req_get_port_status(
568 sc->sc_udev, NULL, &ps, portno);
570 /* update status regardless of error */
572 sc->sc_st.port_status = UGETW(ps.wPortStatus);
573 sc->sc_st.port_change = UGETW(ps.wPortChange);
575 /* debugging print */
577 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
578 "wPortChange=0x%04x, err=%s\n",
579 portno, sc->sc_st.port_status,
580 sc->sc_st.port_change, usbd_errstr(err));
584 /*------------------------------------------------------------------------*
589 * Else: A control transaction failed
590 *------------------------------------------------------------------------*/
592 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
594 struct usb_device *child;
595 struct usb_device *udev;
596 enum usb_dev_speed speed;
597 enum usb_hc_mode mode;
602 DPRINTF("reattaching port %d\n", portno);
607 child = usb_bus_port_get_device(udev->bus,
608 udev->hub->ports + portno - 1);
612 /* first clear the port connection change bit */
614 err = usbd_req_clear_port_feature(udev, NULL,
615 portno, UHF_C_PORT_CONNECTION);
620 /* check if there is a child */
624 * Free USB device and all subdevices, if any.
626 usb_free_device(child, 0);
629 /* get fresh status */
631 err = uhub_read_port_status(sc, portno);
635 /* check if nothing is connected to the port */
637 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
640 /* check if there is no power on the port and print a warning */
642 switch (udev->speed) {
646 power_mask = UPS_PORT_POWER;
648 case USB_SPEED_SUPER:
649 if (udev->parent_hub == NULL)
650 power_mask = UPS_PORT_POWER;
652 power_mask = UPS_PORT_POWER_SS;
658 if (!(sc->sc_st.port_status & power_mask)) {
659 DPRINTF("WARNING: strange, connected port %d "
660 "has no power\n", portno);
663 /* check if the device is in Host Mode */
665 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
667 DPRINTF("Port %d is in Host Mode\n", portno);
669 if (sc->sc_st.port_status & UPS_SUSPEND) {
671 * NOTE: Should not get here in SuperSpeed
672 * mode, because the HUB should report this
675 DPRINTF("Port %d was still "
676 "suspended, clearing.\n", portno);
677 err = usbd_req_clear_port_feature(udev,
678 NULL, portno, UHF_PORT_SUSPEND);
683 /* wait for maximum device power up time */
686 USB_MS_TO_TICKS(usb_port_powerup_delay));
688 /* reset port, which implies enabling it */
690 err = usbd_req_reset_port(udev, NULL, portno);
693 DPRINTFN(0, "port %d reset "
694 "failed, error=%s\n",
695 portno, usbd_errstr(err));
698 /* get port status again, it might have changed during reset */
700 err = uhub_read_port_status(sc, portno);
704 /* check if something changed during port reset */
706 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
707 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
709 DPRINTFN(0, "giving up port reset "
710 "- device vanished\n");
717 DPRINTF("Port %d is in Device Mode\n", portno);
721 * Figure out the device speed
723 switch (udev->speed) {
725 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
726 speed = USB_SPEED_HIGH;
727 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
728 speed = USB_SPEED_LOW;
730 speed = USB_SPEED_FULL;
733 if (sc->sc_st.port_status & UPS_LOW_SPEED)
734 speed = USB_SPEED_LOW;
736 speed = USB_SPEED_FULL;
739 speed = USB_SPEED_LOW;
741 case USB_SPEED_SUPER:
742 if (udev->parent_hub == NULL) {
743 /* Root HUB - special case */
744 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
746 speed = USB_SPEED_FULL;
749 speed = USB_SPEED_LOW;
752 speed = USB_SPEED_HIGH;
755 speed = USB_SPEED_SUPER;
759 speed = USB_SPEED_SUPER;
763 /* same speed like parent */
767 if (speed == USB_SPEED_SUPER) {
768 err = usbd_req_set_hub_u1_timeout(udev, NULL,
769 portno, 128 - (2 * udev->depth));
771 DPRINTFN(0, "port %d U1 timeout "
772 "failed, error=%s\n",
773 portno, usbd_errstr(err));
775 err = usbd_req_set_hub_u2_timeout(udev, NULL,
776 portno, 128 - (2 * udev->depth));
778 DPRINTFN(0, "port %d U2 timeout "
779 "failed, error=%s\n",
780 portno, usbd_errstr(err));
785 * Figure out the device mode
787 * NOTE: This part is currently FreeBSD specific.
789 if (udev->parent_hub != NULL) {
790 /* inherit mode from the parent HUB */
791 mode = udev->parent_hub->flags.usb_mode;
792 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
793 mode = USB_MODE_DEVICE;
795 mode = USB_MODE_HOST;
797 /* need to create a new child */
798 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
799 udev->depth + 1, portno - 1, portno, speed, mode);
801 DPRINTFN(0, "could not allocate new device\n");
804 return (0); /* success */
809 * Free USB device and all subdevices, if any.
811 usb_free_device(child, 0);
815 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
816 err = usbd_req_clear_port_feature(
818 portno, UHF_PORT_ENABLE);
822 DPRINTFN(0, "device problem (%s), "
823 "disabling port %d\n", usbd_errstr(err), portno);
828 /*------------------------------------------------------------------------*
829 * usb_device_20_compatible
832 * 0: HUB does not support suspend and resume
833 * Else: HUB supports suspend and resume
834 *------------------------------------------------------------------------*/
836 usb_device_20_compatible(struct usb_device *udev)
840 switch (udev->speed) {
850 /*------------------------------------------------------------------------*
851 * uhub_suspend_resume_port
855 * Else: A control transaction failed
856 *------------------------------------------------------------------------*/
858 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
860 struct usb_device *child;
861 struct usb_device *udev;
865 DPRINTF("port %d\n", portno);
868 child = usb_bus_port_get_device(udev->bus,
869 udev->hub->ports + portno - 1);
871 /* first clear the port suspend change bit */
873 if (usb_device_20_compatible(udev)) {
874 err = usbd_req_clear_port_feature(udev, NULL,
875 portno, UHF_C_PORT_SUSPEND);
877 err = usbd_req_clear_port_feature(udev, NULL,
878 portno, UHF_C_PORT_LINK_STATE);
882 DPRINTF("clearing suspend failed.\n");
885 /* get fresh status */
887 err = uhub_read_port_status(sc, portno);
889 DPRINTF("reading port status failed.\n");
892 /* convert current state */
894 if (usb_device_20_compatible(udev)) {
895 if (sc->sc_st.port_status & UPS_SUSPEND) {
901 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
905 case UPS_PORT_LS_SS_INA:
906 usbd_req_warm_reset_port(udev, NULL, portno);
915 DPRINTF("suspended=%u\n", is_suspend);
917 /* do the suspend or resume */
921 * This code handle two cases: 1) Host Mode - we can only
922 * receive resume here 2) Device Mode - we can receive
923 * suspend and resume here
926 usb_dev_resume_peer(child);
927 else if (child->flags.usb_mode == USB_MODE_DEVICE)
928 usb_dev_suspend_peer(child);
934 /*------------------------------------------------------------------------*
935 * uhub_root_interrupt
937 * This function is called when a Root HUB interrupt has
938 * happened. "ptr" and "len" makes up the Root HUB interrupt
939 * packet. This function is called having the "bus_mtx" locked.
940 *------------------------------------------------------------------------*/
942 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
944 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
946 usb_needs_explore(bus, 0);
950 uhub_is_too_deep(struct usb_device *udev)
952 switch (udev->speed) {
956 if (udev->depth > USB_HUB_MAX_DEPTH)
959 case USB_SPEED_SUPER:
960 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
969 /*------------------------------------------------------------------------*
975 *------------------------------------------------------------------------*/
977 uhub_explore(struct usb_device *udev)
980 struct uhub_softc *sc;
990 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
992 /* ignore devices that are too deep */
993 if (uhub_is_too_deep(udev))
994 return (USB_ERR_TOO_DEEP);
996 /* check if device is suspended */
997 if (udev->flags.self_suspended) {
998 /* need to wait until the child signals resume */
999 DPRINTF("Device is suspended!\n");
1004 * Make sure we don't race against user-space applications
1007 do_unlock = usbd_enum_lock(udev);
1009 for (x = 0; x != hub->nports; x++) {
1010 up = hub->ports + x;
1013 err = uhub_read_port_status(sc, portno);
1015 /* most likely the HUB is gone */
1018 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1019 DPRINTF("Overcurrent on port %u.\n", portno);
1020 err = usbd_req_clear_port_feature(
1021 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1023 /* most likely the HUB is gone */
1027 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1029 * Fake a connect status change so that the
1030 * status gets checked initially!
1032 sc->sc_st.port_change |=
1033 UPS_C_CONNECT_STATUS;
1035 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1036 err = usbd_req_clear_port_feature(
1037 udev, NULL, portno, UHF_C_PORT_ENABLE);
1039 /* most likely the HUB is gone */
1042 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1044 * Ignore the port error if the device
1047 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1048 DPRINTFN(0, "illegal enable change, "
1049 "port %d\n", portno);
1052 if (up->restartcnt == USB_RESTART_MAX) {
1053 /* XXX could try another speed ? */
1054 DPRINTFN(0, "port error, giving up "
1055 "port %d\n", portno);
1057 sc->sc_st.port_change |=
1058 UPS_C_CONNECT_STATUS;
1063 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1064 err = uhub_reattach_port(sc, portno);
1066 /* most likely the HUB is gone */
1070 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1071 UPS_C_PORT_LINK_STATE)) {
1072 err = uhub_suspend_resume_port(sc, portno);
1074 /* most likely the HUB is gone */
1078 err = uhub_explore_sub(sc, up);
1080 /* no device(s) present */
1083 /* explore succeeded - reset restart counter */
1088 usbd_enum_unlock(udev);
1090 /* initial status checked */
1091 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1093 /* return success */
1094 return (USB_ERR_NORMAL_COMPLETION);
1098 uhub_probe(device_t dev)
1100 struct usb_attach_arg *uaa = device_get_ivars(dev);
1102 if (uaa->usb_mode != USB_MODE_HOST)
1106 * The subclass for USB HUBs is currently ignored because it
1107 * is 0 for some and 1 for others.
1109 if (uaa->info.bConfigIndex == 0 &&
1110 uaa->info.bDeviceClass == UDCLASS_HUB)
1116 /* NOTE: The information returned by this function can be wrong. */
1118 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1120 struct usb_hub_descriptor hubdesc20;
1121 struct usb_hub_ss_descriptor hubdesc30;
1126 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1127 return (USB_ERR_INVAL);
1132 switch (udev->speed) {
1134 case USB_SPEED_FULL:
1135 case USB_SPEED_HIGH:
1136 /* assuming that there is one port */
1137 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1139 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1140 "error=%s\n", usbd_errstr(err));
1143 nports = hubdesc20.bNbrPorts;
1147 if (udev->speed == USB_SPEED_HIGH)
1148 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1151 case USB_SPEED_SUPER:
1152 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1154 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1155 "error=%s\n", usbd_errstr(err));
1158 nports = hubdesc30.bNbrPorts;
1164 err = USB_ERR_INVAL;
1168 if (pnports != NULL)
1178 uhub_attach(device_t dev)
1180 struct uhub_softc *sc = device_get_softc(dev);
1181 struct usb_attach_arg *uaa = device_get_ivars(dev);
1182 struct usb_device *udev = uaa->device;
1183 struct usb_device *parent_hub = udev->parent_hub;
1184 struct usb_hub *hub;
1185 struct usb_hub_descriptor hubdesc20;
1186 struct usb_hub_ss_descriptor hubdesc30;
1192 uint8_t iface_index;
1198 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1200 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
1201 device_get_nameunit(dev));
1203 device_set_usb_desc(dev);
1205 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1206 "parent->selfpowered=%d\n",
1208 udev->flags.self_powered,
1211 parent_hub->flags.self_powered : 0);
1213 if (uhub_is_too_deep(udev)) {
1214 DPRINTFN(0, "HUB at depth %d, "
1215 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1219 if (!udev->flags.self_powered && parent_hub &&
1220 !parent_hub->flags.self_powered) {
1221 DPRINTFN(0, "Bus powered HUB connected to "
1222 "bus powered HUB. HUB ignored\n");
1226 if (UHUB_IS_MULTI_TT(sc)) {
1227 err = usbd_set_alt_interface_index(udev, 0, 1);
1229 device_printf(dev, "MTT could not be enabled\n");
1232 device_printf(dev, "MTT enabled\n");
1235 /* get HUB descriptor */
1237 DPRINTFN(2, "Getting HUB descriptor\n");
1239 switch (udev->speed) {
1241 case USB_SPEED_FULL:
1242 case USB_SPEED_HIGH:
1243 /* assuming that there is one port */
1244 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1246 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1247 "error=%s\n", usbd_errstr(err));
1250 /* get number of ports */
1251 nports = hubdesc20.bNbrPorts;
1253 /* get power delay */
1254 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1255 usb_extra_power_up_time);
1257 /* get complete HUB descriptor */
1259 /* check number of ports */
1261 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1262 "error=%s\n", usbd_errstr(err));
1265 /* get complete HUB descriptor */
1266 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1269 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1270 "error=%s\n", usbd_errstr(err));
1273 if (hubdesc20.bNbrPorts != nports) {
1274 DPRINTFN(0, "Number of ports changed\n");
1279 case USB_SPEED_SUPER:
1280 if (udev->parent_hub != NULL) {
1281 err = usbd_req_set_hub_depth(udev, NULL,
1284 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1285 "error=%s\n", usbd_errstr(err));
1289 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1291 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1292 "error=%s\n", usbd_errstr(err));
1295 /* get number of ports */
1296 nports = hubdesc30.bNbrPorts;
1298 /* get power delay */
1299 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1300 usb_extra_power_up_time);
1302 /* get complete HUB descriptor */
1304 /* check number of ports */
1305 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1306 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1307 "error=%s\n", usbd_errstr(err));
1310 /* get complete HUB descriptor */
1311 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1314 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1315 "error=%s\n", usbd_errstr(err));
1318 if (hubdesc30.bNbrPorts != nports) {
1319 DPRINTFN(0, "Number of ports changed\n");
1325 DPRINTF("Assuming HUB has only one port\n");
1326 /* default number of ports */
1328 /* default power delay */
1329 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1333 DPRINTFN(0, "portless HUB\n");
1336 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1337 M_USBDEV, M_WAITOK | M_ZERO);
1344 /* initialize HUB structure */
1346 hub->explore = &uhub_explore;
1347 hub->nports = nports;
1348 hub->hubudev = udev;
1349 #if USB_HAVE_TT_SUPPORT
1350 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1351 hub->tt_msg[0].udev = udev;
1352 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1353 hub->tt_msg[1].udev = udev;
1355 /* if self powered hub, give ports maximum current */
1356 if (udev->flags.self_powered) {
1357 hub->portpower = USB_MAX_POWER;
1359 hub->portpower = USB_MIN_POWER;
1362 /* set up interrupt pipe */
1364 if (udev->parent_hub == NULL) {
1365 /* root HUB is special */
1369 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1370 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1373 DPRINTFN(0, "cannot setup interrupt transfer, "
1374 "errstr=%s\n", usbd_errstr(err));
1377 /* wait with power off for a while */
1378 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1381 * To have the best chance of success we do things in the exact same
1382 * order as Windoze98. This should not be necessary, but some
1383 * devices do not follow the USB specs to the letter.
1385 * These are the events on the bus when a hub is attached:
1386 * Get device and config descriptors (see attach code)
1387 * Get hub descriptor (see above)
1390 * wait for power to become stable
1391 * (all below happens in explore code)
1393 * clear C_PORT_CONNECTION
1396 * if device connected
1400 * clear C_PORT_RESET
1402 * proceed with device attachment
1405 /* XXX should check for none, individual, or ganged power? */
1409 for (x = 0; x != nports; x++) {
1410 /* set up data structures */
1411 struct usb_port *up = hub->ports + x;
1413 up->device_index = 0;
1417 /* check if port is removable */
1418 switch (udev->speed) {
1420 case USB_SPEED_FULL:
1421 case USB_SPEED_HIGH:
1422 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1425 case USB_SPEED_SUPER:
1426 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1430 DPRINTF("Assuming removable port\n");
1435 /* turn the power on */
1436 err = usbd_req_set_port_feature(udev, NULL,
1437 portno, UHF_PORT_POWER);
1440 DPRINTFN(0, "port %d power on failed, %s\n",
1441 portno, usbd_errstr(err));
1443 DPRINTF("turn on port %d power\n",
1446 /* wait for stable power */
1447 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1450 device_printf(dev, "%d port%s with %d "
1451 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1452 removable, udev->flags.self_powered ? "self" : "bus");
1454 /* Start the interrupt endpoint, if any */
1456 mtx_lock(&sc->sc_mtx);
1457 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1458 mtx_unlock(&sc->sc_mtx);
1460 /* Enable automatic power save on all USB HUBs */
1462 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1467 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1470 free(udev->hub, M_USBDEV);
1474 mtx_destroy(&sc->sc_mtx);
1480 * Called from process context when the hub is gone.
1481 * Detach all devices on active ports.
1484 uhub_detach(device_t dev)
1486 struct uhub_softc *sc = device_get_softc(dev);
1487 struct usb_hub *hub = sc->sc_udev->hub;
1488 struct usb_bus *bus = sc->sc_udev->bus;
1489 struct usb_device *child;
1492 if (hub == NULL) /* must be partially working */
1495 /* Make sure interrupt transfer is gone. */
1496 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1498 /* Detach all ports */
1499 for (x = 0; x != hub->nports; x++) {
1501 child = usb_bus_port_get_device(bus, hub->ports + x);
1503 if (child == NULL) {
1508 * Free USB device and all subdevices, if any.
1510 usb_free_device(child, 0);
1513 #if USB_HAVE_TT_SUPPORT
1514 /* Make sure our TT messages are not queued anywhere */
1516 usb_proc_mwait(&bus->non_giant_callback_proc,
1517 &hub->tt_msg[0], &hub->tt_msg[1]);
1518 USB_BUS_UNLOCK(bus);
1520 free(hub, M_USBDEV);
1521 sc->sc_udev->hub = NULL;
1523 mtx_destroy(&sc->sc_mtx);
1529 uhub_suspend(device_t dev)
1532 /* Sub-devices are not suspended here! */
1537 uhub_resume(device_t dev)
1540 /* Sub-devices are not resumed here! */
1545 uhub_driver_added(device_t dev, driver_t *driver)
1547 usb_needs_explore_all();
1551 struct usb_device *udev;
1553 uint8_t iface_index;
1557 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1558 struct hub_result *res)
1560 struct usb_interface *iface;
1561 struct usb_device *udev;
1566 nports = hub->nports;
1567 for (x = 0; x != nports; x++) {
1568 udev = usb_bus_port_get_device(hub->hubudev->bus,
1573 for (i = 0; i != USB_IFACE_MAX; i++) {
1574 iface = usbd_get_iface(udev, i);
1576 (iface->subdev == child)) {
1577 res->iface_index = i;
1579 res->portno = x + 1;
1584 res->iface_index = 0;
1590 uhub_child_location_string(device_t parent, device_t child,
1591 char *buf, size_t buflen)
1593 struct uhub_softc *sc;
1594 struct usb_hub *hub;
1595 struct hub_result res;
1597 if (!device_is_attached(parent)) {
1603 sc = device_get_softc(parent);
1604 hub = sc->sc_udev->hub;
1607 uhub_find_iface_index(hub, child, &res);
1609 DPRINTF("device not on hub\n");
1615 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1616 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1617 res.portno, device_get_unit(res.udev->bus->bdev),
1618 res.udev->device_index, res.iface_index);
1626 uhub_child_pnpinfo_string(device_t parent, device_t child,
1627 char *buf, size_t buflen)
1629 struct uhub_softc *sc;
1630 struct usb_hub *hub;
1631 struct usb_interface *iface;
1632 struct hub_result res;
1634 if (!device_is_attached(parent)) {
1640 sc = device_get_softc(parent);
1641 hub = sc->sc_udev->hub;
1644 uhub_find_iface_index(hub, child, &res);
1646 DPRINTF("device not on hub\n");
1652 iface = usbd_get_iface(res.udev, res.iface_index);
1653 if (iface && iface->idesc) {
1654 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1655 "devclass=0x%02x devsubclass=0x%02x "
1659 "intclass=0x%02x intsubclass=0x%02x "
1660 "intprotocol=0x%02x " "%s%s",
1661 UGETW(res.udev->ddesc.idVendor),
1662 UGETW(res.udev->ddesc.idProduct),
1663 res.udev->ddesc.bDeviceClass,
1664 res.udev->ddesc.bDeviceSubClass,
1665 usb_get_serial(res.udev),
1666 UGETW(res.udev->ddesc.bcdDevice),
1667 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1668 iface->idesc->bInterfaceClass,
1669 iface->idesc->bInterfaceSubClass,
1670 iface->idesc->bInterfaceProtocol,
1671 iface->pnpinfo ? " " : "",
1672 iface->pnpinfo ? iface->pnpinfo : "");
1686 * The USB Transaction Translator:
1687 * ===============================
1689 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1690 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1691 * USB transfers. To utilize bandwidth dynamically the "scatter and
1692 * gather" principle must be applied. This means that bandwidth must
1693 * be divided into equal parts of bandwidth. With regard to USB all
1694 * data is transferred in smaller packets with length
1695 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1698 * The bandwidth scheduler which I have implemented will simply pack
1699 * the USB transfers back to back until there is no more space in the
1700 * schedule. Out of the 8 microframes which the USB 2.0 standard
1701 * provides, only 6 are available for non-HIGH-speed devices. I have
1702 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1703 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1704 * this division, it is very difficult to allocate and free bandwidth
1707 * NOTE about the Transaction Translator in USB HUBs:
1709 * USB HUBs have a very simple Transaction Translator, that will
1710 * simply pipeline all the SPLIT transactions. That means that the
1711 * transactions will be executed in the order they are queued!
1715 /*------------------------------------------------------------------------*
1716 * usb_intr_find_best_slot
1719 * The best Transaction Translation slot for an interrupt endpoint.
1720 *------------------------------------------------------------------------*/
1722 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1723 uint8_t end, uint8_t mask)
1725 usb_size_t min = (usb_size_t)-1;
1733 /* find the last slot with lesser used bandwidth */
1735 for (x = start; x < end; x++) {
1739 /* compute sum of bandwidth */
1740 for (z = x; z < end; z++) {
1741 if (mask & (1U << (z - x)))
1745 /* check if the current multi-slot is more optimal */
1751 /* check if the mask is about to be shifted out */
1752 if (mask & (1U << (end - 1 - x)))
1758 /*------------------------------------------------------------------------*
1759 * usb_hs_bandwidth_adjust
1761 * This function will update the bandwith usage for the microframe
1762 * having index "slot" by "len" bytes. "len" can be negative. If the
1763 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1764 * the "slot" argument will be replaced by the slot having least used
1765 * bandwidth. The "mask" argument is used for multi-slot allocations.
1768 * The slot in which the bandwidth update was done: 0..7
1769 *------------------------------------------------------------------------*/
1771 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1772 uint8_t slot, uint8_t mask)
1774 struct usb_bus *bus = udev->bus;
1775 struct usb_hub *hub;
1776 enum usb_dev_speed speed;
1779 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1781 speed = usbd_get_speed(udev);
1785 case USB_SPEED_FULL:
1786 if (speed == USB_SPEED_LOW) {
1790 * The Host Controller Driver should have
1791 * performed checks so that the lookup
1792 * below does not result in a NULL pointer
1796 hub = udev->parent_hs_hub->hub;
1797 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1798 slot = usb_intr_find_best_slot(hub->uframe_usage,
1799 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1801 for (x = slot; x < 8; x++) {
1802 if (mask & (1U << (x - slot))) {
1803 hub->uframe_usage[x] += len;
1804 bus->uframe_usage[x] += len;
1809 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1810 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1811 USB_HS_MICRO_FRAMES_MAX, mask);
1813 for (x = slot; x < 8; x++) {
1814 if (mask & (1U << (x - slot))) {
1815 bus->uframe_usage[x] += len;
1823 /*------------------------------------------------------------------------*
1824 * usb_hs_bandwidth_alloc
1826 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1827 *------------------------------------------------------------------------*/
1829 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1831 struct usb_device *udev;
1836 udev = xfer->xroot->udev;
1838 if (udev->flags.usb_mode != USB_MODE_HOST)
1839 return; /* not supported */
1841 xfer->endpoint->refcount_bw++;
1842 if (xfer->endpoint->refcount_bw != 1)
1843 return; /* already allocated */
1845 speed = usbd_get_speed(udev);
1847 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1849 /* allocate a microframe slot */
1852 slot = usb_hs_bandwidth_adjust(udev,
1853 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1855 xfer->endpoint->usb_uframe = slot;
1856 xfer->endpoint->usb_smask = mask << slot;
1858 if ((speed != USB_SPEED_FULL) &&
1859 (speed != USB_SPEED_LOW)) {
1860 xfer->endpoint->usb_cmask = 0x00 ;
1862 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1866 case UE_ISOCHRONOUS:
1867 switch (usbd_xfer_get_fps_shift(xfer)) {
1882 /* allocate a microframe multi-slot */
1884 slot = usb_hs_bandwidth_adjust(udev,
1885 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1887 xfer->endpoint->usb_uframe = slot;
1888 xfer->endpoint->usb_cmask = 0;
1889 xfer->endpoint->usb_smask = mask << slot;
1893 xfer->endpoint->usb_uframe = 0;
1894 xfer->endpoint->usb_cmask = 0;
1895 xfer->endpoint->usb_smask = 0;
1899 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1900 xfer->endpoint->usb_uframe,
1901 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1904 /*------------------------------------------------------------------------*
1905 * usb_hs_bandwidth_free
1907 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1908 *------------------------------------------------------------------------*/
1910 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1912 struct usb_device *udev;
1916 udev = xfer->xroot->udev;
1918 if (udev->flags.usb_mode != USB_MODE_HOST)
1919 return; /* not supported */
1921 xfer->endpoint->refcount_bw--;
1922 if (xfer->endpoint->refcount_bw != 0)
1923 return; /* still allocated */
1925 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1927 case UE_ISOCHRONOUS:
1929 slot = xfer->endpoint->usb_uframe;
1930 mask = xfer->endpoint->usb_smask;
1932 /* free microframe slot(s): */
1933 usb_hs_bandwidth_adjust(udev,
1934 -xfer->max_frame_size, slot, mask >> slot);
1936 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1937 slot, mask >> slot);
1939 xfer->endpoint->usb_uframe = 0;
1940 xfer->endpoint->usb_cmask = 0;
1941 xfer->endpoint->usb_smask = 0;
1949 /*------------------------------------------------------------------------*
1950 * usb_isoc_time_expand
1952 * This function will expand the time counter from 7-bit to 16-bit.
1955 * 16-bit isochronous time counter.
1956 *------------------------------------------------------------------------*/
1958 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1962 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1964 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1966 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1968 if (isoc_time_curr < rem) {
1969 /* the time counter wrapped around */
1970 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1972 /* update the remainder */
1974 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1975 bus->isoc_time_last |= isoc_time_curr;
1977 return (bus->isoc_time_last);
1980 /*------------------------------------------------------------------------*
1981 * usbd_fs_isoc_schedule_alloc_slot
1983 * This function will allocate bandwidth for an isochronous FULL speed
1984 * transaction in the FULL speed schedule.
1989 *------------------------------------------------------------------------*/
1990 #if USB_HAVE_TT_SUPPORT
1992 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1994 struct usb_xfer *xfer;
1995 struct usb_xfer *pipe_xfer;
1996 struct usb_bus *bus;
1998 usb_frlength_t data_len;
2006 bus = isoc_xfer->xroot->bus;
2008 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2010 /* skip self, if any */
2012 if (xfer == isoc_xfer)
2015 /* check if this USB transfer is going through the same TT */
2017 if (xfer->xroot->udev->parent_hs_hub !=
2018 isoc_xfer->xroot->udev->parent_hs_hub) {
2021 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2022 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2023 (xfer->xroot->udev->hs_port_no !=
2024 isoc_xfer->xroot->udev->hs_port_no)) {
2027 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2030 /* check if isoc_time is part of this transfer */
2032 delta = xfer->isoc_time_complete - isoc_time;
2033 if (delta > 0 && delta <= xfer->nframes) {
2034 delta = xfer->nframes - delta;
2036 len = xfer->frlengths[delta];
2044 /* check double buffered transfers */
2046 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
2049 /* skip self, if any */
2051 if (pipe_xfer == isoc_xfer)
2054 /* check if isoc_time is part of this transfer */
2056 delta = pipe_xfer->isoc_time_complete - isoc_time;
2057 if (delta > 0 && delta <= pipe_xfer->nframes) {
2058 delta = pipe_xfer->nframes - delta;
2060 len = pipe_xfer->frlengths[delta];
2070 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2071 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2075 /* check for overflow */
2077 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2082 delta = isoc_xfer->isoc_time_complete - isoc_time;
2083 if (delta > 0 && delta <= isoc_xfer->nframes) {
2084 delta = isoc_xfer->nframes - delta;
2086 len = isoc_xfer->frlengths[delta];
2094 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2095 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2099 /* check for overflow */
2101 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2108 /*------------------------------------------------------------------------*
2109 * usb_bus_port_get_device
2111 * This function is NULL safe.
2112 *------------------------------------------------------------------------*/
2114 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2116 if ((bus == NULL) || (up == NULL)) {
2120 if (up->device_index == 0) {
2124 return (bus->devices[up->device_index]);
2127 /*------------------------------------------------------------------------*
2128 * usb_bus_port_set_device
2130 * This function is NULL safe.
2131 *------------------------------------------------------------------------*/
2133 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2134 struct usb_device *udev, uint8_t device_index)
2141 * There is only one case where we don't
2142 * have an USB port, and that is the Root Hub!
2146 up->device_index = device_index;
2148 device_index = up->device_index;
2149 up->device_index = 0;
2153 * Make relationships to our new device
2155 if (device_index != 0) {
2157 mtx_lock(&usb_ref_lock);
2159 bus->devices[device_index] = udev;
2161 mtx_unlock(&usb_ref_lock);
2167 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2170 /*------------------------------------------------------------------------*
2173 * This functions is called when the USB event thread needs to run.
2174 *------------------------------------------------------------------------*/
2176 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2183 DPRINTF("No bus pointer!\n");
2186 if ((bus->devices == NULL) ||
2187 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2188 DPRINTF("No root HUB\n");
2191 if (mtx_owned(&bus->bus_mtx)) {
2200 if (usb_proc_msignal(&bus->explore_proc,
2201 &bus->explore_msg[0], &bus->explore_msg[1])) {
2205 USB_BUS_UNLOCK(bus);
2209 /*------------------------------------------------------------------------*
2210 * usb_needs_explore_all
2212 * This function is called whenever a new driver is loaded and will
2213 * cause that all USB busses are re-explored.
2214 *------------------------------------------------------------------------*/
2216 usb_needs_explore_all(void)
2218 struct usb_bus *bus;
2225 dc = usb_devclass_ptr;
2227 DPRINTFN(0, "no devclass\n");
2231 * Explore all USB busses in parallell.
2233 max = devclass_get_maxunit(dc);
2235 dev = devclass_get_device(dc, max);
2237 bus = device_get_softc(dev);
2239 usb_needs_explore(bus, 1);
2246 /*------------------------------------------------------------------------*
2247 * usb_bus_power_update
2249 * This function will ensure that all USB devices on the given bus are
2250 * properly suspended or resumed according to the device transfer
2252 *------------------------------------------------------------------------*/
2255 usb_bus_power_update(struct usb_bus *bus)
2257 usb_needs_explore(bus, 0 /* no probe */ );
2261 /*------------------------------------------------------------------------*
2262 * usbd_transfer_power_ref
2264 * This function will modify the power save reference counts and
2265 * wakeup the USB device associated with the given USB transfer, if
2267 *------------------------------------------------------------------------*/
2270 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2272 static const usb_power_mask_t power_mask[4] = {
2273 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2274 [UE_BULK] = USB_HW_POWER_BULK,
2275 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2276 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2278 struct usb_device *udev;
2279 uint8_t needs_explore;
2280 uint8_t needs_hw_power;
2283 udev = xfer->xroot->udev;
2285 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2286 /* no power save for root HUB */
2289 USB_BUS_LOCK(udev->bus);
2291 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2293 udev->pwr_save.last_xfer_time = ticks;
2294 udev->pwr_save.type_refs[xfer_type] += val;
2296 if (xfer->flags_int.control_xfr) {
2297 udev->pwr_save.read_refs += val;
2298 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2300 * It is not allowed to suspend during a
2303 udev->pwr_save.write_refs += val;
2305 } else if (USB_GET_DATA_ISREAD(xfer)) {
2306 udev->pwr_save.read_refs += val;
2308 udev->pwr_save.write_refs += val;
2312 if (udev->flags.self_suspended)
2313 needs_explore = usb_peer_should_wakeup(udev);
2317 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2318 DPRINTF("Adding type %u to power state\n", xfer_type);
2319 udev->bus->hw_power_state |= power_mask[xfer_type];
2329 USB_BUS_UNLOCK(udev->bus);
2331 if (needs_explore) {
2332 DPRINTF("update\n");
2333 usb_bus_power_update(udev->bus);
2334 } else if (needs_hw_power) {
2335 DPRINTF("needs power\n");
2336 if (udev->bus->methods->set_hw_power != NULL) {
2337 (udev->bus->methods->set_hw_power) (udev->bus);
2343 /*------------------------------------------------------------------------*
2344 * usb_peer_should_wakeup
2346 * This function returns non-zero if the current device should wake up.
2347 *------------------------------------------------------------------------*/
2349 usb_peer_should_wakeup(struct usb_device *udev)
2351 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2352 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2353 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2354 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2355 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2356 (udev->pwr_save.write_refs != 0) ||
2357 ((udev->pwr_save.read_refs != 0) &&
2358 (udev->flags.usb_mode == USB_MODE_HOST) &&
2359 (usb_peer_can_wakeup(udev) == 0)));
2362 /*------------------------------------------------------------------------*
2365 * This function implements the USB power daemon and is called
2366 * regularly from the USB explore thread.
2367 *------------------------------------------------------------------------*/
2370 usb_bus_powerd(struct usb_bus *bus)
2372 struct usb_device *udev;
2375 usb_ticks_t mintime;
2376 usb_size_t type_refs[5];
2379 limit = usb_power_timeout;
2382 else if (limit > 255)
2387 DPRINTF("bus=%p\n", bus);
2392 * The root HUB device is never suspended
2393 * and we simply skip it.
2395 for (x = USB_ROOT_HUB_ADDR + 1;
2396 x != bus->devices_max; x++) {
2398 udev = bus->devices[x];
2402 temp = ticks - udev->pwr_save.last_xfer_time;
2404 if (usb_peer_should_wakeup(udev)) {
2405 /* check if we are suspended */
2406 if (udev->flags.self_suspended != 0) {
2407 USB_BUS_UNLOCK(bus);
2408 usb_dev_resume_peer(udev);
2411 } else if ((temp >= limit) &&
2412 (udev->flags.usb_mode == USB_MODE_HOST) &&
2413 (udev->flags.self_suspended == 0)) {
2414 /* try to do suspend */
2416 USB_BUS_UNLOCK(bus);
2417 usb_dev_suspend_peer(udev);
2422 /* reset counters */
2424 mintime = (usb_ticks_t)-1;
2431 /* Re-loop all the devices to get the actual state */
2433 for (x = USB_ROOT_HUB_ADDR + 1;
2434 x != bus->devices_max; x++) {
2436 udev = bus->devices[x];
2440 /* we found a non-Root-Hub USB device */
2443 /* "last_xfer_time" can be updated by a resume */
2444 temp = ticks - udev->pwr_save.last_xfer_time;
2447 * Compute minimum time since last transfer for the complete
2453 if (udev->flags.self_suspended == 0) {
2454 type_refs[0] += udev->pwr_save.type_refs[0];
2455 type_refs[1] += udev->pwr_save.type_refs[1];
2456 type_refs[2] += udev->pwr_save.type_refs[2];
2457 type_refs[3] += udev->pwr_save.type_refs[3];
2461 if (mintime >= (usb_ticks_t)(1 * hz)) {
2462 /* recompute power masks */
2463 DPRINTF("Recomputing power masks\n");
2464 bus->hw_power_state = 0;
2465 if (type_refs[UE_CONTROL] != 0)
2466 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2467 if (type_refs[UE_BULK] != 0)
2468 bus->hw_power_state |= USB_HW_POWER_BULK;
2469 if (type_refs[UE_INTERRUPT] != 0)
2470 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2471 if (type_refs[UE_ISOCHRONOUS] != 0)
2472 bus->hw_power_state |= USB_HW_POWER_ISOC;
2473 if (type_refs[4] != 0)
2474 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2476 USB_BUS_UNLOCK(bus);
2478 if (bus->methods->set_hw_power != NULL) {
2479 /* always update hardware power! */
2480 (bus->methods->set_hw_power) (bus);
2486 /*------------------------------------------------------------------------*
2487 * usb_dev_resume_peer
2489 * This function will resume an USB peer and do the required USB
2490 * signalling to get an USB device out of the suspended state.
2491 *------------------------------------------------------------------------*/
2493 usb_dev_resume_peer(struct usb_device *udev)
2495 struct usb_bus *bus;
2502 /* check if already resumed */
2503 if (udev->flags.self_suspended == 0)
2506 /* we need a parent HUB to do resume */
2507 if (udev->parent_hub == NULL)
2510 DPRINTF("udev=%p\n", udev);
2512 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2513 (udev->flags.remote_wakeup == 0)) {
2515 * If the host did not set the remote wakeup feature, we can
2516 * not wake it up either!
2518 DPRINTF("remote wakeup is not set!\n");
2521 /* get bus pointer */
2524 /* resume parent hub first */
2525 usb_dev_resume_peer(udev->parent_hub);
2527 /* reduce chance of instant resume failure by waiting a little bit */
2528 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2530 if (usb_device_20_compatible(udev)) {
2531 /* resume current port (Valid in Host and Device Mode) */
2532 err = usbd_req_clear_port_feature(udev->parent_hub,
2533 NULL, udev->port_no, UHF_PORT_SUSPEND);
2535 DPRINTFN(0, "Resuming port failed\n");
2539 /* resume current port (Valid in Host and Device Mode) */
2540 err = usbd_req_set_port_link_state(udev->parent_hub,
2541 NULL, udev->port_no, UPS_PORT_LS_U0);
2543 DPRINTFN(0, "Resuming port failed\n");
2548 /* resume settle time */
2549 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2551 if (bus->methods->device_resume != NULL) {
2552 /* resume USB device on the USB controller */
2553 (bus->methods->device_resume) (udev);
2556 /* set that this device is now resumed */
2557 udev->flags.self_suspended = 0;
2559 /* make sure that we don't go into suspend right away */
2560 udev->pwr_save.last_xfer_time = ticks;
2562 /* make sure the needed power masks are on */
2563 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2564 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2565 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2566 bus->hw_power_state |= USB_HW_POWER_BULK;
2567 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2568 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2569 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2570 bus->hw_power_state |= USB_HW_POWER_ISOC;
2572 USB_BUS_UNLOCK(bus);
2574 if (bus->methods->set_hw_power != NULL) {
2575 /* always update hardware power! */
2576 (bus->methods->set_hw_power) (bus);
2581 /* notify all sub-devices about resume */
2582 err = usb_suspend_resume(udev, 0);
2584 usbd_sr_unlock(udev);
2586 /* check if peer has wakeup capability */
2587 if (usb_peer_can_wakeup(udev)) {
2588 /* clear remote wakeup */
2589 err = usbd_req_clear_device_feature(udev,
2590 NULL, UF_DEVICE_REMOTE_WAKEUP);
2592 DPRINTFN(0, "Clearing device "
2593 "remote wakeup failed: %s\n",
2599 /*------------------------------------------------------------------------*
2600 * usb_dev_suspend_peer
2602 * This function will suspend an USB peer and do the required USB
2603 * signalling to get an USB device into the suspended state.
2604 *------------------------------------------------------------------------*/
2606 usb_dev_suspend_peer(struct usb_device *udev)
2608 struct usb_device *child;
2618 /* check if already suspended */
2619 if (udev->flags.self_suspended)
2622 /* we need a parent HUB to do suspend */
2623 if (udev->parent_hub == NULL)
2626 DPRINTF("udev=%p\n", udev);
2628 /* check if the current device is a HUB */
2629 if (udev->hub != NULL) {
2630 nports = udev->hub->nports;
2632 /* check if all devices on the HUB are suspended */
2633 for (x = 0; x != nports; x++) {
2634 child = usb_bus_port_get_device(udev->bus,
2635 udev->hub->ports + x);
2640 if (child->flags.self_suspended)
2643 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2648 if (usb_peer_can_wakeup(udev)) {
2650 * This request needs to be done before we set
2651 * "udev->flags.self_suspended":
2654 /* allow device to do remote wakeup */
2655 err = usbd_req_set_device_feature(udev,
2656 NULL, UF_DEVICE_REMOTE_WAKEUP);
2658 DPRINTFN(0, "Setting device "
2659 "remote wakeup failed\n");
2663 USB_BUS_LOCK(udev->bus);
2665 * Checking for suspend condition and setting suspended bit
2668 err = usb_peer_should_wakeup(udev);
2671 * Set that this device is suspended. This variable
2672 * must be set before calling USB controller suspend
2675 udev->flags.self_suspended = 1;
2677 USB_BUS_UNLOCK(udev->bus);
2680 if (usb_peer_can_wakeup(udev)) {
2681 /* allow device to do remote wakeup */
2682 err = usbd_req_clear_device_feature(udev,
2683 NULL, UF_DEVICE_REMOTE_WAKEUP);
2685 DPRINTFN(0, "Setting device "
2686 "remote wakeup failed\n");
2690 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2691 /* resume parent HUB first */
2692 usb_dev_resume_peer(udev->parent_hub);
2694 /* reduce chance of instant resume failure by waiting a little bit */
2695 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2697 /* resume current port (Valid in Host and Device Mode) */
2698 err = usbd_req_clear_port_feature(udev->parent_hub,
2699 NULL, udev->port_no, UHF_PORT_SUSPEND);
2701 /* resume settle time */
2702 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2704 DPRINTF("Suspend was cancelled!\n");
2710 /* notify all sub-devices about suspend */
2711 err = usb_suspend_resume(udev, 1);
2713 usbd_sr_unlock(udev);
2715 if (udev->bus->methods->device_suspend != NULL) {
2718 /* suspend device on the USB controller */
2719 (udev->bus->methods->device_suspend) (udev);
2722 temp = usbd_get_dma_delay(udev);
2724 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2728 if (usb_device_20_compatible(udev)) {
2729 /* suspend current port */
2730 err = usbd_req_set_port_feature(udev->parent_hub,
2731 NULL, udev->port_no, UHF_PORT_SUSPEND);
2733 DPRINTFN(0, "Suspending port failed\n");
2737 /* suspend current port */
2738 err = usbd_req_set_port_link_state(udev->parent_hub,
2739 NULL, udev->port_no, UPS_PORT_LS_U3);
2741 DPRINTFN(0, "Suspending port failed\n");
2746 udev = udev->parent_hub;
2750 /*------------------------------------------------------------------------*
2751 * usbd_set_power_mode
2753 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2755 *------------------------------------------------------------------------*/
2757 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2759 /* filter input argument */
2760 if ((power_mode != USB_POWER_MODE_ON) &&
2761 (power_mode != USB_POWER_MODE_OFF))
2762 power_mode = USB_POWER_MODE_SAVE;
2764 power_mode = usbd_filter_power_mode(udev, power_mode);
2766 udev->power_mode = power_mode; /* update copy of power mode */
2769 usb_bus_power_update(udev->bus);
2771 usb_needs_explore(udev->bus, 0 /* no probe */ );
2775 /*------------------------------------------------------------------------*
2776 * usbd_filter_power_mode
2778 * This function filters the power mode based on hardware requirements.
2779 *------------------------------------------------------------------------*/
2781 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2783 struct usb_bus_methods *mtod;
2786 mtod = udev->bus->methods;
2789 if (mtod->get_power_mode != NULL)
2790 (mtod->get_power_mode) (udev, &temp);
2792 /* check if we should not filter */
2794 return (power_mode);
2796 /* use fixed power mode given by hardware driver */
2800 /*------------------------------------------------------------------------*
2801 * usbd_start_re_enumerate
2803 * This function starts re-enumeration of the given USB device. This
2804 * function does not need to be called BUS-locked. This function does
2805 * not wait until the re-enumeration is completed.
2806 *------------------------------------------------------------------------*/
2808 usbd_start_re_enumerate(struct usb_device *udev)
2810 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2811 udev->re_enumerate_wait = USB_RE_ENUM_START;
2812 usb_needs_explore(udev->bus, 0);
2816 /*-----------------------------------------------------------------------*
2817 * usbd_start_set_config
2819 * This function starts setting a USB configuration. This function
2820 * does not need to be called BUS-locked. This function does not wait
2821 * until the set USB configuratino is completed.
2822 *------------------------------------------------------------------------*/
2824 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2826 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2827 if (udev->curr_config_index == index) {
2828 /* no change needed */
2831 udev->next_config_index = index;
2832 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2833 usb_needs_explore(udev->bus, 0);
2835 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2836 if (udev->next_config_index == index) {
2837 /* no change needed */
2841 return (USB_ERR_PENDING_REQUESTS);