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