xref: /freebsd/sys/dev/usb/usb_hub.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5  * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
31  */
32 
33 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
35 #else
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <sys/module.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/sx.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 
59 #define	USB_DEBUG_VAR uhub_debug
60 
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_hub.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_dynamic.h>
71 
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif			/* USB_GLOBAL_INCLUDE_FILE */
75 
76 #define	UHUB_INTR_INTERVAL 250		/* ms */
77 enum {
78 	UHUB_INTR_TRANSFER,
79 #if USB_HAVE_TT_SUPPORT
80 	UHUB_RESET_TT_TRANSFER,
81 #endif
82 	UHUB_N_TRANSFER,
83 };
84 
85 #ifdef USB_DEBUG
86 static int uhub_debug = 0;
87 
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RWTUN, &uhub_debug, 0,
90     "Debug level");
91 #endif
92 
93 #if USB_HAVE_POWERD
94 static int usb_power_timeout = 30;	/* seconds */
95 
96 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RWTUN,
97     &usb_power_timeout, 0, "USB power timeout");
98 #endif
99 
100 #if USB_HAVE_DISABLE_ENUM
101 static int usb_disable_enumeration = 0;
102 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
103     &usb_disable_enumeration, 0, "Set to disable all USB device enumeration.");
104 
105 static int usb_disable_port_power = 0;
106 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
107     &usb_disable_port_power, 0, "Set to disable all USB port power.");
108 #endif
109 
110 struct uhub_current_state {
111 	uint16_t port_change;
112 	uint16_t port_status;
113 };
114 
115 struct uhub_softc {
116 	struct uhub_current_state sc_st;/* current state */
117 #if (USB_HAVE_FIXED_PORT != 0)
118 	struct usb_hub sc_hub;
119 #endif
120 	device_t sc_dev;		/* base device */
121 	struct mtx sc_mtx;		/* our mutex */
122 	struct usb_device *sc_udev;	/* USB device */
123 	struct usb_xfer *sc_xfer[UHUB_N_TRANSFER];	/* interrupt xfer */
124 #if USB_HAVE_DISABLE_ENUM
125 	int sc_disable_enumeration;
126 	int sc_disable_port_power;
127 #endif
128 	uint8_t	sc_flags;
129 #define	UHUB_FLAG_DID_EXPLORE 0x01
130 };
131 
132 #define	UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
133 #define	UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
134 #define	UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
135 #define	UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
136 #define	UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
137 
138 /* prototypes for type checking: */
139 
140 static device_probe_t uhub_probe;
141 static device_attach_t uhub_attach;
142 static device_detach_t uhub_detach;
143 static device_suspend_t uhub_suspend;
144 static device_resume_t uhub_resume;
145 
146 static bus_driver_added_t uhub_driver_added;
147 static bus_child_location_str_t uhub_child_location_string;
148 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
149 
150 static usb_callback_t uhub_intr_callback;
151 #if USB_HAVE_TT_SUPPORT
152 static usb_callback_t uhub_reset_tt_callback;
153 #endif
154 
155 static void usb_dev_resume_peer(struct usb_device *udev);
156 static void usb_dev_suspend_peer(struct usb_device *udev);
157 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
158 
159 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
160 
161 	[UHUB_INTR_TRANSFER] = {
162 		.type = UE_INTERRUPT,
163 		.endpoint = UE_ADDR_ANY,
164 		.direction = UE_DIR_ANY,
165 		.timeout = 0,
166 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
167 		.bufsize = 0,	/* use wMaxPacketSize */
168 		.callback = &uhub_intr_callback,
169 		.interval = UHUB_INTR_INTERVAL,
170 	},
171 #if USB_HAVE_TT_SUPPORT
172 	[UHUB_RESET_TT_TRANSFER] = {
173 		.type = UE_CONTROL,
174 		.endpoint = 0x00,	/* Control pipe */
175 		.direction = UE_DIR_ANY,
176 		.bufsize = sizeof(struct usb_device_request),
177 		.callback = &uhub_reset_tt_callback,
178 		.timeout = 1000,	/* 1 second */
179 		.usb_mode = USB_MODE_HOST,
180 	},
181 #endif
182 };
183 
184 /*
185  * driver instance for "hub" connected to "usb"
186  * and "hub" connected to "hub"
187  */
188 static devclass_t uhub_devclass;
189 
190 static device_method_t uhub_methods[] = {
191 	DEVMETHOD(device_probe, uhub_probe),
192 	DEVMETHOD(device_attach, uhub_attach),
193 	DEVMETHOD(device_detach, uhub_detach),
194 
195 	DEVMETHOD(device_suspend, uhub_suspend),
196 	DEVMETHOD(device_resume, uhub_resume),
197 
198 	DEVMETHOD(bus_child_location_str, uhub_child_location_string),
199 	DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
200 	DEVMETHOD(bus_driver_added, uhub_driver_added),
201 	DEVMETHOD_END
202 };
203 
204 static driver_t uhub_driver = {
205 	.name = "uhub",
206 	.methods = uhub_methods,
207 	.size = sizeof(struct uhub_softc)
208 };
209 
210 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
211 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
212 MODULE_VERSION(uhub, 1);
213 
214 static void
215 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
216 {
217 	struct uhub_softc *sc = usbd_xfer_softc(xfer);
218 
219 	switch (USB_GET_STATE(xfer)) {
220 	case USB_ST_TRANSFERRED:
221 		DPRINTFN(2, "\n");
222 		/*
223 		 * This is an indication that some port
224 		 * has changed status. Notify the bus
225 		 * event handler thread that we need
226 		 * to be explored again:
227 		 */
228 		usb_needs_explore(sc->sc_udev->bus, 0);
229 
230 	case USB_ST_SETUP:
231 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
232 		usbd_transfer_submit(xfer);
233 		break;
234 
235 	default:			/* Error */
236 		if (xfer->error != USB_ERR_CANCELLED) {
237 			/*
238 			 * Do a clear-stall. The "stall_pipe" flag
239 			 * will get cleared before next callback by
240 			 * the USB stack.
241 			 */
242 			usbd_xfer_set_stall(xfer);
243 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
244 			usbd_transfer_submit(xfer);
245 		}
246 		break;
247 	}
248 }
249 
250 /*------------------------------------------------------------------------*
251  *      uhub_reset_tt_proc
252  *
253  * This function starts the TT reset USB request
254  *------------------------------------------------------------------------*/
255 #if USB_HAVE_TT_SUPPORT
256 static void
257 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
258 {
259 	struct usb_udev_msg *pm = (void *)_pm;
260 	struct usb_device *udev = pm->udev;
261 	struct usb_hub *hub;
262 	struct uhub_softc *sc;
263 
264 	hub = udev->hub;
265 	if (hub == NULL)
266 		return;
267 	sc = hub->hubsoftc;
268 	if (sc == NULL)
269 		return;
270 
271 	/* Change lock */
272 	USB_BUS_UNLOCK(udev->bus);
273 	mtx_lock(&sc->sc_mtx);
274 	/* Start transfer */
275 	usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
276 	/* Change lock */
277 	mtx_unlock(&sc->sc_mtx);
278 	USB_BUS_LOCK(udev->bus);
279 }
280 #endif
281 
282 /*------------------------------------------------------------------------*
283  *      uhub_tt_buffer_reset_async_locked
284  *
285  * This function queues a TT reset for the given USB device and endpoint.
286  *------------------------------------------------------------------------*/
287 #if USB_HAVE_TT_SUPPORT
288 void
289 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
290 {
291 	struct usb_device_request req;
292 	struct usb_device *udev;
293 	struct usb_hub *hub;
294 	struct usb_port *up;
295 	uint16_t wValue;
296 	uint8_t port;
297 
298 	if (child == NULL || ep == NULL)
299 		return;
300 
301 	udev = child->parent_hs_hub;
302 	port = child->hs_port_no;
303 
304 	if (udev == NULL)
305 		return;
306 
307 	hub = udev->hub;
308 	if ((hub == NULL) ||
309 	    (udev->speed != USB_SPEED_HIGH) ||
310 	    (child->speed != USB_SPEED_LOW &&
311 	     child->speed != USB_SPEED_FULL) ||
312 	    (child->flags.usb_mode != USB_MODE_HOST) ||
313 	    (port == 0) || (ep->edesc == NULL)) {
314 		/* not applicable */
315 		return;
316 	}
317 
318 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
319 
320 	up = hub->ports + port - 1;
321 
322 	if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
323 	    udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
324 		port = 1;
325 
326 	/* if we already received a clear buffer request, reset the whole TT */
327 	if (up->req_reset_tt.bRequest != 0) {
328 		req.bmRequestType = UT_WRITE_CLASS_OTHER;
329 		req.bRequest = UR_RESET_TT;
330 		USETW(req.wValue, 0);
331 		req.wIndex[0] = port;
332 		req.wIndex[1] = 0;
333 		USETW(req.wLength, 0);
334 	} else {
335 		wValue = (ep->edesc->bEndpointAddress & 0xF) |
336 		      ((child->address & 0x7F) << 4) |
337 		      ((ep->edesc->bEndpointAddress & 0x80) << 8) |
338 		      ((ep->edesc->bmAttributes & 3) << 12);
339 
340 		req.bmRequestType = UT_WRITE_CLASS_OTHER;
341 		req.bRequest = UR_CLEAR_TT_BUFFER;
342 		USETW(req.wValue, wValue);
343 		req.wIndex[0] = port;
344 		req.wIndex[1] = 0;
345 		USETW(req.wLength, 0);
346 	}
347 	up->req_reset_tt = req;
348 	/* get reset transfer started */
349 	usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev->bus),
350 	    &hub->tt_msg[0], &hub->tt_msg[1]);
351 }
352 #endif
353 
354 #if USB_HAVE_TT_SUPPORT
355 static void
356 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
357 {
358 	struct uhub_softc *sc;
359 	struct usb_device *udev;
360 	struct usb_port *up;
361 	uint8_t x;
362 
363 	DPRINTF("TT buffer reset\n");
364 
365 	sc = usbd_xfer_softc(xfer);
366 	udev = sc->sc_udev;
367 
368 	switch (USB_GET_STATE(xfer)) {
369 	case USB_ST_TRANSFERRED:
370 	case USB_ST_SETUP:
371 tr_setup:
372 		USB_BUS_LOCK(udev->bus);
373 		/* find first port which needs a TT reset */
374 		for (x = 0; x != udev->hub->nports; x++) {
375 			up = udev->hub->ports + x;
376 
377 			if (up->req_reset_tt.bRequest == 0)
378 				continue;
379 
380 			/* copy in the transfer */
381 			usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
382 			    sizeof(up->req_reset_tt));
383 			/* reset buffer */
384 			memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
385 
386 			/* set length */
387 			usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
388 			xfer->nframes = 1;
389 			USB_BUS_UNLOCK(udev->bus);
390 
391 			usbd_transfer_submit(xfer);
392 			return;
393 		}
394 		USB_BUS_UNLOCK(udev->bus);
395 		break;
396 
397 	default:
398 		if (error == USB_ERR_CANCELLED)
399 			break;
400 
401 		DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
402 		goto tr_setup;
403 	}
404 }
405 #endif
406 
407 /*------------------------------------------------------------------------*
408  *      uhub_count_active_host_ports
409  *
410  * This function counts the number of active ports at the given speed.
411  *------------------------------------------------------------------------*/
412 uint8_t
413 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
414 {
415 	struct uhub_softc *sc;
416 	struct usb_device *child;
417 	struct usb_hub *hub;
418 	struct usb_port *up;
419 	uint8_t retval = 0;
420 	uint8_t x;
421 
422 	if (udev == NULL)
423 		goto done;
424 	hub = udev->hub;
425 	if (hub == NULL)
426 		goto done;
427 	sc = hub->hubsoftc;
428 	if (sc == NULL)
429 		goto done;
430 
431 	for (x = 0; x != hub->nports; x++) {
432 		up = hub->ports + x;
433 		child = usb_bus_port_get_device(udev->bus, up);
434 		if (child != NULL &&
435 		    child->flags.usb_mode == USB_MODE_HOST &&
436 		    child->speed == speed)
437 			retval++;
438 	}
439 done:
440 	return (retval);
441 }
442 
443 void
444 uhub_explore_handle_re_enumerate(struct usb_device *child)
445 {
446 	uint8_t do_unlock;
447 	usb_error_t err;
448 
449 	/* check if device should be re-enumerated */
450 	if (child->flags.usb_mode != USB_MODE_HOST)
451 		return;
452 
453 	do_unlock = usbd_enum_lock(child);
454 	switch (child->re_enumerate_wait) {
455 	case USB_RE_ENUM_START:
456 		err = usbd_set_config_index(child,
457 		    USB_UNCONFIG_INDEX);
458 		if (err != 0) {
459 			DPRINTF("Unconfigure failed: %s: Ignored.\n",
460 			    usbd_errstr(err));
461 		}
462 		if (child->parent_hub == NULL) {
463 			/* the root HUB cannot be re-enumerated */
464 			DPRINTFN(6, "cannot reset root HUB\n");
465 			err = 0;
466 		} else {
467 			err = usbd_req_re_enumerate(child, NULL);
468 		}
469 		if (err == 0)
470 			err = usbd_set_config_index(child, 0);
471 		if (err == 0) {
472 			err = usb_probe_and_attach(child,
473 			    USB_IFACE_INDEX_ANY);
474 		}
475 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
476 		break;
477 
478 	case USB_RE_ENUM_PWR_OFF:
479 		/* get the device unconfigured */
480 		err = usbd_set_config_index(child,
481 		    USB_UNCONFIG_INDEX);
482 		if (err) {
483 			DPRINTFN(0, "Could not unconfigure "
484 			    "device (ignored)\n");
485 		}
486 		if (child->parent_hub == NULL) {
487 			/* the root HUB cannot be re-enumerated */
488 			DPRINTFN(6, "cannot set port feature\n");
489 			err = 0;
490 		} else {
491 			/* clear port enable */
492 			err = usbd_req_clear_port_feature(child->parent_hub,
493 			    NULL, child->port_no, UHF_PORT_ENABLE);
494 			if (err) {
495 				DPRINTFN(0, "Could not disable port "
496 				    "(ignored)\n");
497 			}
498 		}
499 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
500 		break;
501 
502 	case USB_RE_ENUM_SET_CONFIG:
503 		err = usbd_set_config_index(child,
504 		    child->next_config_index);
505 		if (err != 0) {
506 			DPRINTF("Configure failed: %s: Ignored.\n",
507 			    usbd_errstr(err));
508 		} else {
509 			err = usb_probe_and_attach(child,
510 			    USB_IFACE_INDEX_ANY);
511 		}
512 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
513 		break;
514 
515 	default:
516 		child->re_enumerate_wait = USB_RE_ENUM_DONE;
517 		break;
518 	}
519 	if (do_unlock)
520 		usbd_enum_unlock(child);
521 }
522 
523 /*------------------------------------------------------------------------*
524  *	uhub_explore_sub - subroutine
525  *
526  * Return values:
527  *    0: Success
528  * Else: A control transaction failed
529  *------------------------------------------------------------------------*/
530 static usb_error_t
531 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
532 {
533 	struct usb_bus *bus;
534 	struct usb_device *child;
535 	uint8_t refcount;
536 	usb_error_t err;
537 
538 	bus = sc->sc_udev->bus;
539 	err = 0;
540 
541 	/* get driver added refcount from USB bus */
542 	refcount = bus->driver_added_refcount;
543 
544 	/* get device assosiated with the given port */
545 	child = usb_bus_port_get_device(bus, up);
546 	if (child == NULL) {
547 		/* nothing to do */
548 		goto done;
549 	}
550 
551 	uhub_explore_handle_re_enumerate(child);
552 
553 	/* check if probe and attach should be done */
554 
555 	if (child->driver_added_refcount != refcount) {
556 		child->driver_added_refcount = refcount;
557 		err = usb_probe_and_attach(child,
558 		    USB_IFACE_INDEX_ANY);
559 		if (err) {
560 			goto done;
561 		}
562 	}
563 	/* start control transfer, if device mode */
564 
565 	if (child->flags.usb_mode == USB_MODE_DEVICE)
566 		usbd_ctrl_transfer_setup(child);
567 
568 	/* if a HUB becomes present, do a recursive HUB explore */
569 
570 	if (child->hub)
571 		err = (child->hub->explore) (child);
572 
573 done:
574 	return (err);
575 }
576 
577 /*------------------------------------------------------------------------*
578  *	uhub_read_port_status - factored out code
579  *------------------------------------------------------------------------*/
580 static usb_error_t
581 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
582 {
583 	struct usb_port_status ps;
584 	usb_error_t err;
585 
586 	err = usbd_req_get_port_status(
587 	    sc->sc_udev, NULL, &ps, portno);
588 
589 	/* update status regardless of error */
590 
591 	sc->sc_st.port_status = UGETW(ps.wPortStatus);
592 	sc->sc_st.port_change = UGETW(ps.wPortChange);
593 
594 	/* debugging print */
595 
596 	DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
597 	    "wPortChange=0x%04x, err=%s\n",
598 	    portno, sc->sc_st.port_status,
599 	    sc->sc_st.port_change, usbd_errstr(err));
600 	return (err);
601 }
602 
603 /*------------------------------------------------------------------------*
604  *	uhub_reattach_port
605  *
606  * Returns:
607  *    0: Success
608  * Else: A control transaction failed
609  *------------------------------------------------------------------------*/
610 static usb_error_t
611 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
612 {
613 	struct usb_device *child;
614 	struct usb_device *udev;
615 	enum usb_dev_speed speed;
616 	enum usb_hc_mode mode;
617 	usb_error_t err;
618 	uint16_t power_mask;
619 	uint8_t timeout;
620 
621 	DPRINTF("reattaching port %d\n", portno);
622 
623 	timeout = 0;
624 	udev = sc->sc_udev;
625 	child = usb_bus_port_get_device(udev->bus,
626 	    udev->hub->ports + portno - 1);
627 
628 repeat:
629 
630 	/* first clear the port connection change bit */
631 
632 	err = usbd_req_clear_port_feature(udev, NULL,
633 	    portno, UHF_C_PORT_CONNECTION);
634 
635 	if (err)
636 		goto error;
637 
638 	/* check if there is a child */
639 
640 	if (child != NULL) {
641 		/*
642 		 * Free USB device and all subdevices, if any.
643 		 */
644 		usb_free_device(child, 0);
645 		child = NULL;
646 	}
647 	/* get fresh status */
648 
649 	err = uhub_read_port_status(sc, portno);
650 	if (err)
651 		goto error;
652 
653 #if USB_HAVE_DISABLE_ENUM
654 	/* check if we should skip enumeration from this USB HUB */
655 	if (usb_disable_enumeration != 0 ||
656 	    sc->sc_disable_enumeration != 0) {
657 		DPRINTF("Enumeration is disabled!\n");
658 		goto error;
659 	}
660 #endif
661 	/* check if nothing is connected to the port */
662 
663 	if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
664 		goto error;
665 
666 	/* check if there is no power on the port and print a warning */
667 
668 	switch (udev->speed) {
669 	case USB_SPEED_HIGH:
670 	case USB_SPEED_FULL:
671 	case USB_SPEED_LOW:
672 		power_mask = UPS_PORT_POWER;
673 		break;
674 	case USB_SPEED_SUPER:
675 		if (udev->parent_hub == NULL)
676 			power_mask = UPS_PORT_POWER;
677 		else
678 			power_mask = UPS_PORT_POWER_SS;
679 		break;
680 	default:
681 		power_mask = 0;
682 		break;
683 	}
684 	if (!(sc->sc_st.port_status & power_mask)) {
685 		DPRINTF("WARNING: strange, connected port %d "
686 		    "has no power\n", portno);
687 	}
688 
689 	/* check if the device is in Host Mode */
690 
691 	if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
692 
693 		DPRINTF("Port %d is in Host Mode\n", portno);
694 
695 		if (sc->sc_st.port_status & UPS_SUSPEND) {
696 			/*
697 			 * NOTE: Should not get here in SuperSpeed
698 			 * mode, because the HUB should report this
699 			 * bit as zero.
700 			 */
701 			DPRINTF("Port %d was still "
702 			    "suspended, clearing.\n", portno);
703 			err = usbd_req_clear_port_feature(udev,
704 			    NULL, portno, UHF_PORT_SUSPEND);
705 		}
706 
707 		/* USB Host Mode */
708 
709 		/* wait for maximum device power up time */
710 
711 		usb_pause_mtx(NULL,
712 		    USB_MS_TO_TICKS(usb_port_powerup_delay));
713 
714 		/* reset port, which implies enabling it */
715 
716 		err = usbd_req_reset_port(udev, NULL, portno);
717 
718 		if (err) {
719 			DPRINTFN(0, "port %d reset "
720 			    "failed, error=%s\n",
721 			    portno, usbd_errstr(err));
722 			goto error;
723 		}
724 		/* get port status again, it might have changed during reset */
725 
726 		err = uhub_read_port_status(sc, portno);
727 		if (err) {
728 			goto error;
729 		}
730 		/* check if something changed during port reset */
731 
732 		if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
733 		    (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
734 			if (timeout) {
735 				DPRINTFN(0, "giving up port reset "
736 				    "- device vanished\n");
737 				goto error;
738 			}
739 			timeout = 1;
740 			goto repeat;
741 		}
742 	} else {
743 		DPRINTF("Port %d is in Device Mode\n", portno);
744 	}
745 
746 	/*
747 	 * Figure out the device speed
748 	 */
749 	switch (udev->speed) {
750 	case USB_SPEED_HIGH:
751 		if (sc->sc_st.port_status & UPS_HIGH_SPEED)
752 			speed = USB_SPEED_HIGH;
753 		else if (sc->sc_st.port_status & UPS_LOW_SPEED)
754 			speed = USB_SPEED_LOW;
755 		else
756 			speed = USB_SPEED_FULL;
757 		break;
758 	case USB_SPEED_FULL:
759 		if (sc->sc_st.port_status & UPS_LOW_SPEED)
760 			speed = USB_SPEED_LOW;
761 		else
762 			speed = USB_SPEED_FULL;
763 		break;
764 	case USB_SPEED_LOW:
765 		speed = USB_SPEED_LOW;
766 		break;
767 	case USB_SPEED_SUPER:
768 		if (udev->parent_hub == NULL) {
769 			/* Root HUB - special case */
770 			switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
771 			case 0:
772 				speed = USB_SPEED_FULL;
773 				break;
774 			case UPS_LOW_SPEED:
775 				speed = USB_SPEED_LOW;
776 				break;
777 			case UPS_HIGH_SPEED:
778 				speed = USB_SPEED_HIGH;
779 				break;
780 			default:
781 				speed = USB_SPEED_SUPER;
782 				break;
783 			}
784 		} else {
785 			speed = USB_SPEED_SUPER;
786 		}
787 		break;
788 	default:
789 		/* same speed like parent */
790 		speed = udev->speed;
791 		break;
792 	}
793 	if (speed == USB_SPEED_SUPER) {
794 		err = usbd_req_set_hub_u1_timeout(udev, NULL,
795 		    portno, 128 - (2 * udev->depth));
796 		if (err) {
797 			DPRINTFN(0, "port %d U1 timeout "
798 			    "failed, error=%s\n",
799 			    portno, usbd_errstr(err));
800 		}
801 		err = usbd_req_set_hub_u2_timeout(udev, NULL,
802 		    portno, 128 - (2 * udev->depth));
803 		if (err) {
804 			DPRINTFN(0, "port %d U2 timeout "
805 			    "failed, error=%s\n",
806 			    portno, usbd_errstr(err));
807 		}
808 	}
809 
810 	/*
811 	 * Figure out the device mode
812 	 *
813 	 * NOTE: This part is currently FreeBSD specific.
814 	 */
815 	if (udev->parent_hub != NULL) {
816 		/* inherit mode from the parent HUB */
817 		mode = udev->parent_hub->flags.usb_mode;
818 	} else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
819 		mode = USB_MODE_DEVICE;
820 	else
821 		mode = USB_MODE_HOST;
822 
823 	/* need to create a new child */
824 	child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
825 	    udev->depth + 1, portno - 1, portno, speed, mode);
826 	if (child == NULL) {
827 		DPRINTFN(0, "could not allocate new device\n");
828 		goto error;
829 	}
830 	return (0);			/* success */
831 
832 error:
833 	if (child != NULL) {
834 		/*
835 		 * Free USB device and all subdevices, if any.
836 		 */
837 		usb_free_device(child, 0);
838 		child = NULL;
839 	}
840 	if (err == 0) {
841 		if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
842 			err = usbd_req_clear_port_feature(
843 			    sc->sc_udev, NULL,
844 			    portno, UHF_PORT_ENABLE);
845 		}
846 	}
847 	if (err) {
848 		DPRINTFN(0, "device problem (%s), "
849 		    "disabling port %d\n", usbd_errstr(err), portno);
850 	}
851 	return (err);
852 }
853 
854 /*------------------------------------------------------------------------*
855  *	usb_device_20_compatible
856  *
857  * Returns:
858  *    0: HUB does not support suspend and resume
859  * Else: HUB supports suspend and resume
860  *------------------------------------------------------------------------*/
861 static uint8_t
862 usb_device_20_compatible(struct usb_device *udev)
863 {
864 	if (udev == NULL)
865 		return (0);
866 	switch (udev->speed) {
867 	case USB_SPEED_LOW:
868 	case USB_SPEED_FULL:
869 	case USB_SPEED_HIGH:
870 		return (1);
871 	default:
872 		return (0);
873 	}
874 }
875 
876 /*------------------------------------------------------------------------*
877  *	uhub_suspend_resume_port
878  *
879  * Returns:
880  *    0: Success
881  * Else: A control transaction failed
882  *------------------------------------------------------------------------*/
883 static usb_error_t
884 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
885 {
886 	struct usb_device *child;
887 	struct usb_device *udev;
888 	uint8_t is_suspend;
889 	usb_error_t err;
890 
891 	DPRINTF("port %d\n", portno);
892 
893 	udev = sc->sc_udev;
894 	child = usb_bus_port_get_device(udev->bus,
895 	    udev->hub->ports + portno - 1);
896 
897 	/* first clear the port suspend change bit */
898 
899 	if (usb_device_20_compatible(udev)) {
900 		err = usbd_req_clear_port_feature(udev, NULL,
901 		    portno, UHF_C_PORT_SUSPEND);
902 	} else {
903 		err = usbd_req_clear_port_feature(udev, NULL,
904 		    portno, UHF_C_PORT_LINK_STATE);
905 	}
906 
907 	if (err) {
908 		DPRINTF("clearing suspend failed.\n");
909 		goto done;
910 	}
911 	/* get fresh status */
912 
913 	err = uhub_read_port_status(sc, portno);
914 	if (err) {
915 		DPRINTF("reading port status failed.\n");
916 		goto done;
917 	}
918 	/* convert current state */
919 
920 	if (usb_device_20_compatible(udev)) {
921 		if (sc->sc_st.port_status & UPS_SUSPEND) {
922 			is_suspend = 1;
923 		} else {
924 			is_suspend = 0;
925 		}
926 	} else {
927 		switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
928 		case UPS_PORT_LS_U3:
929 			is_suspend = 1;
930 			break;
931 		case UPS_PORT_LS_SS_INA:
932 			usbd_req_warm_reset_port(udev, NULL, portno);
933 			is_suspend = 0;
934 			break;
935 		default:
936 			is_suspend = 0;
937 			break;
938 		}
939 	}
940 
941 	DPRINTF("suspended=%u\n", is_suspend);
942 
943 	/* do the suspend or resume */
944 
945 	if (child) {
946 		/*
947 		 * This code handle two cases: 1) Host Mode - we can only
948 		 * receive resume here 2) Device Mode - we can receive
949 		 * suspend and resume here
950 		 */
951 		if (is_suspend == 0)
952 			usb_dev_resume_peer(child);
953 		else if (child->flags.usb_mode == USB_MODE_DEVICE)
954 			usb_dev_suspend_peer(child);
955 	}
956 done:
957 	return (err);
958 }
959 
960 /*------------------------------------------------------------------------*
961  *	uhub_root_interrupt
962  *
963  * This function is called when a Root HUB interrupt has
964  * happened. "ptr" and "len" makes up the Root HUB interrupt
965  * packet. This function is called having the "bus_mtx" locked.
966  *------------------------------------------------------------------------*/
967 void
968 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
969 {
970 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
971 
972 	usb_needs_explore(bus, 0);
973 }
974 
975 static uint8_t
976 uhub_is_too_deep(struct usb_device *udev)
977 {
978 	switch (udev->speed) {
979 	case USB_SPEED_FULL:
980 	case USB_SPEED_LOW:
981 	case USB_SPEED_HIGH:
982 		if (udev->depth > USB_HUB_MAX_DEPTH)
983 			return (1);
984 		break;
985 	case USB_SPEED_SUPER:
986 		if (udev->depth > USB_SS_HUB_DEPTH_MAX)
987 			return (1);
988 		break;
989 	default:
990 		break;
991 	}
992 	return (0);
993 }
994 
995 /*------------------------------------------------------------------------*
996  *	uhub_explore
997  *
998  * Returns:
999  *     0: Success
1000  *  Else: Failure
1001  *------------------------------------------------------------------------*/
1002 static usb_error_t
1003 uhub_explore(struct usb_device *udev)
1004 {
1005 	struct usb_hub *hub;
1006 	struct uhub_softc *sc;
1007 	struct usb_port *up;
1008 	usb_error_t err;
1009 	uint8_t portno;
1010 	uint8_t x;
1011 	uint8_t do_unlock;
1012 
1013 	hub = udev->hub;
1014 	sc = hub->hubsoftc;
1015 
1016 	DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
1017 
1018 	/* ignore devices that are too deep */
1019 	if (uhub_is_too_deep(udev))
1020 		return (USB_ERR_TOO_DEEP);
1021 
1022 	/* check if device is suspended */
1023 	if (udev->flags.self_suspended) {
1024 		/* need to wait until the child signals resume */
1025 		DPRINTF("Device is suspended!\n");
1026 		return (0);
1027 	}
1028 
1029 	/*
1030 	 * Make sure we don't race against user-space applications
1031 	 * like LibUSB:
1032 	 */
1033 	do_unlock = usbd_enum_lock(udev);
1034 
1035 	for (x = 0; x != hub->nports; x++) {
1036 		up = hub->ports + x;
1037 		portno = x + 1;
1038 
1039 		err = uhub_read_port_status(sc, portno);
1040 		if (err) {
1041 			/* most likely the HUB is gone */
1042 			break;
1043 		}
1044 		if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
1045 			DPRINTF("Overcurrent on port %u.\n", portno);
1046 			err = usbd_req_clear_port_feature(
1047 			    udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
1048 			if (err) {
1049 				/* most likely the HUB is gone */
1050 				break;
1051 			}
1052 		}
1053 		if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
1054 			/*
1055 			 * Fake a connect status change so that the
1056 			 * status gets checked initially!
1057 			 */
1058 			sc->sc_st.port_change |=
1059 			    UPS_C_CONNECT_STATUS;
1060 		}
1061 		if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
1062 			err = usbd_req_clear_port_feature(
1063 			    udev, NULL, portno, UHF_C_PORT_ENABLE);
1064 			if (err) {
1065 				/* most likely the HUB is gone */
1066 				break;
1067 			}
1068 			if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1069 				/*
1070 				 * Ignore the port error if the device
1071 				 * has vanished !
1072 				 */
1073 			} else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
1074 				DPRINTFN(0, "illegal enable change, "
1075 				    "port %d\n", portno);
1076 			} else {
1077 
1078 				if (up->restartcnt == USB_RESTART_MAX) {
1079 					/* XXX could try another speed ? */
1080 					DPRINTFN(0, "port error, giving up "
1081 					    "port %d\n", portno);
1082 				} else {
1083 					sc->sc_st.port_change |=
1084 					    UPS_C_CONNECT_STATUS;
1085 					up->restartcnt++;
1086 				}
1087 			}
1088 		}
1089 		if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
1090 			err = uhub_reattach_port(sc, portno);
1091 			if (err) {
1092 				/* most likely the HUB is gone */
1093 				break;
1094 			}
1095 		}
1096 		if (sc->sc_st.port_change & (UPS_C_SUSPEND |
1097 		    UPS_C_PORT_LINK_STATE)) {
1098 			err = uhub_suspend_resume_port(sc, portno);
1099 			if (err) {
1100 				/* most likely the HUB is gone */
1101 				break;
1102 			}
1103 		}
1104 		err = uhub_explore_sub(sc, up);
1105 		if (err) {
1106 			/* no device(s) present */
1107 			continue;
1108 		}
1109 		/* explore succeeded - reset restart counter */
1110 		up->restartcnt = 0;
1111 	}
1112 
1113 	if (do_unlock)
1114 		usbd_enum_unlock(udev);
1115 
1116 	/* initial status checked */
1117 	sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
1118 
1119 	/* return success */
1120 	return (USB_ERR_NORMAL_COMPLETION);
1121 }
1122 
1123 static int
1124 uhub_probe(device_t dev)
1125 {
1126 	struct usb_attach_arg *uaa = device_get_ivars(dev);
1127 
1128 	if (uaa->usb_mode != USB_MODE_HOST)
1129 		return (ENXIO);
1130 
1131 	/*
1132 	 * The subclass for USB HUBs is currently ignored because it
1133 	 * is 0 for some and 1 for others.
1134 	 */
1135 	if (uaa->info.bConfigIndex == 0 &&
1136 	    uaa->info.bDeviceClass == UDCLASS_HUB)
1137 		return (0);
1138 
1139 	return (ENXIO);
1140 }
1141 
1142 /* NOTE: The information returned by this function can be wrong. */
1143 usb_error_t
1144 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
1145 {
1146 	struct usb_hub_descriptor hubdesc20;
1147 	struct usb_hub_ss_descriptor hubdesc30;
1148 	usb_error_t err;
1149 	uint8_t nports;
1150 	uint8_t tt;
1151 
1152 	if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
1153 		return (USB_ERR_INVAL);
1154 
1155 	nports = 0;
1156 	tt = 0;
1157 
1158 	switch (udev->speed) {
1159 	case USB_SPEED_LOW:
1160 	case USB_SPEED_FULL:
1161 	case USB_SPEED_HIGH:
1162 		/* assuming that there is one port */
1163 		err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1164 		if (err) {
1165 			DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1166 			    "error=%s\n", usbd_errstr(err));
1167 			break;
1168 		}
1169 		nports = hubdesc20.bNbrPorts;
1170 		if (nports > 127)
1171 			nports = 127;
1172 
1173 		if (udev->speed == USB_SPEED_HIGH)
1174 			tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
1175 		break;
1176 
1177 	case USB_SPEED_SUPER:
1178 		err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1179 		if (err) {
1180 			DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1181 			    "error=%s\n", usbd_errstr(err));
1182 			break;
1183 		}
1184 		nports = hubdesc30.bNbrPorts;
1185 		if (nports > 16)
1186 			nports = 16;
1187 		break;
1188 
1189 	default:
1190 		err = USB_ERR_INVAL;
1191 		break;
1192 	}
1193 
1194 	if (pnports != NULL)
1195 		*pnports = nports;
1196 
1197 	if (ptt != NULL)
1198 		*ptt = tt;
1199 
1200 	return (err);
1201 }
1202 
1203 static int
1204 uhub_attach(device_t dev)
1205 {
1206 	struct uhub_softc *sc = device_get_softc(dev);
1207 	struct usb_attach_arg *uaa = device_get_ivars(dev);
1208 	struct usb_device *udev = uaa->device;
1209 	struct usb_device *parent_hub = udev->parent_hub;
1210 	struct usb_hub *hub;
1211 	struct usb_hub_descriptor hubdesc20;
1212 	struct usb_hub_ss_descriptor hubdesc30;
1213 #if USB_HAVE_DISABLE_ENUM
1214 	struct sysctl_ctx_list *sysctl_ctx;
1215 	struct sysctl_oid *sysctl_tree;
1216 #endif
1217 	uint16_t pwrdly;
1218 	uint16_t nports;
1219 	uint8_t x;
1220 	uint8_t portno;
1221 	uint8_t removable;
1222 	uint8_t iface_index;
1223 	usb_error_t err;
1224 
1225 	sc->sc_udev = udev;
1226 	sc->sc_dev = dev;
1227 
1228 	mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
1229 
1230 	device_set_usb_desc(dev);
1231 
1232 	DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
1233 	    "parent->selfpowered=%d\n",
1234 	    udev->depth,
1235 	    udev->flags.self_powered,
1236 	    parent_hub,
1237 	    parent_hub ?
1238 	    parent_hub->flags.self_powered : 0);
1239 
1240 	if (uhub_is_too_deep(udev)) {
1241 		DPRINTFN(0, "HUB at depth %d, "
1242 		    "exceeds maximum. HUB ignored\n", (int)udev->depth);
1243 		goto error;
1244 	}
1245 
1246 	if (!udev->flags.self_powered && parent_hub &&
1247 	    !parent_hub->flags.self_powered) {
1248 		DPRINTFN(0, "Bus powered HUB connected to "
1249 		    "bus powered HUB. HUB ignored\n");
1250 		goto error;
1251 	}
1252 
1253 	if (UHUB_IS_MULTI_TT(sc)) {
1254 		err = usbd_set_alt_interface_index(udev, 0, 1);
1255 		if (err) {
1256 			device_printf(dev, "MTT could not be enabled\n");
1257 			goto error;
1258 		}
1259 		device_printf(dev, "MTT enabled\n");
1260 	}
1261 
1262 	/* get HUB descriptor */
1263 
1264 	DPRINTFN(2, "Getting HUB descriptor\n");
1265 
1266 	switch (udev->speed) {
1267 	case USB_SPEED_LOW:
1268 	case USB_SPEED_FULL:
1269 	case USB_SPEED_HIGH:
1270 		/* assuming that there is one port */
1271 		err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
1272 		if (err) {
1273 			DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
1274 			    "error=%s\n", usbd_errstr(err));
1275 			goto error;
1276 		}
1277 		/* get number of ports */
1278 		nports = hubdesc20.bNbrPorts;
1279 
1280 		/* get power delay */
1281 		pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1282 		    usb_extra_power_up_time);
1283 
1284 		/* get complete HUB descriptor */
1285 		if (nports >= 8) {
1286 			/* check number of ports */
1287 			if (nports > 127) {
1288 				DPRINTFN(0, "Invalid number of USB 2.0 ports,"
1289 				    "error=%s\n", usbd_errstr(err));
1290 				goto error;
1291 			}
1292 			/* get complete HUB descriptor */
1293 			err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
1294 
1295 			if (err) {
1296 				DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1297 				    "error=%s\n", usbd_errstr(err));
1298 				goto error;
1299 			}
1300 			if (hubdesc20.bNbrPorts != nports) {
1301 				DPRINTFN(0, "Number of ports changed\n");
1302 				goto error;
1303 			}
1304 		}
1305 		break;
1306 	case USB_SPEED_SUPER:
1307 		if (udev->parent_hub != NULL) {
1308 			err = usbd_req_set_hub_depth(udev, NULL,
1309 			    udev->depth - 1);
1310 			if (err) {
1311 				DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
1312 				    "error=%s\n", usbd_errstr(err));
1313 				goto error;
1314 			}
1315 		}
1316 		err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
1317 		if (err) {
1318 			DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
1319 			    "error=%s\n", usbd_errstr(err));
1320 			goto error;
1321 		}
1322 		/* get number of ports */
1323 		nports = hubdesc30.bNbrPorts;
1324 
1325 		/* get power delay */
1326 		pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
1327 		    usb_extra_power_up_time);
1328 
1329 		/* get complete HUB descriptor */
1330 		if (nports >= 8) {
1331 			/* check number of ports */
1332 			if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
1333 				DPRINTFN(0, "Invalid number of USB 3.0 ports,"
1334 				    "error=%s\n", usbd_errstr(err));
1335 				goto error;
1336 			}
1337 			/* get complete HUB descriptor */
1338 			err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
1339 
1340 			if (err) {
1341 				DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
1342 				    "error=%s\n", usbd_errstr(err));
1343 				goto error;
1344 			}
1345 			if (hubdesc30.bNbrPorts != nports) {
1346 				DPRINTFN(0, "Number of ports changed\n");
1347 				goto error;
1348 			}
1349 		}
1350 		break;
1351 	default:
1352 		DPRINTF("Assuming HUB has only one port\n");
1353 		/* default number of ports */
1354 		nports = 1;
1355 		/* default power delay */
1356 		pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
1357 		break;
1358 	}
1359 	if (nports == 0) {
1360 		DPRINTFN(0, "portless HUB\n");
1361 		goto error;
1362 	}
1363 	if (nports > USB_MAX_PORTS) {
1364 		DPRINTF("Port limit exceeded\n");
1365 		goto error;
1366 	}
1367 #if (USB_HAVE_FIXED_PORT == 0)
1368 	hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
1369 	    M_USBDEV, M_WAITOK | M_ZERO);
1370 
1371 	if (hub == NULL)
1372 		goto error;
1373 #else
1374 	hub = &sc->sc_hub;
1375 #endif
1376 	udev->hub = hub;
1377 
1378 	/* initialize HUB structure */
1379 	hub->hubsoftc = sc;
1380 	hub->explore = &uhub_explore;
1381 	hub->nports = nports;
1382 	hub->hubudev = udev;
1383 #if USB_HAVE_TT_SUPPORT
1384 	hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
1385 	hub->tt_msg[0].udev = udev;
1386 	hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
1387 	hub->tt_msg[1].udev = udev;
1388 #endif
1389 	/* if self powered hub, give ports maximum current */
1390 	if (udev->flags.self_powered) {
1391 		hub->portpower = USB_MAX_POWER;
1392 	} else {
1393 		hub->portpower = USB_MIN_POWER;
1394 	}
1395 
1396 	/* set up interrupt pipe */
1397 	iface_index = 0;
1398 	if (udev->parent_hub == NULL) {
1399 		/* root HUB is special */
1400 		err = 0;
1401 	} else {
1402 		/* normal HUB */
1403 		err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
1404 		    uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
1405 	}
1406 	if (err) {
1407 		DPRINTFN(0, "cannot setup interrupt transfer, "
1408 		    "errstr=%s\n", usbd_errstr(err));
1409 		goto error;
1410 	}
1411 	/* wait with power off for a while */
1412 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
1413 
1414 #if USB_HAVE_DISABLE_ENUM
1415 	/* Add device sysctls */
1416 
1417 	sysctl_ctx = device_get_sysctl_ctx(dev);
1418 	sysctl_tree = device_get_sysctl_tree(dev);
1419 
1420 	if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1421 		(void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1422 		    OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
1423 		    &sc->sc_disable_enumeration, 0,
1424 		    "Set to disable enumeration on this USB HUB.");
1425 
1426 		(void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1427 		    OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
1428 		    &sc->sc_disable_port_power, 0,
1429 		    "Set to disable USB port power on this USB HUB.");
1430 	}
1431 #endif
1432 	/*
1433 	 * To have the best chance of success we do things in the exact same
1434 	 * order as Windoze98.  This should not be necessary, but some
1435 	 * devices do not follow the USB specs to the letter.
1436 	 *
1437 	 * These are the events on the bus when a hub is attached:
1438 	 *  Get device and config descriptors (see attach code)
1439 	 *  Get hub descriptor (see above)
1440 	 *  For all ports
1441 	 *     turn on power
1442 	 *     wait for power to become stable
1443 	 * (all below happens in explore code)
1444 	 *  For all ports
1445 	 *     clear C_PORT_CONNECTION
1446 	 *  For all ports
1447 	 *     get port status
1448 	 *     if device connected
1449 	 *        wait 100 ms
1450 	 *        turn on reset
1451 	 *        wait
1452 	 *        clear C_PORT_RESET
1453 	 *        get port status
1454 	 *        proceed with device attachment
1455 	 */
1456 
1457 	/* XXX should check for none, individual, or ganged power? */
1458 
1459 	removable = 0;
1460 
1461 	for (x = 0; x != nports; x++) {
1462 		/* set up data structures */
1463 		struct usb_port *up = hub->ports + x;
1464 
1465 		up->device_index = 0;
1466 		up->restartcnt = 0;
1467 		portno = x + 1;
1468 
1469 		/* check if port is removable */
1470 		switch (udev->speed) {
1471 		case USB_SPEED_LOW:
1472 		case USB_SPEED_FULL:
1473 		case USB_SPEED_HIGH:
1474 			if (!UHD_NOT_REMOV(&hubdesc20, portno))
1475 				removable++;
1476 			break;
1477 		case USB_SPEED_SUPER:
1478 			if (!UHD_NOT_REMOV(&hubdesc30, portno))
1479 				removable++;
1480 			break;
1481 		default:
1482 			DPRINTF("Assuming removable port\n");
1483 			removable++;
1484 			break;
1485 		}
1486 		if (err == 0) {
1487 #if USB_HAVE_DISABLE_ENUM
1488 			/* check if we should disable USB port power or not */
1489 			if (usb_disable_port_power != 0 ||
1490 			    sc->sc_disable_port_power != 0) {
1491 				/* turn the power off */
1492 				DPRINTFN(2, "Turning port %d power off\n", portno);
1493 				err = usbd_req_clear_port_feature(udev, NULL,
1494 				    portno, UHF_PORT_POWER);
1495 			} else {
1496 #endif
1497 				/* turn the power on */
1498 				DPRINTFN(2, "Turning port %d power on\n", portno);
1499 				err = usbd_req_set_port_feature(udev, NULL,
1500 				    portno, UHF_PORT_POWER);
1501 #if USB_HAVE_DISABLE_ENUM
1502 			}
1503 #endif
1504 		}
1505 		if (err != 0) {
1506 			DPRINTFN(0, "port %d power on or off failed, %s\n",
1507 			    portno, usbd_errstr(err));
1508 		}
1509 		DPRINTF("turn on port %d power\n",
1510 		    portno);
1511 
1512 		/* wait for stable power */
1513 		usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
1514 	}
1515 
1516 	device_printf(dev, "%d port%s with %d "
1517 	    "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
1518 	    removable, udev->flags.self_powered ? "self" : "bus");
1519 
1520 	/* Start the interrupt endpoint, if any */
1521 
1522 	mtx_lock(&sc->sc_mtx);
1523 	usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
1524 	mtx_unlock(&sc->sc_mtx);
1525 
1526 	/* Enable automatic power save on all USB HUBs */
1527 
1528 	usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1529 
1530 	return (0);
1531 
1532 error:
1533 	usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1534 
1535 #if (USB_HAVE_FIXED_PORT == 0)
1536 	free(udev->hub, M_USBDEV);
1537 #endif
1538 	udev->hub = NULL;
1539 
1540 	mtx_destroy(&sc->sc_mtx);
1541 
1542 	return (ENXIO);
1543 }
1544 
1545 /*
1546  * Called from process context when the hub is gone.
1547  * Detach all devices on active ports.
1548  */
1549 static int
1550 uhub_detach(device_t dev)
1551 {
1552 	struct uhub_softc *sc = device_get_softc(dev);
1553 	struct usb_hub *hub = sc->sc_udev->hub;
1554 	struct usb_bus *bus = sc->sc_udev->bus;
1555 	struct usb_device *child;
1556 	uint8_t x;
1557 
1558 	if (hub == NULL)		/* must be partially working */
1559 		return (0);
1560 
1561 	/* Make sure interrupt transfer is gone. */
1562 	usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
1563 
1564 	/* Detach all ports */
1565 	for (x = 0; x != hub->nports; x++) {
1566 
1567 		child = usb_bus_port_get_device(bus, hub->ports + x);
1568 
1569 		if (child == NULL) {
1570 			continue;
1571 		}
1572 
1573 		/*
1574 		 * Free USB device and all subdevices, if any.
1575 		 */
1576 		usb_free_device(child, 0);
1577 	}
1578 
1579 #if USB_HAVE_TT_SUPPORT
1580 	/* Make sure our TT messages are not queued anywhere */
1581 	USB_BUS_LOCK(bus);
1582 	usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus),
1583 	    &hub->tt_msg[0], &hub->tt_msg[1]);
1584 	USB_BUS_UNLOCK(bus);
1585 #endif
1586 
1587 #if (USB_HAVE_FIXED_PORT == 0)
1588 	free(hub, M_USBDEV);
1589 #endif
1590 	sc->sc_udev->hub = NULL;
1591 
1592 	mtx_destroy(&sc->sc_mtx);
1593 
1594 	return (0);
1595 }
1596 
1597 static int
1598 uhub_suspend(device_t dev)
1599 {
1600 	DPRINTF("\n");
1601 	/* Sub-devices are not suspended here! */
1602 	return (0);
1603 }
1604 
1605 static int
1606 uhub_resume(device_t dev)
1607 {
1608 	DPRINTF("\n");
1609 	/* Sub-devices are not resumed here! */
1610 	return (0);
1611 }
1612 
1613 static void
1614 uhub_driver_added(device_t dev, driver_t *driver)
1615 {
1616 	usb_needs_explore_all();
1617 }
1618 
1619 struct hub_result {
1620 	struct usb_device *udev;
1621 	uint8_t	portno;
1622 	uint8_t	iface_index;
1623 };
1624 
1625 static void
1626 uhub_find_iface_index(struct usb_hub *hub, device_t child,
1627     struct hub_result *res)
1628 {
1629 	struct usb_interface *iface;
1630 	struct usb_device *udev;
1631 	uint8_t nports;
1632 	uint8_t x;
1633 	uint8_t i;
1634 
1635 	nports = hub->nports;
1636 	for (x = 0; x != nports; x++) {
1637 		udev = usb_bus_port_get_device(hub->hubudev->bus,
1638 		    hub->ports + x);
1639 		if (!udev) {
1640 			continue;
1641 		}
1642 		for (i = 0; i != USB_IFACE_MAX; i++) {
1643 			iface = usbd_get_iface(udev, i);
1644 			if (iface &&
1645 			    (iface->subdev == child)) {
1646 				res->iface_index = i;
1647 				res->udev = udev;
1648 				res->portno = x + 1;
1649 				return;
1650 			}
1651 		}
1652 	}
1653 	res->iface_index = 0;
1654 	res->udev = NULL;
1655 	res->portno = 0;
1656 }
1657 
1658 static int
1659 uhub_child_location_string(device_t parent, device_t child,
1660     char *buf, size_t buflen)
1661 {
1662 	struct uhub_softc *sc;
1663 	struct usb_hub *hub;
1664 	struct hub_result res;
1665 
1666 	if (!device_is_attached(parent)) {
1667 		if (buflen)
1668 			buf[0] = 0;
1669 		return (0);
1670 	}
1671 
1672 	sc = device_get_softc(parent);
1673 	hub = sc->sc_udev->hub;
1674 
1675 	mtx_lock(&Giant);
1676 	uhub_find_iface_index(hub, child, &res);
1677 	if (!res.udev) {
1678 		DPRINTF("device not on hub\n");
1679 		if (buflen) {
1680 			buf[0] = '\0';
1681 		}
1682 		goto done;
1683 	}
1684 	snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u interface=%u",
1685 	    device_get_unit(res.udev->bus->bdev),
1686 	    (res.udev->parent_hub != NULL) ? res.udev->parent_hub->device_index : 0,
1687 	    res.portno,
1688 	    res.udev->device_index, res.iface_index);
1689 done:
1690 	mtx_unlock(&Giant);
1691 
1692 	return (0);
1693 }
1694 
1695 static int
1696 uhub_child_pnpinfo_string(device_t parent, device_t child,
1697     char *buf, size_t buflen)
1698 {
1699 	struct uhub_softc *sc;
1700 	struct usb_hub *hub;
1701 	struct usb_interface *iface;
1702 	struct hub_result res;
1703 
1704 	if (!device_is_attached(parent)) {
1705 		if (buflen)
1706 			buf[0] = 0;
1707 		return (0);
1708 	}
1709 
1710 	sc = device_get_softc(parent);
1711 	hub = sc->sc_udev->hub;
1712 
1713 	mtx_lock(&Giant);
1714 	uhub_find_iface_index(hub, child, &res);
1715 	if (!res.udev) {
1716 		DPRINTF("device not on hub\n");
1717 		if (buflen) {
1718 			buf[0] = '\0';
1719 		}
1720 		goto done;
1721 	}
1722 	iface = usbd_get_iface(res.udev, res.iface_index);
1723 	if (iface && iface->idesc) {
1724 		snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
1725 		    "devclass=0x%02x devsubclass=0x%02x "
1726 		    "sernum=\"%s\" "
1727 		    "release=0x%04x "
1728 		    "mode=%s "
1729 		    "intclass=0x%02x intsubclass=0x%02x "
1730 		    "intprotocol=0x%02x " "%s%s",
1731 		    UGETW(res.udev->ddesc.idVendor),
1732 		    UGETW(res.udev->ddesc.idProduct),
1733 		    res.udev->ddesc.bDeviceClass,
1734 		    res.udev->ddesc.bDeviceSubClass,
1735 		    usb_get_serial(res.udev),
1736 		    UGETW(res.udev->ddesc.bcdDevice),
1737 		    (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
1738 		    iface->idesc->bInterfaceClass,
1739 		    iface->idesc->bInterfaceSubClass,
1740 		    iface->idesc->bInterfaceProtocol,
1741 		    iface->pnpinfo ? " " : "",
1742 		    iface->pnpinfo ? iface->pnpinfo : "");
1743 	} else {
1744 		if (buflen) {
1745 			buf[0] = '\0';
1746 		}
1747 		goto done;
1748 	}
1749 done:
1750 	mtx_unlock(&Giant);
1751 
1752 	return (0);
1753 }
1754 
1755 /*
1756  * The USB Transaction Translator:
1757  * ===============================
1758  *
1759  * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
1760  * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
1761  * USB transfers. To utilize bandwidth dynamically the "scatter and
1762  * gather" principle must be applied. This means that bandwidth must
1763  * be divided into equal parts of bandwidth. With regard to USB all
1764  * data is transferred in smaller packets with length
1765  * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
1766  * not a constant!
1767  *
1768  * The bandwidth scheduler which I have implemented will simply pack
1769  * the USB transfers back to back until there is no more space in the
1770  * schedule. Out of the 8 microframes which the USB 2.0 standard
1771  * provides, only 6 are available for non-HIGH-speed devices. I have
1772  * reserved the first 4 microframes for ISOCHRONOUS transfers. The
1773  * last 2 microframes I have reserved for INTERRUPT transfers. Without
1774  * this division, it is very difficult to allocate and free bandwidth
1775  * dynamically.
1776  *
1777  * NOTE about the Transaction Translator in USB HUBs:
1778  *
1779  * USB HUBs have a very simple Transaction Translator, that will
1780  * simply pipeline all the SPLIT transactions. That means that the
1781  * transactions will be executed in the order they are queued!
1782  *
1783  */
1784 
1785 /*------------------------------------------------------------------------*
1786  *	usb_intr_find_best_slot
1787  *
1788  * Return value:
1789  *   The best Transaction Translation slot for an interrupt endpoint.
1790  *------------------------------------------------------------------------*/
1791 static uint8_t
1792 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
1793     uint8_t end, uint8_t mask)
1794 {
1795 	usb_size_t min = (usb_size_t)-1;
1796 	usb_size_t sum;
1797 	uint8_t x;
1798 	uint8_t y;
1799 	uint8_t z;
1800 
1801 	y = 0;
1802 
1803 	/* find the last slot with lesser used bandwidth */
1804 
1805 	for (x = start; x < end; x++) {
1806 
1807 		sum = 0;
1808 
1809 		/* compute sum of bandwidth */
1810 		for (z = x; z < end; z++) {
1811 			if (mask & (1U << (z - x)))
1812 				sum += ptr[z];
1813 		}
1814 
1815 		/* check if the current multi-slot is more optimal */
1816 		if (min >= sum) {
1817 			min = sum;
1818 			y = x;
1819 		}
1820 
1821 		/* check if the mask is about to be shifted out */
1822 		if (mask & (1U << (end - 1 - x)))
1823 			break;
1824 	}
1825 	return (y);
1826 }
1827 
1828 /*------------------------------------------------------------------------*
1829  *	usb_hs_bandwidth_adjust
1830  *
1831  * This function will update the bandwith usage for the microframe
1832  * having index "slot" by "len" bytes. "len" can be negative.  If the
1833  * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
1834  * the "slot" argument will be replaced by the slot having least used
1835  * bandwidth. The "mask" argument is used for multi-slot allocations.
1836  *
1837  * Returns:
1838  *    The slot in which the bandwidth update was done: 0..7
1839  *------------------------------------------------------------------------*/
1840 static uint8_t
1841 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
1842     uint8_t slot, uint8_t mask)
1843 {
1844 	struct usb_bus *bus = udev->bus;
1845 	struct usb_hub *hub;
1846 	enum usb_dev_speed speed;
1847 	uint8_t x;
1848 
1849 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
1850 
1851 	speed = usbd_get_speed(udev);
1852 
1853 	switch (speed) {
1854 	case USB_SPEED_LOW:
1855 	case USB_SPEED_FULL:
1856 		if (speed == USB_SPEED_LOW) {
1857 			len *= 8;
1858 		}
1859 		/*
1860 	         * The Host Controller Driver should have
1861 	         * performed checks so that the lookup
1862 	         * below does not result in a NULL pointer
1863 	         * access.
1864 	         */
1865 
1866 		hub = udev->parent_hs_hub->hub;
1867 		if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1868 			slot = usb_intr_find_best_slot(hub->uframe_usage,
1869 			    USB_FS_ISOC_UFRAME_MAX, 6, mask);
1870 		}
1871 		for (x = slot; x < 8; x++) {
1872 			if (mask & (1U << (x - slot))) {
1873 				hub->uframe_usage[x] += len;
1874 				bus->uframe_usage[x] += len;
1875 			}
1876 		}
1877 		break;
1878 	default:
1879 		if (slot >= USB_HS_MICRO_FRAMES_MAX) {
1880 			slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
1881 			    USB_HS_MICRO_FRAMES_MAX, mask);
1882 		}
1883 		for (x = slot; x < 8; x++) {
1884 			if (mask & (1U << (x - slot))) {
1885 				bus->uframe_usage[x] += len;
1886 			}
1887 		}
1888 		break;
1889 	}
1890 	return (slot);
1891 }
1892 
1893 /*------------------------------------------------------------------------*
1894  *	usb_hs_bandwidth_alloc
1895  *
1896  * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1897  *------------------------------------------------------------------------*/
1898 void
1899 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
1900 {
1901 	struct usb_device *udev;
1902 	uint8_t slot;
1903 	uint8_t mask;
1904 	uint8_t speed;
1905 
1906 	udev = xfer->xroot->udev;
1907 
1908 	if (udev->flags.usb_mode != USB_MODE_HOST)
1909 		return;		/* not supported */
1910 
1911 	xfer->endpoint->refcount_bw++;
1912 	if (xfer->endpoint->refcount_bw != 1)
1913 		return;		/* already allocated */
1914 
1915 	speed = usbd_get_speed(udev);
1916 
1917 	switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1918 	case UE_INTERRUPT:
1919 		/* allocate a microframe slot */
1920 
1921 		mask = 0x01;
1922 		slot = usb_hs_bandwidth_adjust(udev,
1923 		    xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1924 
1925 		xfer->endpoint->usb_uframe = slot;
1926 		xfer->endpoint->usb_smask = mask << slot;
1927 
1928 		if ((speed != USB_SPEED_FULL) &&
1929 		    (speed != USB_SPEED_LOW)) {
1930 			xfer->endpoint->usb_cmask = 0x00 ;
1931 		} else {
1932 			xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
1933 		}
1934 		break;
1935 
1936 	case UE_ISOCHRONOUS:
1937 		switch (usbd_xfer_get_fps_shift(xfer)) {
1938 		case 0:
1939 			mask = 0xFF;
1940 			break;
1941 		case 1:
1942 			mask = 0x55;
1943 			break;
1944 		case 2:
1945 			mask = 0x11;
1946 			break;
1947 		default:
1948 			mask = 0x01;
1949 			break;
1950 		}
1951 
1952 		/* allocate a microframe multi-slot */
1953 
1954 		slot = usb_hs_bandwidth_adjust(udev,
1955 		    xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
1956 
1957 		xfer->endpoint->usb_uframe = slot;
1958 		xfer->endpoint->usb_cmask = 0;
1959 		xfer->endpoint->usb_smask = mask << slot;
1960 		break;
1961 
1962 	default:
1963 		xfer->endpoint->usb_uframe = 0;
1964 		xfer->endpoint->usb_cmask = 0;
1965 		xfer->endpoint->usb_smask = 0;
1966 		break;
1967 	}
1968 
1969 	DPRINTFN(11, "slot=%d, mask=0x%02x\n",
1970 	    xfer->endpoint->usb_uframe,
1971 	    xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
1972 }
1973 
1974 /*------------------------------------------------------------------------*
1975  *	usb_hs_bandwidth_free
1976  *
1977  * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
1978  *------------------------------------------------------------------------*/
1979 void
1980 usb_hs_bandwidth_free(struct usb_xfer *xfer)
1981 {
1982 	struct usb_device *udev;
1983 	uint8_t slot;
1984 	uint8_t mask;
1985 
1986 	udev = xfer->xroot->udev;
1987 
1988 	if (udev->flags.usb_mode != USB_MODE_HOST)
1989 		return;		/* not supported */
1990 
1991 	xfer->endpoint->refcount_bw--;
1992 	if (xfer->endpoint->refcount_bw != 0)
1993 		return;		/* still allocated */
1994 
1995 	switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
1996 	case UE_INTERRUPT:
1997 	case UE_ISOCHRONOUS:
1998 
1999 		slot = xfer->endpoint->usb_uframe;
2000 		mask = xfer->endpoint->usb_smask;
2001 
2002 		/* free microframe slot(s): */
2003 		usb_hs_bandwidth_adjust(udev,
2004 		    -xfer->max_frame_size, slot, mask >> slot);
2005 
2006 		DPRINTFN(11, "slot=%d, mask=0x%02x\n",
2007 		    slot, mask >> slot);
2008 
2009 		xfer->endpoint->usb_uframe = 0;
2010 		xfer->endpoint->usb_cmask = 0;
2011 		xfer->endpoint->usb_smask = 0;
2012 		break;
2013 
2014 	default:
2015 		break;
2016 	}
2017 }
2018 
2019 /*------------------------------------------------------------------------*
2020  *	usb_isoc_time_expand
2021  *
2022  * This function will expand the time counter from 7-bit to 16-bit.
2023  *
2024  * Returns:
2025  *   16-bit isochronous time counter.
2026  *------------------------------------------------------------------------*/
2027 uint16_t
2028 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
2029 {
2030 	uint16_t rem;
2031 
2032 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
2033 
2034 	rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
2035 
2036 	isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
2037 
2038 	if (isoc_time_curr < rem) {
2039 		/* the time counter wrapped around */
2040 		bus->isoc_time_last += USB_ISOC_TIME_MAX;
2041 	}
2042 	/* update the remainder */
2043 
2044 	bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
2045 	bus->isoc_time_last |= isoc_time_curr;
2046 
2047 	return (bus->isoc_time_last);
2048 }
2049 
2050 /*------------------------------------------------------------------------*
2051  *	usbd_fs_isoc_schedule_alloc_slot
2052  *
2053  * This function will allocate bandwidth for an isochronous FULL speed
2054  * transaction in the FULL speed schedule.
2055  *
2056  * Returns:
2057  *    <8: Success
2058  * Else: Error
2059  *------------------------------------------------------------------------*/
2060 #if USB_HAVE_TT_SUPPORT
2061 uint8_t
2062 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
2063 {
2064 	struct usb_xfer *xfer;
2065 	struct usb_xfer *pipe_xfer;
2066 	struct usb_bus *bus;
2067 	usb_frlength_t len;
2068 	usb_frlength_t data_len;
2069 	uint16_t delta;
2070 	uint16_t slot;
2071 	uint8_t retval;
2072 
2073 	data_len = 0;
2074 	slot = 0;
2075 
2076 	bus = isoc_xfer->xroot->bus;
2077 
2078 	TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
2079 
2080 		/* skip self, if any */
2081 
2082 		if (xfer == isoc_xfer)
2083 			continue;
2084 
2085 		/* check if this USB transfer is going through the same TT */
2086 
2087 		if (xfer->xroot->udev->parent_hs_hub !=
2088 		    isoc_xfer->xroot->udev->parent_hs_hub) {
2089 			continue;
2090 		}
2091 		if ((isoc_xfer->xroot->udev->parent_hs_hub->
2092 		    ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
2093 		    (xfer->xroot->udev->hs_port_no !=
2094 		    isoc_xfer->xroot->udev->hs_port_no)) {
2095 			continue;
2096 		}
2097 		if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
2098 			continue;
2099 
2100 		/* check if isoc_time is part of this transfer */
2101 
2102 		delta = xfer->isoc_time_complete - isoc_time;
2103 		if (delta > 0 && delta <= xfer->nframes) {
2104 			delta = xfer->nframes - delta;
2105 
2106 			len = xfer->frlengths[delta];
2107 			len += 8;
2108 			len *= 7;
2109 			len /= 6;
2110 
2111 			data_len += len;
2112 		}
2113 
2114 		/*
2115 		 * Check double buffered transfers. Only stream ID
2116 		 * equal to zero is valid here!
2117 		 */
2118 		TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
2119 		    wait_entry) {
2120 
2121 			/* skip self, if any */
2122 
2123 			if (pipe_xfer == isoc_xfer)
2124 				continue;
2125 
2126 			/* check if isoc_time is part of this transfer */
2127 
2128 			delta = pipe_xfer->isoc_time_complete - isoc_time;
2129 			if (delta > 0 && delta <= pipe_xfer->nframes) {
2130 				delta = pipe_xfer->nframes - delta;
2131 
2132 				len = pipe_xfer->frlengths[delta];
2133 				len += 8;
2134 				len *= 7;
2135 				len /= 6;
2136 
2137 				data_len += len;
2138 			}
2139 		}
2140 	}
2141 
2142 	while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2143 		data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2144 		slot++;
2145 	}
2146 
2147 	/* check for overflow */
2148 
2149 	if (slot >= USB_FS_ISOC_UFRAME_MAX)
2150 		return (255);
2151 
2152 	retval = slot;
2153 
2154 	delta = isoc_xfer->isoc_time_complete - isoc_time;
2155 	if (delta > 0 && delta <= isoc_xfer->nframes) {
2156 		delta = isoc_xfer->nframes - delta;
2157 
2158 		len = isoc_xfer->frlengths[delta];
2159 		len += 8;
2160 		len *= 7;
2161 		len /= 6;
2162 
2163 		data_len += len;
2164 	}
2165 
2166 	while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
2167 		data_len -= USB_FS_BYTES_PER_HS_UFRAME;
2168 		slot++;
2169 	}
2170 
2171 	/* check for overflow */
2172 
2173 	if (slot >= USB_FS_ISOC_UFRAME_MAX)
2174 		return (255);
2175 
2176 	return (retval);
2177 }
2178 #endif
2179 
2180 /*------------------------------------------------------------------------*
2181  *	usb_bus_port_get_device
2182  *
2183  * This function is NULL safe.
2184  *------------------------------------------------------------------------*/
2185 struct usb_device *
2186 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
2187 {
2188 	if ((bus == NULL) || (up == NULL)) {
2189 		/* be NULL safe */
2190 		return (NULL);
2191 	}
2192 	if (up->device_index == 0) {
2193 		/* nothing to do */
2194 		return (NULL);
2195 	}
2196 	return (bus->devices[up->device_index]);
2197 }
2198 
2199 /*------------------------------------------------------------------------*
2200  *	usb_bus_port_set_device
2201  *
2202  * This function is NULL safe.
2203  *------------------------------------------------------------------------*/
2204 void
2205 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
2206     struct usb_device *udev, uint8_t device_index)
2207 {
2208 	if (bus == NULL) {
2209 		/* be NULL safe */
2210 		return;
2211 	}
2212 	/*
2213 	 * There is only one case where we don't
2214 	 * have an USB port, and that is the Root Hub!
2215          */
2216 	if (up) {
2217 		if (udev) {
2218 			up->device_index = device_index;
2219 		} else {
2220 			device_index = up->device_index;
2221 			up->device_index = 0;
2222 		}
2223 	}
2224 	/*
2225 	 * Make relationships to our new device
2226 	 */
2227 	if (device_index != 0) {
2228 #if USB_HAVE_UGEN
2229 		mtx_lock(&usb_ref_lock);
2230 #endif
2231 		bus->devices[device_index] = udev;
2232 #if USB_HAVE_UGEN
2233 		mtx_unlock(&usb_ref_lock);
2234 #endif
2235 	}
2236 	/*
2237 	 * Debug print
2238 	 */
2239 	DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
2240 }
2241 
2242 /*------------------------------------------------------------------------*
2243  *	usb_needs_explore
2244  *
2245  * This functions is called when the USB event thread needs to run.
2246  *------------------------------------------------------------------------*/
2247 void
2248 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
2249 {
2250 	uint8_t do_unlock;
2251 
2252 	DPRINTF("\n");
2253 
2254 	if (bus == NULL) {
2255 		DPRINTF("No bus pointer!\n");
2256 		return;
2257 	}
2258 	if ((bus->devices == NULL) ||
2259 	    (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
2260 		DPRINTF("No root HUB\n");
2261 		return;
2262 	}
2263 	if (mtx_owned(&bus->bus_mtx)) {
2264 		do_unlock = 0;
2265 	} else {
2266 		USB_BUS_LOCK(bus);
2267 		do_unlock = 1;
2268 	}
2269 	if (do_probe) {
2270 		bus->do_probe = 1;
2271 	}
2272 	if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2273 	    &bus->explore_msg[0], &bus->explore_msg[1])) {
2274 		/* ignore */
2275 	}
2276 	if (do_unlock) {
2277 		USB_BUS_UNLOCK(bus);
2278 	}
2279 }
2280 
2281 /*------------------------------------------------------------------------*
2282  *	usb_needs_explore_all
2283  *
2284  * This function is called whenever a new driver is loaded and will
2285  * cause that all USB busses are re-explored.
2286  *------------------------------------------------------------------------*/
2287 void
2288 usb_needs_explore_all(void)
2289 {
2290 	struct usb_bus *bus;
2291 	devclass_t dc;
2292 	device_t dev;
2293 	int max;
2294 
2295 	DPRINTFN(3, "\n");
2296 
2297 	dc = usb_devclass_ptr;
2298 	if (dc == NULL) {
2299 		DPRINTFN(0, "no devclass\n");
2300 		return;
2301 	}
2302 	/*
2303 	 * Explore all USB busses in parallell.
2304 	 */
2305 	max = devclass_get_maxunit(dc);
2306 	while (max >= 0) {
2307 		dev = devclass_get_device(dc, max);
2308 		if (dev) {
2309 			bus = device_get_softc(dev);
2310 			if (bus) {
2311 				usb_needs_explore(bus, 1);
2312 			}
2313 		}
2314 		max--;
2315 	}
2316 }
2317 
2318 /*------------------------------------------------------------------------*
2319  *	usb_bus_power_update
2320  *
2321  * This function will ensure that all USB devices on the given bus are
2322  * properly suspended or resumed according to the device transfer
2323  * state.
2324  *------------------------------------------------------------------------*/
2325 #if USB_HAVE_POWERD
2326 void
2327 usb_bus_power_update(struct usb_bus *bus)
2328 {
2329 	usb_needs_explore(bus, 0 /* no probe */ );
2330 }
2331 #endif
2332 
2333 /*------------------------------------------------------------------------*
2334  *	usbd_transfer_power_ref
2335  *
2336  * This function will modify the power save reference counts and
2337  * wakeup the USB device associated with the given USB transfer, if
2338  * needed.
2339  *------------------------------------------------------------------------*/
2340 #if USB_HAVE_POWERD
2341 void
2342 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
2343 {
2344 	static const usb_power_mask_t power_mask[4] = {
2345 		[UE_CONTROL] = USB_HW_POWER_CONTROL,
2346 		[UE_BULK] = USB_HW_POWER_BULK,
2347 		[UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
2348 		[UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
2349 	};
2350 	struct usb_device *udev;
2351 	uint8_t needs_explore;
2352 	uint8_t needs_hw_power;
2353 	uint8_t xfer_type;
2354 
2355 	udev = xfer->xroot->udev;
2356 
2357 	if (udev->device_index == USB_ROOT_HUB_ADDR) {
2358 		/* no power save for root HUB */
2359 		return;
2360 	}
2361 	USB_BUS_LOCK(udev->bus);
2362 
2363 	xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2364 
2365 	udev->pwr_save.last_xfer_time = ticks;
2366 	udev->pwr_save.type_refs[xfer_type] += val;
2367 
2368 	if (xfer->flags_int.control_xfr) {
2369 		udev->pwr_save.read_refs += val;
2370 		if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2371 			/*
2372 			 * It is not allowed to suspend during a
2373 			 * control transfer:
2374 			 */
2375 			udev->pwr_save.write_refs += val;
2376 		}
2377 	} else if (USB_GET_DATA_ISREAD(xfer)) {
2378 		udev->pwr_save.read_refs += val;
2379 	} else {
2380 		udev->pwr_save.write_refs += val;
2381 	}
2382 
2383 	if (val > 0) {
2384 		if (udev->flags.self_suspended)
2385 			needs_explore = usb_peer_should_wakeup(udev);
2386 		else
2387 			needs_explore = 0;
2388 
2389 		if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
2390 			DPRINTF("Adding type %u to power state\n", xfer_type);
2391 			udev->bus->hw_power_state |= power_mask[xfer_type];
2392 			needs_hw_power = 1;
2393 		} else {
2394 			needs_hw_power = 0;
2395 		}
2396 	} else {
2397 		needs_explore = 0;
2398 		needs_hw_power = 0;
2399 	}
2400 
2401 	USB_BUS_UNLOCK(udev->bus);
2402 
2403 	if (needs_explore) {
2404 		DPRINTF("update\n");
2405 		usb_bus_power_update(udev->bus);
2406 	} else if (needs_hw_power) {
2407 		DPRINTF("needs power\n");
2408 		if (udev->bus->methods->set_hw_power != NULL) {
2409 			(udev->bus->methods->set_hw_power) (udev->bus);
2410 		}
2411 	}
2412 }
2413 #endif
2414 
2415 /*------------------------------------------------------------------------*
2416  *	usb_peer_should_wakeup
2417  *
2418  * This function returns non-zero if the current device should wake up.
2419  *------------------------------------------------------------------------*/
2420 static uint8_t
2421 usb_peer_should_wakeup(struct usb_device *udev)
2422 {
2423 	return (((udev->power_mode == USB_POWER_MODE_ON) &&
2424 	    (udev->flags.usb_mode == USB_MODE_HOST)) ||
2425 	    (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
2426 	    (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
2427 	    (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
2428 	    (udev->pwr_save.write_refs != 0) ||
2429 	    ((udev->pwr_save.read_refs != 0) &&
2430 	    (udev->flags.usb_mode == USB_MODE_HOST) &&
2431 	    (usb_peer_can_wakeup(udev) == 0)));
2432 }
2433 
2434 /*------------------------------------------------------------------------*
2435  *	usb_bus_powerd
2436  *
2437  * This function implements the USB power daemon and is called
2438  * regularly from the USB explore thread.
2439  *------------------------------------------------------------------------*/
2440 #if USB_HAVE_POWERD
2441 void
2442 usb_bus_powerd(struct usb_bus *bus)
2443 {
2444 	struct usb_device *udev;
2445 	usb_ticks_t temp;
2446 	usb_ticks_t limit;
2447 	usb_ticks_t mintime;
2448 	usb_size_t type_refs[5];
2449 	uint8_t x;
2450 
2451 	limit = usb_power_timeout;
2452 	if (limit == 0)
2453 		limit = hz;
2454 	else if (limit > 255)
2455 		limit = 255 * hz;
2456 	else
2457 		limit = limit * hz;
2458 
2459 	DPRINTF("bus=%p\n", bus);
2460 
2461 	USB_BUS_LOCK(bus);
2462 
2463 	/*
2464 	 * The root HUB device is never suspended
2465 	 * and we simply skip it.
2466 	 */
2467 	for (x = USB_ROOT_HUB_ADDR + 1;
2468 	    x != bus->devices_max; x++) {
2469 
2470 		udev = bus->devices[x];
2471 		if (udev == NULL)
2472 			continue;
2473 
2474 		temp = ticks - udev->pwr_save.last_xfer_time;
2475 
2476 		if (usb_peer_should_wakeup(udev)) {
2477 			/* check if we are suspended */
2478 			if (udev->flags.self_suspended != 0) {
2479 				USB_BUS_UNLOCK(bus);
2480 				usb_dev_resume_peer(udev);
2481 				USB_BUS_LOCK(bus);
2482 			}
2483 		} else if ((temp >= limit) &&
2484 		    (udev->flags.usb_mode == USB_MODE_HOST) &&
2485 		    (udev->flags.self_suspended == 0)) {
2486 			/* try to do suspend */
2487 
2488 			USB_BUS_UNLOCK(bus);
2489 			usb_dev_suspend_peer(udev);
2490 			USB_BUS_LOCK(bus);
2491 		}
2492 	}
2493 
2494 	/* reset counters */
2495 
2496 	mintime = (usb_ticks_t)-1;
2497 	type_refs[0] = 0;
2498 	type_refs[1] = 0;
2499 	type_refs[2] = 0;
2500 	type_refs[3] = 0;
2501 	type_refs[4] = 0;
2502 
2503 	/* Re-loop all the devices to get the actual state */
2504 
2505 	for (x = USB_ROOT_HUB_ADDR + 1;
2506 	    x != bus->devices_max; x++) {
2507 
2508 		udev = bus->devices[x];
2509 		if (udev == NULL)
2510 			continue;
2511 
2512 		/* we found a non-Root-Hub USB device */
2513 		type_refs[4] += 1;
2514 
2515 		/* "last_xfer_time" can be updated by a resume */
2516 		temp = ticks - udev->pwr_save.last_xfer_time;
2517 
2518 		/*
2519 		 * Compute minimum time since last transfer for the complete
2520 		 * bus:
2521 		 */
2522 		if (temp < mintime)
2523 			mintime = temp;
2524 
2525 		if (udev->flags.self_suspended == 0) {
2526 			type_refs[0] += udev->pwr_save.type_refs[0];
2527 			type_refs[1] += udev->pwr_save.type_refs[1];
2528 			type_refs[2] += udev->pwr_save.type_refs[2];
2529 			type_refs[3] += udev->pwr_save.type_refs[3];
2530 		}
2531 	}
2532 
2533 	if (mintime >= (usb_ticks_t)(1 * hz)) {
2534 		/* recompute power masks */
2535 		DPRINTF("Recomputing power masks\n");
2536 		bus->hw_power_state = 0;
2537 		if (type_refs[UE_CONTROL] != 0)
2538 			bus->hw_power_state |= USB_HW_POWER_CONTROL;
2539 		if (type_refs[UE_BULK] != 0)
2540 			bus->hw_power_state |= USB_HW_POWER_BULK;
2541 		if (type_refs[UE_INTERRUPT] != 0)
2542 			bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2543 		if (type_refs[UE_ISOCHRONOUS] != 0)
2544 			bus->hw_power_state |= USB_HW_POWER_ISOC;
2545 		if (type_refs[4] != 0)
2546 			bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
2547 	}
2548 	USB_BUS_UNLOCK(bus);
2549 
2550 	if (bus->methods->set_hw_power != NULL) {
2551 		/* always update hardware power! */
2552 		(bus->methods->set_hw_power) (bus);
2553 	}
2554 	return;
2555 }
2556 #endif
2557 
2558 /*------------------------------------------------------------------------*
2559  *	usb_dev_resume_peer
2560  *
2561  * This function will resume an USB peer and do the required USB
2562  * signalling to get an USB device out of the suspended state.
2563  *------------------------------------------------------------------------*/
2564 static void
2565 usb_dev_resume_peer(struct usb_device *udev)
2566 {
2567 	struct usb_bus *bus;
2568 	int err;
2569 
2570 	/* be NULL safe */
2571 	if (udev == NULL)
2572 		return;
2573 
2574 	/* check if already resumed */
2575 	if (udev->flags.self_suspended == 0)
2576 		return;
2577 
2578 	/* we need a parent HUB to do resume */
2579 	if (udev->parent_hub == NULL)
2580 		return;
2581 
2582 	DPRINTF("udev=%p\n", udev);
2583 
2584 	if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
2585 	    (udev->flags.remote_wakeup == 0)) {
2586 		/*
2587 		 * If the host did not set the remote wakeup feature, we can
2588 		 * not wake it up either!
2589 		 */
2590 		DPRINTF("remote wakeup is not set!\n");
2591 		return;
2592 	}
2593 	/* get bus pointer */
2594 	bus = udev->bus;
2595 
2596 	/* resume parent hub first */
2597 	usb_dev_resume_peer(udev->parent_hub);
2598 
2599 	/* reduce chance of instant resume failure by waiting a little bit */
2600 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2601 
2602 	if (usb_device_20_compatible(udev)) {
2603 		/* resume current port (Valid in Host and Device Mode) */
2604 		err = usbd_req_clear_port_feature(udev->parent_hub,
2605 		    NULL, udev->port_no, UHF_PORT_SUSPEND);
2606 		if (err) {
2607 			DPRINTFN(0, "Resuming port failed\n");
2608 			return;
2609 		}
2610 	} else {
2611 		/* resume current port (Valid in Host and Device Mode) */
2612 		err = usbd_req_set_port_link_state(udev->parent_hub,
2613 		    NULL, udev->port_no, UPS_PORT_LS_U0);
2614 		if (err) {
2615 			DPRINTFN(0, "Resuming port failed\n");
2616 			return;
2617 		}
2618 	}
2619 
2620 	/* resume settle time */
2621 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2622 
2623 	if (bus->methods->device_resume != NULL) {
2624 		/* resume USB device on the USB controller */
2625 		(bus->methods->device_resume) (udev);
2626 	}
2627 	USB_BUS_LOCK(bus);
2628 	/* set that this device is now resumed */
2629 	udev->flags.self_suspended = 0;
2630 #if USB_HAVE_POWERD
2631 	/* make sure that we don't go into suspend right away */
2632 	udev->pwr_save.last_xfer_time = ticks;
2633 
2634 	/* make sure the needed power masks are on */
2635 	if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
2636 		bus->hw_power_state |= USB_HW_POWER_CONTROL;
2637 	if (udev->pwr_save.type_refs[UE_BULK] != 0)
2638 		bus->hw_power_state |= USB_HW_POWER_BULK;
2639 	if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
2640 		bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
2641 	if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
2642 		bus->hw_power_state |= USB_HW_POWER_ISOC;
2643 #endif
2644 	USB_BUS_UNLOCK(bus);
2645 
2646 	if (bus->methods->set_hw_power != NULL) {
2647 		/* always update hardware power! */
2648 		(bus->methods->set_hw_power) (bus);
2649 	}
2650 
2651 	usbd_sr_lock(udev);
2652 
2653 	/* notify all sub-devices about resume */
2654 	err = usb_suspend_resume(udev, 0);
2655 
2656 	usbd_sr_unlock(udev);
2657 
2658 	/* check if peer has wakeup capability */
2659 	if (usb_peer_can_wakeup(udev)) {
2660 		/* clear remote wakeup */
2661 		err = usbd_req_clear_device_feature(udev,
2662 		    NULL, UF_DEVICE_REMOTE_WAKEUP);
2663 		if (err) {
2664 			DPRINTFN(0, "Clearing device "
2665 			    "remote wakeup failed: %s\n",
2666 			    usbd_errstr(err));
2667 		}
2668 	}
2669 }
2670 
2671 /*------------------------------------------------------------------------*
2672  *	usb_dev_suspend_peer
2673  *
2674  * This function will suspend an USB peer and do the required USB
2675  * signalling to get an USB device into the suspended state.
2676  *------------------------------------------------------------------------*/
2677 static void
2678 usb_dev_suspend_peer(struct usb_device *udev)
2679 {
2680 	struct usb_device *child;
2681 	int err;
2682 	uint8_t x;
2683 	uint8_t nports;
2684 
2685 repeat:
2686 	/* be NULL safe */
2687 	if (udev == NULL)
2688 		return;
2689 
2690 	/* check if already suspended */
2691 	if (udev->flags.self_suspended)
2692 		return;
2693 
2694 	/* we need a parent HUB to do suspend */
2695 	if (udev->parent_hub == NULL)
2696 		return;
2697 
2698 	DPRINTF("udev=%p\n", udev);
2699 
2700 	/* check if the current device is a HUB */
2701 	if (udev->hub != NULL) {
2702 		nports = udev->hub->nports;
2703 
2704 		/* check if all devices on the HUB are suspended */
2705 		for (x = 0; x != nports; x++) {
2706 			child = usb_bus_port_get_device(udev->bus,
2707 			    udev->hub->ports + x);
2708 
2709 			if (child == NULL)
2710 				continue;
2711 
2712 			if (child->flags.self_suspended)
2713 				continue;
2714 
2715 			DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
2716 			return;
2717 		}
2718 	}
2719 
2720 	if (usb_peer_can_wakeup(udev)) {
2721 		/*
2722 		 * This request needs to be done before we set
2723 		 * "udev->flags.self_suspended":
2724 		 */
2725 
2726 		/* allow device to do remote wakeup */
2727 		err = usbd_req_set_device_feature(udev,
2728 		    NULL, UF_DEVICE_REMOTE_WAKEUP);
2729 		if (err) {
2730 			DPRINTFN(0, "Setting device "
2731 			    "remote wakeup failed\n");
2732 		}
2733 	}
2734 
2735 	USB_BUS_LOCK(udev->bus);
2736 	/*
2737 	 * Checking for suspend condition and setting suspended bit
2738 	 * must be atomic!
2739 	 */
2740 	err = usb_peer_should_wakeup(udev);
2741 	if (err == 0) {
2742 		/*
2743 		 * Set that this device is suspended. This variable
2744 		 * must be set before calling USB controller suspend
2745 		 * callbacks.
2746 		 */
2747 		udev->flags.self_suspended = 1;
2748 	}
2749 	USB_BUS_UNLOCK(udev->bus);
2750 
2751 	if (err != 0) {
2752 		if (usb_peer_can_wakeup(udev)) {
2753 			/* allow device to do remote wakeup */
2754 			err = usbd_req_clear_device_feature(udev,
2755 			    NULL, UF_DEVICE_REMOTE_WAKEUP);
2756 			if (err) {
2757 				DPRINTFN(0, "Setting device "
2758 				    "remote wakeup failed\n");
2759 			}
2760 		}
2761 
2762 		if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2763 			/* resume parent HUB first */
2764 			usb_dev_resume_peer(udev->parent_hub);
2765 
2766 			/* reduce chance of instant resume failure by waiting a little bit */
2767 			usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
2768 
2769 			/* resume current port (Valid in Host and Device Mode) */
2770 			err = usbd_req_clear_port_feature(udev->parent_hub,
2771 			    NULL, udev->port_no, UHF_PORT_SUSPEND);
2772 
2773 			/* resume settle time */
2774 			usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
2775 		}
2776 		DPRINTF("Suspend was cancelled!\n");
2777 		return;
2778 	}
2779 
2780 	usbd_sr_lock(udev);
2781 
2782 	/* notify all sub-devices about suspend */
2783 	err = usb_suspend_resume(udev, 1);
2784 
2785 	usbd_sr_unlock(udev);
2786 
2787 	if (udev->bus->methods->device_suspend != NULL) {
2788 		usb_timeout_t temp;
2789 
2790 		/* suspend device on the USB controller */
2791 		(udev->bus->methods->device_suspend) (udev);
2792 
2793 		/* do DMA delay */
2794 		temp = usbd_get_dma_delay(udev);
2795 		if (temp != 0)
2796 			usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
2797 
2798 	}
2799 
2800 	if (usb_device_20_compatible(udev)) {
2801 		/* suspend current port */
2802 		err = usbd_req_set_port_feature(udev->parent_hub,
2803 		    NULL, udev->port_no, UHF_PORT_SUSPEND);
2804 		if (err) {
2805 			DPRINTFN(0, "Suspending port failed\n");
2806 			return;
2807 		}
2808 	} else {
2809 		/* suspend current port */
2810 		err = usbd_req_set_port_link_state(udev->parent_hub,
2811 		    NULL, udev->port_no, UPS_PORT_LS_U3);
2812 		if (err) {
2813 			DPRINTFN(0, "Suspending port failed\n");
2814 			return;
2815 		}
2816 	}
2817 
2818 	udev = udev->parent_hub;
2819 	goto repeat;
2820 }
2821 
2822 /*------------------------------------------------------------------------*
2823  *	usbd_set_power_mode
2824  *
2825  * This function will set the power mode, see USB_POWER_MODE_XXX for a
2826  * USB device.
2827  *------------------------------------------------------------------------*/
2828 void
2829 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
2830 {
2831 	/* filter input argument */
2832 	if ((power_mode != USB_POWER_MODE_ON) &&
2833 	    (power_mode != USB_POWER_MODE_OFF))
2834 		power_mode = USB_POWER_MODE_SAVE;
2835 
2836 	power_mode = usbd_filter_power_mode(udev, power_mode);
2837 
2838 	udev->power_mode = power_mode;	/* update copy of power mode */
2839 
2840 #if USB_HAVE_POWERD
2841 	usb_bus_power_update(udev->bus);
2842 #else
2843 	usb_needs_explore(udev->bus, 0 /* no probe */ );
2844 #endif
2845 }
2846 
2847 /*------------------------------------------------------------------------*
2848  *	usbd_filter_power_mode
2849  *
2850  * This function filters the power mode based on hardware requirements.
2851  *------------------------------------------------------------------------*/
2852 uint8_t
2853 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
2854 {
2855 	const struct usb_bus_methods *mtod;
2856 	int8_t temp;
2857 
2858 	mtod = udev->bus->methods;
2859 	temp = -1;
2860 
2861 	if (mtod->get_power_mode != NULL)
2862 		(mtod->get_power_mode) (udev, &temp);
2863 
2864 	/* check if we should not filter */
2865 	if (temp < 0)
2866 		return (power_mode);
2867 
2868 	/* use fixed power mode given by hardware driver */
2869 	return (temp);
2870 }
2871 
2872 /*------------------------------------------------------------------------*
2873  *	usbd_start_re_enumerate
2874  *
2875  * This function starts re-enumeration of the given USB device. This
2876  * function does not need to be called BUS-locked. This function does
2877  * not wait until the re-enumeration is completed.
2878  *------------------------------------------------------------------------*/
2879 void
2880 usbd_start_re_enumerate(struct usb_device *udev)
2881 {
2882 	if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2883 		udev->re_enumerate_wait = USB_RE_ENUM_START;
2884 		usb_needs_explore(udev->bus, 0);
2885 	}
2886 }
2887 
2888 /*-----------------------------------------------------------------------*
2889  *	usbd_start_set_config
2890  *
2891  * This function starts setting a USB configuration. This function
2892  * does not need to be called BUS-locked. This function does not wait
2893  * until the set USB configuratino is completed.
2894  *------------------------------------------------------------------------*/
2895 usb_error_t
2896 usbd_start_set_config(struct usb_device *udev, uint8_t index)
2897 {
2898 	if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
2899 		if (udev->curr_config_index == index) {
2900 			/* no change needed */
2901 			return (0);
2902 		}
2903 		udev->next_config_index = index;
2904 		udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
2905 		usb_needs_explore(udev->bus, 0);
2906 		return (0);
2907 	} else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
2908 		if (udev->next_config_index == index) {
2909 			/* no change needed */
2910 			return (0);
2911 		}
2912 	}
2913 	return (USB_ERR_PENDING_REQUESTS);
2914 }
2915