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