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