xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision e0a69b51ac09dc0376611274903ba683e2d1ace7)
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 
2702ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
2802ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
2902ac6454SAndrew Thompson #include <dev/usb/usb.h>
3002ac6454SAndrew Thompson 
3102ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_ctrl_debug
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
3502ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
3602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
3702ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
3802ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
3902ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
4002ac6454SAndrew Thompson 
4102ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
4302ac6454SAndrew Thompson 
4402ac6454SAndrew Thompson /* function prototypes  */
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson static device_probe_t usb2_probe;
4702ac6454SAndrew Thompson static device_attach_t usb2_attach;
4802ac6454SAndrew Thompson static device_detach_t usb2_detach;
4902ac6454SAndrew Thompson 
50760bc48eSAndrew Thompson static void	usb2_attach_sub(device_t, struct usb_bus *);
5102ac6454SAndrew Thompson static void	usb2_post_init(void *);
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson /* static variables */
5402ac6454SAndrew Thompson 
5502ac6454SAndrew Thompson #if USB_DEBUG
5602ac6454SAndrew Thompson static int usb2_ctrl_debug = 0;
5702ac6454SAndrew Thompson 
589360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
599360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb2_ctrl_debug, 0,
6002ac6454SAndrew Thompson     "Debug level");
6102ac6454SAndrew Thompson #endif
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson static uint8_t usb2_post_init_called = 0;
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson static devclass_t usb2_devclass;
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson static device_method_t usb2_methods[] = {
6802ac6454SAndrew Thompson 	DEVMETHOD(device_probe, usb2_probe),
6902ac6454SAndrew Thompson 	DEVMETHOD(device_attach, usb2_attach),
7002ac6454SAndrew Thompson 	DEVMETHOD(device_detach, usb2_detach),
7102ac6454SAndrew Thompson 	DEVMETHOD(device_suspend, bus_generic_suspend),
7202ac6454SAndrew Thompson 	DEVMETHOD(device_resume, bus_generic_resume),
7302ac6454SAndrew Thompson 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
7402ac6454SAndrew Thompson 	{0, 0}
7502ac6454SAndrew Thompson };
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson static driver_t usb2_driver = {
7802ac6454SAndrew Thompson 	.name = "usbus",
7902ac6454SAndrew Thompson 	.methods = usb2_methods,
8002ac6454SAndrew Thompson 	.size = 0,
8102ac6454SAndrew Thompson };
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb2_driver, usb2_devclass, 0, 0);
8402ac6454SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb2_driver, usb2_devclass, 0, 0);
8502ac6454SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb2_driver, usb2_devclass, 0, 0);
8602ac6454SAndrew Thompson DRIVER_MODULE(usbus, at91_udp, usb2_driver, usb2_devclass, 0, 0);
8702ac6454SAndrew Thompson DRIVER_MODULE(usbus, uss820, usb2_driver, usb2_devclass, 0, 0);
8802ac6454SAndrew Thompson 
8902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
9002ac6454SAndrew Thompson  *	usb2_probe
9102ac6454SAndrew Thompson  *
9202ac6454SAndrew Thompson  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
9302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
9402ac6454SAndrew Thompson static int
9502ac6454SAndrew Thompson usb2_probe(device_t dev)
9602ac6454SAndrew Thompson {
9702ac6454SAndrew Thompson 	DPRINTF("\n");
9802ac6454SAndrew Thompson 	return (0);
9902ac6454SAndrew Thompson }
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
10202ac6454SAndrew Thompson  *	usb2_attach
10302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
10402ac6454SAndrew Thompson static int
10502ac6454SAndrew Thompson usb2_attach(device_t dev)
10602ac6454SAndrew Thompson {
107760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_ivars(dev);
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson 	DPRINTF("\n");
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson 	if (bus == NULL) {
11202ac6454SAndrew Thompson 		DPRINTFN(0, "USB device has no ivars\n");
11302ac6454SAndrew Thompson 		return (ENXIO);
11402ac6454SAndrew Thompson 	}
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson 	/* delay vfs_mountroot until the bus is explored */
117853a10a5SAndrew Thompson 	bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson 	if (usb2_post_init_called) {
12002ac6454SAndrew Thompson 		mtx_lock(&Giant);
12102ac6454SAndrew Thompson 		usb2_attach_sub(dev, bus);
12202ac6454SAndrew Thompson 		mtx_unlock(&Giant);
12302ac6454SAndrew Thompson 		usb2_needs_explore(bus, 1);
12402ac6454SAndrew Thompson 	}
12502ac6454SAndrew Thompson 	return (0);			/* return success */
12602ac6454SAndrew Thompson }
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
12902ac6454SAndrew Thompson  *	usb2_detach
13002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
13102ac6454SAndrew Thompson static int
13202ac6454SAndrew Thompson usb2_detach(device_t dev)
13302ac6454SAndrew Thompson {
134760bc48eSAndrew Thompson 	struct usb_bus *bus = device_get_softc(dev);
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson 	DPRINTF("\n");
13702ac6454SAndrew Thompson 
13802ac6454SAndrew Thompson 	if (bus == NULL) {
13902ac6454SAndrew Thompson 		/* was never setup properly */
14002ac6454SAndrew Thompson 		return (0);
14102ac6454SAndrew Thompson 	}
14202ac6454SAndrew Thompson 	/* Stop power watchdog */
14302ac6454SAndrew Thompson 	usb2_callout_drain(&bus->power_wdog);
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson 	/* Let the USB explore process detach all devices. */
14602ac6454SAndrew Thompson 	if (bus->bus_roothold != NULL) {
14702ac6454SAndrew Thompson 		root_mount_rel(bus->bus_roothold);
14802ac6454SAndrew Thompson 		bus->bus_roothold = NULL;
14902ac6454SAndrew Thompson 	}
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
15202ac6454SAndrew Thompson 	if (usb2_proc_msignal(&bus->explore_proc,
15302ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1])) {
15402ac6454SAndrew Thompson 		/* ignore */
15502ac6454SAndrew Thompson 	}
15602ac6454SAndrew Thompson 	/* Wait for detach to complete */
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	usb2_proc_mwait(&bus->explore_proc,
15902ac6454SAndrew Thompson 	    &bus->detach_msg[0], &bus->detach_msg[1]);
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson 	/* Get rid of USB callback processes */
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson 	usb2_proc_free(&bus->giant_callback_proc);
16602ac6454SAndrew Thompson 	usb2_proc_free(&bus->non_giant_callback_proc);
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson 	/* Get rid of USB explore process */
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	usb2_proc_free(&bus->explore_proc);
17102ac6454SAndrew Thompson 
172672c9965SAndrew Thompson 	/* Get rid of control transfer process */
173672c9965SAndrew Thompson 
174672c9965SAndrew Thompson 	usb2_proc_free(&bus->control_xfer_proc);
175672c9965SAndrew Thompson 
17602ac6454SAndrew Thompson 	return (0);
17702ac6454SAndrew Thompson }
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
18002ac6454SAndrew Thompson  *	usb2_bus_explore
18102ac6454SAndrew Thompson  *
18202ac6454SAndrew Thompson  * This function is used to explore the device tree from the root.
18302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
18402ac6454SAndrew Thompson static void
185760bc48eSAndrew Thompson usb2_bus_explore(struct usb_proc_msg *pm)
18602ac6454SAndrew Thompson {
187760bc48eSAndrew Thompson 	struct usb_bus *bus;
188760bc48eSAndrew Thompson 	struct usb_device *udev;
18902ac6454SAndrew Thompson 
190760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
19102ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson 	if (udev && udev->hub) {
19402ac6454SAndrew Thompson 
19502ac6454SAndrew Thompson 		if (bus->do_probe) {
19602ac6454SAndrew Thompson 			bus->do_probe = 0;
19702ac6454SAndrew Thompson 			bus->driver_added_refcount++;
19802ac6454SAndrew Thompson 		}
19902ac6454SAndrew Thompson 		if (bus->driver_added_refcount == 0) {
20002ac6454SAndrew Thompson 			/* avoid zero, hence that is memory default */
20102ac6454SAndrew Thompson 			bus->driver_added_refcount = 1;
20202ac6454SAndrew Thompson 		}
20302ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson 		mtx_lock(&Giant);
20602ac6454SAndrew Thompson 
20702ac6454SAndrew Thompson 		/*
20802ac6454SAndrew Thompson 		 * First update the USB power state!
20902ac6454SAndrew Thompson 		 */
21002ac6454SAndrew Thompson 		usb2_bus_powerd(bus);
21102ac6454SAndrew Thompson 		/*
21202ac6454SAndrew Thompson 		 * Explore the Root USB HUB. This call can sleep,
21302ac6454SAndrew Thompson 		 * exiting Giant, which is actually Giant.
21402ac6454SAndrew Thompson 		 */
21502ac6454SAndrew Thompson 		(udev->hub->explore) (udev);
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson 		mtx_unlock(&Giant);
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
22002ac6454SAndrew Thompson 	}
22102ac6454SAndrew Thompson 	if (bus->bus_roothold != NULL) {
22202ac6454SAndrew Thompson 		root_mount_rel(bus->bus_roothold);
22302ac6454SAndrew Thompson 		bus->bus_roothold = NULL;
22402ac6454SAndrew Thompson 	}
22502ac6454SAndrew Thompson }
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
22802ac6454SAndrew Thompson  *	usb2_bus_detach
22902ac6454SAndrew Thompson  *
23002ac6454SAndrew Thompson  * This function is used to detach the device tree from the root.
23102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
23202ac6454SAndrew Thompson static void
233760bc48eSAndrew Thompson usb2_bus_detach(struct usb_proc_msg *pm)
23402ac6454SAndrew Thompson {
235760bc48eSAndrew Thompson 	struct usb_bus *bus;
236760bc48eSAndrew Thompson 	struct usb_device *udev;
23702ac6454SAndrew Thompson 	device_t dev;
23802ac6454SAndrew Thompson 
239760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
24002ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
24102ac6454SAndrew Thompson 	dev = bus->bdev;
24202ac6454SAndrew Thompson 	/* clear the softc */
24302ac6454SAndrew Thompson 	device_set_softc(dev, NULL);
24402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson 	mtx_lock(&Giant);
24702ac6454SAndrew Thompson 
24802ac6454SAndrew Thompson 	/* detach children first */
24902ac6454SAndrew Thompson 	bus_generic_detach(dev);
25002ac6454SAndrew Thompson 
25102ac6454SAndrew Thompson 	/*
25202ac6454SAndrew Thompson 	 * Free USB Root device, but not any sub-devices, hence they
25302ac6454SAndrew Thompson 	 * are freed by the caller of this function:
25402ac6454SAndrew Thompson 	 */
255bdd41206SAndrew Thompson 	usb2_free_device(udev,
256bdd41206SAndrew Thompson 	    USB_UNCFG_FLAG_FREE_EP0);
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 	mtx_unlock(&Giant);
25902ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
26002ac6454SAndrew Thompson 	/* clear bdev variable last */
26102ac6454SAndrew Thompson 	bus->bdev = NULL;
26202ac6454SAndrew Thompson }
26302ac6454SAndrew Thompson 
26402ac6454SAndrew Thompson static void
26502ac6454SAndrew Thompson usb2_power_wdog(void *arg)
26602ac6454SAndrew Thompson {
267760bc48eSAndrew Thompson 	struct usb_bus *bus = arg;
26802ac6454SAndrew Thompson 
26902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
27002ac6454SAndrew Thompson 
27102ac6454SAndrew Thompson 	usb2_callout_reset(&bus->power_wdog,
27202ac6454SAndrew Thompson 	    4 * hz, usb2_power_wdog, arg);
27302ac6454SAndrew Thompson 
27402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson 	usb2_bus_power_update(bus);
27702ac6454SAndrew Thompson 
278684e3f22SAndrew Thompson 	USB_BUS_LOCK(bus);
27902ac6454SAndrew Thompson }
28002ac6454SAndrew Thompson 
28102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
28202ac6454SAndrew Thompson  *	usb2_bus_attach
28302ac6454SAndrew Thompson  *
28402ac6454SAndrew Thompson  * This function attaches USB in context of the explore thread.
28502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
28602ac6454SAndrew Thompson static void
287760bc48eSAndrew Thompson usb2_bus_attach(struct usb_proc_msg *pm)
28802ac6454SAndrew Thompson {
289760bc48eSAndrew Thompson 	struct usb_bus *bus;
290760bc48eSAndrew Thompson 	struct usb_device *child;
29102ac6454SAndrew Thompson 	device_t dev;
292e0a69b51SAndrew Thompson 	usb_error_t err;
2938d2dd5ddSAndrew Thompson 	enum usb_dev_speed speed;
29402ac6454SAndrew Thompson 
295760bc48eSAndrew Thompson 	bus = ((struct usb_bus_msg *)pm)->bus;
29602ac6454SAndrew Thompson 	dev = bus->bdev;
29702ac6454SAndrew Thompson 
29802ac6454SAndrew Thompson 	DPRINTF("\n");
29902ac6454SAndrew Thompson 
30002ac6454SAndrew Thompson 	switch (bus->usbrev) {
30102ac6454SAndrew Thompson 	case USB_REV_1_0:
30202ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
30302ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
30402ac6454SAndrew Thompson 		break;
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 	case USB_REV_1_1:
30702ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
30802ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
30902ac6454SAndrew Thompson 		break;
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson 	case USB_REV_2_0:
31202ac6454SAndrew Thompson 		speed = USB_SPEED_HIGH;
31302ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
31402ac6454SAndrew Thompson 		break;
31502ac6454SAndrew Thompson 
31602ac6454SAndrew Thompson 	case USB_REV_2_5:
31702ac6454SAndrew Thompson 		speed = USB_SPEED_VARIABLE;
31802ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
31902ac6454SAndrew Thompson 		break;
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	default:
32202ac6454SAndrew Thompson 		device_printf(bus->bdev, "Unsupported USB revision!\n");
32302ac6454SAndrew Thompson 		return;
32402ac6454SAndrew Thompson 	}
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
32702ac6454SAndrew Thompson 	mtx_lock(&Giant);		/* XXX not required by USB */
32802ac6454SAndrew Thompson 
3294eae601eSAndrew Thompson 	/* default power_mask value */
3304eae601eSAndrew Thompson 	bus->hw_power_state =
3314eae601eSAndrew Thompson 	  USB_HW_POWER_CONTROL |
3324eae601eSAndrew Thompson 	  USB_HW_POWER_BULK |
3334eae601eSAndrew Thompson 	  USB_HW_POWER_INTERRUPT |
3344eae601eSAndrew Thompson 	  USB_HW_POWER_ISOC |
3354eae601eSAndrew Thompson 	  USB_HW_POWER_NON_ROOT_HUB;
3364eae601eSAndrew Thompson 
3374eae601eSAndrew Thompson 	/* make sure power is set at least once */
3384eae601eSAndrew Thompson 
3394eae601eSAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
3404eae601eSAndrew Thompson 		(bus->methods->set_hw_power) (bus);
3414eae601eSAndrew Thompson 	}
3424eae601eSAndrew Thompson 
34302ac6454SAndrew Thompson 	/* Allocate the Root USB device */
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 	child = usb2_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
34602ac6454SAndrew Thompson 	    speed, USB_MODE_HOST);
34702ac6454SAndrew Thompson 	if (child) {
34802ac6454SAndrew Thompson 		err = usb2_probe_and_attach(child,
34902ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
35002ac6454SAndrew Thompson 		if (!err) {
3511be5bf51SAndrew Thompson 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
3521be5bf51SAndrew Thompson 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
35302ac6454SAndrew Thompson 				err = USB_ERR_NO_ROOT_HUB;
35402ac6454SAndrew Thompson 			}
35502ac6454SAndrew Thompson 		}
35602ac6454SAndrew Thompson 	} else {
35702ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
35802ac6454SAndrew Thompson 	}
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	mtx_unlock(&Giant);
36102ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson 	if (err) {
36402ac6454SAndrew Thompson 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
36502ac6454SAndrew Thompson 		    usb2_errstr(err));
36602ac6454SAndrew Thompson 	}
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	/* set softc - we are ready */
36902ac6454SAndrew Thompson 	device_set_softc(dev, bus);
37002ac6454SAndrew Thompson 
371684e3f22SAndrew Thompson 	/* start watchdog */
37202ac6454SAndrew Thompson 	usb2_power_wdog(bus);
37302ac6454SAndrew Thompson }
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
37602ac6454SAndrew Thompson  *	usb2_attach_sub
37702ac6454SAndrew Thompson  *
37802ac6454SAndrew Thompson  * This function creates a thread which runs the USB attach code. It
37902ac6454SAndrew Thompson  * is factored out, hence it can be called at two different places in
38002ac6454SAndrew Thompson  * time. During bootup this function is called from
38102ac6454SAndrew Thompson  * "usb2_post_init". During hot-plug it is called directly from the
38202ac6454SAndrew Thompson  * "usb2_attach()" method.
38302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
38402ac6454SAndrew Thompson static void
385760bc48eSAndrew Thompson usb2_attach_sub(device_t dev, struct usb_bus *bus)
38602ac6454SAndrew Thompson {
38702ac6454SAndrew Thompson 	const char *pname = device_get_nameunit(dev);
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	/* Initialise USB process messages */
39002ac6454SAndrew Thompson 	bus->explore_msg[0].hdr.pm_callback = &usb2_bus_explore;
39102ac6454SAndrew Thompson 	bus->explore_msg[0].bus = bus;
39202ac6454SAndrew Thompson 	bus->explore_msg[1].hdr.pm_callback = &usb2_bus_explore;
39302ac6454SAndrew Thompson 	bus->explore_msg[1].bus = bus;
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	bus->detach_msg[0].hdr.pm_callback = &usb2_bus_detach;
39602ac6454SAndrew Thompson 	bus->detach_msg[0].bus = bus;
39702ac6454SAndrew Thompson 	bus->detach_msg[1].hdr.pm_callback = &usb2_bus_detach;
39802ac6454SAndrew Thompson 	bus->detach_msg[1].bus = bus;
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson 	bus->attach_msg[0].hdr.pm_callback = &usb2_bus_attach;
40102ac6454SAndrew Thompson 	bus->attach_msg[0].bus = bus;
40202ac6454SAndrew Thompson 	bus->attach_msg[1].hdr.pm_callback = &usb2_bus_attach;
40302ac6454SAndrew Thompson 	bus->attach_msg[1].bus = bus;
40402ac6454SAndrew Thompson 
40539307315SAndrew Thompson 	/* Create USB explore and callback processes */
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 	if (usb2_proc_create(&bus->giant_callback_proc,
40802ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
40902ac6454SAndrew Thompson 		printf("WARNING: Creation of USB Giant "
41002ac6454SAndrew Thompson 		    "callback process failed.\n");
41102ac6454SAndrew Thompson 	} else if (usb2_proc_create(&bus->non_giant_callback_proc,
41202ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
41302ac6454SAndrew Thompson 		printf("WARNING: Creation of USB non-Giant "
41402ac6454SAndrew Thompson 		    "callback process failed.\n");
41502ac6454SAndrew Thompson 	} else if (usb2_proc_create(&bus->explore_proc,
41602ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
41702ac6454SAndrew Thompson 		printf("WARNING: Creation of USB explore "
41802ac6454SAndrew Thompson 		    "process failed.\n");
419672c9965SAndrew Thompson 	} else if (usb2_proc_create(&bus->control_xfer_proc,
420672c9965SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
421672c9965SAndrew Thompson 		printf("WARNING: Creation of USB control transfer "
422672c9965SAndrew Thompson 		    "process failed.\n");
42302ac6454SAndrew Thompson 	} else {
42402ac6454SAndrew Thompson 		/* Get final attach going */
42502ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
42602ac6454SAndrew Thompson 		if (usb2_proc_msignal(&bus->explore_proc,
42702ac6454SAndrew Thompson 		    &bus->attach_msg[0], &bus->attach_msg[1])) {
42802ac6454SAndrew Thompson 			/* ignore */
42902ac6454SAndrew Thompson 		}
43002ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
43102ac6454SAndrew Thompson 	}
43202ac6454SAndrew Thompson }
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
43502ac6454SAndrew Thompson  *	usb2_post_init
43602ac6454SAndrew Thompson  *
43702ac6454SAndrew Thompson  * This function is called to attach all USB busses that were found
43802ac6454SAndrew Thompson  * during bootup.
43902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
44002ac6454SAndrew Thompson static void
44102ac6454SAndrew Thompson usb2_post_init(void *arg)
44202ac6454SAndrew Thompson {
443760bc48eSAndrew Thompson 	struct usb_bus *bus;
44402ac6454SAndrew Thompson 	devclass_t dc;
44502ac6454SAndrew Thompson 	device_t dev;
44602ac6454SAndrew Thompson 	int max;
44702ac6454SAndrew Thompson 	int n;
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	mtx_lock(&Giant);
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	usb2_devclass_ptr = devclass_find("usbus");
45202ac6454SAndrew Thompson 
45302ac6454SAndrew Thompson 	dc = usb2_devclass_ptr;
45402ac6454SAndrew Thompson 	if (dc) {
45502ac6454SAndrew Thompson 		max = devclass_get_maxunit(dc) + 1;
45602ac6454SAndrew Thompson 		for (n = 0; n != max; n++) {
45702ac6454SAndrew Thompson 			dev = devclass_get_device(dc, n);
45802ac6454SAndrew Thompson 			if (dev && device_is_attached(dev)) {
45902ac6454SAndrew Thompson 				bus = device_get_ivars(dev);
46002ac6454SAndrew Thompson 				if (bus) {
46102ac6454SAndrew Thompson 					mtx_lock(&Giant);
46202ac6454SAndrew Thompson 					usb2_attach_sub(dev, bus);
46302ac6454SAndrew Thompson 					mtx_unlock(&Giant);
46402ac6454SAndrew Thompson 				}
46502ac6454SAndrew Thompson 			}
46602ac6454SAndrew Thompson 		}
46702ac6454SAndrew Thompson 	} else {
46802ac6454SAndrew Thompson 		DPRINTFN(0, "no devclass\n");
46902ac6454SAndrew Thompson 	}
47002ac6454SAndrew Thompson 	usb2_post_init_called = 1;
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	/* explore all USB busses in parallell */
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 	usb2_needs_explore_all();
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 	mtx_unlock(&Giant);
47702ac6454SAndrew Thompson }
47802ac6454SAndrew Thompson 
47902ac6454SAndrew Thompson SYSINIT(usb2_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb2_post_init, NULL);
48002ac6454SAndrew Thompson SYSUNINIT(usb2_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb2_bus_unload, NULL);
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
48302ac6454SAndrew Thompson  *	usb2_bus_mem_flush_all_cb
48402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
485bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
48602ac6454SAndrew Thompson static void
487760bc48eSAndrew Thompson usb2_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
488760bc48eSAndrew Thompson     struct usb_page *pg, uint32_t size, uint32_t align)
48902ac6454SAndrew Thompson {
49002ac6454SAndrew Thompson 	usb2_pc_cpu_flush(pc);
49102ac6454SAndrew Thompson }
492bdc081c6SAndrew Thompson #endif
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
49502ac6454SAndrew Thompson  *	usb2_bus_mem_flush_all - factored out code
49602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
497bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
49802ac6454SAndrew Thompson void
499e0a69b51SAndrew Thompson usb2_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
50002ac6454SAndrew Thompson {
50102ac6454SAndrew Thompson 	if (cb) {
50202ac6454SAndrew Thompson 		cb(bus, &usb2_bus_mem_flush_all_cb);
50302ac6454SAndrew Thompson 	}
50402ac6454SAndrew Thompson }
505bdc081c6SAndrew Thompson #endif
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
50802ac6454SAndrew Thompson  *	usb2_bus_mem_alloc_all_cb
50902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
510bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
51102ac6454SAndrew Thompson static void
512760bc48eSAndrew Thompson usb2_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
513760bc48eSAndrew Thompson     struct usb_page *pg, uint32_t size, uint32_t align)
51402ac6454SAndrew Thompson {
51502ac6454SAndrew Thompson 	/* need to initialize the page cache */
51602ac6454SAndrew Thompson 	pc->tag_parent = bus->dma_parent_tag;
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	if (usb2_pc_alloc_mem(pc, pg, size, align)) {
51902ac6454SAndrew Thompson 		bus->alloc_failed = 1;
52002ac6454SAndrew Thompson 	}
52102ac6454SAndrew Thompson }
522bdc081c6SAndrew Thompson #endif
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
52502ac6454SAndrew Thompson  *	usb2_bus_mem_alloc_all - factored out code
52602ac6454SAndrew Thompson  *
52702ac6454SAndrew Thompson  * Returns:
52802ac6454SAndrew Thompson  *    0: Success
52902ac6454SAndrew Thompson  * Else: Failure
53002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
53102ac6454SAndrew Thompson uint8_t
532760bc48eSAndrew Thompson usb2_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
533e0a69b51SAndrew Thompson     usb_bus_mem_cb_t *cb)
53402ac6454SAndrew Thompson {
53502ac6454SAndrew Thompson 	bus->alloc_failed = 0;
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
53802ac6454SAndrew Thompson 	    NULL, MTX_DEF | MTX_RECURSE);
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 	usb2_callout_init_mtx(&bus->power_wdog,
541684e3f22SAndrew Thompson 	    &bus->bus_mtx, 0);
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	TAILQ_INIT(&bus->intr_q.head);
54402ac6454SAndrew Thompson 
545bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
54602ac6454SAndrew Thompson 	usb2_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
547bdc081c6SAndrew Thompson 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
548bdc081c6SAndrew Thompson #endif
54902ac6454SAndrew Thompson 	if ((bus->devices_max > USB_MAX_DEVICES) ||
55002ac6454SAndrew Thompson 	    (bus->devices_max < USB_MIN_DEVICES) ||
55102ac6454SAndrew Thompson 	    (bus->devices == NULL)) {
55202ac6454SAndrew Thompson 		DPRINTFN(0, "Devices field has not been "
55302ac6454SAndrew Thompson 		    "initialised properly!\n");
55402ac6454SAndrew Thompson 		bus->alloc_failed = 1;		/* failure */
55502ac6454SAndrew Thompson 	}
556bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
55702ac6454SAndrew Thompson 	if (cb) {
55802ac6454SAndrew Thompson 		cb(bus, &usb2_bus_mem_alloc_all_cb);
55902ac6454SAndrew Thompson 	}
560bdc081c6SAndrew Thompson #endif
56102ac6454SAndrew Thompson 	if (bus->alloc_failed) {
56202ac6454SAndrew Thompson 		usb2_bus_mem_free_all(bus, cb);
56302ac6454SAndrew Thompson 	}
56402ac6454SAndrew Thompson 	return (bus->alloc_failed);
56502ac6454SAndrew Thompson }
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
56802ac6454SAndrew Thompson  *	usb2_bus_mem_free_all_cb
56902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
570bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
57102ac6454SAndrew Thompson static void
572760bc48eSAndrew Thompson usb2_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
573760bc48eSAndrew Thompson     struct usb_page *pg, uint32_t size, uint32_t align)
57402ac6454SAndrew Thompson {
57502ac6454SAndrew Thompson 	usb2_pc_free_mem(pc);
57602ac6454SAndrew Thompson }
577bdc081c6SAndrew Thompson #endif
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
58002ac6454SAndrew Thompson  *	usb2_bus_mem_free_all - factored out code
58102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
58202ac6454SAndrew Thompson void
583e0a69b51SAndrew Thompson usb2_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
58402ac6454SAndrew Thompson {
585bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
58602ac6454SAndrew Thompson 	if (cb) {
58702ac6454SAndrew Thompson 		cb(bus, &usb2_bus_mem_free_all_cb);
58802ac6454SAndrew Thompson 	}
58902ac6454SAndrew Thompson 	usb2_dma_tag_unsetup(bus->dma_parent_tag);
590bdc081c6SAndrew Thompson #endif
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 	mtx_destroy(&bus->bus_mtx);
59302ac6454SAndrew Thompson }
594