xref: /freebsd-src/sys/dev/usb/usb_hub.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
502ac6454SAndrew Thompson  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
6c7cd6f80SHans Petter Selasky  * Copyright (c) 2008-2022 Hans Petter Selasky
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1002ac6454SAndrew Thompson  * are met:
1102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1602ac6454SAndrew Thompson  *
1702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1802ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1902ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2002ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2102ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2202ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2302ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2402ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2502ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2602ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2702ac6454SAndrew Thompson  * SUCH DAMAGE.
2802ac6454SAndrew Thompson  */
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /*
3102ac6454SAndrew Thompson  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
34d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
35d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
36d2b99310SHans Petter Selasky #else
37ed6d949aSAndrew Thompson #include <sys/stdint.h>
38ed6d949aSAndrew Thompson #include <sys/stddef.h>
39ed6d949aSAndrew Thompson #include <sys/param.h>
40ed6d949aSAndrew Thompson #include <sys/queue.h>
41ed6d949aSAndrew Thompson #include <sys/types.h>
42ed6d949aSAndrew Thompson #include <sys/systm.h>
43ed6d949aSAndrew Thompson #include <sys/kernel.h>
44ed6d949aSAndrew Thompson #include <sys/bus.h>
45ed6d949aSAndrew Thompson #include <sys/module.h>
46ed6d949aSAndrew Thompson #include <sys/lock.h>
47ed6d949aSAndrew Thompson #include <sys/mutex.h>
48ed6d949aSAndrew Thompson #include <sys/condvar.h>
49ddfc9c4cSWarner Losh #include <sys/sbuf.h>
50ed6d949aSAndrew Thompson #include <sys/sysctl.h>
51ed6d949aSAndrew Thompson #include <sys/sx.h>
52ed6d949aSAndrew Thompson #include <sys/unistd.h>
53ed6d949aSAndrew Thompson #include <sys/callout.h>
54ed6d949aSAndrew Thompson #include <sys/malloc.h>
55ed6d949aSAndrew Thompson #include <sys/priv.h>
56ed6d949aSAndrew Thompson 
5702ac6454SAndrew Thompson #include <dev/usb/usb.h>
58ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
59ae538d85SAndrew Thompson #include <dev/usb/usbdi_util.h>
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson #define	USB_DEBUG_VAR uhub_debug
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6502ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
76d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
7702ac6454SAndrew Thompson 
78e68fcc88STakanori Watanabe #include <dev/usb/usb_hub_private.h>
79e68fcc88STakanori Watanabe 
80ed6d949aSAndrew Thompson #ifdef USB_DEBUG
8102ac6454SAndrew Thompson static int uhub_debug = 0;
8202ac6454SAndrew Thompson 
83f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
84f8d2b1f3SPawel Biernacki     "USB HUB");
85af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
8602ac6454SAndrew Thompson     "Debug level");
8702ac6454SAndrew Thompson #endif
8802ac6454SAndrew Thompson 
8939307315SAndrew Thompson #if USB_HAVE_POWERD
90a593f6b8SAndrew Thompson static int usb_power_timeout = 30;	/* seconds */
9102ac6454SAndrew Thompson 
92ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
93a593f6b8SAndrew Thompson     &usb_power_timeout, 0, "USB power timeout");
9439307315SAndrew Thompson #endif
9502ac6454SAndrew Thompson 
96f80ccb40SHans Petter Selasky #if USB_HAVE_DISABLE_ENUM
97f80ccb40SHans Petter Selasky static int usb_disable_enumeration = 0;
98f80ccb40SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
99d384b167SHans Petter Selasky     &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
100d384b167SHans Petter Selasky 	"This can secure against USB devices turning evil, "
101d384b167SHans Petter Selasky 	"for example a USB memory stick becoming a USB keyboard.");
10277d68af5SHans Petter Selasky 
10377d68af5SHans Petter Selasky static int usb_disable_port_power = 0;
10477d68af5SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
10577d68af5SHans Petter Selasky     &usb_disable_port_power, 0, "Set to disable all USB port power.");
106f80ccb40SHans Petter Selasky #endif
107f80ccb40SHans Petter Selasky 
10802ac6454SAndrew Thompson #define	UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
10902ac6454SAndrew Thompson #define	UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
11002ac6454SAndrew Thompson #define	UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
1110a4cc48fSHans Petter Selasky #define	UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
112963169b4SHans Petter Selasky #define	UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
11302ac6454SAndrew Thompson 
11402ac6454SAndrew Thompson /* prototypes for type checking: */
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson static device_suspend_t uhub_suspend;
11702ac6454SAndrew Thompson static device_resume_t uhub_resume;
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson static bus_driver_added_t uhub_driver_added;
120ddfc9c4cSWarner Losh static bus_child_pnpinfo_t uhub_child_pnpinfo;
12102ac6454SAndrew Thompson 
122e0a69b51SAndrew Thompson static usb_callback_t uhub_intr_callback;
123a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
124a0d53e0bSHans Petter Selasky static usb_callback_t uhub_reset_tt_callback;
125a0d53e0bSHans Petter Selasky #endif
12602ac6454SAndrew Thompson 
127a593f6b8SAndrew Thompson static void usb_dev_resume_peer(struct usb_device *udev);
128a593f6b8SAndrew Thompson static void usb_dev_suspend_peer(struct usb_device *udev);
1292df1e9a6SAndrew Thompson static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
13002ac6454SAndrew Thompson 
131760bc48eSAndrew Thompson static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
132a0d53e0bSHans Petter Selasky 	[UHUB_INTR_TRANSFER] = {
13302ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
13402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
13502ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
1364eae601eSAndrew Thompson 		.timeout = 0,
1374eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1384eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
1394eae601eSAndrew Thompson 		.callback = &uhub_intr_callback,
1404eae601eSAndrew Thompson 		.interval = UHUB_INTR_INTERVAL,
14102ac6454SAndrew Thompson 	},
142a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
143a0d53e0bSHans Petter Selasky 	[UHUB_RESET_TT_TRANSFER] = {
144a0d53e0bSHans Petter Selasky 		.type = UE_CONTROL,
145a0d53e0bSHans Petter Selasky 		.endpoint = 0x00,	/* Control pipe */
146a0d53e0bSHans Petter Selasky 		.direction = UE_DIR_ANY,
147a0d53e0bSHans Petter Selasky 		.bufsize = sizeof(struct usb_device_request),
148a0d53e0bSHans Petter Selasky 		.callback = &uhub_reset_tt_callback,
149a0d53e0bSHans Petter Selasky 		.timeout = 1000,	/* 1 second */
150a0d53e0bSHans Petter Selasky 		.usb_mode = USB_MODE_HOST,
151a0d53e0bSHans Petter Selasky 	},
152a0d53e0bSHans Petter Selasky #endif
15302ac6454SAndrew Thompson };
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson /*
15602ac6454SAndrew Thompson  * driver instance for "hub" connected to "usb"
15702ac6454SAndrew Thompson  * and "hub" connected to "hub"
15802ac6454SAndrew Thompson  */
15939307315SAndrew Thompson static device_method_t uhub_methods[] = {
16002ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uhub_probe),
16102ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uhub_attach),
16202ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uhub_detach),
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, uhub_suspend),
16502ac6454SAndrew Thompson 	DEVMETHOD(device_resume, uhub_resume),
16602ac6454SAndrew Thompson 
167ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_location, uhub_child_location),
168ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_pnpinfo, uhub_child_pnpinfo),
1699c750429SWarner Losh 	DEVMETHOD(bus_get_device_path, uhub_get_device_path),
17002ac6454SAndrew Thompson 	DEVMETHOD(bus_driver_added, uhub_driver_added),
1716320afb5SHans Petter Selasky 	DEVMETHOD_END
17239307315SAndrew Thompson };
17339307315SAndrew Thompson 
174e68fcc88STakanori Watanabe driver_t uhub_driver = {
17539307315SAndrew Thompson 	.name = "uhub",
17639307315SAndrew Thompson 	.methods = uhub_methods,
17702ac6454SAndrew Thompson 	.size = sizeof(struct uhub_softc)
17802ac6454SAndrew Thompson };
17902ac6454SAndrew Thompson 
180bc9372d7SJohn Baldwin DRIVER_MODULE(uhub, usbus, uhub_driver, 0, 0);
181bc9372d7SJohn Baldwin DRIVER_MODULE(uhub, uhub, uhub_driver, 0, 0);
182910cb8feSAndrew Thompson MODULE_VERSION(uhub, 1);
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson static void
uhub_intr_callback(struct usb_xfer * xfer,usb_error_t error)185ed6d949aSAndrew Thompson uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
18602ac6454SAndrew Thompson {
187ed6d949aSAndrew Thompson 	struct uhub_softc *sc = usbd_xfer_softc(xfer);
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
19002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
19102ac6454SAndrew Thompson 		DPRINTFN(2, "\n");
19202ac6454SAndrew Thompson 		/*
19302ac6454SAndrew Thompson 		 * This is an indication that some port
19402ac6454SAndrew Thompson 		 * has changed status. Notify the bus
19502ac6454SAndrew Thompson 		 * event handler thread that we need
19602ac6454SAndrew Thompson 		 * to be explored again:
19702ac6454SAndrew Thompson 		 */
198a593f6b8SAndrew Thompson 		usb_needs_explore(sc->sc_udev->bus, 0);
19902ac6454SAndrew Thompson 
20002ac6454SAndrew Thompson 	case USB_ST_SETUP:
201ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
202a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
20302ac6454SAndrew Thompson 		break;
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson 	default:			/* Error */
20602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
20702ac6454SAndrew Thompson 			/*
20802ac6454SAndrew Thompson 			 * Do a clear-stall. The "stall_pipe" flag
20902ac6454SAndrew Thompson 			 * will get cleared before next callback by
21002ac6454SAndrew Thompson 			 * the USB stack.
21102ac6454SAndrew Thompson 			 */
212ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
213ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
214a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
21502ac6454SAndrew Thompson 		}
21602ac6454SAndrew Thompson 		break;
21702ac6454SAndrew Thompson 	}
21802ac6454SAndrew Thompson }
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
221a0d53e0bSHans Petter Selasky  *      uhub_reset_tt_proc
222a0d53e0bSHans Petter Selasky  *
223a0d53e0bSHans Petter Selasky  * This function starts the TT reset USB request
224a0d53e0bSHans Petter Selasky  *------------------------------------------------------------------------*/
225a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
226a0d53e0bSHans Petter Selasky static void
uhub_reset_tt_proc(struct usb_proc_msg * _pm)227a0d53e0bSHans Petter Selasky uhub_reset_tt_proc(struct usb_proc_msg *_pm)
228a0d53e0bSHans Petter Selasky {
229a0d53e0bSHans Petter Selasky 	struct usb_udev_msg *pm = (void *)_pm;
230a0d53e0bSHans Petter Selasky 	struct usb_device *udev = pm->udev;
231a0d53e0bSHans Petter Selasky 	struct usb_hub *hub;
232a0d53e0bSHans Petter Selasky 	struct uhub_softc *sc;
233a0d53e0bSHans Petter Selasky 
234a0d53e0bSHans Petter Selasky 	hub = udev->hub;
235a0d53e0bSHans Petter Selasky 	if (hub == NULL)
236a0d53e0bSHans Petter Selasky 		return;
237a0d53e0bSHans Petter Selasky 	sc = hub->hubsoftc;
238a0d53e0bSHans Petter Selasky 	if (sc == NULL)
239a0d53e0bSHans Petter Selasky 		return;
240a0d53e0bSHans Petter Selasky 
241a0d53e0bSHans Petter Selasky 	/* Change lock */
242a0d53e0bSHans Petter Selasky 	USB_BUS_UNLOCK(udev->bus);
2430eb8d462SHans Petter Selasky 	USB_MTX_LOCK(&sc->sc_mtx);
244a0d53e0bSHans Petter Selasky 	/* Start transfer */
245a0d53e0bSHans Petter Selasky 	usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
246a0d53e0bSHans Petter Selasky 	/* Change lock */
2470eb8d462SHans Petter Selasky 	USB_MTX_UNLOCK(&sc->sc_mtx);
248a0d53e0bSHans Petter Selasky 	USB_BUS_LOCK(udev->bus);
249a0d53e0bSHans Petter Selasky }
250a0d53e0bSHans Petter Selasky #endif
251a0d53e0bSHans Petter Selasky 
252a0d53e0bSHans Petter Selasky /*------------------------------------------------------------------------*
253a0d53e0bSHans Petter Selasky  *      uhub_tt_buffer_reset_async_locked
254a0d53e0bSHans Petter Selasky  *
255a0d53e0bSHans Petter Selasky  * This function queues a TT reset for the given USB device and endpoint.
256a0d53e0bSHans Petter Selasky  *------------------------------------------------------------------------*/
257a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
258a0d53e0bSHans Petter Selasky void
uhub_tt_buffer_reset_async_locked(struct usb_device * child,struct usb_endpoint * ep)259a0d53e0bSHans Petter Selasky uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
260a0d53e0bSHans Petter Selasky {
261a0d53e0bSHans Petter Selasky 	struct usb_device_request req;
262a0d53e0bSHans Petter Selasky 	struct usb_device *udev;
263a0d53e0bSHans Petter Selasky 	struct usb_hub *hub;
264a0d53e0bSHans Petter Selasky 	struct usb_port *up;
265a0d53e0bSHans Petter Selasky 	uint16_t wValue;
266a0d53e0bSHans Petter Selasky 	uint8_t port;
267a0d53e0bSHans Petter Selasky 
268a0d53e0bSHans Petter Selasky 	if (child == NULL || ep == NULL)
269a0d53e0bSHans Petter Selasky 		return;
270a0d53e0bSHans Petter Selasky 
271a0d53e0bSHans Petter Selasky 	udev = child->parent_hs_hub;
272a0d53e0bSHans Petter Selasky 	port = child->hs_port_no;
273a0d53e0bSHans Petter Selasky 
274a0d53e0bSHans Petter Selasky 	if (udev == NULL)
275a0d53e0bSHans Petter Selasky 		return;
276a0d53e0bSHans Petter Selasky 
277a0d53e0bSHans Petter Selasky 	hub = udev->hub;
278a0d53e0bSHans Petter Selasky 	if ((hub == NULL) ||
279a0d53e0bSHans Petter Selasky 	    (udev->speed != USB_SPEED_HIGH) ||
280a0d53e0bSHans Petter Selasky 	    (child->speed != USB_SPEED_LOW &&
281a0d53e0bSHans Petter Selasky 	     child->speed != USB_SPEED_FULL) ||
282a0d53e0bSHans Petter Selasky 	    (child->flags.usb_mode != USB_MODE_HOST) ||
283a0d53e0bSHans Petter Selasky 	    (port == 0) || (ep->edesc == NULL)) {
284a0d53e0bSHans Petter Selasky 		/* not applicable */
285a0d53e0bSHans Petter Selasky 		return;
286a0d53e0bSHans Petter Selasky 	}
287a0d53e0bSHans Petter Selasky 
288a0d53e0bSHans Petter Selasky 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
289a0d53e0bSHans Petter Selasky 
290a0d53e0bSHans Petter Selasky 	up = hub->ports + port - 1;
291a0d53e0bSHans Petter Selasky 
292a0d53e0bSHans Petter Selasky 	if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
293a0d53e0bSHans Petter Selasky 	    udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
294a0d53e0bSHans Petter Selasky 		port = 1;
295a0d53e0bSHans Petter Selasky 
296a0d53e0bSHans Petter Selasky 	/* if we already received a clear buffer request, reset the whole TT */
297a0d53e0bSHans Petter Selasky 	if (up->req_reset_tt.bRequest != 0) {
298a0d53e0bSHans Petter Selasky 		req.bmRequestType = UT_WRITE_CLASS_OTHER;
299a0d53e0bSHans Petter Selasky 		req.bRequest = UR_RESET_TT;
300a0d53e0bSHans Petter Selasky 		USETW(req.wValue, 0);
301a0d53e0bSHans Petter Selasky 		req.wIndex[0] = port;
302a0d53e0bSHans Petter Selasky 		req.wIndex[1] = 0;
303a0d53e0bSHans Petter Selasky 		USETW(req.wLength, 0);
304a0d53e0bSHans Petter Selasky 	} else {
305a0d53e0bSHans Petter Selasky 		wValue = (ep->edesc->bEndpointAddress & 0xF) |
306a0d53e0bSHans Petter Selasky 		      ((child->address & 0x7F) << 4) |
307a0d53e0bSHans Petter Selasky 		      ((ep->edesc->bEndpointAddress & 0x80) << 8) |
308a0d53e0bSHans Petter Selasky 		      ((ep->edesc->bmAttributes & 3) << 12);
309a0d53e0bSHans Petter Selasky 
310a0d53e0bSHans Petter Selasky 		req.bmRequestType = UT_WRITE_CLASS_OTHER;
311a0d53e0bSHans Petter Selasky 		req.bRequest = UR_CLEAR_TT_BUFFER;
312a0d53e0bSHans Petter Selasky 		USETW(req.wValue, wValue);
313a0d53e0bSHans Petter Selasky 		req.wIndex[0] = port;
314a0d53e0bSHans Petter Selasky 		req.wIndex[1] = 0;
315a0d53e0bSHans Petter Selasky 		USETW(req.wLength, 0);
316a0d53e0bSHans Petter Selasky 	}
317a0d53e0bSHans Petter Selasky 	up->req_reset_tt = req;
318a0d53e0bSHans Petter Selasky 	/* get reset transfer started */
31943ea03d7SHans Petter Selasky 	usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
320a0d53e0bSHans Petter Selasky 	    &hub->tt_msg[0], &hub->tt_msg[1]);
321a0d53e0bSHans Petter Selasky }
322a0d53e0bSHans Petter Selasky #endif
323a0d53e0bSHans Petter Selasky 
324a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
325a0d53e0bSHans Petter Selasky static void
uhub_reset_tt_callback(struct usb_xfer * xfer,usb_error_t error)326a0d53e0bSHans Petter Selasky uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
327a0d53e0bSHans Petter Selasky {
328a0d53e0bSHans Petter Selasky 	struct uhub_softc *sc;
329a0d53e0bSHans Petter Selasky 	struct usb_device *udev;
330a0d53e0bSHans Petter Selasky 	struct usb_port *up;
331a0d53e0bSHans Petter Selasky 	uint8_t x;
332a0d53e0bSHans Petter Selasky 
333a0d53e0bSHans Petter Selasky 	DPRINTF("TT buffer reset\n");
334a0d53e0bSHans Petter Selasky 
335a0d53e0bSHans Petter Selasky 	sc = usbd_xfer_softc(xfer);
336a0d53e0bSHans Petter Selasky 	udev = sc->sc_udev;
337a0d53e0bSHans Petter Selasky 
338a0d53e0bSHans Petter Selasky 	switch (USB_GET_STATE(xfer)) {
339a0d53e0bSHans Petter Selasky 	case USB_ST_TRANSFERRED:
340a0d53e0bSHans Petter Selasky 	case USB_ST_SETUP:
341a0d53e0bSHans Petter Selasky tr_setup:
342a0d53e0bSHans Petter Selasky 		USB_BUS_LOCK(udev->bus);
343a0d53e0bSHans Petter Selasky 		/* find first port which needs a TT reset */
344a0d53e0bSHans Petter Selasky 		for (x = 0; x != udev->hub->nports; x++) {
345a0d53e0bSHans Petter Selasky 			up = udev->hub->ports + x;
346a0d53e0bSHans Petter Selasky 
347a0d53e0bSHans Petter Selasky 			if (up->req_reset_tt.bRequest == 0)
348a0d53e0bSHans Petter Selasky 				continue;
349a0d53e0bSHans Petter Selasky 
350a0d53e0bSHans Petter Selasky 			/* copy in the transfer */
351a0d53e0bSHans Petter Selasky 			usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
352a0d53e0bSHans Petter Selasky 			    sizeof(up->req_reset_tt));
353a0d53e0bSHans Petter Selasky 			/* reset buffer */
354a0d53e0bSHans Petter Selasky 			memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
355a0d53e0bSHans Petter Selasky 
356a0d53e0bSHans Petter Selasky 			/* set length */
357a0d53e0bSHans Petter Selasky 			usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
358a0d53e0bSHans Petter Selasky 			xfer->nframes = 1;
359a0d53e0bSHans Petter Selasky 			USB_BUS_UNLOCK(udev->bus);
360a0d53e0bSHans Petter Selasky 
361a0d53e0bSHans Petter Selasky 			usbd_transfer_submit(xfer);
362a0d53e0bSHans Petter Selasky 			return;
363a0d53e0bSHans Petter Selasky 		}
364a0d53e0bSHans Petter Selasky 		USB_BUS_UNLOCK(udev->bus);
365a0d53e0bSHans Petter Selasky 		break;
366a0d53e0bSHans Petter Selasky 
367a0d53e0bSHans Petter Selasky 	default:
368a0d53e0bSHans Petter Selasky 		if (error == USB_ERR_CANCELLED)
369a0d53e0bSHans Petter Selasky 			break;
370a0d53e0bSHans Petter Selasky 
371a0d53e0bSHans Petter Selasky 		DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
372a0d53e0bSHans Petter Selasky 		goto tr_setup;
373a0d53e0bSHans Petter Selasky 	}
374a0d53e0bSHans Petter Selasky }
375a0d53e0bSHans Petter Selasky #endif
376a0d53e0bSHans Petter Selasky 
377a0d53e0bSHans Petter Selasky /*------------------------------------------------------------------------*
378a0d53e0bSHans Petter Selasky  *      uhub_count_active_host_ports
379a0d53e0bSHans Petter Selasky  *
380a0d53e0bSHans Petter Selasky  * This function counts the number of active ports at the given speed.
381a0d53e0bSHans Petter Selasky  *------------------------------------------------------------------------*/
382a0d53e0bSHans Petter Selasky uint8_t
uhub_count_active_host_ports(struct usb_device * udev,enum usb_dev_speed speed)383a0d53e0bSHans Petter Selasky uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
384a0d53e0bSHans Petter Selasky {
385a0d53e0bSHans Petter Selasky 	struct uhub_softc *sc;
386a0d53e0bSHans Petter Selasky 	struct usb_device *child;
387a0d53e0bSHans Petter Selasky 	struct usb_hub *hub;
388a0d53e0bSHans Petter Selasky 	struct usb_port *up;
389a0d53e0bSHans Petter Selasky 	uint8_t retval = 0;
390a0d53e0bSHans Petter Selasky 	uint8_t x;
391a0d53e0bSHans Petter Selasky 
392a0d53e0bSHans Petter Selasky 	if (udev == NULL)
393a0d53e0bSHans Petter Selasky 		goto done;
394a0d53e0bSHans Petter Selasky 	hub = udev->hub;
395a0d53e0bSHans Petter Selasky 	if (hub == NULL)
396a0d53e0bSHans Petter Selasky 		goto done;
397a0d53e0bSHans Petter Selasky 	sc = hub->hubsoftc;
398a0d53e0bSHans Petter Selasky 	if (sc == NULL)
399a0d53e0bSHans Petter Selasky 		goto done;
400a0d53e0bSHans Petter Selasky 
401a0d53e0bSHans Petter Selasky 	for (x = 0; x != hub->nports; x++) {
402a0d53e0bSHans Petter Selasky 		up = hub->ports + x;
403a0d53e0bSHans Petter Selasky 		child = usb_bus_port_get_device(udev->bus, up);
404a0d53e0bSHans Petter Selasky 		if (child != NULL &&
405a0d53e0bSHans Petter Selasky 		    child->flags.usb_mode == USB_MODE_HOST &&
406a0d53e0bSHans Petter Selasky 		    child->speed == speed)
407a0d53e0bSHans Petter Selasky 			retval++;
408a0d53e0bSHans Petter Selasky 	}
409a0d53e0bSHans Petter Selasky done:
410a0d53e0bSHans Petter Selasky 	return (retval);
411a0d53e0bSHans Petter Selasky }
412a0d53e0bSHans Petter Selasky 
413d64e9217SHans Petter Selasky void
uhub_explore_handle_re_enumerate(struct usb_device * child)414d64e9217SHans Petter Selasky uhub_explore_handle_re_enumerate(struct usb_device *child)
415d64e9217SHans Petter Selasky {
416d64e9217SHans Petter Selasky 	uint8_t do_unlock;
417d64e9217SHans Petter Selasky 	usb_error_t err;
418d64e9217SHans Petter Selasky 
419d64e9217SHans Petter Selasky 	/* check if device should be re-enumerated */
420d64e9217SHans Petter Selasky 	if (child->flags.usb_mode != USB_MODE_HOST)
421d64e9217SHans Petter Selasky 		return;
422d64e9217SHans Petter Selasky 
423d64e9217SHans Petter Selasky 	do_unlock = usbd_enum_lock(child);
424d64e9217SHans Petter Selasky 	switch (child->re_enumerate_wait) {
425d64e9217SHans Petter Selasky 	case USB_RE_ENUM_START:
426d64e9217SHans Petter Selasky 		err = usbd_set_config_index(child,
427d64e9217SHans Petter Selasky 		    USB_UNCONFIG_INDEX);
428d64e9217SHans Petter Selasky 		if (err != 0) {
429d64e9217SHans Petter Selasky 			DPRINTF("Unconfigure failed: %s: Ignored.\n",
430d64e9217SHans Petter Selasky 			    usbd_errstr(err));
431d64e9217SHans Petter Selasky 		}
432d64e9217SHans Petter Selasky 		if (child->parent_hub == NULL) {
433d64e9217SHans Petter Selasky 			/* the root HUB cannot be re-enumerated */
434d64e9217SHans Petter Selasky 			DPRINTFN(6, "cannot reset root HUB\n");
435d64e9217SHans Petter Selasky 			err = 0;
436d64e9217SHans Petter Selasky 		} else {
437d64e9217SHans Petter Selasky 			err = usbd_req_re_enumerate(child, NULL);
438d64e9217SHans Petter Selasky 		}
439f54ab96dSHans Petter Selasky 		if (err == 0) {
440f54ab96dSHans Petter Selasky 			/* refresh device strings */
441f54ab96dSHans Petter Selasky 			usb_get_langid(child);
442f54ab96dSHans Petter Selasky 			usb_set_device_strings(child);
443f54ab96dSHans Petter Selasky 
444f54ab96dSHans Petter Selasky 			/* set default configuration */
445d64e9217SHans Petter Selasky 			err = usbd_set_config_index(child, 0);
446f54ab96dSHans Petter Selasky 		}
447d64e9217SHans Petter Selasky 		if (err == 0) {
448d64e9217SHans Petter Selasky 			err = usb_probe_and_attach(child,
449d64e9217SHans Petter Selasky 			    USB_IFACE_INDEX_ANY);
450d64e9217SHans Petter Selasky 		}
451d64e9217SHans Petter Selasky 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
452d64e9217SHans Petter Selasky 		break;
453d64e9217SHans Petter Selasky 
454d64e9217SHans Petter Selasky 	case USB_RE_ENUM_PWR_OFF:
455d64e9217SHans Petter Selasky 		/* get the device unconfigured */
456d64e9217SHans Petter Selasky 		err = usbd_set_config_index(child,
457d64e9217SHans Petter Selasky 		    USB_UNCONFIG_INDEX);
458d64e9217SHans Petter Selasky 		if (err) {
459d64e9217SHans Petter Selasky 			DPRINTFN(0, "Could not unconfigure "
460d64e9217SHans Petter Selasky 			    "device (ignored)\n");
461d64e9217SHans Petter Selasky 		}
462d64e9217SHans Petter Selasky 		if (child->parent_hub == NULL) {
463d64e9217SHans Petter Selasky 			/* the root HUB cannot be re-enumerated */
464d64e9217SHans Petter Selasky 			DPRINTFN(6, "cannot set port feature\n");
465d64e9217SHans Petter Selasky 			err = 0;
466d64e9217SHans Petter Selasky 		} else {
467d64e9217SHans Petter Selasky 			/* clear port enable */
468d64e9217SHans Petter Selasky 			err = usbd_req_clear_port_feature(child->parent_hub,
469d64e9217SHans Petter Selasky 			    NULL, child->port_no, UHF_PORT_ENABLE);
470d64e9217SHans Petter Selasky 			if (err) {
471d64e9217SHans Petter Selasky 				DPRINTFN(0, "Could not disable port "
472d64e9217SHans Petter Selasky 				    "(ignored)\n");
473d64e9217SHans Petter Selasky 			}
474d64e9217SHans Petter Selasky 		}
475d64e9217SHans Petter Selasky 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
476d64e9217SHans Petter Selasky 		break;
477d64e9217SHans Petter Selasky 
478d64e9217SHans Petter Selasky 	case USB_RE_ENUM_SET_CONFIG:
479d64e9217SHans Petter Selasky 		err = usbd_set_config_index(child,
480d64e9217SHans Petter Selasky 		    child->next_config_index);
481d64e9217SHans Petter Selasky 		if (err != 0) {
482d64e9217SHans Petter Selasky 			DPRINTF("Configure failed: %s: Ignored.\n",
483d64e9217SHans Petter Selasky 			    usbd_errstr(err));
484d64e9217SHans Petter Selasky 		} else {
485d64e9217SHans Petter Selasky 			err = usb_probe_and_attach(child,
486d64e9217SHans Petter Selasky 			    USB_IFACE_INDEX_ANY);
487d64e9217SHans Petter Selasky 		}
488d64e9217SHans Petter Selasky 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
489d64e9217SHans Petter Selasky 		break;
490d64e9217SHans Petter Selasky 
491d64e9217SHans Petter Selasky 	default:
492d64e9217SHans Petter Selasky 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
493d64e9217SHans Petter Selasky 		break;
494d64e9217SHans Petter Selasky 	}
495d64e9217SHans Petter Selasky 	if (do_unlock)
496d64e9217SHans Petter Selasky 		usbd_enum_unlock(child);
497d64e9217SHans Petter Selasky }
498d64e9217SHans Petter Selasky 
499a0d53e0bSHans Petter Selasky /*------------------------------------------------------------------------*
50002ac6454SAndrew Thompson  *	uhub_explore_sub - subroutine
50102ac6454SAndrew Thompson  *
50202ac6454SAndrew Thompson  * Return values:
50302ac6454SAndrew Thompson  *    0: Success
50402ac6454SAndrew Thompson  * Else: A control transaction failed
50502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
506e0a69b51SAndrew Thompson static usb_error_t
uhub_explore_sub(struct uhub_softc * sc,struct usb_port * up)507760bc48eSAndrew Thompson uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
50802ac6454SAndrew Thompson {
509760bc48eSAndrew Thompson 	struct usb_bus *bus;
510760bc48eSAndrew Thompson 	struct usb_device *child;
51102ac6454SAndrew Thompson 	uint8_t refcount;
512e0a69b51SAndrew Thompson 	usb_error_t err;
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	bus = sc->sc_udev->bus;
515e5ff940aSHans Petter Selasky 	err = USB_ERR_NORMAL_COMPLETION;
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 	/* get driver added refcount from USB bus */
51802ac6454SAndrew Thompson 	refcount = bus->driver_added_refcount;
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson 	/* get device assosiated with the given port */
521a593f6b8SAndrew Thompson 	child = usb_bus_port_get_device(bus, up);
52202ac6454SAndrew Thompson 	if (child == NULL) {
52302ac6454SAndrew Thompson 		/* nothing to do */
52402ac6454SAndrew Thompson 		goto done;
52502ac6454SAndrew Thompson 	}
526963169b4SHans Petter Selasky 
527d64e9217SHans Petter Selasky 	uhub_explore_handle_re_enumerate(child);
5288f9750b7SHans Petter Selasky 
52902ac6454SAndrew Thompson 	/* check if probe and attach should be done */
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson 	if (child->driver_added_refcount != refcount) {
53202ac6454SAndrew Thompson 		child->driver_added_refcount = refcount;
533a593f6b8SAndrew Thompson 		err = usb_probe_and_attach(child,
53402ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
53502ac6454SAndrew Thompson 		if (err) {
53602ac6454SAndrew Thompson 			goto done;
53702ac6454SAndrew Thompson 		}
53802ac6454SAndrew Thompson 	}
53902ac6454SAndrew Thompson 	/* start control transfer, if device mode */
54002ac6454SAndrew Thompson 
541963169b4SHans Petter Selasky 	if (child->flags.usb_mode == USB_MODE_DEVICE)
5425b3bb704SAndrew Thompson 		usbd_ctrl_transfer_setup(child);
543963169b4SHans Petter Selasky 
54402ac6454SAndrew Thompson 	/* if a HUB becomes present, do a recursive HUB explore */
54502ac6454SAndrew Thompson 
546963169b4SHans Petter Selasky 	if (child->hub)
54702ac6454SAndrew Thompson 		err = (child->hub->explore) (child);
548963169b4SHans Petter Selasky 
54902ac6454SAndrew Thompson done:
55002ac6454SAndrew Thompson 	return (err);
55102ac6454SAndrew Thompson }
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
55402ac6454SAndrew Thompson  *	uhub_read_port_status - factored out code
55502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
556e0a69b51SAndrew Thompson static usb_error_t
uhub_read_port_status(struct uhub_softc * sc,uint8_t portno)55702ac6454SAndrew Thompson uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
55802ac6454SAndrew Thompson {
559760bc48eSAndrew Thompson 	struct usb_port_status ps;
560e0a69b51SAndrew Thompson 	usb_error_t err;
56102ac6454SAndrew Thompson 
5624604b6a1SHans Petter Selasky 	if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
5634604b6a1SHans Petter Selasky 		DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
5644604b6a1SHans Petter Selasky 		sc->sc_st.port_status = 0;
5654604b6a1SHans Petter Selasky 		sc->sc_st.port_change = 0;
5664604b6a1SHans Petter Selasky 		return (USB_ERR_TIMEOUT);
5674604b6a1SHans Petter Selasky 	}
5684604b6a1SHans Petter Selasky 
569a593f6b8SAndrew Thompson 	err = usbd_req_get_port_status(
570e2805033SAndrew Thompson 	    sc->sc_udev, NULL, &ps, portno);
57102ac6454SAndrew Thompson 
5724604b6a1SHans Petter Selasky 	if (err == 0) {
57302ac6454SAndrew Thompson 		sc->sc_st.port_status = UGETW(ps.wPortStatus);
57402ac6454SAndrew Thompson 		sc->sc_st.port_change = UGETW(ps.wPortChange);
5754604b6a1SHans Petter Selasky 		sc->sc_usb_port_errors = 0;
5764604b6a1SHans Petter Selasky 	} else {
5774604b6a1SHans Petter Selasky 		sc->sc_st.port_status = 0;
5784604b6a1SHans Petter Selasky 		sc->sc_st.port_change = 0;
5794604b6a1SHans Petter Selasky 		sc->sc_usb_port_errors++;
5804604b6a1SHans Petter Selasky 	}
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	/* debugging print */
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 	DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
58502ac6454SAndrew Thompson 	    "wPortChange=0x%04x, err=%s\n",
58602ac6454SAndrew Thompson 	    portno, sc->sc_st.port_status,
587a593f6b8SAndrew Thompson 	    sc->sc_st.port_change, usbd_errstr(err));
58802ac6454SAndrew Thompson 	return (err);
58902ac6454SAndrew Thompson }
59002ac6454SAndrew Thompson 
59102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
59202ac6454SAndrew Thompson  *	uhub_reattach_port
59302ac6454SAndrew Thompson  *
59402ac6454SAndrew Thompson  * Returns:
59502ac6454SAndrew Thompson  *    0: Success
59602ac6454SAndrew Thompson  * Else: A control transaction failed
59702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
598e0a69b51SAndrew Thompson static usb_error_t
uhub_reattach_port(struct uhub_softc * sc,uint8_t portno)59902ac6454SAndrew Thompson uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
60002ac6454SAndrew Thompson {
601760bc48eSAndrew Thompson 	struct usb_device *child;
602760bc48eSAndrew Thompson 	struct usb_device *udev;
6038d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
6048d2dd5ddSAndrew Thompson 	enum usb_hc_mode mode;
605e0a69b51SAndrew Thompson 	usb_error_t err;
60699b0e607SHans Petter Selasky 	uint16_t power_mask;
60702ac6454SAndrew Thompson 	uint8_t timeout;
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 	DPRINTF("reattaching port %d\n", portno);
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 	timeout = 0;
61202ac6454SAndrew Thompson 	udev = sc->sc_udev;
613a593f6b8SAndrew Thompson 	child = usb_bus_port_get_device(udev->bus,
61402ac6454SAndrew Thompson 	    udev->hub->ports + portno - 1);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson repeat:
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 	/* first clear the port connection change bit */
61902ac6454SAndrew Thompson 
620a593f6b8SAndrew Thompson 	err = usbd_req_clear_port_feature(udev, NULL,
62102ac6454SAndrew Thompson 	    portno, UHF_C_PORT_CONNECTION);
62202ac6454SAndrew Thompson 
623f80ccb40SHans Petter Selasky 	if (err)
62402ac6454SAndrew Thompson 		goto error;
625f80ccb40SHans Petter Selasky 
626d88688c7SAndrew Thompson 	/* check if there is a child */
62702ac6454SAndrew Thompson 
628d88688c7SAndrew Thompson 	if (child != NULL) {
629d88688c7SAndrew Thompson 		/*
630d88688c7SAndrew Thompson 		 * Free USB device and all subdevices, if any.
631d88688c7SAndrew Thompson 		 */
632d88688c7SAndrew Thompson 		usb_free_device(child, 0);
63302ac6454SAndrew Thompson 		child = NULL;
63402ac6454SAndrew Thompson 	}
63502ac6454SAndrew Thompson 	/* get fresh status */
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	err = uhub_read_port_status(sc, portno);
638f80ccb40SHans Petter Selasky 	if (err)
639f80ccb40SHans Petter Selasky 		goto error;
640f80ccb40SHans Petter Selasky 
641f80ccb40SHans Petter Selasky #if USB_HAVE_DISABLE_ENUM
642f80ccb40SHans Petter Selasky 	/* check if we should skip enumeration from this USB HUB */
643f80ccb40SHans Petter Selasky 	if (usb_disable_enumeration != 0 ||
644f80ccb40SHans Petter Selasky 	    sc->sc_disable_enumeration != 0) {
645f80ccb40SHans Petter Selasky 		DPRINTF("Enumeration is disabled!\n");
64602ac6454SAndrew Thompson 		goto error;
64702ac6454SAndrew Thompson 	}
648f80ccb40SHans Petter Selasky #endif
64902ac6454SAndrew Thompson 	/* check if nothing is connected to the port */
65002ac6454SAndrew Thompson 
651f80ccb40SHans Petter Selasky 	if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
65202ac6454SAndrew Thompson 		goto error;
653f80ccb40SHans Petter Selasky 
65402ac6454SAndrew Thompson 	/* check if there is no power on the port and print a warning */
65502ac6454SAndrew Thompson 
6564131f6fbSHans Petter Selasky 	switch (udev->speed) {
6574131f6fbSHans Petter Selasky 	case USB_SPEED_HIGH:
6584131f6fbSHans Petter Selasky 	case USB_SPEED_FULL:
6594131f6fbSHans Petter Selasky 	case USB_SPEED_LOW:
66099b0e607SHans Petter Selasky 		power_mask = UPS_PORT_POWER;
6614131f6fbSHans Petter Selasky 		break;
6624131f6fbSHans Petter Selasky 	case USB_SPEED_SUPER:
66399b0e607SHans Petter Selasky 		if (udev->parent_hub == NULL)
664601ee538SHans Petter Selasky 			power_mask = 0;	/* XXX undefined */
66599b0e607SHans Petter Selasky 		else
66699b0e607SHans Petter Selasky 			power_mask = UPS_PORT_POWER_SS;
6674131f6fbSHans Petter Selasky 		break;
6684131f6fbSHans Petter Selasky 	default:
66999b0e607SHans Petter Selasky 		power_mask = 0;
6704131f6fbSHans Petter Selasky 		break;
6714131f6fbSHans Petter Selasky 	}
672601ee538SHans Petter Selasky 	if ((sc->sc_st.port_status & power_mask) != power_mask) {
67399b0e607SHans Petter Selasky 		DPRINTF("WARNING: strange, connected port %d "
67499b0e607SHans Petter Selasky 		    "has no power\n", portno);
67599b0e607SHans Petter Selasky 	}
6764131f6fbSHans Petter Selasky 
67702ac6454SAndrew Thompson 	/* check if the device is in Host Mode */
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 	if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
68002ac6454SAndrew Thompson 		DPRINTF("Port %d is in Host Mode\n", portno);
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson 		if (sc->sc_st.port_status & UPS_SUSPEND) {
683963169b4SHans Petter Selasky 			/*
684963169b4SHans Petter Selasky 			 * NOTE: Should not get here in SuperSpeed
685963169b4SHans Petter Selasky 			 * mode, because the HUB should report this
686963169b4SHans Petter Selasky 			 * bit as zero.
687963169b4SHans Petter Selasky 			 */
68802ac6454SAndrew Thompson 			DPRINTF("Port %d was still "
68902ac6454SAndrew Thompson 			    "suspended, clearing.\n", portno);
690963169b4SHans Petter Selasky 			err = usbd_req_clear_port_feature(udev,
691e2805033SAndrew Thompson 			    NULL, portno, UHF_PORT_SUSPEND);
69202ac6454SAndrew Thompson 		}
693963169b4SHans Petter Selasky 
69402ac6454SAndrew Thompson 		/* USB Host Mode */
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 		/* wait for maximum device power up time */
69702ac6454SAndrew Thompson 
698a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL,
69937506412SHans Petter Selasky 		    USB_MS_TO_TICKS(usb_port_powerup_delay));
70002ac6454SAndrew Thompson 
70102ac6454SAndrew Thompson 		/* reset port, which implies enabling it */
70202ac6454SAndrew Thompson 
703a593f6b8SAndrew Thompson 		err = usbd_req_reset_port(udev, NULL, portno);
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson 		if (err) {
70602ac6454SAndrew Thompson 			DPRINTFN(0, "port %d reset "
70702ac6454SAndrew Thompson 			    "failed, error=%s\n",
708a593f6b8SAndrew Thompson 			    portno, usbd_errstr(err));
70902ac6454SAndrew Thompson 			goto error;
71002ac6454SAndrew Thompson 		}
71102ac6454SAndrew Thompson 		/* get port status again, it might have changed during reset */
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 		err = uhub_read_port_status(sc, portno);
71402ac6454SAndrew Thompson 		if (err) {
71502ac6454SAndrew Thompson 			goto error;
71602ac6454SAndrew Thompson 		}
71702ac6454SAndrew Thompson 		/* check if something changed during port reset */
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson 		if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
72002ac6454SAndrew Thompson 		    (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
72102ac6454SAndrew Thompson 			if (timeout) {
7225c447fe6SMaksym Stetsyuk 				DPRINTFN(1, "giving up port %d reset - "
7232144eb75SBjoern A. Zeeb 				   "device vanished: change %#x status %#x\n",
7242144eb75SBjoern A. Zeeb 				   portno, sc->sc_st.port_change,
7252144eb75SBjoern A. Zeeb 				   sc->sc_st.port_status);
72602ac6454SAndrew Thompson 				goto error;
72702ac6454SAndrew Thompson 			}
72802ac6454SAndrew Thompson 			timeout = 1;
72902ac6454SAndrew Thompson 			goto repeat;
73002ac6454SAndrew Thompson 		}
73102ac6454SAndrew Thompson 	} else {
73202ac6454SAndrew Thompson 		DPRINTF("Port %d is in Device Mode\n", portno);
73302ac6454SAndrew Thompson 	}
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	/*
73602ac6454SAndrew Thompson 	 * Figure out the device speed
73702ac6454SAndrew Thompson 	 */
73802ac6454SAndrew Thompson 	switch (udev->speed) {
73902ac6454SAndrew Thompson 	case USB_SPEED_HIGH:
74002ac6454SAndrew Thompson 		if (sc->sc_st.port_status & UPS_HIGH_SPEED)
74102ac6454SAndrew Thompson 			speed = USB_SPEED_HIGH;
74202ac6454SAndrew Thompson 		else if (sc->sc_st.port_status & UPS_LOW_SPEED)
74302ac6454SAndrew Thompson 			speed = USB_SPEED_LOW;
74402ac6454SAndrew Thompson 		else
74502ac6454SAndrew Thompson 			speed = USB_SPEED_FULL;
74602ac6454SAndrew Thompson 		break;
74702ac6454SAndrew Thompson 	case USB_SPEED_FULL:
74802ac6454SAndrew Thompson 		if (sc->sc_st.port_status & UPS_LOW_SPEED)
74902ac6454SAndrew Thompson 			speed = USB_SPEED_LOW;
75002ac6454SAndrew Thompson 		else
75102ac6454SAndrew Thompson 			speed = USB_SPEED_FULL;
75202ac6454SAndrew Thompson 		break;
75302ac6454SAndrew Thompson 	case USB_SPEED_LOW:
75402ac6454SAndrew Thompson 		speed = USB_SPEED_LOW;
75502ac6454SAndrew Thompson 		break;
756963169b4SHans Petter Selasky 	case USB_SPEED_SUPER:
757963169b4SHans Petter Selasky 		if (udev->parent_hub == NULL) {
758963169b4SHans Petter Selasky 			/* Root HUB - special case */
759963169b4SHans Petter Selasky 			switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
760963169b4SHans Petter Selasky 			case 0:
761963169b4SHans Petter Selasky 				speed = USB_SPEED_FULL;
762963169b4SHans Petter Selasky 				break;
763963169b4SHans Petter Selasky 			case UPS_LOW_SPEED:
764963169b4SHans Petter Selasky 				speed = USB_SPEED_LOW;
765963169b4SHans Petter Selasky 				break;
766963169b4SHans Petter Selasky 			case UPS_HIGH_SPEED:
767963169b4SHans Petter Selasky 				speed = USB_SPEED_HIGH;
768963169b4SHans Petter Selasky 				break;
769963169b4SHans Petter Selasky 			default:
770963169b4SHans Petter Selasky 				speed = USB_SPEED_SUPER;
771963169b4SHans Petter Selasky 				break;
772963169b4SHans Petter Selasky 			}
773963169b4SHans Petter Selasky 		} else {
774963169b4SHans Petter Selasky 			speed = USB_SPEED_SUPER;
775963169b4SHans Petter Selasky 		}
776963169b4SHans Petter Selasky 		break;
77702ac6454SAndrew Thompson 	default:
77802ac6454SAndrew Thompson 		/* same speed like parent */
77902ac6454SAndrew Thompson 		speed = udev->speed;
78002ac6454SAndrew Thompson 		break;
78102ac6454SAndrew Thompson 	}
782963169b4SHans Petter Selasky 	if (speed == USB_SPEED_SUPER) {
783963169b4SHans Petter Selasky 		err = usbd_req_set_hub_u1_timeout(udev, NULL,
784963169b4SHans Petter Selasky 		    portno, 128 - (2 * udev->depth));
785963169b4SHans Petter Selasky 		if (err) {
786963169b4SHans Petter Selasky 			DPRINTFN(0, "port %d U1 timeout "
787963169b4SHans Petter Selasky 			    "failed, error=%s\n",
788963169b4SHans Petter Selasky 			    portno, usbd_errstr(err));
789963169b4SHans Petter Selasky 		}
790963169b4SHans Petter Selasky 		err = usbd_req_set_hub_u2_timeout(udev, NULL,
791963169b4SHans Petter Selasky 		    portno, 128 - (2 * udev->depth));
792963169b4SHans Petter Selasky 		if (err) {
793963169b4SHans Petter Selasky 			DPRINTFN(0, "port %d U2 timeout "
794963169b4SHans Petter Selasky 			    "failed, error=%s\n",
795963169b4SHans Petter Selasky 			    portno, usbd_errstr(err));
796963169b4SHans Petter Selasky 		}
797963169b4SHans Petter Selasky 	}
798963169b4SHans Petter Selasky 
79902ac6454SAndrew Thompson 	/*
80002ac6454SAndrew Thompson 	 * Figure out the device mode
80102ac6454SAndrew Thompson 	 *
80202ac6454SAndrew Thompson 	 * NOTE: This part is currently FreeBSD specific.
80302ac6454SAndrew Thompson 	 */
8040324d54aSHans Petter Selasky 	if (udev->parent_hub != NULL) {
8050324d54aSHans Petter Selasky 		/* inherit mode from the parent HUB */
8060324d54aSHans Petter Selasky 		mode = udev->parent_hub->flags.usb_mode;
8070324d54aSHans Petter Selasky 	} else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
808f29a0724SAndrew Thompson 		mode = USB_MODE_DEVICE;
80902ac6454SAndrew Thompson 	else
810f29a0724SAndrew Thompson 		mode = USB_MODE_HOST;
81102ac6454SAndrew Thompson 
81202ac6454SAndrew Thompson 	/* need to create a new child */
813a593f6b8SAndrew Thompson 	child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
814f29a0724SAndrew Thompson 	    udev->depth + 1, portno - 1, portno, speed, mode);
81502ac6454SAndrew Thompson 	if (child == NULL) {
816767cb2e2SAndrew Thompson 		DPRINTFN(0, "could not allocate new device\n");
81702ac6454SAndrew Thompson 		goto error;
81802ac6454SAndrew Thompson 	}
81902ac6454SAndrew Thompson 	return (0);			/* success */
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson error:
822d88688c7SAndrew Thompson 	if (child != NULL) {
823d88688c7SAndrew Thompson 		/*
824d88688c7SAndrew Thompson 		 * Free USB device and all subdevices, if any.
825d88688c7SAndrew Thompson 		 */
826d88688c7SAndrew Thompson 		usb_free_device(child, 0);
82702ac6454SAndrew Thompson 		child = NULL;
82802ac6454SAndrew Thompson 	}
82902ac6454SAndrew Thompson 	if (err == 0) {
83002ac6454SAndrew Thompson 		if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
831a593f6b8SAndrew Thompson 			err = usbd_req_clear_port_feature(
832e2805033SAndrew Thompson 			    sc->sc_udev, NULL,
83302ac6454SAndrew Thompson 			    portno, UHF_PORT_ENABLE);
83402ac6454SAndrew Thompson 		}
83502ac6454SAndrew Thompson 	}
83602ac6454SAndrew Thompson 	if (err) {
83702ac6454SAndrew Thompson 		DPRINTFN(0, "device problem (%s), "
838a593f6b8SAndrew Thompson 		    "disabling port %d\n", usbd_errstr(err), portno);
83902ac6454SAndrew Thompson 	}
84002ac6454SAndrew Thompson 	return (err);
84102ac6454SAndrew Thompson }
84202ac6454SAndrew Thompson 
84302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
844963169b4SHans Petter Selasky  *	usb_device_20_compatible
845963169b4SHans Petter Selasky  *
846963169b4SHans Petter Selasky  * Returns:
847963169b4SHans Petter Selasky  *    0: HUB does not support suspend and resume
848963169b4SHans Petter Selasky  * Else: HUB supports suspend and resume
849963169b4SHans Petter Selasky  *------------------------------------------------------------------------*/
850963169b4SHans Petter Selasky static uint8_t
usb_device_20_compatible(struct usb_device * udev)851963169b4SHans Petter Selasky usb_device_20_compatible(struct usb_device *udev)
852963169b4SHans Petter Selasky {
853963169b4SHans Petter Selasky 	if (udev == NULL)
854963169b4SHans Petter Selasky 		return (0);
855963169b4SHans Petter Selasky 	switch (udev->speed) {
856963169b4SHans Petter Selasky 	case USB_SPEED_LOW:
857963169b4SHans Petter Selasky 	case USB_SPEED_FULL:
858963169b4SHans Petter Selasky 	case USB_SPEED_HIGH:
859963169b4SHans Petter Selasky 		return (1);
860963169b4SHans Petter Selasky 	default:
861963169b4SHans Petter Selasky 		return (0);
862963169b4SHans Petter Selasky 	}
863963169b4SHans Petter Selasky }
864963169b4SHans Petter Selasky 
865963169b4SHans Petter Selasky /*------------------------------------------------------------------------*
86602ac6454SAndrew Thompson  *	uhub_suspend_resume_port
86702ac6454SAndrew Thompson  *
86802ac6454SAndrew Thompson  * Returns:
86902ac6454SAndrew Thompson  *    0: Success
87002ac6454SAndrew Thompson  * Else: A control transaction failed
87102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
872e0a69b51SAndrew Thompson static usb_error_t
uhub_suspend_resume_port(struct uhub_softc * sc,uint8_t portno)87302ac6454SAndrew Thompson uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
87402ac6454SAndrew Thompson {
875760bc48eSAndrew Thompson 	struct usb_device *child;
876760bc48eSAndrew Thompson 	struct usb_device *udev;
87702ac6454SAndrew Thompson 	uint8_t is_suspend;
878e0a69b51SAndrew Thompson 	usb_error_t err;
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson 	DPRINTF("port %d\n", portno);
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson 	udev = sc->sc_udev;
883a593f6b8SAndrew Thompson 	child = usb_bus_port_get_device(udev->bus,
88402ac6454SAndrew Thompson 	    udev->hub->ports + portno - 1);
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	/* first clear the port suspend change bit */
88702ac6454SAndrew Thompson 
888963169b4SHans Petter Selasky 	if (usb_device_20_compatible(udev)) {
889a593f6b8SAndrew Thompson 		err = usbd_req_clear_port_feature(udev, NULL,
89002ac6454SAndrew Thompson 		    portno, UHF_C_PORT_SUSPEND);
891963169b4SHans Petter Selasky 	} else {
892963169b4SHans Petter Selasky 		err = usbd_req_clear_port_feature(udev, NULL,
893963169b4SHans Petter Selasky 		    portno, UHF_C_PORT_LINK_STATE);
894963169b4SHans Petter Selasky 	}
895963169b4SHans Petter Selasky 
89602ac6454SAndrew Thompson 	if (err) {
89702ac6454SAndrew Thompson 		DPRINTF("clearing suspend failed.\n");
89802ac6454SAndrew Thompson 		goto done;
89902ac6454SAndrew Thompson 	}
90002ac6454SAndrew Thompson 	/* get fresh status */
90102ac6454SAndrew Thompson 
90202ac6454SAndrew Thompson 	err = uhub_read_port_status(sc, portno);
90302ac6454SAndrew Thompson 	if (err) {
90402ac6454SAndrew Thompson 		DPRINTF("reading port status failed.\n");
90502ac6454SAndrew Thompson 		goto done;
90602ac6454SAndrew Thompson 	}
907963169b4SHans Petter Selasky 	/* convert current state */
90802ac6454SAndrew Thompson 
909963169b4SHans Petter Selasky 	if (usb_device_20_compatible(udev)) {
91002ac6454SAndrew Thompson 		if (sc->sc_st.port_status & UPS_SUSPEND) {
91102ac6454SAndrew Thompson 			is_suspend = 1;
91202ac6454SAndrew Thompson 		} else {
91302ac6454SAndrew Thompson 			is_suspend = 0;
91402ac6454SAndrew Thompson 		}
915963169b4SHans Petter Selasky 	} else {
916963169b4SHans Petter Selasky 		switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
91793ee6e85SHans Petter Selasky 		case UPS_PORT_LS_U3:
91893ee6e85SHans Petter Selasky 			is_suspend = 1;
91993ee6e85SHans Petter Selasky 			break;
92093ee6e85SHans Petter Selasky 		case UPS_PORT_LS_SS_INA:
92193ee6e85SHans Petter Selasky 			usbd_req_warm_reset_port(udev, NULL, portno);
922963169b4SHans Petter Selasky 			is_suspend = 0;
923963169b4SHans Petter Selasky 			break;
924963169b4SHans Petter Selasky 		default:
92593ee6e85SHans Petter Selasky 			is_suspend = 0;
926963169b4SHans Petter Selasky 			break;
927963169b4SHans Petter Selasky 		}
928963169b4SHans Petter Selasky 	}
92902ac6454SAndrew Thompson 
93002ac6454SAndrew Thompson 	DPRINTF("suspended=%u\n", is_suspend);
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 	/* do the suspend or resume */
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson 	if (child) {
93502ac6454SAndrew Thompson 		/*
93602ac6454SAndrew Thompson 		 * This code handle two cases: 1) Host Mode - we can only
93702ac6454SAndrew Thompson 		 * receive resume here 2) Device Mode - we can receive
93802ac6454SAndrew Thompson 		 * suspend and resume here
93902ac6454SAndrew Thompson 		 */
94002ac6454SAndrew Thompson 		if (is_suspend == 0)
941a593f6b8SAndrew Thompson 			usb_dev_resume_peer(child);
9424131f6fbSHans Petter Selasky 		else if (child->flags.usb_mode == USB_MODE_DEVICE)
943a593f6b8SAndrew Thompson 			usb_dev_suspend_peer(child);
94402ac6454SAndrew Thompson 	}
94502ac6454SAndrew Thompson done:
94602ac6454SAndrew Thompson 	return (err);
94702ac6454SAndrew Thompson }
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
95039307315SAndrew Thompson  *	uhub_root_interrupt
95139307315SAndrew Thompson  *
95239307315SAndrew Thompson  * This function is called when a Root HUB interrupt has
95339307315SAndrew Thompson  * happened. "ptr" and "len" makes up the Root HUB interrupt
95439307315SAndrew Thompson  * packet. This function is called having the "bus_mtx" locked.
95539307315SAndrew Thompson  *------------------------------------------------------------------------*/
95639307315SAndrew Thompson void
uhub_root_intr(struct usb_bus * bus,const uint8_t * ptr,uint8_t len)957760bc48eSAndrew Thompson uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
95839307315SAndrew Thompson {
95939307315SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
96039307315SAndrew Thompson 
961a593f6b8SAndrew Thompson 	usb_needs_explore(bus, 0);
96239307315SAndrew Thompson }
96339307315SAndrew Thompson 
964963169b4SHans Petter Selasky static uint8_t
uhub_is_too_deep(struct usb_device * udev)965963169b4SHans Petter Selasky uhub_is_too_deep(struct usb_device *udev)
966963169b4SHans Petter Selasky {
967963169b4SHans Petter Selasky 	switch (udev->speed) {
968963169b4SHans Petter Selasky 	case USB_SPEED_FULL:
969963169b4SHans Petter Selasky 	case USB_SPEED_LOW:
970963169b4SHans Petter Selasky 	case USB_SPEED_HIGH:
971963169b4SHans Petter Selasky 		if (udev->depth > USB_HUB_MAX_DEPTH)
972963169b4SHans Petter Selasky 			return (1);
973963169b4SHans Petter Selasky 		break;
974963169b4SHans Petter Selasky 	case USB_SPEED_SUPER:
975963169b4SHans Petter Selasky 		if (udev->depth > USB_SS_HUB_DEPTH_MAX)
976963169b4SHans Petter Selasky 			return (1);
977963169b4SHans Petter Selasky 		break;
978963169b4SHans Petter Selasky 	default:
979963169b4SHans Petter Selasky 		break;
980963169b4SHans Petter Selasky 	}
981963169b4SHans Petter Selasky 	return (0);
982963169b4SHans Petter Selasky }
983963169b4SHans Petter Selasky 
98439307315SAndrew Thompson /*------------------------------------------------------------------------*
98502ac6454SAndrew Thompson  *	uhub_explore
98602ac6454SAndrew Thompson  *
98702ac6454SAndrew Thompson  * Returns:
98802ac6454SAndrew Thompson  *     0: Success
98902ac6454SAndrew Thompson  *  Else: Failure
99002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
991e0a69b51SAndrew Thompson static usb_error_t
uhub_explore(struct usb_device * udev)992760bc48eSAndrew Thompson uhub_explore(struct usb_device *udev)
99302ac6454SAndrew Thompson {
994760bc48eSAndrew Thompson 	struct usb_hub *hub;
99502ac6454SAndrew Thompson 	struct uhub_softc *sc;
996760bc48eSAndrew Thompson 	struct usb_port *up;
997c7cd6f80SHans Petter Selasky 	usb_error_t retval;
998e0a69b51SAndrew Thompson 	usb_error_t err;
99902ac6454SAndrew Thompson 	uint8_t portno;
100002ac6454SAndrew Thompson 	uint8_t x;
1001a18a7a41SHans Petter Selasky 	uint8_t do_unlock;
100202ac6454SAndrew Thompson 
100302ac6454SAndrew Thompson 	hub = udev->hub;
100402ac6454SAndrew Thompson 	sc = hub->hubsoftc;
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson 	DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
100702ac6454SAndrew Thompson 
1008963169b4SHans Petter Selasky 	/* ignore devices that are too deep */
1009963169b4SHans Petter Selasky 	if (uhub_is_too_deep(udev))
101002ac6454SAndrew Thompson 		return (USB_ERR_TOO_DEEP);
1011ec8f3127SAndrew Thompson 
1012963169b4SHans Petter Selasky 	/* check if device is suspended */
1013ec8f3127SAndrew Thompson 	if (udev->flags.self_suspended) {
101402ac6454SAndrew Thompson 		/* need to wait until the child signals resume */
101502ac6454SAndrew Thompson 		DPRINTF("Device is suspended!\n");
1016e5ff940aSHans Petter Selasky 		return (USB_ERR_NORMAL_COMPLETION);
101702ac6454SAndrew Thompson 	}
101885b44a01SHans Petter Selasky 
101985b44a01SHans Petter Selasky 	/*
102085b44a01SHans Petter Selasky 	 * Make sure we don't race against user-space applications
102185b44a01SHans Petter Selasky 	 * like LibUSB:
102285b44a01SHans Petter Selasky 	 */
1023a18a7a41SHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
102485b44a01SHans Petter Selasky 
1025e5ff940aSHans Petter Selasky 	/*
1026e5ff940aSHans Petter Selasky 	 * Set default error code to avoid compiler warnings.
1027e5ff940aSHans Petter Selasky 	 * Note that hub->nports cannot be zero.
1028e5ff940aSHans Petter Selasky 	 */
1029c7cd6f80SHans Petter Selasky 	retval = USB_ERR_NORMAL_COMPLETION;
1030e5ff940aSHans Petter Selasky 
103102ac6454SAndrew Thompson 	for (x = 0; x != hub->nports; x++) {
103202ac6454SAndrew Thompson 		up = hub->ports + x;
103302ac6454SAndrew Thompson 		portno = x + 1;
103402ac6454SAndrew Thompson 
103502ac6454SAndrew Thompson 		err = uhub_read_port_status(sc, portno);
1036c7cd6f80SHans Petter Selasky 		if (err != USB_ERR_NORMAL_COMPLETION)
1037c7cd6f80SHans Petter Selasky 			retval = err;
1038c7cd6f80SHans Petter Selasky 
103902ac6454SAndrew Thompson 		if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
104002ac6454SAndrew Thompson 			DPRINTF("Overcurrent on port %u.\n", portno);
1041a593f6b8SAndrew Thompson 			err = usbd_req_clear_port_feature(
1042e2805033SAndrew Thompson 			    udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1043c7cd6f80SHans Petter Selasky 			if (err != USB_ERR_NORMAL_COMPLETION)
1044c7cd6f80SHans Petter Selasky 				retval = err;
104502ac6454SAndrew Thompson 		}
104602ac6454SAndrew Thompson 		if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
104702ac6454SAndrew Thompson 			/*
104802ac6454SAndrew Thompson 			 * Fake a connect status change so that the
104902ac6454SAndrew Thompson 			 * status gets checked initially!
105002ac6454SAndrew Thompson 			 */
105102ac6454SAndrew Thompson 			sc->sc_st.port_change |=
105202ac6454SAndrew Thompson 			    UPS_C_CONNECT_STATUS;
105302ac6454SAndrew Thompson 		}
105402ac6454SAndrew Thompson 		if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1055a593f6b8SAndrew Thompson 			err = usbd_req_clear_port_feature(
1056e2805033SAndrew Thompson 			    udev, NULL, portno, UHF_C_PORT_ENABLE);
1057c7cd6f80SHans Petter Selasky 			if (err != USB_ERR_NORMAL_COMPLETION)
1058c7cd6f80SHans Petter Selasky 				retval = err;
105902ac6454SAndrew Thompson 			if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
106002ac6454SAndrew Thompson 				/*
106102ac6454SAndrew Thompson 				 * Ignore the port error if the device
106202ac6454SAndrew Thompson 				 * has vanished !
106302ac6454SAndrew Thompson 				 */
106402ac6454SAndrew Thompson 			} else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
106502ac6454SAndrew Thompson 				DPRINTFN(0, "illegal enable change, "
106602ac6454SAndrew Thompson 				    "port %d\n", portno);
106702ac6454SAndrew Thompson 			} else {
106802ac6454SAndrew Thompson 				if (up->restartcnt == USB_RESTART_MAX) {
106902ac6454SAndrew Thompson 					/* XXX could try another speed ? */
107002ac6454SAndrew Thompson 					DPRINTFN(0, "port error, giving up "
107102ac6454SAndrew Thompson 					    "port %d\n", portno);
107202ac6454SAndrew Thompson 				} else {
107302ac6454SAndrew Thompson 					sc->sc_st.port_change |=
107402ac6454SAndrew Thompson 					    UPS_C_CONNECT_STATUS;
107502ac6454SAndrew Thompson 					up->restartcnt++;
107602ac6454SAndrew Thompson 				}
107702ac6454SAndrew Thompson 			}
107802ac6454SAndrew Thompson 		}
107902ac6454SAndrew Thompson 		if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
108002ac6454SAndrew Thompson 			err = uhub_reattach_port(sc, portno);
1081c7cd6f80SHans Petter Selasky 			if (err != USB_ERR_NORMAL_COMPLETION)
1082c7cd6f80SHans Petter Selasky 				retval = err;
108302ac6454SAndrew Thompson 		}
108493ee6e85SHans Petter Selasky 		if (sc->sc_st.port_change & (UPS_C_SUSPEND |
108593ee6e85SHans Petter Selasky 		    UPS_C_PORT_LINK_STATE)) {
108602ac6454SAndrew Thompson 			err = uhub_suspend_resume_port(sc, portno);
1087c7cd6f80SHans Petter Selasky 			if (err != USB_ERR_NORMAL_COMPLETION)
1088c7cd6f80SHans Petter Selasky 				retval = err;
108902ac6454SAndrew Thompson 		}
10908f892e9bSBjoern A. Zeeb 		if (udev->speed == USB_SPEED_SUPER &&
10918f892e9bSBjoern A. Zeeb 		    (sc->sc_st.port_change & UPS_C_BH_PORT_RESET) != 0) {
10928f892e9bSBjoern A. Zeeb 			DPRINTF("Warm reset finished on port %u.\n", portno);
10938f892e9bSBjoern A. Zeeb 			err = usbd_req_clear_port_feature(
10948f892e9bSBjoern A. Zeeb 			    udev, NULL, portno, UHF_C_BH_PORT_RESET);
10958f892e9bSBjoern A. Zeeb 			if (err != USB_ERR_NORMAL_COMPLETION)
10968f892e9bSBjoern A. Zeeb 				retval = err;
10978f892e9bSBjoern A. Zeeb 		}
10988f892e9bSBjoern A. Zeeb 		if (sc->sc_st.port_change & UPS_C_PORT_RESET) {
10998f892e9bSBjoern A. Zeeb 			DPRINTF("Port reset finished on port %u.\n", portno);
11008f892e9bSBjoern A. Zeeb 			err = usbd_req_clear_port_feature(
11018f892e9bSBjoern A. Zeeb 			    udev, NULL, portno, UHF_C_PORT_RESET);
11028f892e9bSBjoern A. Zeeb 			if (err != USB_ERR_NORMAL_COMPLETION)
11038f892e9bSBjoern A. Zeeb 				retval = err;
11048f892e9bSBjoern A. Zeeb 		}
1105e5ff940aSHans Petter Selasky 
1106e5ff940aSHans Petter Selasky 		if (uhub_explore_sub(sc, up) == USB_ERR_NORMAL_COMPLETION) {
110702ac6454SAndrew Thompson 			/* explore succeeded - reset restart counter */
110802ac6454SAndrew Thompson 			up->restartcnt = 0;
110902ac6454SAndrew Thompson 		}
1110e5ff940aSHans Petter Selasky 	}
111102ac6454SAndrew Thompson 
1112a18a7a41SHans Petter Selasky 	if (do_unlock)
111385b44a01SHans Petter Selasky 		usbd_enum_unlock(udev);
111485b44a01SHans Petter Selasky 
111502ac6454SAndrew Thompson 	/* initial status checked */
111602ac6454SAndrew Thompson 	sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
111702ac6454SAndrew Thompson 
1118c7cd6f80SHans Petter Selasky 	return (retval);
111902ac6454SAndrew Thompson }
112002ac6454SAndrew Thompson 
1121e68fcc88STakanori Watanabe int
uhub_probe(device_t dev)112202ac6454SAndrew Thompson uhub_probe(device_t dev)
112302ac6454SAndrew Thompson {
1124760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
112502ac6454SAndrew Thompson 
1126963169b4SHans Petter Selasky 	if (uaa->usb_mode != USB_MODE_HOST)
112702ac6454SAndrew Thompson 		return (ENXIO);
1128963169b4SHans Petter Selasky 
112902ac6454SAndrew Thompson 	/*
1130963169b4SHans Petter Selasky 	 * The subclass for USB HUBs is currently ignored because it
1131963169b4SHans Petter Selasky 	 * is 0 for some and 1 for others.
113202ac6454SAndrew Thompson 	 */
1133963169b4SHans Petter Selasky 	if (uaa->info.bConfigIndex == 0 &&
1134963169b4SHans Petter Selasky 	    uaa->info.bDeviceClass == UDCLASS_HUB)
1135e68fcc88STakanori Watanabe 		return (BUS_PROBE_DEFAULT);
1136963169b4SHans Petter Selasky 
113702ac6454SAndrew Thompson 	return (ENXIO);
113802ac6454SAndrew Thompson }
113902ac6454SAndrew Thompson 
1140963169b4SHans Petter Selasky /* NOTE: The information returned by this function can be wrong. */
1141963169b4SHans Petter Selasky usb_error_t
uhub_query_info(struct usb_device * udev,uint8_t * pnports,uint8_t * ptt)1142963169b4SHans Petter Selasky uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1143963169b4SHans Petter Selasky {
1144963169b4SHans Petter Selasky 	struct usb_hub_descriptor hubdesc20;
1145963169b4SHans Petter Selasky 	struct usb_hub_ss_descriptor hubdesc30;
1146963169b4SHans Petter Selasky 	usb_error_t err;
1147963169b4SHans Petter Selasky 	uint8_t nports;
1148963169b4SHans Petter Selasky 	uint8_t tt;
1149963169b4SHans Petter Selasky 
1150963169b4SHans Petter Selasky 	if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1151963169b4SHans Petter Selasky 		return (USB_ERR_INVAL);
1152963169b4SHans Petter Selasky 
1153963169b4SHans Petter Selasky 	nports = 0;
1154963169b4SHans Petter Selasky 	tt = 0;
1155963169b4SHans Petter Selasky 
1156963169b4SHans Petter Selasky 	switch (udev->speed) {
1157963169b4SHans Petter Selasky 	case USB_SPEED_LOW:
1158963169b4SHans Petter Selasky 	case USB_SPEED_FULL:
1159963169b4SHans Petter Selasky 	case USB_SPEED_HIGH:
1160963169b4SHans Petter Selasky 		/* assuming that there is one port */
1161963169b4SHans Petter Selasky 		err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1162963169b4SHans Petter Selasky 		if (err) {
1163963169b4SHans Petter Selasky 			DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1164963169b4SHans Petter Selasky 			    "error=%s\n", usbd_errstr(err));
1165963169b4SHans Petter Selasky 			break;
1166963169b4SHans Petter Selasky 		}
1167963169b4SHans Petter Selasky 		nports = hubdesc20.bNbrPorts;
1168963169b4SHans Petter Selasky 		if (nports > 127)
1169963169b4SHans Petter Selasky 			nports = 127;
1170963169b4SHans Petter Selasky 
1171963169b4SHans Petter Selasky 		if (udev->speed == USB_SPEED_HIGH)
1172963169b4SHans Petter Selasky 			tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1173963169b4SHans Petter Selasky 		break;
1174963169b4SHans Petter Selasky 
1175963169b4SHans Petter Selasky 	case USB_SPEED_SUPER:
1176963169b4SHans Petter Selasky 		err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1177963169b4SHans Petter Selasky 		if (err) {
1178963169b4SHans Petter Selasky 			DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1179963169b4SHans Petter Selasky 			    "error=%s\n", usbd_errstr(err));
1180963169b4SHans Petter Selasky 			break;
1181963169b4SHans Petter Selasky 		}
1182963169b4SHans Petter Selasky 		nports = hubdesc30.bNbrPorts;
1183963169b4SHans Petter Selasky 		if (nports > 16)
1184963169b4SHans Petter Selasky 			nports = 16;
1185963169b4SHans Petter Selasky 		break;
1186963169b4SHans Petter Selasky 
1187963169b4SHans Petter Selasky 	default:
1188963169b4SHans Petter Selasky 		err = USB_ERR_INVAL;
1189963169b4SHans Petter Selasky 		break;
1190963169b4SHans Petter Selasky 	}
1191963169b4SHans Petter Selasky 
1192963169b4SHans Petter Selasky 	if (pnports != NULL)
1193963169b4SHans Petter Selasky 		*pnports = nports;
1194963169b4SHans Petter Selasky 
1195963169b4SHans Petter Selasky 	if (ptt != NULL)
1196963169b4SHans Petter Selasky 		*ptt = tt;
1197963169b4SHans Petter Selasky 
1198963169b4SHans Petter Selasky 	return (err);
1199963169b4SHans Petter Selasky }
1200963169b4SHans Petter Selasky 
1201e68fcc88STakanori Watanabe int
uhub_attach(device_t dev)120202ac6454SAndrew Thompson uhub_attach(device_t dev)
120302ac6454SAndrew Thompson {
120402ac6454SAndrew Thompson 	struct uhub_softc *sc = device_get_softc(dev);
1205760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
1206760bc48eSAndrew Thompson 	struct usb_device *udev = uaa->device;
1207760bc48eSAndrew Thompson 	struct usb_device *parent_hub = udev->parent_hub;
1208760bc48eSAndrew Thompson 	struct usb_hub *hub;
1209963169b4SHans Petter Selasky 	struct usb_hub_descriptor hubdesc20;
1210963169b4SHans Petter Selasky 	struct usb_hub_ss_descriptor hubdesc30;
1211c38aa253SHans Petter Selasky #if USB_HAVE_DISABLE_ENUM
1212c38aa253SHans Petter Selasky 	struct sysctl_ctx_list *sysctl_ctx;
1213c38aa253SHans Petter Selasky 	struct sysctl_oid *sysctl_tree;
1214c38aa253SHans Petter Selasky #endif
121502ac6454SAndrew Thompson 	uint16_t pwrdly;
12162c79a775SHans Petter Selasky 	uint16_t nports;
121702ac6454SAndrew Thompson 	uint8_t x;
121802ac6454SAndrew Thompson 	uint8_t portno;
121902ac6454SAndrew Thompson 	uint8_t removable;
122002ac6454SAndrew Thompson 	uint8_t iface_index;
1221e0a69b51SAndrew Thompson 	usb_error_t err;
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson 	sc->sc_udev = udev;
122402ac6454SAndrew Thompson 	sc->sc_dev = dev;
122502ac6454SAndrew Thompson 
1226cb18f7d1SAlfred Perlstein 	mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1227cb18f7d1SAlfred Perlstein 
1228a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
122902ac6454SAndrew Thompson 
123002ac6454SAndrew Thompson 	DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
123102ac6454SAndrew Thompson 	    "parent->selfpowered=%d\n",
123202ac6454SAndrew Thompson 	    udev->depth,
123302ac6454SAndrew Thompson 	    udev->flags.self_powered,
123402ac6454SAndrew Thompson 	    parent_hub,
123502ac6454SAndrew Thompson 	    parent_hub ?
123602ac6454SAndrew Thompson 	    parent_hub->flags.self_powered : 0);
123702ac6454SAndrew Thompson 
1238963169b4SHans Petter Selasky 	if (uhub_is_too_deep(udev)) {
1239963169b4SHans Petter Selasky 		DPRINTFN(0, "HUB at depth %d, "
1240963169b4SHans Petter Selasky 		    "exceeds maximum. HUB ignored\n", (int)udev->depth);
124102ac6454SAndrew Thompson 		goto error;
124202ac6454SAndrew Thompson 	}
1243963169b4SHans Petter Selasky 
124402ac6454SAndrew Thompson 	if (!udev->flags.self_powered && parent_hub &&
1245963169b4SHans Petter Selasky 	    !parent_hub->flags.self_powered) {
1246963169b4SHans Petter Selasky 		DPRINTFN(0, "Bus powered HUB connected to "
1247767cb2e2SAndrew Thompson 		    "bus powered HUB. HUB ignored\n");
124802ac6454SAndrew Thompson 		goto error;
124902ac6454SAndrew Thompson 	}
12500a4cc48fSHans Petter Selasky 
12510a4cc48fSHans Petter Selasky 	if (UHUB_IS_MULTI_TT(sc)) {
12520a4cc48fSHans Petter Selasky 		err = usbd_set_alt_interface_index(udev, 0, 1);
12530a4cc48fSHans Petter Selasky 		if (err) {
12540a4cc48fSHans Petter Selasky 			device_printf(dev, "MTT could not be enabled\n");
12550a4cc48fSHans Petter Selasky 			goto error;
12560a4cc48fSHans Petter Selasky 		}
12570a4cc48fSHans Petter Selasky 		device_printf(dev, "MTT enabled\n");
12580a4cc48fSHans Petter Selasky 	}
12590a4cc48fSHans Petter Selasky 
126002ac6454SAndrew Thompson 	/* get HUB descriptor */
126102ac6454SAndrew Thompson 
1262963169b4SHans Petter Selasky 	DPRINTFN(2, "Getting HUB descriptor\n");
126302ac6454SAndrew Thompson 
1264963169b4SHans Petter Selasky 	switch (udev->speed) {
1265963169b4SHans Petter Selasky 	case USB_SPEED_LOW:
1266963169b4SHans Petter Selasky 	case USB_SPEED_FULL:
1267963169b4SHans Petter Selasky 	case USB_SPEED_HIGH:
126802ac6454SAndrew Thompson 		/* assuming that there is one port */
1269963169b4SHans Petter Selasky 		err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
127002ac6454SAndrew Thompson 		if (err) {
1271963169b4SHans Petter Selasky 			DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1272a593f6b8SAndrew Thompson 			    "error=%s\n", usbd_errstr(err));
127302ac6454SAndrew Thompson 			goto error;
127402ac6454SAndrew Thompson 		}
1275963169b4SHans Petter Selasky 		/* get number of ports */
1276963169b4SHans Petter Selasky 		nports = hubdesc20.bNbrPorts;
1277963169b4SHans Petter Selasky 
1278963169b4SHans Petter Selasky 		/* get power delay */
1279963169b4SHans Petter Selasky 		pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
128037506412SHans Petter Selasky 		    usb_extra_power_up_time);
1281963169b4SHans Petter Selasky 
1282963169b4SHans Petter Selasky 		/* get complete HUB descriptor */
1283963169b4SHans Petter Selasky 		if (nports >= 8) {
1284963169b4SHans Petter Selasky 			/* check number of ports */
1285963169b4SHans Petter Selasky 			if (nports > 127) {
1286963169b4SHans Petter Selasky 				DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1287963169b4SHans Petter Selasky 				    "error=%s\n", usbd_errstr(err));
128802ac6454SAndrew Thompson 				goto error;
128902ac6454SAndrew Thompson 			}
1290963169b4SHans Petter Selasky 			/* get complete HUB descriptor */
1291963169b4SHans Petter Selasky 			err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1292963169b4SHans Petter Selasky 
1293963169b4SHans Petter Selasky 			if (err) {
1294963169b4SHans Petter Selasky 				DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1295963169b4SHans Petter Selasky 				    "error=%s\n", usbd_errstr(err));
1296963169b4SHans Petter Selasky 				goto error;
1297963169b4SHans Petter Selasky 			}
1298963169b4SHans Petter Selasky 			if (hubdesc20.bNbrPorts != nports) {
1299963169b4SHans Petter Selasky 				DPRINTFN(0, "Number of ports changed\n");
1300963169b4SHans Petter Selasky 				goto error;
1301963169b4SHans Petter Selasky 			}
1302963169b4SHans Petter Selasky 		}
1303963169b4SHans Petter Selasky 		break;
1304963169b4SHans Petter Selasky 	case USB_SPEED_SUPER:
1305963169b4SHans Petter Selasky 		if (udev->parent_hub != NULL) {
1306963169b4SHans Petter Selasky 			err = usbd_req_set_hub_depth(udev, NULL,
1307963169b4SHans Petter Selasky 			    udev->depth - 1);
1308963169b4SHans Petter Selasky 			if (err) {
1309963169b4SHans Petter Selasky 				DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1310963169b4SHans Petter Selasky 				    "error=%s\n", usbd_errstr(err));
1311963169b4SHans Petter Selasky 				goto error;
1312963169b4SHans Petter Selasky 			}
1313963169b4SHans Petter Selasky 		}
1314963169b4SHans Petter Selasky 		err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1315963169b4SHans Petter Selasky 		if (err) {
1316963169b4SHans Petter Selasky 			DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1317963169b4SHans Petter Selasky 			    "error=%s\n", usbd_errstr(err));
1318963169b4SHans Petter Selasky 			goto error;
1319963169b4SHans Petter Selasky 		}
1320963169b4SHans Petter Selasky 		/* get number of ports */
1321963169b4SHans Petter Selasky 		nports = hubdesc30.bNbrPorts;
1322963169b4SHans Petter Selasky 
1323963169b4SHans Petter Selasky 		/* get power delay */
1324963169b4SHans Petter Selasky 		pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
132537506412SHans Petter Selasky 		    usb_extra_power_up_time);
1326963169b4SHans Petter Selasky 
1327963169b4SHans Petter Selasky 		/* get complete HUB descriptor */
1328963169b4SHans Petter Selasky 		if (nports >= 8) {
1329963169b4SHans Petter Selasky 			/* check number of ports */
1330963169b4SHans Petter Selasky 			if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1331963169b4SHans Petter Selasky 				DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1332963169b4SHans Petter Selasky 				    "error=%s\n", usbd_errstr(err));
1333963169b4SHans Petter Selasky 				goto error;
1334963169b4SHans Petter Selasky 			}
1335963169b4SHans Petter Selasky 			/* get complete HUB descriptor */
1336963169b4SHans Petter Selasky 			err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1337963169b4SHans Petter Selasky 
1338963169b4SHans Petter Selasky 			if (err) {
1339963169b4SHans Petter Selasky 				DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1340963169b4SHans Petter Selasky 				    "error=%s\n", usbd_errstr(err));
1341963169b4SHans Petter Selasky 				goto error;
1342963169b4SHans Petter Selasky 			}
1343963169b4SHans Petter Selasky 			if (hubdesc30.bNbrPorts != nports) {
1344963169b4SHans Petter Selasky 				DPRINTFN(0, "Number of ports changed\n");
1345963169b4SHans Petter Selasky 				goto error;
1346963169b4SHans Petter Selasky 			}
1347963169b4SHans Petter Selasky 		}
1348963169b4SHans Petter Selasky 		break;
1349963169b4SHans Petter Selasky 	default:
1350963169b4SHans Petter Selasky 		DPRINTF("Assuming HUB has only one port\n");
1351963169b4SHans Petter Selasky 		/* default number of ports */
1352963169b4SHans Petter Selasky 		nports = 1;
1353963169b4SHans Petter Selasky 		/* default power delay */
135437506412SHans Petter Selasky 		pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1355963169b4SHans Petter Selasky 		break;
1356963169b4SHans Petter Selasky 	}
135702ac6454SAndrew Thompson 	if (nports == 0) {
1358767cb2e2SAndrew Thompson 		DPRINTFN(0, "portless HUB\n");
135902ac6454SAndrew Thompson 		goto error;
136002ac6454SAndrew Thompson 	}
13612c79a775SHans Petter Selasky 	if (nports > USB_MAX_PORTS) {
13622c79a775SHans Petter Selasky 		DPRINTF("Port limit exceeded\n");
13632c79a775SHans Petter Selasky 		goto error;
13642c79a775SHans Petter Selasky 	}
13652c79a775SHans Petter Selasky #if (USB_HAVE_FIXED_PORT == 0)
136602ac6454SAndrew Thompson 	hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
136702ac6454SAndrew Thompson 	    M_USBDEV, M_WAITOK | M_ZERO);
136802ac6454SAndrew Thompson 
13692c79a775SHans Petter Selasky 	if (hub == NULL)
137002ac6454SAndrew Thompson 		goto error;
13712c79a775SHans Petter Selasky #else
13722c79a775SHans Petter Selasky 	hub = &sc->sc_hub;
13732c79a775SHans Petter Selasky #endif
137402ac6454SAndrew Thompson 	udev->hub = hub;
137502ac6454SAndrew Thompson 
137602ac6454SAndrew Thompson 	/* initialize HUB structure */
137702ac6454SAndrew Thompson 	hub->hubsoftc = sc;
137802ac6454SAndrew Thompson 	hub->explore = &uhub_explore;
1379963169b4SHans Petter Selasky 	hub->nports = nports;
138002ac6454SAndrew Thompson 	hub->hubudev = udev;
1381a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
1382a0d53e0bSHans Petter Selasky 	hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1383a0d53e0bSHans Petter Selasky 	hub->tt_msg[0].udev = udev;
1384a0d53e0bSHans Petter Selasky 	hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1385a0d53e0bSHans Petter Selasky 	hub->tt_msg[1].udev = udev;
1386a0d53e0bSHans Petter Selasky #endif
138702ac6454SAndrew Thompson 	/* if self powered hub, give ports maximum current */
138802ac6454SAndrew Thompson 	if (udev->flags.self_powered) {
138902ac6454SAndrew Thompson 		hub->portpower = USB_MAX_POWER;
139002ac6454SAndrew Thompson 	} else {
139102ac6454SAndrew Thompson 		hub->portpower = USB_MIN_POWER;
139202ac6454SAndrew Thompson 	}
139302ac6454SAndrew Thompson 
139402ac6454SAndrew Thompson 	/* set up interrupt pipe */
139502ac6454SAndrew Thompson 	iface_index = 0;
139639307315SAndrew Thompson 	if (udev->parent_hub == NULL) {
139739307315SAndrew Thompson 		/* root HUB is special */
139839307315SAndrew Thompson 		err = 0;
139939307315SAndrew Thompson 	} else {
140039307315SAndrew Thompson 		/* normal HUB */
1401a593f6b8SAndrew Thompson 		err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1402cb18f7d1SAlfred Perlstein 		    uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
140339307315SAndrew Thompson 	}
140402ac6454SAndrew Thompson 	if (err) {
140502ac6454SAndrew Thompson 		DPRINTFN(0, "cannot setup interrupt transfer, "
1406767cb2e2SAndrew Thompson 		    "errstr=%s\n", usbd_errstr(err));
140702ac6454SAndrew Thompson 		goto error;
140802ac6454SAndrew Thompson 	}
140902ac6454SAndrew Thompson 	/* wait with power off for a while */
1410a593f6b8SAndrew Thompson 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
141102ac6454SAndrew Thompson 
141277d68af5SHans Petter Selasky #if USB_HAVE_DISABLE_ENUM
141377d68af5SHans Petter Selasky 	/* Add device sysctls */
141477d68af5SHans Petter Selasky 
141577d68af5SHans Petter Selasky 	sysctl_ctx = device_get_sysctl_ctx(dev);
141677d68af5SHans Petter Selasky 	sysctl_tree = device_get_sysctl_tree(dev);
141777d68af5SHans Petter Selasky 
141877d68af5SHans Petter Selasky 	if (sysctl_ctx != NULL && sysctl_tree != NULL) {
141977d68af5SHans Petter Selasky 		(void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
142077d68af5SHans Petter Selasky 		    OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
142177d68af5SHans Petter Selasky 		    &sc->sc_disable_enumeration, 0,
142277d68af5SHans Petter Selasky 		    "Set to disable enumeration on this USB HUB.");
142377d68af5SHans Petter Selasky 
142477d68af5SHans Petter Selasky 		(void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
142577d68af5SHans Petter Selasky 		    OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
142677d68af5SHans Petter Selasky 		    &sc->sc_disable_port_power, 0,
142777d68af5SHans Petter Selasky 		    "Set to disable USB port power on this USB HUB.");
142877d68af5SHans Petter Selasky 	}
142977d68af5SHans Petter Selasky #endif
143002ac6454SAndrew Thompson 	/*
143102ac6454SAndrew Thompson 	 * To have the best chance of success we do things in the exact same
143202ac6454SAndrew Thompson 	 * order as Windoze98.  This should not be necessary, but some
143302ac6454SAndrew Thompson 	 * devices do not follow the USB specs to the letter.
143402ac6454SAndrew Thompson 	 *
143502ac6454SAndrew Thompson 	 * These are the events on the bus when a hub is attached:
143602ac6454SAndrew Thompson 	 *  Get device and config descriptors (see attach code)
143702ac6454SAndrew Thompson 	 *  Get hub descriptor (see above)
143802ac6454SAndrew Thompson 	 *  For all ports
143902ac6454SAndrew Thompson 	 *     turn on power
144002ac6454SAndrew Thompson 	 *     wait for power to become stable
144102ac6454SAndrew Thompson 	 * (all below happens in explore code)
144202ac6454SAndrew Thompson 	 *  For all ports
144302ac6454SAndrew Thompson 	 *     clear C_PORT_CONNECTION
144402ac6454SAndrew Thompson 	 *  For all ports
144502ac6454SAndrew Thompson 	 *     get port status
144602ac6454SAndrew Thompson 	 *     if device connected
144702ac6454SAndrew Thompson 	 *        wait 100 ms
144802ac6454SAndrew Thompson 	 *        turn on reset
144902ac6454SAndrew Thompson 	 *        wait
145002ac6454SAndrew Thompson 	 *        clear C_PORT_RESET
145102ac6454SAndrew Thompson 	 *        get port status
145202ac6454SAndrew Thompson 	 *        proceed with device attachment
145302ac6454SAndrew Thompson 	 */
145402ac6454SAndrew Thompson 
145502ac6454SAndrew Thompson 	/* XXX should check for none, individual, or ganged power? */
145602ac6454SAndrew Thompson 
145702ac6454SAndrew Thompson 	removable = 0;
145802ac6454SAndrew Thompson 
145902ac6454SAndrew Thompson 	for (x = 0; x != nports; x++) {
146002ac6454SAndrew Thompson 		/* set up data structures */
1461760bc48eSAndrew Thompson 		struct usb_port *up = hub->ports + x;
146202ac6454SAndrew Thompson 
146302ac6454SAndrew Thompson 		up->device_index = 0;
146402ac6454SAndrew Thompson 		up->restartcnt = 0;
146502ac6454SAndrew Thompson 		portno = x + 1;
146602ac6454SAndrew Thompson 
146702ac6454SAndrew Thompson 		/* check if port is removable */
1468963169b4SHans Petter Selasky 		switch (udev->speed) {
1469963169b4SHans Petter Selasky 		case USB_SPEED_LOW:
1470963169b4SHans Petter Selasky 		case USB_SPEED_FULL:
1471963169b4SHans Petter Selasky 		case USB_SPEED_HIGH:
1472963169b4SHans Petter Selasky 			if (!UHD_NOT_REMOV(&hubdesc20, portno))
147302ac6454SAndrew Thompson 				removable++;
1474963169b4SHans Petter Selasky 			break;
1475963169b4SHans Petter Selasky 		case USB_SPEED_SUPER:
1476963169b4SHans Petter Selasky 			if (!UHD_NOT_REMOV(&hubdesc30, portno))
1477963169b4SHans Petter Selasky 				removable++;
1478963169b4SHans Petter Selasky 			break;
1479963169b4SHans Petter Selasky 		default:
1480963169b4SHans Petter Selasky 			DPRINTF("Assuming removable port\n");
1481963169b4SHans Petter Selasky 			removable++;
1482963169b4SHans Petter Selasky 			break;
148302ac6454SAndrew Thompson 		}
148477d68af5SHans Petter Selasky 		if (err == 0) {
148577d68af5SHans Petter Selasky #if USB_HAVE_DISABLE_ENUM
148677d68af5SHans Petter Selasky 			/* check if we should disable USB port power or not */
148777d68af5SHans Petter Selasky 			if (usb_disable_port_power != 0 ||
148877d68af5SHans Petter Selasky 			    sc->sc_disable_port_power != 0) {
148977d68af5SHans Petter Selasky 				/* turn the power off */
149087273b37SHans Petter Selasky 				DPRINTFN(2, "Turning port %d power off\n", portno);
149177d68af5SHans Petter Selasky 				err = usbd_req_clear_port_feature(udev, NULL,
149277d68af5SHans Petter Selasky 				    portno, UHF_PORT_POWER);
149377d68af5SHans Petter Selasky 			} else {
149477d68af5SHans Petter Selasky #endif
149502ac6454SAndrew Thompson 				/* turn the power on */
149687273b37SHans Petter Selasky 				DPRINTFN(2, "Turning port %d power on\n", portno);
1497a593f6b8SAndrew Thompson 				err = usbd_req_set_port_feature(udev, NULL,
149802ac6454SAndrew Thompson 				    portno, UHF_PORT_POWER);
149977d68af5SHans Petter Selasky #if USB_HAVE_DISABLE_ENUM
150002ac6454SAndrew Thompson 			}
150177d68af5SHans Petter Selasky #endif
150277d68af5SHans Petter Selasky 		}
150377d68af5SHans Petter Selasky 		if (err != 0) {
150477d68af5SHans Petter Selasky 			DPRINTFN(0, "port %d power on or off failed, %s\n",
1505a593f6b8SAndrew Thompson 			    portno, usbd_errstr(err));
150602ac6454SAndrew Thompson 		}
150702ac6454SAndrew Thompson 		DPRINTF("turn on port %d power\n",
150802ac6454SAndrew Thompson 		    portno);
150902ac6454SAndrew Thompson 
151002ac6454SAndrew Thompson 		/* wait for stable power */
1511a593f6b8SAndrew Thompson 		usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
151202ac6454SAndrew Thompson 	}
151302ac6454SAndrew Thompson 
151402ac6454SAndrew Thompson 	device_printf(dev, "%d port%s with %d "
151502ac6454SAndrew Thompson 	    "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
151602ac6454SAndrew Thompson 	    removable, udev->flags.self_powered ? "self" : "bus");
151702ac6454SAndrew Thompson 
151839307315SAndrew Thompson 	/* Start the interrupt endpoint, if any */
151902ac6454SAndrew Thompson 
15200eb8d462SHans Petter Selasky 	USB_MTX_LOCK(&sc->sc_mtx);
1521a0d53e0bSHans Petter Selasky 	usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
15220eb8d462SHans Petter Selasky 	USB_MTX_UNLOCK(&sc->sc_mtx);
152302ac6454SAndrew Thompson 
152402ac6454SAndrew Thompson 	/* Enable automatic power save on all USB HUBs */
152502ac6454SAndrew Thompson 
1526a593f6b8SAndrew Thompson 	usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
152702ac6454SAndrew Thompson 
152802ac6454SAndrew Thompson 	return (0);
152902ac6454SAndrew Thompson 
153002ac6454SAndrew Thompson error:
1531a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
153202ac6454SAndrew Thompson 
15332c79a775SHans Petter Selasky #if (USB_HAVE_FIXED_PORT == 0)
153402ac6454SAndrew Thompson 	free(udev->hub, M_USBDEV);
15352c79a775SHans Petter Selasky #endif
153602ac6454SAndrew Thompson 	udev->hub = NULL;
1537cb18f7d1SAlfred Perlstein 
1538cb18f7d1SAlfred Perlstein 	mtx_destroy(&sc->sc_mtx);
1539cb18f7d1SAlfred Perlstein 
154002ac6454SAndrew Thompson 	return (ENXIO);
154102ac6454SAndrew Thompson }
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson /*
154402ac6454SAndrew Thompson  * Called from process context when the hub is gone.
154502ac6454SAndrew Thompson  * Detach all devices on active ports.
154602ac6454SAndrew Thompson  */
1547e68fcc88STakanori Watanabe int
uhub_detach(device_t dev)154802ac6454SAndrew Thompson uhub_detach(device_t dev)
154902ac6454SAndrew Thompson {
155002ac6454SAndrew Thompson 	struct uhub_softc *sc = device_get_softc(dev);
1551760bc48eSAndrew Thompson 	struct usb_hub *hub = sc->sc_udev->hub;
1552a0d53e0bSHans Petter Selasky 	struct usb_bus *bus = sc->sc_udev->bus;
1553760bc48eSAndrew Thompson 	struct usb_device *child;
155402ac6454SAndrew Thompson 	uint8_t x;
155502ac6454SAndrew Thompson 
1556963169b4SHans Petter Selasky 	if (hub == NULL)		/* must be partially working */
155702ac6454SAndrew Thompson 		return (0);
1558d88688c7SAndrew Thompson 
1559d88688c7SAndrew Thompson 	/* Make sure interrupt transfer is gone. */
1560d88688c7SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1561d88688c7SAndrew Thompson 
1562d88688c7SAndrew Thompson 	/* Detach all ports */
156302ac6454SAndrew Thompson 	for (x = 0; x != hub->nports; x++) {
1564a0d53e0bSHans Petter Selasky 		child = usb_bus_port_get_device(bus, hub->ports + x);
156502ac6454SAndrew Thompson 
156602ac6454SAndrew Thompson 		if (child == NULL) {
156702ac6454SAndrew Thompson 			continue;
156802ac6454SAndrew Thompson 		}
156902ac6454SAndrew Thompson 
1570d88688c7SAndrew Thompson 		/*
1571d88688c7SAndrew Thompson 		 * Free USB device and all subdevices, if any.
1572d88688c7SAndrew Thompson 		 */
1573d88688c7SAndrew Thompson 		usb_free_device(child, 0);
1574d88688c7SAndrew Thompson 	}
157502ac6454SAndrew Thompson 
1576a0d53e0bSHans Petter Selasky #if USB_HAVE_TT_SUPPORT
1577a0d53e0bSHans Petter Selasky 	/* Make sure our TT messages are not queued anywhere */
1578a0d53e0bSHans Petter Selasky 	USB_BUS_LOCK(bus);
157943ea03d7SHans Petter Selasky 	usb_proc_mwait(USB_BUS_TT_PROC(bus),
1580a0d53e0bSHans Petter Selasky 	    &hub->tt_msg[0], &hub->tt_msg[1]);
1581a0d53e0bSHans Petter Selasky 	USB_BUS_UNLOCK(bus);
1582a0d53e0bSHans Petter Selasky #endif
1583a0d53e0bSHans Petter Selasky 
15842c79a775SHans Petter Selasky #if (USB_HAVE_FIXED_PORT == 0)
158502ac6454SAndrew Thompson 	free(hub, M_USBDEV);
15862c79a775SHans Petter Selasky #endif
158702ac6454SAndrew Thompson 	sc->sc_udev->hub = NULL;
1588cb18f7d1SAlfred Perlstein 
1589cb18f7d1SAlfred Perlstein 	mtx_destroy(&sc->sc_mtx);
1590cb18f7d1SAlfred Perlstein 
159102ac6454SAndrew Thompson 	return (0);
159202ac6454SAndrew Thompson }
159302ac6454SAndrew Thompson 
159402ac6454SAndrew Thompson static int
uhub_suspend(device_t dev)159502ac6454SAndrew Thompson uhub_suspend(device_t dev)
159602ac6454SAndrew Thompson {
159702ac6454SAndrew Thompson 	DPRINTF("\n");
159802ac6454SAndrew Thompson 	/* Sub-devices are not suspended here! */
159902ac6454SAndrew Thompson 	return (0);
160002ac6454SAndrew Thompson }
160102ac6454SAndrew Thompson 
160202ac6454SAndrew Thompson static int
uhub_resume(device_t dev)160302ac6454SAndrew Thompson uhub_resume(device_t dev)
160402ac6454SAndrew Thompson {
160502ac6454SAndrew Thompson 	DPRINTF("\n");
160602ac6454SAndrew Thompson 	/* Sub-devices are not resumed here! */
160702ac6454SAndrew Thompson 	return (0);
160802ac6454SAndrew Thompson }
160902ac6454SAndrew Thompson 
161002ac6454SAndrew Thompson static void
uhub_driver_added(device_t dev,driver_t * driver)161102ac6454SAndrew Thompson uhub_driver_added(device_t dev, driver_t *driver)
161202ac6454SAndrew Thompson {
1613a593f6b8SAndrew Thompson 	usb_needs_explore_all();
161402ac6454SAndrew Thompson }
161502ac6454SAndrew Thompson 
1616e68fcc88STakanori Watanabe void
uhub_find_iface_index(struct usb_hub * hub,device_t child,struct hub_result * res)1617760bc48eSAndrew Thompson uhub_find_iface_index(struct usb_hub *hub, device_t child,
161802ac6454SAndrew Thompson     struct hub_result *res)
161902ac6454SAndrew Thompson {
1620760bc48eSAndrew Thompson 	struct usb_interface *iface;
1621760bc48eSAndrew Thompson 	struct usb_device *udev;
162202ac6454SAndrew Thompson 	uint8_t nports;
162302ac6454SAndrew Thompson 	uint8_t x;
162402ac6454SAndrew Thompson 	uint8_t i;
162502ac6454SAndrew Thompson 
162602ac6454SAndrew Thompson 	nports = hub->nports;
162702ac6454SAndrew Thompson 	for (x = 0; x != nports; x++) {
1628a593f6b8SAndrew Thompson 		udev = usb_bus_port_get_device(hub->hubudev->bus,
162902ac6454SAndrew Thompson 		    hub->ports + x);
163002ac6454SAndrew Thompson 		if (!udev) {
163102ac6454SAndrew Thompson 			continue;
163202ac6454SAndrew Thompson 		}
163302ac6454SAndrew Thompson 		for (i = 0; i != USB_IFACE_MAX; i++) {
1634a593f6b8SAndrew Thompson 			iface = usbd_get_iface(udev, i);
163502ac6454SAndrew Thompson 			if (iface &&
163602ac6454SAndrew Thompson 			    (iface->subdev == child)) {
163702ac6454SAndrew Thompson 				res->iface_index = i;
163802ac6454SAndrew Thompson 				res->udev = udev;
163902ac6454SAndrew Thompson 				res->portno = x + 1;
164002ac6454SAndrew Thompson 				return;
164102ac6454SAndrew Thompson 			}
164202ac6454SAndrew Thompson 		}
164302ac6454SAndrew Thompson 	}
164402ac6454SAndrew Thompson 	res->iface_index = 0;
164502ac6454SAndrew Thompson 	res->udev = NULL;
164602ac6454SAndrew Thompson 	res->portno = 0;
164702ac6454SAndrew Thompson }
164802ac6454SAndrew Thompson 
1649e68fcc88STakanori Watanabe int
uhub_child_location(device_t parent,device_t child,struct sbuf * sb)1650ddfc9c4cSWarner Losh uhub_child_location(device_t parent, device_t child, struct sbuf *sb)
165102ac6454SAndrew Thompson {
1652d88688c7SAndrew Thompson 	struct uhub_softc *sc;
1653d88688c7SAndrew Thompson 	struct usb_hub *hub;
165402ac6454SAndrew Thompson 	struct hub_result res;
165502ac6454SAndrew Thompson 
1656ddfc9c4cSWarner Losh 	if (!device_is_attached(parent))
1657d88688c7SAndrew Thompson 		return (0);
1658d88688c7SAndrew Thompson 
1659d88688c7SAndrew Thompson 	sc = device_get_softc(parent);
1660d88688c7SAndrew Thompson 	hub = sc->sc_udev->hub;
1661d88688c7SAndrew Thompson 
16625e203517SHans Petter Selasky 	bus_topo_lock();
166302ac6454SAndrew Thompson 	uhub_find_iface_index(hub, child, &res);
166402ac6454SAndrew Thompson 	if (!res.udev) {
166502ac6454SAndrew Thompson 		DPRINTF("device not on hub\n");
166602ac6454SAndrew Thompson 		goto done;
166702ac6454SAndrew Thompson 	}
1668ddfc9c4cSWarner Losh 	sbuf_printf(sb, "bus=%u hubaddr=%u port=%u devaddr=%u"
16696fc22e8fSHans Petter Selasky 	    " interface=%u"
16706fc22e8fSHans Petter Selasky #if USB_HAVE_UGEN
16716fc22e8fSHans Petter Selasky 	    " ugen=%s"
16726fc22e8fSHans Petter Selasky #endif
16736fc22e8fSHans Petter Selasky 	    , device_get_unit(res.udev->bus->bdev)
16746fc22e8fSHans Petter Selasky 	    , (res.udev->parent_hub != NULL) ?
16756fc22e8fSHans Petter Selasky 	    res.udev->parent_hub->device_index : 0
16766fc22e8fSHans Petter Selasky 	    , res.portno, res.udev->device_index, res.iface_index
16776fc22e8fSHans Petter Selasky #if USB_HAVE_UGEN
16786fc22e8fSHans Petter Selasky 	    , res.udev->ugen_name
16796fc22e8fSHans Petter Selasky #endif
16806fc22e8fSHans Petter Selasky 	    );
168102ac6454SAndrew Thompson done:
16825e203517SHans Petter Selasky 	bus_topo_unlock();
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson 	return (0);
168502ac6454SAndrew Thompson }
168602ac6454SAndrew Thompson 
16879c750429SWarner Losh int
uhub_get_device_path(device_t bus,device_t child,const char * locator,struct sbuf * sb)16889c750429SWarner Losh uhub_get_device_path(device_t bus, device_t child, const char *locator,
16899c750429SWarner Losh     struct sbuf *sb)
16909c750429SWarner Losh {
16919c750429SWarner Losh 	struct uhub_softc *sc;
16929c750429SWarner Losh 	struct usb_hub *hub;
16939c750429SWarner Losh 	struct hub_result res;
16949c750429SWarner Losh 	int rv;
16959c750429SWarner Losh 
16969c750429SWarner Losh 	if (strcmp(locator, BUS_LOCATOR_UEFI) == 0) {
16979c750429SWarner Losh 		rv = bus_generic_get_device_path(device_get_parent(bus), bus, locator, sb);
16986bce8a35SHans Petter Selasky 		if (rv != 0)
16996bce8a35SHans Petter Selasky 			return (rv);
17009c750429SWarner Losh 
17019c750429SWarner Losh 		sc = device_get_softc(bus);
17029c750429SWarner Losh 		hub = sc->sc_udev->hub;
17039c750429SWarner Losh 
17045e203517SHans Petter Selasky 		bus_topo_lock();
17059c750429SWarner Losh 		uhub_find_iface_index(hub, child, &res);
17069c750429SWarner Losh 		if (!res.udev) {
17079c750429SWarner Losh 			printf("device not on hub\n");
17089c750429SWarner Losh 			goto done;
17099c750429SWarner Losh 		}
17109c750429SWarner Losh 		sbuf_printf(sb, "/USB(0x%x,0x%x)", res.portno - 1, res.iface_index);
17119c750429SWarner Losh 	done:
17125e203517SHans Petter Selasky 		bus_topo_unlock();
17139c750429SWarner Losh 		return (0);
17149c750429SWarner Losh 	}
17159c750429SWarner Losh 
17169c750429SWarner Losh 	/* For the rest, punt to the default handler */
17179c750429SWarner Losh 	return (bus_generic_get_device_path(bus, child, locator, sb));
17189c750429SWarner Losh }
17199c750429SWarner Losh 
172002ac6454SAndrew Thompson static int
uhub_child_pnpinfo(device_t parent,device_t child,struct sbuf * sb)1721ddfc9c4cSWarner Losh uhub_child_pnpinfo(device_t parent, device_t child, struct sbuf*sb)
172202ac6454SAndrew Thompson {
1723d88688c7SAndrew Thompson 	struct uhub_softc *sc;
1724d88688c7SAndrew Thompson 	struct usb_hub *hub;
1725760bc48eSAndrew Thompson 	struct usb_interface *iface;
172602ac6454SAndrew Thompson 	struct hub_result res;
1727f54ab96dSHans Petter Selasky 	uint8_t do_unlock;
172802ac6454SAndrew Thompson 
1729ddfc9c4cSWarner Losh 	if (!device_is_attached(parent))
1730d88688c7SAndrew Thompson 		return (0);
1731d88688c7SAndrew Thompson 
1732d88688c7SAndrew Thompson 	sc = device_get_softc(parent);
1733d88688c7SAndrew Thompson 	hub = sc->sc_udev->hub;
1734d88688c7SAndrew Thompson 
17355e203517SHans Petter Selasky 	bus_topo_lock();
173602ac6454SAndrew Thompson 	uhub_find_iface_index(hub, child, &res);
173702ac6454SAndrew Thompson 	if (!res.udev) {
173802ac6454SAndrew Thompson 		DPRINTF("device not on hub\n");
173902ac6454SAndrew Thompson 		goto done;
174002ac6454SAndrew Thompson 	}
1741a593f6b8SAndrew Thompson 	iface = usbd_get_iface(res.udev, res.iface_index);
174202ac6454SAndrew Thompson 	if (iface && iface->idesc) {
1743f54ab96dSHans Petter Selasky 		/* Make sure device information is not changed during the print. */
1744f54ab96dSHans Petter Selasky 		do_unlock = usbd_ctrl_lock(res.udev);
1745f54ab96dSHans Petter Selasky 
1746ddfc9c4cSWarner Losh 		sbuf_printf(sb, "vendor=0x%04x product=0x%04x "
174702ac6454SAndrew Thompson 		    "devclass=0x%02x devsubclass=0x%02x "
1748f809f280SWarner Losh 		    "devproto=0x%02x "
174902ac6454SAndrew Thompson 		    "sernum=\"%s\" "
1750bd73b187SAlfred Perlstein 		    "release=0x%04x "
1751e71c2138SHans Petter Selasky 		    "mode=%s "
1752e71c2138SHans Petter Selasky 		    "intclass=0x%02x intsubclass=0x%02x "
1753e71c2138SHans Petter Selasky 		    "intprotocol=0x%02x" "%s%s",
175402ac6454SAndrew Thompson 		    UGETW(res.udev->ddesc.idVendor),
175502ac6454SAndrew Thompson 		    UGETW(res.udev->ddesc.idProduct),
175602ac6454SAndrew Thompson 		    res.udev->ddesc.bDeviceClass,
175702ac6454SAndrew Thompson 		    res.udev->ddesc.bDeviceSubClass,
1758f809f280SWarner Losh 		    res.udev->ddesc.bDeviceProtocol,
1759ae538d85SAndrew Thompson 		    usb_get_serial(res.udev),
1760bd73b187SAlfred Perlstein 		    UGETW(res.udev->ddesc.bcdDevice),
1761e71c2138SHans Petter Selasky 		    (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
176202ac6454SAndrew Thompson 		    iface->idesc->bInterfaceClass,
17638427ed84SHans Petter Selasky 		    iface->idesc->bInterfaceSubClass,
1764e71c2138SHans Petter Selasky 		    iface->idesc->bInterfaceProtocol,
17658427ed84SHans Petter Selasky 		    iface->pnpinfo ? " " : "",
17668427ed84SHans Petter Selasky 		    iface->pnpinfo ? iface->pnpinfo : "");
1767f54ab96dSHans Petter Selasky 
1768f54ab96dSHans Petter Selasky 		if (do_unlock)
1769f54ab96dSHans Petter Selasky 			usbd_ctrl_unlock(res.udev);
177002ac6454SAndrew Thompson 	}
177102ac6454SAndrew Thompson done:
17725e203517SHans Petter Selasky 	bus_topo_unlock();
177302ac6454SAndrew Thompson 
177402ac6454SAndrew Thompson 	return (0);
177502ac6454SAndrew Thompson }
177602ac6454SAndrew Thompson 
177702ac6454SAndrew Thompson /*
177802ac6454SAndrew Thompson  * The USB Transaction Translator:
177902ac6454SAndrew Thompson  * ===============================
178002ac6454SAndrew Thompson  *
178120733245SPedro F. Giffuni  * When doing LOW- and FULL-speed USB transfers across a HIGH-speed
178202ac6454SAndrew Thompson  * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
178302ac6454SAndrew Thompson  * USB transfers. To utilize bandwidth dynamically the "scatter and
178402ac6454SAndrew Thompson  * gather" principle must be applied. This means that bandwidth must
178502ac6454SAndrew Thompson  * be divided into equal parts of bandwidth. With regard to USB all
178602ac6454SAndrew Thompson  * data is transferred in smaller packets with length
178702ac6454SAndrew Thompson  * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
178802ac6454SAndrew Thompson  * not a constant!
178902ac6454SAndrew Thompson  *
179002ac6454SAndrew Thompson  * The bandwidth scheduler which I have implemented will simply pack
179102ac6454SAndrew Thompson  * the USB transfers back to back until there is no more space in the
179202ac6454SAndrew Thompson  * schedule. Out of the 8 microframes which the USB 2.0 standard
179302ac6454SAndrew Thompson  * provides, only 6 are available for non-HIGH-speed devices. I have
179402ac6454SAndrew Thompson  * reserved the first 4 microframes for ISOCHRONOUS transfers. The
179502ac6454SAndrew Thompson  * last 2 microframes I have reserved for INTERRUPT transfers. Without
179602ac6454SAndrew Thompson  * this division, it is very difficult to allocate and free bandwidth
179702ac6454SAndrew Thompson  * dynamically.
179802ac6454SAndrew Thompson  *
179902ac6454SAndrew Thompson  * NOTE about the Transaction Translator in USB HUBs:
180002ac6454SAndrew Thompson  *
180102ac6454SAndrew Thompson  * USB HUBs have a very simple Transaction Translator, that will
180202ac6454SAndrew Thompson  * simply pipeline all the SPLIT transactions. That means that the
180302ac6454SAndrew Thompson  * transactions will be executed in the order they are queued!
180402ac6454SAndrew Thompson  *
180502ac6454SAndrew Thompson  */
180602ac6454SAndrew Thompson 
180702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1808a593f6b8SAndrew Thompson  *	usb_intr_find_best_slot
180902ac6454SAndrew Thompson  *
181002ac6454SAndrew Thompson  * Return value:
181102ac6454SAndrew Thompson  *   The best Transaction Translation slot for an interrupt endpoint.
181202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
181302ac6454SAndrew Thompson static uint8_t
usb_intr_find_best_slot(usb_size_t * ptr,uint8_t start,uint8_t end,uint8_t mask)1814f12c6c29SAndrew Thompson usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1815f12c6c29SAndrew Thompson     uint8_t end, uint8_t mask)
181602ac6454SAndrew Thompson {
18176d917491SHans Petter Selasky 	usb_size_t min = (usb_size_t)-1;
1818f12c6c29SAndrew Thompson 	usb_size_t sum;
181902ac6454SAndrew Thompson 	uint8_t x;
182002ac6454SAndrew Thompson 	uint8_t y;
1821f12c6c29SAndrew Thompson 	uint8_t z;
182202ac6454SAndrew Thompson 
182302ac6454SAndrew Thompson 	y = 0;
182402ac6454SAndrew Thompson 
182502ac6454SAndrew Thompson 	/* find the last slot with lesser used bandwidth */
182602ac6454SAndrew Thompson 
182702ac6454SAndrew Thompson 	for (x = start; x < end; x++) {
1828f12c6c29SAndrew Thompson 		sum = 0;
1829f12c6c29SAndrew Thompson 
1830f12c6c29SAndrew Thompson 		/* compute sum of bandwidth */
1831f12c6c29SAndrew Thompson 		for (z = x; z < end; z++) {
1832f12c6c29SAndrew Thompson 			if (mask & (1U << (z - x)))
1833f12c6c29SAndrew Thompson 				sum += ptr[z];
1834f12c6c29SAndrew Thompson 		}
1835f12c6c29SAndrew Thompson 
1836f12c6c29SAndrew Thompson 		/* check if the current multi-slot is more optimal */
1837f12c6c29SAndrew Thompson 		if (min >= sum) {
1838f12c6c29SAndrew Thompson 			min = sum;
183902ac6454SAndrew Thompson 			y = x;
184002ac6454SAndrew Thompson 		}
1841f12c6c29SAndrew Thompson 
1842f12c6c29SAndrew Thompson 		/* check if the mask is about to be shifted out */
1843f12c6c29SAndrew Thompson 		if (mask & (1U << (end - 1 - x)))
1844f12c6c29SAndrew Thompson 			break;
184502ac6454SAndrew Thompson 	}
184602ac6454SAndrew Thompson 	return (y);
184702ac6454SAndrew Thompson }
184802ac6454SAndrew Thompson 
184902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1850f12c6c29SAndrew Thompson  *	usb_hs_bandwidth_adjust
185102ac6454SAndrew Thompson  *
185220733245SPedro F. Giffuni  * This function will update the bandwidth usage for the microframe
185302ac6454SAndrew Thompson  * having index "slot" by "len" bytes. "len" can be negative.  If the
185402ac6454SAndrew Thompson  * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
185502ac6454SAndrew Thompson  * the "slot" argument will be replaced by the slot having least used
1856f12c6c29SAndrew Thompson  * bandwidth. The "mask" argument is used for multi-slot allocations.
185702ac6454SAndrew Thompson  *
185802ac6454SAndrew Thompson  * Returns:
1859f12c6c29SAndrew Thompson  *    The slot in which the bandwidth update was done: 0..7
186002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1861f12c6c29SAndrew Thompson static uint8_t
usb_hs_bandwidth_adjust(struct usb_device * udev,int16_t len,uint8_t slot,uint8_t mask)1862f12c6c29SAndrew Thompson usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1863f12c6c29SAndrew Thompson     uint8_t slot, uint8_t mask)
186402ac6454SAndrew Thompson {
1865760bc48eSAndrew Thompson 	struct usb_bus *bus = udev->bus;
1866760bc48eSAndrew Thompson 	struct usb_hub *hub;
18678d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
1868f12c6c29SAndrew Thompson 	uint8_t x;
186902ac6454SAndrew Thompson 
187002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
187102ac6454SAndrew Thompson 
1872a593f6b8SAndrew Thompson 	speed = usbd_get_speed(udev);
187302ac6454SAndrew Thompson 
187402ac6454SAndrew Thompson 	switch (speed) {
187502ac6454SAndrew Thompson 	case USB_SPEED_LOW:
187602ac6454SAndrew Thompson 	case USB_SPEED_FULL:
187702ac6454SAndrew Thompson 		if (speed == USB_SPEED_LOW) {
187802ac6454SAndrew Thompson 			len *= 8;
187902ac6454SAndrew Thompson 		}
188002ac6454SAndrew Thompson 		/*
188102ac6454SAndrew Thompson 	         * The Host Controller Driver should have
188202ac6454SAndrew Thompson 	         * performed checks so that the lookup
188302ac6454SAndrew Thompson 	         * below does not result in a NULL pointer
188402ac6454SAndrew Thompson 	         * access.
188502ac6454SAndrew Thompson 	         */
188602ac6454SAndrew Thompson 
1887495f25ceSAndrew Thompson 		hub = udev->parent_hs_hub->hub;
188802ac6454SAndrew Thompson 		if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1889a593f6b8SAndrew Thompson 			slot = usb_intr_find_best_slot(hub->uframe_usage,
1890f12c6c29SAndrew Thompson 			    USB_FS_ISOC_UFRAME_MAX, 6, mask);
189102ac6454SAndrew Thompson 		}
1892f12c6c29SAndrew Thompson 		for (x = slot; x < 8; x++) {
1893f12c6c29SAndrew Thompson 			if (mask & (1U << (x - slot))) {
1894f12c6c29SAndrew Thompson 				hub->uframe_usage[x] += len;
1895f12c6c29SAndrew Thompson 				bus->uframe_usage[x] += len;
1896f12c6c29SAndrew Thompson 			}
1897f12c6c29SAndrew Thompson 		}
189802ac6454SAndrew Thompson 		break;
189902ac6454SAndrew Thompson 	default:
190002ac6454SAndrew Thompson 		if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1901a593f6b8SAndrew Thompson 			slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1902f12c6c29SAndrew Thompson 			    USB_HS_MICRO_FRAMES_MAX, mask);
190302ac6454SAndrew Thompson 		}
1904f12c6c29SAndrew Thompson 		for (x = slot; x < 8; x++) {
1905f12c6c29SAndrew Thompson 			if (mask & (1U << (x - slot))) {
1906f12c6c29SAndrew Thompson 				bus->uframe_usage[x] += len;
1907f12c6c29SAndrew Thompson 			}
1908f12c6c29SAndrew Thompson 		}
190902ac6454SAndrew Thompson 		break;
191002ac6454SAndrew Thompson 	}
191102ac6454SAndrew Thompson 	return (slot);
191202ac6454SAndrew Thompson }
191302ac6454SAndrew Thompson 
191402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1915f12c6c29SAndrew Thompson  *	usb_hs_bandwidth_alloc
1916f12c6c29SAndrew Thompson  *
1917f12c6c29SAndrew Thompson  * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1918f12c6c29SAndrew Thompson  *------------------------------------------------------------------------*/
1919f12c6c29SAndrew Thompson void
usb_hs_bandwidth_alloc(struct usb_xfer * xfer)1920f12c6c29SAndrew Thompson usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1921f12c6c29SAndrew Thompson {
1922f12c6c29SAndrew Thompson 	struct usb_device *udev;
1923f12c6c29SAndrew Thompson 	uint8_t slot;
1924f12c6c29SAndrew Thompson 	uint8_t mask;
1925f12c6c29SAndrew Thompson 	uint8_t speed;
1926f12c6c29SAndrew Thompson 
1927f12c6c29SAndrew Thompson 	udev = xfer->xroot->udev;
1928f12c6c29SAndrew Thompson 
1929f12c6c29SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_HOST)
1930f12c6c29SAndrew Thompson 		return;		/* not supported */
1931f12c6c29SAndrew Thompson 
1932f12c6c29SAndrew Thompson 	xfer->endpoint->refcount_bw++;
1933f12c6c29SAndrew Thompson 	if (xfer->endpoint->refcount_bw != 1)
1934f12c6c29SAndrew Thompson 		return;		/* already allocated */
1935f12c6c29SAndrew Thompson 
1936f12c6c29SAndrew Thompson 	speed = usbd_get_speed(udev);
1937f12c6c29SAndrew Thompson 
1938f12c6c29SAndrew Thompson 	switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1939f12c6c29SAndrew Thompson 	case UE_INTERRUPT:
1940f12c6c29SAndrew Thompson 		/* allocate a microframe slot */
1941f12c6c29SAndrew Thompson 
1942f12c6c29SAndrew Thompson 		mask = 0x01;
1943f12c6c29SAndrew Thompson 		slot = usb_hs_bandwidth_adjust(udev,
1944f12c6c29SAndrew Thompson 		    xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1945f12c6c29SAndrew Thompson 
1946f12c6c29SAndrew Thompson 		xfer->endpoint->usb_uframe = slot;
1947f12c6c29SAndrew Thompson 		xfer->endpoint->usb_smask = mask << slot;
1948f12c6c29SAndrew Thompson 
1949f12c6c29SAndrew Thompson 		if ((speed != USB_SPEED_FULL) &&
1950f12c6c29SAndrew Thompson 		    (speed != USB_SPEED_LOW)) {
1951f12c6c29SAndrew Thompson 			xfer->endpoint->usb_cmask = 0x00 ;
1952f12c6c29SAndrew Thompson 		} else {
1953f12c6c29SAndrew Thompson 			xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1954f12c6c29SAndrew Thompson 		}
1955f12c6c29SAndrew Thompson 		break;
1956f12c6c29SAndrew Thompson 
1957f12c6c29SAndrew Thompson 	case UE_ISOCHRONOUS:
1958f12c6c29SAndrew Thompson 		switch (usbd_xfer_get_fps_shift(xfer)) {
1959f12c6c29SAndrew Thompson 		case 0:
1960f12c6c29SAndrew Thompson 			mask = 0xFF;
1961f12c6c29SAndrew Thompson 			break;
1962f12c6c29SAndrew Thompson 		case 1:
1963f12c6c29SAndrew Thompson 			mask = 0x55;
1964f12c6c29SAndrew Thompson 			break;
1965f12c6c29SAndrew Thompson 		case 2:
1966f12c6c29SAndrew Thompson 			mask = 0x11;
1967f12c6c29SAndrew Thompson 			break;
1968f12c6c29SAndrew Thompson 		default:
1969f12c6c29SAndrew Thompson 			mask = 0x01;
1970f12c6c29SAndrew Thompson 			break;
1971f12c6c29SAndrew Thompson 		}
1972f12c6c29SAndrew Thompson 
1973f12c6c29SAndrew Thompson 		/* allocate a microframe multi-slot */
1974f12c6c29SAndrew Thompson 
1975f12c6c29SAndrew Thompson 		slot = usb_hs_bandwidth_adjust(udev,
1976f12c6c29SAndrew Thompson 		    xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1977f12c6c29SAndrew Thompson 
1978f12c6c29SAndrew Thompson 		xfer->endpoint->usb_uframe = slot;
1979f12c6c29SAndrew Thompson 		xfer->endpoint->usb_cmask = 0;
1980f12c6c29SAndrew Thompson 		xfer->endpoint->usb_smask = mask << slot;
1981f12c6c29SAndrew Thompson 		break;
1982f12c6c29SAndrew Thompson 
1983f12c6c29SAndrew Thompson 	default:
1984f12c6c29SAndrew Thompson 		xfer->endpoint->usb_uframe = 0;
1985f12c6c29SAndrew Thompson 		xfer->endpoint->usb_cmask = 0;
1986f12c6c29SAndrew Thompson 		xfer->endpoint->usb_smask = 0;
1987f12c6c29SAndrew Thompson 		break;
1988f12c6c29SAndrew Thompson 	}
1989f12c6c29SAndrew Thompson 
1990f12c6c29SAndrew Thompson 	DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1991f12c6c29SAndrew Thompson 	    xfer->endpoint->usb_uframe,
1992f12c6c29SAndrew Thompson 	    xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1993f12c6c29SAndrew Thompson }
1994f12c6c29SAndrew Thompson 
1995f12c6c29SAndrew Thompson /*------------------------------------------------------------------------*
1996f12c6c29SAndrew Thompson  *	usb_hs_bandwidth_free
1997f12c6c29SAndrew Thompson  *
1998f12c6c29SAndrew Thompson  * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1999f12c6c29SAndrew Thompson  *------------------------------------------------------------------------*/
2000f12c6c29SAndrew Thompson void
usb_hs_bandwidth_free(struct usb_xfer * xfer)2001f12c6c29SAndrew Thompson usb_hs_bandwidth_free(struct usb_xfer *xfer)
2002f12c6c29SAndrew Thompson {
2003f12c6c29SAndrew Thompson 	struct usb_device *udev;
2004f12c6c29SAndrew Thompson 	uint8_t slot;
2005f12c6c29SAndrew Thompson 	uint8_t mask;
2006f12c6c29SAndrew Thompson 
2007f12c6c29SAndrew Thompson 	udev = xfer->xroot->udev;
2008f12c6c29SAndrew Thompson 
2009f12c6c29SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_HOST)
2010f12c6c29SAndrew Thompson 		return;		/* not supported */
2011f12c6c29SAndrew Thompson 
2012f12c6c29SAndrew Thompson 	xfer->endpoint->refcount_bw--;
2013f12c6c29SAndrew Thompson 	if (xfer->endpoint->refcount_bw != 0)
2014f12c6c29SAndrew Thompson 		return;		/* still allocated */
2015f12c6c29SAndrew Thompson 
2016f12c6c29SAndrew Thompson 	switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2017f12c6c29SAndrew Thompson 	case UE_INTERRUPT:
2018f12c6c29SAndrew Thompson 	case UE_ISOCHRONOUS:
2019f12c6c29SAndrew Thompson 
2020f12c6c29SAndrew Thompson 		slot = xfer->endpoint->usb_uframe;
2021f12c6c29SAndrew Thompson 		mask = xfer->endpoint->usb_smask;
2022f12c6c29SAndrew Thompson 
2023f12c6c29SAndrew Thompson 		/* free microframe slot(s): */
2024f12c6c29SAndrew Thompson 		usb_hs_bandwidth_adjust(udev,
2025f12c6c29SAndrew Thompson 		    -xfer->max_frame_size, slot, mask >> slot);
2026f12c6c29SAndrew Thompson 
2027f12c6c29SAndrew Thompson 		DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2028f12c6c29SAndrew Thompson 		    slot, mask >> slot);
2029f12c6c29SAndrew Thompson 
2030f12c6c29SAndrew Thompson 		xfer->endpoint->usb_uframe = 0;
2031f12c6c29SAndrew Thompson 		xfer->endpoint->usb_cmask = 0;
2032f12c6c29SAndrew Thompson 		xfer->endpoint->usb_smask = 0;
2033f12c6c29SAndrew Thompson 		break;
2034f12c6c29SAndrew Thompson 
2035f12c6c29SAndrew Thompson 	default:
2036f12c6c29SAndrew Thompson 		break;
2037f12c6c29SAndrew Thompson 	}
2038f12c6c29SAndrew Thompson }
2039f12c6c29SAndrew Thompson 
2040f12c6c29SAndrew Thompson /*------------------------------------------------------------------------*
2041a593f6b8SAndrew Thompson  *	usb_isoc_time_expand
204202ac6454SAndrew Thompson  *
204302ac6454SAndrew Thompson  * This function will expand the time counter from 7-bit to 16-bit.
204402ac6454SAndrew Thompson  *
204502ac6454SAndrew Thompson  * Returns:
204602ac6454SAndrew Thompson  *   16-bit isochronous time counter.
204702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
204802ac6454SAndrew Thompson uint16_t
usb_isoc_time_expand(struct usb_bus * bus,uint16_t isoc_time_curr)2049a593f6b8SAndrew Thompson usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
205002ac6454SAndrew Thompson {
205102ac6454SAndrew Thompson 	uint16_t rem;
205202ac6454SAndrew Thompson 
205302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
205402ac6454SAndrew Thompson 
205502ac6454SAndrew Thompson 	rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
205602ac6454SAndrew Thompson 
205702ac6454SAndrew Thompson 	isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
205802ac6454SAndrew Thompson 
205902ac6454SAndrew Thompson 	if (isoc_time_curr < rem) {
206002ac6454SAndrew Thompson 		/* the time counter wrapped around */
206102ac6454SAndrew Thompson 		bus->isoc_time_last += USB_ISOC_TIME_MAX;
206202ac6454SAndrew Thompson 	}
206302ac6454SAndrew Thompson 	/* update the remainder */
206402ac6454SAndrew Thompson 
206502ac6454SAndrew Thompson 	bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
206602ac6454SAndrew Thompson 	bus->isoc_time_last |= isoc_time_curr;
206702ac6454SAndrew Thompson 
206802ac6454SAndrew Thompson 	return (bus->isoc_time_last);
206902ac6454SAndrew Thompson }
207002ac6454SAndrew Thompson 
207102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
20720a4cc48fSHans Petter Selasky  *	usbd_fs_isoc_schedule_alloc_slot
207302ac6454SAndrew Thompson  *
207402ac6454SAndrew Thompson  * This function will allocate bandwidth for an isochronous FULL speed
20750a4cc48fSHans Petter Selasky  * transaction in the FULL speed schedule.
207602ac6454SAndrew Thompson  *
207702ac6454SAndrew Thompson  * Returns:
20780a4cc48fSHans Petter Selasky  *    <8: Success
207902ac6454SAndrew Thompson  * Else: Error
208002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
20814eae601eSAndrew Thompson #if USB_HAVE_TT_SUPPORT
208202ac6454SAndrew Thompson uint8_t
usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer * isoc_xfer,uint16_t isoc_time)20830a4cc48fSHans Petter Selasky usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
208402ac6454SAndrew Thompson {
20850a4cc48fSHans Petter Selasky 	struct usb_xfer *xfer;
20860a4cc48fSHans Petter Selasky 	struct usb_xfer *pipe_xfer;
20870a4cc48fSHans Petter Selasky 	struct usb_bus *bus;
20880a4cc48fSHans Petter Selasky 	usb_frlength_t len;
20890a4cc48fSHans Petter Selasky 	usb_frlength_t data_len;
20900a4cc48fSHans Petter Selasky 	uint16_t delta;
20910a4cc48fSHans Petter Selasky 	uint16_t slot;
20920a4cc48fSHans Petter Selasky 	uint8_t retval;
209302ac6454SAndrew Thompson 
20940a4cc48fSHans Petter Selasky 	data_len = 0;
20950a4cc48fSHans Petter Selasky 	slot = 0;
209602ac6454SAndrew Thompson 
20970a4cc48fSHans Petter Selasky 	bus = isoc_xfer->xroot->bus;
20980a4cc48fSHans Petter Selasky 
20990a4cc48fSHans Petter Selasky 	TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
21000a4cc48fSHans Petter Selasky 		/* skip self, if any */
21010a4cc48fSHans Petter Selasky 
21020a4cc48fSHans Petter Selasky 		if (xfer == isoc_xfer)
21030a4cc48fSHans Petter Selasky 			continue;
21040a4cc48fSHans Petter Selasky 
21050a4cc48fSHans Petter Selasky 		/* check if this USB transfer is going through the same TT */
21060a4cc48fSHans Petter Selasky 
21070a4cc48fSHans Petter Selasky 		if (xfer->xroot->udev->parent_hs_hub !=
21080a4cc48fSHans Petter Selasky 		    isoc_xfer->xroot->udev->parent_hs_hub) {
21090a4cc48fSHans Petter Selasky 			continue;
21100a4cc48fSHans Petter Selasky 		}
21110a4cc48fSHans Petter Selasky 		if ((isoc_xfer->xroot->udev->parent_hs_hub->
21120a4cc48fSHans Petter Selasky 		    ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
21130a4cc48fSHans Petter Selasky 		    (xfer->xroot->udev->hs_port_no !=
21140a4cc48fSHans Petter Selasky 		    isoc_xfer->xroot->udev->hs_port_no)) {
21150a4cc48fSHans Petter Selasky 			continue;
21160a4cc48fSHans Petter Selasky 		}
21170a4cc48fSHans Petter Selasky 		if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
21180a4cc48fSHans Petter Selasky 			continue;
21190a4cc48fSHans Petter Selasky 
21200a4cc48fSHans Petter Selasky 		/* check if isoc_time is part of this transfer */
21210a4cc48fSHans Petter Selasky 
21220a4cc48fSHans Petter Selasky 		delta = xfer->isoc_time_complete - isoc_time;
21230a4cc48fSHans Petter Selasky 		if (delta > 0 && delta <= xfer->nframes) {
21240a4cc48fSHans Petter Selasky 			delta = xfer->nframes - delta;
21250a4cc48fSHans Petter Selasky 
21260a4cc48fSHans Petter Selasky 			len = xfer->frlengths[delta];
212702ac6454SAndrew Thompson 			len += 8;
212802ac6454SAndrew Thompson 			len *= 7;
212902ac6454SAndrew Thompson 			len /= 6;
213002ac6454SAndrew Thompson 
21310a4cc48fSHans Petter Selasky 			data_len += len;
213202ac6454SAndrew Thompson 		}
213302ac6454SAndrew Thompson 
2134a5cf1aaaSHans Petter Selasky 		/*
2135a5cf1aaaSHans Petter Selasky 		 * Check double buffered transfers. Only stream ID
2136a5cf1aaaSHans Petter Selasky 		 * equal to zero is valid here!
2137a5cf1aaaSHans Petter Selasky 		 */
2138a5cf1aaaSHans Petter Selasky 		TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
21390a4cc48fSHans Petter Selasky 		    wait_entry) {
21400a4cc48fSHans Petter Selasky 			/* skip self, if any */
21410a4cc48fSHans Petter Selasky 
21420a4cc48fSHans Petter Selasky 			if (pipe_xfer == isoc_xfer)
21430a4cc48fSHans Petter Selasky 				continue;
21440a4cc48fSHans Petter Selasky 
21450a4cc48fSHans Petter Selasky 			/* check if isoc_time is part of this transfer */
21460a4cc48fSHans Petter Selasky 
21470a4cc48fSHans Petter Selasky 			delta = pipe_xfer->isoc_time_complete - isoc_time;
21480a4cc48fSHans Petter Selasky 			if (delta > 0 && delta <= pipe_xfer->nframes) {
21490a4cc48fSHans Petter Selasky 				delta = pipe_xfer->nframes - delta;
21500a4cc48fSHans Petter Selasky 
21510a4cc48fSHans Petter Selasky 				len = pipe_xfer->frlengths[delta];
21520a4cc48fSHans Petter Selasky 				len += 8;
21530a4cc48fSHans Petter Selasky 				len *= 7;
21540a4cc48fSHans Petter Selasky 				len /= 6;
21550a4cc48fSHans Petter Selasky 
21560a4cc48fSHans Petter Selasky 				data_len += len;
215702ac6454SAndrew Thompson 			}
21580a4cc48fSHans Petter Selasky 		}
21590a4cc48fSHans Petter Selasky 	}
21600a4cc48fSHans Petter Selasky 
21610a4cc48fSHans Petter Selasky 	while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
21620a4cc48fSHans Petter Selasky 		data_len -= USB_FS_BYTES_PER_HS_UFRAME;
21630a4cc48fSHans Petter Selasky 		slot++;
21640a4cc48fSHans Petter Selasky 	}
21650a4cc48fSHans Petter Selasky 
21660a4cc48fSHans Petter Selasky 	/* check for overflow */
21670a4cc48fSHans Petter Selasky 
21680a4cc48fSHans Petter Selasky 	if (slot >= USB_FS_ISOC_UFRAME_MAX)
21690a4cc48fSHans Petter Selasky 		return (255);
21700a4cc48fSHans Petter Selasky 
21710a4cc48fSHans Petter Selasky 	retval = slot;
21720a4cc48fSHans Petter Selasky 
21730a4cc48fSHans Petter Selasky 	delta = isoc_xfer->isoc_time_complete - isoc_time;
21740a4cc48fSHans Petter Selasky 	if (delta > 0 && delta <= isoc_xfer->nframes) {
21750a4cc48fSHans Petter Selasky 		delta = isoc_xfer->nframes - delta;
21760a4cc48fSHans Petter Selasky 
21770a4cc48fSHans Petter Selasky 		len = isoc_xfer->frlengths[delta];
21780a4cc48fSHans Petter Selasky 		len += 8;
21790a4cc48fSHans Petter Selasky 		len *= 7;
21800a4cc48fSHans Petter Selasky 		len /= 6;
21810a4cc48fSHans Petter Selasky 
21820a4cc48fSHans Petter Selasky 		data_len += len;
21830a4cc48fSHans Petter Selasky 	}
21840a4cc48fSHans Petter Selasky 
21850a4cc48fSHans Petter Selasky 	while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
21860a4cc48fSHans Petter Selasky 		data_len -= USB_FS_BYTES_PER_HS_UFRAME;
21870a4cc48fSHans Petter Selasky 		slot++;
21880a4cc48fSHans Petter Selasky 	}
21890a4cc48fSHans Petter Selasky 
21900a4cc48fSHans Petter Selasky 	/* check for overflow */
21910a4cc48fSHans Petter Selasky 
21920a4cc48fSHans Petter Selasky 	if (slot >= USB_FS_ISOC_UFRAME_MAX)
21930a4cc48fSHans Petter Selasky 		return (255);
21940a4cc48fSHans Petter Selasky 
21950a4cc48fSHans Petter Selasky 	return (retval);
219602ac6454SAndrew Thompson }
21974eae601eSAndrew Thompson #endif
219802ac6454SAndrew Thompson 
219902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2200a593f6b8SAndrew Thompson  *	usb_bus_port_get_device
220102ac6454SAndrew Thompson  *
220202ac6454SAndrew Thompson  * This function is NULL safe.
220302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
2204760bc48eSAndrew Thompson struct usb_device *
usb_bus_port_get_device(struct usb_bus * bus,struct usb_port * up)2205a593f6b8SAndrew Thompson usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
220602ac6454SAndrew Thompson {
220702ac6454SAndrew Thompson 	if ((bus == NULL) || (up == NULL)) {
220802ac6454SAndrew Thompson 		/* be NULL safe */
220902ac6454SAndrew Thompson 		return (NULL);
221002ac6454SAndrew Thompson 	}
221102ac6454SAndrew Thompson 	if (up->device_index == 0) {
221202ac6454SAndrew Thompson 		/* nothing to do */
221302ac6454SAndrew Thompson 		return (NULL);
221402ac6454SAndrew Thompson 	}
221502ac6454SAndrew Thompson 	return (bus->devices[up->device_index]);
221602ac6454SAndrew Thompson }
221702ac6454SAndrew Thompson 
221802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2219a593f6b8SAndrew Thompson  *	usb_bus_port_set_device
222002ac6454SAndrew Thompson  *
222102ac6454SAndrew Thompson  * This function is NULL safe.
222202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
222302ac6454SAndrew Thompson void
usb_bus_port_set_device(struct usb_bus * bus,struct usb_port * up,struct usb_device * udev,uint8_t device_index)2224a593f6b8SAndrew Thompson usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2225760bc48eSAndrew Thompson     struct usb_device *udev, uint8_t device_index)
222602ac6454SAndrew Thompson {
222702ac6454SAndrew Thompson 	if (bus == NULL) {
222802ac6454SAndrew Thompson 		/* be NULL safe */
222902ac6454SAndrew Thompson 		return;
223002ac6454SAndrew Thompson 	}
223102ac6454SAndrew Thompson 	/*
223202ac6454SAndrew Thompson 	 * There is only one case where we don't
223302ac6454SAndrew Thompson 	 * have an USB port, and that is the Root Hub!
223402ac6454SAndrew Thompson          */
223502ac6454SAndrew Thompson 	if (up) {
223602ac6454SAndrew Thompson 		if (udev) {
223702ac6454SAndrew Thompson 			up->device_index = device_index;
223802ac6454SAndrew Thompson 		} else {
223902ac6454SAndrew Thompson 			device_index = up->device_index;
224002ac6454SAndrew Thompson 			up->device_index = 0;
224102ac6454SAndrew Thompson 		}
224202ac6454SAndrew Thompson 	}
224302ac6454SAndrew Thompson 	/*
224402ac6454SAndrew Thompson 	 * Make relationships to our new device
224502ac6454SAndrew Thompson 	 */
224602ac6454SAndrew Thompson 	if (device_index != 0) {
22470f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
2248a593f6b8SAndrew Thompson 		mtx_lock(&usb_ref_lock);
22490f6e8f93SAndrew Thompson #endif
225002ac6454SAndrew Thompson 		bus->devices[device_index] = udev;
22510f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
2252a593f6b8SAndrew Thompson 		mtx_unlock(&usb_ref_lock);
22530f6e8f93SAndrew Thompson #endif
225402ac6454SAndrew Thompson 	}
225502ac6454SAndrew Thompson 	/*
225602ac6454SAndrew Thompson 	 * Debug print
225702ac6454SAndrew Thompson 	 */
225802ac6454SAndrew Thompson 	DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
225902ac6454SAndrew Thompson }
226002ac6454SAndrew Thompson 
226102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2262a593f6b8SAndrew Thompson  *	usb_needs_explore
226302ac6454SAndrew Thompson  *
226402ac6454SAndrew Thompson  * This functions is called when the USB event thread needs to run.
226502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
226602ac6454SAndrew Thompson void
usb_needs_explore(struct usb_bus * bus,uint8_t do_probe)2267a593f6b8SAndrew Thompson usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
226802ac6454SAndrew Thompson {
226939307315SAndrew Thompson 	uint8_t do_unlock;
227039307315SAndrew Thompson 
227102ac6454SAndrew Thompson 	DPRINTF("\n");
227202ac6454SAndrew Thompson 
2273433ded8bSHans Petter Selasky 	if (cold != 0) {
2274433ded8bSHans Petter Selasky 		DPRINTF("Cold\n");
2275433ded8bSHans Petter Selasky 		return;
2276433ded8bSHans Petter Selasky 	}
2277433ded8bSHans Petter Selasky 
227802ac6454SAndrew Thompson 	if (bus == NULL) {
227902ac6454SAndrew Thompson 		DPRINTF("No bus pointer!\n");
228002ac6454SAndrew Thompson 		return;
228102ac6454SAndrew Thompson 	}
228239307315SAndrew Thompson 	if ((bus->devices == NULL) ||
228339307315SAndrew Thompson 	    (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
228439307315SAndrew Thompson 		DPRINTF("No root HUB\n");
228539307315SAndrew Thompson 		return;
228639307315SAndrew Thompson 	}
228739307315SAndrew Thompson 	if (mtx_owned(&bus->bus_mtx)) {
228839307315SAndrew Thompson 		do_unlock = 0;
228939307315SAndrew Thompson 	} else {
229002ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
229139307315SAndrew Thompson 		do_unlock = 1;
229239307315SAndrew Thompson 	}
229302ac6454SAndrew Thompson 	if (do_probe) {
229402ac6454SAndrew Thompson 		bus->do_probe = 1;
229502ac6454SAndrew Thompson 	}
22969b3a48eeSHans Petter Selasky 	if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
229702ac6454SAndrew Thompson 	    &bus->explore_msg[0], &bus->explore_msg[1])) {
229802ac6454SAndrew Thompson 		/* ignore */
229902ac6454SAndrew Thompson 	}
230039307315SAndrew Thompson 	if (do_unlock) {
230102ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
230202ac6454SAndrew Thompson 	}
230339307315SAndrew Thompson }
230402ac6454SAndrew Thompson 
230502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2306a593f6b8SAndrew Thompson  *	usb_needs_explore_all
230702ac6454SAndrew Thompson  *
230802ac6454SAndrew Thompson  * This function is called whenever a new driver is loaded and will
2309db4fcadfSConrad Meyer  * cause that all USB buses are re-explored.
231002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
231102ac6454SAndrew Thompson void
usb_needs_explore_all(void)2312a593f6b8SAndrew Thompson usb_needs_explore_all(void)
231302ac6454SAndrew Thompson {
2314760bc48eSAndrew Thompson 	struct usb_bus *bus;
231502ac6454SAndrew Thompson 	devclass_t dc;
231602ac6454SAndrew Thompson 	device_t dev;
231702ac6454SAndrew Thompson 	int max;
231802ac6454SAndrew Thompson 
231902ac6454SAndrew Thompson 	DPRINTFN(3, "\n");
232002ac6454SAndrew Thompson 
2321a593f6b8SAndrew Thompson 	dc = usb_devclass_ptr;
232202ac6454SAndrew Thompson 	if (dc == NULL) {
232302ac6454SAndrew Thompson 		DPRINTFN(0, "no devclass\n");
232402ac6454SAndrew Thompson 		return;
232502ac6454SAndrew Thompson 	}
232602ac6454SAndrew Thompson 	/*
2327db4fcadfSConrad Meyer 	 * Explore all USB buses in parallel.
232802ac6454SAndrew Thompson 	 */
232902ac6454SAndrew Thompson 	max = devclass_get_maxunit(dc);
233002ac6454SAndrew Thompson 	while (max >= 0) {
233102ac6454SAndrew Thompson 		dev = devclass_get_device(dc, max);
233202ac6454SAndrew Thompson 		if (dev) {
233302ac6454SAndrew Thompson 			bus = device_get_softc(dev);
233402ac6454SAndrew Thompson 			if (bus) {
2335a593f6b8SAndrew Thompson 				usb_needs_explore(bus, 1);
233602ac6454SAndrew Thompson 			}
233702ac6454SAndrew Thompson 		}
233802ac6454SAndrew Thompson 		max--;
233902ac6454SAndrew Thompson 	}
234002ac6454SAndrew Thompson }
234102ac6454SAndrew Thompson 
234202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2343433ded8bSHans Petter Selasky  *	usb_needs_explore_init
2344433ded8bSHans Petter Selasky  *
2345433ded8bSHans Petter Selasky  * This function will ensure that the USB controllers are not enumerated
2346433ded8bSHans Petter Selasky  * until the "cold" variable is cleared.
2347433ded8bSHans Petter Selasky  *------------------------------------------------------------------------*/
2348433ded8bSHans Petter Selasky static void
usb_needs_explore_init(void * arg)2349433ded8bSHans Petter Selasky usb_needs_explore_init(void *arg)
2350433ded8bSHans Petter Selasky {
2351433ded8bSHans Petter Selasky 	/*
2352433ded8bSHans Petter Selasky 	 * The cold variable should be cleared prior to this function
2353433ded8bSHans Petter Selasky 	 * being called:
2354433ded8bSHans Petter Selasky 	 */
2355433ded8bSHans Petter Selasky 	if (cold == 0)
2356433ded8bSHans Petter Selasky 		usb_needs_explore_all();
2357433ded8bSHans Petter Selasky 	else
2358433ded8bSHans Petter Selasky 		DPRINTFN(-1, "Cold variable is still set!\n");
2359433ded8bSHans Petter Selasky }
2360433ded8bSHans Petter Selasky SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
2361433ded8bSHans Petter Selasky 
2362433ded8bSHans Petter Selasky /*------------------------------------------------------------------------*
2363a593f6b8SAndrew Thompson  *	usb_bus_power_update
236402ac6454SAndrew Thompson  *
236502ac6454SAndrew Thompson  * This function will ensure that all USB devices on the given bus are
236602ac6454SAndrew Thompson  * properly suspended or resumed according to the device transfer
236702ac6454SAndrew Thompson  * state.
236802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
23694eae601eSAndrew Thompson #if USB_HAVE_POWERD
237002ac6454SAndrew Thompson void
usb_bus_power_update(struct usb_bus * bus)2371a593f6b8SAndrew Thompson usb_bus_power_update(struct usb_bus *bus)
237202ac6454SAndrew Thompson {
2373a593f6b8SAndrew Thompson 	usb_needs_explore(bus, 0 /* no probe */ );
237402ac6454SAndrew Thompson }
23754eae601eSAndrew Thompson #endif
237602ac6454SAndrew Thompson 
237702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2378a593f6b8SAndrew Thompson  *	usbd_transfer_power_ref
237902ac6454SAndrew Thompson  *
238002ac6454SAndrew Thompson  * This function will modify the power save reference counts and
238102ac6454SAndrew Thompson  * wakeup the USB device associated with the given USB transfer, if
238202ac6454SAndrew Thompson  * needed.
238302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
23844eae601eSAndrew Thompson #if USB_HAVE_POWERD
238502ac6454SAndrew Thompson void
usbd_transfer_power_ref(struct usb_xfer * xfer,int val)2386a593f6b8SAndrew Thompson usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
238702ac6454SAndrew Thompson {
2388e0a69b51SAndrew Thompson 	static const usb_power_mask_t power_mask[4] = {
238902ac6454SAndrew Thompson 		[UE_CONTROL] = USB_HW_POWER_CONTROL,
239002ac6454SAndrew Thompson 		[UE_BULK] = USB_HW_POWER_BULK,
239102ac6454SAndrew Thompson 		[UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
239202ac6454SAndrew Thompson 		[UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
239302ac6454SAndrew Thompson 	};
2394760bc48eSAndrew Thompson 	struct usb_device *udev;
239502ac6454SAndrew Thompson 	uint8_t needs_explore;
239602ac6454SAndrew Thompson 	uint8_t needs_hw_power;
239702ac6454SAndrew Thompson 	uint8_t xfer_type;
239802ac6454SAndrew Thompson 
239902ac6454SAndrew Thompson 	udev = xfer->xroot->udev;
240002ac6454SAndrew Thompson 
240102ac6454SAndrew Thompson 	if (udev->device_index == USB_ROOT_HUB_ADDR) {
240202ac6454SAndrew Thompson 		/* no power save for root HUB */
240302ac6454SAndrew Thompson 		return;
240402ac6454SAndrew Thompson 	}
240502ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
240602ac6454SAndrew Thompson 
2407ae60fdfbSAndrew Thompson 	xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
240802ac6454SAndrew Thompson 
240902ac6454SAndrew Thompson 	udev->pwr_save.last_xfer_time = ticks;
241002ac6454SAndrew Thompson 	udev->pwr_save.type_refs[xfer_type] += val;
241102ac6454SAndrew Thompson 
241202ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
241302ac6454SAndrew Thompson 		udev->pwr_save.read_refs += val;
2414f29a0724SAndrew Thompson 		if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
241502ac6454SAndrew Thompson 			/*
24162df1e9a6SAndrew Thompson 			 * It is not allowed to suspend during a
24172df1e9a6SAndrew Thompson 			 * control transfer:
241802ac6454SAndrew Thompson 			 */
241902ac6454SAndrew Thompson 			udev->pwr_save.write_refs += val;
242002ac6454SAndrew Thompson 		}
242102ac6454SAndrew Thompson 	} else if (USB_GET_DATA_ISREAD(xfer)) {
242202ac6454SAndrew Thompson 		udev->pwr_save.read_refs += val;
242302ac6454SAndrew Thompson 	} else {
242402ac6454SAndrew Thompson 		udev->pwr_save.write_refs += val;
242502ac6454SAndrew Thompson 	}
242602ac6454SAndrew Thompson 
24272df1e9a6SAndrew Thompson 	if (val > 0) {
2428ec8f3127SAndrew Thompson 		if (udev->flags.self_suspended)
24292df1e9a6SAndrew Thompson 			needs_explore = usb_peer_should_wakeup(udev);
243002ac6454SAndrew Thompson 		else
243102ac6454SAndrew Thompson 			needs_explore = 0;
243202ac6454SAndrew Thompson 
243302ac6454SAndrew Thompson 		if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
243402ac6454SAndrew Thompson 			DPRINTF("Adding type %u to power state\n", xfer_type);
243502ac6454SAndrew Thompson 			udev->bus->hw_power_state |= power_mask[xfer_type];
243602ac6454SAndrew Thompson 			needs_hw_power = 1;
243702ac6454SAndrew Thompson 		} else {
243802ac6454SAndrew Thompson 			needs_hw_power = 0;
243902ac6454SAndrew Thompson 		}
24402df1e9a6SAndrew Thompson 	} else {
24412df1e9a6SAndrew Thompson 		needs_explore = 0;
24422df1e9a6SAndrew Thompson 		needs_hw_power = 0;
24432df1e9a6SAndrew Thompson 	}
244402ac6454SAndrew Thompson 
244502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
244602ac6454SAndrew Thompson 
244702ac6454SAndrew Thompson 	if (needs_explore) {
244802ac6454SAndrew Thompson 		DPRINTF("update\n");
2449a593f6b8SAndrew Thompson 		usb_bus_power_update(udev->bus);
245002ac6454SAndrew Thompson 	} else if (needs_hw_power) {
245102ac6454SAndrew Thompson 		DPRINTF("needs power\n");
245202ac6454SAndrew Thompson 		if (udev->bus->methods->set_hw_power != NULL) {
245302ac6454SAndrew Thompson 			(udev->bus->methods->set_hw_power) (udev->bus);
245402ac6454SAndrew Thompson 		}
245502ac6454SAndrew Thompson 	}
245602ac6454SAndrew Thompson }
24574eae601eSAndrew Thompson #endif
245802ac6454SAndrew Thompson 
245902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
24602df1e9a6SAndrew Thompson  *	usb_peer_should_wakeup
24612df1e9a6SAndrew Thompson  *
24622df1e9a6SAndrew Thompson  * This function returns non-zero if the current device should wake up.
24632df1e9a6SAndrew Thompson  *------------------------------------------------------------------------*/
24642df1e9a6SAndrew Thompson static uint8_t
usb_peer_should_wakeup(struct usb_device * udev)24652df1e9a6SAndrew Thompson usb_peer_should_wakeup(struct usb_device *udev)
24662df1e9a6SAndrew Thompson {
2467418b87f8SHans Petter Selasky 	return (((udev->power_mode == USB_POWER_MODE_ON) &&
2468418b87f8SHans Petter Selasky 	    (udev->flags.usb_mode == USB_MODE_HOST)) ||
24698f9750b7SHans Petter Selasky 	    (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2470d6f4a9f9SHans Petter Selasky 	    (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
24712df1e9a6SAndrew Thompson 	    (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
24722df1e9a6SAndrew Thompson 	    (udev->pwr_save.write_refs != 0) ||
24732df1e9a6SAndrew Thompson 	    ((udev->pwr_save.read_refs != 0) &&
24742df1e9a6SAndrew Thompson 	    (udev->flags.usb_mode == USB_MODE_HOST) &&
24752df1e9a6SAndrew Thompson 	    (usb_peer_can_wakeup(udev) == 0)));
24762df1e9a6SAndrew Thompson }
24772df1e9a6SAndrew Thompson 
24782df1e9a6SAndrew Thompson /*------------------------------------------------------------------------*
2479a593f6b8SAndrew Thompson  *	usb_bus_powerd
248002ac6454SAndrew Thompson  *
248102ac6454SAndrew Thompson  * This function implements the USB power daemon and is called
248202ac6454SAndrew Thompson  * regularly from the USB explore thread.
248302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
24844eae601eSAndrew Thompson #if USB_HAVE_POWERD
248502ac6454SAndrew Thompson void
usb_bus_powerd(struct usb_bus * bus)2486a593f6b8SAndrew Thompson usb_bus_powerd(struct usb_bus *bus)
248702ac6454SAndrew Thompson {
2488760bc48eSAndrew Thompson 	struct usb_device *udev;
2489e0a69b51SAndrew Thompson 	usb_ticks_t temp;
2490e0a69b51SAndrew Thompson 	usb_ticks_t limit;
2491e0a69b51SAndrew Thompson 	usb_ticks_t mintime;
2492f9cb546cSAndrew Thompson 	usb_size_t type_refs[5];
249302ac6454SAndrew Thompson 	uint8_t x;
249402ac6454SAndrew Thompson 
2495a593f6b8SAndrew Thompson 	limit = usb_power_timeout;
249602ac6454SAndrew Thompson 	if (limit == 0)
249702ac6454SAndrew Thompson 		limit = hz;
249802ac6454SAndrew Thompson 	else if (limit > 255)
249902ac6454SAndrew Thompson 		limit = 255 * hz;
250002ac6454SAndrew Thompson 	else
250102ac6454SAndrew Thompson 		limit = limit * hz;
250202ac6454SAndrew Thompson 
250302ac6454SAndrew Thompson 	DPRINTF("bus=%p\n", bus);
250402ac6454SAndrew Thompson 
250502ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
250602ac6454SAndrew Thompson 
250702ac6454SAndrew Thompson 	/*
250802ac6454SAndrew Thompson 	 * The root HUB device is never suspended
250902ac6454SAndrew Thompson 	 * and we simply skip it.
251002ac6454SAndrew Thompson 	 */
251102ac6454SAndrew Thompson 	for (x = USB_ROOT_HUB_ADDR + 1;
251202ac6454SAndrew Thompson 	    x != bus->devices_max; x++) {
251302ac6454SAndrew Thompson 		udev = bus->devices[x];
251402ac6454SAndrew Thompson 		if (udev == NULL)
251502ac6454SAndrew Thompson 			continue;
251602ac6454SAndrew Thompson 
251702ac6454SAndrew Thompson 		temp = ticks - udev->pwr_save.last_xfer_time;
251802ac6454SAndrew Thompson 
25192df1e9a6SAndrew Thompson 		if (usb_peer_should_wakeup(udev)) {
252002ac6454SAndrew Thompson 			/* check if we are suspended */
2521ec8f3127SAndrew Thompson 			if (udev->flags.self_suspended != 0) {
252202ac6454SAndrew Thompson 				USB_BUS_UNLOCK(bus);
2523a593f6b8SAndrew Thompson 				usb_dev_resume_peer(udev);
252402ac6454SAndrew Thompson 				USB_BUS_LOCK(bus);
252502ac6454SAndrew Thompson 			}
25262df1e9a6SAndrew Thompson 		} else if ((temp >= limit) &&
25272df1e9a6SAndrew Thompson 		    (udev->flags.usb_mode == USB_MODE_HOST) &&
25282df1e9a6SAndrew Thompson 		    (udev->flags.self_suspended == 0)) {
25292df1e9a6SAndrew Thompson 			/* try to do suspend */
253002ac6454SAndrew Thompson 
253102ac6454SAndrew Thompson 			USB_BUS_UNLOCK(bus);
2532a593f6b8SAndrew Thompson 			usb_dev_suspend_peer(udev);
253302ac6454SAndrew Thompson 			USB_BUS_LOCK(bus);
253402ac6454SAndrew Thompson 		}
253502ac6454SAndrew Thompson 	}
253602ac6454SAndrew Thompson 
253702ac6454SAndrew Thompson 	/* reset counters */
253802ac6454SAndrew Thompson 
25396d917491SHans Petter Selasky 	mintime = (usb_ticks_t)-1;
254002ac6454SAndrew Thompson 	type_refs[0] = 0;
254102ac6454SAndrew Thompson 	type_refs[1] = 0;
254202ac6454SAndrew Thompson 	type_refs[2] = 0;
254302ac6454SAndrew Thompson 	type_refs[3] = 0;
254402ac6454SAndrew Thompson 	type_refs[4] = 0;
254502ac6454SAndrew Thompson 
254602ac6454SAndrew Thompson 	/* Re-loop all the devices to get the actual state */
254702ac6454SAndrew Thompson 
254802ac6454SAndrew Thompson 	for (x = USB_ROOT_HUB_ADDR + 1;
254902ac6454SAndrew Thompson 	    x != bus->devices_max; x++) {
255002ac6454SAndrew Thompson 		udev = bus->devices[x];
255102ac6454SAndrew Thompson 		if (udev == NULL)
255202ac6454SAndrew Thompson 			continue;
255302ac6454SAndrew Thompson 
255402ac6454SAndrew Thompson 		/* we found a non-Root-Hub USB device */
255502ac6454SAndrew Thompson 		type_refs[4] += 1;
255602ac6454SAndrew Thompson 
255702ac6454SAndrew Thompson 		/* "last_xfer_time" can be updated by a resume */
255802ac6454SAndrew Thompson 		temp = ticks - udev->pwr_save.last_xfer_time;
255902ac6454SAndrew Thompson 
256002ac6454SAndrew Thompson 		/*
256102ac6454SAndrew Thompson 		 * Compute minimum time since last transfer for the complete
256202ac6454SAndrew Thompson 		 * bus:
256302ac6454SAndrew Thompson 		 */
256402ac6454SAndrew Thompson 		if (temp < mintime)
256502ac6454SAndrew Thompson 			mintime = temp;
256602ac6454SAndrew Thompson 
2567ec8f3127SAndrew Thompson 		if (udev->flags.self_suspended == 0) {
256802ac6454SAndrew Thompson 			type_refs[0] += udev->pwr_save.type_refs[0];
256902ac6454SAndrew Thompson 			type_refs[1] += udev->pwr_save.type_refs[1];
257002ac6454SAndrew Thompson 			type_refs[2] += udev->pwr_save.type_refs[2];
257102ac6454SAndrew Thompson 			type_refs[3] += udev->pwr_save.type_refs[3];
257202ac6454SAndrew Thompson 		}
257302ac6454SAndrew Thompson 	}
257402ac6454SAndrew Thompson 
25756d917491SHans Petter Selasky 	if (mintime >= (usb_ticks_t)(1 * hz)) {
257602ac6454SAndrew Thompson 		/* recompute power masks */
257702ac6454SAndrew Thompson 		DPRINTF("Recomputing power masks\n");
257802ac6454SAndrew Thompson 		bus->hw_power_state = 0;
257902ac6454SAndrew Thompson 		if (type_refs[UE_CONTROL] != 0)
258002ac6454SAndrew Thompson 			bus->hw_power_state |= USB_HW_POWER_CONTROL;
258102ac6454SAndrew Thompson 		if (type_refs[UE_BULK] != 0)
258202ac6454SAndrew Thompson 			bus->hw_power_state |= USB_HW_POWER_BULK;
258302ac6454SAndrew Thompson 		if (type_refs[UE_INTERRUPT] != 0)
258402ac6454SAndrew Thompson 			bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
258502ac6454SAndrew Thompson 		if (type_refs[UE_ISOCHRONOUS] != 0)
258602ac6454SAndrew Thompson 			bus->hw_power_state |= USB_HW_POWER_ISOC;
258702ac6454SAndrew Thompson 		if (type_refs[4] != 0)
258802ac6454SAndrew Thompson 			bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
258902ac6454SAndrew Thompson 	}
259002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
259102ac6454SAndrew Thompson 
259202ac6454SAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
259302ac6454SAndrew Thompson 		/* always update hardware power! */
259402ac6454SAndrew Thompson 		(bus->methods->set_hw_power) (bus);
259502ac6454SAndrew Thompson 	}
259602ac6454SAndrew Thompson 	return;
259702ac6454SAndrew Thompson }
25984eae601eSAndrew Thompson #endif
259902ac6454SAndrew Thompson 
2600d76ca5b1SHans Petter Selasky static usb_error_t
usbd_device_30_remote_wakeup(struct usb_device * udev,uint8_t bRequest)2601d76ca5b1SHans Petter Selasky usbd_device_30_remote_wakeup(struct usb_device *udev, uint8_t bRequest)
2602d76ca5b1SHans Petter Selasky {
2603d76ca5b1SHans Petter Selasky 	struct usb_device_request req = {};
2604d76ca5b1SHans Petter Selasky 
2605d76ca5b1SHans Petter Selasky 	req.bmRequestType = UT_WRITE_INTERFACE;
2606d76ca5b1SHans Petter Selasky 	req.bRequest = bRequest;
2607d76ca5b1SHans Petter Selasky 	USETW(req.wValue, USB_INTERFACE_FUNC_SUSPEND);
2608d76ca5b1SHans Petter Selasky 	USETW(req.wIndex, USB_INTERFACE_FUNC_SUSPEND_LP |
2609d76ca5b1SHans Petter Selasky 	    USB_INTERFACE_FUNC_SUSPEND_RW);
2610d76ca5b1SHans Petter Selasky 
2611d76ca5b1SHans Petter Selasky 	return (usbd_do_request(udev, NULL, &req, 0));
2612d76ca5b1SHans Petter Selasky }
2613d76ca5b1SHans Petter Selasky 
2614d76ca5b1SHans Petter Selasky static usb_error_t
usbd_clear_dev_wakeup(struct usb_device * udev)2615d76ca5b1SHans Petter Selasky usbd_clear_dev_wakeup(struct usb_device *udev)
2616d76ca5b1SHans Petter Selasky {
2617d76ca5b1SHans Petter Selasky 	usb_error_t err;
2618d76ca5b1SHans Petter Selasky 
2619d76ca5b1SHans Petter Selasky 	if (usb_device_20_compatible(udev)) {
2620d76ca5b1SHans Petter Selasky 		err = usbd_req_clear_device_feature(udev,
2621d76ca5b1SHans Petter Selasky 		    NULL, UF_DEVICE_REMOTE_WAKEUP);
2622d76ca5b1SHans Petter Selasky 	} else {
2623d76ca5b1SHans Petter Selasky 		err = usbd_device_30_remote_wakeup(udev,
2624d76ca5b1SHans Petter Selasky 		    UR_CLEAR_FEATURE);
2625d76ca5b1SHans Petter Selasky 	}
2626d76ca5b1SHans Petter Selasky 	return (err);
2627d76ca5b1SHans Petter Selasky }
2628d76ca5b1SHans Petter Selasky 
2629d76ca5b1SHans Petter Selasky static usb_error_t
usbd_set_dev_wakeup(struct usb_device * udev)2630d76ca5b1SHans Petter Selasky usbd_set_dev_wakeup(struct usb_device *udev)
2631d76ca5b1SHans Petter Selasky {
2632d76ca5b1SHans Petter Selasky 	usb_error_t err;
2633d76ca5b1SHans Petter Selasky 
2634d76ca5b1SHans Petter Selasky 	if (usb_device_20_compatible(udev)) {
2635d76ca5b1SHans Petter Selasky 		err = usbd_req_set_device_feature(udev,
2636d76ca5b1SHans Petter Selasky 		    NULL, UF_DEVICE_REMOTE_WAKEUP);
2637d76ca5b1SHans Petter Selasky 	} else {
2638d76ca5b1SHans Petter Selasky 		err = usbd_device_30_remote_wakeup(udev,
2639d76ca5b1SHans Petter Selasky 		    UR_SET_FEATURE);
2640d76ca5b1SHans Petter Selasky 	}
2641d76ca5b1SHans Petter Selasky 	return (err);
2642d76ca5b1SHans Petter Selasky }
2643d76ca5b1SHans Petter Selasky 
264402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2645a593f6b8SAndrew Thompson  *	usb_dev_resume_peer
264602ac6454SAndrew Thompson  *
264702ac6454SAndrew Thompson  * This function will resume an USB peer and do the required USB
264802ac6454SAndrew Thompson  * signalling to get an USB device out of the suspended state.
264902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
265002ac6454SAndrew Thompson static void
usb_dev_resume_peer(struct usb_device * udev)2651a593f6b8SAndrew Thompson usb_dev_resume_peer(struct usb_device *udev)
265202ac6454SAndrew Thompson {
2653760bc48eSAndrew Thompson 	struct usb_bus *bus;
265402ac6454SAndrew Thompson 	int err;
265502ac6454SAndrew Thompson 
265602ac6454SAndrew Thompson 	/* be NULL safe */
265702ac6454SAndrew Thompson 	if (udev == NULL)
265802ac6454SAndrew Thompson 		return;
265902ac6454SAndrew Thompson 
266002ac6454SAndrew Thompson 	/* check if already resumed */
2661ec8f3127SAndrew Thompson 	if (udev->flags.self_suspended == 0)
266202ac6454SAndrew Thompson 		return;
266302ac6454SAndrew Thompson 
266402ac6454SAndrew Thompson 	/* we need a parent HUB to do resume */
266502ac6454SAndrew Thompson 	if (udev->parent_hub == NULL)
266602ac6454SAndrew Thompson 		return;
266702ac6454SAndrew Thompson 
266802ac6454SAndrew Thompson 	DPRINTF("udev=%p\n", udev);
266902ac6454SAndrew Thompson 
2670f29a0724SAndrew Thompson 	if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
267102ac6454SAndrew Thompson 	    (udev->flags.remote_wakeup == 0)) {
267202ac6454SAndrew Thompson 		/*
267302ac6454SAndrew Thompson 		 * If the host did not set the remote wakeup feature, we can
267402ac6454SAndrew Thompson 		 * not wake it up either!
267502ac6454SAndrew Thompson 		 */
267602ac6454SAndrew Thompson 		DPRINTF("remote wakeup is not set!\n");
267702ac6454SAndrew Thompson 		return;
267802ac6454SAndrew Thompson 	}
267902ac6454SAndrew Thompson 	/* get bus pointer */
268002ac6454SAndrew Thompson 	bus = udev->bus;
268102ac6454SAndrew Thompson 
268202ac6454SAndrew Thompson 	/* resume parent hub first */
2683a593f6b8SAndrew Thompson 	usb_dev_resume_peer(udev->parent_hub);
268402ac6454SAndrew Thompson 
26852df1e9a6SAndrew Thompson 	/* reduce chance of instant resume failure by waiting a little bit */
26862df1e9a6SAndrew Thompson 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
26872df1e9a6SAndrew Thompson 
2688963169b4SHans Petter Selasky 	if (usb_device_20_compatible(udev)) {
268902ac6454SAndrew Thompson 		/* resume current port (Valid in Host and Device Mode) */
2690a593f6b8SAndrew Thompson 		err = usbd_req_clear_port_feature(udev->parent_hub,
2691e2805033SAndrew Thompson 		    NULL, udev->port_no, UHF_PORT_SUSPEND);
26924131f6fbSHans Petter Selasky 	} else {
26934131f6fbSHans Petter Selasky 		/* resume current port (Valid in Host and Device Mode) */
26944131f6fbSHans Petter Selasky 		err = usbd_req_set_port_link_state(udev->parent_hub,
26954131f6fbSHans Petter Selasky 		    NULL, udev->port_no, UPS_PORT_LS_U0);
26964131f6fbSHans Petter Selasky 	}
2697a88e1a04SHans Petter Selasky 
2698a88e1a04SHans Petter Selasky 	if (err != 0) {
2699a88e1a04SHans Petter Selasky 		DPRINTFN(0, "Resuming port failed: %s (ignored)\n",
2700a88e1a04SHans Petter Selasky 		    usbd_errstr(err));
2701963169b4SHans Petter Selasky 	}
2702963169b4SHans Petter Selasky 
270302ac6454SAndrew Thompson 	/* resume settle time */
270437506412SHans Petter Selasky 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
270502ac6454SAndrew Thompson 
270602ac6454SAndrew Thompson 	if (bus->methods->device_resume != NULL) {
270702ac6454SAndrew Thompson 		/* resume USB device on the USB controller */
270802ac6454SAndrew Thompson 		(bus->methods->device_resume) (udev);
270902ac6454SAndrew Thompson 	}
271002ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
271102ac6454SAndrew Thompson 	/* set that this device is now resumed */
2712ec8f3127SAndrew Thompson 	udev->flags.self_suspended = 0;
27134eae601eSAndrew Thompson #if USB_HAVE_POWERD
271402ac6454SAndrew Thompson 	/* make sure that we don't go into suspend right away */
271502ac6454SAndrew Thompson 	udev->pwr_save.last_xfer_time = ticks;
271602ac6454SAndrew Thompson 
271702ac6454SAndrew Thompson 	/* make sure the needed power masks are on */
271802ac6454SAndrew Thompson 	if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
271902ac6454SAndrew Thompson 		bus->hw_power_state |= USB_HW_POWER_CONTROL;
272002ac6454SAndrew Thompson 	if (udev->pwr_save.type_refs[UE_BULK] != 0)
272102ac6454SAndrew Thompson 		bus->hw_power_state |= USB_HW_POWER_BULK;
272202ac6454SAndrew Thompson 	if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
272302ac6454SAndrew Thompson 		bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
272402ac6454SAndrew Thompson 	if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
272502ac6454SAndrew Thompson 		bus->hw_power_state |= USB_HW_POWER_ISOC;
27264eae601eSAndrew Thompson #endif
272702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
272802ac6454SAndrew Thompson 
272902ac6454SAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
273002ac6454SAndrew Thompson 		/* always update hardware power! */
273102ac6454SAndrew Thompson 		(bus->methods->set_hw_power) (bus);
273202ac6454SAndrew Thompson 	}
2733cb18f7d1SAlfred Perlstein 
27342df1e9a6SAndrew Thompson 	usbd_sr_lock(udev);
2735cb18f7d1SAlfred Perlstein 
273602ac6454SAndrew Thompson 	/* notify all sub-devices about resume */
2737a593f6b8SAndrew Thompson 	err = usb_suspend_resume(udev, 0);
2738cb18f7d1SAlfred Perlstein 
27392df1e9a6SAndrew Thompson 	usbd_sr_unlock(udev);
274002ac6454SAndrew Thompson 
274102ac6454SAndrew Thompson 	/* check if peer has wakeup capability */
27424131f6fbSHans Petter Selasky 	if (usb_peer_can_wakeup(udev)) {
274302ac6454SAndrew Thompson 		/* clear remote wakeup */
2744d76ca5b1SHans Petter Selasky 		err = usbd_clear_dev_wakeup(udev);
274502ac6454SAndrew Thompson 		if (err) {
274602ac6454SAndrew Thompson 			DPRINTFN(0, "Clearing device "
2747767cb2e2SAndrew Thompson 			    "remote wakeup failed: %s\n",
2748a593f6b8SAndrew Thompson 			    usbd_errstr(err));
274902ac6454SAndrew Thompson 		}
275002ac6454SAndrew Thompson 	}
275102ac6454SAndrew Thompson }
275202ac6454SAndrew Thompson 
275302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2754a593f6b8SAndrew Thompson  *	usb_dev_suspend_peer
275502ac6454SAndrew Thompson  *
275602ac6454SAndrew Thompson  * This function will suspend an USB peer and do the required USB
275702ac6454SAndrew Thompson  * signalling to get an USB device into the suspended state.
275802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
275902ac6454SAndrew Thompson static void
usb_dev_suspend_peer(struct usb_device * udev)2760a593f6b8SAndrew Thompson usb_dev_suspend_peer(struct usb_device *udev)
276102ac6454SAndrew Thompson {
2762760bc48eSAndrew Thompson 	struct usb_device *child;
276302ac6454SAndrew Thompson 	int err;
276402ac6454SAndrew Thompson 	uint8_t x;
276502ac6454SAndrew Thompson 	uint8_t nports;
276602ac6454SAndrew Thompson 
276702ac6454SAndrew Thompson repeat:
276802ac6454SAndrew Thompson 	/* be NULL safe */
276902ac6454SAndrew Thompson 	if (udev == NULL)
277002ac6454SAndrew Thompson 		return;
277102ac6454SAndrew Thompson 
277202ac6454SAndrew Thompson 	/* check if already suspended */
2773ec8f3127SAndrew Thompson 	if (udev->flags.self_suspended)
277402ac6454SAndrew Thompson 		return;
277502ac6454SAndrew Thompson 
277602ac6454SAndrew Thompson 	/* we need a parent HUB to do suspend */
277702ac6454SAndrew Thompson 	if (udev->parent_hub == NULL)
277802ac6454SAndrew Thompson 		return;
277902ac6454SAndrew Thompson 
278002ac6454SAndrew Thompson 	DPRINTF("udev=%p\n", udev);
278102ac6454SAndrew Thompson 
27828ac08646SAndrew Thompson 	/* check if the current device is a HUB */
27838ac08646SAndrew Thompson 	if (udev->hub != NULL) {
27848ac08646SAndrew Thompson 		nports = udev->hub->nports;
278502ac6454SAndrew Thompson 
27868ac08646SAndrew Thompson 		/* check if all devices on the HUB are suspended */
278702ac6454SAndrew Thompson 		for (x = 0; x != nports; x++) {
2788a593f6b8SAndrew Thompson 			child = usb_bus_port_get_device(udev->bus,
27898ac08646SAndrew Thompson 			    udev->hub->ports + x);
279002ac6454SAndrew Thompson 
279102ac6454SAndrew Thompson 			if (child == NULL)
279202ac6454SAndrew Thompson 				continue;
279302ac6454SAndrew Thompson 
2794ec8f3127SAndrew Thompson 			if (child->flags.self_suspended)
279502ac6454SAndrew Thompson 				continue;
279602ac6454SAndrew Thompson 
27978ac08646SAndrew Thompson 			DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
27988ac08646SAndrew Thompson 			return;
279902ac6454SAndrew Thompson 		}
280002ac6454SAndrew Thompson 	}
280102ac6454SAndrew Thompson 
28024131f6fbSHans Petter Selasky 	if (usb_peer_can_wakeup(udev)) {
2803963169b4SHans Petter Selasky 		/*
2804963169b4SHans Petter Selasky 		 * This request needs to be done before we set
2805963169b4SHans Petter Selasky 		 * "udev->flags.self_suspended":
2806963169b4SHans Petter Selasky 		 */
2807963169b4SHans Petter Selasky 
2808963169b4SHans Petter Selasky 		/* allow device to do remote wakeup */
2809d76ca5b1SHans Petter Selasky 		err = usbd_set_dev_wakeup(udev);
2810963169b4SHans Petter Selasky 		if (err) {
2811963169b4SHans Petter Selasky 			DPRINTFN(0, "Setting device "
2812963169b4SHans Petter Selasky 			    "remote wakeup failed\n");
2813963169b4SHans Petter Selasky 		}
2814963169b4SHans Petter Selasky 	}
2815963169b4SHans Petter Selasky 
28162df1e9a6SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
28172df1e9a6SAndrew Thompson 	/*
28182df1e9a6SAndrew Thompson 	 * Checking for suspend condition and setting suspended bit
28192df1e9a6SAndrew Thompson 	 * must be atomic!
28202df1e9a6SAndrew Thompson 	 */
28212df1e9a6SAndrew Thompson 	err = usb_peer_should_wakeup(udev);
28222df1e9a6SAndrew Thompson 	if (err == 0) {
28232df1e9a6SAndrew Thompson 		/*
28242df1e9a6SAndrew Thompson 		 * Set that this device is suspended. This variable
28252df1e9a6SAndrew Thompson 		 * must be set before calling USB controller suspend
28262df1e9a6SAndrew Thompson 		 * callbacks.
28272df1e9a6SAndrew Thompson 		 */
28282df1e9a6SAndrew Thompson 		udev->flags.self_suspended = 1;
28292df1e9a6SAndrew Thompson 	}
28302df1e9a6SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
28312df1e9a6SAndrew Thompson 
28322df1e9a6SAndrew Thompson 	if (err != 0) {
28334131f6fbSHans Petter Selasky 		if (usb_peer_can_wakeup(udev)) {
2834963169b4SHans Petter Selasky 			/* allow device to do remote wakeup */
2835d76ca5b1SHans Petter Selasky 			err = usbd_clear_dev_wakeup(udev);
2836963169b4SHans Petter Selasky 			if (err) {
2837963169b4SHans Petter Selasky 				DPRINTFN(0, "Setting device "
2838963169b4SHans Petter Selasky 				    "remote wakeup failed\n");
2839963169b4SHans Petter Selasky 			}
2840963169b4SHans Petter Selasky 		}
2841963169b4SHans Petter Selasky 
28422df1e9a6SAndrew Thompson 		if (udev->flags.usb_mode == USB_MODE_DEVICE) {
28432df1e9a6SAndrew Thompson 			/* resume parent HUB first */
28442df1e9a6SAndrew Thompson 			usb_dev_resume_peer(udev->parent_hub);
28452df1e9a6SAndrew Thompson 
28462df1e9a6SAndrew Thompson 			/* reduce chance of instant resume failure by waiting a little bit */
28472df1e9a6SAndrew Thompson 			usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
28482df1e9a6SAndrew Thompson 
28492df1e9a6SAndrew Thompson 			/* resume current port (Valid in Host and Device Mode) */
28502df1e9a6SAndrew Thompson 			err = usbd_req_clear_port_feature(udev->parent_hub,
28512df1e9a6SAndrew Thompson 			    NULL, udev->port_no, UHF_PORT_SUSPEND);
28522df1e9a6SAndrew Thompson 
28532df1e9a6SAndrew Thompson 			/* resume settle time */
285437506412SHans Petter Selasky 			usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
28552df1e9a6SAndrew Thompson 		}
28562df1e9a6SAndrew Thompson 		DPRINTF("Suspend was cancelled!\n");
28572df1e9a6SAndrew Thompson 		return;
28582df1e9a6SAndrew Thompson 	}
28592df1e9a6SAndrew Thompson 
28602df1e9a6SAndrew Thompson 	usbd_sr_lock(udev);
2861cb18f7d1SAlfred Perlstein 
286202ac6454SAndrew Thompson 	/* notify all sub-devices about suspend */
2863a593f6b8SAndrew Thompson 	err = usb_suspend_resume(udev, 1);
2864cb18f7d1SAlfred Perlstein 
28652df1e9a6SAndrew Thompson 	usbd_sr_unlock(udev);
286602ac6454SAndrew Thompson 
286702ac6454SAndrew Thompson 	if (udev->bus->methods->device_suspend != NULL) {
2868e0a69b51SAndrew Thompson 		usb_timeout_t temp;
286902ac6454SAndrew Thompson 
287002ac6454SAndrew Thompson 		/* suspend device on the USB controller */
287102ac6454SAndrew Thompson 		(udev->bus->methods->device_suspend) (udev);
287202ac6454SAndrew Thompson 
287302ac6454SAndrew Thompson 		/* do DMA delay */
2874527aa7b2SAndrew Thompson 		temp = usbd_get_dma_delay(udev);
2875527aa7b2SAndrew Thompson 		if (temp != 0)
2876a593f6b8SAndrew Thompson 			usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
287702ac6454SAndrew Thompson 	}
2878963169b4SHans Petter Selasky 
2879963169b4SHans Petter Selasky 	if (usb_device_20_compatible(udev)) {
288002ac6454SAndrew Thompson 		/* suspend current port */
2881a593f6b8SAndrew Thompson 		err = usbd_req_set_port_feature(udev->parent_hub,
2882e2805033SAndrew Thompson 		    NULL, udev->port_no, UHF_PORT_SUSPEND);
288302ac6454SAndrew Thompson 		if (err) {
288402ac6454SAndrew Thompson 			DPRINTFN(0, "Suspending port failed\n");
288502ac6454SAndrew Thompson 			return;
288602ac6454SAndrew Thompson 		}
28874131f6fbSHans Petter Selasky 	} else {
28884131f6fbSHans Petter Selasky 		/* suspend current port */
28894131f6fbSHans Petter Selasky 		err = usbd_req_set_port_link_state(udev->parent_hub,
28904131f6fbSHans Petter Selasky 		    NULL, udev->port_no, UPS_PORT_LS_U3);
28914131f6fbSHans Petter Selasky 		if (err) {
28924131f6fbSHans Petter Selasky 			DPRINTFN(0, "Suspending port failed\n");
28934131f6fbSHans Petter Selasky 			return;
28944131f6fbSHans Petter Selasky 		}
2895963169b4SHans Petter Selasky 	}
28968ac08646SAndrew Thompson 
289702ac6454SAndrew Thompson 	udev = udev->parent_hub;
289802ac6454SAndrew Thompson 	goto repeat;
289902ac6454SAndrew Thompson }
290002ac6454SAndrew Thompson 
290102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2902a593f6b8SAndrew Thompson  *	usbd_set_power_mode
290302ac6454SAndrew Thompson  *
290402ac6454SAndrew Thompson  * This function will set the power mode, see USB_POWER_MODE_XXX for a
290502ac6454SAndrew Thompson  * USB device.
290602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
290702ac6454SAndrew Thompson void
usbd_set_power_mode(struct usb_device * udev,uint8_t power_mode)2908a593f6b8SAndrew Thompson usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
290902ac6454SAndrew Thompson {
291002ac6454SAndrew Thompson 	/* filter input argument */
291102ac6454SAndrew Thompson 	if ((power_mode != USB_POWER_MODE_ON) &&
2912d2d71ce7SAndrew Thompson 	    (power_mode != USB_POWER_MODE_OFF))
291302ac6454SAndrew Thompson 		power_mode = USB_POWER_MODE_SAVE;
2914d2d71ce7SAndrew Thompson 
2915d2d71ce7SAndrew Thompson 	power_mode = usbd_filter_power_mode(udev, power_mode);
2916d2d71ce7SAndrew Thompson 
291702ac6454SAndrew Thompson 	udev->power_mode = power_mode;	/* update copy of power mode */
291802ac6454SAndrew Thompson 
29194eae601eSAndrew Thompson #if USB_HAVE_POWERD
2920a593f6b8SAndrew Thompson 	usb_bus_power_update(udev->bus);
2921d6f4a9f9SHans Petter Selasky #else
2922d6f4a9f9SHans Petter Selasky 	usb_needs_explore(udev->bus, 0 /* no probe */ );
29234eae601eSAndrew Thompson #endif
292402ac6454SAndrew Thompson }
2925d2d71ce7SAndrew Thompson 
2926d2d71ce7SAndrew Thompson /*------------------------------------------------------------------------*
2927d2d71ce7SAndrew Thompson  *	usbd_filter_power_mode
2928d2d71ce7SAndrew Thompson  *
2929d2d71ce7SAndrew Thompson  * This function filters the power mode based on hardware requirements.
2930d2d71ce7SAndrew Thompson  *------------------------------------------------------------------------*/
2931d2d71ce7SAndrew Thompson uint8_t
usbd_filter_power_mode(struct usb_device * udev,uint8_t power_mode)2932d2d71ce7SAndrew Thompson usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2933d2d71ce7SAndrew Thompson {
2934e892b3feSHans Petter Selasky 	const struct usb_bus_methods *mtod;
2935d2d71ce7SAndrew Thompson 	int8_t temp;
2936d2d71ce7SAndrew Thompson 
2937d2d71ce7SAndrew Thompson 	mtod = udev->bus->methods;
2938d2d71ce7SAndrew Thompson 	temp = -1;
2939d2d71ce7SAndrew Thompson 
2940d2d71ce7SAndrew Thompson 	if (mtod->get_power_mode != NULL)
2941d2d71ce7SAndrew Thompson 		(mtod->get_power_mode) (udev, &temp);
2942d2d71ce7SAndrew Thompson 
2943d2d71ce7SAndrew Thompson 	/* check if we should not filter */
2944d2d71ce7SAndrew Thompson 	if (temp < 0)
2945d2d71ce7SAndrew Thompson 		return (power_mode);
2946d2d71ce7SAndrew Thompson 
2947d2d71ce7SAndrew Thompson 	/* use fixed power mode given by hardware driver */
2948d2d71ce7SAndrew Thompson 	return (temp);
2949d2d71ce7SAndrew Thompson }
29509eb0d702SHans Petter Selasky 
29519eb0d702SHans Petter Selasky /*------------------------------------------------------------------------*
29529eb0d702SHans Petter Selasky  *	usbd_start_re_enumerate
29539eb0d702SHans Petter Selasky  *
29549eb0d702SHans Petter Selasky  * This function starts re-enumeration of the given USB device. This
29559eb0d702SHans Petter Selasky  * function does not need to be called BUS-locked. This function does
29569eb0d702SHans Petter Selasky  * not wait until the re-enumeration is completed.
29579eb0d702SHans Petter Selasky  *------------------------------------------------------------------------*/
29589eb0d702SHans Petter Selasky void
usbd_start_re_enumerate(struct usb_device * udev)29599eb0d702SHans Petter Selasky usbd_start_re_enumerate(struct usb_device *udev)
29609eb0d702SHans Petter Selasky {
2961d6f4a9f9SHans Petter Selasky 	if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2962d6f4a9f9SHans Petter Selasky 		udev->re_enumerate_wait = USB_RE_ENUM_START;
29639eb0d702SHans Petter Selasky 		usb_needs_explore(udev->bus, 0);
29649eb0d702SHans Petter Selasky 	}
29659eb0d702SHans Petter Selasky }
2966d64e9217SHans Petter Selasky 
2967d64e9217SHans Petter Selasky /*-----------------------------------------------------------------------*
2968d64e9217SHans Petter Selasky  *	usbd_start_set_config
2969d64e9217SHans Petter Selasky  *
2970d64e9217SHans Petter Selasky  * This function starts setting a USB configuration. This function
2971d64e9217SHans Petter Selasky  * does not need to be called BUS-locked. This function does not wait
2972d64e9217SHans Petter Selasky  * until the set USB configuratino is completed.
2973d64e9217SHans Petter Selasky  *------------------------------------------------------------------------*/
2974d64e9217SHans Petter Selasky usb_error_t
usbd_start_set_config(struct usb_device * udev,uint8_t index)2975d64e9217SHans Petter Selasky usbd_start_set_config(struct usb_device *udev, uint8_t index)
2976d64e9217SHans Petter Selasky {
2977d64e9217SHans Petter Selasky 	if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2978d64e9217SHans Petter Selasky 		if (udev->curr_config_index == index) {
2979d64e9217SHans Petter Selasky 			/* no change needed */
2980d64e9217SHans Petter Selasky 			return (0);
2981d64e9217SHans Petter Selasky 		}
2982d64e9217SHans Petter Selasky 		udev->next_config_index = index;
2983d64e9217SHans Petter Selasky 		udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2984d64e9217SHans Petter Selasky 		usb_needs_explore(udev->bus, 0);
2985d64e9217SHans Petter Selasky 		return (0);
2986d64e9217SHans Petter Selasky 	} else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2987d64e9217SHans Petter Selasky 		if (udev->next_config_index == index) {
2988d64e9217SHans Petter Selasky 			/* no change needed */
2989d64e9217SHans Petter Selasky 			return (0);
2990d64e9217SHans Petter Selasky 		}
2991d64e9217SHans Petter Selasky 	}
2992d64e9217SHans Petter Selasky 	return (USB_ERR_PENDING_REQUESTS);
2993d64e9217SHans Petter Selasky }
2994