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