]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/usb/controller/uss820dci_atmelarm.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / usb / controller / uss820dci_atmelarm.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
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 <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/linker_set.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_util.h>
57
58 #include <dev/usb/usb_controller.h>
59 #include <dev/usb/usb_bus.h>
60 #include <dev/usb/controller/uss820dci.h>
61
62 #include <sys/rman.h>
63
64 static device_probe_t uss820_atmelarm_probe;
65 static device_attach_t uss820_atmelarm_attach;
66 static device_detach_t uss820_atmelarm_detach;
67 static device_suspend_t uss820_atmelarm_suspend;
68 static device_resume_t uss820_atmelarm_resume;
69 static device_shutdown_t uss820_atmelarm_shutdown;
70
71 static device_method_t uss820dci_methods[] = {
72         /* Device interface */
73         DEVMETHOD(device_probe, uss820_atmelarm_probe),
74         DEVMETHOD(device_attach, uss820_atmelarm_attach),
75         DEVMETHOD(device_detach, uss820_atmelarm_detach),
76         DEVMETHOD(device_suspend, uss820_atmelarm_suspend),
77         DEVMETHOD(device_resume, uss820_atmelarm_resume),
78         DEVMETHOD(device_shutdown, uss820_atmelarm_shutdown),
79
80         /* Bus interface */
81         DEVMETHOD(bus_print_child, bus_generic_print_child),
82
83         {0, 0}
84 };
85
86 static driver_t uss820dci_driver = {
87         .name = "uss820",
88         .methods = uss820dci_methods,
89         .size = sizeof(struct uss820dci_softc),
90 };
91
92 static devclass_t uss820dci_devclass;
93
94 DRIVER_MODULE(uss820, atmelarm, uss820dci_driver, uss820dci_devclass, 0, 0);
95 MODULE_DEPEND(uss820, usb, 1, 1, 1);
96
97 static const char *const uss820_desc = "USS820 USB Device Controller";
98
99 static int
100 uss820_atmelarm_suspend(device_t dev)
101 {
102         struct uss820dci_softc *sc = device_get_softc(dev);
103         int err;
104
105         err = bus_generic_suspend(dev);
106         if (err == 0) {
107                 uss820dci_suspend(sc);
108         }
109         return (err);
110 }
111
112 static int
113 uss820_atmelarm_resume(device_t dev)
114 {
115         struct uss820dci_softc *sc = device_get_softc(dev);
116         int err;
117
118         uss820dci_resume(sc);
119
120         err = bus_generic_resume(dev);
121
122         return (err);
123 }
124
125 static int
126 uss820_atmelarm_shutdown(device_t dev)
127 {
128         struct uss820dci_softc *sc = device_get_softc(dev);
129         int err;
130
131         err = bus_generic_shutdown(dev);
132         if (err)
133                 return (err);
134
135         uss820dci_uninit(sc);
136
137         return (0);
138 }
139
140 static int
141 uss820_atmelarm_probe(device_t dev)
142 {
143         device_set_desc(dev, uss820_desc);
144         return (0);                     /* success */
145 }
146
147 static int
148 uss820_atmelarm_attach(device_t dev)
149 {
150         struct uss820dci_softc *sc = device_get_softc(dev);
151         int err;
152         int rid;
153
154         /* initialise some bus fields */
155         sc->sc_bus.parent = dev;
156         sc->sc_bus.devices = sc->sc_devices;
157         sc->sc_bus.devices_max = USS820_MAX_DEVICES;
158
159         /* get all DMA memory */
160         if (usb_bus_mem_alloc_all(&sc->sc_bus,
161             USB_GET_DMA_TAG(dev), NULL)) {
162                 return (ENOMEM);
163         }
164         rid = 0;
165         sc->sc_io_res =
166             bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
167
168         if (!sc->sc_io_res) {
169                 goto error;
170         }
171         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
172         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
173         sc->sc_io_size = rman_get_size(sc->sc_io_res);
174
175         rid = 0;
176         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
177             RF_SHAREABLE | RF_ACTIVE);
178         if (sc->sc_irq_res == NULL) {
179                 goto error;
180         }
181         sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
182         if (!(sc->sc_bus.bdev)) {
183                 goto error;
184         }
185         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
186
187 #if (__FreeBSD_version >= 700031)
188         err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
189             NULL, (driver_intr_t *)uss820dci_interrupt, sc, &sc->sc_intr_hdl);
190 #else
191         err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
192             (driver_intr_t *)uss820dci_interrupt, sc, &sc->sc_intr_hdl);
193 #endif
194         if (err) {
195                 sc->sc_intr_hdl = NULL;
196                 goto error;
197         }
198         err = uss820dci_init(sc);
199         if (err) {
200                 device_printf(dev, "Init failed\n");
201                 goto error;
202         }
203         err = device_probe_and_attach(sc->sc_bus.bdev);
204         if (err) {
205                 device_printf(dev, "USB probe and attach failed\n");
206                 goto error;
207         }
208         return (0);
209
210 error:
211         uss820_atmelarm_detach(dev);
212         return (ENXIO);
213 }
214
215 static int
216 uss820_atmelarm_detach(device_t dev)
217 {
218         struct uss820dci_softc *sc = device_get_softc(dev);
219         device_t bdev;
220         int err;
221
222         if (sc->sc_bus.bdev) {
223                 bdev = sc->sc_bus.bdev;
224                 device_detach(bdev);
225                 device_delete_child(dev, bdev);
226         }
227         /* during module unload there are lots of children leftover */
228         device_delete_all_children(dev);
229
230         if (sc->sc_irq_res && sc->sc_intr_hdl) {
231                 /*
232                  * only call at91_udp_uninit() after at91_udp_init()
233                  */
234                 uss820dci_uninit(sc);
235
236                 err = bus_teardown_intr(dev, sc->sc_irq_res,
237                     sc->sc_intr_hdl);
238                 sc->sc_intr_hdl = NULL;
239         }
240         if (sc->sc_irq_res) {
241                 bus_release_resource(dev, SYS_RES_IRQ, 0,
242                     sc->sc_irq_res);
243                 sc->sc_irq_res = NULL;
244         }
245         if (sc->sc_io_res) {
246                 bus_release_resource(dev, SYS_RES_IOPORT, 0,
247                     sc->sc_io_res);
248                 sc->sc_io_res = NULL;
249         }
250         usb_bus_mem_free_all(&sc->sc_bus, NULL);
251
252         return (0);
253 }