1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "opt_ddb.h"
28 
29 #include <sys/stdint.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/unistd.h>
41 #include <sys/callout.h>
42 #include <sys/malloc.h>
43 #include <sys/priv.h>
44 
45 #include <bus/u4b/usb.h>
46 #include <bus/u4b/usbdi.h>
47 
48 #define	USB_DEBUG_VAR usb_ctrl_debug
49 
50 #include <bus/u4b/usb_core.h>
51 #include <bus/u4b/usb_debug.h>
52 #include <bus/u4b/usb_process.h>
53 #include <bus/u4b/usb_busdma.h>
54 #include <bus/u4b/usb_dynamic.h>
55 #include <bus/u4b/usb_device.h>
56 #include <bus/u4b/usb_hub.h>
57 
58 #include <bus/u4b/usb_controller.h>
59 #include <bus/u4b/usb_bus.h>
60 #include <bus/u4b/usb_pf.h>
61 #include "usb_if.h"
62 
63 /* function prototypes  */
64 
65 static device_probe_t usb_probe;
66 static device_attach_t usb_attach;
67 static device_detach_t usb_detach;
68 static device_suspend_t usb_suspend;
69 static device_resume_t usb_resume;
70 static device_shutdown_t usb_shutdown;
71 
72 static void	usb_attach_sub(device_t, struct usb_bus *);
73 
74 /* static variables */
75 
76 #ifdef USB_DEBUG
77 static int usb_ctrl_debug = 0;
78 
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
80 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
81     "Debug level");
82 #endif
83 
84 static int usb_no_boot_wait = 0;
85 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
86 /* XXX freebsd uses CTLFLAG_RDTUN here */
87 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RW, &usb_no_boot_wait, 0,
88     "No USB device enumerate waiting at boot.");
89 
90 static int usb_no_suspend_wait = 0;
91 TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait);
92 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW,
93     &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
94 
95 static int usb_no_shutdown_wait = 0;
96 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
97 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW, &usb_no_shutdown_wait, 0,
98     "No USB device waiting at system shutdown.");
99 
100 static devclass_t usb_devclass;
101 
102 static device_method_t usb_methods[] = {
103 	DEVMETHOD(device_probe, usb_probe),
104 	DEVMETHOD(device_attach, usb_attach),
105 	DEVMETHOD(device_detach, usb_detach),
106 	DEVMETHOD(device_suspend, usb_suspend),
107 	DEVMETHOD(device_resume, usb_resume),
108 	DEVMETHOD(device_shutdown, usb_shutdown),
109 	DEVMETHOD_END
110 };
111 
112 static driver_t usb_driver = {
113 	.name = "usbus",
114 	.methods = usb_methods,
115 	.size = 0,
116 };
117 
118 /* Host Only Drivers */
119 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
120 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
121 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
122 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
123 
124 #if XXX // these are not ported yet (mpf)
125 /* Device Only Drivers */
126 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
127 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
128 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
129 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
130 
131 /* Dual Mode Drivers */
132 DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0);
133 #endif
134 /*------------------------------------------------------------------------*
135  *	usb_probe
136  *
137  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
138  *------------------------------------------------------------------------*/
139 static int
140 usb_probe(device_t dev)
141 {
142 	DPRINTF("\n");
143 	return (0);
144 }
145 
146 #if USB_HAVE_ROOT_MOUNT_HOLD
147 static void
148 usb_root_mount_rel(struct usb_bus *bus)
149 {
150 	if (bus->bus_roothold != NULL) {
151 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
152 		root_mount_rel(bus->bus_roothold);
153 		bus->bus_roothold = NULL;
154 	}
155 }
156 #endif
157 
158 /*------------------------------------------------------------------------*
159  *	usb_attach
160  *------------------------------------------------------------------------*/
161 static int
162 usb_attach(device_t dev)
163 {
164 	struct usb_bus *bus = device_get_ivars(dev);
165 
166 	DPRINTF("\n");
167 
168 	if (bus == NULL) {
169 		device_printf(dev, "USB device has no ivars\n");
170 		return (ENXIO);
171 	}
172 
173 #if USB_HAVE_ROOT_MOUNT_HOLD
174 	if (usb_no_boot_wait == 0) {
175 		/* delay vfs_mountroot until the bus is explored */
176 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
177 	}
178 #endif
179 	usb_attach_sub(dev, bus);
180 
181 	return (0);			/* return success */
182 }
183 
184 /*------------------------------------------------------------------------*
185  *	usb_detach
186  *------------------------------------------------------------------------*/
187 static int
188 usb_detach(device_t dev)
189 {
190 	struct usb_bus *bus = device_get_softc(dev);
191 
192 	DPRINTF("\n");
193 
194 	if (bus == NULL) {
195 		/* was never setup properly */
196 		return (0);
197 	}
198 	/* Stop power watchdog */
199 	usb_callout_drain(&bus->power_wdog);
200 
201 #if USB_HAVE_ROOT_MOUNT_HOLD
202 	/* Let the USB explore process detach all devices. */
203 	usb_root_mount_rel(bus);
204 #endif
205 
206 	USB_BUS_LOCK(bus);
207 
208 	/* Queue detach job */
209 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
210 	    &bus->detach_msg[0], &bus->detach_msg[1]);
211 
212 	/* Wait for detach to complete */
213 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
214 	    &bus->detach_msg[0], &bus->detach_msg[1]);
215 
216 	USB_BUS_UNLOCK(bus);
217 
218 #if USB_HAVE_PER_BUS_PROCESS
219 	/* Get rid of USB callback processes */
220 
221 	usb_proc_free(USB_BUS_GIANT_PROC(bus));
222 	usb_proc_free(USB_BUS_NON_GIANT_PROC(bus));
223 
224 	/* Get rid of USB explore process */
225 
226 	usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
227 
228 	/* Get rid of control transfer process */
229 
230 	usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
231 #endif
232 
233 #if USB_HAVE_PF
234 	usbpf_detach(bus);
235 #endif
236 	return (0);
237 }
238 
239 /*------------------------------------------------------------------------*
240  *	usb_suspend
241  *------------------------------------------------------------------------*/
242 static int
243 usb_suspend(device_t dev)
244 {
245 	struct usb_bus *bus = device_get_softc(dev);
246 
247 	DPRINTF("\n");
248 
249 	if (bus == NULL) {
250 		/* was never setup properly */
251 		return (0);
252 	}
253 
254 	USB_BUS_LOCK(bus);
255 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
256 	    &bus->suspend_msg[0], &bus->suspend_msg[1]);
257 	if (usb_no_suspend_wait == 0) {
258 		/* wait for suspend callback to be executed */
259 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
260 		    &bus->suspend_msg[0], &bus->suspend_msg[1]);
261 	}
262 	USB_BUS_UNLOCK(bus);
263 
264 	return (0);
265 }
266 
267 /*------------------------------------------------------------------------*
268  *	usb_resume
269  *------------------------------------------------------------------------*/
270 static int
271 usb_resume(device_t dev)
272 {
273 	struct usb_bus *bus = device_get_softc(dev);
274 
275 	DPRINTF("\n");
276 
277 	if (bus == NULL) {
278 		/* was never setup properly */
279 		return (0);
280 	}
281 
282 	USB_BUS_LOCK(bus);
283 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
284 	    &bus->resume_msg[0], &bus->resume_msg[1]);
285 	USB_BUS_UNLOCK(bus);
286 
287 	return (0);
288 }
289 
290 /*------------------------------------------------------------------------*
291  *	usb_bus_reset_async_locked
292  *------------------------------------------------------------------------*/
293 void
294 usb_bus_reset_async_locked(struct usb_bus *bus)
295 {
296 	USB_BUS_LOCK_ASSERT(bus);
297 
298 	DPRINTF("\n");
299 
300 	if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
301 	    bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
302 		DPRINTF("Reset already pending\n");
303 		return;
304 	}
305 
306 	device_printf(bus->parent, "Resetting controller\n");
307 
308 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
309 	    &bus->reset_msg[0], &bus->reset_msg[1]);
310 }
311 
312 /*------------------------------------------------------------------------*
313  *	usb_shutdown
314  *------------------------------------------------------------------------*/
315 static int
316 usb_shutdown(device_t dev)
317 {
318 	struct usb_bus *bus = device_get_softc(dev);
319 
320 	DPRINTF("\n");
321 
322 	if (bus == NULL) {
323 		/* was never setup properly */
324 		return (0);
325 	}
326 
327 	device_printf(bus->bdev, "Controller shutdown\n");
328 
329 	USB_BUS_LOCK(bus);
330 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
331 	    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
332 	if (usb_no_shutdown_wait == 0) {
333 		/* wait for shutdown callback to be executed */
334 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
335 		    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
336 	}
337 	USB_BUS_UNLOCK(bus);
338 
339 	device_printf(bus->bdev, "Controller shutdown complete\n");
340 
341 	return (0);
342 }
343 
344 /*------------------------------------------------------------------------*
345  *	usb_bus_explore
346  *
347  * This function is used to explore the device tree from the root.
348  *------------------------------------------------------------------------*/
349 static void
350 usb_bus_explore(struct usb_proc_msg *pm)
351 {
352 	struct usb_bus *bus;
353 	struct usb_device *udev;
354 
355 	bus = ((struct usb_bus_msg *)pm)->bus;
356 	udev = bus->devices[USB_ROOT_HUB_ADDR];
357 
358 	if (bus->no_explore != 0)
359 		return;
360 
361 	if (udev && udev->hub) {
362 
363 		if (bus->do_probe) {
364 			bus->do_probe = 0;
365 			bus->driver_added_refcount++;
366 		}
367 		if (bus->driver_added_refcount == 0) {
368 			/* avoid zero, hence that is memory default */
369 			bus->driver_added_refcount = 1;
370 		}
371 
372 #ifdef DDB
373 		/*
374 		 * The following three lines of code are only here to
375 		 * recover from DDB:
376 		 */
377 		usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
378 		usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
379 		usb_proc_rewakeup(USB_BUS_NON_GIANT_PROC(bus));
380 #endif
381 
382 		USB_BUS_UNLOCK(bus);
383 
384 #if USB_HAVE_POWERD
385 		/*
386 		 * First update the USB power state!
387 		 */
388 		usb_bus_powerd(bus);
389 #endif
390 		 /* Explore the Root USB HUB. */
391 		(udev->hub->explore) (udev);
392 		USB_BUS_LOCK(bus);
393 	}
394 #if USB_HAVE_ROOT_MOUNT_HOLD
395 	usb_root_mount_rel(bus);
396 #endif
397 }
398 
399 /*------------------------------------------------------------------------*
400  *	usb_bus_detach
401  *
402  * This function is used to detach the device tree from the root.
403  *------------------------------------------------------------------------*/
404 static void
405 usb_bus_detach(struct usb_proc_msg *pm)
406 {
407 	struct usb_bus *bus;
408 	struct usb_device *udev;
409 	device_t dev;
410 
411 	bus = ((struct usb_bus_msg *)pm)->bus;
412 	udev = bus->devices[USB_ROOT_HUB_ADDR];
413 	dev = bus->bdev;
414 	/* clear the softc */
415 	device_set_softc(dev, NULL);
416 	USB_BUS_UNLOCK(bus);
417 
418 	/* detach children first */
419 	bus_generic_detach(dev);
420 
421 	/*
422 	 * Free USB device and all subdevices, if any.
423 	 */
424 	usb_free_device(udev, 0);
425 
426 	USB_BUS_LOCK(bus);
427 	/* clear bdev variable last */
428 	bus->bdev = NULL;
429 }
430 
431 /*------------------------------------------------------------------------*
432  *	usb_bus_suspend
433  *
434  * This function is used to suspend the USB controller.
435  *------------------------------------------------------------------------*/
436 static void
437 usb_bus_suspend(struct usb_proc_msg *pm)
438 {
439 	struct usb_bus *bus;
440 	struct usb_device *udev;
441 	usb_error_t err;
442 	uint8_t do_unlock;
443 
444 	DPRINTF("\n");
445 
446 	bus = ((struct usb_bus_msg *)pm)->bus;
447 	udev = bus->devices[USB_ROOT_HUB_ADDR];
448 
449 	if (udev == NULL || bus->bdev == NULL)
450 		return;
451 
452 	USB_BUS_UNLOCK(bus);
453 
454 	/*
455 	 * We use the shutdown event here because the suspend and
456 	 * resume events are reserved for the USB port suspend and
457 	 * resume. The USB system suspend is implemented like full
458 	 * shutdown and all connected USB devices will be disconnected
459 	 * subsequently. At resume all USB devices will be
460 	 * re-connected again.
461 	 */
462 
463 	bus_generic_shutdown(bus->bdev);
464 
465 	do_unlock = usbd_enum_lock(udev);
466 
467 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
468 	if (err)
469 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
470 
471 	USB_BUS_LOCK(bus);
472 	bus->hw_power_state = 0;
473 	bus->no_explore = 1;
474 	USB_BUS_UNLOCK(bus);
475 
476 	if (bus->methods->set_hw_power != NULL)
477 		(bus->methods->set_hw_power) (bus);
478 
479 	if (bus->methods->set_hw_power_sleep != NULL)
480 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
481 
482 	if (do_unlock)
483 		usbd_enum_unlock(udev);
484 
485 	USB_BUS_LOCK(bus);
486 }
487 
488 /*------------------------------------------------------------------------*
489  *	usb_bus_resume
490  *
491  * This function is used to resume the USB controller.
492  *------------------------------------------------------------------------*/
493 static void
494 usb_bus_resume(struct usb_proc_msg *pm)
495 {
496 	struct usb_bus *bus;
497 	struct usb_device *udev;
498 	usb_error_t err;
499 	uint8_t do_unlock;
500 
501 	DPRINTF("\n");
502 
503 	bus = ((struct usb_bus_msg *)pm)->bus;
504 	udev = bus->devices[USB_ROOT_HUB_ADDR];
505 
506 	if (udev == NULL || bus->bdev == NULL)
507 		return;
508 
509 	USB_BUS_UNLOCK(bus);
510 
511 	do_unlock = usbd_enum_lock(udev);
512 #if 0
513 	DEVMETHOD(usb_take_controller, NULL);	/* dummy */
514 #endif
515 	USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
516 
517 	USB_BUS_LOCK(bus);
518  	bus->hw_power_state =
519 	  USB_HW_POWER_CONTROL |
520 	  USB_HW_POWER_BULK |
521 	  USB_HW_POWER_INTERRUPT |
522 	  USB_HW_POWER_ISOC |
523 	  USB_HW_POWER_NON_ROOT_HUB;
524 	bus->no_explore = 0;
525 	USB_BUS_UNLOCK(bus);
526 
527 	if (bus->methods->set_hw_power_sleep != NULL)
528 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
529 
530 	if (bus->methods->set_hw_power != NULL)
531 		(bus->methods->set_hw_power) (bus);
532 
533 	/* restore USB configuration to index 0 */
534 	err = usbd_set_config_index(udev, 0);
535 	if (err)
536 		device_printf(bus->bdev, "Could not configure root HUB\n");
537 
538 	/* probe and attach */
539 	err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
540 	if (err) {
541 		device_printf(bus->bdev, "Could not probe and "
542 		    "attach root HUB\n");
543 	}
544 
545 	if (do_unlock)
546 		usbd_enum_unlock(udev);
547 
548 	USB_BUS_LOCK(bus);
549 }
550 
551 /*------------------------------------------------------------------------*
552  *	usb_bus_reset
553  *
554  * This function is used to reset the USB controller.
555  *------------------------------------------------------------------------*/
556 static void
557 usb_bus_reset(struct usb_proc_msg *pm)
558 {
559 	struct usb_bus *bus;
560 
561 	DPRINTF("\n");
562 
563 	bus = ((struct usb_bus_msg *)pm)->bus;
564 
565 	if (bus->bdev == NULL || bus->no_explore != 0)
566 		return;
567 
568 	/* a suspend and resume will reset the USB controller */
569 	usb_bus_suspend(pm);
570 	usb_bus_resume(pm);
571 }
572 
573 /*------------------------------------------------------------------------*
574  *	usb_bus_shutdown
575  *
576  * This function is used to shutdown the USB controller.
577  *------------------------------------------------------------------------*/
578 static void
579 usb_bus_shutdown(struct usb_proc_msg *pm)
580 {
581 	struct usb_bus *bus;
582 	struct usb_device *udev;
583 	usb_error_t err;
584 	uint8_t do_unlock;
585 
586 	bus = ((struct usb_bus_msg *)pm)->bus;
587 	udev = bus->devices[USB_ROOT_HUB_ADDR];
588 
589 	if (udev == NULL || bus->bdev == NULL)
590 		return;
591 
592 	USB_BUS_UNLOCK(bus);
593 
594 	bus_generic_shutdown(bus->bdev);
595 
596 	do_unlock = usbd_enum_lock(udev);
597 
598 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
599 	if (err)
600 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
601 
602 	USB_BUS_LOCK(bus);
603 	bus->hw_power_state = 0;
604 	bus->no_explore = 1;
605 	USB_BUS_UNLOCK(bus);
606 
607 	if (bus->methods->set_hw_power != NULL)
608 		(bus->methods->set_hw_power) (bus);
609 
610 	if (bus->methods->set_hw_power_sleep != NULL)
611 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
612 
613 	if (do_unlock)
614 		usbd_enum_unlock(udev);
615 
616 	USB_BUS_LOCK(bus);
617 }
618 
619 static void
620 usb_power_wdog(void *arg)
621 {
622 	struct usb_bus *bus = arg;
623 
624 	USB_BUS_LOCK_ASSERT(bus);
625 
626 	usb_callout_reset(&bus->power_wdog,
627 	    4 * hz, usb_power_wdog, arg);
628 
629 #ifdef DDB
630 	/*
631 	 * The following line of code is only here to recover from
632 	 * DDB:
633 	 */
634 	usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus));	/* recover from DDB */
635 #endif
636 
637 #if USB_HAVE_POWERD
638 	USB_BUS_UNLOCK(bus);
639 
640 	usb_bus_power_update(bus);
641 
642 	USB_BUS_LOCK(bus);
643 #endif
644 }
645 
646 /*------------------------------------------------------------------------*
647  *	usb_bus_attach
648  *
649  * This function attaches USB in context of the explore thread.
650  *------------------------------------------------------------------------*/
651 static void
652 usb_bus_attach(struct usb_proc_msg *pm)
653 {
654 	struct usb_bus *bus;
655 	struct usb_device *child;
656 	device_t dev;
657 	usb_error_t err;
658 	enum usb_dev_speed speed;
659 
660 	bus = ((struct usb_bus_msg *)pm)->bus;
661 	dev = bus->bdev;
662 
663 	DPRINTF("\n");
664 
665 	switch (bus->usbrev) {
666 	case USB_REV_1_0:
667 		speed = USB_SPEED_FULL;
668 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
669 		break;
670 
671 	case USB_REV_1_1:
672 		speed = USB_SPEED_FULL;
673 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
674 		break;
675 
676 	case USB_REV_2_0:
677 		speed = USB_SPEED_HIGH;
678 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
679 		break;
680 
681 	case USB_REV_2_5:
682 		speed = USB_SPEED_VARIABLE;
683 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
684 		break;
685 
686 	case USB_REV_3_0:
687 		speed = USB_SPEED_SUPER;
688 		device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
689 		break;
690 
691 	default:
692 		device_printf(bus->bdev, "Unsupported USB revision\n");
693 #if USB_HAVE_ROOT_MOUNT_HOLD
694 		usb_root_mount_rel(bus);
695 #endif
696 		return;
697 	}
698 
699 	/* default power_mask value */
700 	bus->hw_power_state =
701 	  USB_HW_POWER_CONTROL |
702 	  USB_HW_POWER_BULK |
703 	  USB_HW_POWER_INTERRUPT |
704 	  USB_HW_POWER_ISOC |
705 	  USB_HW_POWER_NON_ROOT_HUB;
706 
707 	USB_BUS_UNLOCK(bus);
708 
709 	/* make sure power is set at least once */
710 
711 	if (bus->methods->set_hw_power != NULL) {
712 		(bus->methods->set_hw_power) (bus);
713 	}
714 
715 	/* allocate the Root USB device */
716 
717 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
718 	    speed, USB_MODE_HOST);
719 	if (child) {
720 		err = usb_probe_and_attach(child,
721 		    USB_IFACE_INDEX_ANY);
722 		if (!err) {
723 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
724 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
725 				err = USB_ERR_NO_ROOT_HUB;
726 			}
727 		}
728 	} else {
729 		err = USB_ERR_NOMEM;
730 	}
731 
732 	USB_BUS_LOCK(bus);
733 
734 	if (err) {
735 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
736 		    usbd_errstr(err));
737 #if USB_HAVE_ROOT_MOUNT_HOLD
738 		usb_root_mount_rel(bus);
739 #endif
740 	}
741 
742 	/* set softc - we are ready */
743 	device_set_softc(dev, bus);
744 
745 	/* start watchdog */
746 	usb_power_wdog(bus);
747 }
748 
749 /*------------------------------------------------------------------------*
750  *	usb_attach_sub
751  *
752  * This function creates a thread which runs the USB attach code.
753  *------------------------------------------------------------------------*/
754 static void
755 usb_attach_sub(device_t dev, struct usb_bus *bus)
756 {
757 	if (usb_devclass_ptr == NULL)
758 		usb_devclass_ptr = devclass_find("usbus");
759 
760 #if USB_HAVE_PF
761 	usbpf_attach(bus);
762 #endif
763 	/* Initialise USB process messages */
764 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
765 	bus->explore_msg[0].bus = bus;
766 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
767 	bus->explore_msg[1].bus = bus;
768 
769 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
770 	bus->detach_msg[0].bus = bus;
771 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
772 	bus->detach_msg[1].bus = bus;
773 
774 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
775 	bus->attach_msg[0].bus = bus;
776 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
777 	bus->attach_msg[1].bus = bus;
778 
779 	bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
780 	bus->suspend_msg[0].bus = bus;
781 	bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
782 	bus->suspend_msg[1].bus = bus;
783 
784 	bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
785 	bus->resume_msg[0].bus = bus;
786 	bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
787 	bus->resume_msg[1].bus = bus;
788 
789 	bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
790 	bus->reset_msg[0].bus = bus;
791 	bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
792 	bus->reset_msg[1].bus = bus;
793 
794 	bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
795 	bus->shutdown_msg[0].bus = bus;
796 	bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
797 	bus->shutdown_msg[1].bus = bus;
798 
799 #if USB_HAVE_PER_BUS_PROCESS
800 	/* Create USB explore and callback processes */
801 
802 	if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
803 	    &bus->bus_lock, device_get_nameunit(dev), USB_PRI_MED)) {
804 		device_printf(dev, "WARNING: Creation of USB Giant "
805 		    "callback process failed.\n");
806 	} else if (usb_proc_create(USB_BUS_NON_GIANT_PROC(bus),
807 	    &bus->bus_lock, device_get_nameunit(dev), USB_PRI_HIGH)) {
808 		device_printf(dev, "WARNING: Creation of USB non-Giant "
809 		    "callback process failed.\n");
810 	} else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
811 	    &bus->bus_lock, device_get_nameunit(dev), USB_PRI_MED)) {
812 		device_printf(dev, "WARNING: Creation of USB explore "
813 		    "process failed.\n");
814 	} else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
815 	    &bus->bus_lock, device_get_nameunit(dev), USB_PRI_MED)) {
816 		device_printf(dev, "WARNING: Creation of USB control transfer "
817 		    "process failed.\n");
818 	} else
819 #endif
820 	{
821 		/* Get final attach going */
822 		USB_BUS_LOCK(bus);
823 		usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
824 		    &bus->attach_msg[0], &bus->attach_msg[1]);
825 		USB_BUS_UNLOCK(bus);
826 
827 		/* Do initial explore */
828 		usb_needs_explore(bus, 1);
829 	}
830 }
831 
832 SYSUNINIT(usb_bus_unload, SI_SUB_CONFIGURE, SI_ORDER_ANY, usb_bus_unload, NULL);
833 
834 /*------------------------------------------------------------------------*
835  *	usb_bus_mem_flush_all_cb
836  *------------------------------------------------------------------------*/
837 #if USB_HAVE_BUSDMA
838 static void
839 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
840     struct usb_page *pg, usb_size_t size, usb_size_t align)
841 {
842 	usb_pc_cpu_flush(pc);
843 }
844 #endif
845 
846 /*------------------------------------------------------------------------*
847  *	usb_bus_mem_flush_all - factored out code
848  *------------------------------------------------------------------------*/
849 #if USB_HAVE_BUSDMA
850 void
851 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
852 {
853 	if (cb) {
854 		cb(bus, &usb_bus_mem_flush_all_cb);
855 	}
856 }
857 #endif
858 
859 /*------------------------------------------------------------------------*
860  *	usb_bus_mem_alloc_all_cb
861  *------------------------------------------------------------------------*/
862 #if USB_HAVE_BUSDMA
863 static void
864 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
865     struct usb_page *pg, usb_size_t size, usb_size_t align)
866 {
867 	/* need to initialize the page cache */
868 	pc->tag_parent = bus->dma_parent_tag;
869 
870 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
871 		bus->alloc_failed = 1;
872 	}
873 }
874 #endif
875 
876 /*------------------------------------------------------------------------*
877  *	usb_bus_mem_alloc_all - factored out code
878  *
879  * Returns:
880  *    0: Success
881  * Else: Failure
882  *------------------------------------------------------------------------*/
883 uint8_t
884 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
885     usb_bus_mem_cb_t *cb)
886 {
887 	bus->alloc_failed = 0;
888 
889 	lockinit(&bus->bus_lock, "USB bus mem", 0, LK_CANRECURSE);
890 
891 	usb_callout_init_mtx(&bus->power_wdog,
892 	    &bus->bus_lock, 0);
893 
894 	TAILQ_INIT(&bus->intr_q.head);
895 
896 #if USB_HAVE_BUSDMA
897 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
898 	    dmat, &bus->bus_lock, NULL, 32, USB_BUS_DMA_TAG_MAX);
899 #endif
900 	if ((bus->devices_max > USB_MAX_DEVICES) ||
901 	    (bus->devices_max < USB_MIN_DEVICES) ||
902 	    (bus->devices == NULL)) {
903 		DPRINTFN(0, "Devices field has not been "
904 		    "initialised properly\n");
905 		bus->alloc_failed = 1;		/* failure */
906 	}
907 #if USB_HAVE_BUSDMA
908 	if (cb) {
909 		cb(bus, &usb_bus_mem_alloc_all_cb);
910 	}
911 #endif
912 	if (bus->alloc_failed) {
913 		usb_bus_mem_free_all(bus, cb);
914 	}
915 	return (bus->alloc_failed);
916 }
917 
918 /*------------------------------------------------------------------------*
919  *	usb_bus_mem_free_all_cb
920  *------------------------------------------------------------------------*/
921 #if USB_HAVE_BUSDMA
922 static void
923 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
924     struct usb_page *pg, usb_size_t size, usb_size_t align)
925 {
926 	usb_pc_free_mem(pc);
927 }
928 #endif
929 
930 /*------------------------------------------------------------------------*
931  *	usb_bus_mem_free_all - factored out code
932  *------------------------------------------------------------------------*/
933 void
934 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
935 {
936 #if USB_HAVE_BUSDMA
937 	if (cb) {
938 		cb(bus, &usb_bus_mem_free_all_cb);
939 	}
940 	usb_dma_tag_unsetup(bus->dma_parent_tag);
941 #endif
942 
943 	lockuninit(&bus->bus_lock);
944 }
945 
946 /* convenience wrappers */
947 void
948 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
949 {
950 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
951 }
952 
953 void	*
954 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
955 {
956 	return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
957 }
958 
959 void
960 usb_proc_explore_lock(struct usb_device *udev)
961 {
962 	USB_BUS_LOCK(udev->bus);
963 }
964 
965 void
966 usb_proc_explore_unlock(struct usb_device *udev)
967 {
968 	USB_BUS_UNLOCK(udev->bus);
969 }
970