xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision 864bc41261e1bfad2b5ed8272263c65720577432)
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/linker_set.h>
38ed6d949aSAndrew Thompson #include <sys/module.h>
39ed6d949aSAndrew Thompson #include <sys/lock.h>
40ed6d949aSAndrew Thompson #include <sys/mutex.h>
41ed6d949aSAndrew Thompson #include <sys/condvar.h>
42ed6d949aSAndrew Thompson #include <sys/sysctl.h>
43ed6d949aSAndrew Thompson #include <sys/sx.h>
44ed6d949aSAndrew Thompson #include <sys/unistd.h>
45ed6d949aSAndrew Thompson #include <sys/callout.h>
46ed6d949aSAndrew Thompson #include <sys/malloc.h>
47ed6d949aSAndrew Thompson #include <sys/priv.h>
48ed6d949aSAndrew Thompson 
4902ac6454SAndrew Thompson #include <dev/usb/usb.h>
50ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
5102ac6454SAndrew Thompson 
52a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_ctrl_debug
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
6102ac6454SAndrew Thompson 
6202ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson /* function prototypes  */
6602ac6454SAndrew Thompson 
67a593f6b8SAndrew Thompson static device_probe_t usb_probe;
68a593f6b8SAndrew Thompson static device_attach_t usb_attach;
69a593f6b8SAndrew Thompson static device_detach_t usb_detach;
7002ac6454SAndrew Thompson 
71a593f6b8SAndrew Thompson static void	usb_attach_sub(device_t, struct usb_bus *);
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson /* static variables */
7402ac6454SAndrew Thompson 
75ed6d949aSAndrew Thompson #ifdef USB_DEBUG
76a593f6b8SAndrew Thompson static int usb_ctrl_debug = 0;
7702ac6454SAndrew Thompson 
789360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
79a593f6b8SAndrew Thompson SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
8002ac6454SAndrew Thompson     "Debug level");
8102ac6454SAndrew Thompson #endif
8202ac6454SAndrew Thompson 
83a0c61406SAlfred Perlstein static int usb_no_boot_wait = 0;
84a0c61406SAlfred Perlstein TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
85a0c61406SAlfred Perlstein SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
86a0c61406SAlfred Perlstein     "No device enumerate waiting at boot.");
87a0c61406SAlfred Perlstein 
88a593f6b8SAndrew Thompson static devclass_t usb_devclass;
8902ac6454SAndrew Thompson 
90a593f6b8SAndrew Thompson static device_method_t usb_methods[] = {
91a593f6b8SAndrew Thompson 	DEVMETHOD(device_probe, usb_probe),
92a593f6b8SAndrew Thompson 	DEVMETHOD(device_attach, usb_attach),
93a593f6b8SAndrew Thompson 	DEVMETHOD(device_detach, usb_detach),
9402ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, bus_generic_suspend),
9502ac6454SAndrew Thompson 	DEVMETHOD(device_resume, bus_generic_resume),
9602ac6454SAndrew Thompson 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
9702ac6454SAndrew Thompson 	{0, 0}
9802ac6454SAndrew Thompson };
9902ac6454SAndrew Thompson 
100a593f6b8SAndrew Thompson static driver_t usb_driver = {
10102ac6454SAndrew Thompson 	.name = "usbus",
102a593f6b8SAndrew Thompson 	.methods = usb_methods,
10302ac6454SAndrew Thompson 	.size = 0,
10402ac6454SAndrew Thompson };
10502ac6454SAndrew Thompson 
106*864bc412SHans Petter Selasky /* Host Only Drivers */
107a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
108a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
109a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
110*864bc412SHans Petter Selasky 
111*864bc412SHans Petter Selasky /* Device Only Drivers */
112a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
113*864bc412SHans Petter Selasky DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
114a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
117a593f6b8SAndrew Thompson  *	usb_probe
11802ac6454SAndrew Thompson  *
11902ac6454SAndrew Thompson  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
12002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
12102ac6454SAndrew Thompson static int
122a593f6b8SAndrew Thompson usb_probe(device_t dev)
12302ac6454SAndrew Thompson {
12402ac6454SAndrew Thompson 	DPRINTF("\n");
12502ac6454SAndrew Thompson 	return (0);
12602ac6454SAndrew Thompson }
12702ac6454SAndrew Thompson 
1283df007ceSHans Petter Selasky static void
1293df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus)
1303df007ceSHans Petter Selasky {
1313df007ceSHans Petter Selasky 	if (bus->bus_roothold != NULL) {
1323df007ceSHans Petter Selasky 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
1333df007ceSHans Petter Selasky 		root_mount_rel(bus->bus_roothold);
1343df007ceSHans Petter Selasky 		bus->bus_roothold = NULL;
1353df007ceSHans Petter Selasky 	}
1363df007ceSHans Petter Selasky }
1373df007ceSHans Petter Selasky 
13802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
139a593f6b8SAndrew Thompson  *	usb_attach
14002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
14102ac6454SAndrew Thompson static int
142a593f6b8SAndrew Thompson usb_attach(device_t dev)
14302ac6454SAndrew Thompson {
144760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_ivars(dev);
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson 	DPRINTF("\n");
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson 	if (bus == NULL) {
149767cb2e2SAndrew Thompson 		device_printf(dev, "USB device has no ivars\n");
15002ac6454SAndrew Thompson 		return (ENXIO);
15102ac6454SAndrew Thompson 	}
15202ac6454SAndrew Thompson 
153a0c61406SAlfred Perlstein 	if (usb_no_boot_wait == 0) {
15402ac6454SAndrew Thompson 		/* delay vfs_mountroot until the bus is explored */
155853a10a5SAndrew Thompson 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
156a0c61406SAlfred Perlstein 	}
15702ac6454SAndrew Thompson 
158a593f6b8SAndrew Thompson 	usb_attach_sub(dev, bus);
15934b48722SAlfred Perlstein 
16002ac6454SAndrew Thompson 	return (0);			/* return success */
16102ac6454SAndrew Thompson }
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
164a593f6b8SAndrew Thompson  *	usb_detach
16502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
16602ac6454SAndrew Thompson static int
167a593f6b8SAndrew Thompson usb_detach(device_t dev)
16802ac6454SAndrew Thompson {
169760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_softc(dev);
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson 	DPRINTF("\n");
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	if (bus == NULL) {
17402ac6454SAndrew Thompson 		/* was never setup properly */
17502ac6454SAndrew Thompson 		return (0);
17602ac6454SAndrew Thompson 	}
17702ac6454SAndrew Thompson 	/* Stop power watchdog */
178a593f6b8SAndrew Thompson 	usb_callout_drain(&bus->power_wdog);
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson 	/* Let the USB explore process detach all devices. */
1813df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
184a593f6b8SAndrew Thompson 	if (usb_proc_msignal(&bus->explore_proc,
18502ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1])) {
18602ac6454SAndrew Thompson 		/* ignore */
18702ac6454SAndrew Thompson 	}
18802ac6454SAndrew Thompson 	/* Wait for detach to complete */
18902ac6454SAndrew Thompson 
190a593f6b8SAndrew Thompson 	usb_proc_mwait(&bus->explore_proc,
19102ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1]);
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
19402ac6454SAndrew Thompson 
19502ac6454SAndrew Thompson 	/* Get rid of USB callback processes */
19602ac6454SAndrew Thompson 
197a593f6b8SAndrew Thompson 	usb_proc_free(&bus->giant_callback_proc);
198a593f6b8SAndrew Thompson 	usb_proc_free(&bus->non_giant_callback_proc);
19902ac6454SAndrew Thompson 
20002ac6454SAndrew Thompson 	/* Get rid of USB explore process */
20102ac6454SAndrew Thompson 
202a593f6b8SAndrew Thompson 	usb_proc_free(&bus->explore_proc);
20302ac6454SAndrew Thompson 
204672c9965SAndrew Thompson 	/* Get rid of control transfer process */
205672c9965SAndrew Thompson 
206a593f6b8SAndrew Thompson 	usb_proc_free(&bus->control_xfer_proc);
207672c9965SAndrew Thompson 
20802ac6454SAndrew Thompson 	return (0);
20902ac6454SAndrew Thompson }
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
212a593f6b8SAndrew Thompson  *	usb_bus_explore
21302ac6454SAndrew Thompson  *
21402ac6454SAndrew Thompson  * This function is used to explore the device tree from the root.
21502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
21602ac6454SAndrew Thompson static void
217a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm)
21802ac6454SAndrew Thompson {
219760bc48eSAndrew Thompson 	struct usb_bus *bus;
220760bc48eSAndrew Thompson 	struct usb_device *udev;
22102ac6454SAndrew Thompson 
222760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
22302ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
22402ac6454SAndrew Thompson 
22502ac6454SAndrew Thompson 	if (udev && udev->hub) {
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 		if (bus->do_probe) {
22802ac6454SAndrew Thompson 			bus->do_probe = 0;
22902ac6454SAndrew Thompson 			bus->driver_added_refcount++;
23002ac6454SAndrew Thompson 		}
23102ac6454SAndrew Thompson 		if (bus->driver_added_refcount == 0) {
23202ac6454SAndrew Thompson 			/* avoid zero, hence that is memory default */
23302ac6454SAndrew Thompson 			bus->driver_added_refcount = 1;
23402ac6454SAndrew Thompson 		}
23502ac6454SAndrew Thompson 
236f0c078e6SAndrew Thompson #ifdef DDB
23734b48722SAlfred Perlstein 		/*
23834b48722SAlfred Perlstein 		 * The following three lines of code are only here to
23934b48722SAlfred Perlstein 		 * recover from DDB:
24034b48722SAlfred Perlstein 		 */
24134b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->control_xfer_proc);
24234b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->giant_callback_proc);
24334b48722SAlfred Perlstein 		usb_proc_rewakeup(&bus->non_giant_callback_proc);
244f0c078e6SAndrew Thompson #endif
24534b48722SAlfred Perlstein 
24634b48722SAlfred Perlstein 		USB_BUS_UNLOCK(bus);
247a56fe095SJohn Baldwin 
248e727a16cSAndrew Thompson #if USB_HAVE_POWERD
24902ac6454SAndrew Thompson 		/*
25002ac6454SAndrew Thompson 		 * First update the USB power state!
25102ac6454SAndrew Thompson 		 */
252a593f6b8SAndrew Thompson 		usb_bus_powerd(bus);
253e727a16cSAndrew Thompson #endif
25434b48722SAlfred Perlstein 		 /* Explore the Root USB HUB. */
25502ac6454SAndrew Thompson 		(udev->hub->explore) (udev);
25602ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
25702ac6454SAndrew Thompson 	}
2583df007ceSHans Petter Selasky 	usb_root_mount_rel(bus);
25902ac6454SAndrew Thompson }
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
262a593f6b8SAndrew Thompson  *	usb_bus_detach
26302ac6454SAndrew Thompson  *
26402ac6454SAndrew Thompson  * This function is used to detach the device tree from the root.
26502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
26602ac6454SAndrew Thompson static void
267a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm)
26802ac6454SAndrew Thompson {
269760bc48eSAndrew Thompson 	struct usb_bus *bus;
270760bc48eSAndrew Thompson 	struct usb_device *udev;
27102ac6454SAndrew Thompson 	device_t dev;
27202ac6454SAndrew Thompson 
273760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
27402ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
27502ac6454SAndrew Thompson 	dev = bus->bdev;
27602ac6454SAndrew Thompson 	/* clear the softc */
27702ac6454SAndrew Thompson 	device_set_softc(dev, NULL);
27802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
27902ac6454SAndrew Thompson 
28002ac6454SAndrew Thompson 	/* detach children first */
28134b48722SAlfred Perlstein 	mtx_lock(&Giant);
28202ac6454SAndrew Thompson 	bus_generic_detach(dev);
28334b48722SAlfred Perlstein 	mtx_unlock(&Giant);
28402ac6454SAndrew Thompson 
28502ac6454SAndrew Thompson 	/*
286d88688c7SAndrew Thompson 	 * Free USB device and all subdevices, if any.
28702ac6454SAndrew Thompson 	 */
288d88688c7SAndrew Thompson 	usb_free_device(udev, 0);
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
29102ac6454SAndrew Thompson 	/* clear bdev variable last */
29202ac6454SAndrew Thompson 	bus->bdev = NULL;
29302ac6454SAndrew Thompson }
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson static void
296a593f6b8SAndrew Thompson usb_power_wdog(void *arg)
29702ac6454SAndrew Thompson {
298760bc48eSAndrew Thompson 	struct usb_bus *bus = arg;
29902ac6454SAndrew Thompson 
30002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
30102ac6454SAndrew Thompson 
302a593f6b8SAndrew Thompson 	usb_callout_reset(&bus->power_wdog,
303a593f6b8SAndrew Thompson 	    4 * hz, usb_power_wdog, arg);
30402ac6454SAndrew Thompson 
305f0c078e6SAndrew Thompson #ifdef DDB
30634b48722SAlfred Perlstein 	/*
30734b48722SAlfred Perlstein 	 * The following line of code is only here to recover from
30834b48722SAlfred Perlstein 	 * DDB:
30934b48722SAlfred Perlstein 	 */
31034b48722SAlfred Perlstein 	usb_proc_rewakeup(&bus->explore_proc);	/* recover from DDB */
311f0c078e6SAndrew Thompson #endif
31234b48722SAlfred Perlstein 
313e727a16cSAndrew Thompson #if USB_HAVE_POWERD
31402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
31502ac6454SAndrew Thompson 
316a593f6b8SAndrew Thompson 	usb_bus_power_update(bus);
31702ac6454SAndrew Thompson 
318684e3f22SAndrew Thompson 	USB_BUS_LOCK(bus);
319e727a16cSAndrew Thompson #endif
32002ac6454SAndrew Thompson }
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
323a593f6b8SAndrew Thompson  *	usb_bus_attach
32402ac6454SAndrew Thompson  *
32502ac6454SAndrew Thompson  * This function attaches USB in context of the explore thread.
32602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
32702ac6454SAndrew Thompson static void
328a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm)
32902ac6454SAndrew Thompson {
330760bc48eSAndrew Thompson 	struct usb_bus *bus;
331760bc48eSAndrew Thompson 	struct usb_device *child;
33202ac6454SAndrew Thompson 	device_t dev;
333e0a69b51SAndrew Thompson 	usb_error_t err;
3348d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
33502ac6454SAndrew Thompson 
336760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
33702ac6454SAndrew Thompson 	dev = bus->bdev;
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	DPRINTF("\n");
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	switch (bus->usbrev) {
34202ac6454SAndrew Thompson 	case USB_REV_1_0:
34302ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
34402ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
34502ac6454SAndrew Thompson 		break;
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 	case USB_REV_1_1:
34802ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
34902ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
35002ac6454SAndrew Thompson 		break;
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 	case USB_REV_2_0:
35302ac6454SAndrew Thompson 		speed = USB_SPEED_HIGH;
35402ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
35502ac6454SAndrew Thompson 		break;
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	case USB_REV_2_5:
35802ac6454SAndrew Thompson 		speed = USB_SPEED_VARIABLE;
35902ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
36002ac6454SAndrew Thompson 		break;
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson 	default:
363767cb2e2SAndrew Thompson 		device_printf(bus->bdev, "Unsupported USB revision\n");
3643df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
36502ac6454SAndrew Thompson 		return;
36602ac6454SAndrew Thompson 	}
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
36902ac6454SAndrew Thompson 
3704eae601eSAndrew Thompson 	/* default power_mask value */
3714eae601eSAndrew Thompson 	bus->hw_power_state =
3724eae601eSAndrew Thompson 	  USB_HW_POWER_CONTROL |
3734eae601eSAndrew Thompson 	  USB_HW_POWER_BULK |
3744eae601eSAndrew Thompson 	  USB_HW_POWER_INTERRUPT |
3754eae601eSAndrew Thompson 	  USB_HW_POWER_ISOC |
3764eae601eSAndrew Thompson 	  USB_HW_POWER_NON_ROOT_HUB;
3774eae601eSAndrew Thompson 
3784eae601eSAndrew Thompson 	/* make sure power is set at least once */
3794eae601eSAndrew Thompson 
3804eae601eSAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
3814eae601eSAndrew Thompson 		(bus->methods->set_hw_power) (bus);
3824eae601eSAndrew Thompson 	}
3834eae601eSAndrew Thompson 
38402ac6454SAndrew Thompson 	/* Allocate the Root USB device */
38502ac6454SAndrew Thompson 
386a593f6b8SAndrew Thompson 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
38702ac6454SAndrew Thompson 	    speed, USB_MODE_HOST);
38802ac6454SAndrew Thompson 	if (child) {
389a593f6b8SAndrew Thompson 		err = usb_probe_and_attach(child,
39002ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
39102ac6454SAndrew Thompson 		if (!err) {
3921be5bf51SAndrew Thompson 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
3931be5bf51SAndrew Thompson 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
39402ac6454SAndrew Thompson 				err = USB_ERR_NO_ROOT_HUB;
39502ac6454SAndrew Thompson 			}
39602ac6454SAndrew Thompson 		}
39702ac6454SAndrew Thompson 	} else {
39802ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
39902ac6454SAndrew Thompson 	}
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
40202ac6454SAndrew Thompson 
40302ac6454SAndrew Thompson 	if (err) {
40402ac6454SAndrew Thompson 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
405a593f6b8SAndrew Thompson 		    usbd_errstr(err));
4063df007ceSHans Petter Selasky 		usb_root_mount_rel(bus);
40702ac6454SAndrew Thompson 	}
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 	/* set softc - we are ready */
41002ac6454SAndrew Thompson 	device_set_softc(dev, bus);
41102ac6454SAndrew Thompson 
412684e3f22SAndrew Thompson 	/* start watchdog */
413a593f6b8SAndrew Thompson 	usb_power_wdog(bus);
41402ac6454SAndrew Thompson }
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
417a593f6b8SAndrew Thompson  *	usb_attach_sub
41802ac6454SAndrew Thompson  *
41934b48722SAlfred Perlstein  * This function creates a thread which runs the USB attach code.
42002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
42102ac6454SAndrew Thompson static void
422a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus)
42302ac6454SAndrew Thompson {
42402ac6454SAndrew Thompson 	const char *pname = device_get_nameunit(dev);
42502ac6454SAndrew Thompson 
42634b48722SAlfred Perlstein 	mtx_lock(&Giant);
42734b48722SAlfred Perlstein 	if (usb_devclass_ptr == NULL)
42834b48722SAlfred Perlstein 		usb_devclass_ptr = devclass_find("usbus");
42934b48722SAlfred Perlstein 	mtx_unlock(&Giant);
43034b48722SAlfred Perlstein 
43102ac6454SAndrew Thompson 	/* Initialise USB process messages */
432a593f6b8SAndrew Thompson 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
43302ac6454SAndrew Thompson 	bus->explore_msg[0].bus = bus;
434a593f6b8SAndrew Thompson 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
43502ac6454SAndrew Thompson 	bus->explore_msg[1].bus = bus;
43602ac6454SAndrew Thompson 
437a593f6b8SAndrew Thompson 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
43802ac6454SAndrew Thompson 	bus->detach_msg[0].bus = bus;
439a593f6b8SAndrew Thompson 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
44002ac6454SAndrew Thompson 	bus->detach_msg[1].bus = bus;
44102ac6454SAndrew Thompson 
442a593f6b8SAndrew Thompson 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
44302ac6454SAndrew Thompson 	bus->attach_msg[0].bus = bus;
444a593f6b8SAndrew Thompson 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
44502ac6454SAndrew Thompson 	bus->attach_msg[1].bus = bus;
44602ac6454SAndrew Thompson 
44739307315SAndrew Thompson 	/* Create USB explore and callback processes */
44802ac6454SAndrew Thompson 
449a593f6b8SAndrew Thompson 	if (usb_proc_create(&bus->giant_callback_proc,
45002ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
45102ac6454SAndrew Thompson 		printf("WARNING: Creation of USB Giant "
45202ac6454SAndrew Thompson 		    "callback process failed.\n");
453a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->non_giant_callback_proc,
45402ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
45502ac6454SAndrew Thompson 		printf("WARNING: Creation of USB non-Giant "
45602ac6454SAndrew Thompson 		    "callback process failed.\n");
457a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->explore_proc,
45802ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
45902ac6454SAndrew Thompson 		printf("WARNING: Creation of USB explore "
46002ac6454SAndrew Thompson 		    "process failed.\n");
461a593f6b8SAndrew Thompson 	} else if (usb_proc_create(&bus->control_xfer_proc,
462672c9965SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
463672c9965SAndrew Thompson 		printf("WARNING: Creation of USB control transfer "
464672c9965SAndrew Thompson 		    "process failed.\n");
46502ac6454SAndrew Thompson 	} else {
46602ac6454SAndrew Thompson 		/* Get final attach going */
46702ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
468a593f6b8SAndrew Thompson 		if (usb_proc_msignal(&bus->explore_proc,
46902ac6454SAndrew Thompson 		    &bus->attach_msg[0], &bus->attach_msg[1])) {
47002ac6454SAndrew Thompson 			/* ignore */
47102ac6454SAndrew Thompson 		}
47202ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
47334b48722SAlfred Perlstein 
47434b48722SAlfred Perlstein 		/* Do initial explore */
47534b48722SAlfred Perlstein 		usb_needs_explore(bus, 1);
47602ac6454SAndrew Thompson 	}
47702ac6454SAndrew Thompson }
47802ac6454SAndrew Thompson 
479a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
48002ac6454SAndrew Thompson 
48102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
482a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all_cb
48302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
484bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
48502ac6454SAndrew Thompson static void
486a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
487f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
48802ac6454SAndrew Thompson {
489a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(pc);
49002ac6454SAndrew Thompson }
491bdc081c6SAndrew Thompson #endif
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
494a593f6b8SAndrew Thompson  *	usb_bus_mem_flush_all - factored out code
49502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
496bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
49702ac6454SAndrew Thompson void
498a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
49902ac6454SAndrew Thompson {
50002ac6454SAndrew Thompson 	if (cb) {
501a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_flush_all_cb);
50202ac6454SAndrew Thompson 	}
50302ac6454SAndrew Thompson }
504bdc081c6SAndrew Thompson #endif
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
507a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all_cb
50802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
509bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
51002ac6454SAndrew Thompson static void
511a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
512f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
51302ac6454SAndrew Thompson {
51402ac6454SAndrew Thompson 	/* need to initialize the page cache */
51502ac6454SAndrew Thompson 	pc->tag_parent = bus->dma_parent_tag;
51602ac6454SAndrew Thompson 
517a593f6b8SAndrew Thompson 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
51802ac6454SAndrew Thompson 		bus->alloc_failed = 1;
51902ac6454SAndrew Thompson 	}
52002ac6454SAndrew Thompson }
521bdc081c6SAndrew Thompson #endif
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
524a593f6b8SAndrew Thompson  *	usb_bus_mem_alloc_all - factored out code
52502ac6454SAndrew Thompson  *
52602ac6454SAndrew Thompson  * Returns:
52702ac6454SAndrew Thompson  *    0: Success
52802ac6454SAndrew Thompson  * Else: Failure
52902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
53002ac6454SAndrew Thompson uint8_t
531a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
532e0a69b51SAndrew Thompson     usb_bus_mem_cb_t *cb)
53302ac6454SAndrew Thompson {
53402ac6454SAndrew Thompson 	bus->alloc_failed = 0;
53502ac6454SAndrew Thompson 
53602ac6454SAndrew Thompson 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
53702ac6454SAndrew Thompson 	    NULL, MTX_DEF | MTX_RECURSE);
53802ac6454SAndrew Thompson 
539a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&bus->power_wdog,
540684e3f22SAndrew Thompson 	    &bus->bus_mtx, 0);
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson 	TAILQ_INIT(&bus->intr_q.head);
54302ac6454SAndrew Thompson 
544bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
545a593f6b8SAndrew Thompson 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
546bdc081c6SAndrew Thompson 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
547bdc081c6SAndrew Thompson #endif
54802ac6454SAndrew Thompson 	if ((bus->devices_max > USB_MAX_DEVICES) ||
54902ac6454SAndrew Thompson 	    (bus->devices_max < USB_MIN_DEVICES) ||
55002ac6454SAndrew Thompson 	    (bus->devices == NULL)) {
55102ac6454SAndrew Thompson 		DPRINTFN(0, "Devices field has not been "
552767cb2e2SAndrew Thompson 		    "initialised properly\n");
55302ac6454SAndrew Thompson 		bus->alloc_failed = 1;		/* failure */
55402ac6454SAndrew Thompson 	}
555bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
55602ac6454SAndrew Thompson 	if (cb) {
557a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_alloc_all_cb);
55802ac6454SAndrew Thompson 	}
559bdc081c6SAndrew Thompson #endif
56002ac6454SAndrew Thompson 	if (bus->alloc_failed) {
561a593f6b8SAndrew Thompson 		usb_bus_mem_free_all(bus, cb);
56202ac6454SAndrew Thompson 	}
56302ac6454SAndrew Thompson 	return (bus->alloc_failed);
56402ac6454SAndrew Thompson }
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
567a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all_cb
56802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
569bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
57002ac6454SAndrew Thompson static void
571a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
572f9cb546cSAndrew Thompson     struct usb_page *pg, usb_size_t size, usb_size_t align)
57302ac6454SAndrew Thompson {
574a593f6b8SAndrew Thompson 	usb_pc_free_mem(pc);
57502ac6454SAndrew Thompson }
576bdc081c6SAndrew Thompson #endif
57702ac6454SAndrew Thompson 
57802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
579a593f6b8SAndrew Thompson  *	usb_bus_mem_free_all - factored out code
58002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
58102ac6454SAndrew Thompson void
582a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
58302ac6454SAndrew Thompson {
584bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
58502ac6454SAndrew Thompson 	if (cb) {
586a593f6b8SAndrew Thompson 		cb(bus, &usb_bus_mem_free_all_cb);
58702ac6454SAndrew Thompson 	}
588a593f6b8SAndrew Thompson 	usb_dma_tag_unsetup(bus->dma_parent_tag);
589bdc081c6SAndrew Thompson #endif
59002ac6454SAndrew Thompson 
59102ac6454SAndrew Thompson 	mtx_destroy(&bus->bus_mtx);
59202ac6454SAndrew Thompson }
593