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