1 /* $FreeBSD: head/sys/dev/usb/usb_hub.c 276701 2015-01-05 15:04:17Z hselasky $ */
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/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usb_ioctl.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usbdi_util.h>
55 #define USB_DEBUG_VAR uhub_debug
57 #include <bus/u4b/usb_core.h>
58 #include <bus/u4b/usb_process.h>
59 #include <bus/u4b/usb_device.h>
60 #include <bus/u4b/usb_request.h>
61 #include <bus/u4b/usb_debug.h>
62 #include <bus/u4b/usb_hub.h>
63 #include <bus/u4b/usb_util.h>
64 #include <bus/u4b/usb_busdma.h>
65 #include <bus/u4b/usb_transfer.h>
66 #include <bus/u4b/usb_dynamic.h>
68 #include <bus/u4b/usb_controller.h>
69 #include <bus/u4b/usb_bus.h>
71 #define UHUB_INTR_INTERVAL 250 /* ms */
74 #if USB_HAVE_TT_SUPPORT
75 UHUB_RESET_TT_TRANSFER
,
81 static int uhub_debug
= 0;
83 static SYSCTL_NODE(_hw_usb
, OID_AUTO
, uhub
, CTLFLAG_RW
, 0, "USB HUB");
84 SYSCTL_INT(_hw_usb_uhub
, OID_AUTO
, debug
, CTLFLAG_RW
, &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");
95 TUNABLE_INT("hw.usb.power_timeout", &usb_power_timeout
);
98 #if USB_HAVE_DISABLE_ENUM
99 static int usb_disable_enumeration
= 0;
100 SYSCTL_INT(_hw_usb
, OID_AUTO
, disable_enumeration
, CTLFLAG_RW
,
101 &usb_disable_enumeration
, 0, "Set to disable all USB device enumeration.");
102 TUNABLE_INT("hw.usb.disable_enumeration", &usb_disable_enumeration
);
104 static int usb_disable_port_power
= 0;
105 SYSCTL_INT(_hw_usb
, OID_AUTO
, disable_port_power
, CTLFLAG_RW
,
106 &usb_disable_port_power
, 0, "Set to disable all USB port power.");
107 TUNABLE_INT("hw.usb.disable_port_power", &usb_disable_port_power
);
110 struct uhub_current_state
{
111 uint16_t port_change
;
112 uint16_t port_status
;
116 struct uhub_current_state sc_st
;/* current state */
117 #if (USB_HAVE_FIXED_PORT != 0)
118 struct usb_hub sc_hub
;
120 device_t sc_dev
; /* base device */
121 struct lock sc_lock
; /* our mutex */
122 struct usb_device
*sc_udev
; /* USB device */
123 struct usb_xfer
*sc_xfer
[UHUB_N_TRANSFER
]; /* interrupt xfer */
124 #if USB_HAVE_DISABLE_ENUM
125 int sc_disable_enumeration
;
126 int sc_disable_port_power
;
129 #define UHUB_FLAG_DID_EXPLORE 0x01
132 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
133 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
134 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
135 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
136 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
138 /* prototypes for type checking: */
140 static device_probe_t uhub_probe
;
141 static device_attach_t uhub_attach
;
142 static device_detach_t uhub_detach
;
143 static device_suspend_t uhub_suspend
;
144 static device_resume_t uhub_resume
;
146 static bus_driver_added_t uhub_driver_added
;
147 static bus_child_location_str_t uhub_child_location_string
;
148 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string
;
150 static usb_callback_t uhub_intr_callback
;
151 #if USB_HAVE_TT_SUPPORT
152 static usb_callback_t uhub_reset_tt_callback
;
155 static void usb_dev_resume_peer(struct usb_device
*udev
);
156 static void usb_dev_suspend_peer(struct usb_device
*udev
);
157 static uint8_t usb_peer_should_wakeup(struct usb_device
*udev
);
159 static const struct usb_config uhub_config
[UHUB_N_TRANSFER
] = {
161 [UHUB_INTR_TRANSFER
] = {
162 .type
= UE_INTERRUPT
,
163 .endpoint
= UE_ADDR_ANY
,
164 .direction
= UE_DIR_ANY
,
166 .flags
= {.pipe_bof
= 1,.short_xfer_ok
= 1,},
167 .bufsize
= 0, /* use wMaxPacketSize */
168 .callback
= &uhub_intr_callback
,
169 .interval
= UHUB_INTR_INTERVAL
,
171 #if USB_HAVE_TT_SUPPORT
172 [UHUB_RESET_TT_TRANSFER
] = {
174 .endpoint
= 0x00, /* Control pipe */
175 .direction
= UE_DIR_ANY
,
176 .bufsize
= sizeof(struct usb_device_request
),
177 .callback
= &uhub_reset_tt_callback
,
178 .timeout
= 1000, /* 1 second */
179 .usb_mode
= USB_MODE_HOST
,
185 * driver instance for "hub" connected to "usb"
186 * and "hub" connected to "hub"
188 static devclass_t uhub_devclass
;
190 static device_method_t uhub_methods
[] = {
191 DEVMETHOD(device_probe
, uhub_probe
),
192 DEVMETHOD(device_attach
, uhub_attach
),
193 DEVMETHOD(device_detach
, uhub_detach
),
195 DEVMETHOD(device_suspend
, uhub_suspend
),
196 DEVMETHOD(device_resume
, uhub_resume
),
198 DEVMETHOD(bus_child_location_str
, uhub_child_location_string
),
199 DEVMETHOD(bus_child_pnpinfo_str
, uhub_child_pnpinfo_string
),
200 DEVMETHOD(bus_driver_added
, uhub_driver_added
),
204 static driver_t uhub_driver
= {
206 .methods
= uhub_methods
,
207 .size
= sizeof(struct uhub_softc
)
210 DRIVER_MODULE(uhub
, usbus
, uhub_driver
, uhub_devclass
, NULL
, NULL
);
211 DRIVER_MODULE(uhub
, uhub
, uhub_driver
, uhub_devclass
, NULL
, NULL
);
212 MODULE_VERSION(uhub
, 1);
215 uhub_intr_callback(struct usb_xfer
*xfer
, usb_error_t error
)
217 struct uhub_softc
*sc
= usbd_xfer_softc(xfer
);
219 switch (USB_GET_STATE(xfer
)) {
220 case USB_ST_TRANSFERRED
:
223 * This is an indication that some port
224 * has changed status. Notify the bus
225 * event handler thread that we need
226 * to be explored again:
228 usb_needs_explore(sc
->sc_udev
->bus
, 0);
231 usbd_xfer_set_frame_len(xfer
, 0, usbd_xfer_max_len(xfer
));
232 usbd_transfer_submit(xfer
);
236 if (xfer
->error
!= USB_ERR_CANCELLED
) {
238 * Do a clear-stall. The "stall_pipe" flag
239 * will get cleared before next callback by
242 usbd_xfer_set_stall(xfer
);
243 usbd_xfer_set_frame_len(xfer
, 0, usbd_xfer_max_len(xfer
));
244 usbd_transfer_submit(xfer
);
250 /*------------------------------------------------------------------------*
253 * This function starts the TT reset USB request
254 *------------------------------------------------------------------------*/
255 #if USB_HAVE_TT_SUPPORT
257 uhub_reset_tt_proc(struct usb_proc_msg
*_pm
)
259 struct usb_udev_msg
*pm
= (void *)_pm
;
260 struct usb_device
*udev
= pm
->udev
;
262 struct uhub_softc
*sc
;
272 USB_BUS_UNLOCK(udev
->bus
);
273 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
275 usbd_transfer_start(sc
->sc_xfer
[UHUB_RESET_TT_TRANSFER
]);
277 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
278 USB_BUS_LOCK(udev
->bus
);
282 /*------------------------------------------------------------------------*
283 * uhub_tt_buffer_reset_async_locked
285 * This function queues a TT reset for the given USB device and endpoint.
286 *------------------------------------------------------------------------*/
287 #if USB_HAVE_TT_SUPPORT
289 uhub_tt_buffer_reset_async_locked(struct usb_device
*child
, struct usb_endpoint
*ep
)
291 struct usb_device_request req
;
292 struct usb_device
*udev
;
298 if (child
== NULL
|| ep
== NULL
)
301 udev
= child
->parent_hs_hub
;
302 port
= child
->hs_port_no
;
309 (udev
->speed
!= USB_SPEED_HIGH
) ||
310 (child
->speed
!= USB_SPEED_LOW
&&
311 child
->speed
!= USB_SPEED_FULL
) ||
312 (child
->flags
.usb_mode
!= USB_MODE_HOST
) ||
313 (port
== 0) || (ep
->edesc
== NULL
)) {
318 USB_BUS_LOCK_ASSERT(udev
->bus
);
320 up
= hub
->ports
+ port
- 1;
322 if (udev
->ddesc
.bDeviceClass
== UDCLASS_HUB
&&
323 udev
->ddesc
.bDeviceProtocol
== UDPROTO_HSHUBSTT
)
326 /* if we already received a clear buffer request, reset the whole TT */
327 if (up
->req_reset_tt
.bRequest
!= 0) {
328 req
.bmRequestType
= UT_WRITE_CLASS_OTHER
;
329 req
.bRequest
= UR_RESET_TT
;
330 USETW(req
.wValue
, 0);
331 req
.wIndex
[0] = port
;
333 USETW(req
.wLength
, 0);
335 wValue
= (ep
->edesc
->bEndpointAddress
& 0xF) |
336 ((child
->address
& 0x7F) << 4) |
337 ((ep
->edesc
->bEndpointAddress
& 0x80) << 8) |
338 ((ep
->edesc
->bmAttributes
& 3) << 12);
340 req
.bmRequestType
= UT_WRITE_CLASS_OTHER
;
341 req
.bRequest
= UR_CLEAR_TT_BUFFER
;
342 USETW(req
.wValue
, wValue
);
343 req
.wIndex
[0] = port
;
345 USETW(req
.wLength
, 0);
347 up
->req_reset_tt
= req
;
348 /* get reset transfer started */
349 usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev
->bus
),
350 &hub
->tt_msg
[0], &hub
->tt_msg
[1]);
354 #if USB_HAVE_TT_SUPPORT
356 uhub_reset_tt_callback(struct usb_xfer
*xfer
, usb_error_t error
)
358 struct uhub_softc
*sc
;
359 struct usb_device
*udev
;
363 DPRINTF("TT buffer reset\n");
365 sc
= usbd_xfer_softc(xfer
);
368 switch (USB_GET_STATE(xfer
)) {
369 case USB_ST_TRANSFERRED
:
372 USB_BUS_LOCK(udev
->bus
);
373 /* find first port which needs a TT reset */
374 for (x
= 0; x
!= udev
->hub
->nports
; x
++) {
375 up
= udev
->hub
->ports
+ x
;
377 if (up
->req_reset_tt
.bRequest
== 0)
380 /* copy in the transfer */
381 usbd_copy_in(xfer
->frbuffers
, 0, &up
->req_reset_tt
,
382 sizeof(up
->req_reset_tt
));
384 memset(&up
->req_reset_tt
, 0, sizeof(up
->req_reset_tt
));
387 usbd_xfer_set_frame_len(xfer
, 0, sizeof(up
->req_reset_tt
));
389 USB_BUS_UNLOCK(udev
->bus
);
391 usbd_transfer_submit(xfer
);
394 USB_BUS_UNLOCK(udev
->bus
);
398 if (error
== USB_ERR_CANCELLED
)
401 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error
));
407 /*------------------------------------------------------------------------*
408 * uhub_count_active_host_ports
410 * This function counts the number of active ports at the given speed.
411 *------------------------------------------------------------------------*/
413 uhub_count_active_host_ports(struct usb_device
*udev
, enum usb_dev_speed speed
)
415 struct uhub_softc
*sc
;
416 struct usb_device
*child
;
431 for (x
= 0; x
!= hub
->nports
; x
++) {
433 child
= usb_bus_port_get_device(udev
->bus
, up
);
435 child
->flags
.usb_mode
== USB_MODE_HOST
&&
436 child
->speed
== speed
)
444 uhub_explore_handle_re_enumerate(struct usb_device
*child
)
449 /* check if device should be re-enumerated */
450 if (child
->flags
.usb_mode
!= USB_MODE_HOST
)
453 do_unlock
= usbd_enum_lock(child
);
454 switch (child
->re_enumerate_wait
) {
455 case USB_RE_ENUM_START
:
456 err
= usbd_set_config_index(child
,
459 DPRINTF("Unconfigure failed: %s: Ignored.\n",
462 if (child
->parent_hub
== NULL
) {
463 /* the root HUB cannot be re-enumerated */
464 DPRINTFN(6, "cannot reset root HUB\n");
467 err
= usbd_req_re_enumerate(child
, NULL
);
470 err
= usbd_set_config_index(child
, 0);
472 err
= usb_probe_and_attach(child
,
473 USB_IFACE_INDEX_ANY
);
475 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");
486 if (child
->parent_hub
== NULL
) {
487 /* the root HUB cannot be re-enumerated */
488 DPRINTFN(6, "cannot set port feature\n");
491 /* clear port enable */
492 err
= usbd_req_clear_port_feature(child
->parent_hub
,
493 NULL
, child
->port_no
, UHF_PORT_ENABLE
);
495 DPRINTFN(0, "Could not disable port "
499 child
->re_enumerate_wait
= USB_RE_ENUM_DONE
;
502 case USB_RE_ENUM_SET_CONFIG
:
503 err
= usbd_set_config_index(child
,
504 child
->next_config_index
);
506 DPRINTF("Configure failed: %s: Ignored.\n",
509 err
= usb_probe_and_attach(child
,
510 USB_IFACE_INDEX_ANY
);
512 child
->re_enumerate_wait
= USB_RE_ENUM_DONE
;
516 child
->re_enumerate_wait
= USB_RE_ENUM_DONE
;
520 usbd_enum_unlock(child
);
523 /*------------------------------------------------------------------------*
524 * uhub_explore_sub - subroutine
528 * Else: A control transaction failed
529 *------------------------------------------------------------------------*/
531 uhub_explore_sub(struct uhub_softc
*sc
, struct usb_port
*up
)
534 struct usb_device
*child
;
538 bus
= sc
->sc_udev
->bus
;
541 /* get driver added refcount from USB bus */
542 refcount
= bus
->driver_added_refcount
;
544 /* get device assosiated with the given port */
545 child
= usb_bus_port_get_device(bus
, up
);
551 uhub_explore_handle_re_enumerate(child
);
553 /* check if probe and attach should be done */
555 if (child
->driver_added_refcount
!= refcount
) {
556 child
->driver_added_refcount
= refcount
;
557 err
= usb_probe_and_attach(child
,
558 USB_IFACE_INDEX_ANY
);
563 /* start control transfer, if device mode */
565 if (child
->flags
.usb_mode
== USB_MODE_DEVICE
)
566 usbd_ctrl_transfer_setup(child
);
568 /* if a HUB becomes present, do a recursive HUB explore */
571 err
= (child
->hub
->explore
) (child
);
577 /*------------------------------------------------------------------------*
578 * uhub_read_port_status - factored out code
579 *------------------------------------------------------------------------*/
581 uhub_read_port_status(struct uhub_softc
*sc
, uint8_t portno
)
583 struct usb_port_status ps
;
586 err
= usbd_req_get_port_status(
587 sc
->sc_udev
, NULL
, &ps
, portno
);
589 /* update status regardless of error */
591 sc
->sc_st
.port_status
= UGETW(ps
.wPortStatus
);
592 sc
->sc_st
.port_change
= UGETW(ps
.wPortChange
);
594 /* debugging print */
596 DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
597 "wPortChange=0x%04x, err=%s\n",
598 portno
, sc
->sc_st
.port_status
,
599 sc
->sc_st
.port_change
, usbd_errstr(err
));
603 /*------------------------------------------------------------------------*
608 * Else: A control transaction failed
609 *------------------------------------------------------------------------*/
611 uhub_reattach_port(struct uhub_softc
*sc
, uint8_t portno
)
613 struct usb_device
*child
;
614 struct usb_device
*udev
;
615 enum usb_dev_speed speed
;
616 enum usb_hc_mode mode
;
621 DPRINTF("reattaching port %d\n", portno
);
625 child
= usb_bus_port_get_device(udev
->bus
,
626 udev
->hub
->ports
+ portno
- 1);
630 /* first clear the port connection change bit */
632 err
= usbd_req_clear_port_feature(udev
, NULL
,
633 portno
, UHF_C_PORT_CONNECTION
);
638 /* check if there is a child */
642 * Free USB device and all subdevices, if any.
644 usb_free_device(child
, 0);
647 /* get fresh status */
649 err
= uhub_read_port_status(sc
, portno
);
653 #if USB_HAVE_DISABLE_ENUM
654 /* check if we should skip enumeration from this USB HUB */
655 if (usb_disable_enumeration
!= 0 ||
656 sc
->sc_disable_enumeration
!= 0) {
657 DPRINTF("Enumeration is disabled!\n");
661 /* check if nothing is connected to the port */
663 if (!(sc
->sc_st
.port_status
& UPS_CURRENT_CONNECT_STATUS
)) {
666 /* check if there is no power on the port and print a warning */
668 switch (udev
->speed
) {
672 power_mask
= UPS_PORT_POWER
;
674 case USB_SPEED_SUPER
:
675 if (udev
->parent_hub
== NULL
)
676 power_mask
= UPS_PORT_POWER
;
678 power_mask
= UPS_PORT_POWER_SS
;
684 if (!(sc
->sc_st
.port_status
& power_mask
)) {
685 DPRINTF("WARNING: strange, connected port %d "
686 "has no power\n", portno
);
689 /* check if the device is in Host Mode */
691 if (!(sc
->sc_st
.port_status
& UPS_PORT_MODE_DEVICE
)) {
693 DPRINTF("Port %d is in Host Mode\n", portno
);
695 if (sc
->sc_st
.port_status
& UPS_SUSPEND
) {
697 * NOTE: Should not get here in SuperSpeed
698 * mode, because the HUB should report this
701 DPRINTF("Port %d was still "
702 "suspended, clearing.\n", portno
);
703 err
= usbd_req_clear_port_feature(udev
,
704 NULL
, portno
, UHF_PORT_SUSPEND
);
709 /* wait for maximum device power up time */
712 USB_MS_TO_TICKS(usb_port_powerup_delay
));
714 /* reset port, which implies enabling it */
716 err
= usbd_req_reset_port(udev
, NULL
, portno
);
719 DPRINTFN(0, "port %d reset "
720 "failed, error=%s\n",
721 portno
, usbd_errstr(err
));
724 /* get port status again, it might have changed during reset */
726 err
= uhub_read_port_status(sc
, portno
);
730 /* check if something changed during port reset */
732 if ((sc
->sc_st
.port_change
& UPS_C_CONNECT_STATUS
) ||
733 (!(sc
->sc_st
.port_status
& UPS_CURRENT_CONNECT_STATUS
))) {
735 DPRINTFN(0, "giving up port reset "
736 "- device vanished\n");
743 DPRINTF("Port %d is in Device Mode\n", portno
);
747 * Figure out the device speed
749 switch (udev
->speed
) {
751 if (sc
->sc_st
.port_status
& UPS_HIGH_SPEED
)
752 speed
= USB_SPEED_HIGH
;
753 else if (sc
->sc_st
.port_status
& UPS_LOW_SPEED
)
754 speed
= USB_SPEED_LOW
;
756 speed
= USB_SPEED_FULL
;
759 if (sc
->sc_st
.port_status
& UPS_LOW_SPEED
)
760 speed
= USB_SPEED_LOW
;
762 speed
= USB_SPEED_FULL
;
765 speed
= USB_SPEED_LOW
;
767 case USB_SPEED_SUPER
:
768 if (udev
->parent_hub
== NULL
) {
769 /* Root HUB - special case */
770 switch (sc
->sc_st
.port_status
& UPS_OTHER_SPEED
) {
772 speed
= USB_SPEED_FULL
;
775 speed
= USB_SPEED_LOW
;
778 speed
= USB_SPEED_HIGH
;
781 speed
= USB_SPEED_SUPER
;
785 speed
= USB_SPEED_SUPER
;
789 /* same speed like parent */
793 if (speed
== USB_SPEED_SUPER
) {
794 err
= usbd_req_set_hub_u1_timeout(udev
, NULL
,
795 portno
, 128 - (2 * udev
->depth
));
797 DPRINTFN(0, "port %d U1 timeout "
798 "failed, error=%s\n",
799 portno
, usbd_errstr(err
));
801 err
= usbd_req_set_hub_u2_timeout(udev
, NULL
,
802 portno
, 128 - (2 * udev
->depth
));
804 DPRINTFN(0, "port %d U2 timeout "
805 "failed, error=%s\n",
806 portno
, usbd_errstr(err
));
811 * Figure out the device mode
813 * NOTE: This part is currently FreeBSD specific.
815 if (udev
->parent_hub
!= NULL
) {
816 /* inherit mode from the parent HUB */
817 mode
= udev
->parent_hub
->flags
.usb_mode
;
818 } else if (sc
->sc_st
.port_status
& UPS_PORT_MODE_DEVICE
)
819 mode
= USB_MODE_DEVICE
;
821 mode
= USB_MODE_HOST
;
823 /* need to create a new child */
824 child
= usb_alloc_device(sc
->sc_dev
, udev
->bus
, udev
,
825 udev
->depth
+ 1, portno
- 1, portno
, speed
, mode
);
827 DPRINTFN(0, "could not allocate new device\n");
830 return (0); /* success */
835 * Free USB device and all subdevices, if any.
837 usb_free_device(child
, 0);
841 if (sc
->sc_st
.port_status
& UPS_PORT_ENABLED
) {
842 err
= usbd_req_clear_port_feature(
844 portno
, UHF_PORT_ENABLE
);
848 DPRINTFN(0, "device problem (%s), "
849 "disabling port %d\n", usbd_errstr(err
), portno
);
854 /*------------------------------------------------------------------------*
855 * usb_device_20_compatible
858 * 0: HUB does not support suspend and resume
859 * Else: HUB supports suspend and resume
860 *------------------------------------------------------------------------*/
862 usb_device_20_compatible(struct usb_device
*udev
)
866 switch (udev
->speed
) {
876 /*------------------------------------------------------------------------*
877 * uhub_suspend_resume_port
881 * Else: A control transaction failed
882 *------------------------------------------------------------------------*/
884 uhub_suspend_resume_port(struct uhub_softc
*sc
, uint8_t portno
)
886 struct usb_device
*child
;
887 struct usb_device
*udev
;
891 DPRINTF("port %d\n", portno
);
894 child
= usb_bus_port_get_device(udev
->bus
,
895 udev
->hub
->ports
+ portno
- 1);
897 /* first clear the port suspend change bit */
899 if (usb_device_20_compatible(udev
)) {
900 err
= usbd_req_clear_port_feature(udev
, NULL
,
901 portno
, UHF_C_PORT_SUSPEND
);
903 err
= usbd_req_clear_port_feature(udev
, NULL
,
904 portno
, UHF_C_PORT_LINK_STATE
);
908 DPRINTF("clearing suspend failed.\n");
911 /* get fresh status */
913 err
= uhub_read_port_status(sc
, portno
);
915 DPRINTF("reading port status failed.\n");
918 /* convert current state */
920 if (usb_device_20_compatible(udev
)) {
921 if (sc
->sc_st
.port_status
& UPS_SUSPEND
) {
927 switch (UPS_PORT_LINK_STATE_GET(sc
->sc_st
.port_status
)) {
931 case UPS_PORT_LS_SS_INA
:
932 usbd_req_warm_reset_port(udev
, NULL
, portno
);
941 DPRINTF("suspended=%u\n", is_suspend
);
943 /* do the suspend or resume */
947 * This code handle two cases: 1) Host Mode - we can only
948 * receive resume here 2) Device Mode - we can receive
949 * suspend and resume here
952 usb_dev_resume_peer(child
);
953 else if (child
->flags
.usb_mode
== USB_MODE_DEVICE
)
954 usb_dev_suspend_peer(child
);
960 /*------------------------------------------------------------------------*
961 * uhub_root_interrupt
963 * This function is called when a Root HUB interrupt has
964 * happened. "ptr" and "len" makes up the Root HUB interrupt
965 * packet. This function is called having the "bus_mtx" locked.
966 *------------------------------------------------------------------------*/
968 uhub_root_intr(struct usb_bus
*bus
, const uint8_t *ptr
, uint8_t len
)
970 USB_BUS_LOCK_ASSERT(bus
);
972 usb_needs_explore(bus
, 0);
976 uhub_is_too_deep(struct usb_device
*udev
)
978 switch (udev
->speed
) {
982 if (udev
->depth
> USB_HUB_MAX_DEPTH
)
985 case USB_SPEED_SUPER
:
986 if (udev
->depth
> USB_SS_HUB_DEPTH_MAX
)
995 /*------------------------------------------------------------------------*
1001 *------------------------------------------------------------------------*/
1003 uhub_explore(struct usb_device
*udev
)
1005 struct usb_hub
*hub
;
1006 struct uhub_softc
*sc
;
1007 struct usb_port
*up
;
1016 DPRINTFN(11, "udev=%p addr=%d\n", udev
, udev
->address
);
1018 /* ignore devices that are too deep */
1019 if (uhub_is_too_deep(udev
))
1020 return (USB_ERR_TOO_DEEP
);
1022 /* check if device is suspended */
1023 if (udev
->flags
.self_suspended
) {
1024 /* need to wait until the child signals resume */
1025 DPRINTF("Device is suspended!\n");
1030 * Make sure we don't race against user-space applications
1033 do_unlock
= usbd_enum_lock(udev
);
1035 for (x
= 0; x
!= hub
->nports
; x
++) {
1036 up
= hub
->ports
+ x
;
1039 err
= uhub_read_port_status(sc
, portno
);
1041 /* most likely the HUB is gone */
1044 if (sc
->sc_st
.port_change
& UPS_C_OVERCURRENT_INDICATOR
) {
1045 DPRINTF("Overcurrent on port %u.\n", portno
);
1046 err
= usbd_req_clear_port_feature(
1047 udev
, NULL
, portno
, UHF_C_PORT_OVER_CURRENT
);
1049 /* most likely the HUB is gone */
1053 if (!(sc
->sc_flags
& UHUB_FLAG_DID_EXPLORE
)) {
1055 * Fake a connect status change so that the
1056 * status gets checked initially!
1058 sc
->sc_st
.port_change
|=
1059 UPS_C_CONNECT_STATUS
;
1061 if (sc
->sc_st
.port_change
& UPS_C_PORT_ENABLED
) {
1062 err
= usbd_req_clear_port_feature(
1063 udev
, NULL
, portno
, UHF_C_PORT_ENABLE
);
1065 /* most likely the HUB is gone */
1068 if (sc
->sc_st
.port_change
& UPS_C_CONNECT_STATUS
) {
1070 * Ignore the port error if the device
1073 } else if (sc
->sc_st
.port_status
& UPS_PORT_ENABLED
) {
1074 DPRINTFN(0, "illegal enable change, "
1075 "port %d\n", portno
);
1078 if (up
->restartcnt
== USB_RESTART_MAX
) {
1079 /* XXX could try another speed ? */
1080 DPRINTFN(0, "port error, giving up "
1081 "port %d\n", portno
);
1083 sc
->sc_st
.port_change
|=
1084 UPS_C_CONNECT_STATUS
;
1089 if (sc
->sc_st
.port_change
& UPS_C_CONNECT_STATUS
) {
1090 err
= uhub_reattach_port(sc
, portno
);
1092 /* most likely the HUB is gone */
1096 if (sc
->sc_st
.port_change
& (UPS_C_SUSPEND
|
1097 UPS_C_PORT_LINK_STATE
)) {
1098 err
= uhub_suspend_resume_port(sc
, portno
);
1100 /* most likely the HUB is gone */
1104 err
= uhub_explore_sub(sc
, up
);
1106 /* no device(s) present */
1109 /* explore succeeded - reset restart counter */
1114 usbd_enum_unlock(udev
);
1116 /* initial status checked */
1117 sc
->sc_flags
|= UHUB_FLAG_DID_EXPLORE
;
1119 /* return success */
1120 return (USB_ERR_NORMAL_COMPLETION
);
1124 uhub_probe(device_t dev
)
1126 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
1128 if (uaa
->usb_mode
!= USB_MODE_HOST
)
1132 * The subclass for USB HUBs is currently ignored because it
1133 * is 0 for some and 1 for others.
1135 if (uaa
->info
.bConfigIndex
== 0 &&
1136 uaa
->info
.bDeviceClass
== UDCLASS_HUB
)
1142 /* NOTE: The information returned by this function can be wrong. */
1144 uhub_query_info(struct usb_device
*udev
, uint8_t *pnports
, uint8_t *ptt
)
1146 struct usb_hub_descriptor hubdesc20
;
1147 struct usb_hub_ss_descriptor hubdesc30
;
1152 if (udev
->ddesc
.bDeviceClass
!= UDCLASS_HUB
)
1153 return (USB_ERR_INVAL
);
1158 switch (udev
->speed
) {
1160 case USB_SPEED_FULL
:
1161 case USB_SPEED_HIGH
:
1162 /* assuming that there is one port */
1163 err
= usbd_req_get_hub_descriptor(udev
, NULL
, &hubdesc20
, 1);
1165 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1166 "error=%s\n", usbd_errstr(err
));
1169 nports
= hubdesc20
.bNbrPorts
;
1173 if (udev
->speed
== USB_SPEED_HIGH
)
1174 tt
= (UGETW(hubdesc20
.wHubCharacteristics
) >> 5) & 3;
1177 case USB_SPEED_SUPER
:
1178 err
= usbd_req_get_ss_hub_descriptor(udev
, NULL
, &hubdesc30
, 1);
1180 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1181 "error=%s\n", usbd_errstr(err
));
1184 nports
= hubdesc30
.bNbrPorts
;
1190 err
= USB_ERR_INVAL
;
1194 if (pnports
!= NULL
)
1204 uhub_attach(device_t dev
)
1206 struct uhub_softc
*sc
= device_get_softc(dev
);
1207 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
1208 struct usb_device
*udev
= uaa
->device
;
1209 struct usb_device
*parent_hub
= udev
->parent_hub
;
1210 struct usb_hub
*hub
;
1211 struct usb_hub_descriptor hubdesc20
;
1212 struct usb_hub_ss_descriptor hubdesc30
;
1213 #if USB_HAVE_DISABLE_ENUM
1214 struct sysctl_ctx_list
*sysctl_ctx
;
1215 struct sysctl_oid
*sysctl_tree
;
1222 uint8_t iface_index
;
1228 lockinit(&sc
->sc_lock
, "USB HUB mutex", 0, 0);
1230 device_set_usb_desc(dev
);
1232 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1233 "parent->selfpowered=%d\n",
1235 udev
->flags
.self_powered
,
1238 parent_hub
->flags
.self_powered
: 0);
1240 if (uhub_is_too_deep(udev
)) {
1241 DPRINTFN(0, "HUB at depth %d, "
1242 "exceeds maximum. HUB ignored\n", (int)udev
->depth
);
1246 if (!udev
->flags
.self_powered
&& parent_hub
&&
1247 !parent_hub
->flags
.self_powered
) {
1248 DPRINTFN(0, "Bus powered HUB connected to "
1249 "bus powered HUB. HUB ignored\n");
1253 if (UHUB_IS_MULTI_TT(sc
)) {
1254 err
= usbd_set_alt_interface_index(udev
, 0, 1);
1256 device_printf(dev
, "MTT could not be enabled\n");
1259 device_printf(dev
, "MTT enabled\n");
1262 /* get HUB descriptor */
1264 DPRINTFN(2, "Getting HUB descriptor\n");
1266 switch (udev
->speed
) {
1268 case USB_SPEED_FULL
:
1269 case USB_SPEED_HIGH
:
1270 /* assuming that there is one port */
1271 err
= usbd_req_get_hub_descriptor(udev
, NULL
, &hubdesc20
, 1);
1273 DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1274 "error=%s\n", usbd_errstr(err
));
1277 /* get number of ports */
1278 nports
= hubdesc20
.bNbrPorts
;
1280 /* get power delay */
1281 pwrdly
= ((hubdesc20
.bPwrOn2PwrGood
* UHD_PWRON_FACTOR
) +
1282 usb_extra_power_up_time
);
1284 /* get complete HUB descriptor */
1286 /* check number of ports */
1288 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1289 "error=%s\n", usbd_errstr(err
));
1292 /* get complete HUB descriptor */
1293 err
= usbd_req_get_hub_descriptor(udev
, NULL
, &hubdesc20
, nports
);
1296 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1297 "error=%s\n", usbd_errstr(err
));
1300 if (hubdesc20
.bNbrPorts
!= nports
) {
1301 DPRINTFN(0, "Number of ports changed\n");
1306 case USB_SPEED_SUPER
:
1307 if (udev
->parent_hub
!= NULL
) {
1308 err
= usbd_req_set_hub_depth(udev
, NULL
,
1311 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1312 "error=%s\n", usbd_errstr(err
));
1316 err
= usbd_req_get_ss_hub_descriptor(udev
, NULL
, &hubdesc30
, 1);
1318 DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1319 "error=%s\n", usbd_errstr(err
));
1322 /* get number of ports */
1323 nports
= hubdesc30
.bNbrPorts
;
1325 /* get power delay */
1326 pwrdly
= ((hubdesc30
.bPwrOn2PwrGood
* UHD_PWRON_FACTOR
) +
1327 usb_extra_power_up_time
);
1329 /* get complete HUB descriptor */
1331 /* check number of ports */
1332 if (nports
> ((udev
->parent_hub
!= NULL
) ? 15 : 127)) {
1333 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1334 "error=%s\n", usbd_errstr(err
));
1337 /* get complete HUB descriptor */
1338 err
= usbd_req_get_ss_hub_descriptor(udev
, NULL
, &hubdesc30
, nports
);
1341 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1342 "error=%s\n", usbd_errstr(err
));
1345 if (hubdesc30
.bNbrPorts
!= nports
) {
1346 DPRINTFN(0, "Number of ports changed\n");
1352 DPRINTF("Assuming HUB has only one port\n");
1353 /* default number of ports */
1355 /* default power delay */
1356 pwrdly
= ((10 * UHD_PWRON_FACTOR
) + usb_extra_power_up_time
);
1360 DPRINTFN(0, "portless HUB\n");
1363 if (nports
> USB_MAX_PORTS
) {
1364 DPRINTF("Port limit exceeded\n");
1367 #if (USB_HAVE_FIXED_PORT == 0)
1368 hub
= kmalloc(sizeof(hub
[0]) + (sizeof(hub
->ports
[0]) * nports
),
1369 M_USBDEV
, M_WAITOK
| M_ZERO
);
1378 /* initialize HUB structure */
1380 hub
->explore
= &uhub_explore
;
1381 hub
->nports
= nports
;
1382 hub
->hubudev
= udev
;
1383 #if USB_HAVE_TT_SUPPORT
1384 hub
->tt_msg
[0].hdr
.pm_callback
= &uhub_reset_tt_proc
;
1385 hub
->tt_msg
[0].udev
= udev
;
1386 hub
->tt_msg
[1].hdr
.pm_callback
= &uhub_reset_tt_proc
;
1387 hub
->tt_msg
[1].udev
= udev
;
1389 /* if self powered hub, give ports maximum current */
1390 if (udev
->flags
.self_powered
) {
1391 hub
->portpower
= USB_MAX_POWER
;
1393 hub
->portpower
= USB_MIN_POWER
;
1396 /* set up interrupt pipe */
1398 if (udev
->parent_hub
== NULL
) {
1399 /* root HUB is special */
1403 err
= usbd_transfer_setup(udev
, &iface_index
, sc
->sc_xfer
,
1404 uhub_config
, UHUB_N_TRANSFER
, sc
, &sc
->sc_lock
);
1407 DPRINTFN(0, "cannot setup interrupt transfer, "
1408 "errstr=%s\n", usbd_errstr(err
));
1411 /* wait with power off for a while */
1412 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME
));
1414 #if USB_HAVE_DISABLE_ENUM
1415 /* Add device sysctls */
1417 sysctl_ctx
= device_get_sysctl_ctx(dev
);
1418 sysctl_tree
= device_get_sysctl_tree(dev
);
1420 if (sysctl_ctx
!= NULL
&& sysctl_tree
!= NULL
) {
1421 (void) SYSCTL_ADD_INT(sysctl_ctx
, SYSCTL_CHILDREN(sysctl_tree
),
1422 OID_AUTO
, "disable_enumeration", CTLFLAG_RW
,
1423 &sc
->sc_disable_enumeration
, 0,
1424 "Set to disable enumeration on this USB HUB.");
1426 (void) SYSCTL_ADD_INT(sysctl_ctx
, SYSCTL_CHILDREN(sysctl_tree
),
1427 OID_AUTO
, "disable_port_power", CTLFLAG_RW
,
1428 &sc
->sc_disable_port_power
, 0,
1429 "Set to disable USB port power on this USB HUB.");
1433 * To have the best chance of success we do things in the exact same
1434 * order as Windoze98. This should not be necessary, but some
1435 * devices do not follow the USB specs to the letter.
1437 * These are the events on the bus when a hub is attached:
1438 * Get device and config descriptors (see attach code)
1439 * Get hub descriptor (see above)
1442 * wait for power to become stable
1443 * (all below happens in explore code)
1445 * clear C_PORT_CONNECTION
1448 * if device connected
1452 * clear C_PORT_RESET
1454 * proceed with device attachment
1457 /* XXX should check for none, individual, or ganged power? */
1461 for (x
= 0; x
!= nports
; x
++) {
1462 /* set up data structures */
1463 struct usb_port
*up
= hub
->ports
+ x
;
1465 up
->device_index
= 0;
1469 /* check if port is removable */
1470 switch (udev
->speed
) {
1472 case USB_SPEED_FULL
:
1473 case USB_SPEED_HIGH
:
1474 if (!UHD_NOT_REMOV(&hubdesc20
, portno
))
1477 case USB_SPEED_SUPER
:
1478 if (!UHD_NOT_REMOV(&hubdesc30
, portno
))
1482 DPRINTF("Assuming removable port\n");
1487 #if USB_HAVE_DISABLE_ENUM
1488 /* check if we should disable USB port power or not */
1489 if (usb_disable_port_power
!= 0 ||
1490 sc
->sc_disable_port_power
!= 0) {
1491 /* turn the power off */
1492 DPRINTFN(2, "Turning port %d power off\n", portno
);
1493 err
= usbd_req_clear_port_feature(udev
, NULL
,
1494 portno
, UHF_PORT_POWER
);
1497 /* turn the power on */
1498 DPRINTFN(2, "Turning port %d power on\n", portno
);
1499 err
= usbd_req_set_port_feature(udev
, NULL
,
1500 portno
, UHF_PORT_POWER
);
1501 #if USB_HAVE_DISABLE_ENUM
1506 DPRINTFN(0, "port %d power on or off failed, %s\n",
1507 portno
, usbd_errstr(err
));
1509 DPRINTF("turn on port %d power\n",
1512 /* wait for stable power */
1513 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(pwrdly
));
1516 device_printf(dev
, "%d port%s with %d "
1517 "removable, %s powered\n", nports
, (nports
!= 1) ? "s" : "",
1518 removable
, udev
->flags
.self_powered
? "self" : "bus");
1520 /* Start the interrupt endpoint, if any */
1522 if (sc
->sc_xfer
[0] != NULL
) {
1523 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
1524 usbd_transfer_start(sc
->sc_xfer
[0]);
1525 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
1528 /* Enable automatic power save on all USB HUBs */
1530 usbd_set_power_mode(udev
, USB_POWER_MODE_SAVE
);
1535 usbd_transfer_unsetup(sc
->sc_xfer
, UHUB_N_TRANSFER
);
1538 #if (USB_HAVE_FIXED_PORT == 0)
1539 kfree(udev
->hub
, M_USBDEV
);
1543 lockuninit(&sc
->sc_lock
);
1549 * Called from process context when the hub is gone.
1550 * Detach all devices on active ports.
1553 uhub_detach(device_t dev
)
1555 struct uhub_softc
*sc
= device_get_softc(dev
);
1556 struct usb_hub
*hub
= sc
->sc_udev
->hub
;
1557 struct usb_bus
*bus
= sc
->sc_udev
->bus
;
1558 struct usb_device
*child
;
1561 if (hub
== NULL
) /* must be partially working */
1564 /* Make sure interrupt transfer is gone. */
1565 usbd_transfer_unsetup(sc
->sc_xfer
, UHUB_N_TRANSFER
);
1567 /* Detach all ports */
1568 for (x
= 0; x
!= hub
->nports
; x
++) {
1570 child
= usb_bus_port_get_device(bus
, hub
->ports
+ x
);
1572 if (child
== NULL
) {
1577 * Free USB device and all subdevices, if any.
1579 usb_free_device(child
, 0);
1582 #if USB_HAVE_TT_SUPPORT
1583 /* Make sure our TT messages are not queued anywhere */
1585 usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus
),
1586 &hub
->tt_msg
[0], &hub
->tt_msg
[1]);
1587 USB_BUS_UNLOCK(bus
);
1590 #if (USB_HAVE_FIXED_PORT == 0)
1591 kfree(hub
, M_USBDEV
);
1593 sc
->sc_udev
->hub
= NULL
;
1595 lockuninit(&sc
->sc_lock
);
1601 uhub_suspend(device_t dev
)
1604 /* Sub-devices are not suspended here! */
1609 uhub_resume(device_t dev
)
1612 /* Sub-devices are not resumed here! */
1617 uhub_driver_added(device_t dev
, driver_t
*driver
)
1619 usb_needs_explore_all();
1623 struct usb_device
*udev
;
1625 uint8_t iface_index
;
1629 uhub_find_iface_index(struct usb_hub
*hub
, device_t child
,
1630 struct hub_result
*res
)
1632 struct usb_interface
*iface
;
1633 struct usb_device
*udev
;
1638 nports
= hub
->nports
;
1639 for (x
= 0; x
!= nports
; x
++) {
1640 udev
= usb_bus_port_get_device(hub
->hubudev
->bus
,
1645 for (i
= 0; i
!= USB_IFACE_MAX
; i
++) {
1646 iface
= usbd_get_iface(udev
, i
);
1648 (iface
->subdev
== child
)) {
1649 res
->iface_index
= i
;
1651 res
->portno
= x
+ 1;
1656 res
->iface_index
= 0;
1662 uhub_child_location_string(device_t parent
, device_t child
,
1663 char *buf
, size_t buflen
)
1665 struct uhub_softc
*sc
;
1666 struct usb_hub
*hub
;
1667 struct hub_result res
;
1669 if (!device_is_attached(parent
)) {
1675 sc
= device_get_softc(parent
);
1676 hub
= sc
->sc_udev
->hub
;
1678 uhub_find_iface_index(hub
, child
, &res
);
1680 DPRINTF("device not on hub\n");
1686 ksnprintf(buf
, buflen
, "bus=%u hubaddr=%u port=%u devaddr=%u"
1691 , device_get_unit(res
.udev
->bus
->bdev
)
1692 , (res
.udev
->parent_hub
!= NULL
) ?
1693 res
.udev
->parent_hub
->device_index
: 0
1694 , res
.portno
, res
.udev
->device_index
, res
.iface_index
1696 , res
.udev
->ugen_name
1704 uhub_child_pnpinfo_string(device_t parent
, device_t child
,
1705 char *buf
, size_t buflen
)
1707 struct uhub_softc
*sc
;
1708 struct usb_hub
*hub
;
1709 struct usb_interface
*iface
;
1710 struct hub_result res
;
1712 if (!device_is_attached(parent
)) {
1718 sc
= device_get_softc(parent
);
1719 hub
= sc
->sc_udev
->hub
;
1721 uhub_find_iface_index(hub
, child
, &res
);
1723 DPRINTF("device not on hub\n");
1729 iface
= usbd_get_iface(res
.udev
, res
.iface_index
);
1730 if (iface
&& iface
->idesc
) {
1731 ksnprintf(buf
, buflen
, "vendor=0x%04x product=0x%04x "
1732 "devclass=0x%02x devsubclass=0x%02x "
1736 "intclass=0x%02x intsubclass=0x%02x "
1737 "intprotocol=0x%02x" "%s%s",
1738 UGETW(res
.udev
->ddesc
.idVendor
),
1739 UGETW(res
.udev
->ddesc
.idProduct
),
1740 res
.udev
->ddesc
.bDeviceClass
,
1741 res
.udev
->ddesc
.bDeviceSubClass
,
1742 usb_get_serial(res
.udev
),
1743 UGETW(res
.udev
->ddesc
.bcdDevice
),
1744 (res
.udev
->flags
.usb_mode
== USB_MODE_HOST
) ? "host" : "device",
1745 iface
->idesc
->bInterfaceClass
,
1746 iface
->idesc
->bInterfaceSubClass
,
1747 iface
->idesc
->bInterfaceProtocol
,
1748 iface
->pnpinfo
? " " : "",
1749 iface
->pnpinfo
? iface
->pnpinfo
: "");
1761 * The USB Transaction Translator:
1762 * ===============================
1764 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1765 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1766 * USB transfers. To utilize bandwidth dynamically the "scatter and
1767 * gather" principle must be applied. This means that bandwidth must
1768 * be divided into equal parts of bandwidth. With regard to USB all
1769 * data is transferred in smaller packets with length
1770 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1773 * The bandwidth scheduler which I have implemented will simply pack
1774 * the USB transfers back to back until there is no more space in the
1775 * schedule. Out of the 8 microframes which the USB 2.0 standard
1776 * provides, only 6 are available for non-HIGH-speed devices. I have
1777 * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1778 * last 2 microframes I have reserved for INTERRUPT transfers. Without
1779 * this division, it is very difficult to allocate and free bandwidth
1782 * NOTE about the Transaction Translator in USB HUBs:
1784 * USB HUBs have a very simple Transaction Translator, that will
1785 * simply pipeline all the SPLIT transactions. That means that the
1786 * transactions will be executed in the order they are queued!
1790 /*------------------------------------------------------------------------*
1791 * usb_intr_find_best_slot
1794 * The best Transaction Translation slot for an interrupt endpoint.
1795 *------------------------------------------------------------------------*/
1797 usb_intr_find_best_slot(usb_size_t
*ptr
, uint8_t start
,
1798 uint8_t end
, uint8_t mask
)
1800 usb_size_t min
= (usb_size_t
)-1;
1808 /* find the last slot with lesser used bandwidth */
1810 for (x
= start
; x
< end
; x
++) {
1814 /* compute sum of bandwidth */
1815 for (z
= x
; z
< end
; z
++) {
1816 if (mask
& (1U << (z
- x
)))
1820 /* check if the current multi-slot is more optimal */
1826 /* check if the mask is about to be shifted out */
1827 if (mask
& (1U << (end
- 1 - x
)))
1833 /*------------------------------------------------------------------------*
1834 * usb_hs_bandwidth_adjust
1836 * This function will update the bandwith usage for the microframe
1837 * having index "slot" by "len" bytes. "len" can be negative. If the
1838 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1839 * the "slot" argument will be replaced by the slot having least used
1840 * bandwidth. The "mask" argument is used for multi-slot allocations.
1843 * The slot in which the bandwidth update was done: 0..7
1844 *------------------------------------------------------------------------*/
1846 usb_hs_bandwidth_adjust(struct usb_device
*udev
, int16_t len
,
1847 uint8_t slot
, uint8_t mask
)
1849 struct usb_bus
*bus
= udev
->bus
;
1850 struct usb_hub
*hub
;
1851 enum usb_dev_speed speed
;
1854 USB_BUS_LOCK_ASSERT(bus
);
1856 speed
= usbd_get_speed(udev
);
1860 case USB_SPEED_FULL
:
1861 if (speed
== USB_SPEED_LOW
) {
1865 * The Host Controller Driver should have
1866 * performed checks so that the lookup
1867 * below does not result in a NULL pointer
1871 hub
= udev
->parent_hs_hub
->hub
;
1872 if (slot
>= USB_HS_MICRO_FRAMES_MAX
) {
1873 slot
= usb_intr_find_best_slot(hub
->uframe_usage
,
1874 USB_FS_ISOC_UFRAME_MAX
, 6, mask
);
1876 for (x
= slot
; x
< 8; x
++) {
1877 if (mask
& (1U << (x
- slot
))) {
1878 hub
->uframe_usage
[x
] += len
;
1879 bus
->uframe_usage
[x
] += len
;
1884 if (slot
>= USB_HS_MICRO_FRAMES_MAX
) {
1885 slot
= usb_intr_find_best_slot(bus
->uframe_usage
, 0,
1886 USB_HS_MICRO_FRAMES_MAX
, mask
);
1888 for (x
= slot
; x
< 8; x
++) {
1889 if (mask
& (1U << (x
- slot
))) {
1890 bus
->uframe_usage
[x
] += len
;
1898 /*------------------------------------------------------------------------*
1899 * usb_hs_bandwidth_alloc
1901 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1902 *------------------------------------------------------------------------*/
1904 usb_hs_bandwidth_alloc(struct usb_xfer
*xfer
)
1906 struct usb_device
*udev
;
1911 udev
= xfer
->xroot
->udev
;
1913 if (udev
->flags
.usb_mode
!= USB_MODE_HOST
)
1914 return; /* not supported */
1916 xfer
->endpoint
->refcount_bw
++;
1917 if (xfer
->endpoint
->refcount_bw
!= 1)
1918 return; /* already allocated */
1920 speed
= usbd_get_speed(udev
);
1922 switch (xfer
->endpoint
->edesc
->bmAttributes
& UE_XFERTYPE
) {
1924 /* allocate a microframe slot */
1927 slot
= usb_hs_bandwidth_adjust(udev
,
1928 xfer
->max_frame_size
, USB_HS_MICRO_FRAMES_MAX
, mask
);
1930 xfer
->endpoint
->usb_uframe
= slot
;
1931 xfer
->endpoint
->usb_smask
= mask
<< slot
;
1933 if ((speed
!= USB_SPEED_FULL
) &&
1934 (speed
!= USB_SPEED_LOW
)) {
1935 xfer
->endpoint
->usb_cmask
= 0x00 ;
1937 xfer
->endpoint
->usb_cmask
= (-(0x04 << slot
)) & 0xFE;
1941 case UE_ISOCHRONOUS
:
1942 switch (usbd_xfer_get_fps_shift(xfer
)) {
1957 /* allocate a microframe multi-slot */
1959 slot
= usb_hs_bandwidth_adjust(udev
,
1960 xfer
->max_frame_size
, USB_HS_MICRO_FRAMES_MAX
, mask
);
1962 xfer
->endpoint
->usb_uframe
= slot
;
1963 xfer
->endpoint
->usb_cmask
= 0;
1964 xfer
->endpoint
->usb_smask
= mask
<< slot
;
1968 xfer
->endpoint
->usb_uframe
= 0;
1969 xfer
->endpoint
->usb_cmask
= 0;
1970 xfer
->endpoint
->usb_smask
= 0;
1974 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1975 xfer
->endpoint
->usb_uframe
,
1976 xfer
->endpoint
->usb_smask
>> xfer
->endpoint
->usb_uframe
);
1979 /*------------------------------------------------------------------------*
1980 * usb_hs_bandwidth_free
1982 * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1983 *------------------------------------------------------------------------*/
1985 usb_hs_bandwidth_free(struct usb_xfer
*xfer
)
1987 struct usb_device
*udev
;
1991 udev
= xfer
->xroot
->udev
;
1993 if (udev
->flags
.usb_mode
!= USB_MODE_HOST
)
1994 return; /* not supported */
1996 xfer
->endpoint
->refcount_bw
--;
1997 if (xfer
->endpoint
->refcount_bw
!= 0)
1998 return; /* still allocated */
2000 switch (xfer
->endpoint
->edesc
->bmAttributes
& UE_XFERTYPE
) {
2002 case UE_ISOCHRONOUS
:
2004 slot
= xfer
->endpoint
->usb_uframe
;
2005 mask
= xfer
->endpoint
->usb_smask
;
2007 /* free microframe slot(s): */
2008 usb_hs_bandwidth_adjust(udev
,
2009 -xfer
->max_frame_size
, slot
, mask
>> slot
);
2011 DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2012 slot
, mask
>> slot
);
2014 xfer
->endpoint
->usb_uframe
= 0;
2015 xfer
->endpoint
->usb_cmask
= 0;
2016 xfer
->endpoint
->usb_smask
= 0;
2024 /*------------------------------------------------------------------------*
2025 * usb_isoc_time_expand
2027 * This function will expand the time counter from 7-bit to 16-bit.
2030 * 16-bit isochronous time counter.
2031 *------------------------------------------------------------------------*/
2033 usb_isoc_time_expand(struct usb_bus
*bus
, uint16_t isoc_time_curr
)
2037 USB_BUS_LOCK_ASSERT(bus
);
2039 rem
= bus
->isoc_time_last
& (USB_ISOC_TIME_MAX
- 1);
2041 isoc_time_curr
&= (USB_ISOC_TIME_MAX
- 1);
2043 if (isoc_time_curr
< rem
) {
2044 /* the time counter wrapped around */
2045 bus
->isoc_time_last
+= USB_ISOC_TIME_MAX
;
2047 /* update the remainder */
2049 bus
->isoc_time_last
&= ~(USB_ISOC_TIME_MAX
- 1);
2050 bus
->isoc_time_last
|= isoc_time_curr
;
2052 return (bus
->isoc_time_last
);
2055 /*------------------------------------------------------------------------*
2056 * usbd_fs_isoc_schedule_alloc_slot
2058 * This function will allocate bandwidth for an isochronous FULL speed
2059 * transaction in the FULL speed schedule.
2064 *------------------------------------------------------------------------*/
2065 #if USB_HAVE_TT_SUPPORT
2067 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer
*isoc_xfer
, uint16_t isoc_time
)
2069 struct usb_xfer
*xfer
;
2070 struct usb_xfer
*pipe_xfer
;
2071 struct usb_bus
*bus
;
2073 usb_frlength_t data_len
;
2081 bus
= isoc_xfer
->xroot
->bus
;
2083 TAILQ_FOREACH(xfer
, &bus
->intr_q
.head
, wait_entry
) {
2085 /* skip self, if any */
2087 if (xfer
== isoc_xfer
)
2090 /* check if this USB transfer is going through the same TT */
2092 if (xfer
->xroot
->udev
->parent_hs_hub
!=
2093 isoc_xfer
->xroot
->udev
->parent_hs_hub
) {
2096 if ((isoc_xfer
->xroot
->udev
->parent_hs_hub
->
2097 ddesc
.bDeviceProtocol
== UDPROTO_HSHUBMTT
) &&
2098 (xfer
->xroot
->udev
->hs_port_no
!=
2099 isoc_xfer
->xroot
->udev
->hs_port_no
)) {
2102 if (xfer
->endpoint
->methods
!= isoc_xfer
->endpoint
->methods
)
2105 /* check if isoc_time is part of this transfer */
2107 delta
= xfer
->isoc_time_complete
- isoc_time
;
2108 if (delta
> 0 && delta
<= xfer
->nframes
) {
2109 delta
= xfer
->nframes
- delta
;
2111 len
= xfer
->frlengths
[delta
];
2120 * Check double buffered transfers. Only stream ID
2121 * equal to zero is valid here!
2123 TAILQ_FOREACH(pipe_xfer
, &xfer
->endpoint
->endpoint_q
[0].head
,
2126 /* skip self, if any */
2128 if (pipe_xfer
== isoc_xfer
)
2131 /* check if isoc_time is part of this transfer */
2133 delta
= pipe_xfer
->isoc_time_complete
- isoc_time
;
2134 if (delta
> 0 && delta
<= pipe_xfer
->nframes
) {
2135 delta
= pipe_xfer
->nframes
- delta
;
2137 len
= pipe_xfer
->frlengths
[delta
];
2147 while (data_len
>= USB_FS_BYTES_PER_HS_UFRAME
) {
2148 data_len
-= USB_FS_BYTES_PER_HS_UFRAME
;
2152 /* check for overflow */
2154 if (slot
>= USB_FS_ISOC_UFRAME_MAX
)
2159 delta
= isoc_xfer
->isoc_time_complete
- isoc_time
;
2160 if (delta
> 0 && delta
<= isoc_xfer
->nframes
) {
2161 delta
= isoc_xfer
->nframes
- delta
;
2163 len
= isoc_xfer
->frlengths
[delta
];
2171 while (data_len
>= USB_FS_BYTES_PER_HS_UFRAME
) {
2172 data_len
-= USB_FS_BYTES_PER_HS_UFRAME
;
2176 /* check for overflow */
2178 if (slot
>= USB_FS_ISOC_UFRAME_MAX
)
2185 /*------------------------------------------------------------------------*
2186 * usb_bus_port_get_device
2188 * This function is NULL safe.
2189 *------------------------------------------------------------------------*/
2191 usb_bus_port_get_device(struct usb_bus
*bus
, struct usb_port
*up
)
2193 if ((bus
== NULL
) || (up
== NULL
)) {
2197 if (up
->device_index
== 0) {
2201 return (bus
->devices
[up
->device_index
]);
2204 /*------------------------------------------------------------------------*
2205 * usb_bus_port_set_device
2207 * This function is NULL safe.
2208 *------------------------------------------------------------------------*/
2210 usb_bus_port_set_device(struct usb_bus
*bus
, struct usb_port
*up
,
2211 struct usb_device
*udev
, uint8_t device_index
)
2218 * There is only one case where we don't
2219 * have an USB port, and that is the Root Hub!
2223 up
->device_index
= device_index
;
2225 device_index
= up
->device_index
;
2226 up
->device_index
= 0;
2230 * Make relationships to our new device
2232 if (device_index
!= 0) {
2234 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
2236 bus
->devices
[device_index
] = udev
;
2238 lockmgr(&usb_ref_lock
, LK_RELEASE
);
2244 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus
, device_index
, udev
);
2247 /*------------------------------------------------------------------------*
2250 * This functions is called when the USB event thread needs to run.
2251 *------------------------------------------------------------------------*/
2253 usb_needs_explore(struct usb_bus
*bus
, uint8_t do_probe
)
2260 DPRINTF("No bus pointer!\n");
2263 if ((bus
->devices
== NULL
) ||
2264 (bus
->devices
[USB_ROOT_HUB_ADDR
] == NULL
)) {
2265 DPRINTF("No root HUB\n");
2268 if (lockowned(&bus
->bus_lock
) != 0) {
2277 if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus
),
2278 &bus
->explore_msg
[0], &bus
->explore_msg
[1])) {
2282 USB_BUS_UNLOCK(bus
);
2286 /*------------------------------------------------------------------------*
2287 * usb_needs_explore_all
2289 * This function is called whenever a new driver is loaded and will
2290 * cause that all USB busses are re-explored.
2291 *------------------------------------------------------------------------*/
2293 usb_needs_explore_all(void)
2295 struct usb_bus
*bus
;
2302 dc
= usb_devclass_ptr
;
2304 DPRINTFN(0, "no devclass\n");
2308 * Explore all USB busses in parallell.
2310 max
= devclass_get_maxunit(dc
);
2312 dev
= devclass_get_device(dc
, max
);
2314 bus
= device_get_softc(dev
);
2316 usb_needs_explore(bus
, 1);
2323 /*------------------------------------------------------------------------*
2324 * usb_bus_power_update
2326 * This function will ensure that all USB devices on the given bus are
2327 * properly suspended or resumed according to the device transfer
2329 *------------------------------------------------------------------------*/
2332 usb_bus_power_update(struct usb_bus
*bus
)
2334 usb_needs_explore(bus
, 0 /* no probe */ );
2338 /*------------------------------------------------------------------------*
2339 * usbd_transfer_power_ref
2341 * This function will modify the power save reference counts and
2342 * wakeup the USB device associated with the given USB transfer, if
2344 *------------------------------------------------------------------------*/
2347 usbd_transfer_power_ref(struct usb_xfer
*xfer
, int val
)
2349 static const usb_power_mask_t power_mask
[4] = {
2350 [UE_CONTROL
] = USB_HW_POWER_CONTROL
,
2351 [UE_BULK
] = USB_HW_POWER_BULK
,
2352 [UE_INTERRUPT
] = USB_HW_POWER_INTERRUPT
,
2353 [UE_ISOCHRONOUS
] = USB_HW_POWER_ISOC
,
2355 struct usb_device
*udev
;
2356 uint8_t needs_explore
;
2357 uint8_t needs_hw_power
;
2360 udev
= xfer
->xroot
->udev
;
2362 if (udev
->device_index
== USB_ROOT_HUB_ADDR
) {
2363 /* no power save for root HUB */
2366 USB_BUS_LOCK(udev
->bus
);
2368 xfer_type
= xfer
->endpoint
->edesc
->bmAttributes
& UE_XFERTYPE
;
2370 udev
->pwr_save
.last_xfer_time
= ticks
;
2371 udev
->pwr_save
.type_refs
[xfer_type
] += val
;
2373 if (xfer
->flags_int
.control_xfr
) {
2374 udev
->pwr_save
.read_refs
+= val
;
2375 if (xfer
->flags_int
.usb_mode
== USB_MODE_HOST
) {
2377 * It is not allowed to suspend during a
2380 udev
->pwr_save
.write_refs
+= val
;
2382 } else if (USB_GET_DATA_ISREAD(xfer
)) {
2383 udev
->pwr_save
.read_refs
+= val
;
2385 udev
->pwr_save
.write_refs
+= val
;
2389 if (udev
->flags
.self_suspended
)
2390 needs_explore
= usb_peer_should_wakeup(udev
);
2394 if (!(udev
->bus
->hw_power_state
& power_mask
[xfer_type
])) {
2395 DPRINTF("Adding type %u to power state\n", xfer_type
);
2396 udev
->bus
->hw_power_state
|= power_mask
[xfer_type
];
2406 USB_BUS_UNLOCK(udev
->bus
);
2408 if (needs_explore
) {
2409 DPRINTF("update\n");
2410 usb_bus_power_update(udev
->bus
);
2411 } else if (needs_hw_power
) {
2412 DPRINTF("needs power\n");
2413 if (udev
->bus
->methods
->set_hw_power
!= NULL
) {
2414 (udev
->bus
->methods
->set_hw_power
) (udev
->bus
);
2420 /*------------------------------------------------------------------------*
2421 * usb_peer_should_wakeup
2423 * This function returns non-zero if the current device should wake up.
2424 *------------------------------------------------------------------------*/
2426 usb_peer_should_wakeup(struct usb_device
*udev
)
2428 return (((udev
->power_mode
== USB_POWER_MODE_ON
) &&
2429 (udev
->flags
.usb_mode
== USB_MODE_HOST
)) ||
2430 (udev
->driver_added_refcount
!= udev
->bus
->driver_added_refcount
) ||
2431 (udev
->re_enumerate_wait
!= USB_RE_ENUM_DONE
) ||
2432 (udev
->pwr_save
.type_refs
[UE_ISOCHRONOUS
] != 0) ||
2433 (udev
->pwr_save
.write_refs
!= 0) ||
2434 ((udev
->pwr_save
.read_refs
!= 0) &&
2435 (udev
->flags
.usb_mode
== USB_MODE_HOST
) &&
2436 (usb_peer_can_wakeup(udev
) == 0)));
2439 /*------------------------------------------------------------------------*
2442 * This function implements the USB power daemon and is called
2443 * regularly from the USB explore thread.
2444 *------------------------------------------------------------------------*/
2447 usb_bus_powerd(struct usb_bus
*bus
)
2449 struct usb_device
*udev
;
2452 usb_ticks_t mintime
;
2453 usb_size_t type_refs
[5];
2456 limit
= usb_power_timeout
;
2459 else if (limit
> 255)
2464 DPRINTF("bus=%p\n", bus
);
2469 * The root HUB device is never suspended
2470 * and we simply skip it.
2472 for (x
= USB_ROOT_HUB_ADDR
+ 1;
2473 x
!= bus
->devices_max
; x
++) {
2475 udev
= bus
->devices
[x
];
2479 temp
= ticks
- udev
->pwr_save
.last_xfer_time
;
2481 if (usb_peer_should_wakeup(udev
)) {
2482 /* check if we are suspended */
2483 if (udev
->flags
.self_suspended
!= 0) {
2484 USB_BUS_UNLOCK(bus
);
2485 usb_dev_resume_peer(udev
);
2488 } else if ((temp
>= limit
) &&
2489 (udev
->flags
.usb_mode
== USB_MODE_HOST
) &&
2490 (udev
->flags
.self_suspended
== 0)) {
2491 /* try to do suspend */
2493 USB_BUS_UNLOCK(bus
);
2494 usb_dev_suspend_peer(udev
);
2499 /* reset counters */
2501 mintime
= (usb_ticks_t
)-1;
2508 /* Re-loop all the devices to get the actual state */
2510 for (x
= USB_ROOT_HUB_ADDR
+ 1;
2511 x
!= bus
->devices_max
; x
++) {
2513 udev
= bus
->devices
[x
];
2517 /* we found a non-Root-Hub USB device */
2520 /* "last_xfer_time" can be updated by a resume */
2521 temp
= ticks
- udev
->pwr_save
.last_xfer_time
;
2524 * Compute minimum time since last transfer for the complete
2530 if (udev
->flags
.self_suspended
== 0) {
2531 type_refs
[0] += udev
->pwr_save
.type_refs
[0];
2532 type_refs
[1] += udev
->pwr_save
.type_refs
[1];
2533 type_refs
[2] += udev
->pwr_save
.type_refs
[2];
2534 type_refs
[3] += udev
->pwr_save
.type_refs
[3];
2538 if (mintime
>= (usb_ticks_t
)(1 * hz
)) {
2539 /* recompute power masks */
2540 DPRINTF("Recomputing power masks\n");
2541 bus
->hw_power_state
= 0;
2542 if (type_refs
[UE_CONTROL
] != 0)
2543 bus
->hw_power_state
|= USB_HW_POWER_CONTROL
;
2544 if (type_refs
[UE_BULK
] != 0)
2545 bus
->hw_power_state
|= USB_HW_POWER_BULK
;
2546 if (type_refs
[UE_INTERRUPT
] != 0)
2547 bus
->hw_power_state
|= USB_HW_POWER_INTERRUPT
;
2548 if (type_refs
[UE_ISOCHRONOUS
] != 0)
2549 bus
->hw_power_state
|= USB_HW_POWER_ISOC
;
2550 if (type_refs
[4] != 0)
2551 bus
->hw_power_state
|= USB_HW_POWER_NON_ROOT_HUB
;
2553 USB_BUS_UNLOCK(bus
);
2555 if (bus
->methods
->set_hw_power
!= NULL
) {
2556 /* always update hardware power! */
2557 (bus
->methods
->set_hw_power
) (bus
);
2563 /*------------------------------------------------------------------------*
2564 * usb_dev_resume_peer
2566 * This function will resume an USB peer and do the required USB
2567 * signalling to get an USB device out of the suspended state.
2568 *------------------------------------------------------------------------*/
2570 usb_dev_resume_peer(struct usb_device
*udev
)
2572 struct usb_bus
*bus
;
2579 /* check if already resumed */
2580 if (udev
->flags
.self_suspended
== 0)
2583 /* we need a parent HUB to do resume */
2584 if (udev
->parent_hub
== NULL
)
2587 DPRINTF("udev=%p\n", udev
);
2589 if ((udev
->flags
.usb_mode
== USB_MODE_DEVICE
) &&
2590 (udev
->flags
.remote_wakeup
== 0)) {
2592 * If the host did not set the remote wakeup feature, we can
2593 * not wake it up either!
2595 DPRINTF("remote wakeup is not set!\n");
2598 /* get bus pointer */
2601 /* resume parent hub first */
2602 usb_dev_resume_peer(udev
->parent_hub
);
2604 /* reduce chance of instant resume failure by waiting a little bit */
2605 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(20));
2607 if (usb_device_20_compatible(udev
)) {
2608 /* resume current port (Valid in Host and Device Mode) */
2609 err
= usbd_req_clear_port_feature(udev
->parent_hub
,
2610 NULL
, udev
->port_no
, UHF_PORT_SUSPEND
);
2612 DPRINTFN(0, "Resuming port failed\n");
2616 /* resume current port (Valid in Host and Device Mode) */
2617 err
= usbd_req_set_port_link_state(udev
->parent_hub
,
2618 NULL
, udev
->port_no
, UPS_PORT_LS_U0
);
2620 DPRINTFN(0, "Resuming port failed\n");
2625 /* resume settle time */
2626 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(usb_port_resume_delay
));
2628 if (bus
->methods
->device_resume
!= NULL
) {
2629 /* resume USB device on the USB controller */
2630 (bus
->methods
->device_resume
) (udev
);
2633 /* set that this device is now resumed */
2634 udev
->flags
.self_suspended
= 0;
2636 /* make sure that we don't go into suspend right away */
2637 udev
->pwr_save
.last_xfer_time
= ticks
;
2639 /* make sure the needed power masks are on */
2640 if (udev
->pwr_save
.type_refs
[UE_CONTROL
] != 0)
2641 bus
->hw_power_state
|= USB_HW_POWER_CONTROL
;
2642 if (udev
->pwr_save
.type_refs
[UE_BULK
] != 0)
2643 bus
->hw_power_state
|= USB_HW_POWER_BULK
;
2644 if (udev
->pwr_save
.type_refs
[UE_INTERRUPT
] != 0)
2645 bus
->hw_power_state
|= USB_HW_POWER_INTERRUPT
;
2646 if (udev
->pwr_save
.type_refs
[UE_ISOCHRONOUS
] != 0)
2647 bus
->hw_power_state
|= USB_HW_POWER_ISOC
;
2649 USB_BUS_UNLOCK(bus
);
2651 if (bus
->methods
->set_hw_power
!= NULL
) {
2652 /* always update hardware power! */
2653 (bus
->methods
->set_hw_power
) (bus
);
2658 /* notify all sub-devices about resume */
2659 err
= usb_suspend_resume(udev
, 0);
2661 usbd_sr_unlock(udev
);
2663 /* check if peer has wakeup capability */
2664 if (usb_peer_can_wakeup(udev
)) {
2665 /* clear remote wakeup */
2666 err
= usbd_req_clear_device_feature(udev
,
2667 NULL
, UF_DEVICE_REMOTE_WAKEUP
);
2669 DPRINTFN(0, "Clearing device "
2670 "remote wakeup failed: %s\n",
2676 /*------------------------------------------------------------------------*
2677 * usb_dev_suspend_peer
2679 * This function will suspend an USB peer and do the required USB
2680 * signalling to get an USB device into the suspended state.
2681 *------------------------------------------------------------------------*/
2683 usb_dev_suspend_peer(struct usb_device
*udev
)
2685 struct usb_device
*child
;
2695 /* check if already suspended */
2696 if (udev
->flags
.self_suspended
)
2699 /* we need a parent HUB to do suspend */
2700 if (udev
->parent_hub
== NULL
)
2703 DPRINTF("udev=%p\n", udev
);
2705 /* check if the current device is a HUB */
2706 if (udev
->hub
!= NULL
) {
2707 nports
= udev
->hub
->nports
;
2709 /* check if all devices on the HUB are suspended */
2710 for (x
= 0; x
!= nports
; x
++) {
2711 child
= usb_bus_port_get_device(udev
->bus
,
2712 udev
->hub
->ports
+ x
);
2717 if (child
->flags
.self_suspended
)
2720 DPRINTFN(1, "Port %u is busy on the HUB!\n", x
+ 1);
2725 if (usb_peer_can_wakeup(udev
)) {
2727 * This request needs to be done before we set
2728 * "udev->flags.self_suspended":
2731 /* allow device to do remote wakeup */
2732 err
= usbd_req_set_device_feature(udev
,
2733 NULL
, UF_DEVICE_REMOTE_WAKEUP
);
2735 DPRINTFN(0, "Setting device "
2736 "remote wakeup failed\n");
2740 USB_BUS_LOCK(udev
->bus
);
2742 * Checking for suspend condition and setting suspended bit
2745 err
= usb_peer_should_wakeup(udev
);
2748 * Set that this device is suspended. This variable
2749 * must be set before calling USB controller suspend
2752 udev
->flags
.self_suspended
= 1;
2754 USB_BUS_UNLOCK(udev
->bus
);
2757 if (usb_peer_can_wakeup(udev
)) {
2758 /* allow device to do remote wakeup */
2759 err
= usbd_req_clear_device_feature(udev
,
2760 NULL
, UF_DEVICE_REMOTE_WAKEUP
);
2762 DPRINTFN(0, "Setting device "
2763 "remote wakeup failed\n");
2767 if (udev
->flags
.usb_mode
== USB_MODE_DEVICE
) {
2768 /* resume parent HUB first */
2769 usb_dev_resume_peer(udev
->parent_hub
);
2771 /* reduce chance of instant resume failure by waiting a little bit */
2772 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(20));
2774 /* resume current port (Valid in Host and Device Mode) */
2775 err
= usbd_req_clear_port_feature(udev
->parent_hub
,
2776 NULL
, udev
->port_no
, UHF_PORT_SUSPEND
);
2778 /* resume settle time */
2779 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(usb_port_resume_delay
));
2781 DPRINTF("Suspend was cancelled!\n");
2787 /* notify all sub-devices about suspend */
2788 err
= usb_suspend_resume(udev
, 1);
2790 usbd_sr_unlock(udev
);
2792 if (udev
->bus
->methods
->device_suspend
!= NULL
) {
2795 /* suspend device on the USB controller */
2796 (udev
->bus
->methods
->device_suspend
) (udev
);
2799 temp
= usbd_get_dma_delay(udev
);
2801 usb_pause_mtx(NULL
, USB_MS_TO_TICKS(temp
));
2805 if (usb_device_20_compatible(udev
)) {
2806 /* suspend current port */
2807 err
= usbd_req_set_port_feature(udev
->parent_hub
,
2808 NULL
, udev
->port_no
, UHF_PORT_SUSPEND
);
2810 DPRINTFN(0, "Suspending port failed\n");
2814 /* suspend current port */
2815 err
= usbd_req_set_port_link_state(udev
->parent_hub
,
2816 NULL
, udev
->port_no
, UPS_PORT_LS_U3
);
2818 DPRINTFN(0, "Suspending port failed\n");
2823 udev
= udev
->parent_hub
;
2827 /*------------------------------------------------------------------------*
2828 * usbd_set_power_mode
2830 * This function will set the power mode, see USB_POWER_MODE_XXX for a
2832 *------------------------------------------------------------------------*/
2834 usbd_set_power_mode(struct usb_device
*udev
, uint8_t power_mode
)
2836 /* filter input argument */
2837 if ((power_mode
!= USB_POWER_MODE_ON
) &&
2838 (power_mode
!= USB_POWER_MODE_OFF
))
2839 power_mode
= USB_POWER_MODE_SAVE
;
2841 power_mode
= usbd_filter_power_mode(udev
, power_mode
);
2843 udev
->power_mode
= power_mode
; /* update copy of power mode */
2846 usb_bus_power_update(udev
->bus
);
2848 usb_needs_explore(udev
->bus
, 0 /* no probe */ );
2852 /*------------------------------------------------------------------------*
2853 * usbd_filter_power_mode
2855 * This function filters the power mode based on hardware requirements.
2856 *------------------------------------------------------------------------*/
2858 usbd_filter_power_mode(struct usb_device
*udev
, uint8_t power_mode
)
2860 const struct usb_bus_methods
*mtod
;
2863 mtod
= udev
->bus
->methods
;
2866 if (mtod
->get_power_mode
!= NULL
)
2867 (mtod
->get_power_mode
) (udev
, &temp
);
2869 /* check if we should not filter */
2871 return (power_mode
);
2873 /* use fixed power mode given by hardware driver */
2877 /*------------------------------------------------------------------------*
2878 * usbd_start_re_enumerate
2880 * This function starts re-enumeration of the given USB device. This
2881 * function does not need to be called BUS-locked. This function does
2882 * not wait until the re-enumeration is completed.
2883 *------------------------------------------------------------------------*/
2885 usbd_start_re_enumerate(struct usb_device
*udev
)
2887 if (udev
->re_enumerate_wait
== USB_RE_ENUM_DONE
) {
2888 udev
->re_enumerate_wait
= USB_RE_ENUM_START
;
2889 usb_needs_explore(udev
->bus
, 0);
2893 /*-----------------------------------------------------------------------*
2894 * usbd_start_set_config
2896 * This function starts setting a USB configuration. This function
2897 * does not need to be called BUS-locked. This function does not wait
2898 * until the set USB configuratino is completed.
2899 *------------------------------------------------------------------------*/
2901 usbd_start_set_config(struct usb_device
*udev
, uint8_t index
)
2903 if (udev
->re_enumerate_wait
== USB_RE_ENUM_DONE
) {
2904 if (udev
->curr_config_index
== index
) {
2905 /* no change needed */
2908 udev
->next_config_index
= index
;
2909 udev
->re_enumerate_wait
= USB_RE_ENUM_SET_CONFIG
;
2910 usb_needs_explore(udev
->bus
, 0);
2912 } else if (udev
->re_enumerate_wait
== USB_RE_ENUM_SET_CONFIG
) {
2913 if (udev
->next_config_index
== index
) {
2914 /* no change needed */
2918 return (USB_ERR_PENDING_REQUESTS
);