5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
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>
59 #include <dev/usb/usb_controller.h>
60 #include <dev/usb/usb_bus.h>
61 #include <dev/usb/controller/uss820dci.h>
65 static device_probe_t uss820_atmelarm_probe;
66 static device_attach_t uss820_atmelarm_attach;
67 static device_detach_t uss820_atmelarm_detach;
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),
81 static driver_t uss820dci_driver = {
83 .methods = uss820dci_methods,
84 .size = sizeof(struct uss820dci_softc),
87 static devclass_t uss820dci_devclass;
89 DRIVER_MODULE(uss820dci, atmelarm, uss820dci_driver, uss820dci_devclass, 0, 0);
90 MODULE_DEPEND(uss820dci, usb, 1, 1, 1);
92 static const char *const uss820_desc = "USS820 USB Device Controller";
95 uss820_atmelarm_probe(device_t dev)
97 device_set_desc(dev, uss820_desc);
98 return (0); /* success */
102 uss820_atmelarm_attach(device_t dev)
104 struct uss820dci_softc *sc = device_get_softc(dev);
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;
114 /* get all DMA memory */
115 if (usb_bus_mem_alloc_all(&sc->sc_bus,
116 USB_GET_DMA_TAG(dev), NULL)) {
121 bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
123 if (!sc->sc_io_res) {
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);
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) {
136 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
137 if (!(sc->sc_bus.bdev)) {
140 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
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);
145 sc->sc_intr_hdl = NULL;
148 err = uss820dci_init(sc);
150 device_printf(dev, "Init failed\n");
153 err = device_probe_and_attach(sc->sc_bus.bdev);
155 device_printf(dev, "USB probe and attach failed\n");
161 uss820_atmelarm_detach(dev);
166 uss820_atmelarm_detach(device_t dev)
168 struct uss820dci_softc *sc = device_get_softc(dev);
171 /* during module unload there are lots of children leftover */
172 device_delete_children(dev);
174 if (sc->sc_irq_res && sc->sc_intr_hdl) {
176 * only call at91_udp_uninit() after at91_udp_init()
178 uss820dci_uninit(sc);
180 err = bus_teardown_intr(dev, sc->sc_irq_res,
182 sc->sc_intr_hdl = NULL;
184 if (sc->sc_irq_res) {
185 bus_release_resource(dev, SYS_RES_IRQ, 0,
187 sc->sc_irq_res = NULL;
190 bus_release_resource(dev, SYS_RES_IOPORT, 0,
192 sc->sc_io_res = NULL;
194 usb_bus_mem_free_all(&sc->sc_bus, NULL);