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