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