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);
606 child = usb_bus_port_get_device(udev->bus,
607 udev->hub->ports + portno - 1);
611 /* first clear the port connection change bit */
613 err = usbd_req_clear_port_feature(udev, NULL,
614 portno, UHF_C_PORT_CONNECTION);
619 /* check if there is a child */
623 * Free USB device and all subdevices, if any.
625 usb_free_device(child, 0);
628 /* get fresh status */
630 err = uhub_read_port_status(sc, portno);
634 /* check if nothing is connected to the port */
636 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
639 /* check if there is no power on the port and print a warning */
641 switch (udev->speed) {
645 power_mask = UPS_PORT_POWER;
647 case USB_SPEED_SUPER:
648 if (udev->parent_hub == NULL)
649 power_mask = UPS_PORT_POWER;
651 power_mask = UPS_PORT_POWER_SS;
657 if (!(sc->sc_st.port_status & power_mask)) {
658 DPRINTF("WARNING: strange, connected port %d "
659 "has no power\n", portno);
662 /* check if the device is in Host Mode */
664 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
666 DPRINTF("Port %d is in Host Mode\n", portno);
668 if (sc->sc_st.port_status & UPS_SUSPEND) {
670 * NOTE: Should not get here in SuperSpeed
671 * mode, because the HUB should report this
674 DPRINTF("Port %d was still "
675 "suspended, clearing.\n", portno);
676 err = usbd_req_clear_port_feature(udev,
677 NULL, portno, UHF_PORT_SUSPEND);
682 /* wait for maximum device power up time */
685 USB_MS_TO_TICKS(usb_port_powerup_delay));
687 /* reset port, which implies enabling it */
689 err = usbd_req_reset_port(udev, NULL, portno);
692 DPRINTFN(0, "port %d reset "
693 "failed, error=%s\n",
694 portno, usbd_errstr(err));
697 /* get port status again, it might have changed during reset */
699 err = uhub_read_port_status(sc, portno);
703 /* check if something changed during port reset */
705 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
706 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
708 DPRINTFN(0, "giving up port reset "
709 "- device vanished\n");
716 DPRINTF("Port %d is in Device Mode\n", portno);
720 * Figure out the device speed
722 switch (udev->speed) {
724 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
725 speed = USB_SPEED_HIGH;
726 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
727 speed = USB_SPEED_LOW;
729 speed = USB_SPEED_FULL;
732 if (sc->sc_st.port_status & UPS_LOW_SPEED)
733 speed = USB_SPEED_LOW;
735 speed = USB_SPEED_FULL;
738 speed = USB_SPEED_LOW;
740 case USB_SPEED_SUPER:
741 if (udev->parent_hub == NULL) {
742 /* Root HUB - special case */
743 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
745 speed = USB_SPEED_FULL;
748 speed = USB_SPEED_LOW;
751 speed = USB_SPEED_HIGH;
754 speed = USB_SPEED_SUPER;
758 speed = USB_SPEED_SUPER;
762 /* same speed like parent */
766 if (speed == USB_SPEED_SUPER) {
767 err = usbd_req_set_hub_u1_timeout(udev, NULL,
768 portno, 128 - (2 * udev->depth));
770 DPRINTFN(0, "port %d U1 timeout "
771 "failed, error=%s\n",
772 portno, usbd_errstr(err));
774 err = usbd_req_set_hub_u2_timeout(udev, NULL,
775 portno, 128 - (2 * udev->depth));
777 DPRINTFN(0, "port %d U2 timeout "
778 "failed, error=%s\n",
779 portno, usbd_errstr(err));
784 * Figure out the device mode
786 * NOTE: This part is currently FreeBSD specific.
788 if (udev->parent_hub != NULL) {
789 /* inherit mode from the parent HUB */
790 mode = udev->parent_hub->flags.usb_mode;
791 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
792 mode = USB_MODE_DEVICE;
794 mode = USB_MODE_HOST;
796 /* need to create a new child */
797 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
798 udev->depth + 1, portno - 1, portno, speed, mode);
800 DPRINTFN(0, "could not allocate new device\n");
803 return (0); /* success */
808 * Free USB device and all subdevices, if any.
810 usb_free_device(child, 0);
814 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
815 err = usbd_req_clear_port_feature(
817 portno, UHF_PORT_ENABLE);
821 DPRINTFN(0, "device problem (%s), "
822 "disabling port %d\n", usbd_errstr(err), portno);
827 /*------------------------------------------------------------------------*
828 * usb_device_20_compatible
831 * 0: HUB does not support suspend and resume
832 * Else: HUB supports suspend and resume
833 *------------------------------------------------------------------------*/
835 usb_device_20_compatible(struct usb_device *udev)
839 switch (udev->speed) {
849 /*------------------------------------------------------------------------*
850 * uhub_suspend_resume_port
854 * Else: A control transaction failed
855 *------------------------------------------------------------------------*/
857 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
859 struct usb_device *child;
860 struct usb_device *udev;
864 DPRINTF("port %d\n", portno);
867 child = usb_bus_port_get_device(udev->bus,
868 udev->hub->ports + portno - 1);
870 /* first clear the port suspend change bit */
872 if (usb_device_20_compatible(udev)) {
873 err = usbd_req_clear_port_feature(udev, NULL,
874 portno, UHF_C_PORT_SUSPEND);
876 err = usbd_req_clear_port_feature(udev, NULL,
877 portno, UHF_C_PORT_LINK_STATE);
881 DPRINTF("clearing suspend failed.\n");
884 /* get fresh status */
886 err = uhub_read_port_status(sc, portno);
888 DPRINTF("reading port status failed.\n");
891 /* convert current state */
893 if (usb_device_20_compatible(udev)) {
894 if (sc->sc_st.port_status & UPS_SUSPEND) {
900 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
904 case UPS_PORT_LS_SS_INA:
905 usbd_req_warm_reset_port(udev, NULL, portno);
914 DPRINTF("suspended=%u\n", is_suspend);
916 /* do the suspend or resume */
920 * This code handle two cases: 1) Host Mode - we can only
921 * receive resume here 2) Device Mode - we can receive
922 * suspend and resume here
925 usb_dev_resume_peer(child);
926 else if (child->flags.usb_mode == USB_MODE_DEVICE)
927 usb_dev_suspend_peer(child);
933 /*------------------------------------------------------------------------*
934 * uhub_root_interrupt
936 * This function is called when a Root HUB interrupt has
937 * happened. "ptr" and "len" makes up the Root HUB interrupt
938 * packet. This function is called having the "bus_mtx" locked.
939 *------------------------------------------------------------------------*/
941 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
943 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
945 usb_needs_explore(bus, 0);
949 uhub_is_too_deep(struct usb_device *udev)
951 switch (udev->speed) {
955 if (udev->depth > USB_HUB_MAX_DEPTH)
958 case USB_SPEED_SUPER:
959 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
968 /*------------------------------------------------------------------------*
974 *------------------------------------------------------------------------*/
976 uhub_explore(struct usb_device *udev)
979 struct uhub_softc *sc;
989 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
991 /* ignore devices that are too deep */
992 if (uhub_is_too_deep(udev))
993 return (USB_ERR_TOO_DEEP);
995 /* check if device is suspended */
996 if (udev->flags.self_suspended) {
997 /* need to wait until the child signals resume */
998 DPRINTF("Device is suspended!\n");
1003 * Make sure we don't race against user-space applications
1006 do_unlock = usbd_enum_lock(udev);
1008 for (x = 0; x != hub->nports; x++) {
1009 up = hub->ports + x;
1012 err = uhub_read_port_status(sc, portno);
1014 /* most likely the HUB is gone */
1017 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1018 DPRINTF("Overcurrent on port %u.\n", portno);
1019 err = usbd_req_clear_port_feature(
1020 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1022 /* most likely the HUB is gone */
1026 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1028 * Fake a connect status change so that the
1029 * status gets checked initially!
1031 sc->sc_st.port_change |=
1032 UPS_C_CONNECT_STATUS;
1034 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1035 err = usbd_req_clear_port_feature(
1036 udev, NULL, portno, UHF_C_PORT_ENABLE);
1038 /* most likely the HUB is gone */
1041 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1043 * Ignore the port error if the device
1046 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1047 DPRINTFN(0, "illegal enable change, "
1048 "port %d\n", portno);
1051 if (up->restartcnt == USB_RESTART_MAX) {
1052 /* XXX could try another speed ? */
1053 DPRINTFN(0, "port error, giving up "
1054 "port %d\n", portno);
1056 sc->sc_st.port_change |=
1057 UPS_C_CONNECT_STATUS;
1062 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1063 err = uhub_reattach_port(sc, portno);
1065 /* most likely the HUB is gone */
1069 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1070 UPS_C_PORT_LINK_STATE)) {
1071 err = uhub_suspend_resume_port(sc, portno);
1073 /* most likely the HUB is gone */
1077 err = uhub_explore_sub(sc, up);
1079 /* no device(s) present */
1082 /* explore succeeded - reset restart counter */
1087 usbd_enum_unlock(udev);
1089 /* initial status checked */
1090 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1092 /* return success */
1093 return (USB_ERR_NORMAL_COMPLETION);
1097 uhub_probe(device_t dev)
1099 struct usb_attach_arg *uaa = device_get_ivars(dev);
1101 if (uaa->usb_mode != USB_MODE_HOST)
1105 * The subclass for USB HUBs is currently ignored because it
1106 * is 0 for some and 1 for others.
1108 if (uaa->info.bConfigIndex == 0 &&
1109 uaa->info.bDeviceClass == UDCLASS_HUB)
1115 /* NOTE: The information returned by this function can be wrong. */
1117 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1119 struct usb_hub_descriptor hubdesc20;
1120 struct usb_hub_ss_descriptor hubdesc30;
1125 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1126 return (USB_ERR_INVAL);
1131 switch (udev->speed) {
1133 case USB_SPEED_FULL:
1134 case USB_SPEED_HIGH:
1135 /* assuming that there is one port */
1136 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1138 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1139 "error=%s\n", usbd_errstr(err));
1142 nports = hubdesc20.bNbrPorts;
1146 if (udev->speed == USB_SPEED_HIGH)
1147 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1150 case USB_SPEED_SUPER:
1151 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1153 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1154 "error=%s\n", usbd_errstr(err));
1157 nports = hubdesc30.bNbrPorts;
1163 err = USB_ERR_INVAL;
1167 if (pnports != NULL)
1177 uhub_attach(device_t dev)
1179 struct uhub_softc *sc = device_get_softc(dev);
1180 struct usb_attach_arg *uaa = device_get_ivars(dev);
1181 struct usb_device *udev = uaa->device;
1182 struct usb_device *parent_hub = udev->parent_hub;
1183 struct usb_hub *hub;
1184 struct usb_hub_descriptor hubdesc20;
1185 struct usb_hub_ss_descriptor hubdesc30;
1191 uint8_t iface_index;
1197 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1199 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
1200 device_get_nameunit(dev));
1202 device_set_usb_desc(dev);
1204 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1205 "parent->selfpowered=%d\n",
1207 udev->flags.self_powered,
1210 parent_hub->flags.self_powered : 0);
1212 if (uhub_is_too_deep(udev)) {
1213 DPRINTFN(0, "HUB at depth %d, "
1214 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1218 if (!udev->flags.self_powered && parent_hub &&
1219 !parent_hub->flags.self_powered) {
1220 DPRINTFN(0, "Bus powered HUB connected to "
1221 "bus powered HUB. HUB ignored\n");
1225 if (UHUB_IS_MULTI_TT(sc)) {
1226 err = usbd_set_alt_interface_index(udev, 0, 1);
1228 device_printf(dev, "MTT could not be enabled\n");
1231 device_printf(dev, "MTT enabled\n");
1234 /* get HUB descriptor */
1236 DPRINTFN(2, "Getting HUB descriptor\n");
1238 switch (udev->speed) {
1240 case USB_SPEED_FULL:
1241 case USB_SPEED_HIGH:
1242 /* assuming that there is one port */
1243 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1245 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1246 "error=%s\n", usbd_errstr(err));
1249 /* get number of ports */
1250 nports = hubdesc20.bNbrPorts;
1252 /* get power delay */
1253 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1254 usb_extra_power_up_time);
1256 /* get complete HUB descriptor */
1258 /* check number of ports */
1260 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1261 "error=%s\n", usbd_errstr(err));
1264 /* get complete HUB descriptor */
1265 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1268 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1269 "error=%s\n", usbd_errstr(err));
1272 if (hubdesc20.bNbrPorts != nports) {
1273 DPRINTFN(0, "Number of ports changed\n");
1278 case USB_SPEED_SUPER:
1279 if (udev->parent_hub != NULL) {
1280 err = usbd_req_set_hub_depth(udev, NULL,
1283 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1284 "error=%s\n", usbd_errstr(err));
1288 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1290 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1291 "error=%s\n", usbd_errstr(err));
1294 /* get number of ports */
1295 nports = hubdesc30.bNbrPorts;
1297 /* get power delay */
1298 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1299 usb_extra_power_up_time);
1301 /* get complete HUB descriptor */
1303 /* check number of ports */
1304 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1305 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1306 "error=%s\n", usbd_errstr(err));
1309 /* get complete HUB descriptor */
1310 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1313 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1314 "error=%s\n", usbd_errstr(err));
1317 if (hubdesc30.bNbrPorts != nports) {
1318 DPRINTFN(0, "Number of ports changed\n");
1324 DPRINTF("Assuming HUB has only one port\n");
1325 /* default number of ports */
1327 /* default power delay */
1328 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1332 DPRINTFN(0, "portless HUB\n");
1335 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1336 M_USBDEV, M_WAITOK | M_ZERO);
1343 /* initialize HUB structure */
1345 hub->explore = &uhub_explore;
1346 hub->nports = nports;
1347 hub->hubudev = udev;
1348 #if USB_HAVE_TT_SUPPORT
1349 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1350 hub->tt_msg[0].udev = udev;
1351 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1352 hub->tt_msg[1].udev = udev;
1354 /* if self powered hub, give ports maximum current */
1355 if (udev->flags.self_powered) {
1356 hub->portpower = USB_MAX_POWER;
1358 hub->portpower = USB_MIN_POWER;
1361 /* set up interrupt pipe */
1363 if (udev->parent_hub == NULL) {
1364 /* root HUB is special */
1368 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1369 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1372 DPRINTFN(0, "cannot setup interrupt transfer, "
1373 "errstr=%s\n", usbd_errstr(err));
1376 /* wait with power off for a while */
1377 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1380 * To have the best chance of success we do things in the exact same
1381 * order as Windoze98. This should not be necessary, but some
1382 * devices do not follow the USB specs to the letter.
1384 * These are the events on the bus when a hub is attached:
1385 * Get device and config descriptors (see attach code)
1386 * Get hub descriptor (see above)
1389 * wait for power to become stable
1390 * (all below happens in explore code)
1392 * clear C_PORT_CONNECTION
1395 * if device connected
1399 * clear C_PORT_RESET
1401 * proceed with device attachment
1404 /* XXX should check for none, individual, or ganged power? */
1408 for (x = 0; x != nports; x++) {
1409 /* set up data structures */
1410 struct usb_port *up = hub->ports + x;
1412 up->device_index = 0;
1416 /* check if port is removable */
1417 switch (udev->speed) {
1419 case USB_SPEED_FULL:
1420 case USB_SPEED_HIGH:
1421 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1424 case USB_SPEED_SUPER:
1425 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1429 DPRINTF("Assuming removable port\n");
1434 /* turn the power on */
1435 err = usbd_req_set_port_feature(udev, NULL,
1436 portno, UHF_PORT_POWER);
1439 DPRINTFN(0, "port %d power on failed, %s\n",
1440 portno, usbd_errstr(err));
1442 DPRINTF("turn on port %d power\n",
1445 /* wait for stable power */
1446 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1449 device_printf(dev, "%d port%s with %d "
1450 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1451 removable, udev->flags.self_powered ? "self" : "bus");
1453 /* Start the interrupt endpoint, if any */
1455 mtx_lock(&sc->sc_mtx);
1456 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1457 mtx_unlock(&sc->sc_mtx);
1459 /* Enable automatic power save on all USB HUBs */
1461 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1466 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1469 free(udev->hub, M_USBDEV);
1473 mtx_destroy(&sc->sc_mtx);
1479 * Called from process context when the hub is gone.
1480 * Detach all devices on active ports.
1483 uhub_detach(device_t dev)
1485 struct uhub_softc *sc = device_get_softc(dev);
1486 struct usb_hub *hub = sc->sc_udev->hub;
1487 struct usb_bus *bus = sc->sc_udev->bus;
1488 struct usb_device *child;
1491 if (hub == NULL) /* must be partially working */
1494 /* Make sure interrupt transfer is gone. */
1495 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1497 /* Detach all ports */
1498 for (x = 0; x != hub->nports; x++) {
1500 child = usb_bus_port_get_device(bus, hub->ports + x);
1502 if (child == NULL) {
1507 * Free USB device and all subdevices, if any.
1509 usb_free_device(child, 0);
1512 #if USB_HAVE_TT_SUPPORT
1513 /* Make sure our TT messages are not queued anywhere */
1515 usb_proc_mwait(&bus->non_giant_callback_proc,
1516 &hub->tt_msg[0], &hub->tt_msg[1]);
1517 USB_BUS_UNLOCK(bus);
1519 free(hub, M_USBDEV);
1520 sc->sc_udev->hub = NULL;
1522 mtx_destroy(&sc->sc_mtx);
1528 uhub_suspend(device_t dev)
1531 /* Sub-devices are not suspended here! */
1536 uhub_resume(device_t dev)
1539 /* Sub-devices are not resumed here! */
1544 uhub_driver_added(device_t dev, driver_t *driver)
1546 usb_needs_explore_all();
1550 struct usb_device *udev;
1552 uint8_t iface_index;
1556 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1557 struct hub_result *res)
1559 struct usb_interface *iface;
1560 struct usb_device *udev;
1565 nports = hub->nports;
1566 for (x = 0; x != nports; x++) {
1567 udev = usb_bus_port_get_device(hub->hubudev->bus,
1572 for (i = 0; i != USB_IFACE_MAX; i++) {
1573 iface = usbd_get_iface(udev, i);
1575 (iface->subdev == child)) {
1576 res->iface_index = i;
1578 res->portno = x + 1;
1583 res->iface_index = 0;
1589 uhub_child_location_string(device_t parent, device_t child,
1590 char *buf, size_t buflen)
1592 struct uhub_softc *sc;
1593 struct usb_hub *hub;
1594 struct hub_result res;
1596 if (!device_is_attached(parent)) {
1602 sc = device_get_softc(parent);
1603 hub = sc->sc_udev->hub;
1606 uhub_find_iface_index(hub, child, &res);
1608 DPRINTF("device not on hub\n");
1614 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1615 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1616 res.portno, device_get_unit(res.udev->bus->bdev),
1617 res.udev->device_index, res.iface_index);
1625 uhub_child_pnpinfo_string(device_t parent, device_t child,
1626 char *buf, size_t buflen)
1628 struct uhub_softc *sc;
1629 struct usb_hub *hub;
1630 struct usb_interface *iface;
1631 struct hub_result res;
1633 if (!device_is_attached(parent)) {
1639 sc = device_get_softc(parent);
1640 hub = sc->sc_udev->hub;
1643 uhub_find_iface_index(hub, child, &res);
1645 DPRINTF("device not on hub\n");
1651 iface = usbd_get_iface(res.udev, res.iface_index);
1652 if (iface && iface->idesc) {
1653 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1654 "devclass=0x%02x devsubclass=0x%02x "
1658 "intclass=0x%02x intsubclass=0x%02x "
1659 "intprotocol=0x%02x " "%s%s",
1660 UGETW(res.udev->ddesc.idVendor),
1661 UGETW(res.udev->ddesc.idProduct),
1662 res.udev->ddesc.bDeviceClass,
1663 res.udev->ddesc.bDeviceSubClass,
1664 usb_get_serial(res.udev),
1665 UGETW(res.udev->ddesc.bcdDevice),
1666 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1667 iface->idesc->bInterfaceClass,
1668 iface->idesc->bInterfaceSubClass,
1669 iface->idesc->bInterfaceProtocol,
1670 iface->pnpinfo ? " " : "",
1671 iface->pnpinfo ? iface->pnpinfo : "");
1685 * The USB Transaction Translator:
1686 * ===============================
1688 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1689 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1690 * USB transfers. To utilize bandwidth dynamically the "scatter and
1691 * gather" principle must be applied. This means that bandwidth must
1692 * be divided into equal parts of bandwidth. With regard to USB all
1693 * data is transferred in smaller packets with length
1694 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1697 * The bandwidth scheduler which I have implemented will simply pack
1698 * the USB transfers back to back until there is no more space in the
1699 * schedule. Out of the 8 microframes which the USB 2.0 standard
1700 * provides, only 6 are available for non-HIGH-speed devices. I have
1701 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1702 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1703 * this division, it is very difficult to allocate and free bandwidth
1706 * NOTE about the Transaction Translator in USB HUBs:
1708 * USB HUBs have a very simple Transaction Translator, that will
1709 * simply pipeline all the SPLIT transactions. That means that the
1710 * transactions will be executed in the order they are queued!
1714 /*------------------------------------------------------------------------*
1715 * usb_intr_find_best_slot
1718 * The best Transaction Translation slot for an interrupt endpoint.
1719 *------------------------------------------------------------------------*/
1721 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1722 uint8_t end, uint8_t mask)
1724 usb_size_t min = (usb_size_t)-1;
1732 /* find the last slot with lesser used bandwidth */
1734 for (x = start; x < end; x++) {
1738 /* compute sum of bandwidth */
1739 for (z = x; z < end; z++) {
1740 if (mask & (1U << (z - x)))
1744 /* check if the current multi-slot is more optimal */
1750 /* check if the mask is about to be shifted out */
1751 if (mask & (1U << (end - 1 - x)))
1757 /*------------------------------------------------------------------------*
1758 * usb_hs_bandwidth_adjust
1760 * This function will update the bandwith usage for the microframe
1761 * having index "slot" by "len" bytes. "len" can be negative. If the
1762 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1763 * the "slot" argument will be replaced by the slot having least used
1764 * bandwidth. The "mask" argument is used for multi-slot allocations.
1767 * The slot in which the bandwidth update was done: 0..7
1768 *------------------------------------------------------------------------*/
1770 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1771 uint8_t slot, uint8_t mask)
1773 struct usb_bus *bus = udev->bus;
1774 struct usb_hub *hub;
1775 enum usb_dev_speed speed;
1778 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1780 speed = usbd_get_speed(udev);
1784 case USB_SPEED_FULL:
1785 if (speed == USB_SPEED_LOW) {
1789 * The Host Controller Driver should have
1790 * performed checks so that the lookup
1791 * below does not result in a NULL pointer
1795 hub = udev->parent_hs_hub->hub;
1796 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1797 slot = usb_intr_find_best_slot(hub->uframe_usage,
1798 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1800 for (x = slot; x < 8; x++) {
1801 if (mask & (1U << (x - slot))) {
1802 hub->uframe_usage[x] += len;
1803 bus->uframe_usage[x] += len;
1808 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1809 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1810 USB_HS_MICRO_FRAMES_MAX, mask);
1812 for (x = slot; x < 8; x++) {
1813 if (mask & (1U << (x - slot))) {
1814 bus->uframe_usage[x] += len;
1822 /*------------------------------------------------------------------------*
1823 * usb_hs_bandwidth_alloc
1825 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1826 *------------------------------------------------------------------------*/
1828 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1830 struct usb_device *udev;
1835 udev = xfer->xroot->udev;
1837 if (udev->flags.usb_mode != USB_MODE_HOST)
1838 return; /* not supported */
1840 xfer->endpoint->refcount_bw++;
1841 if (xfer->endpoint->refcount_bw != 1)
1842 return; /* already allocated */
1844 speed = usbd_get_speed(udev);
1846 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1848 /* allocate a microframe slot */
1851 slot = usb_hs_bandwidth_adjust(udev,
1852 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1854 xfer->endpoint->usb_uframe = slot;
1855 xfer->endpoint->usb_smask = mask << slot;
1857 if ((speed != USB_SPEED_FULL) &&
1858 (speed != USB_SPEED_LOW)) {
1859 xfer->endpoint->usb_cmask = 0x00 ;
1861 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1865 case UE_ISOCHRONOUS:
1866 switch (usbd_xfer_get_fps_shift(xfer)) {
1881 /* allocate a microframe multi-slot */
1883 slot = usb_hs_bandwidth_adjust(udev,
1884 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1886 xfer->endpoint->usb_uframe = slot;
1887 xfer->endpoint->usb_cmask = 0;
1888 xfer->endpoint->usb_smask = mask << slot;
1892 xfer->endpoint->usb_uframe = 0;
1893 xfer->endpoint->usb_cmask = 0;
1894 xfer->endpoint->usb_smask = 0;
1898 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1899 xfer->endpoint->usb_uframe,
1900 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1903 /*------------------------------------------------------------------------*
1904 * usb_hs_bandwidth_free
1906 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1907 *------------------------------------------------------------------------*/
1909 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1911 struct usb_device *udev;
1915 udev = xfer->xroot->udev;
1917 if (udev->flags.usb_mode != USB_MODE_HOST)
1918 return; /* not supported */
1920 xfer->endpoint->refcount_bw--;
1921 if (xfer->endpoint->refcount_bw != 0)
1922 return; /* still allocated */
1924 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1926 case UE_ISOCHRONOUS:
1928 slot = xfer->endpoint->usb_uframe;
1929 mask = xfer->endpoint->usb_smask;
1931 /* free microframe slot(s): */
1932 usb_hs_bandwidth_adjust(udev,
1933 -xfer->max_frame_size, slot, mask >> slot);
1935 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1936 slot, mask >> slot);
1938 xfer->endpoint->usb_uframe = 0;
1939 xfer->endpoint->usb_cmask = 0;
1940 xfer->endpoint->usb_smask = 0;
1948 /*------------------------------------------------------------------------*
1949 * usb_isoc_time_expand
1951 * This function will expand the time counter from 7-bit to 16-bit.
1954 * 16-bit isochronous time counter.
1955 *------------------------------------------------------------------------*/
1957 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1961 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1963 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1965 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1967 if (isoc_time_curr < rem) {
1968 /* the time counter wrapped around */
1969 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1971 /* update the remainder */
1973 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1974 bus->isoc_time_last |= isoc_time_curr;
1976 return (bus->isoc_time_last);
1979 /*------------------------------------------------------------------------*
1980 * usbd_fs_isoc_schedule_alloc_slot
1982 * This function will allocate bandwidth for an isochronous FULL speed
1983 * transaction in the FULL speed schedule.
1988 *------------------------------------------------------------------------*/
1989 #if USB_HAVE_TT_SUPPORT
1991 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1993 struct usb_xfer *xfer;
1994 struct usb_xfer *pipe_xfer;
1995 struct usb_bus *bus;
1997 usb_frlength_t data_len;
2005 bus = isoc_xfer->xroot->bus;
2007 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2009 /* skip self, if any */
2011 if (xfer == isoc_xfer)
2014 /* check if this USB transfer is going through the same TT */
2016 if (xfer->xroot->udev->parent_hs_hub !=
2017 isoc_xfer->xroot->udev->parent_hs_hub) {
2020 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2021 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2022 (xfer->xroot->udev->hs_port_no !=
2023 isoc_xfer->xroot->udev->hs_port_no)) {
2026 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2029 /* check if isoc_time is part of this transfer */
2031 delta = xfer->isoc_time_complete - isoc_time;
2032 if (delta > 0 && delta <= xfer->nframes) {
2033 delta = xfer->nframes - delta;
2035 len = xfer->frlengths[delta];
2043 /* check double buffered transfers */
2045 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
2048 /* skip self, if any */
2050 if (pipe_xfer == isoc_xfer)
2053 /* check if isoc_time is part of this transfer */
2055 delta = pipe_xfer->isoc_time_complete - isoc_time;
2056 if (delta > 0 && delta <= pipe_xfer->nframes) {
2057 delta = pipe_xfer->nframes - delta;
2059 len = pipe_xfer->frlengths[delta];
2069 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2070 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2074 /* check for overflow */
2076 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2081 delta = isoc_xfer->isoc_time_complete - isoc_time;
2082 if (delta > 0 && delta <= isoc_xfer->nframes) {
2083 delta = isoc_xfer->nframes - delta;
2085 len = isoc_xfer->frlengths[delta];
2093 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2094 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2098 /* check for overflow */
2100 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2107 /*------------------------------------------------------------------------*
2108 * usb_bus_port_get_device
2110 * This function is NULL safe.
2111 *------------------------------------------------------------------------*/
2113 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2115 if ((bus == NULL) || (up == NULL)) {
2119 if (up->device_index == 0) {
2123 return (bus->devices[up->device_index]);
2126 /*------------------------------------------------------------------------*
2127 * usb_bus_port_set_device
2129 * This function is NULL safe.
2130 *------------------------------------------------------------------------*/
2132 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2133 struct usb_device *udev, uint8_t device_index)
2140 * There is only one case where we don't
2141 * have an USB port, and that is the Root Hub!
2145 up->device_index = device_index;
2147 device_index = up->device_index;
2148 up->device_index = 0;
2152 * Make relationships to our new device
2154 if (device_index != 0) {
2156 mtx_lock(&usb_ref_lock);
2158 bus->devices[device_index] = udev;
2160 mtx_unlock(&usb_ref_lock);
2166 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2169 /*------------------------------------------------------------------------*
2172 * This functions is called when the USB event thread needs to run.
2173 *------------------------------------------------------------------------*/
2175 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2182 DPRINTF("No bus pointer!\n");
2185 if ((bus->devices == NULL) ||
2186 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2187 DPRINTF("No root HUB\n");
2190 if (mtx_owned(&bus->bus_mtx)) {
2199 if (usb_proc_msignal(&bus->explore_proc,
2200 &bus->explore_msg[0], &bus->explore_msg[1])) {
2204 USB_BUS_UNLOCK(bus);
2208 /*------------------------------------------------------------------------*
2209 * usb_needs_explore_all
2211 * This function is called whenever a new driver is loaded and will
2212 * cause that all USB busses are re-explored.
2213 *------------------------------------------------------------------------*/
2215 usb_needs_explore_all(void)
2217 struct usb_bus *bus;
2224 dc = usb_devclass_ptr;
2226 DPRINTFN(0, "no devclass\n");
2230 * Explore all USB busses in parallell.
2232 max = devclass_get_maxunit(dc);
2234 dev = devclass_get_device(dc, max);
2236 bus = device_get_softc(dev);
2238 usb_needs_explore(bus, 1);
2245 /*------------------------------------------------------------------------*
2246 * usb_bus_power_update
2248 * This function will ensure that all USB devices on the given bus are
2249 * properly suspended or resumed according to the device transfer
2251 *------------------------------------------------------------------------*/
2254 usb_bus_power_update(struct usb_bus *bus)
2256 usb_needs_explore(bus, 0 /* no probe */ );
2260 /*------------------------------------------------------------------------*
2261 * usbd_transfer_power_ref
2263 * This function will modify the power save reference counts and
2264 * wakeup the USB device associated with the given USB transfer, if
2266 *------------------------------------------------------------------------*/
2269 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2271 static const usb_power_mask_t power_mask[4] = {
2272 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2273 [UE_BULK] = USB_HW_POWER_BULK,
2274 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2275 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2277 struct usb_device *udev;
2278 uint8_t needs_explore;
2279 uint8_t needs_hw_power;
2282 udev = xfer->xroot->udev;
2284 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2285 /* no power save for root HUB */
2288 USB_BUS_LOCK(udev->bus);
2290 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2292 udev->pwr_save.last_xfer_time = ticks;
2293 udev->pwr_save.type_refs[xfer_type] += val;
2295 if (xfer->flags_int.control_xfr) {
2296 udev->pwr_save.read_refs += val;
2297 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2299 * It is not allowed to suspend during a
2302 udev->pwr_save.write_refs += val;
2304 } else if (USB_GET_DATA_ISREAD(xfer)) {
2305 udev->pwr_save.read_refs += val;
2307 udev->pwr_save.write_refs += val;
2311 if (udev->flags.self_suspended)
2312 needs_explore = usb_peer_should_wakeup(udev);
2316 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2317 DPRINTF("Adding type %u to power state\n", xfer_type);
2318 udev->bus->hw_power_state |= power_mask[xfer_type];
2328 USB_BUS_UNLOCK(udev->bus);
2330 if (needs_explore) {
2331 DPRINTF("update\n");
2332 usb_bus_power_update(udev->bus);
2333 } else if (needs_hw_power) {
2334 DPRINTF("needs power\n");
2335 if (udev->bus->methods->set_hw_power != NULL) {
2336 (udev->bus->methods->set_hw_power) (udev->bus);
2342 /*------------------------------------------------------------------------*
2343 * usb_peer_should_wakeup
2345 * This function returns non-zero if the current device should wake up.
2346 *------------------------------------------------------------------------*/
2348 usb_peer_should_wakeup(struct usb_device *udev)
2350 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2351 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2352 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2353 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2354 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2355 (udev->pwr_save.write_refs != 0) ||
2356 ((udev->pwr_save.read_refs != 0) &&
2357 (udev->flags.usb_mode == USB_MODE_HOST) &&
2358 (usb_peer_can_wakeup(udev) == 0)));
2361 /*------------------------------------------------------------------------*
2364 * This function implements the USB power daemon and is called
2365 * regularly from the USB explore thread.
2366 *------------------------------------------------------------------------*/
2369 usb_bus_powerd(struct usb_bus *bus)
2371 struct usb_device *udev;
2374 usb_ticks_t mintime;
2375 usb_size_t type_refs[5];
2378 limit = usb_power_timeout;
2381 else if (limit > 255)
2386 DPRINTF("bus=%p\n", bus);
2391 * The root HUB device is never suspended
2392 * and we simply skip it.
2394 for (x = USB_ROOT_HUB_ADDR + 1;
2395 x != bus->devices_max; x++) {
2397 udev = bus->devices[x];
2401 temp = ticks - udev->pwr_save.last_xfer_time;
2403 if (usb_peer_should_wakeup(udev)) {
2404 /* check if we are suspended */
2405 if (udev->flags.self_suspended != 0) {
2406 USB_BUS_UNLOCK(bus);
2407 usb_dev_resume_peer(udev);
2410 } else if ((temp >= limit) &&
2411 (udev->flags.usb_mode == USB_MODE_HOST) &&
2412 (udev->flags.self_suspended == 0)) {
2413 /* try to do suspend */
2415 USB_BUS_UNLOCK(bus);
2416 usb_dev_suspend_peer(udev);
2421 /* reset counters */
2423 mintime = (usb_ticks_t)-1;
2430 /* Re-loop all the devices to get the actual state */
2432 for (x = USB_ROOT_HUB_ADDR + 1;
2433 x != bus->devices_max; x++) {
2435 udev = bus->devices[x];
2439 /* we found a non-Root-Hub USB device */
2442 /* "last_xfer_time" can be updated by a resume */
2443 temp = ticks - udev->pwr_save.last_xfer_time;
2446 * Compute minimum time since last transfer for the complete
2452 if (udev->flags.self_suspended == 0) {
2453 type_refs[0] += udev->pwr_save.type_refs[0];
2454 type_refs[1] += udev->pwr_save.type_refs[1];
2455 type_refs[2] += udev->pwr_save.type_refs[2];
2456 type_refs[3] += udev->pwr_save.type_refs[3];
2460 if (mintime >= (usb_ticks_t)(1 * hz)) {
2461 /* recompute power masks */
2462 DPRINTF("Recomputing power masks\n");
2463 bus->hw_power_state = 0;
2464 if (type_refs[UE_CONTROL] != 0)
2465 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2466 if (type_refs[UE_BULK] != 0)
2467 bus->hw_power_state |= USB_HW_POWER_BULK;
2468 if (type_refs[UE_INTERRUPT] != 0)
2469 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2470 if (type_refs[UE_ISOCHRONOUS] != 0)
2471 bus->hw_power_state |= USB_HW_POWER_ISOC;
2472 if (type_refs[4] != 0)
2473 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2475 USB_BUS_UNLOCK(bus);
2477 if (bus->methods->set_hw_power != NULL) {
2478 /* always update hardware power! */
2479 (bus->methods->set_hw_power) (bus);
2485 /*------------------------------------------------------------------------*
2486 * usb_dev_resume_peer
2488 * This function will resume an USB peer and do the required USB
2489 * signalling to get an USB device out of the suspended state.
2490 *------------------------------------------------------------------------*/
2492 usb_dev_resume_peer(struct usb_device *udev)
2494 struct usb_bus *bus;
2501 /* check if already resumed */
2502 if (udev->flags.self_suspended == 0)
2505 /* we need a parent HUB to do resume */
2506 if (udev->parent_hub == NULL)
2509 DPRINTF("udev=%p\n", udev);
2511 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2512 (udev->flags.remote_wakeup == 0)) {
2514 * If the host did not set the remote wakeup feature, we can
2515 * not wake it up either!
2517 DPRINTF("remote wakeup is not set!\n");
2520 /* get bus pointer */
2523 /* resume parent hub first */
2524 usb_dev_resume_peer(udev->parent_hub);
2526 /* reduce chance of instant resume failure by waiting a little bit */
2527 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2529 if (usb_device_20_compatible(udev)) {
2530 /* resume current port (Valid in Host and Device Mode) */
2531 err = usbd_req_clear_port_feature(udev->parent_hub,
2532 NULL, udev->port_no, UHF_PORT_SUSPEND);
2534 DPRINTFN(0, "Resuming port failed\n");
2538 /* resume current port (Valid in Host and Device Mode) */
2539 err = usbd_req_set_port_link_state(udev->parent_hub,
2540 NULL, udev->port_no, UPS_PORT_LS_U0);
2542 DPRINTFN(0, "Resuming port failed\n");
2547 /* resume settle time */
2548 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2550 if (bus->methods->device_resume != NULL) {
2551 /* resume USB device on the USB controller */
2552 (bus->methods->device_resume) (udev);
2555 /* set that this device is now resumed */
2556 udev->flags.self_suspended = 0;
2558 /* make sure that we don't go into suspend right away */
2559 udev->pwr_save.last_xfer_time = ticks;
2561 /* make sure the needed power masks are on */
2562 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2563 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2564 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2565 bus->hw_power_state |= USB_HW_POWER_BULK;
2566 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2567 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2568 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2569 bus->hw_power_state |= USB_HW_POWER_ISOC;
2571 USB_BUS_UNLOCK(bus);
2573 if (bus->methods->set_hw_power != NULL) {
2574 /* always update hardware power! */
2575 (bus->methods->set_hw_power) (bus);
2580 /* notify all sub-devices about resume */
2581 err = usb_suspend_resume(udev, 0);
2583 usbd_sr_unlock(udev);
2585 /* check if peer has wakeup capability */
2586 if (usb_peer_can_wakeup(udev)) {
2587 /* clear remote wakeup */
2588 err = usbd_req_clear_device_feature(udev,
2589 NULL, UF_DEVICE_REMOTE_WAKEUP);
2591 DPRINTFN(0, "Clearing device "
2592 "remote wakeup failed: %s\n",
2598 /*------------------------------------------------------------------------*
2599 * usb_dev_suspend_peer
2601 * This function will suspend an USB peer and do the required USB
2602 * signalling to get an USB device into the suspended state.
2603 *------------------------------------------------------------------------*/
2605 usb_dev_suspend_peer(struct usb_device *udev)
2607 struct usb_device *child;
2617 /* check if already suspended */
2618 if (udev->flags.self_suspended)
2621 /* we need a parent HUB to do suspend */
2622 if (udev->parent_hub == NULL)
2625 DPRINTF("udev=%p\n", udev);
2627 /* check if the current device is a HUB */
2628 if (udev->hub != NULL) {
2629 nports = udev->hub->nports;
2631 /* check if all devices on the HUB are suspended */
2632 for (x = 0; x != nports; x++) {
2633 child = usb_bus_port_get_device(udev->bus,
2634 udev->hub->ports + x);
2639 if (child->flags.self_suspended)
2642 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2647 if (usb_peer_can_wakeup(udev)) {
2649 * This request needs to be done before we set
2650 * "udev->flags.self_suspended":
2653 /* allow device to do remote wakeup */
2654 err = usbd_req_set_device_feature(udev,
2655 NULL, UF_DEVICE_REMOTE_WAKEUP);
2657 DPRINTFN(0, "Setting device "
2658 "remote wakeup failed\n");
2662 USB_BUS_LOCK(udev->bus);
2664 * Checking for suspend condition and setting suspended bit
2667 err = usb_peer_should_wakeup(udev);
2670 * Set that this device is suspended. This variable
2671 * must be set before calling USB controller suspend
2674 udev->flags.self_suspended = 1;
2676 USB_BUS_UNLOCK(udev->bus);
2679 if (usb_peer_can_wakeup(udev)) {
2680 /* allow device to do remote wakeup */
2681 err = usbd_req_clear_device_feature(udev,
2682 NULL, UF_DEVICE_REMOTE_WAKEUP);
2684 DPRINTFN(0, "Setting device "
2685 "remote wakeup failed\n");
2689 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2690 /* resume parent HUB first */
2691 usb_dev_resume_peer(udev->parent_hub);
2693 /* reduce chance of instant resume failure by waiting a little bit */
2694 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2696 /* resume current port (Valid in Host and Device Mode) */
2697 err = usbd_req_clear_port_feature(udev->parent_hub,
2698 NULL, udev->port_no, UHF_PORT_SUSPEND);
2700 /* resume settle time */
2701 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2703 DPRINTF("Suspend was cancelled!\n");
2709 /* notify all sub-devices about suspend */
2710 err = usb_suspend_resume(udev, 1);
2712 usbd_sr_unlock(udev);
2714 if (udev->bus->methods->device_suspend != NULL) {
2717 /* suspend device on the USB controller */
2718 (udev->bus->methods->device_suspend) (udev);
2721 temp = usbd_get_dma_delay(udev);
2723 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2727 if (usb_device_20_compatible(udev)) {
2728 /* suspend current port */
2729 err = usbd_req_set_port_feature(udev->parent_hub,
2730 NULL, udev->port_no, UHF_PORT_SUSPEND);
2732 DPRINTFN(0, "Suspending port failed\n");
2736 /* suspend current port */
2737 err = usbd_req_set_port_link_state(udev->parent_hub,
2738 NULL, udev->port_no, UPS_PORT_LS_U3);
2740 DPRINTFN(0, "Suspending port failed\n");
2745 udev = udev->parent_hub;
2749 /*------------------------------------------------------------------------*
2750 * usbd_set_power_mode
2752 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2754 *------------------------------------------------------------------------*/
2756 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2758 /* filter input argument */
2759 if ((power_mode != USB_POWER_MODE_ON) &&
2760 (power_mode != USB_POWER_MODE_OFF))
2761 power_mode = USB_POWER_MODE_SAVE;
2763 power_mode = usbd_filter_power_mode(udev, power_mode);
2765 udev->power_mode = power_mode; /* update copy of power mode */
2768 usb_bus_power_update(udev->bus);
2770 usb_needs_explore(udev->bus, 0 /* no probe */ );
2774 /*------------------------------------------------------------------------*
2775 * usbd_filter_power_mode
2777 * This function filters the power mode based on hardware requirements.
2778 *------------------------------------------------------------------------*/
2780 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2782 struct usb_bus_methods *mtod;
2785 mtod = udev->bus->methods;
2788 if (mtod->get_power_mode != NULL)
2789 (mtod->get_power_mode) (udev, &temp);
2791 /* check if we should not filter */
2793 return (power_mode);
2795 /* use fixed power mode given by hardware driver */
2799 /*------------------------------------------------------------------------*
2800 * usbd_start_re_enumerate
2802 * This function starts re-enumeration of the given USB device. This
2803 * function does not need to be called BUS-locked. This function does
2804 * not wait until the re-enumeration is completed.
2805 *------------------------------------------------------------------------*/
2807 usbd_start_re_enumerate(struct usb_device *udev)
2809 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2810 udev->re_enumerate_wait = USB_RE_ENUM_START;
2811 usb_needs_explore(udev->bus, 0);
2815 /*-----------------------------------------------------------------------*
2816 * usbd_start_set_config
2818 * This function starts setting a USB configuration. This function
2819 * does not need to be called BUS-locked. This function does not wait
2820 * until the set USB configuratino is completed.
2821 *------------------------------------------------------------------------*/
2823 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2825 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2826 if (udev->curr_config_index == index) {
2827 /* no change needed */
2830 udev->next_config_index = index;
2831 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2832 usb_needs_explore(udev->bus, 0);
2834 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2835 if (udev->next_config_index == index) {
2836 /* no change needed */
2840 return (USB_ERR_PENDING_REQUESTS);