FreeBSD kernel usb device Code
generic_ehci.c
Go to the documentation of this file.
1/*-
2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3 * Copyright (c) 2016 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * This software was developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * Generic EHCI driver based on the Allwinner A10 EHCI driver
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38#include "opt_bus.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/bus.h>
43#include <sys/rman.h>
44#include <sys/condvar.h>
45#include <sys/kernel.h>
46#include <sys/module.h>
47
48#include <machine/bus.h>
49
50#include <dev/usb/usb.h>
51#include <dev/usb/usbdi.h>
52
53#include <dev/usb/usb_core.h>
54#include <dev/usb/usb_busdma.h>
55#include <dev/usb/usb_process.h>
56#include <dev/usb/usb_util.h>
57
59#include <dev/usb/usb_bus.h>
62
63#include "generic_ehci.h"
64
65int
66generic_ehci_attach(device_t self)
67{
68 ehci_softc_t *sc = device_get_softc(self);
69 int err;
70 int rid;
71
72 /* initialise some bus fields */
73 sc->sc_bus.parent = self;
74 sc->sc_bus.devices = sc->sc_devices;
76 sc->sc_bus.dma_bits = 32;
77
78 /* get all DMA memory */
81 return (ENOMEM);
82 }
83
85
86 rid = 0;
87 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
88 RF_ACTIVE);
89 if (!sc->sc_io_res) {
90 device_printf(self, "Could not map memory\n");
91 goto error;
92 }
93
94 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
95 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
96 sc->sc_io_size = rman_get_size(sc->sc_io_res);
97
98 rid = 0;
99 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
100 RF_SHAREABLE | RF_ACTIVE);
101 if (sc->sc_irq_res == NULL) {
102 device_printf(self, "Could not allocate irq\n");
103 goto error;
104 }
105 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
106 if (!sc->sc_bus.bdev) {
107 device_printf(self, "Could not add USB device\n");
108 goto error;
109 }
110 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
111
112 strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor));
113
114 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
115 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
116 if (err) {
117 device_printf(self, "Could not setup irq, %d\n", err);
118 sc->sc_intr_hdl = NULL;
119 goto error;
120 }
121
123
124 err = ehci_init(sc);
125 if (!err)
126 err = device_probe_and_attach(sc->sc_bus.bdev);
127 if (err)
128 goto error;
129
130 return (0);
131
132error:
134 return (ENXIO);
135}
136
137int
139{
140 ehci_softc_t *sc = device_get_softc(self);
141 int err;
142
143 /* during module unload there are lots of children leftover */
144 device_delete_children(self);
145
146 if (sc->sc_irq_res && sc->sc_intr_hdl) {
147 /*
148 * only call ehci_detach() after ehci_init()
149 */
150 ehci_detach(sc);
151
152 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
153
154 if (err)
155 /* XXX or should we panic? */
156 device_printf(self, "Could not tear down irq, %d\n",
157 err);
158 sc->sc_intr_hdl = NULL;
159 }
160
161 if (sc->sc_irq_res) {
162 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
163 sc->sc_irq_res = NULL;
164 }
165 if (sc->sc_io_res) {
166 bus_release_resource(self, SYS_RES_MEMORY, 0,
167 sc->sc_io_res);
168 sc->sc_io_res = NULL;
169 }
171
172 return (0);
173}
174
175static device_method_t ehci_methods[] = {
176 /* Device interface */
177 DEVMETHOD(device_attach, generic_ehci_attach),
178 DEVMETHOD(device_detach, generic_ehci_detach),
179 DEVMETHOD(device_suspend, bus_generic_suspend),
180 DEVMETHOD(device_resume, bus_generic_resume),
181 DEVMETHOD(device_shutdown, bus_generic_shutdown),
182
183 DEVMETHOD_END
184};
185
187 .name = "ehci",
188 .methods = ehci_methods,
189 .size = sizeof(ehci_softc_t),
190};
void ehci_interrupt(ehci_softc_t *sc)
Definition: ehci.c:1451
void ehci_detach(ehci_softc_t *sc)
Definition: ehci.c:553
usb_error_t ehci_init(ehci_softc_t *sc)
Definition: ehci.c:281
struct ehci_softc ehci_softc_t
#define EHCI_SCFLG_DONTRESET
Definition: ehci.h:346
usb_bus_mem_cb_t ehci_iterate_hw_softc
Definition: ehci.h:445
#define EHCI_MAX_DEVICES
Definition: ehci.h:36
int generic_ehci_attach(device_t self)
Definition: generic_ehci.c:66
__FBSDID("$FreeBSD$")
int generic_ehci_detach(device_t self)
Definition: generic_ehci.c:138
driver_t generic_ehci_driver
Definition: generic_ehci.c:186
static device_method_t ehci_methods[]
Definition: generic_ehci.c:175
struct @109 error
uint16_t rid
struct usb_device * sc_devices[EHCI_MAX_DEVICES]
Definition: ehci.h:323
char sc_vendor[16]
Definition: ehci.h:357
bus_size_t sc_io_size
Definition: ehci.h:331
struct resource * sc_irq_res
Definition: ehci.h:325
void * sc_intr_hdl
Definition: ehci.h:330
bus_space_handle_t sc_io_hdl
Definition: ehci.h:333
struct resource * sc_io_res
Definition: ehci.h:324
bus_space_tag_t sc_io_tag
Definition: ehci.h:332
uint16_t sc_flags
Definition: ehci.h:340
struct usb_bus sc_bus
Definition: ehci.h:318
enum usb_revision usbrev
Definition: usb_bus.h:119
device_t bdev
Definition: usb_bus.h:101
device_t parent
Definition: usb_bus.h:100
struct usb_device ** devices
Definition: usb_bus.h:108
uint8_t devices_max
Definition: usb_bus.h:121
uint8_t dma_bits
Definition: usb_bus.h:124
@ USB_REV_2_0
Definition: usb.h:768
#define USB_GET_DMA_TAG(dev)
Definition: usb_busdma.h:46
void usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
uint8_t usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, usb_bus_mem_cb_t *cb)