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)
430 /*------------------------------------------------------------------------*
431 * uhub_explore_sub - subroutine
435 * Else: A control transaction failed
436 *------------------------------------------------------------------------*/
438 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
441 struct usb_device *child;
445 bus = sc->sc_udev->bus;
448 /* get driver added refcount from USB bus */
449 refcount = bus->driver_added_refcount;
451 /* get device assosiated with the given port */
452 child = usb_bus_port_get_device(bus, up);
458 /* check if device should be re-enumerated */
460 if (child->flags.usb_mode == USB_MODE_HOST) {
463 do_unlock = usbd_enum_lock(child);
464 switch (child->re_enumerate_wait) {
465 case USB_RE_ENUM_START:
466 err = usbd_set_config_index(child,
469 DPRINTF("Unconfigure failed: "
473 err = usbd_req_re_enumerate(child, NULL);
475 err = usbd_set_config_index(child, 0);
477 err = usb_probe_and_attach(child,
478 USB_IFACE_INDEX_ANY);
480 child->re_enumerate_wait = USB_RE_ENUM_DONE;
484 case USB_RE_ENUM_PWR_OFF:
485 /* get the device unconfigured */
486 err = usbd_set_config_index(child,
489 DPRINTFN(0, "Could not unconfigure "
490 "device (ignored)\n");
493 /* clear port enable */
494 err = usbd_req_clear_port_feature(child->parent_hub,
495 NULL, child->port_no, UHF_PORT_ENABLE);
497 DPRINTFN(0, "Could not disable port "
500 child->re_enumerate_wait = USB_RE_ENUM_DONE;
505 child->re_enumerate_wait = USB_RE_ENUM_DONE;
509 usbd_enum_unlock(child);
512 /* check if probe and attach should be done */
514 if (child->driver_added_refcount != refcount) {
515 child->driver_added_refcount = refcount;
516 err = usb_probe_and_attach(child,
517 USB_IFACE_INDEX_ANY);
522 /* start control transfer, if device mode */
524 if (child->flags.usb_mode == USB_MODE_DEVICE)
525 usbd_ctrl_transfer_setup(child);
527 /* if a HUB becomes present, do a recursive HUB explore */
530 err = (child->hub->explore) (child);
536 /*------------------------------------------------------------------------*
537 * uhub_read_port_status - factored out code
538 *------------------------------------------------------------------------*/
540 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
542 struct usb_port_status ps;
545 err = usbd_req_get_port_status(
546 sc->sc_udev, NULL, &ps, portno);
548 /* update status regardless of error */
550 sc->sc_st.port_status = UGETW(ps.wPortStatus);
551 sc->sc_st.port_change = UGETW(ps.wPortChange);
553 /* debugging print */
555 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
556 "wPortChange=0x%04x, err=%s\n",
557 portno, sc->sc_st.port_status,
558 sc->sc_st.port_change, usbd_errstr(err));
562 /*------------------------------------------------------------------------*
567 * Else: A control transaction failed
568 *------------------------------------------------------------------------*/
570 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
572 struct usb_device *child;
573 struct usb_device *udev;
574 enum usb_dev_speed speed;
575 enum usb_hc_mode mode;
580 DPRINTF("reattaching port %d\n", portno);
584 child = usb_bus_port_get_device(udev->bus,
585 udev->hub->ports + portno - 1);
589 /* first clear the port connection change bit */
591 err = usbd_req_clear_port_feature(udev, NULL,
592 portno, UHF_C_PORT_CONNECTION);
597 /* check if there is a child */
601 * Free USB device and all subdevices, if any.
603 usb_free_device(child, 0);
606 /* get fresh status */
608 err = uhub_read_port_status(sc, portno);
612 /* check if nothing is connected to the port */
614 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
617 /* check if there is no power on the port and print a warning */
619 switch (udev->speed) {
623 power_mask = UPS_PORT_POWER;
625 case USB_SPEED_SUPER:
626 if (udev->parent_hub == NULL)
627 power_mask = UPS_PORT_POWER;
629 power_mask = UPS_PORT_POWER_SS;
635 if (!(sc->sc_st.port_status & power_mask)) {
636 DPRINTF("WARNING: strange, connected port %d "
637 "has no power\n", portno);
640 /* check if the device is in Host Mode */
642 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
644 DPRINTF("Port %d is in Host Mode\n", portno);
646 if (sc->sc_st.port_status & UPS_SUSPEND) {
648 * NOTE: Should not get here in SuperSpeed
649 * mode, because the HUB should report this
652 DPRINTF("Port %d was still "
653 "suspended, clearing.\n", portno);
654 err = usbd_req_clear_port_feature(udev,
655 NULL, portno, UHF_PORT_SUSPEND);
660 /* wait for maximum device power up time */
663 USB_MS_TO_TICKS(usb_port_powerup_delay));
665 /* reset port, which implies enabling it */
667 err = usbd_req_reset_port(udev, NULL, portno);
670 DPRINTFN(0, "port %d reset "
671 "failed, error=%s\n",
672 portno, usbd_errstr(err));
675 /* get port status again, it might have changed during reset */
677 err = uhub_read_port_status(sc, portno);
681 /* check if something changed during port reset */
683 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
684 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
686 DPRINTFN(0, "giving up port reset "
687 "- device vanished\n");
694 DPRINTF("Port %d is in Device Mode\n", portno);
698 * Figure out the device speed
700 switch (udev->speed) {
702 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
703 speed = USB_SPEED_HIGH;
704 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
705 speed = USB_SPEED_LOW;
707 speed = USB_SPEED_FULL;
710 if (sc->sc_st.port_status & UPS_LOW_SPEED)
711 speed = USB_SPEED_LOW;
713 speed = USB_SPEED_FULL;
716 speed = USB_SPEED_LOW;
718 case USB_SPEED_SUPER:
719 if (udev->parent_hub == NULL) {
720 /* Root HUB - special case */
721 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
723 speed = USB_SPEED_FULL;
726 speed = USB_SPEED_LOW;
729 speed = USB_SPEED_HIGH;
732 speed = USB_SPEED_SUPER;
736 speed = USB_SPEED_SUPER;
740 /* same speed like parent */
744 if (speed == USB_SPEED_SUPER) {
745 err = usbd_req_set_hub_u1_timeout(udev, NULL,
746 portno, 128 - (2 * udev->depth));
748 DPRINTFN(0, "port %d U1 timeout "
749 "failed, error=%s\n",
750 portno, usbd_errstr(err));
752 err = usbd_req_set_hub_u2_timeout(udev, NULL,
753 portno, 128 - (2 * udev->depth));
755 DPRINTFN(0, "port %d U2 timeout "
756 "failed, error=%s\n",
757 portno, usbd_errstr(err));
762 * Figure out the device mode
764 * NOTE: This part is currently FreeBSD specific.
766 if (udev->parent_hub != NULL) {
767 /* inherit mode from the parent HUB */
768 mode = udev->parent_hub->flags.usb_mode;
769 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
770 mode = USB_MODE_DEVICE;
772 mode = USB_MODE_HOST;
774 /* need to create a new child */
775 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
776 udev->depth + 1, portno - 1, portno, speed, mode);
778 DPRINTFN(0, "could not allocate new device\n");
781 return (0); /* success */
786 * Free USB device and all subdevices, if any.
788 usb_free_device(child, 0);
792 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
793 err = usbd_req_clear_port_feature(
795 portno, UHF_PORT_ENABLE);
799 DPRINTFN(0, "device problem (%s), "
800 "disabling port %d\n", usbd_errstr(err), portno);
805 /*------------------------------------------------------------------------*
806 * usb_device_20_compatible
809 * 0: HUB does not support suspend and resume
810 * Else: HUB supports suspend and resume
811 *------------------------------------------------------------------------*/
813 usb_device_20_compatible(struct usb_device *udev)
817 switch (udev->speed) {
827 /*------------------------------------------------------------------------*
828 * uhub_suspend_resume_port
832 * Else: A control transaction failed
833 *------------------------------------------------------------------------*/
835 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
837 struct usb_device *child;
838 struct usb_device *udev;
842 DPRINTF("port %d\n", portno);
845 child = usb_bus_port_get_device(udev->bus,
846 udev->hub->ports + portno - 1);
848 /* first clear the port suspend change bit */
850 if (usb_device_20_compatible(udev)) {
851 err = usbd_req_clear_port_feature(udev, NULL,
852 portno, UHF_C_PORT_SUSPEND);
854 err = usbd_req_clear_port_feature(udev, NULL,
855 portno, UHF_C_PORT_LINK_STATE);
859 DPRINTF("clearing suspend failed.\n");
862 /* get fresh status */
864 err = uhub_read_port_status(sc, portno);
866 DPRINTF("reading port status failed.\n");
869 /* convert current state */
871 if (usb_device_20_compatible(udev)) {
872 if (sc->sc_st.port_status & UPS_SUSPEND) {
878 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
882 case UPS_PORT_LS_SS_INA:
883 usbd_req_warm_reset_port(udev, NULL, portno);
892 DPRINTF("suspended=%u\n", is_suspend);
894 /* do the suspend or resume */
898 * This code handle two cases: 1) Host Mode - we can only
899 * receive resume here 2) Device Mode - we can receive
900 * suspend and resume here
903 usb_dev_resume_peer(child);
904 else if (child->flags.usb_mode == USB_MODE_DEVICE)
905 usb_dev_suspend_peer(child);
911 /*------------------------------------------------------------------------*
912 * uhub_root_interrupt
914 * This function is called when a Root HUB interrupt has
915 * happened. "ptr" and "len" makes up the Root HUB interrupt
916 * packet. This function is called having the "bus_mtx" locked.
917 *------------------------------------------------------------------------*/
919 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
921 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
923 usb_needs_explore(bus, 0);
927 uhub_is_too_deep(struct usb_device *udev)
929 switch (udev->speed) {
933 if (udev->depth > USB_HUB_MAX_DEPTH)
936 case USB_SPEED_SUPER:
937 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
946 /*------------------------------------------------------------------------*
952 *------------------------------------------------------------------------*/
954 uhub_explore(struct usb_device *udev)
957 struct uhub_softc *sc;
967 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
969 /* ignore devices that are too deep */
970 if (uhub_is_too_deep(udev))
971 return (USB_ERR_TOO_DEEP);
973 /* check if device is suspended */
974 if (udev->flags.self_suspended) {
975 /* need to wait until the child signals resume */
976 DPRINTF("Device is suspended!\n");
981 * Make sure we don't race against user-space applications
984 do_unlock = usbd_enum_lock(udev);
986 for (x = 0; x != hub->nports; x++) {
990 err = uhub_read_port_status(sc, portno);
992 /* most likely the HUB is gone */
995 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
996 DPRINTF("Overcurrent on port %u.\n", portno);
997 err = usbd_req_clear_port_feature(
998 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1000 /* most likely the HUB is gone */
1004 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1006 * Fake a connect status change so that the
1007 * status gets checked initially!
1009 sc->sc_st.port_change |=
1010 UPS_C_CONNECT_STATUS;
1012 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1013 err = usbd_req_clear_port_feature(
1014 udev, NULL, portno, UHF_C_PORT_ENABLE);
1016 /* most likely the HUB is gone */
1019 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1021 * Ignore the port error if the device
1024 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1025 DPRINTFN(0, "illegal enable change, "
1026 "port %d\n", portno);
1029 if (up->restartcnt == USB_RESTART_MAX) {
1030 /* XXX could try another speed ? */
1031 DPRINTFN(0, "port error, giving up "
1032 "port %d\n", portno);
1034 sc->sc_st.port_change |=
1035 UPS_C_CONNECT_STATUS;
1040 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1041 err = uhub_reattach_port(sc, portno);
1043 /* most likely the HUB is gone */
1047 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1048 UPS_C_PORT_LINK_STATE)) {
1049 err = uhub_suspend_resume_port(sc, portno);
1051 /* most likely the HUB is gone */
1055 err = uhub_explore_sub(sc, up);
1057 /* no device(s) present */
1060 /* explore succeeded - reset restart counter */
1065 usbd_enum_unlock(udev);
1067 /* initial status checked */
1068 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1070 /* return success */
1071 return (USB_ERR_NORMAL_COMPLETION);
1075 uhub_probe(device_t dev)
1077 struct usb_attach_arg *uaa = device_get_ivars(dev);
1079 if (uaa->usb_mode != USB_MODE_HOST)
1083 * The subclass for USB HUBs is currently ignored because it
1084 * is 0 for some and 1 for others.
1086 if (uaa->info.bConfigIndex == 0 &&
1087 uaa->info.bDeviceClass == UDCLASS_HUB)
1093 /* NOTE: The information returned by this function can be wrong. */
1095 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1097 struct usb_hub_descriptor hubdesc20;
1098 struct usb_hub_ss_descriptor hubdesc30;
1103 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1104 return (USB_ERR_INVAL);
1109 switch (udev->speed) {
1111 case USB_SPEED_FULL:
1112 case USB_SPEED_HIGH:
1113 /* assuming that there is one port */
1114 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1116 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1117 "error=%s\n", usbd_errstr(err));
1120 nports = hubdesc20.bNbrPorts;
1124 if (udev->speed == USB_SPEED_HIGH)
1125 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1128 case USB_SPEED_SUPER:
1129 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1131 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1132 "error=%s\n", usbd_errstr(err));
1135 nports = hubdesc30.bNbrPorts;
1141 err = USB_ERR_INVAL;
1145 if (pnports != NULL)
1155 uhub_attach(device_t dev)
1157 struct uhub_softc *sc = device_get_softc(dev);
1158 struct usb_attach_arg *uaa = device_get_ivars(dev);
1159 struct usb_device *udev = uaa->device;
1160 struct usb_device *parent_hub = udev->parent_hub;
1161 struct usb_hub *hub;
1162 struct usb_hub_descriptor hubdesc20;
1163 struct usb_hub_ss_descriptor hubdesc30;
1169 uint8_t iface_index;
1175 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1177 device_set_usb_desc(dev);
1179 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1180 "parent->selfpowered=%d\n",
1182 udev->flags.self_powered,
1185 parent_hub->flags.self_powered : 0);
1187 if (uhub_is_too_deep(udev)) {
1188 DPRINTFN(0, "HUB at depth %d, "
1189 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1193 if (!udev->flags.self_powered && parent_hub &&
1194 !parent_hub->flags.self_powered) {
1195 DPRINTFN(0, "Bus powered HUB connected to "
1196 "bus powered HUB. HUB ignored\n");
1200 if (UHUB_IS_MULTI_TT(sc)) {
1201 err = usbd_set_alt_interface_index(udev, 0, 1);
1203 device_printf(dev, "MTT could not be enabled\n");
1206 device_printf(dev, "MTT enabled\n");
1209 /* get HUB descriptor */
1211 DPRINTFN(2, "Getting HUB descriptor\n");
1213 switch (udev->speed) {
1215 case USB_SPEED_FULL:
1216 case USB_SPEED_HIGH:
1217 /* assuming that there is one port */
1218 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1220 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1221 "error=%s\n", usbd_errstr(err));
1224 /* get number of ports */
1225 nports = hubdesc20.bNbrPorts;
1227 /* get power delay */
1228 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1229 usb_extra_power_up_time);
1231 /* get complete HUB descriptor */
1233 /* check number of ports */
1235 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1236 "error=%s\n", usbd_errstr(err));
1239 /* get complete HUB descriptor */
1240 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1243 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1244 "error=%s\n", usbd_errstr(err));
1247 if (hubdesc20.bNbrPorts != nports) {
1248 DPRINTFN(0, "Number of ports changed\n");
1253 case USB_SPEED_SUPER:
1254 if (udev->parent_hub != NULL) {
1255 err = usbd_req_set_hub_depth(udev, NULL,
1258 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1259 "error=%s\n", usbd_errstr(err));
1263 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1265 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1266 "error=%s\n", usbd_errstr(err));
1269 /* get number of ports */
1270 nports = hubdesc30.bNbrPorts;
1272 /* get power delay */
1273 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1274 usb_extra_power_up_time);
1276 /* get complete HUB descriptor */
1278 /* check number of ports */
1279 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1280 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1281 "error=%s\n", usbd_errstr(err));
1284 /* get complete HUB descriptor */
1285 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1288 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1289 "error=%s\n", usbd_errstr(err));
1292 if (hubdesc30.bNbrPorts != nports) {
1293 DPRINTFN(0, "Number of ports changed\n");
1299 DPRINTF("Assuming HUB has only one port\n");
1300 /* default number of ports */
1302 /* default power delay */
1303 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1307 DPRINTFN(0, "portless HUB\n");
1310 if (nports > USB_MAX_PORTS) {
1311 DPRINTF("Port limit exceeded\n");
1314 #if (USB_HAVE_FIXED_PORT == 0)
1315 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1316 M_USBDEV, M_WAITOK | M_ZERO);
1325 /* initialize HUB structure */
1327 hub->explore = &uhub_explore;
1328 hub->nports = nports;
1329 hub->hubudev = udev;
1330 #if USB_HAVE_TT_SUPPORT
1331 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1332 hub->tt_msg[0].udev = udev;
1333 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1334 hub->tt_msg[1].udev = udev;
1336 /* if self powered hub, give ports maximum current */
1337 if (udev->flags.self_powered) {
1338 hub->portpower = USB_MAX_POWER;
1340 hub->portpower = USB_MIN_POWER;
1343 /* set up interrupt pipe */
1345 if (udev->parent_hub == NULL) {
1346 /* root HUB is special */
1350 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1351 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1354 DPRINTFN(0, "cannot setup interrupt transfer, "
1355 "errstr=%s\n", usbd_errstr(err));
1358 /* wait with power off for a while */
1359 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1362 * To have the best chance of success we do things in the exact same
1363 * order as Windoze98. This should not be necessary, but some
1364 * devices do not follow the USB specs to the letter.
1366 * These are the events on the bus when a hub is attached:
1367 * Get device and config descriptors (see attach code)
1368 * Get hub descriptor (see above)
1371 * wait for power to become stable
1372 * (all below happens in explore code)
1374 * clear C_PORT_CONNECTION
1377 * if device connected
1381 * clear C_PORT_RESET
1383 * proceed with device attachment
1386 /* XXX should check for none, individual, or ganged power? */
1390 for (x = 0; x != nports; x++) {
1391 /* set up data structures */
1392 struct usb_port *up = hub->ports + x;
1394 up->device_index = 0;
1398 /* check if port is removable */
1399 switch (udev->speed) {
1401 case USB_SPEED_FULL:
1402 case USB_SPEED_HIGH:
1403 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1406 case USB_SPEED_SUPER:
1407 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1411 DPRINTF("Assuming removable port\n");
1416 /* turn the power on */
1417 err = usbd_req_set_port_feature(udev, NULL,
1418 portno, UHF_PORT_POWER);
1421 DPRINTFN(0, "port %d power on failed, %s\n",
1422 portno, usbd_errstr(err));
1424 DPRINTF("turn on port %d power\n",
1427 /* wait for stable power */
1428 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1431 device_printf(dev, "%d port%s with %d "
1432 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1433 removable, udev->flags.self_powered ? "self" : "bus");
1435 /* Start the interrupt endpoint, if any */
1437 mtx_lock(&sc->sc_mtx);
1438 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1439 mtx_unlock(&sc->sc_mtx);
1441 /* Enable automatic power save on all USB HUBs */
1443 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1448 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1450 #if (USB_HAVE_FIXED_PORT == 0)
1451 free(udev->hub, M_USBDEV);
1455 mtx_destroy(&sc->sc_mtx);
1461 * Called from process context when the hub is gone.
1462 * Detach all devices on active ports.
1465 uhub_detach(device_t dev)
1467 struct uhub_softc *sc = device_get_softc(dev);
1468 struct usb_hub *hub = sc->sc_udev->hub;
1469 struct usb_bus *bus = sc->sc_udev->bus;
1470 struct usb_device *child;
1473 if (hub == NULL) /* must be partially working */
1476 /* Make sure interrupt transfer is gone. */
1477 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1479 /* Detach all ports */
1480 for (x = 0; x != hub->nports; x++) {
1482 child = usb_bus_port_get_device(bus, hub->ports + x);
1484 if (child == NULL) {
1489 * Free USB device and all subdevices, if any.
1491 usb_free_device(child, 0);
1494 #if USB_HAVE_TT_SUPPORT
1495 /* Make sure our TT messages are not queued anywhere */
1497 usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus),
1498 &hub->tt_msg[0], &hub->tt_msg[1]);
1499 USB_BUS_UNLOCK(bus);
1502 #if (USB_HAVE_FIXED_PORT == 0)
1503 free(hub, M_USBDEV);
1505 sc->sc_udev->hub = NULL;
1507 mtx_destroy(&sc->sc_mtx);
1513 uhub_suspend(device_t dev)
1516 /* Sub-devices are not suspended here! */
1521 uhub_resume(device_t dev)
1524 /* Sub-devices are not resumed here! */
1529 uhub_driver_added(device_t dev, driver_t *driver)
1531 usb_needs_explore_all();
1535 struct usb_device *udev;
1537 uint8_t iface_index;
1541 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1542 struct hub_result *res)
1544 struct usb_interface *iface;
1545 struct usb_device *udev;
1550 nports = hub->nports;
1551 for (x = 0; x != nports; x++) {
1552 udev = usb_bus_port_get_device(hub->hubudev->bus,
1557 for (i = 0; i != USB_IFACE_MAX; i++) {
1558 iface = usbd_get_iface(udev, i);
1560 (iface->subdev == child)) {
1561 res->iface_index = i;
1563 res->portno = x + 1;
1568 res->iface_index = 0;
1574 uhub_child_location_string(device_t parent, device_t child,
1575 char *buf, size_t buflen)
1577 struct uhub_softc *sc;
1578 struct usb_hub *hub;
1579 struct hub_result res;
1581 if (!device_is_attached(parent)) {
1587 sc = device_get_softc(parent);
1588 hub = sc->sc_udev->hub;
1591 uhub_find_iface_index(hub, child, &res);
1593 DPRINTF("device not on hub\n");
1599 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1600 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1601 res.portno, device_get_unit(res.udev->bus->bdev),
1602 res.udev->device_index, res.iface_index);
1610 uhub_child_pnpinfo_string(device_t parent, device_t child,
1611 char *buf, size_t buflen)
1613 struct uhub_softc *sc;
1614 struct usb_hub *hub;
1615 struct usb_interface *iface;
1616 struct hub_result res;
1618 if (!device_is_attached(parent)) {
1624 sc = device_get_softc(parent);
1625 hub = sc->sc_udev->hub;
1628 uhub_find_iface_index(hub, child, &res);
1630 DPRINTF("device not on hub\n");
1636 iface = usbd_get_iface(res.udev, res.iface_index);
1637 if (iface && iface->idesc) {
1638 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1639 "devclass=0x%02x devsubclass=0x%02x "
1643 "intclass=0x%02x intsubclass=0x%02x "
1644 "intprotocol=0x%02x " "%s%s",
1645 UGETW(res.udev->ddesc.idVendor),
1646 UGETW(res.udev->ddesc.idProduct),
1647 res.udev->ddesc.bDeviceClass,
1648 res.udev->ddesc.bDeviceSubClass,
1649 usb_get_serial(res.udev),
1650 UGETW(res.udev->ddesc.bcdDevice),
1651 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1652 iface->idesc->bInterfaceClass,
1653 iface->idesc->bInterfaceSubClass,
1654 iface->idesc->bInterfaceProtocol,
1655 iface->pnpinfo ? " " : "",
1656 iface->pnpinfo ? iface->pnpinfo : "");
1670 * The USB Transaction Translator:
1671 * ===============================
1673 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1674 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1675 * USB transfers. To utilize bandwidth dynamically the "scatter and
1676 * gather" principle must be applied. This means that bandwidth must
1677 * be divided into equal parts of bandwidth. With regard to USB all
1678 * data is transferred in smaller packets with length
1679 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1682 * The bandwidth scheduler which I have implemented will simply pack
1683 * the USB transfers back to back until there is no more space in the
1684 * schedule. Out of the 8 microframes which the USB 2.0 standard
1685 * provides, only 6 are available for non-HIGH-speed devices. I have
1686 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1687 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1688 * this division, it is very difficult to allocate and free bandwidth
1691 * NOTE about the Transaction Translator in USB HUBs:
1693 * USB HUBs have a very simple Transaction Translator, that will
1694 * simply pipeline all the SPLIT transactions. That means that the
1695 * transactions will be executed in the order they are queued!
1699 /*------------------------------------------------------------------------*
1700 * usb_intr_find_best_slot
1703 * The best Transaction Translation slot for an interrupt endpoint.
1704 *------------------------------------------------------------------------*/
1706 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1707 uint8_t end, uint8_t mask)
1709 usb_size_t min = (usb_size_t)-1;
1717 /* find the last slot with lesser used bandwidth */
1719 for (x = start; x < end; x++) {
1723 /* compute sum of bandwidth */
1724 for (z = x; z < end; z++) {
1725 if (mask & (1U << (z - x)))
1729 /* check if the current multi-slot is more optimal */
1735 /* check if the mask is about to be shifted out */
1736 if (mask & (1U << (end - 1 - x)))
1742 /*------------------------------------------------------------------------*
1743 * usb_hs_bandwidth_adjust
1745 * This function will update the bandwith usage for the microframe
1746 * having index "slot" by "len" bytes. "len" can be negative. If the
1747 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1748 * the "slot" argument will be replaced by the slot having least used
1749 * bandwidth. The "mask" argument is used for multi-slot allocations.
1752 * The slot in which the bandwidth update was done: 0..7
1753 *------------------------------------------------------------------------*/
1755 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1756 uint8_t slot, uint8_t mask)
1758 struct usb_bus *bus = udev->bus;
1759 struct usb_hub *hub;
1760 enum usb_dev_speed speed;
1763 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1765 speed = usbd_get_speed(udev);
1769 case USB_SPEED_FULL:
1770 if (speed == USB_SPEED_LOW) {
1774 * The Host Controller Driver should have
1775 * performed checks so that the lookup
1776 * below does not result in a NULL pointer
1780 hub = udev->parent_hs_hub->hub;
1781 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1782 slot = usb_intr_find_best_slot(hub->uframe_usage,
1783 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1785 for (x = slot; x < 8; x++) {
1786 if (mask & (1U << (x - slot))) {
1787 hub->uframe_usage[x] += len;
1788 bus->uframe_usage[x] += len;
1793 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1794 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1795 USB_HS_MICRO_FRAMES_MAX, mask);
1797 for (x = slot; x < 8; x++) {
1798 if (mask & (1U << (x - slot))) {
1799 bus->uframe_usage[x] += len;
1807 /*------------------------------------------------------------------------*
1808 * usb_hs_bandwidth_alloc
1810 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1811 *------------------------------------------------------------------------*/
1813 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1815 struct usb_device *udev;
1820 udev = xfer->xroot->udev;
1822 if (udev->flags.usb_mode != USB_MODE_HOST)
1823 return; /* not supported */
1825 xfer->endpoint->refcount_bw++;
1826 if (xfer->endpoint->refcount_bw != 1)
1827 return; /* already allocated */
1829 speed = usbd_get_speed(udev);
1831 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1833 /* allocate a microframe slot */
1836 slot = usb_hs_bandwidth_adjust(udev,
1837 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1839 xfer->endpoint->usb_uframe = slot;
1840 xfer->endpoint->usb_smask = mask << slot;
1842 if ((speed != USB_SPEED_FULL) &&
1843 (speed != USB_SPEED_LOW)) {
1844 xfer->endpoint->usb_cmask = 0x00 ;
1846 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1850 case UE_ISOCHRONOUS:
1851 switch (usbd_xfer_get_fps_shift(xfer)) {
1866 /* allocate a microframe multi-slot */
1868 slot = usb_hs_bandwidth_adjust(udev,
1869 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1871 xfer->endpoint->usb_uframe = slot;
1872 xfer->endpoint->usb_cmask = 0;
1873 xfer->endpoint->usb_smask = mask << slot;
1877 xfer->endpoint->usb_uframe = 0;
1878 xfer->endpoint->usb_cmask = 0;
1879 xfer->endpoint->usb_smask = 0;
1883 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1884 xfer->endpoint->usb_uframe,
1885 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1888 /*------------------------------------------------------------------------*
1889 * usb_hs_bandwidth_free
1891 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1892 *------------------------------------------------------------------------*/
1894 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1896 struct usb_device *udev;
1900 udev = xfer->xroot->udev;
1902 if (udev->flags.usb_mode != USB_MODE_HOST)
1903 return; /* not supported */
1905 xfer->endpoint->refcount_bw--;
1906 if (xfer->endpoint->refcount_bw != 0)
1907 return; /* still allocated */
1909 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1911 case UE_ISOCHRONOUS:
1913 slot = xfer->endpoint->usb_uframe;
1914 mask = xfer->endpoint->usb_smask;
1916 /* free microframe slot(s): */
1917 usb_hs_bandwidth_adjust(udev,
1918 -xfer->max_frame_size, slot, mask >> slot);
1920 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1921 slot, mask >> slot);
1923 xfer->endpoint->usb_uframe = 0;
1924 xfer->endpoint->usb_cmask = 0;
1925 xfer->endpoint->usb_smask = 0;
1933 /*------------------------------------------------------------------------*
1934 * usb_isoc_time_expand
1936 * This function will expand the time counter from 7-bit to 16-bit.
1939 * 16-bit isochronous time counter.
1940 *------------------------------------------------------------------------*/
1942 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1946 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1948 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1950 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1952 if (isoc_time_curr < rem) {
1953 /* the time counter wrapped around */
1954 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1956 /* update the remainder */
1958 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1959 bus->isoc_time_last |= isoc_time_curr;
1961 return (bus->isoc_time_last);
1964 /*------------------------------------------------------------------------*
1965 * usbd_fs_isoc_schedule_alloc_slot
1967 * This function will allocate bandwidth for an isochronous FULL speed
1968 * transaction in the FULL speed schedule.
1973 *------------------------------------------------------------------------*/
1974 #if USB_HAVE_TT_SUPPORT
1976 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1978 struct usb_xfer *xfer;
1979 struct usb_xfer *pipe_xfer;
1980 struct usb_bus *bus;
1982 usb_frlength_t data_len;
1990 bus = isoc_xfer->xroot->bus;
1992 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1994 /* skip self, if any */
1996 if (xfer == isoc_xfer)
1999 /* check if this USB transfer is going through the same TT */
2001 if (xfer->xroot->udev->parent_hs_hub !=
2002 isoc_xfer->xroot->udev->parent_hs_hub) {
2005 if ((isoc_xfer->xroot->udev->parent_hs_hub->
2006 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2007 (xfer->xroot->udev->hs_port_no !=
2008 isoc_xfer->xroot->udev->hs_port_no)) {
2011 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2014 /* check if isoc_time is part of this transfer */
2016 delta = xfer->isoc_time_complete - isoc_time;
2017 if (delta > 0 && delta <= xfer->nframes) {
2018 delta = xfer->nframes - delta;
2020 len = xfer->frlengths[delta];
2029 * Check double buffered transfers. Only stream ID
2030 * equal to zero is valid here!
2032 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2035 /* skip self, if any */
2037 if (pipe_xfer == isoc_xfer)
2040 /* check if isoc_time is part of this transfer */
2042 delta = pipe_xfer->isoc_time_complete - isoc_time;
2043 if (delta > 0 && delta <= pipe_xfer->nframes) {
2044 delta = pipe_xfer->nframes - delta;
2046 len = pipe_xfer->frlengths[delta];
2056 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2057 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2061 /* check for overflow */
2063 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2068 delta = isoc_xfer->isoc_time_complete - isoc_time;
2069 if (delta > 0 && delta <= isoc_xfer->nframes) {
2070 delta = isoc_xfer->nframes - delta;
2072 len = isoc_xfer->frlengths[delta];
2080 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2081 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2085 /* check for overflow */
2087 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2094 /*------------------------------------------------------------------------*
2095 * usb_bus_port_get_device
2097 * This function is NULL safe.
2098 *------------------------------------------------------------------------*/
2100 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2102 if ((bus == NULL) || (up == NULL)) {
2106 if (up->device_index == 0) {
2110 return (bus->devices[up->device_index]);
2113 /*------------------------------------------------------------------------*
2114 * usb_bus_port_set_device
2116 * This function is NULL safe.
2117 *------------------------------------------------------------------------*/
2119 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2120 struct usb_device *udev, uint8_t device_index)
2127 * There is only one case where we don't
2128 * have an USB port, and that is the Root Hub!
2132 up->device_index = device_index;
2134 device_index = up->device_index;
2135 up->device_index = 0;
2139 * Make relationships to our new device
2141 if (device_index != 0) {
2143 mtx_lock(&usb_ref_lock);
2145 bus->devices[device_index] = udev;
2147 mtx_unlock(&usb_ref_lock);
2153 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2156 /*------------------------------------------------------------------------*
2159 * This functions is called when the USB event thread needs to run.
2160 *------------------------------------------------------------------------*/
2162 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2169 DPRINTF("No bus pointer!\n");
2172 if ((bus->devices == NULL) ||
2173 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2174 DPRINTF("No root HUB\n");
2177 if (mtx_owned(&bus->bus_mtx)) {
2186 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2187 &bus->explore_msg[0], &bus->explore_msg[1])) {
2191 USB_BUS_UNLOCK(bus);
2195 /*------------------------------------------------------------------------*
2196 * usb_needs_explore_all
2198 * This function is called whenever a new driver is loaded and will
2199 * cause that all USB busses are re-explored.
2200 *------------------------------------------------------------------------*/
2202 usb_needs_explore_all(void)
2204 struct usb_bus *bus;
2211 dc = usb_devclass_ptr;
2213 DPRINTFN(0, "no devclass\n");
2217 * Explore all USB busses in parallell.
2219 max = devclass_get_maxunit(dc);
2221 dev = devclass_get_device(dc, max);
2223 bus = device_get_softc(dev);
2225 usb_needs_explore(bus, 1);
2232 /*------------------------------------------------------------------------*
2233 * usb_bus_power_update
2235 * This function will ensure that all USB devices on the given bus are
2236 * properly suspended or resumed according to the device transfer
2238 *------------------------------------------------------------------------*/
2241 usb_bus_power_update(struct usb_bus *bus)
2243 usb_needs_explore(bus, 0 /* no probe */ );
2247 /*------------------------------------------------------------------------*
2248 * usbd_transfer_power_ref
2250 * This function will modify the power save reference counts and
2251 * wakeup the USB device associated with the given USB transfer, if
2253 *------------------------------------------------------------------------*/
2256 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2258 static const usb_power_mask_t power_mask[4] = {
2259 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2260 [UE_BULK] = USB_HW_POWER_BULK,
2261 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2262 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2264 struct usb_device *udev;
2265 uint8_t needs_explore;
2266 uint8_t needs_hw_power;
2269 udev = xfer->xroot->udev;
2271 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2272 /* no power save for root HUB */
2275 USB_BUS_LOCK(udev->bus);
2277 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2279 udev->pwr_save.last_xfer_time = ticks;
2280 udev->pwr_save.type_refs[xfer_type] += val;
2282 if (xfer->flags_int.control_xfr) {
2283 udev->pwr_save.read_refs += val;
2284 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2286 * It is not allowed to suspend during a
2289 udev->pwr_save.write_refs += val;
2291 } else if (USB_GET_DATA_ISREAD(xfer)) {
2292 udev->pwr_save.read_refs += val;
2294 udev->pwr_save.write_refs += val;
2298 if (udev->flags.self_suspended)
2299 needs_explore = usb_peer_should_wakeup(udev);
2303 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2304 DPRINTF("Adding type %u to power state\n", xfer_type);
2305 udev->bus->hw_power_state |= power_mask[xfer_type];
2315 USB_BUS_UNLOCK(udev->bus);
2317 if (needs_explore) {
2318 DPRINTF("update\n");
2319 usb_bus_power_update(udev->bus);
2320 } else if (needs_hw_power) {
2321 DPRINTF("needs power\n");
2322 if (udev->bus->methods->set_hw_power != NULL) {
2323 (udev->bus->methods->set_hw_power) (udev->bus);
2329 /*------------------------------------------------------------------------*
2330 * usb_peer_should_wakeup
2332 * This function returns non-zero if the current device should wake up.
2333 *------------------------------------------------------------------------*/
2335 usb_peer_should_wakeup(struct usb_device *udev)
2337 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2338 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2339 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2340 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2341 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2342 (udev->pwr_save.write_refs != 0) ||
2343 ((udev->pwr_save.read_refs != 0) &&
2344 (udev->flags.usb_mode == USB_MODE_HOST) &&
2345 (usb_peer_can_wakeup(udev) == 0)));
2348 /*------------------------------------------------------------------------*
2351 * This function implements the USB power daemon and is called
2352 * regularly from the USB explore thread.
2353 *------------------------------------------------------------------------*/
2356 usb_bus_powerd(struct usb_bus *bus)
2358 struct usb_device *udev;
2361 usb_ticks_t mintime;
2362 usb_size_t type_refs[5];
2365 limit = usb_power_timeout;
2368 else if (limit > 255)
2373 DPRINTF("bus=%p\n", bus);
2378 * The root HUB device is never suspended
2379 * and we simply skip it.
2381 for (x = USB_ROOT_HUB_ADDR + 1;
2382 x != bus->devices_max; x++) {
2384 udev = bus->devices[x];
2388 temp = ticks - udev->pwr_save.last_xfer_time;
2390 if (usb_peer_should_wakeup(udev)) {
2391 /* check if we are suspended */
2392 if (udev->flags.self_suspended != 0) {
2393 USB_BUS_UNLOCK(bus);
2394 usb_dev_resume_peer(udev);
2397 } else if ((temp >= limit) &&
2398 (udev->flags.usb_mode == USB_MODE_HOST) &&
2399 (udev->flags.self_suspended == 0)) {
2400 /* try to do suspend */
2402 USB_BUS_UNLOCK(bus);
2403 usb_dev_suspend_peer(udev);
2408 /* reset counters */
2410 mintime = (usb_ticks_t)-1;
2417 /* Re-loop all the devices to get the actual state */
2419 for (x = USB_ROOT_HUB_ADDR + 1;
2420 x != bus->devices_max; x++) {
2422 udev = bus->devices[x];
2426 /* we found a non-Root-Hub USB device */
2429 /* "last_xfer_time" can be updated by a resume */
2430 temp = ticks - udev->pwr_save.last_xfer_time;
2433 * Compute minimum time since last transfer for the complete
2439 if (udev->flags.self_suspended == 0) {
2440 type_refs[0] += udev->pwr_save.type_refs[0];
2441 type_refs[1] += udev->pwr_save.type_refs[1];
2442 type_refs[2] += udev->pwr_save.type_refs[2];
2443 type_refs[3] += udev->pwr_save.type_refs[3];
2447 if (mintime >= (usb_ticks_t)(1 * hz)) {
2448 /* recompute power masks */
2449 DPRINTF("Recomputing power masks\n");
2450 bus->hw_power_state = 0;
2451 if (type_refs[UE_CONTROL] != 0)
2452 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2453 if (type_refs[UE_BULK] != 0)
2454 bus->hw_power_state |= USB_HW_POWER_BULK;
2455 if (type_refs[UE_INTERRUPT] != 0)
2456 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2457 if (type_refs[UE_ISOCHRONOUS] != 0)
2458 bus->hw_power_state |= USB_HW_POWER_ISOC;
2459 if (type_refs[4] != 0)
2460 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2462 USB_BUS_UNLOCK(bus);
2464 if (bus->methods->set_hw_power != NULL) {
2465 /* always update hardware power! */
2466 (bus->methods->set_hw_power) (bus);
2472 /*------------------------------------------------------------------------*
2473 * usb_dev_resume_peer
2475 * This function will resume an USB peer and do the required USB
2476 * signalling to get an USB device out of the suspended state.
2477 *------------------------------------------------------------------------*/
2479 usb_dev_resume_peer(struct usb_device *udev)
2481 struct usb_bus *bus;
2488 /* check if already resumed */
2489 if (udev->flags.self_suspended == 0)
2492 /* we need a parent HUB to do resume */
2493 if (udev->parent_hub == NULL)
2496 DPRINTF("udev=%p\n", udev);
2498 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2499 (udev->flags.remote_wakeup == 0)) {
2501 * If the host did not set the remote wakeup feature, we can
2502 * not wake it up either!
2504 DPRINTF("remote wakeup is not set!\n");
2507 /* get bus pointer */
2510 /* resume parent hub first */
2511 usb_dev_resume_peer(udev->parent_hub);
2513 /* reduce chance of instant resume failure by waiting a little bit */
2514 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2516 if (usb_device_20_compatible(udev)) {
2517 /* resume current port (Valid in Host and Device Mode) */
2518 err = usbd_req_clear_port_feature(udev->parent_hub,
2519 NULL, udev->port_no, UHF_PORT_SUSPEND);
2521 DPRINTFN(0, "Resuming port failed\n");
2525 /* resume current port (Valid in Host and Device Mode) */
2526 err = usbd_req_set_port_link_state(udev->parent_hub,
2527 NULL, udev->port_no, UPS_PORT_LS_U0);
2529 DPRINTFN(0, "Resuming port failed\n");
2534 /* resume settle time */
2535 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2537 if (bus->methods->device_resume != NULL) {
2538 /* resume USB device on the USB controller */
2539 (bus->methods->device_resume) (udev);
2542 /* set that this device is now resumed */
2543 udev->flags.self_suspended = 0;
2545 /* make sure that we don't go into suspend right away */
2546 udev->pwr_save.last_xfer_time = ticks;
2548 /* make sure the needed power masks are on */
2549 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2550 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2551 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2552 bus->hw_power_state |= USB_HW_POWER_BULK;
2553 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2554 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2555 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2556 bus->hw_power_state |= USB_HW_POWER_ISOC;
2558 USB_BUS_UNLOCK(bus);
2560 if (bus->methods->set_hw_power != NULL) {
2561 /* always update hardware power! */
2562 (bus->methods->set_hw_power) (bus);
2567 /* notify all sub-devices about resume */
2568 err = usb_suspend_resume(udev, 0);
2570 usbd_sr_unlock(udev);
2572 /* check if peer has wakeup capability */
2573 if (usb_peer_can_wakeup(udev)) {
2574 /* clear remote wakeup */
2575 err = usbd_req_clear_device_feature(udev,
2576 NULL, UF_DEVICE_REMOTE_WAKEUP);
2578 DPRINTFN(0, "Clearing device "
2579 "remote wakeup failed: %s\n",
2585 /*------------------------------------------------------------------------*
2586 * usb_dev_suspend_peer
2588 * This function will suspend an USB peer and do the required USB
2589 * signalling to get an USB device into the suspended state.
2590 *------------------------------------------------------------------------*/
2592 usb_dev_suspend_peer(struct usb_device *udev)
2594 struct usb_device *child;
2604 /* check if already suspended */
2605 if (udev->flags.self_suspended)
2608 /* we need a parent HUB to do suspend */
2609 if (udev->parent_hub == NULL)
2612 DPRINTF("udev=%p\n", udev);
2614 /* check if the current device is a HUB */
2615 if (udev->hub != NULL) {
2616 nports = udev->hub->nports;
2618 /* check if all devices on the HUB are suspended */
2619 for (x = 0; x != nports; x++) {
2620 child = usb_bus_port_get_device(udev->bus,
2621 udev->hub->ports + x);
2626 if (child->flags.self_suspended)
2629 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2634 if (usb_peer_can_wakeup(udev)) {
2636 * This request needs to be done before we set
2637 * "udev->flags.self_suspended":
2640 /* allow device to do remote wakeup */
2641 err = usbd_req_set_device_feature(udev,
2642 NULL, UF_DEVICE_REMOTE_WAKEUP);
2644 DPRINTFN(0, "Setting device "
2645 "remote wakeup failed\n");
2649 USB_BUS_LOCK(udev->bus);
2651 * Checking for suspend condition and setting suspended bit
2654 err = usb_peer_should_wakeup(udev);
2657 * Set that this device is suspended. This variable
2658 * must be set before calling USB controller suspend
2661 udev->flags.self_suspended = 1;
2663 USB_BUS_UNLOCK(udev->bus);
2666 if (usb_peer_can_wakeup(udev)) {
2667 /* allow device to do remote wakeup */
2668 err = usbd_req_clear_device_feature(udev,
2669 NULL, UF_DEVICE_REMOTE_WAKEUP);
2671 DPRINTFN(0, "Setting device "
2672 "remote wakeup failed\n");
2676 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2677 /* resume parent HUB first */
2678 usb_dev_resume_peer(udev->parent_hub);
2680 /* reduce chance of instant resume failure by waiting a little bit */
2681 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2683 /* resume current port (Valid in Host and Device Mode) */
2684 err = usbd_req_clear_port_feature(udev->parent_hub,
2685 NULL, udev->port_no, UHF_PORT_SUSPEND);
2687 /* resume settle time */
2688 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2690 DPRINTF("Suspend was cancelled!\n");
2696 /* notify all sub-devices about suspend */
2697 err = usb_suspend_resume(udev, 1);
2699 usbd_sr_unlock(udev);
2701 if (udev->bus->methods->device_suspend != NULL) {
2704 /* suspend device on the USB controller */
2705 (udev->bus->methods->device_suspend) (udev);
2708 temp = usbd_get_dma_delay(udev);
2710 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2714 if (usb_device_20_compatible(udev)) {
2715 /* suspend current port */
2716 err = usbd_req_set_port_feature(udev->parent_hub,
2717 NULL, udev->port_no, UHF_PORT_SUSPEND);
2719 DPRINTFN(0, "Suspending port failed\n");
2723 /* suspend current port */
2724 err = usbd_req_set_port_link_state(udev->parent_hub,
2725 NULL, udev->port_no, UPS_PORT_LS_U3);
2727 DPRINTFN(0, "Suspending port failed\n");
2732 udev = udev->parent_hub;
2736 /*------------------------------------------------------------------------*
2737 * usbd_set_power_mode
2739 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2741 *------------------------------------------------------------------------*/
2743 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2745 /* filter input argument */
2746 if ((power_mode != USB_POWER_MODE_ON) &&
2747 (power_mode != USB_POWER_MODE_OFF))
2748 power_mode = USB_POWER_MODE_SAVE;
2750 power_mode = usbd_filter_power_mode(udev, power_mode);
2752 udev->power_mode = power_mode; /* update copy of power mode */
2755 usb_bus_power_update(udev->bus);
2757 usb_needs_explore(udev->bus, 0 /* no probe */ );
2761 /*------------------------------------------------------------------------*
2762 * usbd_filter_power_mode
2764 * This function filters the power mode based on hardware requirements.
2765 *------------------------------------------------------------------------*/
2767 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2769 struct usb_bus_methods *mtod;
2772 mtod = udev->bus->methods;
2775 if (mtod->get_power_mode != NULL)
2776 (mtod->get_power_mode) (udev, &temp);
2778 /* check if we should not filter */
2780 return (power_mode);
2782 /* use fixed power mode given by hardware driver */
2786 /*------------------------------------------------------------------------*
2787 * usbd_start_re_enumerate
2789 * This function starts re-enumeration of the given USB device. This
2790 * function does not need to be called BUS-locked. This function does
2791 * not wait until the re-enumeration is completed.
2792 *------------------------------------------------------------------------*/
2794 usbd_start_re_enumerate(struct usb_device *udev)
2796 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2797 udev->re_enumerate_wait = USB_RE_ENUM_START;
2798 usb_needs_explore(udev->bus, 0);