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