uss820dci_atmelarm.c revision 192444
1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD: head/sys/dev/usb/controller/uss820dci_atmelarm.c 192444 2009-05-20 16:58:53Z thompsa $");
3
4/*-
5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <dev/usb/usb_mfunc.h>
31#include <dev/usb/usb.h>
32
33#include <dev/usb/usb_core.h>
34#include <dev/usb/usb_busdma.h>
35#include <dev/usb/usb_process.h>
36#include <dev/usb/usb_util.h>
37
38#include <dev/usb/usb_controller.h>
39#include <dev/usb/usb_bus.h>
40#include <dev/usb/controller/uss820dci.h>
41
42#include <sys/rman.h>
43
44static device_probe_t uss820_atmelarm_probe;
45static device_attach_t uss820_atmelarm_attach;
46static device_detach_t uss820_atmelarm_detach;
47static device_suspend_t uss820_atmelarm_suspend;
48static device_resume_t uss820_atmelarm_resume;
49static device_shutdown_t uss820_atmelarm_shutdown;
50
51static device_method_t uss820dci_methods[] = {
52	/* Device interface */
53	DEVMETHOD(device_probe, uss820_atmelarm_probe),
54	DEVMETHOD(device_attach, uss820_atmelarm_attach),
55	DEVMETHOD(device_detach, uss820_atmelarm_detach),
56	DEVMETHOD(device_suspend, uss820_atmelarm_suspend),
57	DEVMETHOD(device_resume, uss820_atmelarm_resume),
58	DEVMETHOD(device_shutdown, uss820_atmelarm_shutdown),
59
60	/* Bus interface */
61	DEVMETHOD(bus_print_child, bus_generic_print_child),
62
63	{0, 0}
64};
65
66static driver_t uss820dci_driver = {
67	.name = "uss820",
68	.methods = uss820dci_methods,
69	.size = sizeof(struct uss820dci_softc),
70};
71
72static devclass_t uss820dci_devclass;
73
74DRIVER_MODULE(uss820, atmelarm, uss820dci_driver, uss820dci_devclass, 0, 0);
75MODULE_DEPEND(uss820, usb, 1, 1, 1);
76
77static const char *const uss820_desc = "USS820 USB Device Controller";
78
79static int
80uss820_atmelarm_suspend(device_t dev)
81{
82	struct uss820dci_softc *sc = device_get_softc(dev);
83	int err;
84
85	err = bus_generic_suspend(dev);
86	if (err == 0) {
87		uss820dci_suspend(sc);
88	}
89	return (err);
90}
91
92static int
93uss820_atmelarm_resume(device_t dev)
94{
95	struct uss820dci_softc *sc = device_get_softc(dev);
96	int err;
97
98	uss820dci_resume(sc);
99
100	err = bus_generic_resume(dev);
101
102	return (err);
103}
104
105static int
106uss820_atmelarm_shutdown(device_t dev)
107{
108	struct uss820dci_softc *sc = device_get_softc(dev);
109	int err;
110
111	err = bus_generic_shutdown(dev);
112	if (err)
113		return (err);
114
115	uss820dci_uninit(sc);
116
117	return (0);
118}
119
120static int
121uss820_atmelarm_probe(device_t dev)
122{
123	device_set_desc(dev, uss820_desc);
124	return (0);			/* success */
125}
126
127static int
128uss820_atmelarm_attach(device_t dev)
129{
130	struct uss820dci_softc *sc = device_get_softc(dev);
131	int err;
132	int rid;
133
134	/* initialise some bus fields */
135	sc->sc_bus.parent = dev;
136	sc->sc_bus.devices = sc->sc_devices;
137	sc->sc_bus.devices_max = USS820_MAX_DEVICES;
138
139	/* get all DMA memory */
140	if (usb2_bus_mem_alloc_all(&sc->sc_bus,
141	    USB_GET_DMA_TAG(dev), NULL)) {
142		return (ENOMEM);
143	}
144	rid = 0;
145	sc->sc_io_res =
146	    bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
147
148	if (!sc->sc_io_res) {
149		goto error;
150	}
151	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
152	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
153	sc->sc_io_size = rman_get_size(sc->sc_io_res);
154
155	rid = 0;
156	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
157	    RF_SHAREABLE | RF_ACTIVE);
158	if (sc->sc_irq_res == NULL) {
159		goto error;
160	}
161	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
162	if (!(sc->sc_bus.bdev)) {
163		goto error;
164	}
165	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
166
167#if (__FreeBSD_version >= 700031)
168	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
169	    NULL, (driver_intr_t *)uss820dci_interrupt, sc, &sc->sc_intr_hdl);
170#else
171	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
172	    (driver_intr_t *)uss820dci_interrupt, sc, &sc->sc_intr_hdl);
173#endif
174	if (err) {
175		sc->sc_intr_hdl = NULL;
176		goto error;
177	}
178	err = uss820dci_init(sc);
179	if (err) {
180		device_printf(dev, "Init failed\n");
181		goto error;
182	}
183	err = device_probe_and_attach(sc->sc_bus.bdev);
184	if (err) {
185		device_printf(dev, "USB probe and attach failed\n");
186		goto error;
187	}
188	return (0);
189
190error:
191	uss820_atmelarm_detach(dev);
192	return (ENXIO);
193}
194
195static int
196uss820_atmelarm_detach(device_t dev)
197{
198	struct uss820dci_softc *sc = device_get_softc(dev);
199	device_t bdev;
200	int err;
201
202	if (sc->sc_bus.bdev) {
203		bdev = sc->sc_bus.bdev;
204		device_detach(bdev);
205		device_delete_child(dev, bdev);
206	}
207	/* during module unload there are lots of children leftover */
208	device_delete_all_children(dev);
209
210	if (sc->sc_irq_res && sc->sc_intr_hdl) {
211		/*
212		 * only call at91_udp_uninit() after at91_udp_init()
213		 */
214		uss820dci_uninit(sc);
215
216		err = bus_teardown_intr(dev, sc->sc_irq_res,
217		    sc->sc_intr_hdl);
218		sc->sc_intr_hdl = NULL;
219	}
220	if (sc->sc_irq_res) {
221		bus_release_resource(dev, SYS_RES_IRQ, 0,
222		    sc->sc_irq_res);
223		sc->sc_irq_res = NULL;
224	}
225	if (sc->sc_io_res) {
226		bus_release_resource(dev, SYS_RES_IOPORT, 0,
227		    sc->sc_io_res);
228		sc->sc_io_res = NULL;
229	}
230	usb2_bus_mem_free_all(&sc->sc_bus, NULL);
231
232	return (0);
233}
234