3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR uhub_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_request.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_hub.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_controller.h>
70 #include <dev/usb/usb_bus.h>
72 #define UHUB_INTR_INTERVAL 250 /* ms */
75 #if USB_HAVE_TT_SUPPORT
76 UHUB_RESET_TT_TRANSFER,
82 static int uhub_debug = 0;
84 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
85 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
87 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
91 static int usb_power_timeout = 30; /* seconds */
93 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
94 &usb_power_timeout, 0, "USB power timeout");
97 struct uhub_current_state {
103 struct uhub_current_state sc_st;/* current state */
104 device_t sc_dev; /* base device */
105 struct mtx sc_mtx; /* our mutex */
106 struct usb_device *sc_udev; /* USB device */
107 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
109 #define UHUB_FLAG_DID_EXPLORE 0x01
113 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
114 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
115 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
116 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
117 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
119 /* prototypes for type checking: */
121 static device_probe_t uhub_probe;
122 static device_attach_t uhub_attach;
123 static device_detach_t uhub_detach;
124 static device_suspend_t uhub_suspend;
125 static device_resume_t uhub_resume;
127 static bus_driver_added_t uhub_driver_added;
128 static bus_child_location_str_t uhub_child_location_string;
129 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
131 static usb_callback_t uhub_intr_callback;
132 #if USB_HAVE_TT_SUPPORT
133 static usb_callback_t uhub_reset_tt_callback;
136 static void usb_dev_resume_peer(struct usb_device *udev);
137 static void usb_dev_suspend_peer(struct usb_device *udev);
138 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
140 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
142 [UHUB_INTR_TRANSFER] = {
143 .type = UE_INTERRUPT,
144 .endpoint = UE_ADDR_ANY,
145 .direction = UE_DIR_ANY,
147 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
148 .bufsize = 0, /* use wMaxPacketSize */
149 .callback = &uhub_intr_callback,
150 .interval = UHUB_INTR_INTERVAL,
152 #if USB_HAVE_TT_SUPPORT
153 [UHUB_RESET_TT_TRANSFER] = {
155 .endpoint = 0x00, /* Control pipe */
156 .direction = UE_DIR_ANY,
157 .bufsize = sizeof(struct usb_device_request),
158 .callback = &uhub_reset_tt_callback,
159 .timeout = 1000, /* 1 second */
160 .usb_mode = USB_MODE_HOST,
166 * driver instance for "hub" connected to "usb"
167 * and "hub" connected to "hub"
169 static devclass_t uhub_devclass;
171 static device_method_t uhub_methods[] = {
172 DEVMETHOD(device_probe, uhub_probe),
173 DEVMETHOD(device_attach, uhub_attach),
174 DEVMETHOD(device_detach, uhub_detach),
176 DEVMETHOD(device_suspend, uhub_suspend),
177 DEVMETHOD(device_resume, uhub_resume),
179 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
180 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
181 DEVMETHOD(bus_driver_added, uhub_driver_added),
185 static driver_t uhub_driver = {
187 .methods = uhub_methods,
188 .size = sizeof(struct uhub_softc)
191 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
192 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
193 MODULE_VERSION(uhub, 1);
196 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
198 struct uhub_softc *sc = usbd_xfer_softc(xfer);
200 switch (USB_GET_STATE(xfer)) {
201 case USB_ST_TRANSFERRED:
204 * This is an indication that some port
205 * has changed status. Notify the bus
206 * event handler thread that we need
207 * to be explored again:
209 usb_needs_explore(sc->sc_udev->bus, 0);
212 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
213 usbd_transfer_submit(xfer);
217 if (xfer->error != USB_ERR_CANCELLED) {
219 * Do a clear-stall. The "stall_pipe" flag
220 * will get cleared before next callback by
223 usbd_xfer_set_stall(xfer);
224 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
225 usbd_transfer_submit(xfer);
231 /*------------------------------------------------------------------------*
234 * This function starts the TT reset USB request
235 *------------------------------------------------------------------------*/
236 #if USB_HAVE_TT_SUPPORT
238 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
240 struct usb_udev_msg *pm = (void *)_pm;
241 struct usb_device *udev = pm->udev;
243 struct uhub_softc *sc;
253 USB_BUS_UNLOCK(udev->bus);
254 mtx_lock(&sc->sc_mtx);
256 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
258 mtx_unlock(&sc->sc_mtx);
259 USB_BUS_LOCK(udev->bus);
263 /*------------------------------------------------------------------------*
264 * uhub_tt_buffer_reset_async_locked
266 * This function queues a TT reset for the given USB device and endpoint.
267 *------------------------------------------------------------------------*/
268 #if USB_HAVE_TT_SUPPORT
270 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
272 struct usb_device_request req;
273 struct usb_device *udev;
279 if (child == NULL || ep == NULL)
282 udev = child->parent_hs_hub;
283 port = child->hs_port_no;
290 (udev->speed != USB_SPEED_HIGH) ||
291 (child->speed != USB_SPEED_LOW &&
292 child->speed != USB_SPEED_FULL) ||
293 (child->flags.usb_mode != USB_MODE_HOST) ||
294 (port == 0) || (ep->edesc == NULL)) {
299 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
301 up = hub->ports + port - 1;
303 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
304 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
307 /* if we already received a clear buffer request, reset the whole TT */
308 if (up->req_reset_tt.bRequest != 0) {
309 req.bmRequestType = UT_WRITE_CLASS_OTHER;
310 req.bRequest = UR_RESET_TT;
311 USETW(req.wValue, 0);
312 req.wIndex[0] = port;
314 USETW(req.wLength, 0);
316 wValue = (ep->edesc->bEndpointAddress & 0xF) |
317 ((child->address & 0x7F) << 4) |
318 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
319 ((ep->edesc->bmAttributes & 3) << 12);
321 req.bmRequestType = UT_WRITE_CLASS_OTHER;
322 req.bRequest = UR_CLEAR_TT_BUFFER;
323 USETW(req.wValue, wValue);
324 req.wIndex[0] = port;
326 USETW(req.wLength, 0);
328 up->req_reset_tt = req;
329 /* get reset transfer started */
330 usb_proc_msignal(&udev->bus->non_giant_callback_proc,
331 &hub->tt_msg[0], &hub->tt_msg[1]);
335 #if USB_HAVE_TT_SUPPORT
337 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
339 struct uhub_softc *sc;
340 struct usb_device *udev;
344 DPRINTF("TT buffer reset\n");
346 sc = usbd_xfer_softc(xfer);
349 switch (USB_GET_STATE(xfer)) {
350 case USB_ST_TRANSFERRED:
353 USB_BUS_LOCK(udev->bus);
354 /* find first port which needs a TT reset */
355 for (x = 0; x != udev->hub->nports; x++) {
356 up = udev->hub->ports + x;
358 if (up->req_reset_tt.bRequest == 0)
361 /* copy in the transfer */
362 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
363 sizeof(up->req_reset_tt));
365 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
368 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
370 USB_BUS_UNLOCK(udev->bus);
372 usbd_transfer_submit(xfer);
375 USB_BUS_UNLOCK(udev->bus);
379 if (error == USB_ERR_CANCELLED)
382 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
388 /*------------------------------------------------------------------------*
389 * uhub_count_active_host_ports
391 * This function counts the number of active ports at the given speed.
392 *------------------------------------------------------------------------*/
394 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
396 struct uhub_softc *sc;
397 struct usb_device *child;
412 for (x = 0; x != hub->nports; x++) {
414 child = usb_bus_port_get_device(udev->bus, up);
416 child->flags.usb_mode == USB_MODE_HOST &&
417 child->speed == speed)
424 /*------------------------------------------------------------------------*
425 * uhub_explore_sub - subroutine
429 * Else: A control transaction failed
430 *------------------------------------------------------------------------*/
432 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
435 struct usb_device *child;
439 bus = sc->sc_udev->bus;
442 /* get driver added refcount from USB bus */
443 refcount = bus->driver_added_refcount;
445 /* get device assosiated with the given port */
446 child = usb_bus_port_get_device(bus, up);
452 /* check if device should be re-enumerated */
454 if (child->flags.usb_mode == USB_MODE_HOST) {
457 do_unlock = usbd_enum_lock(child);
458 switch (child->re_enumerate_wait) {
459 case USB_RE_ENUM_START:
460 err = usbd_set_config_index(child,
463 DPRINTF("Unconfigure failed: "
467 err = usbd_req_re_enumerate(child, NULL);
469 err = usbd_set_config_index(child, 0);
471 err = usb_probe_and_attach(child,
472 USB_IFACE_INDEX_ANY);
474 child->re_enumerate_wait = USB_RE_ENUM_DONE;
478 case USB_RE_ENUM_PWR_OFF:
479 /* get the device unconfigured */
480 err = usbd_set_config_index(child,
483 DPRINTFN(0, "Could not unconfigure "
484 "device (ignored)\n");
487 /* clear port enable */
488 err = usbd_req_clear_port_feature(child->parent_hub,
489 NULL, child->port_no, UHF_PORT_ENABLE);
491 DPRINTFN(0, "Could not disable port "
494 child->re_enumerate_wait = USB_RE_ENUM_DONE;
499 child->re_enumerate_wait = USB_RE_ENUM_DONE;
503 usbd_enum_unlock(child);
506 /* check if probe and attach should be done */
508 if (child->driver_added_refcount != refcount) {
509 child->driver_added_refcount = refcount;
510 err = usb_probe_and_attach(child,
511 USB_IFACE_INDEX_ANY);
516 /* start control transfer, if device mode */
518 if (child->flags.usb_mode == USB_MODE_DEVICE)
519 usbd_ctrl_transfer_setup(child);
521 /* if a HUB becomes present, do a recursive HUB explore */
524 err = (child->hub->explore) (child);
530 /*------------------------------------------------------------------------*
531 * uhub_read_port_status - factored out code
532 *------------------------------------------------------------------------*/
534 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
536 struct usb_port_status ps;
539 err = usbd_req_get_port_status(
540 sc->sc_udev, NULL, &ps, portno);
542 /* update status regardless of error */
544 sc->sc_st.port_status = UGETW(ps.wPortStatus);
545 sc->sc_st.port_change = UGETW(ps.wPortChange);
547 /* debugging print */
549 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
550 "wPortChange=0x%04x, err=%s\n",
551 portno, sc->sc_st.port_status,
552 sc->sc_st.port_change, usbd_errstr(err));
556 /*------------------------------------------------------------------------*
561 * Else: A control transaction failed
562 *------------------------------------------------------------------------*/
564 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
566 struct usb_device *child;
567 struct usb_device *udev;
568 enum usb_dev_speed speed;
569 enum usb_hc_mode mode;
574 DPRINTF("reattaching port %d\n", portno);
579 child = usb_bus_port_get_device(udev->bus,
580 udev->hub->ports + portno - 1);
584 /* first clear the port connection change bit */
586 err = usbd_req_clear_port_feature(udev, NULL,
587 portno, UHF_C_PORT_CONNECTION);
592 /* check if there is a child */
596 * Free USB device and all subdevices, if any.
598 usb_free_device(child, 0);
601 /* get fresh status */
603 err = uhub_read_port_status(sc, portno);
607 /* check if nothing is connected to the port */
609 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) {
612 /* check if there is no power on the port and print a warning */
614 switch (udev->speed) {
618 power_mask = UPS_PORT_POWER;
620 case USB_SPEED_SUPER:
621 if (udev->parent_hub == NULL)
622 power_mask = UPS_PORT_POWER;
624 power_mask = UPS_PORT_POWER_SS;
630 if (!(sc->sc_st.port_status & power_mask)) {
631 DPRINTF("WARNING: strange, connected port %d "
632 "has no power\n", portno);
635 /* check if the device is in Host Mode */
637 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
639 DPRINTF("Port %d is in Host Mode\n", portno);
641 if (sc->sc_st.port_status & UPS_SUSPEND) {
643 * NOTE: Should not get here in SuperSpeed
644 * mode, because the HUB should report this
647 DPRINTF("Port %d was still "
648 "suspended, clearing.\n", portno);
649 err = usbd_req_clear_port_feature(udev,
650 NULL, portno, UHF_PORT_SUSPEND);
655 /* wait for maximum device power up time */
658 USB_MS_TO_TICKS(usb_port_powerup_delay));
660 /* reset port, which implies enabling it */
662 err = usbd_req_reset_port(udev, NULL, portno);
665 DPRINTFN(0, "port %d reset "
666 "failed, error=%s\n",
667 portno, usbd_errstr(err));
670 /* get port status again, it might have changed during reset */
672 err = uhub_read_port_status(sc, portno);
676 /* check if something changed during port reset */
678 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
679 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
681 DPRINTFN(0, "giving up port reset "
682 "- device vanished\n");
689 DPRINTF("Port %d is in Device Mode\n", portno);
693 * Figure out the device speed
695 switch (udev->speed) {
697 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
698 speed = USB_SPEED_HIGH;
699 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
700 speed = USB_SPEED_LOW;
702 speed = USB_SPEED_FULL;
705 if (sc->sc_st.port_status & UPS_LOW_SPEED)
706 speed = USB_SPEED_LOW;
708 speed = USB_SPEED_FULL;
711 speed = USB_SPEED_LOW;
713 case USB_SPEED_SUPER:
714 if (udev->parent_hub == NULL) {
715 /* Root HUB - special case */
716 switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
718 speed = USB_SPEED_FULL;
721 speed = USB_SPEED_LOW;
724 speed = USB_SPEED_HIGH;
727 speed = USB_SPEED_SUPER;
731 speed = USB_SPEED_SUPER;
735 /* same speed like parent */
739 if (speed == USB_SPEED_SUPER) {
740 err = usbd_req_set_hub_u1_timeout(udev, NULL,
741 portno, 128 - (2 * udev->depth));
743 DPRINTFN(0, "port %d U1 timeout "
744 "failed, error=%s\n",
745 portno, usbd_errstr(err));
747 err = usbd_req_set_hub_u2_timeout(udev, NULL,
748 portno, 128 - (2 * udev->depth));
750 DPRINTFN(0, "port %d U2 timeout "
751 "failed, error=%s\n",
752 portno, usbd_errstr(err));
757 * Figure out the device mode
759 * NOTE: This part is currently FreeBSD specific.
761 if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
762 mode = USB_MODE_DEVICE;
764 mode = USB_MODE_HOST;
766 /* need to create a new child */
767 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
768 udev->depth + 1, portno - 1, portno, speed, mode);
770 DPRINTFN(0, "could not allocate new device\n");
773 return (0); /* success */
778 * Free USB device and all subdevices, if any.
780 usb_free_device(child, 0);
784 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
785 err = usbd_req_clear_port_feature(
787 portno, UHF_PORT_ENABLE);
791 DPRINTFN(0, "device problem (%s), "
792 "disabling port %d\n", usbd_errstr(err), portno);
797 /*------------------------------------------------------------------------*
798 * usb_device_20_compatible
801 * 0: HUB does not support suspend and resume
802 * Else: HUB supports suspend and resume
803 *------------------------------------------------------------------------*/
805 usb_device_20_compatible(struct usb_device *udev)
809 switch (udev->speed) {
819 /*------------------------------------------------------------------------*
820 * uhub_suspend_resume_port
824 * Else: A control transaction failed
825 *------------------------------------------------------------------------*/
827 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
829 struct usb_device *child;
830 struct usb_device *udev;
834 DPRINTF("port %d\n", portno);
837 child = usb_bus_port_get_device(udev->bus,
838 udev->hub->ports + portno - 1);
840 /* first clear the port suspend change bit */
842 if (usb_device_20_compatible(udev)) {
843 err = usbd_req_clear_port_feature(udev, NULL,
844 portno, UHF_C_PORT_SUSPEND);
846 err = usbd_req_clear_port_feature(udev, NULL,
847 portno, UHF_C_PORT_LINK_STATE);
851 DPRINTF("clearing suspend failed.\n");
854 /* get fresh status */
856 err = uhub_read_port_status(sc, portno);
858 DPRINTF("reading port status failed.\n");
861 /* convert current state */
863 if (usb_device_20_compatible(udev)) {
864 if (sc->sc_st.port_status & UPS_SUSPEND) {
870 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
874 case UPS_PORT_LS_SS_INA:
875 usbd_req_warm_reset_port(udev, NULL, portno);
884 DPRINTF("suspended=%u\n", is_suspend);
886 /* do the suspend or resume */
890 * This code handle two cases: 1) Host Mode - we can only
891 * receive resume here 2) Device Mode - we can receive
892 * suspend and resume here
895 usb_dev_resume_peer(child);
896 else if (child->flags.usb_mode == USB_MODE_DEVICE)
897 usb_dev_suspend_peer(child);
903 /*------------------------------------------------------------------------*
904 * uhub_root_interrupt
906 * This function is called when a Root HUB interrupt has
907 * happened. "ptr" and "len" makes up the Root HUB interrupt
908 * packet. This function is called having the "bus_mtx" locked.
909 *------------------------------------------------------------------------*/
911 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
913 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
915 usb_needs_explore(bus, 0);
919 uhub_is_too_deep(struct usb_device *udev)
921 switch (udev->speed) {
925 if (udev->depth > USB_HUB_MAX_DEPTH)
928 case USB_SPEED_SUPER:
929 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
938 /*------------------------------------------------------------------------*
944 *------------------------------------------------------------------------*/
946 uhub_explore(struct usb_device *udev)
949 struct uhub_softc *sc;
959 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
961 /* ignore devices that are too deep */
962 if (uhub_is_too_deep(udev))
963 return (USB_ERR_TOO_DEEP);
965 /* check if device is suspended */
966 if (udev->flags.self_suspended) {
967 /* need to wait until the child signals resume */
968 DPRINTF("Device is suspended!\n");
973 * Make sure we don't race against user-space applications
976 do_unlock = usbd_enum_lock(udev);
978 for (x = 0; x != hub->nports; x++) {
982 err = uhub_read_port_status(sc, portno);
984 /* most likely the HUB is gone */
987 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
988 DPRINTF("Overcurrent on port %u.\n", portno);
989 err = usbd_req_clear_port_feature(
990 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
992 /* most likely the HUB is gone */
996 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
998 * Fake a connect status change so that the
999 * status gets checked initially!
1001 sc->sc_st.port_change |=
1002 UPS_C_CONNECT_STATUS;
1004 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1005 err = usbd_req_clear_port_feature(
1006 udev, NULL, portno, UHF_C_PORT_ENABLE);
1008 /* most likely the HUB is gone */
1011 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1013 * Ignore the port error if the device
1016 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1017 DPRINTFN(0, "illegal enable change, "
1018 "port %d\n", portno);
1021 if (up->restartcnt == USB_RESTART_MAX) {
1022 /* XXX could try another speed ? */
1023 DPRINTFN(0, "port error, giving up "
1024 "port %d\n", portno);
1026 sc->sc_st.port_change |=
1027 UPS_C_CONNECT_STATUS;
1032 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1033 err = uhub_reattach_port(sc, portno);
1035 /* most likely the HUB is gone */
1039 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1040 UPS_C_PORT_LINK_STATE)) {
1041 err = uhub_suspend_resume_port(sc, portno);
1043 /* most likely the HUB is gone */
1047 err = uhub_explore_sub(sc, up);
1049 /* no device(s) present */
1052 /* explore succeeded - reset restart counter */
1057 usbd_enum_unlock(udev);
1059 /* initial status checked */
1060 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1062 /* return success */
1063 return (USB_ERR_NORMAL_COMPLETION);
1067 uhub_probe(device_t dev)
1069 struct usb_attach_arg *uaa = device_get_ivars(dev);
1071 if (uaa->usb_mode != USB_MODE_HOST)
1075 * The subclass for USB HUBs is currently ignored because it
1076 * is 0 for some and 1 for others.
1078 if (uaa->info.bConfigIndex == 0 &&
1079 uaa->info.bDeviceClass == UDCLASS_HUB)
1085 /* NOTE: The information returned by this function can be wrong. */
1087 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1089 struct usb_hub_descriptor hubdesc20;
1090 struct usb_hub_ss_descriptor hubdesc30;
1095 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1096 return (USB_ERR_INVAL);
1101 switch (udev->speed) {
1103 case USB_SPEED_FULL:
1104 case USB_SPEED_HIGH:
1105 /* assuming that there is one port */
1106 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1108 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1109 "error=%s\n", usbd_errstr(err));
1112 nports = hubdesc20.bNbrPorts;
1116 if (udev->speed == USB_SPEED_HIGH)
1117 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1120 case USB_SPEED_SUPER:
1121 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1123 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1124 "error=%s\n", usbd_errstr(err));
1127 nports = hubdesc30.bNbrPorts;
1133 err = USB_ERR_INVAL;
1137 if (pnports != NULL)
1147 uhub_attach(device_t dev)
1149 struct uhub_softc *sc = device_get_softc(dev);
1150 struct usb_attach_arg *uaa = device_get_ivars(dev);
1151 struct usb_device *udev = uaa->device;
1152 struct usb_device *parent_hub = udev->parent_hub;
1153 struct usb_hub *hub;
1154 struct usb_hub_descriptor hubdesc20;
1155 struct usb_hub_ss_descriptor hubdesc30;
1161 uint8_t iface_index;
1167 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1169 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
1170 device_get_nameunit(dev));
1172 device_set_usb_desc(dev);
1174 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1175 "parent->selfpowered=%d\n",
1177 udev->flags.self_powered,
1180 parent_hub->flags.self_powered : 0);
1182 if (uhub_is_too_deep(udev)) {
1183 DPRINTFN(0, "HUB at depth %d, "
1184 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1188 if (!udev->flags.self_powered && parent_hub &&
1189 !parent_hub->flags.self_powered) {
1190 DPRINTFN(0, "Bus powered HUB connected to "
1191 "bus powered HUB. HUB ignored\n");
1195 if (UHUB_IS_MULTI_TT(sc)) {
1196 err = usbd_set_alt_interface_index(udev, 0, 1);
1198 device_printf(dev, "MTT could not be enabled\n");
1201 device_printf(dev, "MTT enabled\n");
1204 /* get HUB descriptor */
1206 DPRINTFN(2, "Getting HUB descriptor\n");
1208 switch (udev->speed) {
1210 case USB_SPEED_FULL:
1211 case USB_SPEED_HIGH:
1212 /* assuming that there is one port */
1213 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1215 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1216 "error=%s\n", usbd_errstr(err));
1219 /* get number of ports */
1220 nports = hubdesc20.bNbrPorts;
1222 /* get power delay */
1223 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1224 usb_extra_power_up_time);
1226 /* get complete HUB descriptor */
1228 /* check number of ports */
1230 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1231 "error=%s\n", usbd_errstr(err));
1234 /* get complete HUB descriptor */
1235 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1238 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1239 "error=%s\n", usbd_errstr(err));
1242 if (hubdesc20.bNbrPorts != nports) {
1243 DPRINTFN(0, "Number of ports changed\n");
1248 case USB_SPEED_SUPER:
1249 if (udev->parent_hub != NULL) {
1250 err = usbd_req_set_hub_depth(udev, NULL,
1253 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1254 "error=%s\n", usbd_errstr(err));
1258 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1260 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1261 "error=%s\n", usbd_errstr(err));
1264 /* get number of ports */
1265 nports = hubdesc30.bNbrPorts;
1267 /* get power delay */
1268 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1269 usb_extra_power_up_time);
1271 /* get complete HUB descriptor */
1273 /* check number of ports */
1274 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1275 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1276 "error=%s\n", usbd_errstr(err));
1279 /* get complete HUB descriptor */
1280 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1283 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1284 "error=%s\n", usbd_errstr(err));
1287 if (hubdesc30.bNbrPorts != nports) {
1288 DPRINTFN(0, "Number of ports changed\n");
1294 DPRINTF("Assuming HUB has only one port\n");
1295 /* default number of ports */
1297 /* default power delay */
1298 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1302 DPRINTFN(0, "portless HUB\n");
1305 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1306 M_USBDEV, M_WAITOK | M_ZERO);
1313 /* initialize HUB structure */
1315 hub->explore = &uhub_explore;
1316 hub->nports = nports;
1317 hub->hubudev = udev;
1318 #if USB_HAVE_TT_SUPPORT
1319 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1320 hub->tt_msg[0].udev = udev;
1321 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1322 hub->tt_msg[1].udev = udev;
1324 /* if self powered hub, give ports maximum current */
1325 if (udev->flags.self_powered) {
1326 hub->portpower = USB_MAX_POWER;
1328 hub->portpower = USB_MIN_POWER;
1331 /* set up interrupt pipe */
1333 if (udev->parent_hub == NULL) {
1334 /* root HUB is special */
1338 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1339 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1342 DPRINTFN(0, "cannot setup interrupt transfer, "
1343 "errstr=%s\n", usbd_errstr(err));
1346 /* wait with power off for a while */
1347 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1350 * To have the best chance of success we do things in the exact same
1351 * order as Windoze98. This should not be necessary, but some
1352 * devices do not follow the USB specs to the letter.
1354 * These are the events on the bus when a hub is attached:
1355 * Get device and config descriptors (see attach code)
1356 * Get hub descriptor (see above)
1359 * wait for power to become stable
1360 * (all below happens in explore code)
1362 * clear C_PORT_CONNECTION
1365 * if device connected
1369 * clear C_PORT_RESET
1371 * proceed with device attachment
1374 /* XXX should check for none, individual, or ganged power? */
1378 for (x = 0; x != nports; x++) {
1379 /* set up data structures */
1380 struct usb_port *up = hub->ports + x;
1382 up->device_index = 0;
1386 /* check if port is removable */
1387 switch (udev->speed) {
1389 case USB_SPEED_FULL:
1390 case USB_SPEED_HIGH:
1391 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1394 case USB_SPEED_SUPER:
1395 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1399 DPRINTF("Assuming removable port\n");
1404 /* turn the power on */
1405 err = usbd_req_set_port_feature(udev, NULL,
1406 portno, UHF_PORT_POWER);
1409 DPRINTFN(0, "port %d power on failed, %s\n",
1410 portno, usbd_errstr(err));
1412 DPRINTF("turn on port %d power\n",
1415 /* wait for stable power */
1416 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1419 device_printf(dev, "%d port%s with %d "
1420 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1421 removable, udev->flags.self_powered ? "self" : "bus");
1423 /* Start the interrupt endpoint, if any */
1425 mtx_lock(&sc->sc_mtx);
1426 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1427 mtx_unlock(&sc->sc_mtx);
1429 /* Enable automatic power save on all USB HUBs */
1431 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1436 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1439 free(udev->hub, M_USBDEV);
1443 mtx_destroy(&sc->sc_mtx);
1449 * Called from process context when the hub is gone.
1450 * Detach all devices on active ports.
1453 uhub_detach(device_t dev)
1455 struct uhub_softc *sc = device_get_softc(dev);
1456 struct usb_hub *hub = sc->sc_udev->hub;
1457 struct usb_bus *bus = sc->sc_udev->bus;
1458 struct usb_device *child;
1461 if (hub == NULL) /* must be partially working */
1464 /* Make sure interrupt transfer is gone. */
1465 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1467 /* Detach all ports */
1468 for (x = 0; x != hub->nports; x++) {
1470 child = usb_bus_port_get_device(bus, hub->ports + x);
1472 if (child == NULL) {
1477 * Free USB device and all subdevices, if any.
1479 usb_free_device(child, 0);
1482 #if USB_HAVE_TT_SUPPORT
1483 /* Make sure our TT messages are not queued anywhere */
1485 usb_proc_mwait(&bus->non_giant_callback_proc,
1486 &hub->tt_msg[0], &hub->tt_msg[1]);
1487 USB_BUS_UNLOCK(bus);
1490 free(hub, M_USBDEV);
1491 sc->sc_udev->hub = NULL;
1493 mtx_destroy(&sc->sc_mtx);
1499 uhub_suspend(device_t dev)
1502 /* Sub-devices are not suspended here! */
1507 uhub_resume(device_t dev)
1510 /* Sub-devices are not resumed here! */
1515 uhub_driver_added(device_t dev, driver_t *driver)
1517 usb_needs_explore_all();
1521 struct usb_device *udev;
1523 uint8_t iface_index;
1527 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1528 struct hub_result *res)
1530 struct usb_interface *iface;
1531 struct usb_device *udev;
1536 nports = hub->nports;
1537 for (x = 0; x != nports; x++) {
1538 udev = usb_bus_port_get_device(hub->hubudev->bus,
1543 for (i = 0; i != USB_IFACE_MAX; i++) {
1544 iface = usbd_get_iface(udev, i);
1546 (iface->subdev == child)) {
1547 res->iface_index = i;
1549 res->portno = x + 1;
1554 res->iface_index = 0;
1560 uhub_child_location_string(device_t parent, device_t child,
1561 char *buf, size_t buflen)
1563 struct uhub_softc *sc;
1564 struct usb_hub *hub;
1565 struct hub_result res;
1567 if (!device_is_attached(parent)) {
1573 sc = device_get_softc(parent);
1574 hub = sc->sc_udev->hub;
1577 uhub_find_iface_index(hub, child, &res);
1579 DPRINTF("device not on hub\n");
1585 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1586 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1587 res.portno, device_get_unit(res.udev->bus->bdev),
1588 res.udev->device_index, res.iface_index);
1596 uhub_child_pnpinfo_string(device_t parent, device_t child,
1597 char *buf, size_t buflen)
1599 struct uhub_softc *sc;
1600 struct usb_hub *hub;
1601 struct usb_interface *iface;
1602 struct hub_result res;
1604 if (!device_is_attached(parent)) {
1610 sc = device_get_softc(parent);
1611 hub = sc->sc_udev->hub;
1614 uhub_find_iface_index(hub, child, &res);
1616 DPRINTF("device not on hub\n");
1622 iface = usbd_get_iface(res.udev, res.iface_index);
1623 if (iface && iface->idesc) {
1624 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1625 "devclass=0x%02x devsubclass=0x%02x "
1629 "intclass=0x%02x intsubclass=0x%02x "
1630 "intprotocol=0x%02x " "%s%s",
1631 UGETW(res.udev->ddesc.idVendor),
1632 UGETW(res.udev->ddesc.idProduct),
1633 res.udev->ddesc.bDeviceClass,
1634 res.udev->ddesc.bDeviceSubClass,
1635 usb_get_serial(res.udev),
1636 UGETW(res.udev->ddesc.bcdDevice),
1637 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1638 iface->idesc->bInterfaceClass,
1639 iface->idesc->bInterfaceSubClass,
1640 iface->idesc->bInterfaceProtocol,
1641 iface->pnpinfo ? " " : "",
1642 iface->pnpinfo ? iface->pnpinfo : "");
1656 * The USB Transaction Translator:
1657 * ===============================
1659 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1660 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1661 * USB transfers. To utilize bandwidth dynamically the "scatter and
1662 * gather" principle must be applied. This means that bandwidth must
1663 * be divided into equal parts of bandwidth. With regard to USB all
1664 * data is transferred in smaller packets with length
1665 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1668 * The bandwidth scheduler which I have implemented will simply pack
1669 * the USB transfers back to back until there is no more space in the
1670 * schedule. Out of the 8 microframes which the USB 2.0 standard
1671 * provides, only 6 are available for non-HIGH-speed devices. I have
1672 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1673 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1674 * this division, it is very difficult to allocate and free bandwidth
1677 * NOTE about the Transaction Translator in USB HUBs:
1679 * USB HUBs have a very simple Transaction Translator, that will
1680 * simply pipeline all the SPLIT transactions. That means that the
1681 * transactions will be executed in the order they are queued!
1685 /*------------------------------------------------------------------------*
1686 * usb_intr_find_best_slot
1689 * The best Transaction Translation slot for an interrupt endpoint.
1690 *------------------------------------------------------------------------*/
1692 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1693 uint8_t end, uint8_t mask)
1695 usb_size_t min = (usb_size_t)-1;
1703 /* find the last slot with lesser used bandwidth */
1705 for (x = start; x < end; x++) {
1709 /* compute sum of bandwidth */
1710 for (z = x; z < end; z++) {
1711 if (mask & (1U << (z - x)))
1715 /* check if the current multi-slot is more optimal */
1721 /* check if the mask is about to be shifted out */
1722 if (mask & (1U << (end - 1 - x)))
1728 /*------------------------------------------------------------------------*
1729 * usb_hs_bandwidth_adjust
1731 * This function will update the bandwith usage for the microframe
1732 * having index "slot" by "len" bytes. "len" can be negative. If the
1733 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1734 * the "slot" argument will be replaced by the slot having least used
1735 * bandwidth. The "mask" argument is used for multi-slot allocations.
1738 * The slot in which the bandwidth update was done: 0..7
1739 *------------------------------------------------------------------------*/
1741 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1742 uint8_t slot, uint8_t mask)
1744 struct usb_bus *bus = udev->bus;
1745 struct usb_hub *hub;
1746 enum usb_dev_speed speed;
1749 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1751 speed = usbd_get_speed(udev);
1755 case USB_SPEED_FULL:
1756 if (speed == USB_SPEED_LOW) {
1760 * The Host Controller Driver should have
1761 * performed checks so that the lookup
1762 * below does not result in a NULL pointer
1766 hub = udev->parent_hs_hub->hub;
1767 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1768 slot = usb_intr_find_best_slot(hub->uframe_usage,
1769 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1771 for (x = slot; x < 8; x++) {
1772 if (mask & (1U << (x - slot))) {
1773 hub->uframe_usage[x] += len;
1774 bus->uframe_usage[x] += len;
1779 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1780 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1781 USB_HS_MICRO_FRAMES_MAX, mask);
1783 for (x = slot; x < 8; x++) {
1784 if (mask & (1U << (x - slot))) {
1785 bus->uframe_usage[x] += len;
1793 /*------------------------------------------------------------------------*
1794 * usb_hs_bandwidth_alloc
1796 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1797 *------------------------------------------------------------------------*/
1799 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1801 struct usb_device *udev;
1806 udev = xfer->xroot->udev;
1808 if (udev->flags.usb_mode != USB_MODE_HOST)
1809 return; /* not supported */
1811 xfer->endpoint->refcount_bw++;
1812 if (xfer->endpoint->refcount_bw != 1)
1813 return; /* already allocated */
1815 speed = usbd_get_speed(udev);
1817 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1819 /* allocate a microframe slot */
1822 slot = usb_hs_bandwidth_adjust(udev,
1823 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1825 xfer->endpoint->usb_uframe = slot;
1826 xfer->endpoint->usb_smask = mask << slot;
1828 if ((speed != USB_SPEED_FULL) &&
1829 (speed != USB_SPEED_LOW)) {
1830 xfer->endpoint->usb_cmask = 0x00 ;
1832 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1836 case UE_ISOCHRONOUS:
1837 switch (usbd_xfer_get_fps_shift(xfer)) {
1852 /* allocate a microframe multi-slot */
1854 slot = usb_hs_bandwidth_adjust(udev,
1855 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1857 xfer->endpoint->usb_uframe = slot;
1858 xfer->endpoint->usb_cmask = 0;
1859 xfer->endpoint->usb_smask = mask << slot;
1863 xfer->endpoint->usb_uframe = 0;
1864 xfer->endpoint->usb_cmask = 0;
1865 xfer->endpoint->usb_smask = 0;
1869 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1870 xfer->endpoint->usb_uframe,
1871 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1874 /*------------------------------------------------------------------------*
1875 * usb_hs_bandwidth_free
1877 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1878 *------------------------------------------------------------------------*/
1880 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1882 struct usb_device *udev;
1886 udev = xfer->xroot->udev;
1888 if (udev->flags.usb_mode != USB_MODE_HOST)
1889 return; /* not supported */
1891 xfer->endpoint->refcount_bw--;
1892 if (xfer->endpoint->refcount_bw != 0)
1893 return; /* still allocated */
1895 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1897 case UE_ISOCHRONOUS:
1899 slot = xfer->endpoint->usb_uframe;
1900 mask = xfer->endpoint->usb_smask;
1902 /* free microframe slot(s): */
1903 usb_hs_bandwidth_adjust(udev,
1904 -xfer->max_frame_size, slot, mask >> slot);
1906 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1907 slot, mask >> slot);
1909 xfer->endpoint->usb_uframe = 0;
1910 xfer->endpoint->usb_cmask = 0;
1911 xfer->endpoint->usb_smask = 0;
1919 /*------------------------------------------------------------------------*
1920 * usb_isoc_time_expand
1922 * This function will expand the time counter from 7-bit to 16-bit.
1925 * 16-bit isochronous time counter.
1926 *------------------------------------------------------------------------*/
1928 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1932 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1934 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1936 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1938 if (isoc_time_curr < rem) {
1939 /* the time counter wrapped around */
1940 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1942 /* update the remainder */
1944 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1945 bus->isoc_time_last |= isoc_time_curr;
1947 return (bus->isoc_time_last);
1950 /*------------------------------------------------------------------------*
1951 * usbd_fs_isoc_schedule_alloc_slot
1953 * This function will allocate bandwidth for an isochronous FULL speed
1954 * transaction in the FULL speed schedule.
1959 *------------------------------------------------------------------------*/
1960 #if USB_HAVE_TT_SUPPORT
1962 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1964 struct usb_xfer *xfer;
1965 struct usb_xfer *pipe_xfer;
1966 struct usb_bus *bus;
1968 usb_frlength_t data_len;
1976 bus = isoc_xfer->xroot->bus;
1978 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1980 /* skip self, if any */
1982 if (xfer == isoc_xfer)
1985 /* check if this USB transfer is going through the same TT */
1987 if (xfer->xroot->udev->parent_hs_hub !=
1988 isoc_xfer->xroot->udev->parent_hs_hub) {
1991 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1992 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1993 (xfer->xroot->udev->hs_port_no !=
1994 isoc_xfer->xroot->udev->hs_port_no)) {
1997 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2000 /* check if isoc_time is part of this transfer */
2002 delta = xfer->isoc_time_complete - isoc_time;
2003 if (delta > 0 && delta <= xfer->nframes) {
2004 delta = xfer->nframes - delta;
2006 len = xfer->frlengths[delta];
2014 /* check double buffered transfers */
2016 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
2019 /* skip self, if any */
2021 if (pipe_xfer == isoc_xfer)
2024 /* check if isoc_time is part of this transfer */
2026 delta = pipe_xfer->isoc_time_complete - isoc_time;
2027 if (delta > 0 && delta <= pipe_xfer->nframes) {
2028 delta = pipe_xfer->nframes - delta;
2030 len = pipe_xfer->frlengths[delta];
2040 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2041 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2045 /* check for overflow */
2047 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2052 delta = isoc_xfer->isoc_time_complete - isoc_time;
2053 if (delta > 0 && delta <= isoc_xfer->nframes) {
2054 delta = isoc_xfer->nframes - delta;
2056 len = isoc_xfer->frlengths[delta];
2064 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2065 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2069 /* check for overflow */
2071 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2078 /*------------------------------------------------------------------------*
2079 * usb_bus_port_get_device
2081 * This function is NULL safe.
2082 *------------------------------------------------------------------------*/
2084 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2086 if ((bus == NULL) || (up == NULL)) {
2090 if (up->device_index == 0) {
2094 return (bus->devices[up->device_index]);
2097 /*------------------------------------------------------------------------*
2098 * usb_bus_port_set_device
2100 * This function is NULL safe.
2101 *------------------------------------------------------------------------*/
2103 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2104 struct usb_device *udev, uint8_t device_index)
2111 * There is only one case where we don't
2112 * have an USB port, and that is the Root Hub!
2116 up->device_index = device_index;
2118 device_index = up->device_index;
2119 up->device_index = 0;
2123 * Make relationships to our new device
2125 if (device_index != 0) {
2127 mtx_lock(&usb_ref_lock);
2129 bus->devices[device_index] = udev;
2131 mtx_unlock(&usb_ref_lock);
2137 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2140 /*------------------------------------------------------------------------*
2143 * This functions is called when the USB event thread needs to run.
2144 *------------------------------------------------------------------------*/
2146 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2153 DPRINTF("No bus pointer!\n");
2156 if ((bus->devices == NULL) ||
2157 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2158 DPRINTF("No root HUB\n");
2161 if (mtx_owned(&bus->bus_mtx)) {
2170 if (usb_proc_msignal(&bus->explore_proc,
2171 &bus->explore_msg[0], &bus->explore_msg[1])) {
2175 USB_BUS_UNLOCK(bus);
2179 /*------------------------------------------------------------------------*
2180 * usb_needs_explore_all
2182 * This function is called whenever a new driver is loaded and will
2183 * cause that all USB busses are re-explored.
2184 *------------------------------------------------------------------------*/
2186 usb_needs_explore_all(void)
2188 struct usb_bus *bus;
2195 dc = usb_devclass_ptr;
2197 DPRINTFN(0, "no devclass\n");
2201 * Explore all USB busses in parallell.
2203 max = devclass_get_maxunit(dc);
2205 dev = devclass_get_device(dc, max);
2207 bus = device_get_softc(dev);
2209 usb_needs_explore(bus, 1);
2216 /*------------------------------------------------------------------------*
2217 * usb_bus_power_update
2219 * This function will ensure that all USB devices on the given bus are
2220 * properly suspended or resumed according to the device transfer
2222 *------------------------------------------------------------------------*/
2225 usb_bus_power_update(struct usb_bus *bus)
2227 usb_needs_explore(bus, 0 /* no probe */ );
2231 /*------------------------------------------------------------------------*
2232 * usbd_transfer_power_ref
2234 * This function will modify the power save reference counts and
2235 * wakeup the USB device associated with the given USB transfer, if
2237 *------------------------------------------------------------------------*/
2240 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2242 static const usb_power_mask_t power_mask[4] = {
2243 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2244 [UE_BULK] = USB_HW_POWER_BULK,
2245 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2246 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2248 struct usb_device *udev;
2249 uint8_t needs_explore;
2250 uint8_t needs_hw_power;
2253 udev = xfer->xroot->udev;
2255 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2256 /* no power save for root HUB */
2259 USB_BUS_LOCK(udev->bus);
2261 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2263 udev->pwr_save.last_xfer_time = ticks;
2264 udev->pwr_save.type_refs[xfer_type] += val;
2266 if (xfer->flags_int.control_xfr) {
2267 udev->pwr_save.read_refs += val;
2268 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2270 * It is not allowed to suspend during a
2273 udev->pwr_save.write_refs += val;
2275 } else if (USB_GET_DATA_ISREAD(xfer)) {
2276 udev->pwr_save.read_refs += val;
2278 udev->pwr_save.write_refs += val;
2282 if (udev->flags.self_suspended)
2283 needs_explore = usb_peer_should_wakeup(udev);
2287 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2288 DPRINTF("Adding type %u to power state\n", xfer_type);
2289 udev->bus->hw_power_state |= power_mask[xfer_type];
2299 USB_BUS_UNLOCK(udev->bus);
2301 if (needs_explore) {
2302 DPRINTF("update\n");
2303 usb_bus_power_update(udev->bus);
2304 } else if (needs_hw_power) {
2305 DPRINTF("needs power\n");
2306 if (udev->bus->methods->set_hw_power != NULL) {
2307 (udev->bus->methods->set_hw_power) (udev->bus);
2313 /*------------------------------------------------------------------------*
2314 * usb_peer_should_wakeup
2316 * This function returns non-zero if the current device should wake up.
2317 *------------------------------------------------------------------------*/
2319 usb_peer_should_wakeup(struct usb_device *udev)
2321 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2322 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2323 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2324 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2325 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2326 (udev->pwr_save.write_refs != 0) ||
2327 ((udev->pwr_save.read_refs != 0) &&
2328 (udev->flags.usb_mode == USB_MODE_HOST) &&
2329 (usb_peer_can_wakeup(udev) == 0)));
2332 /*------------------------------------------------------------------------*
2335 * This function implements the USB power daemon and is called
2336 * regularly from the USB explore thread.
2337 *------------------------------------------------------------------------*/
2340 usb_bus_powerd(struct usb_bus *bus)
2342 struct usb_device *udev;
2345 usb_ticks_t mintime;
2346 usb_size_t type_refs[5];
2349 limit = usb_power_timeout;
2352 else if (limit > 255)
2357 DPRINTF("bus=%p\n", bus);
2362 * The root HUB device is never suspended
2363 * and we simply skip it.
2365 for (x = USB_ROOT_HUB_ADDR + 1;
2366 x != bus->devices_max; x++) {
2368 udev = bus->devices[x];
2372 temp = ticks - udev->pwr_save.last_xfer_time;
2374 if (usb_peer_should_wakeup(udev)) {
2375 /* check if we are suspended */
2376 if (udev->flags.self_suspended != 0) {
2377 USB_BUS_UNLOCK(bus);
2378 usb_dev_resume_peer(udev);
2381 } else if ((temp >= limit) &&
2382 (udev->flags.usb_mode == USB_MODE_HOST) &&
2383 (udev->flags.self_suspended == 0)) {
2384 /* try to do suspend */
2386 USB_BUS_UNLOCK(bus);
2387 usb_dev_suspend_peer(udev);
2392 /* reset counters */
2394 mintime = (usb_ticks_t)-1;
2401 /* Re-loop all the devices to get the actual state */
2403 for (x = USB_ROOT_HUB_ADDR + 1;
2404 x != bus->devices_max; x++) {
2406 udev = bus->devices[x];
2410 /* we found a non-Root-Hub USB device */
2413 /* "last_xfer_time" can be updated by a resume */
2414 temp = ticks - udev->pwr_save.last_xfer_time;
2417 * Compute minimum time since last transfer for the complete
2423 if (udev->flags.self_suspended == 0) {
2424 type_refs[0] += udev->pwr_save.type_refs[0];
2425 type_refs[1] += udev->pwr_save.type_refs[1];
2426 type_refs[2] += udev->pwr_save.type_refs[2];
2427 type_refs[3] += udev->pwr_save.type_refs[3];
2431 if (mintime >= (usb_ticks_t)(1 * hz)) {
2432 /* recompute power masks */
2433 DPRINTF("Recomputing power masks\n");
2434 bus->hw_power_state = 0;
2435 if (type_refs[UE_CONTROL] != 0)
2436 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2437 if (type_refs[UE_BULK] != 0)
2438 bus->hw_power_state |= USB_HW_POWER_BULK;
2439 if (type_refs[UE_INTERRUPT] != 0)
2440 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2441 if (type_refs[UE_ISOCHRONOUS] != 0)
2442 bus->hw_power_state |= USB_HW_POWER_ISOC;
2443 if (type_refs[4] != 0)
2444 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2446 USB_BUS_UNLOCK(bus);
2448 if (bus->methods->set_hw_power != NULL) {
2449 /* always update hardware power! */
2450 (bus->methods->set_hw_power) (bus);
2456 /*------------------------------------------------------------------------*
2457 * usb_dev_resume_peer
2459 * This function will resume an USB peer and do the required USB
2460 * signalling to get an USB device out of the suspended state.
2461 *------------------------------------------------------------------------*/
2463 usb_dev_resume_peer(struct usb_device *udev)
2465 struct usb_bus *bus;
2472 /* check if already resumed */
2473 if (udev->flags.self_suspended == 0)
2476 /* we need a parent HUB to do resume */
2477 if (udev->parent_hub == NULL)
2480 DPRINTF("udev=%p\n", udev);
2482 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2483 (udev->flags.remote_wakeup == 0)) {
2485 * If the host did not set the remote wakeup feature, we can
2486 * not wake it up either!
2488 DPRINTF("remote wakeup is not set!\n");
2491 /* get bus pointer */
2494 /* resume parent hub first */
2495 usb_dev_resume_peer(udev->parent_hub);
2497 /* reduce chance of instant resume failure by waiting a little bit */
2498 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2500 if (usb_device_20_compatible(udev)) {
2501 /* resume current port (Valid in Host and Device Mode) */
2502 err = usbd_req_clear_port_feature(udev->parent_hub,
2503 NULL, udev->port_no, UHF_PORT_SUSPEND);
2505 DPRINTFN(0, "Resuming port failed\n");
2509 /* resume current port (Valid in Host and Device Mode) */
2510 err = usbd_req_set_port_link_state(udev->parent_hub,
2511 NULL, udev->port_no, UPS_PORT_LS_U0);
2513 DPRINTFN(0, "Resuming port failed\n");
2518 /* resume settle time */
2519 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2521 if (bus->methods->device_resume != NULL) {
2522 /* resume USB device on the USB controller */
2523 (bus->methods->device_resume) (udev);
2526 /* set that this device is now resumed */
2527 udev->flags.self_suspended = 0;
2529 /* make sure that we don't go into suspend right away */
2530 udev->pwr_save.last_xfer_time = ticks;
2532 /* make sure the needed power masks are on */
2533 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2534 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2535 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2536 bus->hw_power_state |= USB_HW_POWER_BULK;
2537 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2538 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2539 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2540 bus->hw_power_state |= USB_HW_POWER_ISOC;
2542 USB_BUS_UNLOCK(bus);
2544 if (bus->methods->set_hw_power != NULL) {
2545 /* always update hardware power! */
2546 (bus->methods->set_hw_power) (bus);
2551 /* notify all sub-devices about resume */
2552 err = usb_suspend_resume(udev, 0);
2554 usbd_sr_unlock(udev);
2556 /* check if peer has wakeup capability */
2557 if (usb_peer_can_wakeup(udev)) {
2558 /* clear remote wakeup */
2559 err = usbd_req_clear_device_feature(udev,
2560 NULL, UF_DEVICE_REMOTE_WAKEUP);
2562 DPRINTFN(0, "Clearing device "
2563 "remote wakeup failed: %s\n",
2569 /*------------------------------------------------------------------------*
2570 * usb_dev_suspend_peer
2572 * This function will suspend an USB peer and do the required USB
2573 * signalling to get an USB device into the suspended state.
2574 *------------------------------------------------------------------------*/
2576 usb_dev_suspend_peer(struct usb_device *udev)
2578 struct usb_device *child;
2588 /* check if already suspended */
2589 if (udev->flags.self_suspended)
2592 /* we need a parent HUB to do suspend */
2593 if (udev->parent_hub == NULL)
2596 DPRINTF("udev=%p\n", udev);
2598 /* check if the current device is a HUB */
2599 if (udev->hub != NULL) {
2600 nports = udev->hub->nports;
2602 /* check if all devices on the HUB are suspended */
2603 for (x = 0; x != nports; x++) {
2604 child = usb_bus_port_get_device(udev->bus,
2605 udev->hub->ports + x);
2610 if (child->flags.self_suspended)
2613 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2618 if (usb_peer_can_wakeup(udev)) {
2620 * This request needs to be done before we set
2621 * "udev->flags.self_suspended":
2624 /* allow device to do remote wakeup */
2625 err = usbd_req_set_device_feature(udev,
2626 NULL, UF_DEVICE_REMOTE_WAKEUP);
2628 DPRINTFN(0, "Setting device "
2629 "remote wakeup failed\n");
2633 USB_BUS_LOCK(udev->bus);
2635 * Checking for suspend condition and setting suspended bit
2638 err = usb_peer_should_wakeup(udev);
2641 * Set that this device is suspended. This variable
2642 * must be set before calling USB controller suspend
2645 udev->flags.self_suspended = 1;
2647 USB_BUS_UNLOCK(udev->bus);
2650 if (usb_peer_can_wakeup(udev)) {
2651 /* allow device to do remote wakeup */
2652 err = usbd_req_clear_device_feature(udev,
2653 NULL, UF_DEVICE_REMOTE_WAKEUP);
2655 DPRINTFN(0, "Setting device "
2656 "remote wakeup failed\n");
2660 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2661 /* resume parent HUB first */
2662 usb_dev_resume_peer(udev->parent_hub);
2664 /* reduce chance of instant resume failure by waiting a little bit */
2665 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2667 /* resume current port (Valid in Host and Device Mode) */
2668 err = usbd_req_clear_port_feature(udev->parent_hub,
2669 NULL, udev->port_no, UHF_PORT_SUSPEND);
2671 /* resume settle time */
2672 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2674 DPRINTF("Suspend was cancelled!\n");
2680 /* notify all sub-devices about suspend */
2681 err = usb_suspend_resume(udev, 1);
2683 usbd_sr_unlock(udev);
2685 if (udev->bus->methods->device_suspend != NULL) {
2688 /* suspend device on the USB controller */
2689 (udev->bus->methods->device_suspend) (udev);
2692 temp = usbd_get_dma_delay(udev);
2694 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2698 if (usb_device_20_compatible(udev)) {
2699 /* suspend current port */
2700 err = usbd_req_set_port_feature(udev->parent_hub,
2701 NULL, udev->port_no, UHF_PORT_SUSPEND);
2703 DPRINTFN(0, "Suspending port failed\n");
2707 /* suspend current port */
2708 err = usbd_req_set_port_link_state(udev->parent_hub,
2709 NULL, udev->port_no, UPS_PORT_LS_U3);
2711 DPRINTFN(0, "Suspending port failed\n");
2716 udev = udev->parent_hub;
2720 /*------------------------------------------------------------------------*
2721 * usbd_set_power_mode
2723 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2725 *------------------------------------------------------------------------*/
2727 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2729 /* filter input argument */
2730 if ((power_mode != USB_POWER_MODE_ON) &&
2731 (power_mode != USB_POWER_MODE_OFF))
2732 power_mode = USB_POWER_MODE_SAVE;
2734 power_mode = usbd_filter_power_mode(udev, power_mode);
2736 udev->power_mode = power_mode; /* update copy of power mode */
2739 usb_bus_power_update(udev->bus);
2741 usb_needs_explore(udev->bus, 0 /* no probe */ );
2745 /*------------------------------------------------------------------------*
2746 * usbd_filter_power_mode
2748 * This function filters the power mode based on hardware requirements.
2749 *------------------------------------------------------------------------*/
2751 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2753 struct usb_bus_methods *mtod;
2756 mtod = udev->bus->methods;
2759 if (mtod->get_power_mode != NULL)
2760 (mtod->get_power_mode) (udev, &temp);
2762 /* check if we should not filter */
2764 return (power_mode);
2766 /* use fixed power mode given by hardware driver */
2770 /*------------------------------------------------------------------------*
2771 * usbd_start_re_enumerate
2773 * This function starts re-enumeration of the given USB device. This
2774 * function does not need to be called BUS-locked. This function does
2775 * not wait until the re-enumeration is completed.
2776 *------------------------------------------------------------------------*/
2778 usbd_start_re_enumerate(struct usb_device *udev)
2780 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2781 udev->re_enumerate_wait = USB_RE_ENUM_START;
2782 usb_needs_explore(udev->bus, 0);