xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision ba6c35f0cadbbcf37a89f193609c6dc90f08a8b2)
102ac6454SAndrew Thompson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
28d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
29d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
30d2b99310SHans Petter Selasky #else
31f0c078e6SAndrew Thompson #include "opt_ddb.h"
32f0c078e6SAndrew Thompson 
33ed6d949aSAndrew Thompson #include <sys/stdint.h>
34ed6d949aSAndrew Thompson #include <sys/stddef.h>
35ed6d949aSAndrew Thompson #include <sys/param.h>
36ed6d949aSAndrew Thompson #include <sys/queue.h>
37ed6d949aSAndrew Thompson #include <sys/types.h>
38ed6d949aSAndrew Thompson #include <sys/systm.h>
39ed6d949aSAndrew Thompson #include <sys/kernel.h>
40ed6d949aSAndrew Thompson #include <sys/bus.h>
41ed6d949aSAndrew Thompson #include <sys/module.h>
42ed6d949aSAndrew Thompson #include <sys/lock.h>
43ed6d949aSAndrew Thompson #include <sys/mutex.h>
44ed6d949aSAndrew Thompson #include <sys/condvar.h>
45ed6d949aSAndrew Thompson #include <sys/sysctl.h>
46ed6d949aSAndrew Thompson #include <sys/sx.h>
47ed6d949aSAndrew Thompson #include <sys/unistd.h>
48ed6d949aSAndrew Thompson #include <sys/callout.h>
49ed6d949aSAndrew Thompson #include <sys/malloc.h>
50ed6d949aSAndrew Thompson #include <sys/priv.h>
51ed6d949aSAndrew Thompson 
5202ac6454SAndrew Thompson #include <dev/usb/usb.h>
53ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
5402ac6454SAndrew Thompson 
55a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_ctrl_debug
5602ac6454SAndrew Thompson 
5702ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
63b78e84d1SHans Petter Selasky #include <dev/usb/usb_dev.h>
6402ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6818ec6525SWeongyo Jeong #include <dev/usb/usb_pf.h>
692e141748SHans Petter Selasky #include "usb_if.h"
70d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson /* function prototypes  */
7302ac6454SAndrew Thompson 
74a593f6b8SAndrew Thompson static device_probe_t usb_probe;
75a593f6b8SAndrew Thompson static device_attach_t usb_attach;
76a593f6b8SAndrew Thompson static device_detach_t usb_detach;
772e141748SHans Petter Selasky static device_suspend_t usb_suspend;
782e141748SHans Petter Selasky static device_resume_t usb_resume;
792e141748SHans Petter Selasky static device_shutdown_t usb_shutdown;
8002ac6454SAndrew Thompson 
81a593f6b8SAndrew Thompson static void	usb_attach_sub(device_t, struct usb_bus *);
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson /* static variables */
8402ac6454SAndrew Thompson 
85ed6d949aSAndrew Thompson #ifdef USB_DEBUG
86a593f6b8SAndrew Thompson static int usb_ctrl_debug = 0;
8702ac6454SAndrew Thompson 
88f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
89f8d2b1f3SPawel Biernacki     "USB controller");
90ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RWTUN, &usb_ctrl_debug, 0,
9102ac6454SAndrew Thompson     "Debug level");
9202ac6454SAndrew Thompson #endif
9302ac6454SAndrew Thompson 
94d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
95a0c61406SAlfred Perlstein static int usb_no_boot_wait = 0;
96af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
975ed294aeSHans Petter Selasky     "No USB device enumerate waiting at boot.");
98d2b99310SHans Petter Selasky #endif
995ed294aeSHans Petter Selasky 
10002f728afSHans Petter Selasky static int usb_no_suspend_wait = 0;
101af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RWTUN,
10202f728afSHans Petter Selasky     &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
10302f728afSHans Petter Selasky 
1045ed294aeSHans Petter Selasky static int usb_no_shutdown_wait = 0;
105af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RWTUN,
10602f728afSHans Petter Selasky     &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
107a0c61406SAlfred Perlstein 
108a593f6b8SAndrew Thompson static device_method_t usb_methods[] = {
109a593f6b8SAndrew Thompson 	DEVMETHOD(device_probe, usb_probe),
110a593f6b8SAndrew Thompson 	DEVMETHOD(device_attach, usb_attach),
111a593f6b8SAndrew Thompson 	DEVMETHOD(device_detach, usb_detach),
1122e141748SHans Petter Selasky 	DEVMETHOD(device_suspend, usb_suspend),
1132e141748SHans Petter Selasky 	DEVMETHOD(device_resume, usb_resume),
1142e141748SHans Petter Selasky 	DEVMETHOD(device_shutdown, usb_shutdown),
11561bfd867SSofian Brabez 
11661bfd867SSofian Brabez 	DEVMETHOD_END
11702ac6454SAndrew Thompson };
11802ac6454SAndrew Thompson 
119a593f6b8SAndrew Thompson static driver_t usb_driver = {
12002ac6454SAndrew Thompson 	.name = "usbus",
121a593f6b8SAndrew Thompson 	.methods = usb_methods,
12202ac6454SAndrew Thompson 	.size = 0,
12302ac6454SAndrew Thompson };
12402ac6454SAndrew Thompson 
125864bc412SHans Petter Selasky /* Host Only Drivers */
126bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, ohci, usb_driver, 0, 0);
127bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, uhci, usb_driver, 0, 0);
128bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, ehci, usb_driver, 0, 0);
129bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, xhci, usb_driver, 0, 0);
130864bc412SHans Petter Selasky 
131864bc412SHans Petter Selasky /* Device Only Drivers */
132bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, musbotg, usb_driver, 0, 0);
133bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, uss820dci, usb_driver, 0, 0);
134bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, octusb, usb_driver, 0, 0);
13502ac6454SAndrew Thompson 
136268ae63aSHans Petter Selasky /* Dual Mode Drivers */
137bc9372d7SJohn Baldwin DRIVER_MODULE(usbus, dwcotg, usb_driver, 0, 0);
138268ae63aSHans Petter Selasky 
13902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
140a593f6b8SAndrew Thompson  *	usb_probe
14102ac6454SAndrew Thompson  *
14202ac6454SAndrew Thompson  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
14302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
14402ac6454SAndrew Thompson static int
usb_probe(device_t dev)145a593f6b8SAndrew Thompson usb_probe(device_t dev)
14602ac6454SAndrew Thompson {
14702ac6454SAndrew Thompson 	DPRINTF("\n");
14802ac6454SAndrew Thompson 	return (0);
14902ac6454SAndrew Thompson }
15002ac6454SAndrew Thompson 
151d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
1523df007ceSHans Petter Selasky static void
usb_root_mount_rel(struct usb_bus * bus)1533df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus)
1543df007ceSHans Petter Selasky {
1553df007ceSHans Petter Selasky 	if (bus->bus_roothold != NULL) {
1563df007ceSHans Petter Selasky 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
1573df007ceSHans Petter Selasky 		root_mount_rel(bus->bus_roothold);
1583df007ceSHans Petter Selasky 		bus->bus_roothold = NULL;
1593df007ceSHans Petter Selasky 	}
1603df007ceSHans Petter Selasky }
161d2b99310SHans Petter Selasky #endif
1623df007ceSHans Petter Selasky 
16302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
164a593f6b8SAndrew Thompson  *	usb_attach
16502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
16602ac6454SAndrew Thompson static int
usb_attach(device_t dev)167a593f6b8SAndrew Thompson usb_attach(device_t dev)
16802ac6454SAndrew Thompson {
169760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_ivars(dev);
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson 	DPRINTF("\n");
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	if (bus == NULL) {
174767cb2e2SAndrew Thompson 		device_printf(dev, "USB device has no ivars\n");
17502ac6454SAndrew Thompson 		return (ENXIO);
17602ac6454SAndrew Thompson 	}
17702ac6454SAndrew Thompson 
178d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
179a0c61406SAlfred Perlstein 	if (usb_no_boot_wait == 0) {
18002ac6454SAndrew Thompson 		/* delay vfs_mountroot until the bus is explored */
181853a10a5SAndrew Thompson 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
182a0c61406SAlfred Perlstein 	}
183d2b99310SHans Petter Selasky #endif
184a593f6b8SAndrew Thompson 	usb_attach_sub(dev, bus);
18534b48722SAlfred Perlstein 
18602ac6454SAndrew Thompson 	return (0);			/* return success */
18702ac6454SAndrew Thompson }
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
190a593f6b8SAndrew Thompson  *	usb_detach
19102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
19202ac6454SAndrew Thompson static int
usb_detach(device_t dev)193a593f6b8SAndrew Thompson usb_detach(device_t dev)
19402ac6454SAndrew Thompson {
195760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_softc(dev);
19602ac6454SAndrew Thompson 
19702ac6454SAndrew Thompson 	DPRINTF("\n");
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	if (bus == NULL) {
20002ac6454SAndrew Thompson 		/* was never setup properly */
20102ac6454SAndrew Thompson 		return (0);
20202ac6454SAndrew Thompson 	}
20302ac6454SAndrew Thompson 	/* Stop power watchdog */
204a593f6b8SAndrew Thompson 	usb_callout_drain(&bus->power_wdog);
20502ac6454SAndrew Thompson 
206d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
20702ac6454SAndrew Thompson 	/* Let the USB explore process detach all devices. */
2083df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
209d2b99310SHans Petter Selasky #endif
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
21202ac6454SAndrew Thompson 
2132e141748SHans Petter Selasky 	/* Queue detach job */
2149b3a48eeSHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2152e141748SHans Petter Selasky 	    &bus->detach_msg[0], &bus->detach_msg[1]);
2162e141748SHans Petter Selasky 
2172e141748SHans Petter Selasky 	/* Wait for detach to complete */
2189b3a48eeSHans Petter Selasky 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
21902ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1]);
22002ac6454SAndrew Thompson 
221b78e84d1SHans Petter Selasky #if USB_HAVE_UGEN
222b78e84d1SHans Petter Selasky 	/* Wait for cleanup to complete */
223b78e84d1SHans Petter Selasky 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
224b78e84d1SHans Petter Selasky 	    &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
225b78e84d1SHans Petter Selasky #endif
22602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
22702ac6454SAndrew Thompson 
2289b3a48eeSHans Petter Selasky #if USB_HAVE_PER_BUS_PROCESS
22902ac6454SAndrew Thompson 	/* Get rid of USB callback processes */
23002ac6454SAndrew Thompson 
2319b3a48eeSHans Petter Selasky 	usb_proc_free(USB_BUS_GIANT_PROC(bus));
23243ea03d7SHans Petter Selasky 	usb_proc_free(USB_BUS_NON_GIANT_ISOC_PROC(bus));
23343ea03d7SHans Petter Selasky 	usb_proc_free(USB_BUS_NON_GIANT_BULK_PROC(bus));
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 	/* Get rid of USB explore process */
23602ac6454SAndrew Thompson 
2379b3a48eeSHans Petter Selasky 	usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
23802ac6454SAndrew Thompson 
239672c9965SAndrew Thompson 	/* Get rid of control transfer process */
240672c9965SAndrew Thompson 
2419b3a48eeSHans Petter Selasky 	usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
2429b3a48eeSHans Petter Selasky #endif
243672c9965SAndrew Thompson 
2448be09334SHans Petter Selasky #if USB_HAVE_PF
245fe1c24e3SWeongyo Jeong 	usbpf_detach(bus);
2468be09334SHans Petter Selasky #endif
24702ac6454SAndrew Thompson 	return (0);
24802ac6454SAndrew Thompson }
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2512e141748SHans Petter Selasky  *	usb_suspend
2522e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2532e141748SHans Petter Selasky static int
usb_suspend(device_t dev)2542e141748SHans Petter Selasky usb_suspend(device_t dev)
2552e141748SHans Petter Selasky {
2562e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2572e141748SHans Petter Selasky 
2582e141748SHans Petter Selasky 	DPRINTF("\n");
2592e141748SHans Petter Selasky 
2602e141748SHans Petter Selasky 	if (bus == NULL) {
2612e141748SHans Petter Selasky 		/* was never setup properly */
2622e141748SHans Petter Selasky 		return (0);
2632e141748SHans Petter Selasky 	}
2642e141748SHans Petter Selasky 
2652e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2669b3a48eeSHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2672e141748SHans Petter Selasky 	    &bus->suspend_msg[0], &bus->suspend_msg[1]);
26802f728afSHans Petter Selasky 	if (usb_no_suspend_wait == 0) {
26902f728afSHans Petter Selasky 		/* wait for suspend callback to be executed */
2709b3a48eeSHans Petter Selasky 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
27102f728afSHans Petter Selasky 		    &bus->suspend_msg[0], &bus->suspend_msg[1]);
27202f728afSHans Petter Selasky 	}
2732e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2742e141748SHans Petter Selasky 
2752e141748SHans Petter Selasky 	return (0);
2762e141748SHans Petter Selasky }
2772e141748SHans Petter Selasky 
2782e141748SHans Petter Selasky /*------------------------------------------------------------------------*
2792e141748SHans Petter Selasky  *	usb_resume
2802e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
2812e141748SHans Petter Selasky static int
usb_resume(device_t dev)2822e141748SHans Petter Selasky usb_resume(device_t dev)
2832e141748SHans Petter Selasky {
2842e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
2852e141748SHans Petter Selasky 
2862e141748SHans Petter Selasky 	DPRINTF("\n");
2872e141748SHans Petter Selasky 
2882e141748SHans Petter Selasky 	if (bus == NULL) {
2892e141748SHans Petter Selasky 		/* was never setup properly */
2902e141748SHans Petter Selasky 		return (0);
2912e141748SHans Petter Selasky 	}
2922e141748SHans Petter Selasky 
2932e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
2949b3a48eeSHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2952e141748SHans Petter Selasky 	    &bus->resume_msg[0], &bus->resume_msg[1]);
2962e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2972e141748SHans Petter Selasky 
2982e141748SHans Petter Selasky 	return (0);
2992e141748SHans Petter Selasky }
3002e141748SHans Petter Selasky 
3012e141748SHans Petter Selasky /*------------------------------------------------------------------------*
302563ab081SHans Petter Selasky  *	usb_bus_reset_async_locked
303563ab081SHans Petter Selasky  *------------------------------------------------------------------------*/
304563ab081SHans Petter Selasky void
usb_bus_reset_async_locked(struct usb_bus * bus)305563ab081SHans Petter Selasky usb_bus_reset_async_locked(struct usb_bus *bus)
306563ab081SHans Petter Selasky {
307563ab081SHans Petter Selasky 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
308563ab081SHans Petter Selasky 
309563ab081SHans Petter Selasky 	DPRINTF("\n");
310563ab081SHans Petter Selasky 
311563ab081SHans Petter Selasky 	if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
312563ab081SHans Petter Selasky 	    bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
313563ab081SHans Petter Selasky 		DPRINTF("Reset already pending\n");
314563ab081SHans Petter Selasky 		return;
315563ab081SHans Petter Selasky 	}
316563ab081SHans Petter Selasky 
317563ab081SHans Petter Selasky 	device_printf(bus->parent, "Resetting controller\n");
318563ab081SHans Petter Selasky 
319563ab081SHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
320563ab081SHans Petter Selasky 	    &bus->reset_msg[0], &bus->reset_msg[1]);
321563ab081SHans Petter Selasky }
322563ab081SHans Petter Selasky 
323563ab081SHans Petter Selasky /*------------------------------------------------------------------------*
3242e141748SHans Petter Selasky  *	usb_shutdown
3252e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
3262e141748SHans Petter Selasky static int
usb_shutdown(device_t dev)3272e141748SHans Petter Selasky usb_shutdown(device_t dev)
3282e141748SHans Petter Selasky {
3292e141748SHans Petter Selasky 	struct usb_bus *bus = device_get_softc(dev);
3302e141748SHans Petter Selasky 
3312e141748SHans Petter Selasky 	DPRINTF("\n");
3322e141748SHans Petter Selasky 
3332e141748SHans Petter Selasky 	if (bus == NULL) {
3342e141748SHans Petter Selasky 		/* was never setup properly */
3352e141748SHans Petter Selasky 		return (0);
3362e141748SHans Petter Selasky 	}
3372e141748SHans Petter Selasky 
338cabe79d9SMarius Strobl 	DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
3395ed294aeSHans Petter Selasky 
3402e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
3419b3a48eeSHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
3422e141748SHans Petter Selasky 	    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
3435ed294aeSHans Petter Selasky 	if (usb_no_shutdown_wait == 0) {
3445ed294aeSHans Petter Selasky 		/* wait for shutdown callback to be executed */
3459b3a48eeSHans Petter Selasky 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
3465ed294aeSHans Petter Selasky 		    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
3475ed294aeSHans Petter Selasky 	}
3482e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
3492e141748SHans Petter Selasky 
350cabe79d9SMarius Strobl 	DPRINTF("%s: Controller shutdown complete\n",
351cabe79d9SMarius Strobl 	    device_get_nameunit(bus->bdev));
3525ed294aeSHans Petter Selasky 
3532e141748SHans Petter Selasky 	return (0);
3542e141748SHans Petter Selasky }
3552e141748SHans Petter Selasky 
3562e141748SHans Petter Selasky /*------------------------------------------------------------------------*
357a593f6b8SAndrew Thompson  *	usb_bus_explore
35802ac6454SAndrew Thompson  *
35902ac6454SAndrew Thompson  * This function is used to explore the device tree from the root.
36002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
36102ac6454SAndrew Thompson static void
usb_bus_explore(struct usb_proc_msg * pm)362a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm)
36302ac6454SAndrew Thompson {
364760bc48eSAndrew Thompson 	struct usb_bus *bus;
365760bc48eSAndrew Thompson 	struct usb_device *udev;
36602ac6454SAndrew Thompson 
367760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
36802ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
36902ac6454SAndrew Thompson 
3702e141748SHans Petter Selasky 	if (bus->no_explore != 0)
3712e141748SHans Petter Selasky 		return;
3722e141748SHans Petter Selasky 
373d64e9217SHans Petter Selasky 	if (udev != NULL) {
374d64e9217SHans Petter Selasky 		USB_BUS_UNLOCK(bus);
375d64e9217SHans Petter Selasky 		uhub_explore_handle_re_enumerate(udev);
376d64e9217SHans Petter Selasky 		USB_BUS_LOCK(bus);
377d64e9217SHans Petter Selasky 	}
378d64e9217SHans Petter Selasky 
379f80ccb40SHans Petter Selasky 	if (udev != NULL && udev->hub != NULL) {
38002ac6454SAndrew Thompson 		if (bus->do_probe) {
38102ac6454SAndrew Thompson 			bus->do_probe = 0;
38202ac6454SAndrew Thompson 			bus->driver_added_refcount++;
38302ac6454SAndrew Thompson 		}
38402ac6454SAndrew Thompson 		if (bus->driver_added_refcount == 0) {
38502ac6454SAndrew Thompson 			/* avoid zero, hence that is memory default */
38602ac6454SAndrew Thompson 			bus->driver_added_refcount = 1;
38702ac6454SAndrew Thompson 		}
38802ac6454SAndrew Thompson 
389f0c078e6SAndrew Thompson #ifdef DDB
39034b48722SAlfred Perlstein 		/*
39134b48722SAlfred Perlstein 		 * The following three lines of code are only here to
39234b48722SAlfred Perlstein 		 * recover from DDB:
39334b48722SAlfred Perlstein 		 */
3949b3a48eeSHans Petter Selasky 		usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
3959b3a48eeSHans Petter Selasky 		usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
39643ea03d7SHans Petter Selasky 		usb_proc_rewakeup(USB_BUS_NON_GIANT_ISOC_PROC(bus));
39743ea03d7SHans Petter Selasky 		usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_PROC(bus));
398f0c078e6SAndrew Thompson #endif
39934b48722SAlfred Perlstein 
40034b48722SAlfred Perlstein 		USB_BUS_UNLOCK(bus);
401a56fe095SJohn Baldwin 
402e727a16cSAndrew Thompson #if USB_HAVE_POWERD
40302ac6454SAndrew Thompson 		/*
40402ac6454SAndrew Thompson 		 * First update the USB power state!
40502ac6454SAndrew Thompson 		 */
406a593f6b8SAndrew Thompson 		usb_bus_powerd(bus);
407e727a16cSAndrew Thompson #endif
40834b48722SAlfred Perlstein 		 /* Explore the Root USB HUB. */
40902ac6454SAndrew Thompson 		(udev->hub->explore) (udev);
41002ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
41102ac6454SAndrew Thompson 	}
412d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
4133df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
414d2b99310SHans Petter Selasky #endif
41555a3bd00SHans Petter Selasky 
41655a3bd00SHans Petter Selasky 	/* Nice the enumeration a bit, to avoid looping too fast. */
4178758aabbSHans Petter Selasky 	usb_pause_mtx(&bus->bus_mtx, USB_MS_TO_TICKS(usb_enum_nice_time));
41802ac6454SAndrew Thompson }
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
421a593f6b8SAndrew Thompson  *	usb_bus_detach
42202ac6454SAndrew Thompson  *
42302ac6454SAndrew Thompson  * This function is used to detach the device tree from the root.
42402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
42502ac6454SAndrew Thompson static void
usb_bus_detach(struct usb_proc_msg * pm)426a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm)
42702ac6454SAndrew Thompson {
428760bc48eSAndrew Thompson 	struct usb_bus *bus;
429760bc48eSAndrew Thompson 	struct usb_device *udev;
43002ac6454SAndrew Thompson 	device_t dev;
43102ac6454SAndrew Thompson 
432760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
43302ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
43402ac6454SAndrew Thompson 	dev = bus->bdev;
43502ac6454SAndrew Thompson 	/* clear the softc */
43602ac6454SAndrew Thompson 	device_set_softc(dev, NULL);
43702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
43802ac6454SAndrew Thompson 
43902ac6454SAndrew Thompson 	/* detach children first */
440c6df6f53SWarner Losh 	bus_topo_lock();
441*ba6c35f0SJohn Baldwin 	bus_detach_children(dev);
442c6df6f53SWarner Losh 	bus_topo_unlock();
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 	/*
445d88688c7SAndrew Thompson 	 * Free USB device and all subdevices, if any.
44602ac6454SAndrew Thompson 	 */
447d88688c7SAndrew Thompson 	usb_free_device(udev, 0);
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
45002ac6454SAndrew Thompson 	/* clear bdev variable last */
45102ac6454SAndrew Thompson 	bus->bdev = NULL;
45202ac6454SAndrew Thompson }
45302ac6454SAndrew Thompson 
4542e141748SHans Petter Selasky /*------------------------------------------------------------------------*
4552e141748SHans Petter Selasky  *	usb_bus_suspend
4562e141748SHans Petter Selasky  *
457468f354bSHans Petter Selasky  * This function is used to suspend the USB controller.
4582e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
4592e141748SHans Petter Selasky static void
usb_bus_suspend(struct usb_proc_msg * pm)4602e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm)
4612e141748SHans Petter Selasky {
4622e141748SHans Petter Selasky 	struct usb_bus *bus;
4632e141748SHans Petter Selasky 	struct usb_device *udev;
4642e141748SHans Petter Selasky 	usb_error_t err;
465a18a7a41SHans Petter Selasky 	uint8_t do_unlock;
4662e141748SHans Petter Selasky 
467563ab081SHans Petter Selasky 	DPRINTF("\n");
468563ab081SHans Petter Selasky 
4692e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
4702e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
4712e141748SHans Petter Selasky 
4722e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
4732e141748SHans Petter Selasky 		return;
4742e141748SHans Petter Selasky 
4756bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4766bd3e535SHans Petter Selasky 
47702f728afSHans Petter Selasky 	/*
47802f728afSHans Petter Selasky 	 * We use the shutdown event here because the suspend and
47902f728afSHans Petter Selasky 	 * resume events are reserved for the USB port suspend and
48002f728afSHans Petter Selasky 	 * resume. The USB system suspend is implemented like full
48102f728afSHans Petter Selasky 	 * shutdown and all connected USB devices will be disconnected
48202f728afSHans Petter Selasky 	 * subsequently. At resume all USB devices will be
48302f728afSHans Petter Selasky 	 * re-connected again.
48402f728afSHans Petter Selasky 	 */
48502f728afSHans Petter Selasky 
4862e141748SHans Petter Selasky 	bus_generic_shutdown(bus->bdev);
4872e141748SHans Petter Selasky 
488a18a7a41SHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
4892e141748SHans Petter Selasky 
4902e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
4912e141748SHans Petter Selasky 	if (err)
4922e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
4932e141748SHans Petter Selasky 
4942e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
4952e141748SHans Petter Selasky 	bus->hw_power_state = 0;
4962e141748SHans Petter Selasky 	bus->no_explore = 1;
4972e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
4982e141748SHans Petter Selasky 
4992e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
5002e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
5012e141748SHans Petter Selasky 
5022e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
5032e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
5042e141748SHans Petter Selasky 
505a18a7a41SHans Petter Selasky 	if (do_unlock)
5062e141748SHans Petter Selasky 		usbd_enum_unlock(udev);
5076bd3e535SHans Petter Selasky 
5086bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
5092e141748SHans Petter Selasky }
5102e141748SHans Petter Selasky 
5112e141748SHans Petter Selasky /*------------------------------------------------------------------------*
5122e141748SHans Petter Selasky  *	usb_bus_resume
5132e141748SHans Petter Selasky  *
514468f354bSHans Petter Selasky  * This function is used to resume the USB controller.
5152e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
5162e141748SHans Petter Selasky static void
usb_bus_resume(struct usb_proc_msg * pm)5172e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm)
5182e141748SHans Petter Selasky {
5192e141748SHans Petter Selasky 	struct usb_bus *bus;
5202e141748SHans Petter Selasky 	struct usb_device *udev;
5212e141748SHans Petter Selasky 	usb_error_t err;
522a18a7a41SHans Petter Selasky 	uint8_t do_unlock;
5232e141748SHans Petter Selasky 
524563ab081SHans Petter Selasky 	DPRINTF("\n");
525563ab081SHans Petter Selasky 
5262e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
5272e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
5282e141748SHans Petter Selasky 
5292e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
5302e141748SHans Petter Selasky 		return;
5312e141748SHans Petter Selasky 
5326bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
5336bd3e535SHans Petter Selasky 
534a18a7a41SHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
5352e141748SHans Petter Selasky #if 0
5362e141748SHans Petter Selasky 	DEVMETHOD(usb_take_controller, NULL);	/* dummy */
5372e141748SHans Petter Selasky #endif
5382e141748SHans Petter Selasky 	USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
5392e141748SHans Petter Selasky 
5402e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
5412e141748SHans Petter Selasky  	bus->hw_power_state =
5422e141748SHans Petter Selasky 	  USB_HW_POWER_CONTROL |
5432e141748SHans Petter Selasky 	  USB_HW_POWER_BULK |
5442e141748SHans Petter Selasky 	  USB_HW_POWER_INTERRUPT |
5452e141748SHans Petter Selasky 	  USB_HW_POWER_ISOC |
5462e141748SHans Petter Selasky 	  USB_HW_POWER_NON_ROOT_HUB;
5472e141748SHans Petter Selasky 	bus->no_explore = 0;
5482e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
5492e141748SHans Petter Selasky 
5502e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
5512e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
5522e141748SHans Petter Selasky 
5532e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
5542e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
5552e141748SHans Petter Selasky 
5566bbe7cdfSHans Petter Selasky 	/* restore USB configuration to index 0 */
5572e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, 0);
5582e141748SHans Petter Selasky 	if (err)
5592e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not configure root HUB\n");
5602e141748SHans Petter Selasky 
5616bbe7cdfSHans Petter Selasky 	/* probe and attach */
5626bbe7cdfSHans Petter Selasky 	err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
5636bbe7cdfSHans Petter Selasky 	if (err) {
5646bbe7cdfSHans Petter Selasky 		device_printf(bus->bdev, "Could not probe and "
5656bbe7cdfSHans Petter Selasky 		    "attach root HUB\n");
5666bbe7cdfSHans Petter Selasky 	}
5676bbe7cdfSHans Petter Selasky 
568a18a7a41SHans Petter Selasky 	if (do_unlock)
5692e141748SHans Petter Selasky 		usbd_enum_unlock(udev);
5706bd3e535SHans Petter Selasky 
5716bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
5722e141748SHans Petter Selasky }
5732e141748SHans Petter Selasky 
5742e141748SHans Petter Selasky /*------------------------------------------------------------------------*
575563ab081SHans Petter Selasky  *	usb_bus_reset
576563ab081SHans Petter Selasky  *
577468f354bSHans Petter Selasky  * This function is used to reset the USB controller.
578563ab081SHans Petter Selasky  *------------------------------------------------------------------------*/
579563ab081SHans Petter Selasky static void
usb_bus_reset(struct usb_proc_msg * pm)580563ab081SHans Petter Selasky usb_bus_reset(struct usb_proc_msg *pm)
581563ab081SHans Petter Selasky {
582563ab081SHans Petter Selasky 	struct usb_bus *bus;
583563ab081SHans Petter Selasky 
584563ab081SHans Petter Selasky 	DPRINTF("\n");
585563ab081SHans Petter Selasky 
586563ab081SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
587563ab081SHans Petter Selasky 
588563ab081SHans Petter Selasky 	if (bus->bdev == NULL || bus->no_explore != 0)
589563ab081SHans Petter Selasky 		return;
590563ab081SHans Petter Selasky 
591563ab081SHans Petter Selasky 	/* a suspend and resume will reset the USB controller */
592563ab081SHans Petter Selasky 	usb_bus_suspend(pm);
593563ab081SHans Petter Selasky 	usb_bus_resume(pm);
594563ab081SHans Petter Selasky }
595563ab081SHans Petter Selasky 
596563ab081SHans Petter Selasky /*------------------------------------------------------------------------*
5972e141748SHans Petter Selasky  *	usb_bus_shutdown
5982e141748SHans Petter Selasky  *
599468f354bSHans Petter Selasky  * This function is used to shutdown the USB controller.
6002e141748SHans Petter Selasky  *------------------------------------------------------------------------*/
6012e141748SHans Petter Selasky static void
usb_bus_shutdown(struct usb_proc_msg * pm)6022e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm)
6032e141748SHans Petter Selasky {
6042e141748SHans Petter Selasky 	struct usb_bus *bus;
6052e141748SHans Petter Selasky 	struct usb_device *udev;
6062e141748SHans Petter Selasky 	usb_error_t err;
607a18a7a41SHans Petter Selasky 	uint8_t do_unlock;
6082e141748SHans Petter Selasky 
6092e141748SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
6102e141748SHans Petter Selasky 	udev = bus->devices[USB_ROOT_HUB_ADDR];
6112e141748SHans Petter Selasky 
6122e141748SHans Petter Selasky 	if (udev == NULL || bus->bdev == NULL)
6132e141748SHans Petter Selasky 		return;
6142e141748SHans Petter Selasky 
6156bd3e535SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
6166bd3e535SHans Petter Selasky 
6172e141748SHans Petter Selasky 	bus_generic_shutdown(bus->bdev);
6182e141748SHans Petter Selasky 
619a18a7a41SHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
6202e141748SHans Petter Selasky 
6212e141748SHans Petter Selasky 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
6222e141748SHans Petter Selasky 	if (err)
6232e141748SHans Petter Selasky 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
6242e141748SHans Petter Selasky 
6252e141748SHans Petter Selasky 	USB_BUS_LOCK(bus);
6262e141748SHans Petter Selasky 	bus->hw_power_state = 0;
6272e141748SHans Petter Selasky 	bus->no_explore = 1;
6282e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
6292e141748SHans Petter Selasky 
6302e141748SHans Petter Selasky 	if (bus->methods->set_hw_power != NULL)
6312e141748SHans Petter Selasky 		(bus->methods->set_hw_power) (bus);
6322e141748SHans Petter Selasky 
6332e141748SHans Petter Selasky 	if (bus->methods->set_hw_power_sleep != NULL)
6342e141748SHans Petter Selasky 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
6352e141748SHans Petter Selasky 
636a18a7a41SHans Petter Selasky 	if (do_unlock)
6372e141748SHans Petter Selasky 		usbd_enum_unlock(udev);
6386bd3e535SHans Petter Selasky 
6396bd3e535SHans Petter Selasky 	USB_BUS_LOCK(bus);
6402e141748SHans Petter Selasky }
6412e141748SHans Petter Selasky 
642b78e84d1SHans Petter Selasky /*------------------------------------------------------------------------*
643b78e84d1SHans Petter Selasky  *	usb_bus_cleanup
644b78e84d1SHans Petter Selasky  *
645b78e84d1SHans Petter Selasky  * This function is used to cleanup leftover USB character devices.
646b78e84d1SHans Petter Selasky  *------------------------------------------------------------------------*/
647b78e84d1SHans Petter Selasky #if USB_HAVE_UGEN
648b78e84d1SHans Petter Selasky static void
usb_bus_cleanup(struct usb_proc_msg * pm)649b78e84d1SHans Petter Selasky usb_bus_cleanup(struct usb_proc_msg *pm)
650b78e84d1SHans Petter Selasky {
651b78e84d1SHans Petter Selasky 	struct usb_bus *bus;
652b78e84d1SHans Petter Selasky 	struct usb_fs_privdata *pd;
653b78e84d1SHans Petter Selasky 
654b78e84d1SHans Petter Selasky 	bus = ((struct usb_bus_msg *)pm)->bus;
655b78e84d1SHans Petter Selasky 
6565b8f97d8SBjoern A. Zeeb 	while ((pd = SLIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
6575b8f97d8SBjoern A. Zeeb 		SLIST_REMOVE(&bus->pd_cleanup_list, pd, usb_fs_privdata, pd_next);
658b78e84d1SHans Petter Selasky 		USB_BUS_UNLOCK(bus);
659b78e84d1SHans Petter Selasky 
660b78e84d1SHans Petter Selasky 		usb_destroy_dev_sync(pd);
661b78e84d1SHans Petter Selasky 
662b78e84d1SHans Petter Selasky 		USB_BUS_LOCK(bus);
663b78e84d1SHans Petter Selasky 	}
664b78e84d1SHans Petter Selasky }
665b78e84d1SHans Petter Selasky #endif
666b78e84d1SHans Petter Selasky 
66702ac6454SAndrew Thompson static void
usb_power_wdog(void * arg)668a593f6b8SAndrew Thompson usb_power_wdog(void *arg)
66902ac6454SAndrew Thompson {
670760bc48eSAndrew Thompson 	struct usb_bus *bus = arg;
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
67302ac6454SAndrew Thompson 
674a593f6b8SAndrew Thompson 	usb_callout_reset(&bus->power_wdog,
675a593f6b8SAndrew Thompson 	    4 * hz, usb_power_wdog, arg);
67602ac6454SAndrew Thompson 
677f0c078e6SAndrew Thompson #ifdef DDB
67834b48722SAlfred Perlstein 	/*
67934b48722SAlfred Perlstein 	 * The following line of code is only here to recover from
68034b48722SAlfred Perlstein 	 * DDB:
68134b48722SAlfred Perlstein 	 */
6829b3a48eeSHans Petter Selasky 	usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus));	/* recover from DDB */
683f0c078e6SAndrew Thompson #endif
68434b48722SAlfred Perlstein 
685e727a16cSAndrew Thompson #if USB_HAVE_POWERD
68602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
68702ac6454SAndrew Thompson 
688a593f6b8SAndrew Thompson 	usb_bus_power_update(bus);
68902ac6454SAndrew Thompson 
690684e3f22SAndrew Thompson 	USB_BUS_LOCK(bus);
691e727a16cSAndrew Thompson #endif
69202ac6454SAndrew Thompson }
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
695a593f6b8SAndrew Thompson  *	usb_bus_attach
69602ac6454SAndrew Thompson  *
69702ac6454SAndrew Thompson  * This function attaches USB in context of the explore thread.
69802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
69902ac6454SAndrew Thompson static void
usb_bus_attach(struct usb_proc_msg * pm)700a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm)
70102ac6454SAndrew Thompson {
702760bc48eSAndrew Thompson 	struct usb_bus *bus;
703760bc48eSAndrew Thompson 	struct usb_device *child;
70402ac6454SAndrew Thompson 	device_t dev;
705e0a69b51SAndrew Thompson 	usb_error_t err;
7068d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
70702ac6454SAndrew Thompson 
708760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
70902ac6454SAndrew Thompson 	dev = bus->bdev;
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 	DPRINTF("\n");
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 	switch (bus->usbrev) {
71402ac6454SAndrew Thompson 	case USB_REV_1_0:
71502ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
71602ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
71702ac6454SAndrew Thompson 		break;
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson 	case USB_REV_1_1:
72002ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
72102ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
72202ac6454SAndrew Thompson 		break;
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	case USB_REV_2_0:
72502ac6454SAndrew Thompson 		speed = USB_SPEED_HIGH;
72602ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
72702ac6454SAndrew Thompson 		break;
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 	case USB_REV_2_5:
73002ac6454SAndrew Thompson 		speed = USB_SPEED_VARIABLE;
73102ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
73202ac6454SAndrew Thompson 		break;
73302ac6454SAndrew Thompson 
734963169b4SHans Petter Selasky 	case USB_REV_3_0:
735963169b4SHans Petter Selasky 		speed = USB_SPEED_SUPER;
736ccac019aSHans Petter Selasky 		device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
737963169b4SHans Petter Selasky 		break;
738963169b4SHans Petter Selasky 
73902ac6454SAndrew Thompson 	default:
740767cb2e2SAndrew Thompson 		device_printf(bus->bdev, "Unsupported USB revision\n");
741d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
7423df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
743d2b99310SHans Petter Selasky #endif
74402ac6454SAndrew Thompson 		return;
74502ac6454SAndrew Thompson 	}
74602ac6454SAndrew Thompson 
7474eae601eSAndrew Thompson 	/* default power_mask value */
7484eae601eSAndrew Thompson 	bus->hw_power_state =
7494eae601eSAndrew Thompson 	  USB_HW_POWER_CONTROL |
7504eae601eSAndrew Thompson 	  USB_HW_POWER_BULK |
7514eae601eSAndrew Thompson 	  USB_HW_POWER_INTERRUPT |
7524eae601eSAndrew Thompson 	  USB_HW_POWER_ISOC |
7534eae601eSAndrew Thompson 	  USB_HW_POWER_NON_ROOT_HUB;
7544eae601eSAndrew Thompson 
7552e141748SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
7562e141748SHans Petter Selasky 
7574eae601eSAndrew Thompson 	/* make sure power is set at least once */
7584eae601eSAndrew Thompson 
7594eae601eSAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
7604eae601eSAndrew Thompson 		(bus->methods->set_hw_power) (bus);
7614eae601eSAndrew Thompson 	}
7624eae601eSAndrew Thompson 
7632e141748SHans Petter Selasky 	/* allocate the Root USB device */
76402ac6454SAndrew Thompson 
765a593f6b8SAndrew Thompson 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
76602ac6454SAndrew Thompson 	    speed, USB_MODE_HOST);
76702ac6454SAndrew Thompson 	if (child) {
768a593f6b8SAndrew Thompson 		err = usb_probe_and_attach(child,
76902ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
77002ac6454SAndrew Thompson 		if (!err) {
7711be5bf51SAndrew Thompson 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
7721be5bf51SAndrew Thompson 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
77302ac6454SAndrew Thompson 				err = USB_ERR_NO_ROOT_HUB;
77402ac6454SAndrew Thompson 			}
77502ac6454SAndrew Thompson 		}
77602ac6454SAndrew Thompson 	} else {
77702ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
77802ac6454SAndrew Thompson 	}
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson 	if (err) {
78302ac6454SAndrew Thompson 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
784a593f6b8SAndrew Thompson 		    usbd_errstr(err));
785d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD
7863df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
787d2b99310SHans Petter Selasky #endif
78802ac6454SAndrew Thompson 	}
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	/* set softc - we are ready */
79102ac6454SAndrew Thompson 	device_set_softc(dev, bus);
79202ac6454SAndrew Thompson 
793684e3f22SAndrew Thompson 	/* start watchdog */
794a593f6b8SAndrew Thompson 	usb_power_wdog(bus);
79502ac6454SAndrew Thompson }
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
798a593f6b8SAndrew Thompson  *	usb_attach_sub
79902ac6454SAndrew Thompson  *
80034b48722SAlfred Perlstein  * This function creates a thread which runs the USB attach code.
80102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
80202ac6454SAndrew Thompson static void
usb_attach_sub(device_t dev,struct usb_bus * bus)803a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus)
80402ac6454SAndrew Thompson {
805c6df6f53SWarner Losh 	bus_topo_lock();
80634b48722SAlfred Perlstein 	if (usb_devclass_ptr == NULL)
80734b48722SAlfred Perlstein 		usb_devclass_ptr = devclass_find("usbus");
808c6df6f53SWarner Losh 	bus_topo_unlock();
80934b48722SAlfred Perlstein 
8108be09334SHans Petter Selasky #if USB_HAVE_PF
811fe1c24e3SWeongyo Jeong 	usbpf_attach(bus);
8128be09334SHans Petter Selasky #endif
81302ac6454SAndrew Thompson 	/* Initialise USB process messages */
814a593f6b8SAndrew Thompson 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
81502ac6454SAndrew Thompson 	bus->explore_msg[0].bus = bus;
816a593f6b8SAndrew Thompson 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
81702ac6454SAndrew Thompson 	bus->explore_msg[1].bus = bus;
81802ac6454SAndrew Thompson 
819a593f6b8SAndrew Thompson 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
82002ac6454SAndrew Thompson 	bus->detach_msg[0].bus = bus;
821a593f6b8SAndrew Thompson 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
82202ac6454SAndrew Thompson 	bus->detach_msg[1].bus = bus;
82302ac6454SAndrew Thompson 
824a593f6b8SAndrew Thompson 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
82502ac6454SAndrew Thompson 	bus->attach_msg[0].bus = bus;
826a593f6b8SAndrew Thompson 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
82702ac6454SAndrew Thompson 	bus->attach_msg[1].bus = bus;
82802ac6454SAndrew Thompson 
8292e141748SHans Petter Selasky 	bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
8302e141748SHans Petter Selasky 	bus->suspend_msg[0].bus = bus;
8312e141748SHans Petter Selasky 	bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
8322e141748SHans Petter Selasky 	bus->suspend_msg[1].bus = bus;
8332e141748SHans Petter Selasky 
8342e141748SHans Petter Selasky 	bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
8352e141748SHans Petter Selasky 	bus->resume_msg[0].bus = bus;
8362e141748SHans Petter Selasky 	bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
8372e141748SHans Petter Selasky 	bus->resume_msg[1].bus = bus;
8382e141748SHans Petter Selasky 
839563ab081SHans Petter Selasky 	bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
840563ab081SHans Petter Selasky 	bus->reset_msg[0].bus = bus;
841563ab081SHans Petter Selasky 	bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
842563ab081SHans Petter Selasky 	bus->reset_msg[1].bus = bus;
843563ab081SHans Petter Selasky 
8442e141748SHans Petter Selasky 	bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
8452e141748SHans Petter Selasky 	bus->shutdown_msg[0].bus = bus;
8462e141748SHans Petter Selasky 	bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
8472e141748SHans Petter Selasky 	bus->shutdown_msg[1].bus = bus;
8482e141748SHans Petter Selasky 
849b78e84d1SHans Petter Selasky #if USB_HAVE_UGEN
8505b8f97d8SBjoern A. Zeeb 	SLIST_INIT(&bus->pd_cleanup_list);
851b78e84d1SHans Petter Selasky 	bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup;
852b78e84d1SHans Petter Selasky 	bus->cleanup_msg[0].bus = bus;
853b78e84d1SHans Petter Selasky 	bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup;
854b78e84d1SHans Petter Selasky 	bus->cleanup_msg[1].bus = bus;
855b78e84d1SHans Petter Selasky #endif
856b78e84d1SHans Petter Selasky 
8579b3a48eeSHans Petter Selasky #if USB_HAVE_PER_BUS_PROCESS
85839307315SAndrew Thompson 	/* Create USB explore and callback processes */
85902ac6454SAndrew Thompson 
8609b3a48eeSHans Petter Selasky 	if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
8619b3a48eeSHans Petter Selasky 	    &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
86288334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB Giant "
86302ac6454SAndrew Thompson 		    "callback process failed.\n");
86443ea03d7SHans Petter Selasky 	} else if (usb_proc_create(USB_BUS_NON_GIANT_ISOC_PROC(bus),
86543ea03d7SHans Petter Selasky 	    &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGHEST)) {
86643ea03d7SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB non-Giant ISOC "
86743ea03d7SHans Petter Selasky 		    "callback process failed.\n");
86843ea03d7SHans Petter Selasky 	} else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus),
8699b3a48eeSHans Petter Selasky 	    &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) {
87043ea03d7SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB non-Giant BULK "
87102ac6454SAndrew Thompson 		    "callback process failed.\n");
8729b3a48eeSHans Petter Selasky 	} else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
8739b3a48eeSHans Petter Selasky 	    &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
87488334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB explore "
87502ac6454SAndrew Thompson 		    "process failed.\n");
8769b3a48eeSHans Petter Selasky 	} else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
8779b3a48eeSHans Petter Selasky 	    &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
87888334428SHans Petter Selasky 		device_printf(dev, "WARNING: Creation of USB control transfer "
879672c9965SAndrew Thompson 		    "process failed.\n");
8809b3a48eeSHans Petter Selasky 	} else
8819b3a48eeSHans Petter Selasky #endif
8829b3a48eeSHans Petter Selasky 	{
88302ac6454SAndrew Thompson 		/* Get final attach going */
88402ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
8859b3a48eeSHans Petter Selasky 		usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
8862e141748SHans Petter Selasky 		    &bus->attach_msg[0], &bus->attach_msg[1]);
88702ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
88834b48722SAlfred Perlstein 
88934b48722SAlfred Perlstein 		/* Do initial explore */
89034b48722SAlfred Perlstein 		usb_needs_explore(bus, 1);
89102ac6454SAndrew Thompson 	}
89202ac6454SAndrew Thompson }
893a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
89402ac6454SAndrew Thompson 
89502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
896a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all_cb
89702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
898bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
89902ac6454SAndrew Thompson static void
usb_bus_mem_flush_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)900a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
901f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
90202ac6454SAndrew Thompson {
903a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(pc);
90402ac6454SAndrew Thompson }
905bdc081c6SAndrew Thompson #endif
90602ac6454SAndrew Thompson 
90702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
908a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all - factored out code
90902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
910bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
91102ac6454SAndrew Thompson void
usb_bus_mem_flush_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)912a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
91302ac6454SAndrew Thompson {
91402ac6454SAndrew Thompson 	if (cb) {
915a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_flush_all_cb);
91602ac6454SAndrew Thompson 	}
91702ac6454SAndrew Thompson }
918bdc081c6SAndrew Thompson #endif
91902ac6454SAndrew Thompson 
92002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
921a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all_cb
92202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
923bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
92402ac6454SAndrew Thompson static void
usb_bus_mem_alloc_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)925a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
926f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
92702ac6454SAndrew Thompson {
92802ac6454SAndrew Thompson 	/* need to initialize the page cache */
92902ac6454SAndrew Thompson 	pc->tag_parent = bus->dma_parent_tag;
93002ac6454SAndrew Thompson 
931a593f6b8SAndrew Thompson 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
93202ac6454SAndrew Thompson 		bus->alloc_failed = 1;
93302ac6454SAndrew Thompson 	}
93402ac6454SAndrew Thompson }
935bdc081c6SAndrew Thompson #endif
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
938a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all - factored out code
93902ac6454SAndrew Thompson  *
94002ac6454SAndrew Thompson  * Returns:
94102ac6454SAndrew Thompson  *    0: Success
94202ac6454SAndrew Thompson  * Else: Failure
94302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
94402ac6454SAndrew Thompson uint8_t
usb_bus_mem_alloc_all(struct usb_bus * bus,bus_dma_tag_t dmat,usb_bus_mem_cb_t * cb)945a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
946e0a69b51SAndrew Thompson     usb_bus_mem_cb_t *cb)
94702ac6454SAndrew Thompson {
94802ac6454SAndrew Thompson 	bus->alloc_failed = 0;
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
9514a4da38fSHans Petter Selasky 	    "usb_def_mtx", MTX_DEF | MTX_RECURSE);
95202ac6454SAndrew Thompson 
9532fe7ad87SHans Petter Selasky 	mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
9544a4da38fSHans Petter Selasky 	    "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
9552fe7ad87SHans Petter Selasky 
956a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&bus->power_wdog,
957684e3f22SAndrew Thompson 	    &bus->bus_mtx, 0);
95802ac6454SAndrew Thompson 
95902ac6454SAndrew Thompson 	TAILQ_INIT(&bus->intr_q.head);
96002ac6454SAndrew Thompson 
961bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
962a593f6b8SAndrew Thompson 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
963b217d184SHans Petter Selasky 	    dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
964bdc081c6SAndrew Thompson #endif
96502ac6454SAndrew Thompson 	if ((bus->devices_max > USB_MAX_DEVICES) ||
96602ac6454SAndrew Thompson 	    (bus->devices_max < USB_MIN_DEVICES) ||
96702ac6454SAndrew Thompson 	    (bus->devices == NULL)) {
96802ac6454SAndrew Thompson 		DPRINTFN(0, "Devices field has not been "
969767cb2e2SAndrew Thompson 		    "initialised properly\n");
97002ac6454SAndrew Thompson 		bus->alloc_failed = 1;		/* failure */
97102ac6454SAndrew Thompson 	}
972bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
97302ac6454SAndrew Thompson 	if (cb) {
974a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_alloc_all_cb);
97502ac6454SAndrew Thompson 	}
976bdc081c6SAndrew Thompson #endif
97702ac6454SAndrew Thompson 	if (bus->alloc_failed) {
978a593f6b8SAndrew Thompson 		usb_bus_mem_free_all(bus, cb);
97902ac6454SAndrew Thompson 	}
98002ac6454SAndrew Thompson 	return (bus->alloc_failed);
98102ac6454SAndrew Thompson }
98202ac6454SAndrew Thompson 
98302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
984a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all_cb
98502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
986bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
98702ac6454SAndrew Thompson static void
usb_bus_mem_free_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)988a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
989f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
99002ac6454SAndrew Thompson {
991a593f6b8SAndrew Thompson 	usb_pc_free_mem(pc);
99202ac6454SAndrew Thompson }
993bdc081c6SAndrew Thompson #endif
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
996a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all - factored out code
99702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
99802ac6454SAndrew Thompson void
usb_bus_mem_free_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)999a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
100002ac6454SAndrew Thompson {
1001bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
100202ac6454SAndrew Thompson 	if (cb) {
1003a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_free_all_cb);
100402ac6454SAndrew Thompson 	}
1005a593f6b8SAndrew Thompson 	usb_dma_tag_unsetup(bus->dma_parent_tag);
1006bdc081c6SAndrew Thompson #endif
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson 	mtx_destroy(&bus->bus_mtx);
10092fe7ad87SHans Petter Selasky 	mtx_destroy(&bus->bus_spin_lock);
101002ac6454SAndrew Thompson }
1011751aaf5aSHans Petter Selasky 
1012751aaf5aSHans Petter Selasky /* convenience wrappers */
1013751aaf5aSHans Petter Selasky void
usb_proc_explore_mwait(struct usb_device * udev,void * pm1,void * pm2)1014751aaf5aSHans Petter Selasky usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
1015751aaf5aSHans Petter Selasky {
1016751aaf5aSHans Petter Selasky 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
1017751aaf5aSHans Petter Selasky }
1018751aaf5aSHans Petter Selasky 
1019751aaf5aSHans Petter Selasky void	*
usb_proc_explore_msignal(struct usb_device * udev,void * pm1,void * pm2)1020751aaf5aSHans Petter Selasky usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
1021751aaf5aSHans Petter Selasky {
1022751aaf5aSHans Petter Selasky 	return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
1023751aaf5aSHans Petter Selasky }
1024751aaf5aSHans Petter Selasky 
1025751aaf5aSHans Petter Selasky void
usb_proc_explore_lock(struct usb_device * udev)1026751aaf5aSHans Petter Selasky usb_proc_explore_lock(struct usb_device *udev)
1027751aaf5aSHans Petter Selasky {
1028751aaf5aSHans Petter Selasky 	USB_BUS_LOCK(udev->bus);
1029751aaf5aSHans Petter Selasky }
1030751aaf5aSHans Petter Selasky 
1031751aaf5aSHans Petter Selasky void
usb_proc_explore_unlock(struct usb_device * udev)1032751aaf5aSHans Petter Selasky usb_proc_explore_unlock(struct usb_device *udev)
1033751aaf5aSHans Petter Selasky {
1034751aaf5aSHans Petter Selasky 	USB_BUS_UNLOCK(udev->bus);
1035751aaf5aSHans Petter Selasky }
1036