xref: /freebsd/sys/dev/usb/controller/usb_controller.c (revision 853a10a5819946fb176b743e8b1aef0e057e29c0)
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 
5002ac6454SAndrew Thompson static void	usb2_attach_sub(device_t, struct usb2_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 
5802ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
5902ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_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 {
10702ac6454SAndrew Thompson 	struct usb2_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 {
13402ac6454SAndrew Thompson 	struct usb2_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 
17202ac6454SAndrew Thompson 	return (0);
17302ac6454SAndrew Thompson }
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
17602ac6454SAndrew Thompson  *	usb2_bus_explore
17702ac6454SAndrew Thompson  *
17802ac6454SAndrew Thompson  * This function is used to explore the device tree from the root.
17902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
18002ac6454SAndrew Thompson static void
18102ac6454SAndrew Thompson usb2_bus_explore(struct usb2_proc_msg *pm)
18202ac6454SAndrew Thompson {
18302ac6454SAndrew Thompson 	struct usb2_bus *bus;
18402ac6454SAndrew Thompson 	struct usb2_device *udev;
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson 	bus = ((struct usb2_bus_msg *)pm)->bus;
18702ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson 	if (udev && udev->hub) {
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson 		if (bus->do_probe) {
19202ac6454SAndrew Thompson 			bus->do_probe = 0;
19302ac6454SAndrew Thompson 			bus->driver_added_refcount++;
19402ac6454SAndrew Thompson 		}
19502ac6454SAndrew Thompson 		if (bus->driver_added_refcount == 0) {
19602ac6454SAndrew Thompson 			/* avoid zero, hence that is memory default */
19702ac6454SAndrew Thompson 			bus->driver_added_refcount = 1;
19802ac6454SAndrew Thompson 		}
19902ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson 		mtx_lock(&Giant);
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson 		/*
20402ac6454SAndrew Thompson 		 * First update the USB power state!
20502ac6454SAndrew Thompson 		 */
20602ac6454SAndrew Thompson 		usb2_bus_powerd(bus);
20702ac6454SAndrew Thompson 		/*
20802ac6454SAndrew Thompson 		 * Explore the Root USB HUB. This call can sleep,
20902ac6454SAndrew Thompson 		 * exiting Giant, which is actually Giant.
21002ac6454SAndrew Thompson 		 */
21102ac6454SAndrew Thompson 		(udev->hub->explore) (udev);
21202ac6454SAndrew Thompson 
21302ac6454SAndrew Thompson 		mtx_unlock(&Giant);
21402ac6454SAndrew Thompson 
21502ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
21602ac6454SAndrew Thompson 	}
21702ac6454SAndrew Thompson 	if (bus->bus_roothold != NULL) {
21802ac6454SAndrew Thompson 		root_mount_rel(bus->bus_roothold);
21902ac6454SAndrew Thompson 		bus->bus_roothold = NULL;
22002ac6454SAndrew Thompson 	}
22102ac6454SAndrew Thompson }
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
22402ac6454SAndrew Thompson  *	usb2_bus_detach
22502ac6454SAndrew Thompson  *
22602ac6454SAndrew Thompson  * This function is used to detach the device tree from the root.
22702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
22802ac6454SAndrew Thompson static void
22902ac6454SAndrew Thompson usb2_bus_detach(struct usb2_proc_msg *pm)
23002ac6454SAndrew Thompson {
23102ac6454SAndrew Thompson 	struct usb2_bus *bus;
23202ac6454SAndrew Thompson 	struct usb2_device *udev;
23302ac6454SAndrew Thompson 	device_t dev;
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 	bus = ((struct usb2_bus_msg *)pm)->bus;
23602ac6454SAndrew Thompson 	udev = bus->devices[USB_ROOT_HUB_ADDR];
23702ac6454SAndrew Thompson 	dev = bus->bdev;
23802ac6454SAndrew Thompson 	/* clear the softc */
23902ac6454SAndrew Thompson 	device_set_softc(dev, NULL);
24002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 	mtx_lock(&Giant);
24302ac6454SAndrew Thompson 
24402ac6454SAndrew Thompson 	/* detach children first */
24502ac6454SAndrew Thompson 	bus_generic_detach(dev);
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson 	/*
24802ac6454SAndrew Thompson 	 * Free USB Root device, but not any sub-devices, hence they
24902ac6454SAndrew Thompson 	 * are freed by the caller of this function:
25002ac6454SAndrew Thompson 	 */
251bdd41206SAndrew Thompson 	usb2_free_device(udev,
252bdd41206SAndrew Thompson 	    USB_UNCFG_FLAG_FREE_EP0);
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson 	mtx_unlock(&Giant);
25502ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
25602ac6454SAndrew Thompson 	/* clear bdev variable last */
25702ac6454SAndrew Thompson 	bus->bdev = NULL;
25802ac6454SAndrew Thompson }
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson static void
26102ac6454SAndrew Thompson usb2_power_wdog(void *arg)
26202ac6454SAndrew Thompson {
26302ac6454SAndrew Thompson 	struct usb2_bus *bus = arg;
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
26602ac6454SAndrew Thompson 
26702ac6454SAndrew Thompson 	usb2_callout_reset(&bus->power_wdog,
26802ac6454SAndrew Thompson 	    4 * hz, usb2_power_wdog, arg);
26902ac6454SAndrew Thompson 
27002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
27102ac6454SAndrew Thompson 
27202ac6454SAndrew Thompson 	usb2_bus_power_update(bus);
27302ac6454SAndrew Thompson 
274684e3f22SAndrew Thompson 	USB_BUS_LOCK(bus);
27502ac6454SAndrew Thompson }
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
27802ac6454SAndrew Thompson  *	usb2_bus_attach
27902ac6454SAndrew Thompson  *
28002ac6454SAndrew Thompson  * This function attaches USB in context of the explore thread.
28102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
28202ac6454SAndrew Thompson static void
28302ac6454SAndrew Thompson usb2_bus_attach(struct usb2_proc_msg *pm)
28402ac6454SAndrew Thompson {
28502ac6454SAndrew Thompson 	struct usb2_bus *bus;
28602ac6454SAndrew Thompson 	struct usb2_device *child;
28702ac6454SAndrew Thompson 	device_t dev;
28802ac6454SAndrew Thompson 	usb2_error_t err;
28902ac6454SAndrew Thompson 	uint8_t speed;
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson 	bus = ((struct usb2_bus_msg *)pm)->bus;
29202ac6454SAndrew Thompson 	dev = bus->bdev;
29302ac6454SAndrew Thompson 
29402ac6454SAndrew Thompson 	DPRINTF("\n");
29502ac6454SAndrew Thompson 
29602ac6454SAndrew Thompson 	switch (bus->usbrev) {
29702ac6454SAndrew Thompson 	case USB_REV_1_0:
29802ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
29902ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
30002ac6454SAndrew Thompson 		break;
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	case USB_REV_1_1:
30302ac6454SAndrew Thompson 		speed = USB_SPEED_FULL;
30402ac6454SAndrew Thompson 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
30502ac6454SAndrew Thompson 		break;
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 	case USB_REV_2_0:
30802ac6454SAndrew Thompson 		speed = USB_SPEED_HIGH;
30902ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
31002ac6454SAndrew Thompson 		break;
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson 	case USB_REV_2_5:
31302ac6454SAndrew Thompson 		speed = USB_SPEED_VARIABLE;
31402ac6454SAndrew Thompson 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
31502ac6454SAndrew Thompson 		break;
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson 	default:
31802ac6454SAndrew Thompson 		device_printf(bus->bdev, "Unsupported USB revision!\n");
31902ac6454SAndrew Thompson 		return;
32002ac6454SAndrew Thompson 	}
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(bus);
32302ac6454SAndrew Thompson 	mtx_lock(&Giant);		/* XXX not required by USB */
32402ac6454SAndrew Thompson 
3254eae601eSAndrew Thompson 	/* default power_mask value */
3264eae601eSAndrew Thompson 	bus->hw_power_state =
3274eae601eSAndrew Thompson 	  USB_HW_POWER_CONTROL |
3284eae601eSAndrew Thompson 	  USB_HW_POWER_BULK |
3294eae601eSAndrew Thompson 	  USB_HW_POWER_INTERRUPT |
3304eae601eSAndrew Thompson 	  USB_HW_POWER_ISOC |
3314eae601eSAndrew Thompson 	  USB_HW_POWER_NON_ROOT_HUB;
3324eae601eSAndrew Thompson 
3334eae601eSAndrew Thompson 	/* make sure power is set at least once */
3344eae601eSAndrew Thompson 
3354eae601eSAndrew Thompson 	if (bus->methods->set_hw_power != NULL) {
3364eae601eSAndrew Thompson 		(bus->methods->set_hw_power) (bus);
3374eae601eSAndrew Thompson 	}
3384eae601eSAndrew Thompson 
33902ac6454SAndrew Thompson 	/* Allocate the Root USB device */
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	child = usb2_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
34202ac6454SAndrew Thompson 	    speed, USB_MODE_HOST);
34302ac6454SAndrew Thompson 	if (child) {
34402ac6454SAndrew Thompson 		err = usb2_probe_and_attach(child,
34502ac6454SAndrew Thompson 		    USB_IFACE_INDEX_ANY);
34602ac6454SAndrew Thompson 		if (!err) {
3471be5bf51SAndrew Thompson 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
3481be5bf51SAndrew Thompson 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
34902ac6454SAndrew Thompson 				err = USB_ERR_NO_ROOT_HUB;
35002ac6454SAndrew Thompson 			}
35102ac6454SAndrew Thompson 		}
35202ac6454SAndrew Thompson 	} else {
35302ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
35402ac6454SAndrew Thompson 	}
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson 	mtx_unlock(&Giant);
35702ac6454SAndrew Thompson 	USB_BUS_LOCK(bus);
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 	if (err) {
36002ac6454SAndrew Thompson 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
36102ac6454SAndrew Thompson 		    usb2_errstr(err));
36202ac6454SAndrew Thompson 	}
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 	/* set softc - we are ready */
36502ac6454SAndrew Thompson 	device_set_softc(dev, bus);
36602ac6454SAndrew Thompson 
367684e3f22SAndrew Thompson 	/* start watchdog */
36802ac6454SAndrew Thompson 	usb2_power_wdog(bus);
36902ac6454SAndrew Thompson }
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
37202ac6454SAndrew Thompson  *	usb2_attach_sub
37302ac6454SAndrew Thompson  *
37402ac6454SAndrew Thompson  * This function creates a thread which runs the USB attach code. It
37502ac6454SAndrew Thompson  * is factored out, hence it can be called at two different places in
37602ac6454SAndrew Thompson  * time. During bootup this function is called from
37702ac6454SAndrew Thompson  * "usb2_post_init". During hot-plug it is called directly from the
37802ac6454SAndrew Thompson  * "usb2_attach()" method.
37902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
38002ac6454SAndrew Thompson static void
38102ac6454SAndrew Thompson usb2_attach_sub(device_t dev, struct usb2_bus *bus)
38202ac6454SAndrew Thompson {
38302ac6454SAndrew Thompson 	const char *pname = device_get_nameunit(dev);
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	/* Initialise USB process messages */
38602ac6454SAndrew Thompson 	bus->explore_msg[0].hdr.pm_callback = &usb2_bus_explore;
38702ac6454SAndrew Thompson 	bus->explore_msg[0].bus = bus;
38802ac6454SAndrew Thompson 	bus->explore_msg[1].hdr.pm_callback = &usb2_bus_explore;
38902ac6454SAndrew Thompson 	bus->explore_msg[1].bus = bus;
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson 	bus->detach_msg[0].hdr.pm_callback = &usb2_bus_detach;
39202ac6454SAndrew Thompson 	bus->detach_msg[0].bus = bus;
39302ac6454SAndrew Thompson 	bus->detach_msg[1].hdr.pm_callback = &usb2_bus_detach;
39402ac6454SAndrew Thompson 	bus->detach_msg[1].bus = bus;
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	bus->attach_msg[0].hdr.pm_callback = &usb2_bus_attach;
39702ac6454SAndrew Thompson 	bus->attach_msg[0].bus = bus;
39802ac6454SAndrew Thompson 	bus->attach_msg[1].hdr.pm_callback = &usb2_bus_attach;
39902ac6454SAndrew Thompson 	bus->attach_msg[1].bus = bus;
40002ac6454SAndrew Thompson 
40139307315SAndrew Thompson 	/* Create USB explore and callback processes */
40202ac6454SAndrew Thompson 
40302ac6454SAndrew Thompson 	if (usb2_proc_create(&bus->giant_callback_proc,
40402ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
40502ac6454SAndrew Thompson 		printf("WARNING: Creation of USB Giant "
40602ac6454SAndrew Thompson 		    "callback process failed.\n");
40702ac6454SAndrew Thompson 	} else if (usb2_proc_create(&bus->non_giant_callback_proc,
40802ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
40902ac6454SAndrew Thompson 		printf("WARNING: Creation of USB non-Giant "
41002ac6454SAndrew Thompson 		    "callback process failed.\n");
41102ac6454SAndrew Thompson 	} else if (usb2_proc_create(&bus->explore_proc,
41202ac6454SAndrew Thompson 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
41302ac6454SAndrew Thompson 		printf("WARNING: Creation of USB explore "
41402ac6454SAndrew Thompson 		    "process failed.\n");
41502ac6454SAndrew Thompson 	} else {
41602ac6454SAndrew Thompson 		/* Get final attach going */
41702ac6454SAndrew Thompson 		USB_BUS_LOCK(bus);
41802ac6454SAndrew Thompson 		if (usb2_proc_msignal(&bus->explore_proc,
41902ac6454SAndrew Thompson 		    &bus->attach_msg[0], &bus->attach_msg[1])) {
42002ac6454SAndrew Thompson 			/* ignore */
42102ac6454SAndrew Thompson 		}
42202ac6454SAndrew Thompson 		USB_BUS_UNLOCK(bus);
42302ac6454SAndrew Thompson 	}
42402ac6454SAndrew Thompson }
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
42702ac6454SAndrew Thompson  *	usb2_post_init
42802ac6454SAndrew Thompson  *
42902ac6454SAndrew Thompson  * This function is called to attach all USB busses that were found
43002ac6454SAndrew Thompson  * during bootup.
43102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
43202ac6454SAndrew Thompson static void
43302ac6454SAndrew Thompson usb2_post_init(void *arg)
43402ac6454SAndrew Thompson {
43502ac6454SAndrew Thompson 	struct usb2_bus *bus;
43602ac6454SAndrew Thompson 	devclass_t dc;
43702ac6454SAndrew Thompson 	device_t dev;
43802ac6454SAndrew Thompson 	int max;
43902ac6454SAndrew Thompson 	int n;
44002ac6454SAndrew Thompson 
44102ac6454SAndrew Thompson 	mtx_lock(&Giant);
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson 	usb2_devclass_ptr = devclass_find("usbus");
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson 	dc = usb2_devclass_ptr;
44602ac6454SAndrew Thompson 	if (dc) {
44702ac6454SAndrew Thompson 		max = devclass_get_maxunit(dc) + 1;
44802ac6454SAndrew Thompson 		for (n = 0; n != max; n++) {
44902ac6454SAndrew Thompson 			dev = devclass_get_device(dc, n);
45002ac6454SAndrew Thompson 			if (dev && device_is_attached(dev)) {
45102ac6454SAndrew Thompson 				bus = device_get_ivars(dev);
45202ac6454SAndrew Thompson 				if (bus) {
45302ac6454SAndrew Thompson 					mtx_lock(&Giant);
45402ac6454SAndrew Thompson 					usb2_attach_sub(dev, bus);
45502ac6454SAndrew Thompson 					mtx_unlock(&Giant);
45602ac6454SAndrew Thompson 				}
45702ac6454SAndrew Thompson 			}
45802ac6454SAndrew Thompson 		}
45902ac6454SAndrew Thompson 	} else {
46002ac6454SAndrew Thompson 		DPRINTFN(0, "no devclass\n");
46102ac6454SAndrew Thompson 	}
46202ac6454SAndrew Thompson 	usb2_post_init_called = 1;
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	/* explore all USB busses in parallell */
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson 	usb2_needs_explore_all();
46702ac6454SAndrew Thompson 
46802ac6454SAndrew Thompson 	mtx_unlock(&Giant);
46902ac6454SAndrew Thompson }
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson SYSINIT(usb2_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb2_post_init, NULL);
47202ac6454SAndrew Thompson SYSUNINIT(usb2_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb2_bus_unload, NULL);
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
47502ac6454SAndrew Thompson  *	usb2_bus_mem_flush_all_cb
47602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
477bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
47802ac6454SAndrew Thompson static void
47902ac6454SAndrew Thompson usb2_bus_mem_flush_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
48002ac6454SAndrew Thompson     struct usb2_page *pg, uint32_t size, uint32_t align)
48102ac6454SAndrew Thompson {
48202ac6454SAndrew Thompson 	usb2_pc_cpu_flush(pc);
48302ac6454SAndrew Thompson }
484bdc081c6SAndrew Thompson #endif
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
48702ac6454SAndrew Thompson  *	usb2_bus_mem_flush_all - factored out code
48802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
489bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
49002ac6454SAndrew Thompson void
49102ac6454SAndrew Thompson usb2_bus_mem_flush_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb)
49202ac6454SAndrew Thompson {
49302ac6454SAndrew Thompson 	if (cb) {
49402ac6454SAndrew Thompson 		cb(bus, &usb2_bus_mem_flush_all_cb);
49502ac6454SAndrew Thompson 	}
49602ac6454SAndrew Thompson }
497bdc081c6SAndrew Thompson #endif
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
50002ac6454SAndrew Thompson  *	usb2_bus_mem_alloc_all_cb
50102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
502bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
50302ac6454SAndrew Thompson static void
50402ac6454SAndrew Thompson usb2_bus_mem_alloc_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
50502ac6454SAndrew Thompson     struct usb2_page *pg, uint32_t size, uint32_t align)
50602ac6454SAndrew Thompson {
50702ac6454SAndrew Thompson 	/* need to initialize the page cache */
50802ac6454SAndrew Thompson 	pc->tag_parent = bus->dma_parent_tag;
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson 	if (usb2_pc_alloc_mem(pc, pg, size, align)) {
51102ac6454SAndrew Thompson 		bus->alloc_failed = 1;
51202ac6454SAndrew Thompson 	}
51302ac6454SAndrew Thompson }
514bdc081c6SAndrew Thompson #endif
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
51702ac6454SAndrew Thompson  *	usb2_bus_mem_alloc_all - factored out code
51802ac6454SAndrew Thompson  *
51902ac6454SAndrew Thompson  * Returns:
52002ac6454SAndrew Thompson  *    0: Success
52102ac6454SAndrew Thompson  * Else: Failure
52202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
52302ac6454SAndrew Thompson uint8_t
52402ac6454SAndrew Thompson usb2_bus_mem_alloc_all(struct usb2_bus *bus, bus_dma_tag_t dmat,
52502ac6454SAndrew Thompson     usb2_bus_mem_cb_t *cb)
52602ac6454SAndrew Thompson {
52702ac6454SAndrew Thompson 	bus->alloc_failed = 0;
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
53002ac6454SAndrew Thompson 	    NULL, MTX_DEF | MTX_RECURSE);
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	usb2_callout_init_mtx(&bus->power_wdog,
533684e3f22SAndrew Thompson 	    &bus->bus_mtx, 0);
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 	TAILQ_INIT(&bus->intr_q.head);
53602ac6454SAndrew Thompson 
537bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
53802ac6454SAndrew Thompson 	usb2_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
539bdc081c6SAndrew Thompson 	    dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
540bdc081c6SAndrew Thompson #endif
54102ac6454SAndrew Thompson 	if ((bus->devices_max > USB_MAX_DEVICES) ||
54202ac6454SAndrew Thompson 	    (bus->devices_max < USB_MIN_DEVICES) ||
54302ac6454SAndrew Thompson 	    (bus->devices == NULL)) {
54402ac6454SAndrew Thompson 		DPRINTFN(0, "Devices field has not been "
54502ac6454SAndrew Thompson 		    "initialised properly!\n");
54602ac6454SAndrew Thompson 		bus->alloc_failed = 1;		/* failure */
54702ac6454SAndrew Thompson 	}
548bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
54902ac6454SAndrew Thompson 	if (cb) {
55002ac6454SAndrew Thompson 		cb(bus, &usb2_bus_mem_alloc_all_cb);
55102ac6454SAndrew Thompson 	}
552bdc081c6SAndrew Thompson #endif
55302ac6454SAndrew Thompson 	if (bus->alloc_failed) {
55402ac6454SAndrew Thompson 		usb2_bus_mem_free_all(bus, cb);
55502ac6454SAndrew Thompson 	}
55602ac6454SAndrew Thompson 	return (bus->alloc_failed);
55702ac6454SAndrew Thompson }
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
56002ac6454SAndrew Thompson  *	usb2_bus_mem_free_all_cb
56102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
562bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
56302ac6454SAndrew Thompson static void
56402ac6454SAndrew Thompson usb2_bus_mem_free_all_cb(struct usb2_bus *bus, struct usb2_page_cache *pc,
56502ac6454SAndrew Thompson     struct usb2_page *pg, uint32_t size, uint32_t align)
56602ac6454SAndrew Thompson {
56702ac6454SAndrew Thompson 	usb2_pc_free_mem(pc);
56802ac6454SAndrew Thompson }
569bdc081c6SAndrew Thompson #endif
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
57202ac6454SAndrew Thompson  *	usb2_bus_mem_free_all - factored out code
57302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
57402ac6454SAndrew Thompson void
57502ac6454SAndrew Thompson usb2_bus_mem_free_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb)
57602ac6454SAndrew Thompson {
577bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
57802ac6454SAndrew Thompson 	if (cb) {
57902ac6454SAndrew Thompson 		cb(bus, &usb2_bus_mem_free_all_cb);
58002ac6454SAndrew Thompson 	}
58102ac6454SAndrew Thompson 	usb2_dma_tag_unsetup(bus->dma_parent_tag);
582bdc081c6SAndrew Thompson #endif
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 	mtx_destroy(&bus->bus_mtx);
58502ac6454SAndrew Thompson }
586