3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR uhub_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define UHUB_INTR_INTERVAL 250 /* ms */
79 #if USB_HAVE_TT_SUPPORT
80 UHUB_RESET_TT_TRANSFER,
86 static int uhub_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
91 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
95 static int usb_power_timeout = 30; /* seconds */
97 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
98 &usb_power_timeout, 0, "USB power timeout");
101 struct uhub_current_state {
102 uint16_t port_change;
103 uint16_t port_status;
107 struct uhub_current_state sc_st;/* current state */
108 #if (USB_HAVE_FIXED_PORT != 0)
109 struct usb_hub sc_hub;
111 device_t sc_dev; /* base device */
112 struct mtx sc_mtx; /* our mutex */
113 struct usb_device *sc_udev; /* USB device */
114 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
116 #define UHUB_FLAG_DID_EXPLORE 0x01
119 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
120 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
121 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
122 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
123 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
125 /* prototypes for type checking: */
127 static device_probe_t uhub_probe;
128 static device_attach_t uhub_attach;
129 static device_detach_t uhub_detach;
130 static device_suspend_t uhub_suspend;
131 static device_resume_t uhub_resume;
133 static bus_driver_added_t uhub_driver_added;
134 static bus_child_location_str_t uhub_child_location_string;
135 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
137 static usb_callback_t uhub_intr_callback;
138 #if USB_HAVE_TT_SUPPORT
139 static usb_callback_t uhub_reset_tt_callback;
142 static void usb_dev_resume_peer(struct usb_device *udev);
143 static void usb_dev_suspend_peer(struct usb_device *udev);
144 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
146 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
148 [UHUB_INTR_TRANSFER] = {
149 .type = UE_INTERRUPT,
150 .endpoint = UE_ADDR_ANY,
151 .direction = UE_DIR_ANY,
153 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
154 .bufsize = 0, /* use wMaxPacketSize */
155 .callback = &uhub_intr_callback,
156 .interval = UHUB_INTR_INTERVAL,
158 #if USB_HAVE_TT_SUPPORT
159 [UHUB_RESET_TT_TRANSFER] = {
161 .endpoint = 0x00, /* Control pipe */
162 .direction = UE_DIR_ANY,
163 .bufsize = sizeof(struct usb_device_request),
164 .callback = &uhub_reset_tt_callback,
165 .timeout = 1000, /* 1 second */
166 .usb_mode = USB_MODE_HOST,
172 * driver instance for "hub" connected to "usb"
173 * and "hub" connected to "hub"
175 static devclass_t uhub_devclass;
177 static device_method_t uhub_methods[] = {
178 DEVMETHOD(device_probe, uhub_probe),
179 DEVMETHOD(device_attach, uhub_attach),
180 DEVMETHOD(device_detach, uhub_detach),
182 DEVMETHOD(device_suspend, uhub_suspend),
183 DEVMETHOD(device_resume, uhub_resume),
185 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
186 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
187 DEVMETHOD(bus_driver_added, uhub_driver_added),
191 static driver_t uhub_driver = {
193 .methods = uhub_methods,
194 .size = sizeof(struct uhub_softc)
197 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
198 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
199 MODULE_VERSION(uhub, 1);
202 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
204 struct uhub_softc *sc = usbd_xfer_softc(xfer);
206 switch (USB_GET_STATE(xfer)) {
207 case USB_ST_TRANSFERRED:
210 * This is an indication that some port
211 * has changed status. Notify the bus
212 * event handler thread that we need
213 * to be explored again:
215 usb_needs_explore(sc->sc_udev->bus, 0);
218 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
219 usbd_transfer_submit(xfer);
223 if (xfer->error != USB_ERR_CANCELLED) {
225 * Do a clear-stall. The "stall_pipe" flag
226 * will get cleared before next callback by
229 usbd_xfer_set_stall(xfer);
230 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
231 usbd_transfer_submit(xfer);
237 /*------------------------------------------------------------------------*
240 * This function starts the TT reset USB request
241 *------------------------------------------------------------------------*/
242 #if USB_HAVE_TT_SUPPORT
244 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
246 struct usb_udev_msg *pm = (void *)_pm;
247 struct usb_device *udev = pm->udev;
249 struct uhub_softc *sc;
259 USB_BUS_UNLOCK(udev->bus);
260 mtx_lock(&sc->sc_mtx);
262 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
264 mtx_unlock(&sc->sc_mtx);
265 USB_BUS_LOCK(udev->bus);
269 /*------------------------------------------------------------------------*
270 * uhub_tt_buffer_reset_async_locked
272 * This function queues a TT reset for the given USB device and endpoint.
273 *------------------------------------------------------------------------*/
274 #if USB_HAVE_TT_SUPPORT
276 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
278 struct usb_device_request req;
279 struct usb_device *udev;
285 if (child == NULL || ep == NULL)
288 udev = child->parent_hs_hub;
289 port = child->hs_port_no;
296 (udev->speed != USB_SPEED_HIGH) ||
297 (child->speed != USB_SPEED_LOW &&
298 child->speed != USB_SPEED_FULL) ||
299 (child->flags.usb_mode != USB_MODE_HOST) ||
300 (port == 0) || (ep->edesc == NULL)) {
305 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
307 up = hub->ports + port - 1;
309 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
310 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
313 /* if we already received a clear buffer request, reset the whole TT */
314 if (up->req_reset_tt.bRequest != 0) {
315 req.bmRequestType = UT_WRITE_CLASS_OTHER;
316 req.bRequest = UR_RESET_TT;
317 USETW(req.wValue, 0);
318 req.wIndex[0] = port;
320 USETW(req.wLength, 0);
322 wValue = (ep->edesc->bEndpointAddress & 0xF) |
323 ((child->address & 0x7F) << 4) |
324 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
325 ((ep->edesc->bmAttributes & 3) << 12);
327 req.bmRequestType = UT_WRITE_CLASS_OTHER;
328 req.bRequest = UR_CLEAR_TT_BUFFER;
329 USETW(req.wValue, wValue);
330 req.wIndex[0] = port;
332 USETW(req.wLength, 0);
334 up->req_reset_tt = req;
335 /* get reset transfer started */
336 usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev->bus),
337 &hub->tt_msg[0], &hub->tt_msg[1]);
341 #if USB_HAVE_TT_SUPPORT
343 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
345 struct uhub_softc *sc;
346 struct usb_device *udev;
350 DPRINTF("TT buffer reset\n");
352 sc = usbd_xfer_softc(xfer);
355 switch (USB_GET_STATE(xfer)) {
356 case USB_ST_TRANSFERRED:
359 USB_BUS_LOCK(udev->bus);
360 /* find first port which needs a TT reset */
361 for (x = 0; x != udev->hub->nports; x++) {
362 up = udev->hub->ports + x;
364 if (up->req_reset_tt.bRequest == 0)
367 /* copy in the transfer */
368 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
369 sizeof(up->req_reset_tt));
371 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
374 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
376 USB_BUS_UNLOCK(udev->bus);
378 usbd_transfer_submit(xfer);
381 USB_BUS_UNLOCK(udev->bus);
385 if (error == USB_ERR_CANCELLED)
388 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
394 /*------------------------------------------------------------------------*
395 * uhub_count_active_host_ports
397 * This function counts the number of active ports at the given speed.
398 *------------------------------------------------------------------------*/
400 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
402 struct uhub_softc *sc;
403 struct usb_device *child;
418 for (x = 0; x != hub->nports; x++) {
420 child = usb_bus_port_get_device(udev->bus, up);
422 child->flags.usb_mode == USB_MODE_HOST &&
423 child->speed == speed)
431 uhub_explore_handle_re_enumerate(struct usb_device *child)
436 /* check if device should be re-enumerated */
437 if (child->flags.usb_mode != USB_MODE_HOST)
440 do_unlock = usbd_enum_lock(child);
441 switch (child->re_enumerate_wait) {
442 case USB_RE_ENUM_START:
443 err = usbd_set_config_index(child,
446 DPRINTF("Unconfigure failed: %s: Ignored.\n",
449 if (child->parent_hub == NULL) {
450 /* the root HUB cannot be re-enumerated */
451 DPRINTFN(6, "cannot reset root HUB\n");
454 err = usbd_req_re_enumerate(child, NULL);
457 err = usbd_set_config_index(child, 0);
459 err = usb_probe_and_attach(child,
460 USB_IFACE_INDEX_ANY);
462 child->re_enumerate_wait = USB_RE_ENUM_DONE;
465 case USB_RE_ENUM_PWR_OFF:
466 /* get the device unconfigured */
467 err = usbd_set_config_index(child,
470 DPRINTFN(0, "Could not unconfigure "
471 "device (ignored)\n");
473 if (child->parent_hub == NULL) {
474 /* the root HUB cannot be re-enumerated */
475 DPRINTFN(6, "cannot set port feature\n");
478 /* clear port enable */
479 err = usbd_req_clear_port_feature(child->parent_hub,
480 NULL, child->port_no, UHF_PORT_ENABLE);
482 DPRINTFN(0, "Could not disable port "
486 child->re_enumerate_wait = USB_RE_ENUM_DONE;
489 case USB_RE_ENUM_SET_CONFIG:
490 err = usbd_set_config_index(child,
491 child->next_config_index);
493 DPRINTF("Configure failed: %s: Ignored.\n",
496 err = usb_probe_and_attach(child,
497 USB_IFACE_INDEX_ANY);
499 child->re_enumerate_wait = USB_RE_ENUM_DONE;
503 child->re_enumerate_wait = USB_RE_ENUM_DONE;
507 usbd_enum_unlock(child);
510 /*------------------------------------------------------------------------*
511 * uhub_explore_sub - subroutine
515 * Else: A control transaction failed
516 *------------------------------------------------------------------------*/
518 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
521 struct usb_device *child;
525 bus = sc->sc_udev->bus;
528 /* get driver added refcount from USB bus */
529 refcount = bus->driver_added_refcount;
531 /* get device assosiated with the given port */
532 child = usb_bus_port_get_device(bus, up);
538 uhub_explore_handle_re_enumerate(child);
540 /* check if probe and attach should be done */
542 if (child->driver_added_refcount != refcount) {
543 child->driver_added_refcount = refcount;
544 err = usb_probe_and_attach(child,
545 USB_IFACE_INDEX_ANY);
550 /* start control transfer, if device mode */
552 if (child->flags.usb_mode == USB_MODE_DEVICE)
553 usbd_ctrl_transfer_setup(child);
555 /* if a HUB becomes present, do a recursive HUB explore */
558 err = (child->hub->explore) (child);
564 /*------------------------------------------------------------------------*
565 * uhub_read_port_status - factored out code
566 *------------------------------------------------------------------------*/
568 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
570 struct usb_port_status ps;
573 err = usbd_req_get_port_status(
574 sc->sc_udev, NULL, &ps, portno);
576 /* update status regardless of error */
578 sc->sc_st.port_status = UGETW(ps.wPortStatus);
579 sc->sc_st.port_change = UGETW(ps.wPortChange);
581 /* debugging print */
583 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
584 "wPortChange=0x%04x, err=%s\n",
585 portno, sc->sc_st.port_status,
586 sc->sc_st.port_change, usbd_errstr(err));
590 /*------------------------------------------------------------------------*
595 * Else: A control transaction failed
596 *------------------------------------------------------------------------*/
598 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
600 struct usb_device *child;
601 struct usb_device *udev;
602 enum usb_dev_speed speed;
603 enum usb_hc_mode mode;
608 DPRINTF("reattaching port %d\n", portno);
612 child = usb_bus_port_get_device(udev->bus,
613 udev->hub->ports + portno - 1);
617 /* first clear the port connection change bit */
619 err = usbd_req_clear_port_feature(udev, NULL,
620 portno, UHF_C_PORT_CONNECTION);
625 /* check if there is a child */
629 * Free USB device and all subdevices, if any.
631 usb_free_device(child, 0);
634 /* get fresh status */
636 err = uhub_read_port_status(sc, portno);
640 /* check if nothing is connected to the port */
642 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
645 /* check if there is no power on the port and print a warning */
647 switch (udev->speed) {
651 power_mask = UPS_PORT_POWER;
653 case USB_SPEED_SUPER:
654 if (udev->parent_hub == NULL)
655 power_mask = UPS_PORT_POWER;
657 power_mask = UPS_PORT_POWER_SS;
663 if (!(sc->sc_st.port_status & power_mask)) {
664 DPRINTF("WARNING: strange, connected port %d "
665 "has no power\n", portno);
668 /* check if the device is in Host Mode */
670 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
672 DPRINTF("Port %d is in Host Mode\n", portno);
674 if (sc->sc_st.port_status & UPS_SUSPEND) {
676 * NOTE: Should not get here in SuperSpeed
677 * mode, because the HUB should report this
680 DPRINTF("Port %d was still "
681 "suspended, clearing.\n", portno);
682 err = usbd_req_clear_port_feature(udev,
683 NULL, portno, UHF_PORT_SUSPEND);
688 /* wait for maximum device power up time */
691 USB_MS_TO_TICKS(usb_port_powerup_delay));
693 /* reset port, which implies enabling it */
695 err = usbd_req_reset_port(udev, NULL, portno);
698 DPRINTFN(0, "port %d reset "
699 "failed, error=%s\n",
700 portno, usbd_errstr(err));
703 /* get port status again, it might have changed during reset */
705 err = uhub_read_port_status(sc, portno);
709 /* check if something changed during port reset */
711 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
712 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
714 DPRINTFN(0, "giving up port reset "
715 "- device vanished\n");
722 DPRINTF("Port %d is in Device Mode\n", portno);
726 * Figure out the device speed
728 switch (udev->speed) {
730 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
731 speed = USB_SPEED_HIGH;
732 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
733 speed = USB_SPEED_LOW;
735 speed = USB_SPEED_FULL;
738 if (sc->sc_st.port_status & UPS_LOW_SPEED)
739 speed = USB_SPEED_LOW;
741 speed = USB_SPEED_FULL;
744 speed = USB_SPEED_LOW;
746 case USB_SPEED_SUPER:
747 if (udev->parent_hub == NULL) {
748 /* Root HUB - special case */
749 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
751 speed = USB_SPEED_FULL;
754 speed = USB_SPEED_LOW;
757 speed = USB_SPEED_HIGH;
760 speed = USB_SPEED_SUPER;
764 speed = USB_SPEED_SUPER;
768 /* same speed like parent */
772 if (speed == USB_SPEED_SUPER) {
773 err = usbd_req_set_hub_u1_timeout(udev, NULL,
774 portno, 128 - (2 * udev->depth));
776 DPRINTFN(0, "port %d U1 timeout "
777 "failed, error=%s\n",
778 portno, usbd_errstr(err));
780 err = usbd_req_set_hub_u2_timeout(udev, NULL,
781 portno, 128 - (2 * udev->depth));
783 DPRINTFN(0, "port %d U2 timeout "
784 "failed, error=%s\n",
785 portno, usbd_errstr(err));
790 * Figure out the device mode
792 * NOTE: This part is currently FreeBSD specific.
794 if (udev->parent_hub != NULL) {
795 /* inherit mode from the parent HUB */
796 mode = udev->parent_hub->flags.usb_mode;
797 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
798 mode = USB_MODE_DEVICE;
800 mode = USB_MODE_HOST;
802 /* need to create a new child */
803 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
804 udev->depth + 1, portno - 1, portno, speed, mode);
806 DPRINTFN(0, "could not allocate new device\n");
809 return (0); /* success */
814 * Free USB device and all subdevices, if any.
816 usb_free_device(child, 0);
820 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
821 err = usbd_req_clear_port_feature(
823 portno, UHF_PORT_ENABLE);
827 DPRINTFN(0, "device problem (%s), "
828 "disabling port %d\n", usbd_errstr(err), portno);
833 /*------------------------------------------------------------------------*
834 * usb_device_20_compatible
837 * 0: HUB does not support suspend and resume
838 * Else: HUB supports suspend and resume
839 *------------------------------------------------------------------------*/
841 usb_device_20_compatible(struct usb_device *udev)
845 switch (udev->speed) {
855 /*------------------------------------------------------------------------*
856 * uhub_suspend_resume_port
860 * Else: A control transaction failed
861 *------------------------------------------------------------------------*/
863 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
865 struct usb_device *child;
866 struct usb_device *udev;
870 DPRINTF("port %d\n", portno);
873 child = usb_bus_port_get_device(udev->bus,
874 udev->hub->ports + portno - 1);
876 /* first clear the port suspend change bit */
878 if (usb_device_20_compatible(udev)) {
879 err = usbd_req_clear_port_feature(udev, NULL,
880 portno, UHF_C_PORT_SUSPEND);
882 err = usbd_req_clear_port_feature(udev, NULL,
883 portno, UHF_C_PORT_LINK_STATE);
887 DPRINTF("clearing suspend failed.\n");
890 /* get fresh status */
892 err = uhub_read_port_status(sc, portno);
894 DPRINTF("reading port status failed.\n");
897 /* convert current state */
899 if (usb_device_20_compatible(udev)) {
900 if (sc->sc_st.port_status & UPS_SUSPEND) {
906 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
910 case UPS_PORT_LS_SS_INA:
911 usbd_req_warm_reset_port(udev, NULL, portno);
920 DPRINTF("suspended=%u\n", is_suspend);
922 /* do the suspend or resume */
926 * This code handle two cases: 1) Host Mode - we can only
927 * receive resume here 2) Device Mode - we can receive
928 * suspend and resume here
931 usb_dev_resume_peer(child);
932 else if (child->flags.usb_mode == USB_MODE_DEVICE)
933 usb_dev_suspend_peer(child);
939 /*------------------------------------------------------------------------*
940 * uhub_root_interrupt
942 * This function is called when a Root HUB interrupt has
943 * happened. "ptr" and "len" makes up the Root HUB interrupt
944 * packet. This function is called having the "bus_mtx" locked.
945 *------------------------------------------------------------------------*/
947 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
949 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
951 usb_needs_explore(bus, 0);
955 uhub_is_too_deep(struct usb_device *udev)
957 switch (udev->speed) {
961 if (udev->depth > USB_HUB_MAX_DEPTH)
964 case USB_SPEED_SUPER:
965 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
974 /*------------------------------------------------------------------------*
980 *------------------------------------------------------------------------*/
982 uhub_explore(struct usb_device *udev)
985 struct uhub_softc *sc;
995 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
997 /* ignore devices that are too deep */
998 if (uhub_is_too_deep(udev))
999 return (USB_ERR_TOO_DEEP);
1001 /* check if device is suspended */
1002 if (udev->flags.self_suspended) {
1003 /* need to wait until the child signals resume */
1004 DPRINTF("Device is suspended!\n");
1009 * Make sure we don't race against user-space applications
1012 do_unlock = usbd_enum_lock(udev);
1014 for (x = 0; x != hub->nports; x++) {
1015 up = hub->ports + x;
1018 err = uhub_read_port_status(sc, portno);
1020 /* most likely the HUB is gone */
1023 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1024 DPRINTF("Overcurrent on port %u.\n", portno);
1025 err = usbd_req_clear_port_feature(
1026 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1028 /* most likely the HUB is gone */
1032 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1034 * Fake a connect status change so that the
1035 * status gets checked initially!
1037 sc->sc_st.port_change |=
1038 UPS_C_CONNECT_STATUS;
1040 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1041 err = usbd_req_clear_port_feature(
1042 udev, NULL, portno, UHF_C_PORT_ENABLE);
1044 /* most likely the HUB is gone */
1047 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1049 * Ignore the port error if the device
1052 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1053 DPRINTFN(0, "illegal enable change, "
1054 "port %d\n", portno);
1057 if (up->restartcnt == USB_RESTART_MAX) {
1058 /* XXX could try another speed ? */
1059 DPRINTFN(0, "port error, giving up "
1060 "port %d\n", portno);
1062 sc->sc_st.port_change |=
1063 UPS_C_CONNECT_STATUS;
1068 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1069 err = uhub_reattach_port(sc, portno);
1071 /* most likely the HUB is gone */
1075 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1076 UPS_C_PORT_LINK_STATE)) {
1077 err = uhub_suspend_resume_port(sc, portno);
1079 /* most likely the HUB is gone */
1083 err = uhub_explore_sub(sc, up);
1085 /* no device(s) present */
1088 /* explore succeeded - reset restart counter */
1093 usbd_enum_unlock(udev);
1095 /* initial status checked */
1096 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1098 /* return success */
1099 return (USB_ERR_NORMAL_COMPLETION);
1103 uhub_probe(device_t dev)
1105 struct usb_attach_arg *uaa = device_get_ivars(dev);
1107 if (uaa->usb_mode != USB_MODE_HOST)
1111 * The subclass for USB HUBs is currently ignored because it
1112 * is 0 for some and 1 for others.
1114 if (uaa->info.bConfigIndex == 0 &&
1115 uaa->info.bDeviceClass == UDCLASS_HUB)
1121 /* NOTE: The information returned by this function can be wrong. */
1123 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1125 struct usb_hub_descriptor hubdesc20;
1126 struct usb_hub_ss_descriptor hubdesc30;
1131 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1132 return (USB_ERR_INVAL);
1137 switch (udev->speed) {
1139 case USB_SPEED_FULL:
1140 case USB_SPEED_HIGH:
1141 /* assuming that there is one port */
1142 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1144 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1145 "error=%s\n", usbd_errstr(err));
1148 nports = hubdesc20.bNbrPorts;
1152 if (udev->speed == USB_SPEED_HIGH)
1153 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1156 case USB_SPEED_SUPER:
1157 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1159 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1160 "error=%s\n", usbd_errstr(err));
1163 nports = hubdesc30.bNbrPorts;
1169 err = USB_ERR_INVAL;
1173 if (pnports != NULL)
1183 uhub_attach(device_t dev)
1185 struct uhub_softc *sc = device_get_softc(dev);
1186 struct usb_attach_arg *uaa = device_get_ivars(dev);
1187 struct usb_device *udev = uaa->device;
1188 struct usb_device *parent_hub = udev->parent_hub;
1189 struct usb_hub *hub;
1190 struct usb_hub_descriptor hubdesc20;
1191 struct usb_hub_ss_descriptor hubdesc30;
1197 uint8_t iface_index;
1203 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1205 device_set_usb_desc(dev);
1207 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1208 "parent->selfpowered=%d\n",
1210 udev->flags.self_powered,
1213 parent_hub->flags.self_powered : 0);
1215 if (uhub_is_too_deep(udev)) {
1216 DPRINTFN(0, "HUB at depth %d, "
1217 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1221 if (!udev->flags.self_powered && parent_hub &&
1222 !parent_hub->flags.self_powered) {
1223 DPRINTFN(0, "Bus powered HUB connected to "
1224 "bus powered HUB. HUB ignored\n");
1228 if (UHUB_IS_MULTI_TT(sc)) {
1229 err = usbd_set_alt_interface_index(udev, 0, 1);
1231 device_printf(dev, "MTT could not be enabled\n");
1234 device_printf(dev, "MTT enabled\n");
1237 /* get HUB descriptor */
1239 DPRINTFN(2, "Getting HUB descriptor\n");
1241 switch (udev->speed) {
1243 case USB_SPEED_FULL:
1244 case USB_SPEED_HIGH:
1245 /* assuming that there is one port */
1246 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1248 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1249 "error=%s\n", usbd_errstr(err));
1252 /* get number of ports */
1253 nports = hubdesc20.bNbrPorts;
1255 /* get power delay */
1256 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1257 usb_extra_power_up_time);
1259 /* get complete HUB descriptor */
1261 /* check number of ports */
1263 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1264 "error=%s\n", usbd_errstr(err));
1267 /* get complete HUB descriptor */
1268 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1271 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1272 "error=%s\n", usbd_errstr(err));
1275 if (hubdesc20.bNbrPorts != nports) {
1276 DPRINTFN(0, "Number of ports changed\n");
1281 case USB_SPEED_SUPER:
1282 if (udev->parent_hub != NULL) {
1283 err = usbd_req_set_hub_depth(udev, NULL,
1286 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1287 "error=%s\n", usbd_errstr(err));
1291 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1293 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1294 "error=%s\n", usbd_errstr(err));
1297 /* get number of ports */
1298 nports = hubdesc30.bNbrPorts;
1300 /* get power delay */
1301 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1302 usb_extra_power_up_time);
1304 /* get complete HUB descriptor */
1306 /* check number of ports */
1307 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1308 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1309 "error=%s\n", usbd_errstr(err));
1312 /* get complete HUB descriptor */
1313 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1316 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1317 "error=%s\n", usbd_errstr(err));
1320 if (hubdesc30.bNbrPorts != nports) {
1321 DPRINTFN(0, "Number of ports changed\n");
1327 DPRINTF("Assuming HUB has only one port\n");
1328 /* default number of ports */
1330 /* default power delay */
1331 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1335 DPRINTFN(0, "portless HUB\n");
1338 if (nports > USB_MAX_PORTS) {
1339 DPRINTF("Port limit exceeded\n");
1342 #if (USB_HAVE_FIXED_PORT == 0)
1343 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1344 M_USBDEV, M_WAITOK | M_ZERO);
1353 /* initialize HUB structure */
1355 hub->explore = &uhub_explore;
1356 hub->nports = nports;
1357 hub->hubudev = udev;
1358 #if USB_HAVE_TT_SUPPORT
1359 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1360 hub->tt_msg[0].udev = udev;
1361 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1362 hub->tt_msg[1].udev = udev;
1364 /* if self powered hub, give ports maximum current */
1365 if (udev->flags.self_powered) {
1366 hub->portpower = USB_MAX_POWER;
1368 hub->portpower = USB_MIN_POWER;
1371 /* set up interrupt pipe */
1373 if (udev->parent_hub == NULL) {
1374 /* root HUB is special */
1378 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1379 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1382 DPRINTFN(0, "cannot setup interrupt transfer, "
1383 "errstr=%s\n", usbd_errstr(err));
1386 /* wait with power off for a while */
1387 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1390 * To have the best chance of success we do things in the exact same
1391 * order as Windoze98. This should not be necessary, but some
1392 * devices do not follow the USB specs to the letter.
1394 * These are the events on the bus when a hub is attached:
1395 * Get device and config descriptors (see attach code)
1396 * Get hub descriptor (see above)
1399 * wait for power to become stable
1400 * (all below happens in explore code)
1402 * clear C_PORT_CONNECTION
1405 * if device connected
1409 * clear C_PORT_RESET
1411 * proceed with device attachment
1414 /* XXX should check for none, individual, or ganged power? */
1418 for (x = 0; x != nports; x++) {
1419 /* set up data structures */
1420 struct usb_port *up = hub->ports + x;
1422 up->device_index = 0;
1426 /* check if port is removable */
1427 switch (udev->speed) {
1429 case USB_SPEED_FULL:
1430 case USB_SPEED_HIGH:
1431 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1434 case USB_SPEED_SUPER:
1435 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1439 DPRINTF("Assuming removable port\n");
1444 /* turn the power on */
1445 err = usbd_req_set_port_feature(udev, NULL,
1446 portno, UHF_PORT_POWER);
1449 DPRINTFN(0, "port %d power on failed, %s\n",
1450 portno, usbd_errstr(err));
1452 DPRINTF("turn on port %d power\n",
1455 /* wait for stable power */
1456 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1459 device_printf(dev, "%d port%s with %d "
1460 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1461 removable, udev->flags.self_powered ? "self" : "bus");
1463 /* Start the interrupt endpoint, if any */
1465 mtx_lock(&sc->sc_mtx);
1466 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1467 mtx_unlock(&sc->sc_mtx);
1469 /* Enable automatic power save on all USB HUBs */
1471 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1476 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1478 #if (USB_HAVE_FIXED_PORT == 0)
1479 free(udev->hub, M_USBDEV);
1483 mtx_destroy(&sc->sc_mtx);
1489 * Called from process context when the hub is gone.
1490 * Detach all devices on active ports.
1493 uhub_detach(device_t dev)
1495 struct uhub_softc *sc = device_get_softc(dev);
1496 struct usb_hub *hub = sc->sc_udev->hub;
1497 struct usb_bus *bus = sc->sc_udev->bus;
1498 struct usb_device *child;
1501 if (hub == NULL) /* must be partially working */
1504 /* Make sure interrupt transfer is gone. */
1505 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1507 /* Detach all ports */
1508 for (x = 0; x != hub->nports; x++) {
1510 child = usb_bus_port_get_device(bus, hub->ports + x);
1512 if (child == NULL) {
1517 * Free USB device and all subdevices, if any.
1519 usb_free_device(child, 0);
1522 #if USB_HAVE_TT_SUPPORT
1523 /* Make sure our TT messages are not queued anywhere */
1525 usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus),
1526 &hub->tt_msg[0], &hub->tt_msg[1]);
1527 USB_BUS_UNLOCK(bus);
1530 #if (USB_HAVE_FIXED_PORT == 0)
1531 free(hub, M_USBDEV);
1533 sc->sc_udev->hub = NULL;
1535 mtx_destroy(&sc->sc_mtx);
1541 uhub_suspend(device_t dev)
1544 /* Sub-devices are not suspended here! */
1549 uhub_resume(device_t dev)
1552 /* Sub-devices are not resumed here! */
1557 uhub_driver_added(device_t dev, driver_t *driver)
1559 usb_needs_explore_all();
1563 struct usb_device *udev;
1565 uint8_t iface_index;
1569 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1570 struct hub_result *res)
1572 struct usb_interface *iface;
1573 struct usb_device *udev;
1578 nports = hub->nports;
1579 for (x = 0; x != nports; x++) {
1580 udev = usb_bus_port_get_device(hub->hubudev->bus,
1585 for (i = 0; i != USB_IFACE_MAX; i++) {
1586 iface = usbd_get_iface(udev, i);
1588 (iface->subdev == child)) {
1589 res->iface_index = i;
1591 res->portno = x + 1;
1596 res->iface_index = 0;
1602 uhub_child_location_string(device_t parent, device_t child,
1603 char *buf, size_t buflen)
1605 struct uhub_softc *sc;
1606 struct usb_hub *hub;
1607 struct hub_result res;
1609 if (!device_is_attached(parent)) {
1615 sc = device_get_softc(parent);
1616 hub = sc->sc_udev->hub;
1619 uhub_find_iface_index(hub, child, &res);
1621 DPRINTF("device not on hub\n");
1627 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1628 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1629 res.portno, device_get_unit(res.udev->bus->bdev),
1630 res.udev->device_index, res.iface_index);
1638 uhub_child_pnpinfo_string(device_t parent, device_t child,
1639 char *buf, size_t buflen)
1641 struct uhub_softc *sc;
1642 struct usb_hub *hub;
1643 struct usb_interface *iface;
1644 struct hub_result res;
1646 if (!device_is_attached(parent)) {
1652 sc = device_get_softc(parent);
1653 hub = sc->sc_udev->hub;
1656 uhub_find_iface_index(hub, child, &res);
1658 DPRINTF("device not on hub\n");
1664 iface = usbd_get_iface(res.udev, res.iface_index);
1665 if (iface && iface->idesc) {
1666 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1667 "devclass=0x%02x devsubclass=0x%02x "
1671 "intclass=0x%02x intsubclass=0x%02x "
1672 "intprotocol=0x%02x " "%s%s",
1673 UGETW(res.udev->ddesc.idVendor),
1674 UGETW(res.udev->ddesc.idProduct),
1675 res.udev->ddesc.bDeviceClass,
1676 res.udev->ddesc.bDeviceSubClass,
1677 usb_get_serial(res.udev),
1678 UGETW(res.udev->ddesc.bcdDevice),
1679 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1680 iface->idesc->bInterfaceClass,
1681 iface->idesc->bInterfaceSubClass,
1682 iface->idesc->bInterfaceProtocol,
1683 iface->pnpinfo ? " " : "",
1684 iface->pnpinfo ? iface->pnpinfo : "");
1698 * The USB Transaction Translator:
1699 * ===============================
1701 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1702 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1703 * USB transfers. To utilize bandwidth dynamically the "scatter and
1704 * gather" principle must be applied. This means that bandwidth must
1705 * be divided into equal parts of bandwidth. With regard to USB all
1706 * data is transferred in smaller packets with length
1707 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1710 * The bandwidth scheduler which I have implemented will simply pack
1711 * the USB transfers back to back until there is no more space in the
1712 * schedule. Out of the 8 microframes which the USB 2.0 standard
1713 * provides, only 6 are available for non-HIGH-speed devices. I have
1714 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1715 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1716 * this division, it is very difficult to allocate and free bandwidth
1719 * NOTE about the Transaction Translator in USB HUBs:
1721 * USB HUBs have a very simple Transaction Translator, that will
1722 * simply pipeline all the SPLIT transactions. That means that the
1723 * transactions will be executed in the order they are queued!
1727 /*------------------------------------------------------------------------*
1728 * usb_intr_find_best_slot
1731 * The best Transaction Translation slot for an interrupt endpoint.
1732 *------------------------------------------------------------------------*/
1734 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1735 uint8_t end, uint8_t mask)
1737 usb_size_t min = (usb_size_t)-1;
1745 /* find the last slot with lesser used bandwidth */
1747 for (x = start; x < end; x++) {
1751 /* compute sum of bandwidth */
1752 for (z = x; z < end; z++) {
1753 if (mask & (1U << (z - x)))
1757 /* check if the current multi-slot is more optimal */
1763 /* check if the mask is about to be shifted out */
1764 if (mask & (1U << (end - 1 - x)))
1770 /*------------------------------------------------------------------------*
1771 * usb_hs_bandwidth_adjust
1773 * This function will update the bandwith usage for the microframe
1774 * having index "slot" by "len" bytes. "len" can be negative. If the
1775 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1776 * the "slot" argument will be replaced by the slot having least used
1777 * bandwidth. The "mask" argument is used for multi-slot allocations.
1780 * The slot in which the bandwidth update was done: 0..7
1781 *------------------------------------------------------------------------*/
1783 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1784 uint8_t slot, uint8_t mask)
1786 struct usb_bus *bus = udev->bus;
1787 struct usb_hub *hub;
1788 enum usb_dev_speed speed;
1791 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1793 speed = usbd_get_speed(udev);
1797 case USB_SPEED_FULL:
1798 if (speed == USB_SPEED_LOW) {
1802 * The Host Controller Driver should have
1803 * performed checks so that the lookup
1804 * below does not result in a NULL pointer
1808 hub = udev->parent_hs_hub->hub;
1809 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1810 slot = usb_intr_find_best_slot(hub->uframe_usage,
1811 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1813 for (x = slot; x < 8; x++) {
1814 if (mask & (1U << (x - slot))) {
1815 hub->uframe_usage[x] += len;
1816 bus->uframe_usage[x] += len;
1821 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1822 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1823 USB_HS_MICRO_FRAMES_MAX, mask);
1825 for (x = slot; x < 8; x++) {
1826 if (mask & (1U << (x - slot))) {
1827 bus->uframe_usage[x] += len;
1835 /*------------------------------------------------------------------------*
1836 * usb_hs_bandwidth_alloc
1838 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1839 *------------------------------------------------------------------------*/
1841 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1843 struct usb_device *udev;
1848 udev = xfer->xroot->udev;
1850 if (udev->flags.usb_mode != USB_MODE_HOST)
1851 return; /* not supported */
1853 xfer->endpoint->refcount_bw++;
1854 if (xfer->endpoint->refcount_bw != 1)
1855 return; /* already allocated */
1857 speed = usbd_get_speed(udev);
1859 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1861 /* allocate a microframe slot */
1864 slot = usb_hs_bandwidth_adjust(udev,
1865 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1867 xfer->endpoint->usb_uframe = slot;
1868 xfer->endpoint->usb_smask = mask << slot;
1870 if ((speed != USB_SPEED_FULL) &&
1871 (speed != USB_SPEED_LOW)) {
1872 xfer->endpoint->usb_cmask = 0x00 ;
1874 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1878 case UE_ISOCHRONOUS:
1879 switch (usbd_xfer_get_fps_shift(xfer)) {
1894 /* allocate a microframe multi-slot */
1896 slot = usb_hs_bandwidth_adjust(udev,
1897 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1899 xfer->endpoint->usb_uframe = slot;
1900 xfer->endpoint->usb_cmask = 0;
1901 xfer->endpoint->usb_smask = mask << slot;
1905 xfer->endpoint->usb_uframe = 0;
1906 xfer->endpoint->usb_cmask = 0;
1907 xfer->endpoint->usb_smask = 0;
1911 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1912 xfer->endpoint->usb_uframe,
1913 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1916 /*------------------------------------------------------------------------*
1917 * usb_hs_bandwidth_free
1919 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1920 *------------------------------------------------------------------------*/
1922 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1924 struct usb_device *udev;
1928 udev = xfer->xroot->udev;
1930 if (udev->flags.usb_mode != USB_MODE_HOST)
1931 return; /* not supported */
1933 xfer->endpoint->refcount_bw--;
1934 if (xfer->endpoint->refcount_bw != 0)
1935 return; /* still allocated */
1937 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1939 case UE_ISOCHRONOUS:
1941 slot = xfer->endpoint->usb_uframe;
1942 mask = xfer->endpoint->usb_smask;
1944 /* free microframe slot(s): */
1945 usb_hs_bandwidth_adjust(udev,
1946 -xfer->max_frame_size, slot, mask >> slot);
1948 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1949 slot, mask >> slot);
1951 xfer->endpoint->usb_uframe = 0;
1952 xfer->endpoint->usb_cmask = 0;
1953 xfer->endpoint->usb_smask = 0;
1961 /*------------------------------------------------------------------------*
1962 * usb_isoc_time_expand
1964 * This function will expand the time counter from 7-bit to 16-bit.
1967 * 16-bit isochronous time counter.
1968 *------------------------------------------------------------------------*/
1970 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1974 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1976 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1978 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1980 if (isoc_time_curr < rem) {
1981 /* the time counter wrapped around */
1982 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1984 /* update the remainder */
1986 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1987 bus->isoc_time_last |= isoc_time_curr;
1989 return (bus->isoc_time_last);
1992 /*------------------------------------------------------------------------*
1993 * usbd_fs_isoc_schedule_alloc_slot
1995 * This function will allocate bandwidth for an isochronous FULL speed
1996 * transaction in the FULL speed schedule.
2001 *------------------------------------------------------------------------*/
2002 #if USB_HAVE_TT_SUPPORT
2004 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2006 struct usb_xfer *xfer;
2007 struct usb_xfer *pipe_xfer;
2008 struct usb_bus *bus;
2010 usb_frlength_t data_len;
2018 bus = isoc_xfer->xroot->bus;
2020 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2022 /* skip self, if any */
2024 if (xfer == isoc_xfer)
2027 /* check if this USB transfer is going through the same TT */
2029 if (xfer->xroot->udev->parent_hs_hub !=
2030 isoc_xfer->xroot->udev->parent_hs_hub) {
2033 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2034 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2035 (xfer->xroot->udev->hs_port_no !=
2036 isoc_xfer->xroot->udev->hs_port_no)) {
2039 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2042 /* check if isoc_time is part of this transfer */
2044 delta = xfer->isoc_time_complete - isoc_time;
2045 if (delta > 0 && delta <= xfer->nframes) {
2046 delta = xfer->nframes - delta;
2048 len = xfer->frlengths[delta];
2057 * Check double buffered transfers. Only stream ID
2058 * equal to zero is valid here!
2060 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2063 /* skip self, if any */
2065 if (pipe_xfer == isoc_xfer)
2068 /* check if isoc_time is part of this transfer */
2070 delta = pipe_xfer->isoc_time_complete - isoc_time;
2071 if (delta > 0 && delta <= pipe_xfer->nframes) {
2072 delta = pipe_xfer->nframes - delta;
2074 len = pipe_xfer->frlengths[delta];
2084 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2085 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2089 /* check for overflow */
2091 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2096 delta = isoc_xfer->isoc_time_complete - isoc_time;
2097 if (delta > 0 && delta <= isoc_xfer->nframes) {
2098 delta = isoc_xfer->nframes - delta;
2100 len = isoc_xfer->frlengths[delta];
2108 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2109 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2113 /* check for overflow */
2115 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2122 /*------------------------------------------------------------------------*
2123 * usb_bus_port_get_device
2125 * This function is NULL safe.
2126 *------------------------------------------------------------------------*/
2128 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2130 if ((bus == NULL) || (up == NULL)) {
2134 if (up->device_index == 0) {
2138 return (bus->devices[up->device_index]);
2141 /*------------------------------------------------------------------------*
2142 * usb_bus_port_set_device
2144 * This function is NULL safe.
2145 *------------------------------------------------------------------------*/
2147 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2148 struct usb_device *udev, uint8_t device_index)
2155 * There is only one case where we don't
2156 * have an USB port, and that is the Root Hub!
2160 up->device_index = device_index;
2162 device_index = up->device_index;
2163 up->device_index = 0;
2167 * Make relationships to our new device
2169 if (device_index != 0) {
2171 mtx_lock(&usb_ref_lock);
2173 bus->devices[device_index] = udev;
2175 mtx_unlock(&usb_ref_lock);
2181 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2184 /*------------------------------------------------------------------------*
2187 * This functions is called when the USB event thread needs to run.
2188 *------------------------------------------------------------------------*/
2190 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2197 DPRINTF("No bus pointer!\n");
2200 if ((bus->devices == NULL) ||
2201 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2202 DPRINTF("No root HUB\n");
2205 if (mtx_owned(&bus->bus_mtx)) {
2214 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2215 &bus->explore_msg[0], &bus->explore_msg[1])) {
2219 USB_BUS_UNLOCK(bus);
2223 /*------------------------------------------------------------------------*
2224 * usb_needs_explore_all
2226 * This function is called whenever a new driver is loaded and will
2227 * cause that all USB busses are re-explored.
2228 *------------------------------------------------------------------------*/
2230 usb_needs_explore_all(void)
2232 struct usb_bus *bus;
2239 dc = usb_devclass_ptr;
2241 DPRINTFN(0, "no devclass\n");
2245 * Explore all USB busses in parallell.
2247 max = devclass_get_maxunit(dc);
2249 dev = devclass_get_device(dc, max);
2251 bus = device_get_softc(dev);
2253 usb_needs_explore(bus, 1);
2260 /*------------------------------------------------------------------------*
2261 * usb_bus_power_update
2263 * This function will ensure that all USB devices on the given bus are
2264 * properly suspended or resumed according to the device transfer
2266 *------------------------------------------------------------------------*/
2269 usb_bus_power_update(struct usb_bus *bus)
2271 usb_needs_explore(bus, 0 /* no probe */ );
2275 /*------------------------------------------------------------------------*
2276 * usbd_transfer_power_ref
2278 * This function will modify the power save reference counts and
2279 * wakeup the USB device associated with the given USB transfer, if
2281 *------------------------------------------------------------------------*/
2284 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2286 static const usb_power_mask_t power_mask[4] = {
2287 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2288 [UE_BULK] = USB_HW_POWER_BULK,
2289 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2290 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2292 struct usb_device *udev;
2293 uint8_t needs_explore;
2294 uint8_t needs_hw_power;
2297 udev = xfer->xroot->udev;
2299 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2300 /* no power save for root HUB */
2303 USB_BUS_LOCK(udev->bus);
2305 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2307 udev->pwr_save.last_xfer_time = ticks;
2308 udev->pwr_save.type_refs[xfer_type] += val;
2310 if (xfer->flags_int.control_xfr) {
2311 udev->pwr_save.read_refs += val;
2312 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2314 * It is not allowed to suspend during a
2317 udev->pwr_save.write_refs += val;
2319 } else if (USB_GET_DATA_ISREAD(xfer)) {
2320 udev->pwr_save.read_refs += val;
2322 udev->pwr_save.write_refs += val;
2326 if (udev->flags.self_suspended)
2327 needs_explore = usb_peer_should_wakeup(udev);
2331 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2332 DPRINTF("Adding type %u to power state\n", xfer_type);
2333 udev->bus->hw_power_state |= power_mask[xfer_type];
2343 USB_BUS_UNLOCK(udev->bus);
2345 if (needs_explore) {
2346 DPRINTF("update\n");
2347 usb_bus_power_update(udev->bus);
2348 } else if (needs_hw_power) {
2349 DPRINTF("needs power\n");
2350 if (udev->bus->methods->set_hw_power != NULL) {
2351 (udev->bus->methods->set_hw_power) (udev->bus);
2357 /*------------------------------------------------------------------------*
2358 * usb_peer_should_wakeup
2360 * This function returns non-zero if the current device should wake up.
2361 *------------------------------------------------------------------------*/
2363 usb_peer_should_wakeup(struct usb_device *udev)
2365 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2366 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2367 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2368 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2369 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2370 (udev->pwr_save.write_refs != 0) ||
2371 ((udev->pwr_save.read_refs != 0) &&
2372 (udev->flags.usb_mode == USB_MODE_HOST) &&
2373 (usb_peer_can_wakeup(udev) == 0)));
2376 /*------------------------------------------------------------------------*
2379 * This function implements the USB power daemon and is called
2380 * regularly from the USB explore thread.
2381 *------------------------------------------------------------------------*/
2384 usb_bus_powerd(struct usb_bus *bus)
2386 struct usb_device *udev;
2389 usb_ticks_t mintime;
2390 usb_size_t type_refs[5];
2393 limit = usb_power_timeout;
2396 else if (limit > 255)
2401 DPRINTF("bus=%p\n", bus);
2406 * The root HUB device is never suspended
2407 * and we simply skip it.
2409 for (x = USB_ROOT_HUB_ADDR + 1;
2410 x != bus->devices_max; x++) {
2412 udev = bus->devices[x];
2416 temp = ticks - udev->pwr_save.last_xfer_time;
2418 if (usb_peer_should_wakeup(udev)) {
2419 /* check if we are suspended */
2420 if (udev->flags.self_suspended != 0) {
2421 USB_BUS_UNLOCK(bus);
2422 usb_dev_resume_peer(udev);
2425 } else if ((temp >= limit) &&
2426 (udev->flags.usb_mode == USB_MODE_HOST) &&
2427 (udev->flags.self_suspended == 0)) {
2428 /* try to do suspend */
2430 USB_BUS_UNLOCK(bus);
2431 usb_dev_suspend_peer(udev);
2436 /* reset counters */
2438 mintime = (usb_ticks_t)-1;
2445 /* Re-loop all the devices to get the actual state */
2447 for (x = USB_ROOT_HUB_ADDR + 1;
2448 x != bus->devices_max; x++) {
2450 udev = bus->devices[x];
2454 /* we found a non-Root-Hub USB device */
2457 /* "last_xfer_time" can be updated by a resume */
2458 temp = ticks - udev->pwr_save.last_xfer_time;
2461 * Compute minimum time since last transfer for the complete
2467 if (udev->flags.self_suspended == 0) {
2468 type_refs[0] += udev->pwr_save.type_refs[0];
2469 type_refs[1] += udev->pwr_save.type_refs[1];
2470 type_refs[2] += udev->pwr_save.type_refs[2];
2471 type_refs[3] += udev->pwr_save.type_refs[3];
2475 if (mintime >= (usb_ticks_t)(1 * hz)) {
2476 /* recompute power masks */
2477 DPRINTF("Recomputing power masks\n");
2478 bus->hw_power_state = 0;
2479 if (type_refs[UE_CONTROL] != 0)
2480 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2481 if (type_refs[UE_BULK] != 0)
2482 bus->hw_power_state |= USB_HW_POWER_BULK;
2483 if (type_refs[UE_INTERRUPT] != 0)
2484 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2485 if (type_refs[UE_ISOCHRONOUS] != 0)
2486 bus->hw_power_state |= USB_HW_POWER_ISOC;
2487 if (type_refs[4] != 0)
2488 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2490 USB_BUS_UNLOCK(bus);
2492 if (bus->methods->set_hw_power != NULL) {
2493 /* always update hardware power! */
2494 (bus->methods->set_hw_power) (bus);
2500 /*------------------------------------------------------------------------*
2501 * usb_dev_resume_peer
2503 * This function will resume an USB peer and do the required USB
2504 * signalling to get an USB device out of the suspended state.
2505 *------------------------------------------------------------------------*/
2507 usb_dev_resume_peer(struct usb_device *udev)
2509 struct usb_bus *bus;
2516 /* check if already resumed */
2517 if (udev->flags.self_suspended == 0)
2520 /* we need a parent HUB to do resume */
2521 if (udev->parent_hub == NULL)
2524 DPRINTF("udev=%p\n", udev);
2526 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2527 (udev->flags.remote_wakeup == 0)) {
2529 * If the host did not set the remote wakeup feature, we can
2530 * not wake it up either!
2532 DPRINTF("remote wakeup is not set!\n");
2535 /* get bus pointer */
2538 /* resume parent hub first */
2539 usb_dev_resume_peer(udev->parent_hub);
2541 /* reduce chance of instant resume failure by waiting a little bit */
2542 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2544 if (usb_device_20_compatible(udev)) {
2545 /* resume current port (Valid in Host and Device Mode) */
2546 err = usbd_req_clear_port_feature(udev->parent_hub,
2547 NULL, udev->port_no, UHF_PORT_SUSPEND);
2549 DPRINTFN(0, "Resuming port failed\n");
2553 /* resume current port (Valid in Host and Device Mode) */
2554 err = usbd_req_set_port_link_state(udev->parent_hub,
2555 NULL, udev->port_no, UPS_PORT_LS_U0);
2557 DPRINTFN(0, "Resuming port failed\n");
2562 /* resume settle time */
2563 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2565 if (bus->methods->device_resume != NULL) {
2566 /* resume USB device on the USB controller */
2567 (bus->methods->device_resume) (udev);
2570 /* set that this device is now resumed */
2571 udev->flags.self_suspended = 0;
2573 /* make sure that we don't go into suspend right away */
2574 udev->pwr_save.last_xfer_time = ticks;
2576 /* make sure the needed power masks are on */
2577 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2578 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2579 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2580 bus->hw_power_state |= USB_HW_POWER_BULK;
2581 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2582 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2583 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2584 bus->hw_power_state |= USB_HW_POWER_ISOC;
2586 USB_BUS_UNLOCK(bus);
2588 if (bus->methods->set_hw_power != NULL) {
2589 /* always update hardware power! */
2590 (bus->methods->set_hw_power) (bus);
2595 /* notify all sub-devices about resume */
2596 err = usb_suspend_resume(udev, 0);
2598 usbd_sr_unlock(udev);
2600 /* check if peer has wakeup capability */
2601 if (usb_peer_can_wakeup(udev)) {
2602 /* clear remote wakeup */
2603 err = usbd_req_clear_device_feature(udev,
2604 NULL, UF_DEVICE_REMOTE_WAKEUP);
2606 DPRINTFN(0, "Clearing device "
2607 "remote wakeup failed: %s\n",
2613 /*------------------------------------------------------------------------*
2614 * usb_dev_suspend_peer
2616 * This function will suspend an USB peer and do the required USB
2617 * signalling to get an USB device into the suspended state.
2618 *------------------------------------------------------------------------*/
2620 usb_dev_suspend_peer(struct usb_device *udev)
2622 struct usb_device *child;
2632 /* check if already suspended */
2633 if (udev->flags.self_suspended)
2636 /* we need a parent HUB to do suspend */
2637 if (udev->parent_hub == NULL)
2640 DPRINTF("udev=%p\n", udev);
2642 /* check if the current device is a HUB */
2643 if (udev->hub != NULL) {
2644 nports = udev->hub->nports;
2646 /* check if all devices on the HUB are suspended */
2647 for (x = 0; x != nports; x++) {
2648 child = usb_bus_port_get_device(udev->bus,
2649 udev->hub->ports + x);
2654 if (child->flags.self_suspended)
2657 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2662 if (usb_peer_can_wakeup(udev)) {
2664 * This request needs to be done before we set
2665 * "udev->flags.self_suspended":
2668 /* allow device to do remote wakeup */
2669 err = usbd_req_set_device_feature(udev,
2670 NULL, UF_DEVICE_REMOTE_WAKEUP);
2672 DPRINTFN(0, "Setting device "
2673 "remote wakeup failed\n");
2677 USB_BUS_LOCK(udev->bus);
2679 * Checking for suspend condition and setting suspended bit
2682 err = usb_peer_should_wakeup(udev);
2685 * Set that this device is suspended. This variable
2686 * must be set before calling USB controller suspend
2689 udev->flags.self_suspended = 1;
2691 USB_BUS_UNLOCK(udev->bus);
2694 if (usb_peer_can_wakeup(udev)) {
2695 /* allow device to do remote wakeup */
2696 err = usbd_req_clear_device_feature(udev,
2697 NULL, UF_DEVICE_REMOTE_WAKEUP);
2699 DPRINTFN(0, "Setting device "
2700 "remote wakeup failed\n");
2704 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2705 /* resume parent HUB first */
2706 usb_dev_resume_peer(udev->parent_hub);
2708 /* reduce chance of instant resume failure by waiting a little bit */
2709 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2711 /* resume current port (Valid in Host and Device Mode) */
2712 err = usbd_req_clear_port_feature(udev->parent_hub,
2713 NULL, udev->port_no, UHF_PORT_SUSPEND);
2715 /* resume settle time */
2716 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2718 DPRINTF("Suspend was cancelled!\n");
2724 /* notify all sub-devices about suspend */
2725 err = usb_suspend_resume(udev, 1);
2727 usbd_sr_unlock(udev);
2729 if (udev->bus->methods->device_suspend != NULL) {
2732 /* suspend device on the USB controller */
2733 (udev->bus->methods->device_suspend) (udev);
2736 temp = usbd_get_dma_delay(udev);
2738 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2742 if (usb_device_20_compatible(udev)) {
2743 /* suspend current port */
2744 err = usbd_req_set_port_feature(udev->parent_hub,
2745 NULL, udev->port_no, UHF_PORT_SUSPEND);
2747 DPRINTFN(0, "Suspending port failed\n");
2751 /* suspend current port */
2752 err = usbd_req_set_port_link_state(udev->parent_hub,
2753 NULL, udev->port_no, UPS_PORT_LS_U3);
2755 DPRINTFN(0, "Suspending port failed\n");
2760 udev = udev->parent_hub;
2764 /*------------------------------------------------------------------------*
2765 * usbd_set_power_mode
2767 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2769 *------------------------------------------------------------------------*/
2771 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2773 /* filter input argument */
2774 if ((power_mode != USB_POWER_MODE_ON) &&
2775 (power_mode != USB_POWER_MODE_OFF))
2776 power_mode = USB_POWER_MODE_SAVE;
2778 power_mode = usbd_filter_power_mode(udev, power_mode);
2780 udev->power_mode = power_mode; /* update copy of power mode */
2783 usb_bus_power_update(udev->bus);
2785 usb_needs_explore(udev->bus, 0 /* no probe */ );
2789 /*------------------------------------------------------------------------*
2790 * usbd_filter_power_mode
2792 * This function filters the power mode based on hardware requirements.
2793 *------------------------------------------------------------------------*/
2795 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2797 struct usb_bus_methods *mtod;
2800 mtod = udev->bus->methods;
2803 if (mtod->get_power_mode != NULL)
2804 (mtod->get_power_mode) (udev, &temp);
2806 /* check if we should not filter */
2808 return (power_mode);
2810 /* use fixed power mode given by hardware driver */
2814 /*------------------------------------------------------------------------*
2815 * usbd_start_re_enumerate
2817 * This function starts re-enumeration of the given USB device. This
2818 * function does not need to be called BUS-locked. This function does
2819 * not wait until the re-enumeration is completed.
2820 *------------------------------------------------------------------------*/
2822 usbd_start_re_enumerate(struct usb_device *udev)
2824 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2825 udev->re_enumerate_wait = USB_RE_ENUM_START;
2826 usb_needs_explore(udev->bus, 0);
2830 /*-----------------------------------------------------------------------*
2831 * usbd_start_set_config
2833 * This function starts setting a USB configuration. This function
2834 * does not need to be called BUS-locked. This function does not wait
2835 * until the set USB configuratino is completed.
2836 *------------------------------------------------------------------------*/
2838 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2840 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2841 if (udev->curr_config_index == index) {
2842 /* no change needed */
2845 udev->next_config_index = index;
2846 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2847 usb_needs_explore(udev->bus, 0);
2849 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2850 if (udev->next_config_index == index) {
2851 /* no change needed */
2855 return (USB_ERR_PENDING_REQUESTS);