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