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