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