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