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