xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision 6bbe7cdfb27e4e94cf07adb966c881a35284aedc)
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 
925ed294aeSHans Petter Selasky static int usb_no_shutdown_wait = 0;
935ed294aeSHans Petter Selasky TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
94d63cc02dSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN, &usb_no_shutdown_wait, 0,
955ed294aeSHans Petter Selasky     "No USB device waiting at system shutdown.");
96a0c61406SAlfred Perlstein 
97a593f6b8SAndrew Thompson static devclass_t usb_devclass;
9802ac6454SAndrew Thompson 
99a593f6b8SAndrew Thompson static device_method_t usb_methods[] = {
100a593f6b8SAndrew Thompson 	DEVMETHOD(device_probe, usb_probe),
101a593f6b8SAndrew Thompson 	DEVMETHOD(device_attach, usb_attach),
102a593f6b8SAndrew Thompson 	DEVMETHOD(device_detach, usb_detach),
1032e141748SHans Petter Selasky 	DEVMETHOD(device_suspend, usb_suspend),
1042e141748SHans Petter Selasky 	DEVMETHOD(device_resume, usb_resume),
1052e141748SHans Petter Selasky 	DEVMETHOD(device_shutdown, usb_shutdown),
10602ac6454SAndrew Thompson 	{0, 0}
10702ac6454SAndrew Thompson };
10802ac6454SAndrew Thompson 
109a593f6b8SAndrew Thompson static driver_t usb_driver = {
11002ac6454SAndrew Thompson 	.name = "usbus",
111a593f6b8SAndrew Thompson 	.methods = usb_methods,
11202ac6454SAndrew Thompson 	.size = 0,
11302ac6454SAndrew Thompson };
11402ac6454SAndrew Thompson 
115864bc412SHans Petter Selasky /* Host Only Drivers */
116a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
117a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
118a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
119963169b4SHans Petter Selasky DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
120864bc412SHans Petter Selasky 
121864bc412SHans Petter Selasky /* Device Only Drivers */
122a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
123864bc412SHans Petter Selasky DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
124a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
125d42cc946SOleksandr Tymoshenko DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
128a593f6b8SAndrew Thompson  *	usb_probe
12902ac6454SAndrew Thompson  *
13002ac6454SAndrew Thompson  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
13102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
13202ac6454SAndrew Thompson static int
133a593f6b8SAndrew Thompson usb_probe(device_t dev)
13402ac6454SAndrew Thompson {
13502ac6454SAndrew Thompson 	DPRINTF("\n");
13602ac6454SAndrew Thompson 	return (0);
13702ac6454SAndrew Thompson }
13802ac6454SAndrew Thompson 
1393df007ceSHans Petter Selasky static void
1403df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus)
1413df007ceSHans Petter Selasky {
1423df007ceSHans Petter Selasky 	if (bus->bus_roothold != NULL) {
1433df007ceSHans Petter Selasky 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
1443df007ceSHans Petter Selasky 		root_mount_rel(bus->bus_roothold);
1453df007ceSHans Petter Selasky 		bus->bus_roothold = NULL;
1463df007ceSHans Petter Selasky 	}
1473df007ceSHans Petter Selasky }
1483df007ceSHans Petter Selasky 
14902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
150a593f6b8SAndrew Thompson  *	usb_attach
15102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
15202ac6454SAndrew Thompson static int
153a593f6b8SAndrew Thompson usb_attach(device_t dev)
15402ac6454SAndrew Thompson {
155760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_ivars(dev);
15602ac6454SAndrew Thompson 
15702ac6454SAndrew Thompson 	DPRINTF("\n");
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson 	if (bus == NULL) {
160767cb2e2SAndrew Thompson 		device_printf(dev, "USB device has no ivars\n");
16102ac6454SAndrew Thompson 		return (ENXIO);
16202ac6454SAndrew Thompson 	}
16302ac6454SAndrew Thompson 
164a0c61406SAlfred Perlstein 	if (usb_no_boot_wait == 0) {
16502ac6454SAndrew Thompson 		/* delay vfs_mountroot until the bus is explored */
166853a10a5SAndrew Thompson 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
167a0c61406SAlfred Perlstein 	}
16802ac6454SAndrew Thompson 
169a593f6b8SAndrew Thompson 	usb_attach_sub(dev, bus);
17034b48722SAlfred Perlstein 
17102ac6454SAndrew Thompson 	return (0);			/* return success */
17202ac6454SAndrew Thompson }
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
175a593f6b8SAndrew Thompson  *	usb_detach
17602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
17702ac6454SAndrew Thompson static int
178a593f6b8SAndrew Thompson usb_detach(device_t dev)
17902ac6454SAndrew Thompson {
180760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_softc(dev);
18102ac6454SAndrew Thompson 
18202ac6454SAndrew Thompson 	DPRINTF("\n");
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson 	if (bus == NULL) {
18502ac6454SAndrew Thompson 		/* was never setup properly */
18602ac6454SAndrew Thompson 		return (0);
18702ac6454SAndrew Thompson 	}
18802ac6454SAndrew Thompson 	/* Stop power watchdog */
189a593f6b8SAndrew Thompson 	usb_callout_drain(&bus->power_wdog);
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson 	/* Let the USB explore process detach all devices. */
1923df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
19502ac6454SAndrew Thompson 
1962e141748SHans Petter Selasky 	/* Queue detach job */
1972e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
1982e141748SHans Petter Selasky 	    &bus->detach_msg[0], &bus->detach_msg[1]);
1992e141748SHans Petter Selasky 
2002e141748SHans Petter Selasky 	/* Wait for detach to complete */
201a593f6b8SAndrew Thompson 	usb_proc_mwait(&bus->explore_proc,
20202ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1]);
20302ac6454SAndrew Thompson 
20402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 	/* Get rid of USB callback processes */
20702ac6454SAndrew Thompson 
208a593f6b8SAndrew Thompson 	usb_proc_free(&bus->giant_callback_proc);
209a593f6b8SAndrew Thompson 	usb_proc_free(&bus->non_giant_callback_proc);
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	/* Get rid of USB explore process */
21202ac6454SAndrew Thompson 
213a593f6b8SAndrew Thompson 	usb_proc_free(&bus->explore_proc);
21402ac6454SAndrew Thompson 
215672c9965SAndrew Thompson 	/* Get rid of control transfer process */
216672c9965SAndrew Thompson 
217a593f6b8SAndrew Thompson 	usb_proc_free(&bus->control_xfer_proc);
218672c9965SAndrew Thompson 
2198be09334SHans Petter Selasky #if USB_HAVE_PF
220fe1c24e3SWeongyo Jeong 	usbpf_detach(bus);
2218be09334SHans Petter Selasky #endif
22202ac6454SAndrew Thompson 	return (0);
22302ac6454SAndrew Thompson }
22402ac6454SAndrew Thompson 
22502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2262e141748SHans Petter Selasky  *	usb_suspend
2272e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2282e141748SHans Petter Selasky static int
2292e141748SHans Petter Selasky usb_suspend(device_t dev)
2302e141748SHans Petter Selasky {
2312e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2322e141748SHans Petter Selasky 
2332e141748SHans Petter Selasky 	DPRINTF("\n");
2342e141748SHans Petter Selasky 
2352e141748SHans Petter Selasky 	if (bus == NULL) {
2362e141748SHans Petter Selasky 		/* was never setup properly */
2372e141748SHans Petter Selasky 		return (0);
2382e141748SHans Petter Selasky 	}
2392e141748SHans Petter Selasky 
2402e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2412e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
2422e141748SHans Petter Selasky 	    &bus->suspend_msg[0], &bus->suspend_msg[1]);
2432e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2442e141748SHans Petter Selasky 
2452e141748SHans Petter Selasky 	return (0);
2462e141748SHans Petter Selasky }
2472e141748SHans Petter Selasky 
2482e141748SHans Petter Selasky /*------------------------------------------------------------------------*
2492e141748SHans Petter Selasky  *	usb_resume
2502e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2512e141748SHans Petter Selasky static int
2522e141748SHans Petter Selasky usb_resume(device_t dev)
2532e141748SHans Petter Selasky {
2542e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2552e141748SHans Petter Selasky 
2562e141748SHans Petter Selasky 	DPRINTF("\n");
2572e141748SHans Petter Selasky 
2582e141748SHans Petter Selasky 	if (bus == NULL) {
2592e141748SHans Petter Selasky 		/* was never setup properly */
2602e141748SHans Petter Selasky 		return (0);
2612e141748SHans Petter Selasky 	}
2622e141748SHans Petter Selasky 
2632e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2642e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
2652e141748SHans Petter Selasky 	    &bus->resume_msg[0], &bus->resume_msg[1]);
2662e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2672e141748SHans Petter Selasky 
2682e141748SHans Petter Selasky 	return (0);
2692e141748SHans Petter Selasky }
2702e141748SHans Petter Selasky 
2712e141748SHans Petter Selasky /*------------------------------------------------------------------------*
2722e141748SHans Petter Selasky  *	usb_shutdown
2732e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2742e141748SHans Petter Selasky static int
2752e141748SHans Petter Selasky usb_shutdown(device_t dev)
2762e141748SHans Petter Selasky {
2772e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2782e141748SHans Petter Selasky 
2792e141748SHans Petter Selasky 	DPRINTF("\n");
2802e141748SHans Petter Selasky 
2812e141748SHans Petter Selasky 	if (bus == NULL) {
2822e141748SHans Petter Selasky 		/* was never setup properly */
2832e141748SHans Petter Selasky 		return (0);
2842e141748SHans Petter Selasky 	}
2852e141748SHans Petter Selasky 
2865ed294aeSHans Petter Selasky 	device_printf(bus->bdev, "Controller shutdown\n");
2875ed294aeSHans Petter Selasky 
2882e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2892e141748SHans Petter Selasky 	usb_proc_msignal(&bus->explore_proc,
2902e141748SHans Petter Selasky 	    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
2915ed294aeSHans Petter Selasky 	if (usb_no_shutdown_wait == 0) {
2925ed294aeSHans Petter Selasky 		/* wait for shutdown callback to be executed */
2935ed294aeSHans Petter Selasky 		usb_proc_mwait(&bus->explore_proc,
2945ed294aeSHans Petter Selasky 		    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
2955ed294aeSHans Petter Selasky 	}
2962e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2972e141748SHans Petter Selasky 
2985ed294aeSHans Petter Selasky 	device_printf(bus->bdev, "Controller shutdown complete\n");
2995ed294aeSHans Petter Selasky 
3002e141748SHans Petter Selasky 	return (0);
3012e141748SHans Petter Selasky }
3022e141748SHans Petter Selasky 
3032e141748SHans Petter Selasky /*------------------------------------------------------------------------*
304a593f6b8SAndrew Thompson  *	usb_bus_explore
30502ac6454SAndrew Thompson  *
30602ac6454SAndrew Thompson  * This function is used to explore the device tree from the root.
30702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
30802ac6454SAndrew Thompson static void
309a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm)
31002ac6454SAndrew Thompson {
311760bc48eSAndrew Thompson 	struct usb_bus *bus;
312760bc48eSAndrew Thompson 	struct usb_device *udev;
31302ac6454SAndrew Thompson 
314760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
31502ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
31602ac6454SAndrew Thompson 
3172e141748SHans Petter Selasky 	if (bus->no_explore != 0)
3182e141748SHans Petter Selasky 		return;
3192e141748SHans Petter Selasky 
32002ac6454SAndrew Thompson 	if (udev && udev->hub) {
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 		if (bus->do_probe) {
32302ac6454SAndrew Thompson 			bus->do_probe = 0;
32402ac6454SAndrew Thompson 			bus->driver_added_refcount++;
32502ac6454SAndrew Thompson 		}
32602ac6454SAndrew Thompson 		if (bus->driver_added_refcount == 0) {
32702ac6454SAndrew Thompson 			/* avoid zero, hence that is memory default */
32802ac6454SAndrew Thompson 			bus->driver_added_refcount = 1;
32902ac6454SAndrew Thompson 		}
33002ac6454SAndrew Thompson 
331f0c078e6SAndrew Thompson #ifdef DDB
33234b48722SAlfred Perlstein 		/*
33334b48722SAlfred Perlstein 		 * The following three lines of code are only here to
33434b48722SAlfred Perlstein 		 * recover from DDB:
33534b48722SAlfred Perlstein 		 */
33634b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->control_xfer_proc);
33734b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->giant_callback_proc);
33834b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->non_giant_callback_proc);
339f0c078e6SAndrew Thompson #endif
34034b48722SAlfred Perlstein 
34134b48722SAlfred Perlstein 		USB_BUS_UNLOCK(bus);
342a56fe095SJohn Baldwin 
343e727a16cSAndrew Thompson #if USB_HAVE_POWERD
34402ac6454SAndrew Thompson 		/*
34502ac6454SAndrew Thompson 		 * First update the USB power state!
34602ac6454SAndrew Thompson 		 */
347a593f6b8SAndrew Thompson 		usb_bus_powerd(bus);
348e727a16cSAndrew Thompson #endif
34934b48722SAlfred Perlstein 		 /* Explore the Root USB HUB. */
35002ac6454SAndrew Thompson 		(udev->hub->explore) (udev);
35102ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
35202ac6454SAndrew Thompson 	}
3533df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
35402ac6454SAndrew Thompson }
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
357a593f6b8SAndrew Thompson  *	usb_bus_detach
35802ac6454SAndrew Thompson  *
35902ac6454SAndrew Thompson  * This function is used to detach the device tree from the root.
36002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
36102ac6454SAndrew Thompson static void
362a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm)
36302ac6454SAndrew Thompson {
364760bc48eSAndrew Thompson 	struct usb_bus *bus;
365760bc48eSAndrew Thompson 	struct usb_device *udev;
36602ac6454SAndrew Thompson 	device_t dev;
36702ac6454SAndrew Thompson 
368760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
36902ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
37002ac6454SAndrew Thompson 	dev = bus->bdev;
37102ac6454SAndrew Thompson 	/* clear the softc */
37202ac6454SAndrew Thompson 	device_set_softc(dev, NULL);
37302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	/* detach children first */
37634b48722SAlfred Perlstein 	mtx_lock(&Giant);
37702ac6454SAndrew Thompson 	bus_generic_detach(dev);
37834b48722SAlfred Perlstein 	mtx_unlock(&Giant);
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 	/*
381d88688c7SAndrew Thompson 	 * Free USB device and all subdevices, if any.
38202ac6454SAndrew Thompson 	 */
383d88688c7SAndrew Thompson 	usb_free_device(udev, 0);
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
38602ac6454SAndrew Thompson 	/* clear bdev variable last */
38702ac6454SAndrew Thompson 	bus->bdev = NULL;
38802ac6454SAndrew Thompson }
38902ac6454SAndrew Thompson 
3902e141748SHans Petter Selasky /*------------------------------------------------------------------------*
3912e141748SHans Petter Selasky  *	usb_bus_suspend
3922e141748SHans Petter Selasky  *
3932e141748SHans Petter Selasky  * This function is used to suspend the USB contoller.
3942e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
3952e141748SHans Petter Selasky static void
3962e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm)
3972e141748SHans Petter Selasky {
3982e141748SHans Petter Selasky 	struct usb_bus *bus;
3992e141748SHans Petter Selasky 	struct usb_device *udev;
4002e141748SHans Petter Selasky 	usb_error_t err;
4012e141748SHans Petter Selasky 
4022e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
4032e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
4042e141748SHans Petter Selasky 
4052e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
4062e141748SHans Petter Selasky 		return;
4072e141748SHans Petter Selasky 
4086bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4096bd3e535SHans Petter Selasky 
4102e141748SHans Petter Selasky 	bus_generic_shutdown(bus->bdev);
4112e141748SHans Petter Selasky 
4122e141748SHans Petter Selasky 	usbd_enum_lock(udev);
4132e141748SHans Petter Selasky 
4142e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
4152e141748SHans Petter Selasky 	if (err)
4162e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
4172e141748SHans Petter Selasky 
4182e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
4192e141748SHans Petter Selasky 	bus->hw_power_state = 0;
4202e141748SHans Petter Selasky 	bus->no_explore = 1;
4212e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4222e141748SHans Petter Selasky 
4232e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
4242e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
4252e141748SHans Petter Selasky 
4262e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
4272e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
4282e141748SHans Petter Selasky 
4292e141748SHans Petter Selasky 	usbd_enum_unlock(udev);
4306bd3e535SHans Petter Selasky 
4316bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
4322e141748SHans Petter Selasky }
4332e141748SHans Petter Selasky 
4342e141748SHans Petter Selasky /*------------------------------------------------------------------------*
4352e141748SHans Petter Selasky  *	usb_bus_resume
4362e141748SHans Petter Selasky  *
4372e141748SHans Petter Selasky  * This function is used to resume the USB contoller.
4382e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
4392e141748SHans Petter Selasky static void
4402e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm)
4412e141748SHans Petter Selasky {
4422e141748SHans Petter Selasky 	struct usb_bus *bus;
4432e141748SHans Petter Selasky 	struct usb_device *udev;
4442e141748SHans Petter Selasky 	usb_error_t err;
4452e141748SHans Petter Selasky 
4462e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
4472e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
4482e141748SHans Petter Selasky 
4492e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
4502e141748SHans Petter Selasky 		return;
4512e141748SHans Petter Selasky 
4526bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4536bd3e535SHans Petter Selasky 
4542e141748SHans Petter Selasky 	usbd_enum_lock(udev);
4552e141748SHans Petter Selasky #if 0
4562e141748SHans Petter Selasky 	DEVMETHOD(usb_take_controller, NULL);	/* dummy */
4572e141748SHans Petter Selasky #endif
4582e141748SHans Petter Selasky 	USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
4592e141748SHans Petter Selasky 
4602e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
4612e141748SHans Petter Selasky  	bus->hw_power_state =
4622e141748SHans Petter Selasky 	  USB_HW_POWER_CONTROL |
4632e141748SHans Petter Selasky 	  USB_HW_POWER_BULK |
4642e141748SHans Petter Selasky 	  USB_HW_POWER_INTERRUPT |
4652e141748SHans Petter Selasky 	  USB_HW_POWER_ISOC |
4662e141748SHans Petter Selasky 	  USB_HW_POWER_NON_ROOT_HUB;
4672e141748SHans Petter Selasky 	bus->no_explore = 0;
4682e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4692e141748SHans Petter Selasky 
4702e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
4712e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
4722e141748SHans Petter Selasky 
4732e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
4742e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
4752e141748SHans Petter Selasky 
476*6bbe7cdfSHans Petter Selasky 	/* restore USB configuration to index 0 */
4772e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, 0);
4782e141748SHans Petter Selasky 	if (err)
4792e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not configure root HUB\n");
4802e141748SHans Petter Selasky 
481*6bbe7cdfSHans Petter Selasky 	/* probe and attach */
482*6bbe7cdfSHans Petter Selasky 	err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
483*6bbe7cdfSHans Petter Selasky 	if (err) {
484*6bbe7cdfSHans Petter Selasky 		device_printf(bus->bdev, "Could not probe and "
485*6bbe7cdfSHans Petter Selasky 		    "attach root HUB\n");
486*6bbe7cdfSHans Petter Selasky 	}
487*6bbe7cdfSHans Petter Selasky 
4882e141748SHans Petter Selasky 	usbd_enum_unlock(udev);
4896bd3e535SHans Petter Selasky 
4906bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
4912e141748SHans Petter Selasky }
4922e141748SHans Petter Selasky 
4932e141748SHans Petter Selasky /*------------------------------------------------------------------------*
4942e141748SHans Petter Selasky  *	usb_bus_shutdown
4952e141748SHans Petter Selasky  *
4962e141748SHans Petter Selasky  * This function is used to shutdown the USB contoller.
4972e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
4982e141748SHans Petter Selasky static void
4992e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm)
5002e141748SHans Petter Selasky {
5012e141748SHans Petter Selasky 	struct usb_bus *bus;
5022e141748SHans Petter Selasky 	struct usb_device *udev;
5032e141748SHans Petter Selasky 	usb_error_t err;
5042e141748SHans Petter Selasky 
5052e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
5062e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
5072e141748SHans Petter Selasky 
5082e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
5092e141748SHans Petter Selasky 		return;
5102e141748SHans Petter Selasky 
5116bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
5126bd3e535SHans Petter Selasky 
5132e141748SHans Petter Selasky 	bus_generic_shutdown(bus->bdev);
5142e141748SHans Petter Selasky 
5152e141748SHans Petter Selasky 	usbd_enum_lock(udev);
5162e141748SHans Petter Selasky 
5172e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
5182e141748SHans Petter Selasky 	if (err)
5192e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
5202e141748SHans Petter Selasky 
5212e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
5222e141748SHans Petter Selasky 	bus->hw_power_state = 0;
5232e141748SHans Petter Selasky 	bus->no_explore = 1;
5242e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
5252e141748SHans Petter Selasky 
5262e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
5272e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
5282e141748SHans Petter Selasky 
5292e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
5302e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
5312e141748SHans Petter Selasky 
5322e141748SHans Petter Selasky 	usbd_enum_unlock(udev);
5336bd3e535SHans Petter Selasky 
5346bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
5352e141748SHans Petter Selasky }
5362e141748SHans Petter Selasky 
53702ac6454SAndrew Thompson static void
538a593f6b8SAndrew Thompson usb_power_wdog(void *arg)
53902ac6454SAndrew Thompson {
540760bc48eSAndrew Thompson 	struct usb_bus *bus = arg;
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
54302ac6454SAndrew Thompson 
544a593f6b8SAndrew Thompson 	usb_callout_reset(&bus->power_wdog,
545a593f6b8SAndrew Thompson 	    4 * hz, usb_power_wdog, arg);
54602ac6454SAndrew Thompson 
547f0c078e6SAndrew Thompson #ifdef DDB
54834b48722SAlfred Perlstein 	/*
54934b48722SAlfred Perlstein 	 * The following line of code is only here to recover from
55034b48722SAlfred Perlstein 	 * DDB:
55134b48722SAlfred Perlstein 	 */
55234b48722SAlfred Perlstein 	usb_proc_rewakeup(&bus->explore_proc);	/* recover from DDB */
553f0c078e6SAndrew Thompson #endif
55434b48722SAlfred Perlstein 
555e727a16cSAndrew Thompson #if USB_HAVE_POWERD
55602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
55702ac6454SAndrew Thompson 
558a593f6b8SAndrew Thompson 	usb_bus_power_update(bus);
55902ac6454SAndrew Thompson 
560684e3f22SAndrew Thompson 	USB_BUS_LOCK(bus);
561e727a16cSAndrew Thompson #endif
56202ac6454SAndrew Thompson }
56302ac6454SAndrew Thompson 
56402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
565a593f6b8SAndrew Thompson  *	usb_bus_attach
56602ac6454SAndrew Thompson  *
56702ac6454SAndrew Thompson  * This function attaches USB in context of the explore thread.
56802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
56902ac6454SAndrew Thompson static void
570a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm)
57102ac6454SAndrew Thompson {
572760bc48eSAndrew Thompson 	struct usb_bus *bus;
573760bc48eSAndrew Thompson 	struct usb_device *child;
57402ac6454SAndrew Thompson 	device_t dev;
575e0a69b51SAndrew Thompson 	usb_error_t err;
5768d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
57702ac6454SAndrew Thompson 
578760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
57902ac6454SAndrew Thompson 	dev = bus->bdev;
58002ac6454SAndrew Thompson 
58102ac6454SAndrew Thompson 	DPRINTF("\n");
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 	switch (bus->usbrev) {
58402ac6454SAndrew Thompson 	case USB_REV_1_0:
58502ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
58602ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
58702ac6454SAndrew Thompson 		break;
58802ac6454SAndrew Thompson 
58902ac6454SAndrew Thompson 	case USB_REV_1_1:
59002ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
59102ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
59202ac6454SAndrew Thompson 		break;
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson 	case USB_REV_2_0:
59502ac6454SAndrew Thompson 		speed = USB_SPEED_HIGH;
59602ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
59702ac6454SAndrew Thompson 		break;
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson 	case USB_REV_2_5:
60002ac6454SAndrew Thompson 		speed = USB_SPEED_VARIABLE;
60102ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
60202ac6454SAndrew Thompson 		break;
60302ac6454SAndrew Thompson 
604963169b4SHans Petter Selasky 	case USB_REV_3_0:
605963169b4SHans Petter Selasky 		speed = USB_SPEED_SUPER;
606ccac019aSHans Petter Selasky 		device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
607963169b4SHans Petter Selasky 		break;
608963169b4SHans Petter Selasky 
60902ac6454SAndrew Thompson 	default:
610767cb2e2SAndrew Thompson 		device_printf(bus->bdev, "Unsupported USB revision\n");
6113df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
61202ac6454SAndrew Thompson 		return;
61302ac6454SAndrew Thompson 	}
61402ac6454SAndrew Thompson 
6154eae601eSAndrew Thompson 	/* default power_mask value */
6164eae601eSAndrew Thompson 	bus->hw_power_state =
6174eae601eSAndrew Thompson 	  USB_HW_POWER_CONTROL |
6184eae601eSAndrew Thompson 	  USB_HW_POWER_BULK |
6194eae601eSAndrew Thompson 	  USB_HW_POWER_INTERRUPT |
6204eae601eSAndrew Thompson 	  USB_HW_POWER_ISOC |
6214eae601eSAndrew Thompson 	  USB_HW_POWER_NON_ROOT_HUB;
6224eae601eSAndrew Thompson 
6232e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
6242e141748SHans Petter Selasky 
6254eae601eSAndrew Thompson 	/* make sure power is set at least once */
6264eae601eSAndrew Thompson 
6274eae601eSAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
6284eae601eSAndrew Thompson 		(bus->methods->set_hw_power) (bus);
6294eae601eSAndrew Thompson 	}
6304eae601eSAndrew Thompson 
6312e141748SHans Petter Selasky 	/* allocate the Root USB device */
63202ac6454SAndrew Thompson 
633a593f6b8SAndrew Thompson 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
63402ac6454SAndrew Thompson 	    speed, USB_MODE_HOST);
63502ac6454SAndrew Thompson 	if (child) {
636a593f6b8SAndrew Thompson 		err = usb_probe_and_attach(child,
63702ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
63802ac6454SAndrew Thompson 		if (!err) {
6391be5bf51SAndrew Thompson 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
6401be5bf51SAndrew Thompson 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
64102ac6454SAndrew Thompson 				err = USB_ERR_NO_ROOT_HUB;
64202ac6454SAndrew Thompson 			}
64302ac6454SAndrew Thompson 		}
64402ac6454SAndrew Thompson 	} else {
64502ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
64602ac6454SAndrew Thompson 	}
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	if (err) {
65102ac6454SAndrew Thompson 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
652a593f6b8SAndrew Thompson 		    usbd_errstr(err));
6533df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
65402ac6454SAndrew Thompson 	}
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	/* set softc - we are ready */
65702ac6454SAndrew Thompson 	device_set_softc(dev, bus);
65802ac6454SAndrew Thompson 
659684e3f22SAndrew Thompson 	/* start watchdog */
660a593f6b8SAndrew Thompson 	usb_power_wdog(bus);
66102ac6454SAndrew Thompson }
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
664a593f6b8SAndrew Thompson  *	usb_attach_sub
66502ac6454SAndrew Thompson  *
66634b48722SAlfred Perlstein  * This function creates a thread which runs the USB attach code.
66702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
66802ac6454SAndrew Thompson static void
669a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus)
67002ac6454SAndrew Thompson {
67102ac6454SAndrew Thompson 	const char *pname = device_get_nameunit(dev);
67202ac6454SAndrew Thompson 
67334b48722SAlfred Perlstein 	mtx_lock(&Giant);
67434b48722SAlfred Perlstein 	if (usb_devclass_ptr == NULL)
67534b48722SAlfred Perlstein 		usb_devclass_ptr = devclass_find("usbus");
67634b48722SAlfred Perlstein 	mtx_unlock(&Giant);
67734b48722SAlfred Perlstein 
6788be09334SHans Petter Selasky #if USB_HAVE_PF
679fe1c24e3SWeongyo Jeong 	usbpf_attach(bus);
6808be09334SHans Petter Selasky #endif
68102ac6454SAndrew Thompson 	/* Initialise USB process messages */
682a593f6b8SAndrew Thompson 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
68302ac6454SAndrew Thompson 	bus->explore_msg[0].bus = bus;
684a593f6b8SAndrew Thompson 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
68502ac6454SAndrew Thompson 	bus->explore_msg[1].bus = bus;
68602ac6454SAndrew Thompson 
687a593f6b8SAndrew Thompson 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
68802ac6454SAndrew Thompson 	bus->detach_msg[0].bus = bus;
689a593f6b8SAndrew Thompson 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
69002ac6454SAndrew Thompson 	bus->detach_msg[1].bus = bus;
69102ac6454SAndrew Thompson 
692a593f6b8SAndrew Thompson 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
69302ac6454SAndrew Thompson 	bus->attach_msg[0].bus = bus;
694a593f6b8SAndrew Thompson 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
69502ac6454SAndrew Thompson 	bus->attach_msg[1].bus = bus;
69602ac6454SAndrew Thompson 
6972e141748SHans Petter Selasky 	bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
6982e141748SHans Petter Selasky 	bus->suspend_msg[0].bus = bus;
6992e141748SHans Petter Selasky 	bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
7002e141748SHans Petter Selasky 	bus->suspend_msg[1].bus = bus;
7012e141748SHans Petter Selasky 
7022e141748SHans Petter Selasky 	bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
7032e141748SHans Petter Selasky 	bus->resume_msg[0].bus = bus;
7042e141748SHans Petter Selasky 	bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
7052e141748SHans Petter Selasky 	bus->resume_msg[1].bus = bus;
7062e141748SHans Petter Selasky 
7072e141748SHans Petter Selasky 	bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
7082e141748SHans Petter Selasky 	bus->shutdown_msg[0].bus = bus;
7092e141748SHans Petter Selasky 	bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
7102e141748SHans Petter Selasky 	bus->shutdown_msg[1].bus = bus;
7112e141748SHans Petter Selasky 
71239307315SAndrew Thompson 	/* Create USB explore and callback processes */
71302ac6454SAndrew Thompson 
714a593f6b8SAndrew Thompson 	if (usb_proc_create(&bus->giant_callback_proc,
71502ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
71688334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB Giant "
71702ac6454SAndrew Thompson 		    "callback process failed.\n");
718a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->non_giant_callback_proc,
71902ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
72088334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB non-Giant "
72102ac6454SAndrew Thompson 		    "callback process failed.\n");
722a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->explore_proc,
72302ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
72488334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB explore "
72502ac6454SAndrew Thompson 		    "process failed.\n");
726a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->control_xfer_proc,
727672c9965SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
72888334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB control transfer "
729672c9965SAndrew Thompson 		    "process failed.\n");
73002ac6454SAndrew Thompson 	} else {
73102ac6454SAndrew Thompson 		/* Get final attach going */
73202ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
7332e141748SHans Petter Selasky 		usb_proc_msignal(&bus->explore_proc,
7342e141748SHans Petter Selasky 		    &bus->attach_msg[0], &bus->attach_msg[1]);
73502ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
73634b48722SAlfred Perlstein 
73734b48722SAlfred Perlstein 		/* Do initial explore */
73834b48722SAlfred Perlstein 		usb_needs_explore(bus, 1);
73902ac6454SAndrew Thompson 	}
74002ac6454SAndrew Thompson }
74102ac6454SAndrew Thompson 
742a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
745a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all_cb
74602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
747bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
74802ac6454SAndrew Thompson static void
749a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
750f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
75102ac6454SAndrew Thompson {
752a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(pc);
75302ac6454SAndrew Thompson }
754bdc081c6SAndrew Thompson #endif
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
757a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all - factored out code
75802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
759bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
76002ac6454SAndrew Thompson void
761a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
76202ac6454SAndrew Thompson {
76302ac6454SAndrew Thompson 	if (cb) {
764a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_flush_all_cb);
76502ac6454SAndrew Thompson 	}
76602ac6454SAndrew Thompson }
767bdc081c6SAndrew Thompson #endif
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
770a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all_cb
77102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
772bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
77302ac6454SAndrew Thompson static void
774a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
775f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
77602ac6454SAndrew Thompson {
77702ac6454SAndrew Thompson 	/* need to initialize the page cache */
77802ac6454SAndrew Thompson 	pc->tag_parent = bus->dma_parent_tag;
77902ac6454SAndrew Thompson 
780a593f6b8SAndrew Thompson 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
78102ac6454SAndrew Thompson 		bus->alloc_failed = 1;
78202ac6454SAndrew Thompson 	}
78302ac6454SAndrew Thompson }
784bdc081c6SAndrew Thompson #endif
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
787a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all - factored out code
78802ac6454SAndrew Thompson  *
78902ac6454SAndrew Thompson  * Returns:
79002ac6454SAndrew Thompson  *    0: Success
79102ac6454SAndrew Thompson  * Else: Failure
79202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
79302ac6454SAndrew Thompson uint8_t
794a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
795e0a69b51SAndrew Thompson     usb_bus_mem_cb_t *cb)
79602ac6454SAndrew Thompson {
79702ac6454SAndrew Thompson 	bus->alloc_failed = 0;
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
80002ac6454SAndrew Thompson 	    NULL, MTX_DEF | MTX_RECURSE);
80102ac6454SAndrew Thompson 
802a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&bus->power_wdog,
803684e3f22SAndrew Thompson 	    &bus->bus_mtx, 0);
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson 	TAILQ_INIT(&bus->intr_q.head);
80602ac6454SAndrew Thompson 
807bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
808a593f6b8SAndrew Thompson 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
809bdc081c6SAndrew Thompson 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
810bdc081c6SAndrew Thompson #endif
81102ac6454SAndrew Thompson 	if ((bus->devices_max > USB_MAX_DEVICES) ||
81202ac6454SAndrew Thompson 	    (bus->devices_max < USB_MIN_DEVICES) ||
81302ac6454SAndrew Thompson 	    (bus->devices == NULL)) {
81402ac6454SAndrew Thompson 		DPRINTFN(0, "Devices field has not been "
815767cb2e2SAndrew Thompson 		    "initialised properly\n");
81602ac6454SAndrew Thompson 		bus->alloc_failed = 1;		/* failure */
81702ac6454SAndrew Thompson 	}
818bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
81902ac6454SAndrew Thompson 	if (cb) {
820a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_alloc_all_cb);
82102ac6454SAndrew Thompson 	}
822bdc081c6SAndrew Thompson #endif
82302ac6454SAndrew Thompson 	if (bus->alloc_failed) {
824a593f6b8SAndrew Thompson 		usb_bus_mem_free_all(bus, cb);
82502ac6454SAndrew Thompson 	}
82602ac6454SAndrew Thompson 	return (bus->alloc_failed);
82702ac6454SAndrew Thompson }
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
830a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all_cb
83102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
832bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
83302ac6454SAndrew Thompson static void
834a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
835f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
83602ac6454SAndrew Thompson {
837a593f6b8SAndrew Thompson 	usb_pc_free_mem(pc);
83802ac6454SAndrew Thompson }
839bdc081c6SAndrew Thompson #endif
84002ac6454SAndrew Thompson 
84102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
842a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all - factored out code
84302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
84402ac6454SAndrew Thompson void
845a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
84602ac6454SAndrew Thompson {
847bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
84802ac6454SAndrew Thompson 	if (cb) {
849a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_free_all_cb);
85002ac6454SAndrew Thompson 	}
851a593f6b8SAndrew Thompson 	usb_dma_tag_unsetup(bus->dma_parent_tag);
852bdc081c6SAndrew Thompson #endif
85302ac6454SAndrew Thompson 
85402ac6454SAndrew Thompson 	mtx_destroy(&bus->bus_mtx);
85502ac6454SAndrew Thompson }
856