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_RWTUN, &uhub_debug, 0,
94 static int usb_power_timeout = 30; /* seconds */
96 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
97 &usb_power_timeout, 0, "USB power timeout");
100 #if USB_HAVE_DISABLE_ENUM
101 static int usb_disable_enumeration = 0;
102 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
103 &usb_disable_enumeration, 0, "Set to disable all USB device enumeration.");
106 struct uhub_current_state {
107 uint16_t port_change;
108 uint16_t port_status;
112 struct uhub_current_state sc_st;/* current state */
113 #if (USB_HAVE_FIXED_PORT != 0)
114 struct usb_hub sc_hub;
116 device_t sc_dev; /* base device */
117 struct mtx sc_mtx; /* our mutex */
118 struct usb_device *sc_udev; /* USB device */
119 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
120 #if USB_HAVE_DISABLE_ENUM
121 int sc_disable_enumeration;
124 #define UHUB_FLAG_DID_EXPLORE 0x01
127 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
128 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
129 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
130 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
131 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
133 /* prototypes for type checking: */
135 static device_probe_t uhub_probe;
136 static device_attach_t uhub_attach;
137 static device_detach_t uhub_detach;
138 static device_suspend_t uhub_suspend;
139 static device_resume_t uhub_resume;
141 static bus_driver_added_t uhub_driver_added;
142 static bus_child_location_str_t uhub_child_location_string;
143 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
145 static usb_callback_t uhub_intr_callback;
146 #if USB_HAVE_TT_SUPPORT
147 static usb_callback_t uhub_reset_tt_callback;
150 static void usb_dev_resume_peer(struct usb_device *udev);
151 static void usb_dev_suspend_peer(struct usb_device *udev);
152 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
154 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
156 [UHUB_INTR_TRANSFER] = {
157 .type = UE_INTERRUPT,
158 .endpoint = UE_ADDR_ANY,
159 .direction = UE_DIR_ANY,
161 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
162 .bufsize = 0, /* use wMaxPacketSize */
163 .callback = &uhub_intr_callback,
164 .interval = UHUB_INTR_INTERVAL,
166 #if USB_HAVE_TT_SUPPORT
167 [UHUB_RESET_TT_TRANSFER] = {
169 .endpoint = 0x00, /* Control pipe */
170 .direction = UE_DIR_ANY,
171 .bufsize = sizeof(struct usb_device_request),
172 .callback = &uhub_reset_tt_callback,
173 .timeout = 1000, /* 1 second */
174 .usb_mode = USB_MODE_HOST,
180 * driver instance for "hub" connected to "usb"
181 * and "hub" connected to "hub"
183 static devclass_t uhub_devclass;
185 static device_method_t uhub_methods[] = {
186 DEVMETHOD(device_probe, uhub_probe),
187 DEVMETHOD(device_attach, uhub_attach),
188 DEVMETHOD(device_detach, uhub_detach),
190 DEVMETHOD(device_suspend, uhub_suspend),
191 DEVMETHOD(device_resume, uhub_resume),
193 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
194 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
195 DEVMETHOD(bus_driver_added, uhub_driver_added),
199 static driver_t uhub_driver = {
201 .methods = uhub_methods,
202 .size = sizeof(struct uhub_softc)
205 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
206 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
207 MODULE_VERSION(uhub, 1);
210 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
212 struct uhub_softc *sc = usbd_xfer_softc(xfer);
214 switch (USB_GET_STATE(xfer)) {
215 case USB_ST_TRANSFERRED:
218 * This is an indication that some port
219 * has changed status. Notify the bus
220 * event handler thread that we need
221 * to be explored again:
223 usb_needs_explore(sc->sc_udev->bus, 0);
226 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
227 usbd_transfer_submit(xfer);
231 if (xfer->error != USB_ERR_CANCELLED) {
233 * Do a clear-stall. The "stall_pipe" flag
234 * will get cleared before next callback by
237 usbd_xfer_set_stall(xfer);
238 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
239 usbd_transfer_submit(xfer);
245 /*------------------------------------------------------------------------*
248 * This function starts the TT reset USB request
249 *------------------------------------------------------------------------*/
250 #if USB_HAVE_TT_SUPPORT
252 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
254 struct usb_udev_msg *pm = (void *)_pm;
255 struct usb_device *udev = pm->udev;
257 struct uhub_softc *sc;
267 USB_BUS_UNLOCK(udev->bus);
268 mtx_lock(&sc->sc_mtx);
270 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
272 mtx_unlock(&sc->sc_mtx);
273 USB_BUS_LOCK(udev->bus);
277 /*------------------------------------------------------------------------*
278 * uhub_tt_buffer_reset_async_locked
280 * This function queues a TT reset for the given USB device and endpoint.
281 *------------------------------------------------------------------------*/
282 #if USB_HAVE_TT_SUPPORT
284 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
286 struct usb_device_request req;
287 struct usb_device *udev;
293 if (child == NULL || ep == NULL)
296 udev = child->parent_hs_hub;
297 port = child->hs_port_no;
304 (udev->speed != USB_SPEED_HIGH) ||
305 (child->speed != USB_SPEED_LOW &&
306 child->speed != USB_SPEED_FULL) ||
307 (child->flags.usb_mode != USB_MODE_HOST) ||
308 (port == 0) || (ep->edesc == NULL)) {
313 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
315 up = hub->ports + port - 1;
317 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
318 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
321 /* if we already received a clear buffer request, reset the whole TT */
322 if (up->req_reset_tt.bRequest != 0) {
323 req.bmRequestType = UT_WRITE_CLASS_OTHER;
324 req.bRequest = UR_RESET_TT;
325 USETW(req.wValue, 0);
326 req.wIndex[0] = port;
328 USETW(req.wLength, 0);
330 wValue = (ep->edesc->bEndpointAddress & 0xF) |
331 ((child->address & 0x7F) << 4) |
332 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
333 ((ep->edesc->bmAttributes & 3) << 12);
335 req.bmRequestType = UT_WRITE_CLASS_OTHER;
336 req.bRequest = UR_CLEAR_TT_BUFFER;
337 USETW(req.wValue, wValue);
338 req.wIndex[0] = port;
340 USETW(req.wLength, 0);
342 up->req_reset_tt = req;
343 /* get reset transfer started */
344 usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev->bus),
345 &hub->tt_msg[0], &hub->tt_msg[1]);
349 #if USB_HAVE_TT_SUPPORT
351 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
353 struct uhub_softc *sc;
354 struct usb_device *udev;
358 DPRINTF("TT buffer reset\n");
360 sc = usbd_xfer_softc(xfer);
363 switch (USB_GET_STATE(xfer)) {
364 case USB_ST_TRANSFERRED:
367 USB_BUS_LOCK(udev->bus);
368 /* find first port which needs a TT reset */
369 for (x = 0; x != udev->hub->nports; x++) {
370 up = udev->hub->ports + x;
372 if (up->req_reset_tt.bRequest == 0)
375 /* copy in the transfer */
376 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
377 sizeof(up->req_reset_tt));
379 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
382 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
384 USB_BUS_UNLOCK(udev->bus);
386 usbd_transfer_submit(xfer);
389 USB_BUS_UNLOCK(udev->bus);
393 if (error == USB_ERR_CANCELLED)
396 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
402 /*------------------------------------------------------------------------*
403 * uhub_count_active_host_ports
405 * This function counts the number of active ports at the given speed.
406 *------------------------------------------------------------------------*/
408 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
410 struct uhub_softc *sc;
411 struct usb_device *child;
426 for (x = 0; x != hub->nports; x++) {
428 child = usb_bus_port_get_device(udev->bus, up);
430 child->flags.usb_mode == USB_MODE_HOST &&
431 child->speed == speed)
439 uhub_explore_handle_re_enumerate(struct usb_device *child)
444 /* check if device should be re-enumerated */
445 if (child->flags.usb_mode != USB_MODE_HOST)
448 do_unlock = usbd_enum_lock(child);
449 switch (child->re_enumerate_wait) {
450 case USB_RE_ENUM_START:
451 err = usbd_set_config_index(child,
454 DPRINTF("Unconfigure failed: %s: Ignored.\n",
457 if (child->parent_hub == NULL) {
458 /* the root HUB cannot be re-enumerated */
459 DPRINTFN(6, "cannot reset root HUB\n");
462 err = usbd_req_re_enumerate(child, NULL);
465 err = usbd_set_config_index(child, 0);
467 err = usb_probe_and_attach(child,
468 USB_IFACE_INDEX_ANY);
470 child->re_enumerate_wait = USB_RE_ENUM_DONE;
473 case USB_RE_ENUM_PWR_OFF:
474 /* get the device unconfigured */
475 err = usbd_set_config_index(child,
478 DPRINTFN(0, "Could not unconfigure "
479 "device (ignored)\n");
481 if (child->parent_hub == NULL) {
482 /* the root HUB cannot be re-enumerated */
483 DPRINTFN(6, "cannot set port feature\n");
486 /* clear port enable */
487 err = usbd_req_clear_port_feature(child->parent_hub,
488 NULL, child->port_no, UHF_PORT_ENABLE);
490 DPRINTFN(0, "Could not disable port "
494 child->re_enumerate_wait = USB_RE_ENUM_DONE;
497 case USB_RE_ENUM_SET_CONFIG:
498 err = usbd_set_config_index(child,
499 child->next_config_index);
501 DPRINTF("Configure failed: %s: Ignored.\n",
504 err = usb_probe_and_attach(child,
505 USB_IFACE_INDEX_ANY);
507 child->re_enumerate_wait = USB_RE_ENUM_DONE;
511 child->re_enumerate_wait = USB_RE_ENUM_DONE;
515 usbd_enum_unlock(child);
518 /*------------------------------------------------------------------------*
519 * uhub_explore_sub - subroutine
523 * Else: A control transaction failed
524 *------------------------------------------------------------------------*/
526 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
529 struct usb_device *child;
533 bus = sc->sc_udev->bus;
536 /* get driver added refcount from USB bus */
537 refcount = bus->driver_added_refcount;
539 /* get device assosiated with the given port */
540 child = usb_bus_port_get_device(bus, up);
546 uhub_explore_handle_re_enumerate(child);
548 /* check if probe and attach should be done */
550 if (child->driver_added_refcount != refcount) {
551 child->driver_added_refcount = refcount;
552 err = usb_probe_and_attach(child,
553 USB_IFACE_INDEX_ANY);
558 /* start control transfer, if device mode */
560 if (child->flags.usb_mode == USB_MODE_DEVICE)
561 usbd_ctrl_transfer_setup(child);
563 /* if a HUB becomes present, do a recursive HUB explore */
566 err = (child->hub->explore) (child);
572 /*------------------------------------------------------------------------*
573 * uhub_read_port_status - factored out code
574 *------------------------------------------------------------------------*/
576 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
578 struct usb_port_status ps;
581 err = usbd_req_get_port_status(
582 sc->sc_udev, NULL, &ps, portno);
584 /* update status regardless of error */
586 sc->sc_st.port_status = UGETW(ps.wPortStatus);
587 sc->sc_st.port_change = UGETW(ps.wPortChange);
589 /* debugging print */
591 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
592 "wPortChange=0x%04x, err=%s\n",
593 portno, sc->sc_st.port_status,
594 sc->sc_st.port_change, usbd_errstr(err));
598 /*------------------------------------------------------------------------*
603 * Else: A control transaction failed
604 *------------------------------------------------------------------------*/
606 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
608 struct usb_device *child;
609 struct usb_device *udev;
610 enum usb_dev_speed speed;
611 enum usb_hc_mode mode;
616 DPRINTF("reattaching port %d\n", portno);
620 child = usb_bus_port_get_device(udev->bus,
621 udev->hub->ports + portno - 1);
625 /* first clear the port connection change bit */
627 err = usbd_req_clear_port_feature(udev, NULL,
628 portno, UHF_C_PORT_CONNECTION);
633 /* check if there is a child */
637 * Free USB device and all subdevices, if any.
639 usb_free_device(child, 0);
642 /* get fresh status */
644 err = uhub_read_port_status(sc, portno);
648 #if USB_HAVE_DISABLE_ENUM
649 /* check if we should skip enumeration from this USB HUB */
650 if (usb_disable_enumeration != 0 ||
651 sc->sc_disable_enumeration != 0) {
652 DPRINTF("Enumeration is disabled!\n");
656 /* check if nothing is connected to the port */
658 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
661 /* check if there is no power on the port and print a warning */
663 switch (udev->speed) {
667 power_mask = UPS_PORT_POWER;
669 case USB_SPEED_SUPER:
670 if (udev->parent_hub == NULL)
671 power_mask = UPS_PORT_POWER;
673 power_mask = UPS_PORT_POWER_SS;
679 if (!(sc->sc_st.port_status & power_mask)) {
680 DPRINTF("WARNING: strange, connected port %d "
681 "has no power\n", portno);
684 /* check if the device is in Host Mode */
686 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
688 DPRINTF("Port %d is in Host Mode\n", portno);
690 if (sc->sc_st.port_status & UPS_SUSPEND) {
692 * NOTE: Should not get here in SuperSpeed
693 * mode, because the HUB should report this
696 DPRINTF("Port %d was still "
697 "suspended, clearing.\n", portno);
698 err = usbd_req_clear_port_feature(udev,
699 NULL, portno, UHF_PORT_SUSPEND);
704 /* wait for maximum device power up time */
707 USB_MS_TO_TICKS(usb_port_powerup_delay));
709 /* reset port, which implies enabling it */
711 err = usbd_req_reset_port(udev, NULL, portno);
714 DPRINTFN(0, "port %d reset "
715 "failed, error=%s\n",
716 portno, usbd_errstr(err));
719 /* get port status again, it might have changed during reset */
721 err = uhub_read_port_status(sc, portno);
725 /* check if something changed during port reset */
727 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
728 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
730 DPRINTFN(0, "giving up port reset "
731 "- device vanished\n");
738 DPRINTF("Port %d is in Device Mode\n", portno);
742 * Figure out the device speed
744 switch (udev->speed) {
746 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
747 speed = USB_SPEED_HIGH;
748 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
749 speed = USB_SPEED_LOW;
751 speed = USB_SPEED_FULL;
754 if (sc->sc_st.port_status & UPS_LOW_SPEED)
755 speed = USB_SPEED_LOW;
757 speed = USB_SPEED_FULL;
760 speed = USB_SPEED_LOW;
762 case USB_SPEED_SUPER:
763 if (udev->parent_hub == NULL) {
764 /* Root HUB - special case */
765 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
767 speed = USB_SPEED_FULL;
770 speed = USB_SPEED_LOW;
773 speed = USB_SPEED_HIGH;
776 speed = USB_SPEED_SUPER;
780 speed = USB_SPEED_SUPER;
784 /* same speed like parent */
788 if (speed == USB_SPEED_SUPER) {
789 err = usbd_req_set_hub_u1_timeout(udev, NULL,
790 portno, 128 - (2 * udev->depth));
792 DPRINTFN(0, "port %d U1 timeout "
793 "failed, error=%s\n",
794 portno, usbd_errstr(err));
796 err = usbd_req_set_hub_u2_timeout(udev, NULL,
797 portno, 128 - (2 * udev->depth));
799 DPRINTFN(0, "port %d U2 timeout "
800 "failed, error=%s\n",
801 portno, usbd_errstr(err));
806 * Figure out the device mode
808 * NOTE: This part is currently FreeBSD specific.
810 if (udev->parent_hub != NULL) {
811 /* inherit mode from the parent HUB */
812 mode = udev->parent_hub->flags.usb_mode;
813 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
814 mode = USB_MODE_DEVICE;
816 mode = USB_MODE_HOST;
818 /* need to create a new child */
819 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
820 udev->depth + 1, portno - 1, portno, speed, mode);
822 DPRINTFN(0, "could not allocate new device\n");
825 return (0); /* success */
830 * Free USB device and all subdevices, if any.
832 usb_free_device(child, 0);
836 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
837 err = usbd_req_clear_port_feature(
839 portno, UHF_PORT_ENABLE);
843 DPRINTFN(0, "device problem (%s), "
844 "disabling port %d\n", usbd_errstr(err), portno);
849 /*------------------------------------------------------------------------*
850 * usb_device_20_compatible
853 * 0: HUB does not support suspend and resume
854 * Else: HUB supports suspend and resume
855 *------------------------------------------------------------------------*/
857 usb_device_20_compatible(struct usb_device *udev)
861 switch (udev->speed) {
871 /*------------------------------------------------------------------------*
872 * uhub_suspend_resume_port
876 * Else: A control transaction failed
877 *------------------------------------------------------------------------*/
879 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
881 struct usb_device *child;
882 struct usb_device *udev;
886 DPRINTF("port %d\n", portno);
889 child = usb_bus_port_get_device(udev->bus,
890 udev->hub->ports + portno - 1);
892 /* first clear the port suspend change bit */
894 if (usb_device_20_compatible(udev)) {
895 err = usbd_req_clear_port_feature(udev, NULL,
896 portno, UHF_C_PORT_SUSPEND);
898 err = usbd_req_clear_port_feature(udev, NULL,
899 portno, UHF_C_PORT_LINK_STATE);
903 DPRINTF("clearing suspend failed.\n");
906 /* get fresh status */
908 err = uhub_read_port_status(sc, portno);
910 DPRINTF("reading port status failed.\n");
913 /* convert current state */
915 if (usb_device_20_compatible(udev)) {
916 if (sc->sc_st.port_status & UPS_SUSPEND) {
922 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
926 case UPS_PORT_LS_SS_INA:
927 usbd_req_warm_reset_port(udev, NULL, portno);
936 DPRINTF("suspended=%u\n", is_suspend);
938 /* do the suspend or resume */
942 * This code handle two cases: 1) Host Mode - we can only
943 * receive resume here 2) Device Mode - we can receive
944 * suspend and resume here
947 usb_dev_resume_peer(child);
948 else if (child->flags.usb_mode == USB_MODE_DEVICE)
949 usb_dev_suspend_peer(child);
955 /*------------------------------------------------------------------------*
956 * uhub_root_interrupt
958 * This function is called when a Root HUB interrupt has
959 * happened. "ptr" and "len" makes up the Root HUB interrupt
960 * packet. This function is called having the "bus_mtx" locked.
961 *------------------------------------------------------------------------*/
963 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
965 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
967 usb_needs_explore(bus, 0);
971 uhub_is_too_deep(struct usb_device *udev)
973 switch (udev->speed) {
977 if (udev->depth > USB_HUB_MAX_DEPTH)
980 case USB_SPEED_SUPER:
981 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
990 /*------------------------------------------------------------------------*
996 *------------------------------------------------------------------------*/
998 uhub_explore(struct usb_device *udev)
1000 struct usb_hub *hub;
1001 struct uhub_softc *sc;
1002 struct usb_port *up;
1011 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1013 /* ignore devices that are too deep */
1014 if (uhub_is_too_deep(udev))
1015 return (USB_ERR_TOO_DEEP);
1017 /* check if device is suspended */
1018 if (udev->flags.self_suspended) {
1019 /* need to wait until the child signals resume */
1020 DPRINTF("Device is suspended!\n");
1025 * Make sure we don't race against user-space applications
1028 do_unlock = usbd_enum_lock(udev);
1030 for (x = 0; x != hub->nports; x++) {
1031 up = hub->ports + x;
1034 err = uhub_read_port_status(sc, portno);
1036 /* most likely the HUB is gone */
1039 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1040 DPRINTF("Overcurrent on port %u.\n", portno);
1041 err = usbd_req_clear_port_feature(
1042 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1044 /* most likely the HUB is gone */
1048 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1050 * Fake a connect status change so that the
1051 * status gets checked initially!
1053 sc->sc_st.port_change |=
1054 UPS_C_CONNECT_STATUS;
1056 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1057 err = usbd_req_clear_port_feature(
1058 udev, NULL, portno, UHF_C_PORT_ENABLE);
1060 /* most likely the HUB is gone */
1063 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1065 * Ignore the port error if the device
1068 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1069 DPRINTFN(0, "illegal enable change, "
1070 "port %d\n", portno);
1073 if (up->restartcnt == USB_RESTART_MAX) {
1074 /* XXX could try another speed ? */
1075 DPRINTFN(0, "port error, giving up "
1076 "port %d\n", portno);
1078 sc->sc_st.port_change |=
1079 UPS_C_CONNECT_STATUS;
1084 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1085 err = uhub_reattach_port(sc, portno);
1087 /* most likely the HUB is gone */
1091 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1092 UPS_C_PORT_LINK_STATE)) {
1093 err = uhub_suspend_resume_port(sc, portno);
1095 /* most likely the HUB is gone */
1099 err = uhub_explore_sub(sc, up);
1101 /* no device(s) present */
1104 /* explore succeeded - reset restart counter */
1109 usbd_enum_unlock(udev);
1111 /* initial status checked */
1112 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1114 /* return success */
1115 return (USB_ERR_NORMAL_COMPLETION);
1119 uhub_probe(device_t dev)
1121 struct usb_attach_arg *uaa = device_get_ivars(dev);
1123 if (uaa->usb_mode != USB_MODE_HOST)
1127 * The subclass for USB HUBs is currently ignored because it
1128 * is 0 for some and 1 for others.
1130 if (uaa->info.bConfigIndex == 0 &&
1131 uaa->info.bDeviceClass == UDCLASS_HUB)
1137 /* NOTE: The information returned by this function can be wrong. */
1139 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1141 struct usb_hub_descriptor hubdesc20;
1142 struct usb_hub_ss_descriptor hubdesc30;
1147 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1148 return (USB_ERR_INVAL);
1153 switch (udev->speed) {
1155 case USB_SPEED_FULL:
1156 case USB_SPEED_HIGH:
1157 /* assuming that there is one port */
1158 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1160 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1161 "error=%s\n", usbd_errstr(err));
1164 nports = hubdesc20.bNbrPorts;
1168 if (udev->speed == USB_SPEED_HIGH)
1169 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1172 case USB_SPEED_SUPER:
1173 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1175 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1176 "error=%s\n", usbd_errstr(err));
1179 nports = hubdesc30.bNbrPorts;
1185 err = USB_ERR_INVAL;
1189 if (pnports != NULL)
1199 uhub_attach(device_t dev)
1201 struct uhub_softc *sc = device_get_softc(dev);
1202 struct usb_attach_arg *uaa = device_get_ivars(dev);
1203 struct usb_device *udev = uaa->device;
1204 struct usb_device *parent_hub = udev->parent_hub;
1205 struct usb_hub *hub;
1206 struct usb_hub_descriptor hubdesc20;
1207 struct usb_hub_ss_descriptor hubdesc30;
1208 #if USB_HAVE_DISABLE_ENUM
1209 struct sysctl_ctx_list *sysctl_ctx;
1210 struct sysctl_oid *sysctl_tree;
1217 uint8_t iface_index;
1223 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1225 device_set_usb_desc(dev);
1227 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1228 "parent->selfpowered=%d\n",
1230 udev->flags.self_powered,
1233 parent_hub->flags.self_powered : 0);
1235 if (uhub_is_too_deep(udev)) {
1236 DPRINTFN(0, "HUB at depth %d, "
1237 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1241 if (!udev->flags.self_powered && parent_hub &&
1242 !parent_hub->flags.self_powered) {
1243 DPRINTFN(0, "Bus powered HUB connected to "
1244 "bus powered HUB. HUB ignored\n");
1248 if (UHUB_IS_MULTI_TT(sc)) {
1249 err = usbd_set_alt_interface_index(udev, 0, 1);
1251 device_printf(dev, "MTT could not be enabled\n");
1254 device_printf(dev, "MTT enabled\n");
1257 /* get HUB descriptor */
1259 DPRINTFN(2, "Getting HUB descriptor\n");
1261 switch (udev->speed) {
1263 case USB_SPEED_FULL:
1264 case USB_SPEED_HIGH:
1265 /* assuming that there is one port */
1266 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1268 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1269 "error=%s\n", usbd_errstr(err));
1272 /* get number of ports */
1273 nports = hubdesc20.bNbrPorts;
1275 /* get power delay */
1276 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1277 usb_extra_power_up_time);
1279 /* get complete HUB descriptor */
1281 /* check number of ports */
1283 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1284 "error=%s\n", usbd_errstr(err));
1287 /* get complete HUB descriptor */
1288 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1291 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1292 "error=%s\n", usbd_errstr(err));
1295 if (hubdesc20.bNbrPorts != nports) {
1296 DPRINTFN(0, "Number of ports changed\n");
1301 case USB_SPEED_SUPER:
1302 if (udev->parent_hub != NULL) {
1303 err = usbd_req_set_hub_depth(udev, NULL,
1306 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1307 "error=%s\n", usbd_errstr(err));
1311 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1313 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1314 "error=%s\n", usbd_errstr(err));
1317 /* get number of ports */
1318 nports = hubdesc30.bNbrPorts;
1320 /* get power delay */
1321 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1322 usb_extra_power_up_time);
1324 /* get complete HUB descriptor */
1326 /* check number of ports */
1327 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1328 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1329 "error=%s\n", usbd_errstr(err));
1332 /* get complete HUB descriptor */
1333 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1336 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1337 "error=%s\n", usbd_errstr(err));
1340 if (hubdesc30.bNbrPorts != nports) {
1341 DPRINTFN(0, "Number of ports changed\n");
1347 DPRINTF("Assuming HUB has only one port\n");
1348 /* default number of ports */
1350 /* default power delay */
1351 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1355 DPRINTFN(0, "portless HUB\n");
1358 if (nports > USB_MAX_PORTS) {
1359 DPRINTF("Port limit exceeded\n");
1362 #if (USB_HAVE_FIXED_PORT == 0)
1363 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1364 M_USBDEV, M_WAITOK | M_ZERO);
1373 /* initialize HUB structure */
1375 hub->explore = &uhub_explore;
1376 hub->nports = nports;
1377 hub->hubudev = udev;
1378 #if USB_HAVE_TT_SUPPORT
1379 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1380 hub->tt_msg[0].udev = udev;
1381 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1382 hub->tt_msg[1].udev = udev;
1384 /* if self powered hub, give ports maximum current */
1385 if (udev->flags.self_powered) {
1386 hub->portpower = USB_MAX_POWER;
1388 hub->portpower = USB_MIN_POWER;
1391 /* set up interrupt pipe */
1393 if (udev->parent_hub == NULL) {
1394 /* root HUB is special */
1398 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1399 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1402 DPRINTFN(0, "cannot setup interrupt transfer, "
1403 "errstr=%s\n", usbd_errstr(err));
1406 /* wait with power off for a while */
1407 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1410 * To have the best chance of success we do things in the exact same
1411 * order as Windoze98. This should not be necessary, but some
1412 * devices do not follow the USB specs to the letter.
1414 * These are the events on the bus when a hub is attached:
1415 * Get device and config descriptors (see attach code)
1416 * Get hub descriptor (see above)
1419 * wait for power to become stable
1420 * (all below happens in explore code)
1422 * clear C_PORT_CONNECTION
1425 * if device connected
1429 * clear C_PORT_RESET
1431 * proceed with device attachment
1434 /* XXX should check for none, individual, or ganged power? */
1438 for (x = 0; x != nports; x++) {
1439 /* set up data structures */
1440 struct usb_port *up = hub->ports + x;
1442 up->device_index = 0;
1446 /* check if port is removable */
1447 switch (udev->speed) {
1449 case USB_SPEED_FULL:
1450 case USB_SPEED_HIGH:
1451 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1454 case USB_SPEED_SUPER:
1455 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1459 DPRINTF("Assuming removable port\n");
1464 /* turn the power on */
1465 err = usbd_req_set_port_feature(udev, NULL,
1466 portno, UHF_PORT_POWER);
1469 DPRINTFN(0, "port %d power on failed, %s\n",
1470 portno, usbd_errstr(err));
1472 DPRINTF("turn on port %d power\n",
1475 /* wait for stable power */
1476 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1479 device_printf(dev, "%d port%s with %d "
1480 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1481 removable, udev->flags.self_powered ? "self" : "bus");
1483 /* Start the interrupt endpoint, if any */
1485 mtx_lock(&sc->sc_mtx);
1486 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1487 mtx_unlock(&sc->sc_mtx);
1489 /* Enable automatic power save on all USB HUBs */
1491 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1493 #if USB_HAVE_DISABLE_ENUM
1494 /* Add device sysctls */
1496 sysctl_ctx = device_get_sysctl_ctx(dev);
1497 sysctl_tree = device_get_sysctl_tree(dev);
1499 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1500 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1501 OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1502 &sc->sc_disable_enumeration, 0,
1503 "Set to disable enumeration on this USB HUB.");
1509 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1511 #if (USB_HAVE_FIXED_PORT == 0)
1512 free(udev->hub, M_USBDEV);
1516 mtx_destroy(&sc->sc_mtx);
1522 * Called from process context when the hub is gone.
1523 * Detach all devices on active ports.
1526 uhub_detach(device_t dev)
1528 struct uhub_softc *sc = device_get_softc(dev);
1529 struct usb_hub *hub = sc->sc_udev->hub;
1530 struct usb_bus *bus = sc->sc_udev->bus;
1531 struct usb_device *child;
1534 if (hub == NULL) /* must be partially working */
1537 /* Make sure interrupt transfer is gone. */
1538 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1540 /* Detach all ports */
1541 for (x = 0; x != hub->nports; x++) {
1543 child = usb_bus_port_get_device(bus, hub->ports + x);
1545 if (child == NULL) {
1550 * Free USB device and all subdevices, if any.
1552 usb_free_device(child, 0);
1555 #if USB_HAVE_TT_SUPPORT
1556 /* Make sure our TT messages are not queued anywhere */
1558 usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus),
1559 &hub->tt_msg[0], &hub->tt_msg[1]);
1560 USB_BUS_UNLOCK(bus);
1563 #if (USB_HAVE_FIXED_PORT == 0)
1564 free(hub, M_USBDEV);
1566 sc->sc_udev->hub = NULL;
1568 mtx_destroy(&sc->sc_mtx);
1574 uhub_suspend(device_t dev)
1577 /* Sub-devices are not suspended here! */
1582 uhub_resume(device_t dev)
1585 /* Sub-devices are not resumed here! */
1590 uhub_driver_added(device_t dev, driver_t *driver)
1592 usb_needs_explore_all();
1596 struct usb_device *udev;
1598 uint8_t iface_index;
1602 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1603 struct hub_result *res)
1605 struct usb_interface *iface;
1606 struct usb_device *udev;
1611 nports = hub->nports;
1612 for (x = 0; x != nports; x++) {
1613 udev = usb_bus_port_get_device(hub->hubudev->bus,
1618 for (i = 0; i != USB_IFACE_MAX; i++) {
1619 iface = usbd_get_iface(udev, i);
1621 (iface->subdev == child)) {
1622 res->iface_index = i;
1624 res->portno = x + 1;
1629 res->iface_index = 0;
1635 uhub_child_location_string(device_t parent, device_t child,
1636 char *buf, size_t buflen)
1638 struct uhub_softc *sc;
1639 struct usb_hub *hub;
1640 struct hub_result res;
1642 if (!device_is_attached(parent)) {
1648 sc = device_get_softc(parent);
1649 hub = sc->sc_udev->hub;
1652 uhub_find_iface_index(hub, child, &res);
1654 DPRINTF("device not on hub\n");
1660 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1661 device_get_unit(res.udev->bus->bdev),
1662 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1664 res.udev->device_index, res.iface_index);
1672 uhub_child_pnpinfo_string(device_t parent, device_t child,
1673 char *buf, size_t buflen)
1675 struct uhub_softc *sc;
1676 struct usb_hub *hub;
1677 struct usb_interface *iface;
1678 struct hub_result res;
1680 if (!device_is_attached(parent)) {
1686 sc = device_get_softc(parent);
1687 hub = sc->sc_udev->hub;
1690 uhub_find_iface_index(hub, child, &res);
1692 DPRINTF("device not on hub\n");
1698 iface = usbd_get_iface(res.udev, res.iface_index);
1699 if (iface && iface->idesc) {
1700 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1701 "devclass=0x%02x devsubclass=0x%02x "
1705 "intclass=0x%02x intsubclass=0x%02x "
1706 "intprotocol=0x%02x " "%s%s",
1707 UGETW(res.udev->ddesc.idVendor),
1708 UGETW(res.udev->ddesc.idProduct),
1709 res.udev->ddesc.bDeviceClass,
1710 res.udev->ddesc.bDeviceSubClass,
1711 usb_get_serial(res.udev),
1712 UGETW(res.udev->ddesc.bcdDevice),
1713 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1714 iface->idesc->bInterfaceClass,
1715 iface->idesc->bInterfaceSubClass,
1716 iface->idesc->bInterfaceProtocol,
1717 iface->pnpinfo ? " " : "",
1718 iface->pnpinfo ? iface->pnpinfo : "");
1732 * The USB Transaction Translator:
1733 * ===============================
1735 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1736 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1737 * USB transfers. To utilize bandwidth dynamically the "scatter and
1738 * gather" principle must be applied. This means that bandwidth must
1739 * be divided into equal parts of bandwidth. With regard to USB all
1740 * data is transferred in smaller packets with length
1741 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1744 * The bandwidth scheduler which I have implemented will simply pack
1745 * the USB transfers back to back until there is no more space in the
1746 * schedule. Out of the 8 microframes which the USB 2.0 standard
1747 * provides, only 6 are available for non-HIGH-speed devices. I have
1748 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1749 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1750 * this division, it is very difficult to allocate and free bandwidth
1753 * NOTE about the Transaction Translator in USB HUBs:
1755 * USB HUBs have a very simple Transaction Translator, that will
1756 * simply pipeline all the SPLIT transactions. That means that the
1757 * transactions will be executed in the order they are queued!
1761 /*------------------------------------------------------------------------*
1762 * usb_intr_find_best_slot
1765 * The best Transaction Translation slot for an interrupt endpoint.
1766 *------------------------------------------------------------------------*/
1768 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1769 uint8_t end, uint8_t mask)
1771 usb_size_t min = (usb_size_t)-1;
1779 /* find the last slot with lesser used bandwidth */
1781 for (x = start; x < end; x++) {
1785 /* compute sum of bandwidth */
1786 for (z = x; z < end; z++) {
1787 if (mask & (1U << (z - x)))
1791 /* check if the current multi-slot is more optimal */
1797 /* check if the mask is about to be shifted out */
1798 if (mask & (1U << (end - 1 - x)))
1804 /*------------------------------------------------------------------------*
1805 * usb_hs_bandwidth_adjust
1807 * This function will update the bandwith usage for the microframe
1808 * having index "slot" by "len" bytes. "len" can be negative. If the
1809 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1810 * the "slot" argument will be replaced by the slot having least used
1811 * bandwidth. The "mask" argument is used for multi-slot allocations.
1814 * The slot in which the bandwidth update was done: 0..7
1815 *------------------------------------------------------------------------*/
1817 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1818 uint8_t slot, uint8_t mask)
1820 struct usb_bus *bus = udev->bus;
1821 struct usb_hub *hub;
1822 enum usb_dev_speed speed;
1825 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1827 speed = usbd_get_speed(udev);
1831 case USB_SPEED_FULL:
1832 if (speed == USB_SPEED_LOW) {
1836 * The Host Controller Driver should have
1837 * performed checks so that the lookup
1838 * below does not result in a NULL pointer
1842 hub = udev->parent_hs_hub->hub;
1843 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1844 slot = usb_intr_find_best_slot(hub->uframe_usage,
1845 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1847 for (x = slot; x < 8; x++) {
1848 if (mask & (1U << (x - slot))) {
1849 hub->uframe_usage[x] += len;
1850 bus->uframe_usage[x] += len;
1855 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1856 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1857 USB_HS_MICRO_FRAMES_MAX, mask);
1859 for (x = slot; x < 8; x++) {
1860 if (mask & (1U << (x - slot))) {
1861 bus->uframe_usage[x] += len;
1869 /*------------------------------------------------------------------------*
1870 * usb_hs_bandwidth_alloc
1872 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1873 *------------------------------------------------------------------------*/
1875 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1877 struct usb_device *udev;
1882 udev = xfer->xroot->udev;
1884 if (udev->flags.usb_mode != USB_MODE_HOST)
1885 return; /* not supported */
1887 xfer->endpoint->refcount_bw++;
1888 if (xfer->endpoint->refcount_bw != 1)
1889 return; /* already allocated */
1891 speed = usbd_get_speed(udev);
1893 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1895 /* allocate a microframe slot */
1898 slot = usb_hs_bandwidth_adjust(udev,
1899 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1901 xfer->endpoint->usb_uframe = slot;
1902 xfer->endpoint->usb_smask = mask << slot;
1904 if ((speed != USB_SPEED_FULL) &&
1905 (speed != USB_SPEED_LOW)) {
1906 xfer->endpoint->usb_cmask = 0x00 ;
1908 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1912 case UE_ISOCHRONOUS:
1913 switch (usbd_xfer_get_fps_shift(xfer)) {
1928 /* allocate a microframe multi-slot */
1930 slot = usb_hs_bandwidth_adjust(udev,
1931 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1933 xfer->endpoint->usb_uframe = slot;
1934 xfer->endpoint->usb_cmask = 0;
1935 xfer->endpoint->usb_smask = mask << slot;
1939 xfer->endpoint->usb_uframe = 0;
1940 xfer->endpoint->usb_cmask = 0;
1941 xfer->endpoint->usb_smask = 0;
1945 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1946 xfer->endpoint->usb_uframe,
1947 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1950 /*------------------------------------------------------------------------*
1951 * usb_hs_bandwidth_free
1953 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1954 *------------------------------------------------------------------------*/
1956 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1958 struct usb_device *udev;
1962 udev = xfer->xroot->udev;
1964 if (udev->flags.usb_mode != USB_MODE_HOST)
1965 return; /* not supported */
1967 xfer->endpoint->refcount_bw--;
1968 if (xfer->endpoint->refcount_bw != 0)
1969 return; /* still allocated */
1971 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1973 case UE_ISOCHRONOUS:
1975 slot = xfer->endpoint->usb_uframe;
1976 mask = xfer->endpoint->usb_smask;
1978 /* free microframe slot(s): */
1979 usb_hs_bandwidth_adjust(udev,
1980 -xfer->max_frame_size, slot, mask >> slot);
1982 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1983 slot, mask >> slot);
1985 xfer->endpoint->usb_uframe = 0;
1986 xfer->endpoint->usb_cmask = 0;
1987 xfer->endpoint->usb_smask = 0;
1995 /*------------------------------------------------------------------------*
1996 * usb_isoc_time_expand
1998 * This function will expand the time counter from 7-bit to 16-bit.
2001 * 16-bit isochronous time counter.
2002 *------------------------------------------------------------------------*/
2004 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2008 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2010 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2012 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2014 if (isoc_time_curr < rem) {
2015 /* the time counter wrapped around */
2016 bus->isoc_time_last += USB_ISOC_TIME_MAX;
2018 /* update the remainder */
2020 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2021 bus->isoc_time_last |= isoc_time_curr;
2023 return (bus->isoc_time_last);
2026 /*------------------------------------------------------------------------*
2027 * usbd_fs_isoc_schedule_alloc_slot
2029 * This function will allocate bandwidth for an isochronous FULL speed
2030 * transaction in the FULL speed schedule.
2035 *------------------------------------------------------------------------*/
2036 #if USB_HAVE_TT_SUPPORT
2038 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2040 struct usb_xfer *xfer;
2041 struct usb_xfer *pipe_xfer;
2042 struct usb_bus *bus;
2044 usb_frlength_t data_len;
2052 bus = isoc_xfer->xroot->bus;
2054 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2056 /* skip self, if any */
2058 if (xfer == isoc_xfer)
2061 /* check if this USB transfer is going through the same TT */
2063 if (xfer->xroot->udev->parent_hs_hub !=
2064 isoc_xfer->xroot->udev->parent_hs_hub) {
2067 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2068 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2069 (xfer->xroot->udev->hs_port_no !=
2070 isoc_xfer->xroot->udev->hs_port_no)) {
2073 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2076 /* check if isoc_time is part of this transfer */
2078 delta = xfer->isoc_time_complete - isoc_time;
2079 if (delta > 0 && delta <= xfer->nframes) {
2080 delta = xfer->nframes - delta;
2082 len = xfer->frlengths[delta];
2091 * Check double buffered transfers. Only stream ID
2092 * equal to zero is valid here!
2094 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2097 /* skip self, if any */
2099 if (pipe_xfer == isoc_xfer)
2102 /* check if isoc_time is part of this transfer */
2104 delta = pipe_xfer->isoc_time_complete - isoc_time;
2105 if (delta > 0 && delta <= pipe_xfer->nframes) {
2106 delta = pipe_xfer->nframes - delta;
2108 len = pipe_xfer->frlengths[delta];
2118 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2119 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2123 /* check for overflow */
2125 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2130 delta = isoc_xfer->isoc_time_complete - isoc_time;
2131 if (delta > 0 && delta <= isoc_xfer->nframes) {
2132 delta = isoc_xfer->nframes - delta;
2134 len = isoc_xfer->frlengths[delta];
2142 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2143 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2147 /* check for overflow */
2149 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2156 /*------------------------------------------------------------------------*
2157 * usb_bus_port_get_device
2159 * This function is NULL safe.
2160 *------------------------------------------------------------------------*/
2162 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2164 if ((bus == NULL) || (up == NULL)) {
2168 if (up->device_index == 0) {
2172 return (bus->devices[up->device_index]);
2175 /*------------------------------------------------------------------------*
2176 * usb_bus_port_set_device
2178 * This function is NULL safe.
2179 *------------------------------------------------------------------------*/
2181 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2182 struct usb_device *udev, uint8_t device_index)
2189 * There is only one case where we don't
2190 * have an USB port, and that is the Root Hub!
2194 up->device_index = device_index;
2196 device_index = up->device_index;
2197 up->device_index = 0;
2201 * Make relationships to our new device
2203 if (device_index != 0) {
2205 mtx_lock(&usb_ref_lock);
2207 bus->devices[device_index] = udev;
2209 mtx_unlock(&usb_ref_lock);
2215 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2218 /*------------------------------------------------------------------------*
2221 * This functions is called when the USB event thread needs to run.
2222 *------------------------------------------------------------------------*/
2224 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2231 DPRINTF("No bus pointer!\n");
2234 if ((bus->devices == NULL) ||
2235 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2236 DPRINTF("No root HUB\n");
2239 if (mtx_owned(&bus->bus_mtx)) {
2248 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2249 &bus->explore_msg[0], &bus->explore_msg[1])) {
2253 USB_BUS_UNLOCK(bus);
2257 /*------------------------------------------------------------------------*
2258 * usb_needs_explore_all
2260 * This function is called whenever a new driver is loaded and will
2261 * cause that all USB busses are re-explored.
2262 *------------------------------------------------------------------------*/
2264 usb_needs_explore_all(void)
2266 struct usb_bus *bus;
2273 dc = usb_devclass_ptr;
2275 DPRINTFN(0, "no devclass\n");
2279 * Explore all USB busses in parallell.
2281 max = devclass_get_maxunit(dc);
2283 dev = devclass_get_device(dc, max);
2285 bus = device_get_softc(dev);
2287 usb_needs_explore(bus, 1);
2294 /*------------------------------------------------------------------------*
2295 * usb_bus_power_update
2297 * This function will ensure that all USB devices on the given bus are
2298 * properly suspended or resumed according to the device transfer
2300 *------------------------------------------------------------------------*/
2303 usb_bus_power_update(struct usb_bus *bus)
2305 usb_needs_explore(bus, 0 /* no probe */ );
2309 /*------------------------------------------------------------------------*
2310 * usbd_transfer_power_ref
2312 * This function will modify the power save reference counts and
2313 * wakeup the USB device associated with the given USB transfer, if
2315 *------------------------------------------------------------------------*/
2318 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2320 static const usb_power_mask_t power_mask[4] = {
2321 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2322 [UE_BULK] = USB_HW_POWER_BULK,
2323 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2324 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2326 struct usb_device *udev;
2327 uint8_t needs_explore;
2328 uint8_t needs_hw_power;
2331 udev = xfer->xroot->udev;
2333 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2334 /* no power save for root HUB */
2337 USB_BUS_LOCK(udev->bus);
2339 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2341 udev->pwr_save.last_xfer_time = ticks;
2342 udev->pwr_save.type_refs[xfer_type] += val;
2344 if (xfer->flags_int.control_xfr) {
2345 udev->pwr_save.read_refs += val;
2346 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2348 * It is not allowed to suspend during a
2351 udev->pwr_save.write_refs += val;
2353 } else if (USB_GET_DATA_ISREAD(xfer)) {
2354 udev->pwr_save.read_refs += val;
2356 udev->pwr_save.write_refs += val;
2360 if (udev->flags.self_suspended)
2361 needs_explore = usb_peer_should_wakeup(udev);
2365 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2366 DPRINTF("Adding type %u to power state\n", xfer_type);
2367 udev->bus->hw_power_state |= power_mask[xfer_type];
2377 USB_BUS_UNLOCK(udev->bus);
2379 if (needs_explore) {
2380 DPRINTF("update\n");
2381 usb_bus_power_update(udev->bus);
2382 } else if (needs_hw_power) {
2383 DPRINTF("needs power\n");
2384 if (udev->bus->methods->set_hw_power != NULL) {
2385 (udev->bus->methods->set_hw_power) (udev->bus);
2391 /*------------------------------------------------------------------------*
2392 * usb_peer_should_wakeup
2394 * This function returns non-zero if the current device should wake up.
2395 *------------------------------------------------------------------------*/
2397 usb_peer_should_wakeup(struct usb_device *udev)
2399 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2400 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2401 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2402 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2403 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2404 (udev->pwr_save.write_refs != 0) ||
2405 ((udev->pwr_save.read_refs != 0) &&
2406 (udev->flags.usb_mode == USB_MODE_HOST) &&
2407 (usb_peer_can_wakeup(udev) == 0)));
2410 /*------------------------------------------------------------------------*
2413 * This function implements the USB power daemon and is called
2414 * regularly from the USB explore thread.
2415 *------------------------------------------------------------------------*/
2418 usb_bus_powerd(struct usb_bus *bus)
2420 struct usb_device *udev;
2423 usb_ticks_t mintime;
2424 usb_size_t type_refs[5];
2427 limit = usb_power_timeout;
2430 else if (limit > 255)
2435 DPRINTF("bus=%p\n", bus);
2440 * The root HUB device is never suspended
2441 * and we simply skip it.
2443 for (x = USB_ROOT_HUB_ADDR + 1;
2444 x != bus->devices_max; x++) {
2446 udev = bus->devices[x];
2450 temp = ticks - udev->pwr_save.last_xfer_time;
2452 if (usb_peer_should_wakeup(udev)) {
2453 /* check if we are suspended */
2454 if (udev->flags.self_suspended != 0) {
2455 USB_BUS_UNLOCK(bus);
2456 usb_dev_resume_peer(udev);
2459 } else if ((temp >= limit) &&
2460 (udev->flags.usb_mode == USB_MODE_HOST) &&
2461 (udev->flags.self_suspended == 0)) {
2462 /* try to do suspend */
2464 USB_BUS_UNLOCK(bus);
2465 usb_dev_suspend_peer(udev);
2470 /* reset counters */
2472 mintime = (usb_ticks_t)-1;
2479 /* Re-loop all the devices to get the actual state */
2481 for (x = USB_ROOT_HUB_ADDR + 1;
2482 x != bus->devices_max; x++) {
2484 udev = bus->devices[x];
2488 /* we found a non-Root-Hub USB device */
2491 /* "last_xfer_time" can be updated by a resume */
2492 temp = ticks - udev->pwr_save.last_xfer_time;
2495 * Compute minimum time since last transfer for the complete
2501 if (udev->flags.self_suspended == 0) {
2502 type_refs[0] += udev->pwr_save.type_refs[0];
2503 type_refs[1] += udev->pwr_save.type_refs[1];
2504 type_refs[2] += udev->pwr_save.type_refs[2];
2505 type_refs[3] += udev->pwr_save.type_refs[3];
2509 if (mintime >= (usb_ticks_t)(1 * hz)) {
2510 /* recompute power masks */
2511 DPRINTF("Recomputing power masks\n");
2512 bus->hw_power_state = 0;
2513 if (type_refs[UE_CONTROL] != 0)
2514 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2515 if (type_refs[UE_BULK] != 0)
2516 bus->hw_power_state |= USB_HW_POWER_BULK;
2517 if (type_refs[UE_INTERRUPT] != 0)
2518 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2519 if (type_refs[UE_ISOCHRONOUS] != 0)
2520 bus->hw_power_state |= USB_HW_POWER_ISOC;
2521 if (type_refs[4] != 0)
2522 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2524 USB_BUS_UNLOCK(bus);
2526 if (bus->methods->set_hw_power != NULL) {
2527 /* always update hardware power! */
2528 (bus->methods->set_hw_power) (bus);
2534 /*------------------------------------------------------------------------*
2535 * usb_dev_resume_peer
2537 * This function will resume an USB peer and do the required USB
2538 * signalling to get an USB device out of the suspended state.
2539 *------------------------------------------------------------------------*/
2541 usb_dev_resume_peer(struct usb_device *udev)
2543 struct usb_bus *bus;
2550 /* check if already resumed */
2551 if (udev->flags.self_suspended == 0)
2554 /* we need a parent HUB to do resume */
2555 if (udev->parent_hub == NULL)
2558 DPRINTF("udev=%p\n", udev);
2560 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2561 (udev->flags.remote_wakeup == 0)) {
2563 * If the host did not set the remote wakeup feature, we can
2564 * not wake it up either!
2566 DPRINTF("remote wakeup is not set!\n");
2569 /* get bus pointer */
2572 /* resume parent hub first */
2573 usb_dev_resume_peer(udev->parent_hub);
2575 /* reduce chance of instant resume failure by waiting a little bit */
2576 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2578 if (usb_device_20_compatible(udev)) {
2579 /* resume current port (Valid in Host and Device Mode) */
2580 err = usbd_req_clear_port_feature(udev->parent_hub,
2581 NULL, udev->port_no, UHF_PORT_SUSPEND);
2583 DPRINTFN(0, "Resuming port failed\n");
2587 /* resume current port (Valid in Host and Device Mode) */
2588 err = usbd_req_set_port_link_state(udev->parent_hub,
2589 NULL, udev->port_no, UPS_PORT_LS_U0);
2591 DPRINTFN(0, "Resuming port failed\n");
2596 /* resume settle time */
2597 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2599 if (bus->methods->device_resume != NULL) {
2600 /* resume USB device on the USB controller */
2601 (bus->methods->device_resume) (udev);
2604 /* set that this device is now resumed */
2605 udev->flags.self_suspended = 0;
2607 /* make sure that we don't go into suspend right away */
2608 udev->pwr_save.last_xfer_time = ticks;
2610 /* make sure the needed power masks are on */
2611 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2612 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2613 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2614 bus->hw_power_state |= USB_HW_POWER_BULK;
2615 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2616 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2617 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2618 bus->hw_power_state |= USB_HW_POWER_ISOC;
2620 USB_BUS_UNLOCK(bus);
2622 if (bus->methods->set_hw_power != NULL) {
2623 /* always update hardware power! */
2624 (bus->methods->set_hw_power) (bus);
2629 /* notify all sub-devices about resume */
2630 err = usb_suspend_resume(udev, 0);
2632 usbd_sr_unlock(udev);
2634 /* check if peer has wakeup capability */
2635 if (usb_peer_can_wakeup(udev)) {
2636 /* clear remote wakeup */
2637 err = usbd_req_clear_device_feature(udev,
2638 NULL, UF_DEVICE_REMOTE_WAKEUP);
2640 DPRINTFN(0, "Clearing device "
2641 "remote wakeup failed: %s\n",
2647 /*------------------------------------------------------------------------*
2648 * usb_dev_suspend_peer
2650 * This function will suspend an USB peer and do the required USB
2651 * signalling to get an USB device into the suspended state.
2652 *------------------------------------------------------------------------*/
2654 usb_dev_suspend_peer(struct usb_device *udev)
2656 struct usb_device *child;
2666 /* check if already suspended */
2667 if (udev->flags.self_suspended)
2670 /* we need a parent HUB to do suspend */
2671 if (udev->parent_hub == NULL)
2674 DPRINTF("udev=%p\n", udev);
2676 /* check if the current device is a HUB */
2677 if (udev->hub != NULL) {
2678 nports = udev->hub->nports;
2680 /* check if all devices on the HUB are suspended */
2681 for (x = 0; x != nports; x++) {
2682 child = usb_bus_port_get_device(udev->bus,
2683 udev->hub->ports + x);
2688 if (child->flags.self_suspended)
2691 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2696 if (usb_peer_can_wakeup(udev)) {
2698 * This request needs to be done before we set
2699 * "udev->flags.self_suspended":
2702 /* allow device to do remote wakeup */
2703 err = usbd_req_set_device_feature(udev,
2704 NULL, UF_DEVICE_REMOTE_WAKEUP);
2706 DPRINTFN(0, "Setting device "
2707 "remote wakeup failed\n");
2711 USB_BUS_LOCK(udev->bus);
2713 * Checking for suspend condition and setting suspended bit
2716 err = usb_peer_should_wakeup(udev);
2719 * Set that this device is suspended. This variable
2720 * must be set before calling USB controller suspend
2723 udev->flags.self_suspended = 1;
2725 USB_BUS_UNLOCK(udev->bus);
2728 if (usb_peer_can_wakeup(udev)) {
2729 /* allow device to do remote wakeup */
2730 err = usbd_req_clear_device_feature(udev,
2731 NULL, UF_DEVICE_REMOTE_WAKEUP);
2733 DPRINTFN(0, "Setting device "
2734 "remote wakeup failed\n");
2738 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2739 /* resume parent HUB first */
2740 usb_dev_resume_peer(udev->parent_hub);
2742 /* reduce chance of instant resume failure by waiting a little bit */
2743 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2745 /* resume current port (Valid in Host and Device Mode) */
2746 err = usbd_req_clear_port_feature(udev->parent_hub,
2747 NULL, udev->port_no, UHF_PORT_SUSPEND);
2749 /* resume settle time */
2750 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2752 DPRINTF("Suspend was cancelled!\n");
2758 /* notify all sub-devices about suspend */
2759 err = usb_suspend_resume(udev, 1);
2761 usbd_sr_unlock(udev);
2763 if (udev->bus->methods->device_suspend != NULL) {
2766 /* suspend device on the USB controller */
2767 (udev->bus->methods->device_suspend) (udev);
2770 temp = usbd_get_dma_delay(udev);
2772 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2776 if (usb_device_20_compatible(udev)) {
2777 /* suspend current port */
2778 err = usbd_req_set_port_feature(udev->parent_hub,
2779 NULL, udev->port_no, UHF_PORT_SUSPEND);
2781 DPRINTFN(0, "Suspending port failed\n");
2785 /* suspend current port */
2786 err = usbd_req_set_port_link_state(udev->parent_hub,
2787 NULL, udev->port_no, UPS_PORT_LS_U3);
2789 DPRINTFN(0, "Suspending port failed\n");
2794 udev = udev->parent_hub;
2798 /*------------------------------------------------------------------------*
2799 * usbd_set_power_mode
2801 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2803 *------------------------------------------------------------------------*/
2805 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2807 /* filter input argument */
2808 if ((power_mode != USB_POWER_MODE_ON) &&
2809 (power_mode != USB_POWER_MODE_OFF))
2810 power_mode = USB_POWER_MODE_SAVE;
2812 power_mode = usbd_filter_power_mode(udev, power_mode);
2814 udev->power_mode = power_mode; /* update copy of power mode */
2817 usb_bus_power_update(udev->bus);
2819 usb_needs_explore(udev->bus, 0 /* no probe */ );
2823 /*------------------------------------------------------------------------*
2824 * usbd_filter_power_mode
2826 * This function filters the power mode based on hardware requirements.
2827 *------------------------------------------------------------------------*/
2829 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2831 const struct usb_bus_methods *mtod;
2834 mtod = udev->bus->methods;
2837 if (mtod->get_power_mode != NULL)
2838 (mtod->get_power_mode) (udev, &temp);
2840 /* check if we should not filter */
2842 return (power_mode);
2844 /* use fixed power mode given by hardware driver */
2848 /*------------------------------------------------------------------------*
2849 * usbd_start_re_enumerate
2851 * This function starts re-enumeration of the given USB device. This
2852 * function does not need to be called BUS-locked. This function does
2853 * not wait until the re-enumeration is completed.
2854 *------------------------------------------------------------------------*/
2856 usbd_start_re_enumerate(struct usb_device *udev)
2858 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2859 udev->re_enumerate_wait = USB_RE_ENUM_START;
2860 usb_needs_explore(udev->bus, 0);
2864 /*-----------------------------------------------------------------------*
2865 * usbd_start_set_config
2867 * This function starts setting a USB configuration. This function
2868 * does not need to be called BUS-locked. This function does not wait
2869 * until the set USB configuratino is completed.
2870 *------------------------------------------------------------------------*/
2872 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2874 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2875 if (udev->curr_config_index == index) {
2876 /* no change needed */
2879 udev->next_config_index = index;
2880 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2881 usb_needs_explore(udev->bus, 0);
2883 } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2884 if (udev->next_config_index == index) {
2885 /* no change needed */
2889 return (USB_ERR_PENDING_REQUESTS);