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