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