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