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