3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR uhub_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_request.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_hub.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_controller.h>
70 #include <dev/usb/usb_bus.h>
72 #define UHUB_INTR_INTERVAL 250 /* ms */
75 #if USB_HAVE_TT_SUPPORT
76 UHUB_RESET_TT_TRANSFER,
82 static int uhub_debug = 0;
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
85 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
87 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
91 static int usb_power_timeout = 30; /* seconds */
93 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
94 &usb_power_timeout, 0, "USB power timeout");
97 struct uhub_current_state {
103 struct uhub_current_state sc_st;/* current state */
104 device_t sc_dev; /* base device */
105 struct mtx sc_mtx; /* our mutex */
106 struct usb_device *sc_udev; /* USB device */
107 struct usb_xfer *sc_xfer[UHUB_N_TRANSFER]; /* interrupt xfer */
109 #define UHUB_FLAG_DID_EXPLORE 0x01
113 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
114 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
115 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
116 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
117 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
119 /* prototypes for type checking: */
121 static device_probe_t uhub_probe;
122 static device_attach_t uhub_attach;
123 static device_detach_t uhub_detach;
124 static device_suspend_t uhub_suspend;
125 static device_resume_t uhub_resume;
127 static bus_driver_added_t uhub_driver_added;
128 static bus_child_location_str_t uhub_child_location_string;
129 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
131 static usb_callback_t uhub_intr_callback;
132 #if USB_HAVE_TT_SUPPORT
133 static usb_callback_t uhub_reset_tt_callback;
136 static void usb_dev_resume_peer(struct usb_device *udev);
137 static void usb_dev_suspend_peer(struct usb_device *udev);
138 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
140 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
142 [UHUB_INTR_TRANSFER] = {
143 .type = UE_INTERRUPT,
144 .endpoint = UE_ADDR_ANY,
145 .direction = UE_DIR_ANY,
147 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
148 .bufsize = 0, /* use wMaxPacketSize */
149 .callback = &uhub_intr_callback,
150 .interval = UHUB_INTR_INTERVAL,
152 #if USB_HAVE_TT_SUPPORT
153 [UHUB_RESET_TT_TRANSFER] = {
155 .endpoint = 0x00, /* Control pipe */
156 .direction = UE_DIR_ANY,
157 .bufsize = sizeof(struct usb_device_request),
158 .callback = &uhub_reset_tt_callback,
159 .timeout = 1000, /* 1 second */
160 .usb_mode = USB_MODE_HOST,
166 * driver instance for "hub" connected to "usb"
167 * and "hub" connected to "hub"
169 static devclass_t uhub_devclass;
171 static device_method_t uhub_methods[] = {
172 DEVMETHOD(device_probe, uhub_probe),
173 DEVMETHOD(device_attach, uhub_attach),
174 DEVMETHOD(device_detach, uhub_detach),
176 DEVMETHOD(device_suspend, uhub_suspend),
177 DEVMETHOD(device_resume, uhub_resume),
179 DEVMETHOD(bus_child_location_str, uhub_child_location_string),
180 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
181 DEVMETHOD(bus_driver_added, uhub_driver_added),
185 static driver_t uhub_driver = {
187 .methods = uhub_methods,
188 .size = sizeof(struct uhub_softc)
191 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
192 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
193 MODULE_VERSION(uhub, 1);
196 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
198 struct uhub_softc *sc = usbd_xfer_softc(xfer);
200 switch (USB_GET_STATE(xfer)) {
201 case USB_ST_TRANSFERRED:
204 * This is an indication that some port
205 * has changed status. Notify the bus
206 * event handler thread that we need
207 * to be explored again:
209 usb_needs_explore(sc->sc_udev->bus, 0);
212 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
213 usbd_transfer_submit(xfer);
217 if (xfer->error != USB_ERR_CANCELLED) {
219 * Do a clear-stall. The "stall_pipe" flag
220 * will get cleared before next callback by
223 usbd_xfer_set_stall(xfer);
224 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
225 usbd_transfer_submit(xfer);
231 /*------------------------------------------------------------------------*
234 * This function starts the TT reset USB request
235 *------------------------------------------------------------------------*/
236 #if USB_HAVE_TT_SUPPORT
238 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
240 struct usb_udev_msg *pm = (void *)_pm;
241 struct usb_device *udev = pm->udev;
243 struct uhub_softc *sc;
253 USB_BUS_UNLOCK(udev->bus);
254 mtx_lock(&sc->sc_mtx);
256 usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
258 mtx_unlock(&sc->sc_mtx);
259 USB_BUS_LOCK(udev->bus);
263 /*------------------------------------------------------------------------*
264 * uhub_tt_buffer_reset_async_locked
266 * This function queues a TT reset for the given USB device and endpoint.
267 *------------------------------------------------------------------------*/
268 #if USB_HAVE_TT_SUPPORT
270 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
272 struct usb_device_request req;
273 struct usb_device *udev;
279 if (child == NULL || ep == NULL)
282 udev = child->parent_hs_hub;
283 port = child->hs_port_no;
290 (udev->speed != USB_SPEED_HIGH) ||
291 (child->speed != USB_SPEED_LOW &&
292 child->speed != USB_SPEED_FULL) ||
293 (child->flags.usb_mode != USB_MODE_HOST) ||
294 (port == 0) || (ep->edesc == NULL)) {
299 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
301 up = hub->ports + port - 1;
303 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
304 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
307 /* if we already received a clear buffer request, reset the whole TT */
308 if (up->req_reset_tt.bRequest != 0) {
309 req.bmRequestType = UT_WRITE_CLASS_OTHER;
310 req.bRequest = UR_RESET_TT;
311 USETW(req.wValue, 0);
312 req.wIndex[0] = port;
314 USETW(req.wLength, 0);
316 wValue = (ep->edesc->bEndpointAddress & 0xF) |
317 ((child->address & 0x7F) << 4) |
318 ((ep->edesc->bEndpointAddress & 0x80) << 8) |
319 ((ep->edesc->bmAttributes & 3) << 12);
321 req.bmRequestType = UT_WRITE_CLASS_OTHER;
322 req.bRequest = UR_CLEAR_TT_BUFFER;
323 USETW(req.wValue, wValue);
324 req.wIndex[0] = port;
326 USETW(req.wLength, 0);
328 up->req_reset_tt = req;
329 /* get reset transfer started */
330 usb_proc_msignal(&udev->bus->non_giant_callback_proc,
331 &hub->tt_msg[0], &hub->tt_msg[1]);
335 #if USB_HAVE_TT_SUPPORT
337 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
339 struct uhub_softc *sc;
340 struct usb_device *udev;
344 DPRINTF("TT buffer reset\n");
346 sc = usbd_xfer_softc(xfer);
349 switch (USB_GET_STATE(xfer)) {
350 case USB_ST_TRANSFERRED:
353 USB_BUS_LOCK(udev->bus);
354 /* find first port which needs a TT reset */
355 for (x = 0; x != udev->hub->nports; x++) {
356 up = udev->hub->ports + x;
358 if (up->req_reset_tt.bRequest == 0)
361 /* copy in the transfer */
362 usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
363 sizeof(up->req_reset_tt));
365 memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
368 usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
370 USB_BUS_UNLOCK(udev->bus);
372 usbd_transfer_submit(xfer);
375 USB_BUS_UNLOCK(udev->bus);
379 if (error == USB_ERR_CANCELLED)
382 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
388 /*------------------------------------------------------------------------*
389 * uhub_count_active_host_ports
391 * This function counts the number of active ports at the given speed.
392 *------------------------------------------------------------------------*/
394 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
396 struct uhub_softc *sc;
397 struct usb_device *child;
412 for (x = 0; x != hub->nports; x++) {
414 child = usb_bus_port_get_device(udev->bus, up);
416 child->flags.usb_mode == USB_MODE_HOST &&
417 child->speed == speed)
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 (udev->parent_hub != NULL) {
762 /* inherit mode from the parent HUB */
763 mode = udev->parent_hub->flags.usb_mode;
764 } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
765 mode = USB_MODE_DEVICE;
767 mode = USB_MODE_HOST;
769 /* need to create a new child */
770 child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
771 udev->depth + 1, portno - 1, portno, speed, mode);
773 DPRINTFN(0, "could not allocate new device\n");
776 return (0); /* success */
781 * Free USB device and all subdevices, if any.
783 usb_free_device(child, 0);
787 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
788 err = usbd_req_clear_port_feature(
790 portno, UHF_PORT_ENABLE);
794 DPRINTFN(0, "device problem (%s), "
795 "disabling port %d\n", usbd_errstr(err), portno);
800 /*------------------------------------------------------------------------*
801 * usb_device_20_compatible
804 * 0: HUB does not support suspend and resume
805 * Else: HUB supports suspend and resume
806 *------------------------------------------------------------------------*/
808 usb_device_20_compatible(struct usb_device *udev)
812 switch (udev->speed) {
822 /*------------------------------------------------------------------------*
823 * uhub_suspend_resume_port
827 * Else: A control transaction failed
828 *------------------------------------------------------------------------*/
830 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
832 struct usb_device *child;
833 struct usb_device *udev;
837 DPRINTF("port %d\n", portno);
840 child = usb_bus_port_get_device(udev->bus,
841 udev->hub->ports + portno - 1);
843 /* first clear the port suspend change bit */
845 if (usb_device_20_compatible(udev)) {
846 err = usbd_req_clear_port_feature(udev, NULL,
847 portno, UHF_C_PORT_SUSPEND);
849 err = usbd_req_clear_port_feature(udev, NULL,
850 portno, UHF_C_PORT_LINK_STATE);
854 DPRINTF("clearing suspend failed.\n");
857 /* get fresh status */
859 err = uhub_read_port_status(sc, portno);
861 DPRINTF("reading port status failed.\n");
864 /* convert current state */
866 if (usb_device_20_compatible(udev)) {
867 if (sc->sc_st.port_status & UPS_SUSPEND) {
873 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
877 case UPS_PORT_LS_SS_INA:
878 usbd_req_warm_reset_port(udev, NULL, portno);
887 DPRINTF("suspended=%u\n", is_suspend);
889 /* do the suspend or resume */
893 * This code handle two cases: 1) Host Mode - we can only
894 * receive resume here 2) Device Mode - we can receive
895 * suspend and resume here
898 usb_dev_resume_peer(child);
899 else if (child->flags.usb_mode == USB_MODE_DEVICE)
900 usb_dev_suspend_peer(child);
906 /*------------------------------------------------------------------------*
907 * uhub_root_interrupt
909 * This function is called when a Root HUB interrupt has
910 * happened. "ptr" and "len" makes up the Root HUB interrupt
911 * packet. This function is called having the "bus_mtx" locked.
912 *------------------------------------------------------------------------*/
914 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
916 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
918 usb_needs_explore(bus, 0);
922 uhub_is_too_deep(struct usb_device *udev)
924 switch (udev->speed) {
928 if (udev->depth > USB_HUB_MAX_DEPTH)
931 case USB_SPEED_SUPER:
932 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
941 /*------------------------------------------------------------------------*
947 *------------------------------------------------------------------------*/
949 uhub_explore(struct usb_device *udev)
952 struct uhub_softc *sc;
962 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
964 /* ignore devices that are too deep */
965 if (uhub_is_too_deep(udev))
966 return (USB_ERR_TOO_DEEP);
968 /* check if device is suspended */
969 if (udev->flags.self_suspended) {
970 /* need to wait until the child signals resume */
971 DPRINTF("Device is suspended!\n");
976 * Make sure we don't race against user-space applications
979 do_unlock = usbd_enum_lock(udev);
981 for (x = 0; x != hub->nports; x++) {
985 err = uhub_read_port_status(sc, portno);
987 /* most likely the HUB is gone */
990 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
991 DPRINTF("Overcurrent on port %u.\n", portno);
992 err = usbd_req_clear_port_feature(
993 udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
995 /* most likely the HUB is gone */
999 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1001 * Fake a connect status change so that the
1002 * status gets checked initially!
1004 sc->sc_st.port_change |=
1005 UPS_C_CONNECT_STATUS;
1007 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1008 err = usbd_req_clear_port_feature(
1009 udev, NULL, portno, UHF_C_PORT_ENABLE);
1011 /* most likely the HUB is gone */
1014 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1016 * Ignore the port error if the device
1019 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1020 DPRINTFN(0, "illegal enable change, "
1021 "port %d\n", portno);
1024 if (up->restartcnt == USB_RESTART_MAX) {
1025 /* XXX could try another speed ? */
1026 DPRINTFN(0, "port error, giving up "
1027 "port %d\n", portno);
1029 sc->sc_st.port_change |=
1030 UPS_C_CONNECT_STATUS;
1035 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1036 err = uhub_reattach_port(sc, portno);
1038 /* most likely the HUB is gone */
1042 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1043 UPS_C_PORT_LINK_STATE)) {
1044 err = uhub_suspend_resume_port(sc, portno);
1046 /* most likely the HUB is gone */
1050 err = uhub_explore_sub(sc, up);
1052 /* no device(s) present */
1055 /* explore succeeded - reset restart counter */
1060 usbd_enum_unlock(udev);
1062 /* initial status checked */
1063 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1065 /* return success */
1066 return (USB_ERR_NORMAL_COMPLETION);
1070 uhub_probe(device_t dev)
1072 struct usb_attach_arg *uaa = device_get_ivars(dev);
1074 if (uaa->usb_mode != USB_MODE_HOST)
1078 * The subclass for USB HUBs is currently ignored because it
1079 * is 0 for some and 1 for others.
1081 if (uaa->info.bConfigIndex == 0 &&
1082 uaa->info.bDeviceClass == UDCLASS_HUB)
1088 /* NOTE: The information returned by this function can be wrong. */
1090 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1092 struct usb_hub_descriptor hubdesc20;
1093 struct usb_hub_ss_descriptor hubdesc30;
1098 if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1099 return (USB_ERR_INVAL);
1104 switch (udev->speed) {
1106 case USB_SPEED_FULL:
1107 case USB_SPEED_HIGH:
1108 /* assuming that there is one port */
1109 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1111 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1112 "error=%s\n", usbd_errstr(err));
1115 nports = hubdesc20.bNbrPorts;
1119 if (udev->speed == USB_SPEED_HIGH)
1120 tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1123 case USB_SPEED_SUPER:
1124 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1126 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1127 "error=%s\n", usbd_errstr(err));
1130 nports = hubdesc30.bNbrPorts;
1136 err = USB_ERR_INVAL;
1140 if (pnports != NULL)
1150 uhub_attach(device_t dev)
1152 struct uhub_softc *sc = device_get_softc(dev);
1153 struct usb_attach_arg *uaa = device_get_ivars(dev);
1154 struct usb_device *udev = uaa->device;
1155 struct usb_device *parent_hub = udev->parent_hub;
1156 struct usb_hub *hub;
1157 struct usb_hub_descriptor hubdesc20;
1158 struct usb_hub_ss_descriptor hubdesc30;
1164 uint8_t iface_index;
1170 mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1172 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
1173 device_get_nameunit(dev));
1175 device_set_usb_desc(dev);
1177 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1178 "parent->selfpowered=%d\n",
1180 udev->flags.self_powered,
1183 parent_hub->flags.self_powered : 0);
1185 if (uhub_is_too_deep(udev)) {
1186 DPRINTFN(0, "HUB at depth %d, "
1187 "exceeds maximum. HUB ignored\n", (int)udev->depth);
1191 if (!udev->flags.self_powered && parent_hub &&
1192 !parent_hub->flags.self_powered) {
1193 DPRINTFN(0, "Bus powered HUB connected to "
1194 "bus powered HUB. HUB ignored\n");
1198 if (UHUB_IS_MULTI_TT(sc)) {
1199 err = usbd_set_alt_interface_index(udev, 0, 1);
1201 device_printf(dev, "MTT could not be enabled\n");
1204 device_printf(dev, "MTT enabled\n");
1207 /* get HUB descriptor */
1209 DPRINTFN(2, "Getting HUB descriptor\n");
1211 switch (udev->speed) {
1213 case USB_SPEED_FULL:
1214 case USB_SPEED_HIGH:
1215 /* assuming that there is one port */
1216 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1218 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1219 "error=%s\n", usbd_errstr(err));
1222 /* get number of ports */
1223 nports = hubdesc20.bNbrPorts;
1225 /* get power delay */
1226 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1227 usb_extra_power_up_time);
1229 /* get complete HUB descriptor */
1231 /* check number of ports */
1233 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1234 "error=%s\n", usbd_errstr(err));
1237 /* get complete HUB descriptor */
1238 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1241 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1242 "error=%s\n", usbd_errstr(err));
1245 if (hubdesc20.bNbrPorts != nports) {
1246 DPRINTFN(0, "Number of ports changed\n");
1251 case USB_SPEED_SUPER:
1252 if (udev->parent_hub != NULL) {
1253 err = usbd_req_set_hub_depth(udev, NULL,
1256 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1257 "error=%s\n", usbd_errstr(err));
1261 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1263 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1264 "error=%s\n", usbd_errstr(err));
1267 /* get number of ports */
1268 nports = hubdesc30.bNbrPorts;
1270 /* get power delay */
1271 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1272 usb_extra_power_up_time);
1274 /* get complete HUB descriptor */
1276 /* check number of ports */
1277 if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1278 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1279 "error=%s\n", usbd_errstr(err));
1282 /* get complete HUB descriptor */
1283 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1286 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1287 "error=%s\n", usbd_errstr(err));
1290 if (hubdesc30.bNbrPorts != nports) {
1291 DPRINTFN(0, "Number of ports changed\n");
1297 DPRINTF("Assuming HUB has only one port\n");
1298 /* default number of ports */
1300 /* default power delay */
1301 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1305 DPRINTFN(0, "portless HUB\n");
1308 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1309 M_USBDEV, M_WAITOK | M_ZERO);
1316 /* initialize HUB structure */
1318 hub->explore = &uhub_explore;
1319 hub->nports = nports;
1320 hub->hubudev = udev;
1321 #if USB_HAVE_TT_SUPPORT
1322 hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1323 hub->tt_msg[0].udev = udev;
1324 hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1325 hub->tt_msg[1].udev = udev;
1327 /* if self powered hub, give ports maximum current */
1328 if (udev->flags.self_powered) {
1329 hub->portpower = USB_MAX_POWER;
1331 hub->portpower = USB_MIN_POWER;
1334 /* set up interrupt pipe */
1336 if (udev->parent_hub == NULL) {
1337 /* root HUB is special */
1341 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1342 uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1345 DPRINTFN(0, "cannot setup interrupt transfer, "
1346 "errstr=%s\n", usbd_errstr(err));
1349 /* wait with power off for a while */
1350 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1353 * To have the best chance of success we do things in the exact same
1354 * order as Windoze98. This should not be necessary, but some
1355 * devices do not follow the USB specs to the letter.
1357 * These are the events on the bus when a hub is attached:
1358 * Get device and config descriptors (see attach code)
1359 * Get hub descriptor (see above)
1362 * wait for power to become stable
1363 * (all below happens in explore code)
1365 * clear C_PORT_CONNECTION
1368 * if device connected
1372 * clear C_PORT_RESET
1374 * proceed with device attachment
1377 /* XXX should check for none, individual, or ganged power? */
1381 for (x = 0; x != nports; x++) {
1382 /* set up data structures */
1383 struct usb_port *up = hub->ports + x;
1385 up->device_index = 0;
1389 /* check if port is removable */
1390 switch (udev->speed) {
1392 case USB_SPEED_FULL:
1393 case USB_SPEED_HIGH:
1394 if (!UHD_NOT_REMOV(&hubdesc20, portno))
1397 case USB_SPEED_SUPER:
1398 if (!UHD_NOT_REMOV(&hubdesc30, portno))
1402 DPRINTF("Assuming removable port\n");
1407 /* turn the power on */
1408 err = usbd_req_set_port_feature(udev, NULL,
1409 portno, UHF_PORT_POWER);
1412 DPRINTFN(0, "port %d power on failed, %s\n",
1413 portno, usbd_errstr(err));
1415 DPRINTF("turn on port %d power\n",
1418 /* wait for stable power */
1419 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1422 device_printf(dev, "%d port%s with %d "
1423 "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1424 removable, udev->flags.self_powered ? "self" : "bus");
1426 /* Start the interrupt endpoint, if any */
1428 mtx_lock(&sc->sc_mtx);
1429 usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1430 mtx_unlock(&sc->sc_mtx);
1432 /* Enable automatic power save on all USB HUBs */
1434 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1439 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1442 free(udev->hub, M_USBDEV);
1446 mtx_destroy(&sc->sc_mtx);
1452 * Called from process context when the hub is gone.
1453 * Detach all devices on active ports.
1456 uhub_detach(device_t dev)
1458 struct uhub_softc *sc = device_get_softc(dev);
1459 struct usb_hub *hub = sc->sc_udev->hub;
1460 struct usb_bus *bus = sc->sc_udev->bus;
1461 struct usb_device *child;
1464 if (hub == NULL) /* must be partially working */
1467 /* Make sure interrupt transfer is gone. */
1468 usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1470 /* Detach all ports */
1471 for (x = 0; x != hub->nports; x++) {
1473 child = usb_bus_port_get_device(bus, hub->ports + x);
1475 if (child == NULL) {
1480 * Free USB device and all subdevices, if any.
1482 usb_free_device(child, 0);
1485 #if USB_HAVE_TT_SUPPORT
1486 /* Make sure our TT messages are not queued anywhere */
1488 usb_proc_mwait(&bus->non_giant_callback_proc,
1489 &hub->tt_msg[0], &hub->tt_msg[1]);
1490 USB_BUS_UNLOCK(bus);
1492 free(hub, M_USBDEV);
1493 sc->sc_udev->hub = NULL;
1495 mtx_destroy(&sc->sc_mtx);
1501 uhub_suspend(device_t dev)
1504 /* Sub-devices are not suspended here! */
1509 uhub_resume(device_t dev)
1512 /* Sub-devices are not resumed here! */
1517 uhub_driver_added(device_t dev, driver_t *driver)
1519 usb_needs_explore_all();
1523 struct usb_device *udev;
1525 uint8_t iface_index;
1529 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1530 struct hub_result *res)
1532 struct usb_interface *iface;
1533 struct usb_device *udev;
1538 nports = hub->nports;
1539 for (x = 0; x != nports; x++) {
1540 udev = usb_bus_port_get_device(hub->hubudev->bus,
1545 for (i = 0; i != USB_IFACE_MAX; i++) {
1546 iface = usbd_get_iface(udev, i);
1548 (iface->subdev == child)) {
1549 res->iface_index = i;
1551 res->portno = x + 1;
1556 res->iface_index = 0;
1562 uhub_child_location_string(device_t parent, device_t child,
1563 char *buf, size_t buflen)
1565 struct uhub_softc *sc;
1566 struct usb_hub *hub;
1567 struct hub_result res;
1569 if (!device_is_attached(parent)) {
1575 sc = device_get_softc(parent);
1576 hub = sc->sc_udev->hub;
1579 uhub_find_iface_index(hub, child, &res);
1581 DPRINTF("device not on hub\n");
1587 snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1588 (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1589 res.portno, device_get_unit(res.udev->bus->bdev),
1590 res.udev->device_index, res.iface_index);
1598 uhub_child_pnpinfo_string(device_t parent, device_t child,
1599 char *buf, size_t buflen)
1601 struct uhub_softc *sc;
1602 struct usb_hub *hub;
1603 struct usb_interface *iface;
1604 struct hub_result res;
1606 if (!device_is_attached(parent)) {
1612 sc = device_get_softc(parent);
1613 hub = sc->sc_udev->hub;
1616 uhub_find_iface_index(hub, child, &res);
1618 DPRINTF("device not on hub\n");
1624 iface = usbd_get_iface(res.udev, res.iface_index);
1625 if (iface && iface->idesc) {
1626 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1627 "devclass=0x%02x devsubclass=0x%02x "
1631 "intclass=0x%02x intsubclass=0x%02x "
1632 "intprotocol=0x%02x " "%s%s",
1633 UGETW(res.udev->ddesc.idVendor),
1634 UGETW(res.udev->ddesc.idProduct),
1635 res.udev->ddesc.bDeviceClass,
1636 res.udev->ddesc.bDeviceSubClass,
1637 usb_get_serial(res.udev),
1638 UGETW(res.udev->ddesc.bcdDevice),
1639 (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1640 iface->idesc->bInterfaceClass,
1641 iface->idesc->bInterfaceSubClass,
1642 iface->idesc->bInterfaceProtocol,
1643 iface->pnpinfo ? " " : "",
1644 iface->pnpinfo ? iface->pnpinfo : "");
1658 * The USB Transaction Translator:
1659 * ===============================
1661 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1662 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1663 * USB transfers. To utilize bandwidth dynamically the "scatter and
1664 * gather" principle must be applied. This means that bandwidth must
1665 * be divided into equal parts of bandwidth. With regard to USB all
1666 * data is transferred in smaller packets with length
1667 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1670 * The bandwidth scheduler which I have implemented will simply pack
1671 * the USB transfers back to back until there is no more space in the
1672 * schedule. Out of the 8 microframes which the USB 2.0 standard
1673 * provides, only 6 are available for non-HIGH-speed devices. I have
1674 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1675 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1676 * this division, it is very difficult to allocate and free bandwidth
1679 * NOTE about the Transaction Translator in USB HUBs:
1681 * USB HUBs have a very simple Transaction Translator, that will
1682 * simply pipeline all the SPLIT transactions. That means that the
1683 * transactions will be executed in the order they are queued!
1687 /*------------------------------------------------------------------------*
1688 * usb_intr_find_best_slot
1691 * The best Transaction Translation slot for an interrupt endpoint.
1692 *------------------------------------------------------------------------*/
1694 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1695 uint8_t end, uint8_t mask)
1697 usb_size_t min = (usb_size_t)-1;
1705 /* find the last slot with lesser used bandwidth */
1707 for (x = start; x < end; x++) {
1711 /* compute sum of bandwidth */
1712 for (z = x; z < end; z++) {
1713 if (mask & (1U << (z - x)))
1717 /* check if the current multi-slot is more optimal */
1723 /* check if the mask is about to be shifted out */
1724 if (mask & (1U << (end - 1 - x)))
1730 /*------------------------------------------------------------------------*
1731 * usb_hs_bandwidth_adjust
1733 * This function will update the bandwith usage for the microframe
1734 * having index "slot" by "len" bytes. "len" can be negative. If the
1735 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1736 * the "slot" argument will be replaced by the slot having least used
1737 * bandwidth. The "mask" argument is used for multi-slot allocations.
1740 * The slot in which the bandwidth update was done: 0..7
1741 *------------------------------------------------------------------------*/
1743 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1744 uint8_t slot, uint8_t mask)
1746 struct usb_bus *bus = udev->bus;
1747 struct usb_hub *hub;
1748 enum usb_dev_speed speed;
1751 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1753 speed = usbd_get_speed(udev);
1757 case USB_SPEED_FULL:
1758 if (speed == USB_SPEED_LOW) {
1762 * The Host Controller Driver should have
1763 * performed checks so that the lookup
1764 * below does not result in a NULL pointer
1768 hub = udev->parent_hs_hub->hub;
1769 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1770 slot = usb_intr_find_best_slot(hub->uframe_usage,
1771 USB_FS_ISOC_UFRAME_MAX, 6, mask);
1773 for (x = slot; x < 8; x++) {
1774 if (mask & (1U << (x - slot))) {
1775 hub->uframe_usage[x] += len;
1776 bus->uframe_usage[x] += len;
1781 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1782 slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1783 USB_HS_MICRO_FRAMES_MAX, mask);
1785 for (x = slot; x < 8; x++) {
1786 if (mask & (1U << (x - slot))) {
1787 bus->uframe_usage[x] += len;
1795 /*------------------------------------------------------------------------*
1796 * usb_hs_bandwidth_alloc
1798 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1799 *------------------------------------------------------------------------*/
1801 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1803 struct usb_device *udev;
1808 udev = xfer->xroot->udev;
1810 if (udev->flags.usb_mode != USB_MODE_HOST)
1811 return; /* not supported */
1813 xfer->endpoint->refcount_bw++;
1814 if (xfer->endpoint->refcount_bw != 1)
1815 return; /* already allocated */
1817 speed = usbd_get_speed(udev);
1819 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1821 /* allocate a microframe slot */
1824 slot = usb_hs_bandwidth_adjust(udev,
1825 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1827 xfer->endpoint->usb_uframe = slot;
1828 xfer->endpoint->usb_smask = mask << slot;
1830 if ((speed != USB_SPEED_FULL) &&
1831 (speed != USB_SPEED_LOW)) {
1832 xfer->endpoint->usb_cmask = 0x00 ;
1834 xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1838 case UE_ISOCHRONOUS:
1839 switch (usbd_xfer_get_fps_shift(xfer)) {
1854 /* allocate a microframe multi-slot */
1856 slot = usb_hs_bandwidth_adjust(udev,
1857 xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1859 xfer->endpoint->usb_uframe = slot;
1860 xfer->endpoint->usb_cmask = 0;
1861 xfer->endpoint->usb_smask = mask << slot;
1865 xfer->endpoint->usb_uframe = 0;
1866 xfer->endpoint->usb_cmask = 0;
1867 xfer->endpoint->usb_smask = 0;
1871 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1872 xfer->endpoint->usb_uframe,
1873 xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1876 /*------------------------------------------------------------------------*
1877 * usb_hs_bandwidth_free
1879 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1880 *------------------------------------------------------------------------*/
1882 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1884 struct usb_device *udev;
1888 udev = xfer->xroot->udev;
1890 if (udev->flags.usb_mode != USB_MODE_HOST)
1891 return; /* not supported */
1893 xfer->endpoint->refcount_bw--;
1894 if (xfer->endpoint->refcount_bw != 0)
1895 return; /* still allocated */
1897 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1899 case UE_ISOCHRONOUS:
1901 slot = xfer->endpoint->usb_uframe;
1902 mask = xfer->endpoint->usb_smask;
1904 /* free microframe slot(s): */
1905 usb_hs_bandwidth_adjust(udev,
1906 -xfer->max_frame_size, slot, mask >> slot);
1908 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1909 slot, mask >> slot);
1911 xfer->endpoint->usb_uframe = 0;
1912 xfer->endpoint->usb_cmask = 0;
1913 xfer->endpoint->usb_smask = 0;
1921 /*------------------------------------------------------------------------*
1922 * usb_isoc_time_expand
1924 * This function will expand the time counter from 7-bit to 16-bit.
1927 * 16-bit isochronous time counter.
1928 *------------------------------------------------------------------------*/
1930 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
1934 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1936 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
1938 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
1940 if (isoc_time_curr < rem) {
1941 /* the time counter wrapped around */
1942 bus->isoc_time_last += USB_ISOC_TIME_MAX;
1944 /* update the remainder */
1946 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
1947 bus->isoc_time_last |= isoc_time_curr;
1949 return (bus->isoc_time_last);
1952 /*------------------------------------------------------------------------*
1953 * usbd_fs_isoc_schedule_alloc_slot
1955 * This function will allocate bandwidth for an isochronous FULL speed
1956 * transaction in the FULL speed schedule.
1961 *------------------------------------------------------------------------*/
1962 #if USB_HAVE_TT_SUPPORT
1964 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
1966 struct usb_xfer *xfer;
1967 struct usb_xfer *pipe_xfer;
1968 struct usb_bus *bus;
1970 usb_frlength_t data_len;
1978 bus = isoc_xfer->xroot->bus;
1980 TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
1982 /* skip self, if any */
1984 if (xfer == isoc_xfer)
1987 /* check if this USB transfer is going through the same TT */
1989 if (xfer->xroot->udev->parent_hs_hub !=
1990 isoc_xfer->xroot->udev->parent_hs_hub) {
1993 if ((isoc_xfer->xroot->udev->parent_hs_hub->
1994 ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
1995 (xfer->xroot->udev->hs_port_no !=
1996 isoc_xfer->xroot->udev->hs_port_no)) {
1999 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2002 /* check if isoc_time is part of this transfer */
2004 delta = xfer->isoc_time_complete - isoc_time;
2005 if (delta > 0 && delta <= xfer->nframes) {
2006 delta = xfer->nframes - delta;
2008 len = xfer->frlengths[delta];
2016 /* check double buffered transfers */
2018 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q.head,
2021 /* skip self, if any */
2023 if (pipe_xfer == isoc_xfer)
2026 /* check if isoc_time is part of this transfer */
2028 delta = pipe_xfer->isoc_time_complete - isoc_time;
2029 if (delta > 0 && delta <= pipe_xfer->nframes) {
2030 delta = pipe_xfer->nframes - delta;
2032 len = pipe_xfer->frlengths[delta];
2042 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2043 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2047 /* check for overflow */
2049 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2054 delta = isoc_xfer->isoc_time_complete - isoc_time;
2055 if (delta > 0 && delta <= isoc_xfer->nframes) {
2056 delta = isoc_xfer->nframes - delta;
2058 len = isoc_xfer->frlengths[delta];
2066 while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2067 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2071 /* check for overflow */
2073 if (slot >= USB_FS_ISOC_UFRAME_MAX)
2080 /*------------------------------------------------------------------------*
2081 * usb_bus_port_get_device
2083 * This function is NULL safe.
2084 *------------------------------------------------------------------------*/
2086 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2088 if ((bus == NULL) || (up == NULL)) {
2092 if (up->device_index == 0) {
2096 return (bus->devices[up->device_index]);
2099 /*------------------------------------------------------------------------*
2100 * usb_bus_port_set_device
2102 * This function is NULL safe.
2103 *------------------------------------------------------------------------*/
2105 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2106 struct usb_device *udev, uint8_t device_index)
2113 * There is only one case where we don't
2114 * have an USB port, and that is the Root Hub!
2118 up->device_index = device_index;
2120 device_index = up->device_index;
2121 up->device_index = 0;
2125 * Make relationships to our new device
2127 if (device_index != 0) {
2129 mtx_lock(&usb_ref_lock);
2131 bus->devices[device_index] = udev;
2133 mtx_unlock(&usb_ref_lock);
2139 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2142 /*------------------------------------------------------------------------*
2145 * This functions is called when the USB event thread needs to run.
2146 *------------------------------------------------------------------------*/
2148 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2155 DPRINTF("No bus pointer!\n");
2158 if ((bus->devices == NULL) ||
2159 (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2160 DPRINTF("No root HUB\n");
2163 if (mtx_owned(&bus->bus_mtx)) {
2172 if (usb_proc_msignal(&bus->explore_proc,
2173 &bus->explore_msg[0], &bus->explore_msg[1])) {
2177 USB_BUS_UNLOCK(bus);
2181 /*------------------------------------------------------------------------*
2182 * usb_needs_explore_all
2184 * This function is called whenever a new driver is loaded and will
2185 * cause that all USB busses are re-explored.
2186 *------------------------------------------------------------------------*/
2188 usb_needs_explore_all(void)
2190 struct usb_bus *bus;
2197 dc = usb_devclass_ptr;
2199 DPRINTFN(0, "no devclass\n");
2203 * Explore all USB busses in parallell.
2205 max = devclass_get_maxunit(dc);
2207 dev = devclass_get_device(dc, max);
2209 bus = device_get_softc(dev);
2211 usb_needs_explore(bus, 1);
2218 /*------------------------------------------------------------------------*
2219 * usb_bus_power_update
2221 * This function will ensure that all USB devices on the given bus are
2222 * properly suspended or resumed according to the device transfer
2224 *------------------------------------------------------------------------*/
2227 usb_bus_power_update(struct usb_bus *bus)
2229 usb_needs_explore(bus, 0 /* no probe */ );
2233 /*------------------------------------------------------------------------*
2234 * usbd_transfer_power_ref
2236 * This function will modify the power save reference counts and
2237 * wakeup the USB device associated with the given USB transfer, if
2239 *------------------------------------------------------------------------*/
2242 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2244 static const usb_power_mask_t power_mask[4] = {
2245 [UE_CONTROL] = USB_HW_POWER_CONTROL,
2246 [UE_BULK] = USB_HW_POWER_BULK,
2247 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2248 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2250 struct usb_device *udev;
2251 uint8_t needs_explore;
2252 uint8_t needs_hw_power;
2255 udev = xfer->xroot->udev;
2257 if (udev->device_index == USB_ROOT_HUB_ADDR) {
2258 /* no power save for root HUB */
2261 USB_BUS_LOCK(udev->bus);
2263 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2265 udev->pwr_save.last_xfer_time = ticks;
2266 udev->pwr_save.type_refs[xfer_type] += val;
2268 if (xfer->flags_int.control_xfr) {
2269 udev->pwr_save.read_refs += val;
2270 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2272 * It is not allowed to suspend during a
2275 udev->pwr_save.write_refs += val;
2277 } else if (USB_GET_DATA_ISREAD(xfer)) {
2278 udev->pwr_save.read_refs += val;
2280 udev->pwr_save.write_refs += val;
2284 if (udev->flags.self_suspended)
2285 needs_explore = usb_peer_should_wakeup(udev);
2289 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2290 DPRINTF("Adding type %u to power state\n", xfer_type);
2291 udev->bus->hw_power_state |= power_mask[xfer_type];
2301 USB_BUS_UNLOCK(udev->bus);
2303 if (needs_explore) {
2304 DPRINTF("update\n");
2305 usb_bus_power_update(udev->bus);
2306 } else if (needs_hw_power) {
2307 DPRINTF("needs power\n");
2308 if (udev->bus->methods->set_hw_power != NULL) {
2309 (udev->bus->methods->set_hw_power) (udev->bus);
2315 /*------------------------------------------------------------------------*
2316 * usb_peer_should_wakeup
2318 * This function returns non-zero if the current device should wake up.
2319 *------------------------------------------------------------------------*/
2321 usb_peer_should_wakeup(struct usb_device *udev)
2323 return (((udev->power_mode == USB_POWER_MODE_ON) &&
2324 (udev->flags.usb_mode == USB_MODE_HOST)) ||
2325 (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2326 (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2327 (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2328 (udev->pwr_save.write_refs != 0) ||
2329 ((udev->pwr_save.read_refs != 0) &&
2330 (udev->flags.usb_mode == USB_MODE_HOST) &&
2331 (usb_peer_can_wakeup(udev) == 0)));
2334 /*------------------------------------------------------------------------*
2337 * This function implements the USB power daemon and is called
2338 * regularly from the USB explore thread.
2339 *------------------------------------------------------------------------*/
2342 usb_bus_powerd(struct usb_bus *bus)
2344 struct usb_device *udev;
2347 usb_ticks_t mintime;
2348 usb_size_t type_refs[5];
2351 limit = usb_power_timeout;
2354 else if (limit > 255)
2359 DPRINTF("bus=%p\n", bus);
2364 * The root HUB device is never suspended
2365 * and we simply skip it.
2367 for (x = USB_ROOT_HUB_ADDR + 1;
2368 x != bus->devices_max; x++) {
2370 udev = bus->devices[x];
2374 temp = ticks - udev->pwr_save.last_xfer_time;
2376 if (usb_peer_should_wakeup(udev)) {
2377 /* check if we are suspended */
2378 if (udev->flags.self_suspended != 0) {
2379 USB_BUS_UNLOCK(bus);
2380 usb_dev_resume_peer(udev);
2383 } else if ((temp >= limit) &&
2384 (udev->flags.usb_mode == USB_MODE_HOST) &&
2385 (udev->flags.self_suspended == 0)) {
2386 /* try to do suspend */
2388 USB_BUS_UNLOCK(bus);
2389 usb_dev_suspend_peer(udev);
2394 /* reset counters */
2396 mintime = (usb_ticks_t)-1;
2403 /* Re-loop all the devices to get the actual state */
2405 for (x = USB_ROOT_HUB_ADDR + 1;
2406 x != bus->devices_max; x++) {
2408 udev = bus->devices[x];
2412 /* we found a non-Root-Hub USB device */
2415 /* "last_xfer_time" can be updated by a resume */
2416 temp = ticks - udev->pwr_save.last_xfer_time;
2419 * Compute minimum time since last transfer for the complete
2425 if (udev->flags.self_suspended == 0) {
2426 type_refs[0] += udev->pwr_save.type_refs[0];
2427 type_refs[1] += udev->pwr_save.type_refs[1];
2428 type_refs[2] += udev->pwr_save.type_refs[2];
2429 type_refs[3] += udev->pwr_save.type_refs[3];
2433 if (mintime >= (usb_ticks_t)(1 * hz)) {
2434 /* recompute power masks */
2435 DPRINTF("Recomputing power masks\n");
2436 bus->hw_power_state = 0;
2437 if (type_refs[UE_CONTROL] != 0)
2438 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2439 if (type_refs[UE_BULK] != 0)
2440 bus->hw_power_state |= USB_HW_POWER_BULK;
2441 if (type_refs[UE_INTERRUPT] != 0)
2442 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2443 if (type_refs[UE_ISOCHRONOUS] != 0)
2444 bus->hw_power_state |= USB_HW_POWER_ISOC;
2445 if (type_refs[4] != 0)
2446 bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2448 USB_BUS_UNLOCK(bus);
2450 if (bus->methods->set_hw_power != NULL) {
2451 /* always update hardware power! */
2452 (bus->methods->set_hw_power) (bus);
2458 /*------------------------------------------------------------------------*
2459 * usb_dev_resume_peer
2461 * This function will resume an USB peer and do the required USB
2462 * signalling to get an USB device out of the suspended state.
2463 *------------------------------------------------------------------------*/
2465 usb_dev_resume_peer(struct usb_device *udev)
2467 struct usb_bus *bus;
2474 /* check if already resumed */
2475 if (udev->flags.self_suspended == 0)
2478 /* we need a parent HUB to do resume */
2479 if (udev->parent_hub == NULL)
2482 DPRINTF("udev=%p\n", udev);
2484 if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2485 (udev->flags.remote_wakeup == 0)) {
2487 * If the host did not set the remote wakeup feature, we can
2488 * not wake it up either!
2490 DPRINTF("remote wakeup is not set!\n");
2493 /* get bus pointer */
2496 /* resume parent hub first */
2497 usb_dev_resume_peer(udev->parent_hub);
2499 /* reduce chance of instant resume failure by waiting a little bit */
2500 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2502 if (usb_device_20_compatible(udev)) {
2503 /* resume current port (Valid in Host and Device Mode) */
2504 err = usbd_req_clear_port_feature(udev->parent_hub,
2505 NULL, udev->port_no, UHF_PORT_SUSPEND);
2507 DPRINTFN(0, "Resuming port failed\n");
2511 /* resume current port (Valid in Host and Device Mode) */
2512 err = usbd_req_set_port_link_state(udev->parent_hub,
2513 NULL, udev->port_no, UPS_PORT_LS_U0);
2515 DPRINTFN(0, "Resuming port failed\n");
2520 /* resume settle time */
2521 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2523 if (bus->methods->device_resume != NULL) {
2524 /* resume USB device on the USB controller */
2525 (bus->methods->device_resume) (udev);
2528 /* set that this device is now resumed */
2529 udev->flags.self_suspended = 0;
2531 /* make sure that we don't go into suspend right away */
2532 udev->pwr_save.last_xfer_time = ticks;
2534 /* make sure the needed power masks are on */
2535 if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2536 bus->hw_power_state |= USB_HW_POWER_CONTROL;
2537 if (udev->pwr_save.type_refs[UE_BULK] != 0)
2538 bus->hw_power_state |= USB_HW_POWER_BULK;
2539 if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2540 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2541 if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2542 bus->hw_power_state |= USB_HW_POWER_ISOC;
2544 USB_BUS_UNLOCK(bus);
2546 if (bus->methods->set_hw_power != NULL) {
2547 /* always update hardware power! */
2548 (bus->methods->set_hw_power) (bus);
2553 /* notify all sub-devices about resume */
2554 err = usb_suspend_resume(udev, 0);
2556 usbd_sr_unlock(udev);
2558 /* check if peer has wakeup capability */
2559 if (usb_peer_can_wakeup(udev)) {
2560 /* clear remote wakeup */
2561 err = usbd_req_clear_device_feature(udev,
2562 NULL, UF_DEVICE_REMOTE_WAKEUP);
2564 DPRINTFN(0, "Clearing device "
2565 "remote wakeup failed: %s\n",
2571 /*------------------------------------------------------------------------*
2572 * usb_dev_suspend_peer
2574 * This function will suspend an USB peer and do the required USB
2575 * signalling to get an USB device into the suspended state.
2576 *------------------------------------------------------------------------*/
2578 usb_dev_suspend_peer(struct usb_device *udev)
2580 struct usb_device *child;
2590 /* check if already suspended */
2591 if (udev->flags.self_suspended)
2594 /* we need a parent HUB to do suspend */
2595 if (udev->parent_hub == NULL)
2598 DPRINTF("udev=%p\n", udev);
2600 /* check if the current device is a HUB */
2601 if (udev->hub != NULL) {
2602 nports = udev->hub->nports;
2604 /* check if all devices on the HUB are suspended */
2605 for (x = 0; x != nports; x++) {
2606 child = usb_bus_port_get_device(udev->bus,
2607 udev->hub->ports + x);
2612 if (child->flags.self_suspended)
2615 DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2620 if (usb_peer_can_wakeup(udev)) {
2622 * This request needs to be done before we set
2623 * "udev->flags.self_suspended":
2626 /* allow device to do remote wakeup */
2627 err = usbd_req_set_device_feature(udev,
2628 NULL, UF_DEVICE_REMOTE_WAKEUP);
2630 DPRINTFN(0, "Setting device "
2631 "remote wakeup failed\n");
2635 USB_BUS_LOCK(udev->bus);
2637 * Checking for suspend condition and setting suspended bit
2640 err = usb_peer_should_wakeup(udev);
2643 * Set that this device is suspended. This variable
2644 * must be set before calling USB controller suspend
2647 udev->flags.self_suspended = 1;
2649 USB_BUS_UNLOCK(udev->bus);
2652 if (usb_peer_can_wakeup(udev)) {
2653 /* allow device to do remote wakeup */
2654 err = usbd_req_clear_device_feature(udev,
2655 NULL, UF_DEVICE_REMOTE_WAKEUP);
2657 DPRINTFN(0, "Setting device "
2658 "remote wakeup failed\n");
2662 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2663 /* resume parent HUB first */
2664 usb_dev_resume_peer(udev->parent_hub);
2666 /* reduce chance of instant resume failure by waiting a little bit */
2667 usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2669 /* resume current port (Valid in Host and Device Mode) */
2670 err = usbd_req_clear_port_feature(udev->parent_hub,
2671 NULL, udev->port_no, UHF_PORT_SUSPEND);
2673 /* resume settle time */
2674 usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2676 DPRINTF("Suspend was cancelled!\n");
2682 /* notify all sub-devices about suspend */
2683 err = usb_suspend_resume(udev, 1);
2685 usbd_sr_unlock(udev);
2687 if (udev->bus->methods->device_suspend != NULL) {
2690 /* suspend device on the USB controller */
2691 (udev->bus->methods->device_suspend) (udev);
2694 temp = usbd_get_dma_delay(udev);
2696 usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2700 if (usb_device_20_compatible(udev)) {
2701 /* suspend current port */
2702 err = usbd_req_set_port_feature(udev->parent_hub,
2703 NULL, udev->port_no, UHF_PORT_SUSPEND);
2705 DPRINTFN(0, "Suspending port failed\n");
2709 /* suspend current port */
2710 err = usbd_req_set_port_link_state(udev->parent_hub,
2711 NULL, udev->port_no, UPS_PORT_LS_U3);
2713 DPRINTFN(0, "Suspending port failed\n");
2718 udev = udev->parent_hub;
2722 /*------------------------------------------------------------------------*
2723 * usbd_set_power_mode
2725 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2727 *------------------------------------------------------------------------*/
2729 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2731 /* filter input argument */
2732 if ((power_mode != USB_POWER_MODE_ON) &&
2733 (power_mode != USB_POWER_MODE_OFF))
2734 power_mode = USB_POWER_MODE_SAVE;
2736 power_mode = usbd_filter_power_mode(udev, power_mode);
2738 udev->power_mode = power_mode; /* update copy of power mode */
2741 usb_bus_power_update(udev->bus);
2743 usb_needs_explore(udev->bus, 0 /* no probe */ );
2747 /*------------------------------------------------------------------------*
2748 * usbd_filter_power_mode
2750 * This function filters the power mode based on hardware requirements.
2751 *------------------------------------------------------------------------*/
2753 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2755 struct usb_bus_methods *mtod;
2758 mtod = udev->bus->methods;
2761 if (mtod->get_power_mode != NULL)
2762 (mtod->get_power_mode) (udev, &temp);
2764 /* check if we should not filter */
2766 return (power_mode);
2768 /* use fixed power mode given by hardware driver */
2772 /*------------------------------------------------------------------------*
2773 * usbd_start_re_enumerate
2775 * This function starts re-enumeration of the given USB device. This
2776 * function does not need to be called BUS-locked. This function does
2777 * not wait until the re-enumeration is completed.
2778 *------------------------------------------------------------------------*/
2780 usbd_start_re_enumerate(struct usb_device *udev)
2782 if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2783 udev->re_enumerate_wait = USB_RE_ENUM_START;
2784 usb_needs_explore(udev->bus, 0);