xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision 02f728afc99d74f9b4acf88eb863acb659539394)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
27f0c078e6SAndrew Thompson #include "opt_ddb.h"
28f0c078e6SAndrew Thompson 
29ed6d949aSAndrew Thompson #include <sys/stdint.h>
30ed6d949aSAndrew Thompson #include <sys/stddef.h>
31ed6d949aSAndrew Thompson #include <sys/param.h>
32ed6d949aSAndrew Thompson #include <sys/queue.h>
33ed6d949aSAndrew Thompson #include <sys/types.h>
34ed6d949aSAndrew Thompson #include <sys/systm.h>
35ed6d949aSAndrew Thompson #include <sys/kernel.h>
36ed6d949aSAndrew Thompson #include <sys/bus.h>
37ed6d949aSAndrew Thompson #include <sys/module.h>
38ed6d949aSAndrew Thompson #include <sys/lock.h>
39ed6d949aSAndrew Thompson #include <sys/mutex.h>
40ed6d949aSAndrew Thompson #include <sys/condvar.h>
41ed6d949aSAndrew Thompson #include <sys/sysctl.h>
42ed6d949aSAndrew Thompson #include <sys/sx.h>
43ed6d949aSAndrew Thompson #include <sys/unistd.h>
44ed6d949aSAndrew Thompson #include <sys/callout.h>
45ed6d949aSAndrew Thompson #include <sys/malloc.h>
46ed6d949aSAndrew Thompson #include <sys/priv.h>
47ed6d949aSAndrew Thompson 
4802ac6454SAndrew Thompson #include <dev/usb/usb.h>
49ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
5002ac6454SAndrew Thompson 
51a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_ctrl_debug
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6318ec6525SWeongyo Jeong #include <dev/usb/usb_pf.h>
642e141748SHans Petter Selasky #include "usb_if.h"
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson /* function prototypes  */
6702ac6454SAndrew Thompson 
68a593f6b8SAndrew Thompson static device_probe_t usb_probe;
69a593f6b8SAndrew Thompson static device_attach_t usb_attach;
70a593f6b8SAndrew Thompson static device_detach_t usb_detach;
712e141748SHans Petter Selasky static device_suspend_t usb_suspend;
722e141748SHans Petter Selasky static device_resume_t usb_resume;
732e141748SHans Petter Selasky static device_shutdown_t usb_shutdown;
7402ac6454SAndrew Thompson 
75a593f6b8SAndrew Thompson static void	usb_attach_sub(device_t, struct usb_bus *);
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson /* static variables */
7802ac6454SAndrew Thompson 
79ed6d949aSAndrew Thompson #ifdef USB_DEBUG
80a593f6b8SAndrew Thompson static int usb_ctrl_debug = 0;
8102ac6454SAndrew Thompson 
826472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
83a593f6b8SAndrew Thompson SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
8402ac6454SAndrew Thompson     "Debug level");
8502ac6454SAndrew Thompson #endif
8602ac6454SAndrew Thompson 
87a0c61406SAlfred Perlstein static int usb_no_boot_wait = 0;
88a0c61406SAlfred Perlstein TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
89a0c61406SAlfred Perlstein SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
905ed294aeSHans Petter Selasky     "No USB device enumerate waiting at boot.");
915ed294aeSHans Petter Selasky 
92*02f728afSHans Petter Selasky static int usb_no_suspend_wait = 0;
93*02f728afSHans Petter Selasky TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait);
94*02f728afSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN,
95*02f728afSHans Petter Selasky     &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
96*02f728afSHans Petter Selasky 
975ed294aeSHans Petter Selasky static int usb_no_shutdown_wait = 0;
985ed294aeSHans Petter Selasky TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
99*02f728afSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN,
100*02f728afSHans Petter Selasky     &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
101a0c61406SAlfred Perlstein 
102a593f6b8SAndrew Thompson static devclass_t usb_devclass;
10302ac6454SAndrew Thompson 
104a593f6b8SAndrew Thompson static device_method_t usb_methods[] = {
105a593f6b8SAndrew Thompson 	DEVMETHOD(device_probe, usb_probe),
106a593f6b8SAndrew Thompson 	DEVMETHOD(device_attach, usb_attach),
107a593f6b8SAndrew Thompson 	DEVMETHOD(device_detach, usb_detach),
1082e141748SHans Petter Selasky 	DEVMETHOD(device_suspend, usb_suspend),
1092e141748SHans Petter Selasky 	DEVMETHOD(device_resume, usb_resume),
1102e141748SHans Petter Selasky 	DEVMETHOD(device_shutdown, usb_shutdown),
11102ac6454SAndrew Thompson 	{0, 0}
11202ac6454SAndrew Thompson };
11302ac6454SAndrew Thompson 
114a593f6b8SAndrew Thompson static driver_t usb_driver = {
11502ac6454SAndrew Thompson 	.name = "usbus",
116a593f6b8SAndrew Thompson 	.methods = usb_methods,
11702ac6454SAndrew Thompson 	.size = 0,
11802ac6454SAndrew Thompson };
11902ac6454SAndrew Thompson 
120864bc412SHans Petter Selasky /* Host Only Drivers */
121a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
122a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
123a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
124963169b4SHans Petter Selasky DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
125864bc412SHans Petter Selasky 
126864bc412SHans Petter Selasky /* Device Only Drivers */
127a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
128864bc412SHans Petter Selasky DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
129a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
130d42cc946SOleksandr Tymoshenko DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
133a593f6b8SAndrew Thompson  *	usb_probe
13402ac6454SAndrew Thompson  *
13502ac6454SAndrew Thompson  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
13602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
13702ac6454SAndrew Thompson static int
138a593f6b8SAndrew Thompson usb_probe(device_t dev)
13902ac6454SAndrew Thompson {
14002ac6454SAndrew Thompson 	DPRINTF("\n");
14102ac6454SAndrew Thompson 	return (0);
14202ac6454SAndrew Thompson }
14302ac6454SAndrew Thompson 
1443df007ceSHans Petter Selasky static void
1453df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus)
1463df007ceSHans Petter Selasky {
1473df007ceSHans Petter Selasky 	if (bus->bus_roothold != NULL) {
1483df007ceSHans Petter Selasky 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
1493df007ceSHans Petter Selasky 		root_mount_rel(bus->bus_roothold);
1503df007ceSHans Petter Selasky 		bus->bus_roothold = NULL;
1513df007ceSHans Petter Selasky 	}
1523df007ceSHans Petter Selasky }
1533df007ceSHans Petter Selasky 
15402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
155a593f6b8SAndrew Thompson  *	usb_attach
15602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
15702ac6454SAndrew Thompson static int
158a593f6b8SAndrew Thompson usb_attach(device_t dev)
15902ac6454SAndrew Thompson {
160760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_ivars(dev);
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson 	DPRINTF("\n");
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	if (bus == NULL) {
165767cb2e2SAndrew Thompson 		device_printf(dev, "USB device has no ivars\n");
16602ac6454SAndrew Thompson 		return (ENXIO);
16702ac6454SAndrew Thompson 	}
16802ac6454SAndrew Thompson 
169a0c61406SAlfred Perlstein 	if (usb_no_boot_wait == 0) {
17002ac6454SAndrew Thompson 		/* delay vfs_mountroot until the bus is explored */
171853a10a5SAndrew Thompson 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
172a0c61406SAlfred Perlstein 	}
17302ac6454SAndrew Thompson 
174a593f6b8SAndrew Thompson 	usb_attach_sub(dev, bus);
17534b48722SAlfred Perlstein 
17602ac6454SAndrew Thompson 	return (0);			/* return success */
17702ac6454SAndrew Thompson }
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
180a593f6b8SAndrew Thompson  *	usb_detach
18102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
18202ac6454SAndrew Thompson static int
183a593f6b8SAndrew Thompson usb_detach(device_t dev)
18402ac6454SAndrew Thompson {
185760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_softc(dev);
18602ac6454SAndrew Thompson 
18702ac6454SAndrew Thompson 	DPRINTF("\n");
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson 	if (bus == NULL) {
19002ac6454SAndrew Thompson 		/* was never setup properly */
19102ac6454SAndrew Thompson 		return (0);
19202ac6454SAndrew Thompson 	}
19302ac6454SAndrew Thompson 	/* Stop power watchdog */
194a593f6b8SAndrew Thompson 	usb_callout_drain(&bus->power_wdog);
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson 	/* Let the USB explore process detach all devices. */
1973df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
20002ac6454SAndrew Thompson 
2012e141748SHans Petter Selasky 	/* Queue detach job */
2022e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
2032e141748SHans Petter Selasky 	    &bus->detach_msg[0], &bus->detach_msg[1]);
2042e141748SHans Petter Selasky 
2052e141748SHans Petter Selasky 	/* Wait for detach to complete */
206a593f6b8SAndrew Thompson 	usb_proc_mwait(&bus->explore_proc,
20702ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1]);
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	/* Get rid of USB callback processes */
21202ac6454SAndrew Thompson 
213a593f6b8SAndrew Thompson 	usb_proc_free(&bus->giant_callback_proc);
214a593f6b8SAndrew Thompson 	usb_proc_free(&bus->non_giant_callback_proc);
21502ac6454SAndrew Thompson 
21602ac6454SAndrew Thompson 	/* Get rid of USB explore process */
21702ac6454SAndrew Thompson 
218a593f6b8SAndrew Thompson 	usb_proc_free(&bus->explore_proc);
21902ac6454SAndrew Thompson 
220672c9965SAndrew Thompson 	/* Get rid of control transfer process */
221672c9965SAndrew Thompson 
222a593f6b8SAndrew Thompson 	usb_proc_free(&bus->control_xfer_proc);
223672c9965SAndrew Thompson 
2248be09334SHans Petter Selasky #if USB_HAVE_PF
225fe1c24e3SWeongyo Jeong 	usbpf_detach(bus);
2268be09334SHans Petter Selasky #endif
22702ac6454SAndrew Thompson 	return (0);
22802ac6454SAndrew Thompson }
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2312e141748SHans Petter Selasky  *	usb_suspend
2322e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2332e141748SHans Petter Selasky static int
2342e141748SHans Petter Selasky usb_suspend(device_t dev)
2352e141748SHans Petter Selasky {
2362e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2372e141748SHans Petter Selasky 
2382e141748SHans Petter Selasky 	DPRINTF("\n");
2392e141748SHans Petter Selasky 
2402e141748SHans Petter Selasky 	if (bus == NULL) {
2412e141748SHans Petter Selasky 		/* was never setup properly */
2422e141748SHans Petter Selasky 		return (0);
2432e141748SHans Petter Selasky 	}
2442e141748SHans Petter Selasky 
2452e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2462e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
2472e141748SHans Petter Selasky 	    &bus->suspend_msg[0], &bus->suspend_msg[1]);
248*02f728afSHans Petter Selasky 	if (usb_no_suspend_wait == 0) {
249*02f728afSHans Petter Selasky 		/* wait for suspend callback to be executed */
250*02f728afSHans Petter Selasky 		usb_proc_mwait(&bus->explore_proc,
251*02f728afSHans Petter Selasky 		    &bus->suspend_msg[0], &bus->suspend_msg[1]);
252*02f728afSHans Petter Selasky 	}
2532e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2542e141748SHans Petter Selasky 
2552e141748SHans Petter Selasky 	return (0);
2562e141748SHans Petter Selasky }
2572e141748SHans Petter Selasky 
2582e141748SHans Petter Selasky /*------------------------------------------------------------------------*
2592e141748SHans Petter Selasky  *	usb_resume
2602e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2612e141748SHans Petter Selasky static int
2622e141748SHans Petter Selasky usb_resume(device_t dev)
2632e141748SHans Petter Selasky {
2642e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2652e141748SHans Petter Selasky 
2662e141748SHans Petter Selasky 	DPRINTF("\n");
2672e141748SHans Petter Selasky 
2682e141748SHans Petter Selasky 	if (bus == NULL) {
2692e141748SHans Petter Selasky 		/* was never setup properly */
2702e141748SHans Petter Selasky 		return (0);
2712e141748SHans Petter Selasky 	}
2722e141748SHans Petter Selasky 
2732e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2742e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
2752e141748SHans Petter Selasky 	    &bus->resume_msg[0], &bus->resume_msg[1]);
2762e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2772e141748SHans Petter Selasky 
2782e141748SHans Petter Selasky 	return (0);
2792e141748SHans Petter Selasky }
2802e141748SHans Petter Selasky 
2812e141748SHans Petter Selasky /*------------------------------------------------------------------------*
2822e141748SHans Petter Selasky  *	usb_shutdown
2832e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2842e141748SHans Petter Selasky static int
2852e141748SHans Petter Selasky usb_shutdown(device_t dev)
2862e141748SHans Petter Selasky {
2872e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2882e141748SHans Petter Selasky 
2892e141748SHans Petter Selasky 	DPRINTF("\n");
2902e141748SHans Petter Selasky 
2912e141748SHans Petter Selasky 	if (bus == NULL) {
2922e141748SHans Petter Selasky 		/* was never setup properly */
2932e141748SHans Petter Selasky 		return (0);
2942e141748SHans Petter Selasky 	}
2952e141748SHans Petter Selasky 
2965ed294aeSHans Petter Selasky 	device_printf(bus->bdev, "Controller shutdown\n");
2975ed294aeSHans Petter Selasky 
2982e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2992e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
3002e141748SHans Petter Selasky 	    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
3015ed294aeSHans Petter Selasky 	if (usb_no_shutdown_wait == 0) {
3025ed294aeSHans Petter Selasky 		/* wait for shutdown callback to be executed */
3035ed294aeSHans Petter Selasky 		usb_proc_mwait(&bus->explore_proc,
3045ed294aeSHans Petter Selasky 		    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
3055ed294aeSHans Petter Selasky 	}
3062e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
3072e141748SHans Petter Selasky 
3085ed294aeSHans Petter Selasky 	device_printf(bus->bdev, "Controller shutdown complete\n");
3095ed294aeSHans Petter Selasky 
3102e141748SHans Petter Selasky 	return (0);
3112e141748SHans Petter Selasky }
3122e141748SHans Petter Selasky 
3132e141748SHans Petter Selasky /*------------------------------------------------------------------------*
314a593f6b8SAndrew Thompson  *	usb_bus_explore
31502ac6454SAndrew Thompson  *
31602ac6454SAndrew Thompson  * This function is used to explore the device tree from the root.
31702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
31802ac6454SAndrew Thompson static void
319a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm)
32002ac6454SAndrew Thompson {
321760bc48eSAndrew Thompson 	struct usb_bus *bus;
322760bc48eSAndrew Thompson 	struct usb_device *udev;
32302ac6454SAndrew Thompson 
324760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
32502ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
32602ac6454SAndrew Thompson 
3272e141748SHans Petter Selasky 	if (bus->no_explore != 0)
3282e141748SHans Petter Selasky 		return;
3292e141748SHans Petter Selasky 
33002ac6454SAndrew Thompson 	if (udev && udev->hub) {
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 		if (bus->do_probe) {
33302ac6454SAndrew Thompson 			bus->do_probe = 0;
33402ac6454SAndrew Thompson 			bus->driver_added_refcount++;
33502ac6454SAndrew Thompson 		}
33602ac6454SAndrew Thompson 		if (bus->driver_added_refcount == 0) {
33702ac6454SAndrew Thompson 			/* avoid zero, hence that is memory default */
33802ac6454SAndrew Thompson 			bus->driver_added_refcount = 1;
33902ac6454SAndrew Thompson 		}
34002ac6454SAndrew Thompson 
341f0c078e6SAndrew Thompson #ifdef DDB
34234b48722SAlfred Perlstein 		/*
34334b48722SAlfred Perlstein 		 * The following three lines of code are only here to
34434b48722SAlfred Perlstein 		 * recover from DDB:
34534b48722SAlfred Perlstein 		 */
34634b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->control_xfer_proc);
34734b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->giant_callback_proc);
34834b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->non_giant_callback_proc);
349f0c078e6SAndrew Thompson #endif
35034b48722SAlfred Perlstein 
35134b48722SAlfred Perlstein 		USB_BUS_UNLOCK(bus);
352a56fe095SJohn Baldwin 
353e727a16cSAndrew Thompson #if USB_HAVE_POWERD
35402ac6454SAndrew Thompson 		/*
35502ac6454SAndrew Thompson 		 * First update the USB power state!
35602ac6454SAndrew Thompson 		 */
357a593f6b8SAndrew Thompson 		usb_bus_powerd(bus);
358e727a16cSAndrew Thompson #endif
35934b48722SAlfred Perlstein 		 /* Explore the Root USB HUB. */
36002ac6454SAndrew Thompson 		(udev->hub->explore) (udev);
36102ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
36202ac6454SAndrew Thompson 	}
3633df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
36402ac6454SAndrew Thompson }
36502ac6454SAndrew Thompson 
36602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
367a593f6b8SAndrew Thompson  *	usb_bus_detach
36802ac6454SAndrew Thompson  *
36902ac6454SAndrew Thompson  * This function is used to detach the device tree from the root.
37002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
37102ac6454SAndrew Thompson static void
372a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm)
37302ac6454SAndrew Thompson {
374760bc48eSAndrew Thompson 	struct usb_bus *bus;
375760bc48eSAndrew Thompson 	struct usb_device *udev;
37602ac6454SAndrew Thompson 	device_t dev;
37702ac6454SAndrew Thompson 
378760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
37902ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
38002ac6454SAndrew Thompson 	dev = bus->bdev;
38102ac6454SAndrew Thompson 	/* clear the softc */
38202ac6454SAndrew Thompson 	device_set_softc(dev, NULL);
38302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	/* detach children first */
38634b48722SAlfred Perlstein 	mtx_lock(&Giant);
38702ac6454SAndrew Thompson 	bus_generic_detach(dev);
38834b48722SAlfred Perlstein 	mtx_unlock(&Giant);
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 	/*
391d88688c7SAndrew Thompson 	 * Free USB device and all subdevices, if any.
39202ac6454SAndrew Thompson 	 */
393d88688c7SAndrew Thompson 	usb_free_device(udev, 0);
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
39602ac6454SAndrew Thompson 	/* clear bdev variable last */
39702ac6454SAndrew Thompson 	bus->bdev = NULL;
39802ac6454SAndrew Thompson }
39902ac6454SAndrew Thompson 
4002e141748SHans Petter Selasky /*------------------------------------------------------------------------*
4012e141748SHans Petter Selasky  *	usb_bus_suspend
4022e141748SHans Petter Selasky  *
4032e141748SHans Petter Selasky  * This function is used to suspend the USB contoller.
4042e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
4052e141748SHans Petter Selasky static void
4062e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm)
4072e141748SHans Petter Selasky {
4082e141748SHans Petter Selasky 	struct usb_bus *bus;
4092e141748SHans Petter Selasky 	struct usb_device *udev;
4102e141748SHans Petter Selasky 	usb_error_t err;
4112e141748SHans Petter Selasky 
4122e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
4132e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
4142e141748SHans Petter Selasky 
4152e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
4162e141748SHans Petter Selasky 		return;
4172e141748SHans Petter Selasky 
4186bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4196bd3e535SHans Petter Selasky 
420*02f728afSHans Petter Selasky 	/*
421*02f728afSHans Petter Selasky 	 * We use the shutdown event here because the suspend and
422*02f728afSHans Petter Selasky 	 * resume events are reserved for the USB port suspend and
423*02f728afSHans Petter Selasky 	 * resume. The USB system suspend is implemented like full
424*02f728afSHans Petter Selasky 	 * shutdown and all connected USB devices will be disconnected
425*02f728afSHans Petter Selasky 	 * subsequently. At resume all USB devices will be
426*02f728afSHans Petter Selasky 	 * re-connected again.
427*02f728afSHans Petter Selasky 	 */
428*02f728afSHans Petter Selasky 
4292e141748SHans Petter Selasky 	bus_generic_shutdown(bus->bdev);
4302e141748SHans Petter Selasky 
4312e141748SHans Petter Selasky 	usbd_enum_lock(udev);
4322e141748SHans Petter Selasky 
4332e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
4342e141748SHans Petter Selasky 	if (err)
4352e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
4362e141748SHans Petter Selasky 
4372e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
4382e141748SHans Petter Selasky 	bus->hw_power_state = 0;
4392e141748SHans Petter Selasky 	bus->no_explore = 1;
4402e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4412e141748SHans Petter Selasky 
4422e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
4432e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
4442e141748SHans Petter Selasky 
4452e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
4462e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
4472e141748SHans Petter Selasky 
4482e141748SHans Petter Selasky 	usbd_enum_unlock(udev);
4496bd3e535SHans Petter Selasky 
4506bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
4512e141748SHans Petter Selasky }
4522e141748SHans Petter Selasky 
4532e141748SHans Petter Selasky /*------------------------------------------------------------------------*
4542e141748SHans Petter Selasky  *	usb_bus_resume
4552e141748SHans Petter Selasky  *
4562e141748SHans Petter Selasky  * This function is used to resume the USB contoller.
4572e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
4582e141748SHans Petter Selasky static void
4592e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm)
4602e141748SHans Petter Selasky {
4612e141748SHans Petter Selasky 	struct usb_bus *bus;
4622e141748SHans Petter Selasky 	struct usb_device *udev;
4632e141748SHans Petter Selasky 	usb_error_t err;
4642e141748SHans Petter Selasky 
4652e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
4662e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
4672e141748SHans Petter Selasky 
4682e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
4692e141748SHans Petter Selasky 		return;
4702e141748SHans Petter Selasky 
4716bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4726bd3e535SHans Petter Selasky 
4732e141748SHans Petter Selasky 	usbd_enum_lock(udev);
4742e141748SHans Petter Selasky #if 0
4752e141748SHans Petter Selasky 	DEVMETHOD(usb_take_controller, NULL);	/* dummy */
4762e141748SHans Petter Selasky #endif
4772e141748SHans Petter Selasky 	USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
4782e141748SHans Petter Selasky 
4792e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
4802e141748SHans Petter Selasky  	bus->hw_power_state =
4812e141748SHans Petter Selasky 	  USB_HW_POWER_CONTROL |
4822e141748SHans Petter Selasky 	  USB_HW_POWER_BULK |
4832e141748SHans Petter Selasky 	  USB_HW_POWER_INTERRUPT |
4842e141748SHans Petter Selasky 	  USB_HW_POWER_ISOC |
4852e141748SHans Petter Selasky 	  USB_HW_POWER_NON_ROOT_HUB;
4862e141748SHans Petter Selasky 	bus->no_explore = 0;
4872e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4882e141748SHans Petter Selasky 
4892e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
4902e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
4912e141748SHans Petter Selasky 
4922e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
4932e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
4942e141748SHans Petter Selasky 
4956bbe7cdfSHans Petter Selasky 	/* restore USB configuration to index 0 */
4962e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, 0);
4972e141748SHans Petter Selasky 	if (err)
4982e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not configure root HUB\n");
4992e141748SHans Petter Selasky 
5006bbe7cdfSHans Petter Selasky 	/* probe and attach */
5016bbe7cdfSHans Petter Selasky 	err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
5026bbe7cdfSHans Petter Selasky 	if (err) {
5036bbe7cdfSHans Petter Selasky 		device_printf(bus->bdev, "Could not probe and "
5046bbe7cdfSHans Petter Selasky 		    "attach root HUB\n");
5056bbe7cdfSHans Petter Selasky 	}
5066bbe7cdfSHans Petter Selasky 
5072e141748SHans Petter Selasky 	usbd_enum_unlock(udev);
5086bd3e535SHans Petter Selasky 
5096bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
5102e141748SHans Petter Selasky }
5112e141748SHans Petter Selasky 
5122e141748SHans Petter Selasky /*------------------------------------------------------------------------*
5132e141748SHans Petter Selasky  *	usb_bus_shutdown
5142e141748SHans Petter Selasky  *
5152e141748SHans Petter Selasky  * This function is used to shutdown the USB contoller.
5162e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
5172e141748SHans Petter Selasky static void
5182e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm)
5192e141748SHans Petter Selasky {
5202e141748SHans Petter Selasky 	struct usb_bus *bus;
5212e141748SHans Petter Selasky 	struct usb_device *udev;
5222e141748SHans Petter Selasky 	usb_error_t err;
5232e141748SHans Petter Selasky 
5242e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
5252e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
5262e141748SHans Petter Selasky 
5272e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
5282e141748SHans Petter Selasky 		return;
5292e141748SHans Petter Selasky 
5306bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
5316bd3e535SHans Petter Selasky 
5322e141748SHans Petter Selasky 	bus_generic_shutdown(bus->bdev);
5332e141748SHans Petter Selasky 
5342e141748SHans Petter Selasky 	usbd_enum_lock(udev);
5352e141748SHans Petter Selasky 
5362e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
5372e141748SHans Petter Selasky 	if (err)
5382e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
5392e141748SHans Petter Selasky 
5402e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
5412e141748SHans Petter Selasky 	bus->hw_power_state = 0;
5422e141748SHans Petter Selasky 	bus->no_explore = 1;
5432e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
5442e141748SHans Petter Selasky 
5452e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
5462e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
5472e141748SHans Petter Selasky 
5482e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
5492e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
5502e141748SHans Petter Selasky 
5512e141748SHans Petter Selasky 	usbd_enum_unlock(udev);
5526bd3e535SHans Petter Selasky 
5536bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
5542e141748SHans Petter Selasky }
5552e141748SHans Petter Selasky 
55602ac6454SAndrew Thompson static void
557a593f6b8SAndrew Thompson usb_power_wdog(void *arg)
55802ac6454SAndrew Thompson {
559760bc48eSAndrew Thompson 	struct usb_bus *bus = arg;
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
56202ac6454SAndrew Thompson 
563a593f6b8SAndrew Thompson 	usb_callout_reset(&bus->power_wdog,
564a593f6b8SAndrew Thompson 	    4 * hz, usb_power_wdog, arg);
56502ac6454SAndrew Thompson 
566f0c078e6SAndrew Thompson #ifdef DDB
56734b48722SAlfred Perlstein 	/*
56834b48722SAlfred Perlstein 	 * The following line of code is only here to recover from
56934b48722SAlfred Perlstein 	 * DDB:
57034b48722SAlfred Perlstein 	 */
57134b48722SAlfred Perlstein 	usb_proc_rewakeup(&bus->explore_proc);	/* recover from DDB */
572f0c078e6SAndrew Thompson #endif
57334b48722SAlfred Perlstein 
574e727a16cSAndrew Thompson #if USB_HAVE_POWERD
57502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
57602ac6454SAndrew Thompson 
577a593f6b8SAndrew Thompson 	usb_bus_power_update(bus);
57802ac6454SAndrew Thompson 
579684e3f22SAndrew Thompson 	USB_BUS_LOCK(bus);
580e727a16cSAndrew Thompson #endif
58102ac6454SAndrew Thompson }
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
584a593f6b8SAndrew Thompson  *	usb_bus_attach
58502ac6454SAndrew Thompson  *
58602ac6454SAndrew Thompson  * This function attaches USB in context of the explore thread.
58702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
58802ac6454SAndrew Thompson static void
589a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm)
59002ac6454SAndrew Thompson {
591760bc48eSAndrew Thompson 	struct usb_bus *bus;
592760bc48eSAndrew Thompson 	struct usb_device *child;
59302ac6454SAndrew Thompson 	device_t dev;
594e0a69b51SAndrew Thompson 	usb_error_t err;
5958d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
59602ac6454SAndrew Thompson 
597760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
59802ac6454SAndrew Thompson 	dev = bus->bdev;
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	DPRINTF("\n");
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson 	switch (bus->usbrev) {
60302ac6454SAndrew Thompson 	case USB_REV_1_0:
60402ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
60502ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
60602ac6454SAndrew Thompson 		break;
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson 	case USB_REV_1_1:
60902ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
61002ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
61102ac6454SAndrew Thompson 		break;
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 	case USB_REV_2_0:
61402ac6454SAndrew Thompson 		speed = USB_SPEED_HIGH;
61502ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
61602ac6454SAndrew Thompson 		break;
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 	case USB_REV_2_5:
61902ac6454SAndrew Thompson 		speed = USB_SPEED_VARIABLE;
62002ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
62102ac6454SAndrew Thompson 		break;
62202ac6454SAndrew Thompson 
623963169b4SHans Petter Selasky 	case USB_REV_3_0:
624963169b4SHans Petter Selasky 		speed = USB_SPEED_SUPER;
625ccac019aSHans Petter Selasky 		device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
626963169b4SHans Petter Selasky 		break;
627963169b4SHans Petter Selasky 
62802ac6454SAndrew Thompson 	default:
629767cb2e2SAndrew Thompson 		device_printf(bus->bdev, "Unsupported USB revision\n");
6303df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
63102ac6454SAndrew Thompson 		return;
63202ac6454SAndrew Thompson 	}
63302ac6454SAndrew Thompson 
6344eae601eSAndrew Thompson 	/* default power_mask value */
6354eae601eSAndrew Thompson 	bus->hw_power_state =
6364eae601eSAndrew Thompson 	  USB_HW_POWER_CONTROL |
6374eae601eSAndrew Thompson 	  USB_HW_POWER_BULK |
6384eae601eSAndrew Thompson 	  USB_HW_POWER_INTERRUPT |
6394eae601eSAndrew Thompson 	  USB_HW_POWER_ISOC |
6404eae601eSAndrew Thompson 	  USB_HW_POWER_NON_ROOT_HUB;
6414eae601eSAndrew Thompson 
6422e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
6432e141748SHans Petter Selasky 
6444eae601eSAndrew Thompson 	/* make sure power is set at least once */
6454eae601eSAndrew Thompson 
6464eae601eSAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
6474eae601eSAndrew Thompson 		(bus->methods->set_hw_power) (bus);
6484eae601eSAndrew Thompson 	}
6494eae601eSAndrew Thompson 
6502e141748SHans Petter Selasky 	/* allocate the Root USB device */
65102ac6454SAndrew Thompson 
652a593f6b8SAndrew Thompson 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
65302ac6454SAndrew Thompson 	    speed, USB_MODE_HOST);
65402ac6454SAndrew Thompson 	if (child) {
655a593f6b8SAndrew Thompson 		err = usb_probe_and_attach(child,
65602ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
65702ac6454SAndrew Thompson 		if (!err) {
6581be5bf51SAndrew Thompson 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
6591be5bf51SAndrew Thompson 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
66002ac6454SAndrew Thompson 				err = USB_ERR_NO_ROOT_HUB;
66102ac6454SAndrew Thompson 			}
66202ac6454SAndrew Thompson 		}
66302ac6454SAndrew Thompson 	} else {
66402ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
66502ac6454SAndrew Thompson 	}
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson 	if (err) {
67002ac6454SAndrew Thompson 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
671a593f6b8SAndrew Thompson 		    usbd_errstr(err));
6723df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
67302ac6454SAndrew Thompson 	}
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson 	/* set softc - we are ready */
67602ac6454SAndrew Thompson 	device_set_softc(dev, bus);
67702ac6454SAndrew Thompson 
678684e3f22SAndrew Thompson 	/* start watchdog */
679a593f6b8SAndrew Thompson 	usb_power_wdog(bus);
68002ac6454SAndrew Thompson }
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
683a593f6b8SAndrew Thompson  *	usb_attach_sub
68402ac6454SAndrew Thompson  *
68534b48722SAlfred Perlstein  * This function creates a thread which runs the USB attach code.
68602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
68702ac6454SAndrew Thompson static void
688a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus)
68902ac6454SAndrew Thompson {
69002ac6454SAndrew Thompson 	const char *pname = device_get_nameunit(dev);
69102ac6454SAndrew Thompson 
69234b48722SAlfred Perlstein 	mtx_lock(&Giant);
69334b48722SAlfred Perlstein 	if (usb_devclass_ptr == NULL)
69434b48722SAlfred Perlstein 		usb_devclass_ptr = devclass_find("usbus");
69534b48722SAlfred Perlstein 	mtx_unlock(&Giant);
69634b48722SAlfred Perlstein 
6978be09334SHans Petter Selasky #if USB_HAVE_PF
698fe1c24e3SWeongyo Jeong 	usbpf_attach(bus);
6998be09334SHans Petter Selasky #endif
70002ac6454SAndrew Thompson 	/* Initialise USB process messages */
701a593f6b8SAndrew Thompson 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
70202ac6454SAndrew Thompson 	bus->explore_msg[0].bus = bus;
703a593f6b8SAndrew Thompson 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
70402ac6454SAndrew Thompson 	bus->explore_msg[1].bus = bus;
70502ac6454SAndrew Thompson 
706a593f6b8SAndrew Thompson 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
70702ac6454SAndrew Thompson 	bus->detach_msg[0].bus = bus;
708a593f6b8SAndrew Thompson 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
70902ac6454SAndrew Thompson 	bus->detach_msg[1].bus = bus;
71002ac6454SAndrew Thompson 
711a593f6b8SAndrew Thompson 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
71202ac6454SAndrew Thompson 	bus->attach_msg[0].bus = bus;
713a593f6b8SAndrew Thompson 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
71402ac6454SAndrew Thompson 	bus->attach_msg[1].bus = bus;
71502ac6454SAndrew Thompson 
7162e141748SHans Petter Selasky 	bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
7172e141748SHans Petter Selasky 	bus->suspend_msg[0].bus = bus;
7182e141748SHans Petter Selasky 	bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
7192e141748SHans Petter Selasky 	bus->suspend_msg[1].bus = bus;
7202e141748SHans Petter Selasky 
7212e141748SHans Petter Selasky 	bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
7222e141748SHans Petter Selasky 	bus->resume_msg[0].bus = bus;
7232e141748SHans Petter Selasky 	bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
7242e141748SHans Petter Selasky 	bus->resume_msg[1].bus = bus;
7252e141748SHans Petter Selasky 
7262e141748SHans Petter Selasky 	bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
7272e141748SHans Petter Selasky 	bus->shutdown_msg[0].bus = bus;
7282e141748SHans Petter Selasky 	bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
7292e141748SHans Petter Selasky 	bus->shutdown_msg[1].bus = bus;
7302e141748SHans Petter Selasky 
73139307315SAndrew Thompson 	/* Create USB explore and callback processes */
73202ac6454SAndrew Thompson 
733a593f6b8SAndrew Thompson 	if (usb_proc_create(&bus->giant_callback_proc,
73402ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
73588334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB Giant "
73602ac6454SAndrew Thompson 		    "callback process failed.\n");
737a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->non_giant_callback_proc,
73802ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
73988334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB non-Giant "
74002ac6454SAndrew Thompson 		    "callback process failed.\n");
741a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->explore_proc,
74202ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
74388334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB explore "
74402ac6454SAndrew Thompson 		    "process failed.\n");
745a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->control_xfer_proc,
746672c9965SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
74788334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB control transfer "
748672c9965SAndrew Thompson 		    "process failed.\n");
74902ac6454SAndrew Thompson 	} else {
75002ac6454SAndrew Thompson 		/* Get final attach going */
75102ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
7522e141748SHans Petter Selasky 		usb_proc_msignal(&bus->explore_proc,
7532e141748SHans Petter Selasky 		    &bus->attach_msg[0], &bus->attach_msg[1]);
75402ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
75534b48722SAlfred Perlstein 
75634b48722SAlfred Perlstein 		/* Do initial explore */
75734b48722SAlfred Perlstein 		usb_needs_explore(bus, 1);
75802ac6454SAndrew Thompson 	}
75902ac6454SAndrew Thompson }
76002ac6454SAndrew Thompson 
761a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
76202ac6454SAndrew Thompson 
76302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
764a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all_cb
76502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
766bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
76702ac6454SAndrew Thompson static void
768a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
769f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
77002ac6454SAndrew Thompson {
771a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(pc);
77202ac6454SAndrew Thompson }
773bdc081c6SAndrew Thompson #endif
77402ac6454SAndrew Thompson 
77502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
776a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all - factored out code
77702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
778bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
77902ac6454SAndrew Thompson void
780a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
78102ac6454SAndrew Thompson {
78202ac6454SAndrew Thompson 	if (cb) {
783a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_flush_all_cb);
78402ac6454SAndrew Thompson 	}
78502ac6454SAndrew Thompson }
786bdc081c6SAndrew Thompson #endif
78702ac6454SAndrew Thompson 
78802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
789a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all_cb
79002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
791bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
79202ac6454SAndrew Thompson static void
793a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
794f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
79502ac6454SAndrew Thompson {
79602ac6454SAndrew Thompson 	/* need to initialize the page cache */
79702ac6454SAndrew Thompson 	pc->tag_parent = bus->dma_parent_tag;
79802ac6454SAndrew Thompson 
799a593f6b8SAndrew Thompson 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
80002ac6454SAndrew Thompson 		bus->alloc_failed = 1;
80102ac6454SAndrew Thompson 	}
80202ac6454SAndrew Thompson }
803bdc081c6SAndrew Thompson #endif
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
806a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all - factored out code
80702ac6454SAndrew Thompson  *
80802ac6454SAndrew Thompson  * Returns:
80902ac6454SAndrew Thompson  *    0: Success
81002ac6454SAndrew Thompson  * Else: Failure
81102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
81202ac6454SAndrew Thompson uint8_t
813a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
814e0a69b51SAndrew Thompson     usb_bus_mem_cb_t *cb)
81502ac6454SAndrew Thompson {
81602ac6454SAndrew Thompson 	bus->alloc_failed = 0;
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
81902ac6454SAndrew Thompson 	    NULL, MTX_DEF | MTX_RECURSE);
82002ac6454SAndrew Thompson 
821a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&bus->power_wdog,
822684e3f22SAndrew Thompson 	    &bus->bus_mtx, 0);
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 	TAILQ_INIT(&bus->intr_q.head);
82502ac6454SAndrew Thompson 
826bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
827a593f6b8SAndrew Thompson 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
828bdc081c6SAndrew Thompson 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
829bdc081c6SAndrew Thompson #endif
83002ac6454SAndrew Thompson 	if ((bus->devices_max > USB_MAX_DEVICES) ||
83102ac6454SAndrew Thompson 	    (bus->devices_max < USB_MIN_DEVICES) ||
83202ac6454SAndrew Thompson 	    (bus->devices == NULL)) {
83302ac6454SAndrew Thompson 		DPRINTFN(0, "Devices field has not been "
834767cb2e2SAndrew Thompson 		    "initialised properly\n");
83502ac6454SAndrew Thompson 		bus->alloc_failed = 1;		/* failure */
83602ac6454SAndrew Thompson 	}
837bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
83802ac6454SAndrew Thompson 	if (cb) {
839a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_alloc_all_cb);
84002ac6454SAndrew Thompson 	}
841bdc081c6SAndrew Thompson #endif
84202ac6454SAndrew Thompson 	if (bus->alloc_failed) {
843a593f6b8SAndrew Thompson 		usb_bus_mem_free_all(bus, cb);
84402ac6454SAndrew Thompson 	}
84502ac6454SAndrew Thompson 	return (bus->alloc_failed);
84602ac6454SAndrew Thompson }
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
849a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all_cb
85002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
851bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
85202ac6454SAndrew Thompson static void
853a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
854f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
85502ac6454SAndrew Thompson {
856a593f6b8SAndrew Thompson 	usb_pc_free_mem(pc);
85702ac6454SAndrew Thompson }
858bdc081c6SAndrew Thompson #endif
85902ac6454SAndrew Thompson 
86002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
861a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all - factored out code
86202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
86302ac6454SAndrew Thompson void
864a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
86502ac6454SAndrew Thompson {
866bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
86702ac6454SAndrew Thompson 	if (cb) {
868a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_free_all_cb);
86902ac6454SAndrew Thompson 	}
870a593f6b8SAndrew Thompson 	usb_dma_tag_unsetup(bus->dma_parent_tag);
871bdc081c6SAndrew Thompson #endif
87202ac6454SAndrew Thompson 
87302ac6454SAndrew Thompson 	mtx_destroy(&bus->bus_mtx);
87402ac6454SAndrew Thompson }
875