FreeBSD kernel usb device Code
ehci_msm.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2018 Ruslan Bukin <br@bsdpad.com>
5 * All rights reserved.
6 *
7 * This software was developed by BAE Systems, the University of Cambridge
8 * Computer Laboratory, and Memorial University under DARPA/AFRL contract
9 * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
10 * (TC) research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include "opt_bus.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/bus.h>
42#include <sys/rman.h>
43#include <sys/condvar.h>
44#include <sys/kernel.h>
45#include <sys/module.h>
46
47#include <dev/ofw/ofw_bus.h>
48#include <dev/ofw/ofw_bus_subr.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
65 struct resource *res[3];
66};
67
68static struct resource_spec ehci_msm_spec[] = {
69 { SYS_RES_MEMORY, 0, RF_ACTIVE },
70 { SYS_RES_MEMORY, 1, RF_ACTIVE },
71 { SYS_RES_IRQ, 0, RF_ACTIVE },
72 { -1, 0 }
73};
74
75#define EHCI_HC_DEVSTR "Qualcomm USB 2.0 controller"
76
77static device_attach_t ehci_msm_attach;
78static device_detach_t ehci_msm_detach;
79
80static int
81ehci_msm_probe(device_t dev)
82{
83
84 if (!ofw_bus_is_compatible(dev, "qcom,ci-hdrc"))
85 return (ENXIO);
86
87 device_set_desc(dev, EHCI_HC_DEVSTR);
88
89 return (BUS_PROBE_DEFAULT);
90}
91
92static int
93ehci_msm_attach(device_t dev)
94{
95 struct ehci_msm_softc *esc;
96 bus_space_handle_t bsh;
97 ehci_softc_t *sc;
98 int err;
99
100 esc = device_get_softc(dev);
101 sc = &esc->base;
102 sc->sc_bus.parent = dev;
103 sc->sc_bus.devices = sc->sc_devices;
105 sc->sc_bus.dma_bits = 32;
106
107 if (bus_alloc_resources(dev, ehci_msm_spec, esc->res)) {
108 device_printf(dev, "could not allocate resources\n");
109 return (ENXIO);
110 }
111
112 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
113
114 /* Get all DMA memory */
117 return (ENOMEM);
118 }
119
120 /* EHCI registers */
121 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
122 bsh = rman_get_bushandle(esc->res[0]);
123 sc->sc_io_size = rman_get_size(esc->res[0]);
124
125 if (bus_space_subregion(sc->sc_io_tag, bsh, 0x100,
126 sc->sc_io_size, &sc->sc_io_hdl) != 0)
127 panic("%s: unable to subregion USB host registers",
128 device_get_name(dev));
129
130 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
131 if (!sc->sc_bus.bdev) {
132 device_printf(dev, "Could not add USB device\n");
133 goto error;
134 }
135 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
136 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
137
138 sprintf(sc->sc_vendor, "Qualcomm");
139
140 err = bus_setup_intr(dev, esc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
141 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
142 if (err) {
143 device_printf(dev, "Could not setup irq, %d\n", err);
144 sc->sc_intr_hdl = NULL;
145 goto error;
146 }
147
149
150 err = ehci_init(sc);
151 if (!err) {
153 err = device_probe_and_attach(sc->sc_bus.bdev);
154 }
155
156 if (err) {
157 device_printf(dev, "USB init failed err=%d\n", err);
158 goto error;
159 }
160 return (0);
161
162error:
164 return (ENXIO);
165}
166
167static int
168ehci_msm_detach(device_t dev)
169{
170 ehci_softc_t *sc;
171 device_t bdev;
172 int err;
173
174 sc = device_get_softc(dev);
175
176 if (sc->sc_bus.bdev) {
177 bdev = sc->sc_bus.bdev;
178 device_detach(bdev);
179 device_delete_child(dev, bdev);
180 }
181
182 device_delete_children(dev);
183
184 if (sc->sc_irq_res && sc->sc_intr_hdl) {
185 /* only call ehci_detach() after ehci_init() */
186 ehci_detach(sc);
187
188 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
189 if (err)
190 device_printf(dev, "Could not tear down irq, %d\n",
191 err);
192 sc->sc_intr_hdl = NULL;
193 }
194
195 if (sc->sc_irq_res) {
196 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
197 sc->sc_irq_res = NULL;
198 }
199 if (sc->sc_io_res) {
200 bus_release_resource(dev, SYS_RES_MEMORY, 0,
201 sc->sc_io_res);
202 sc->sc_io_res = NULL;
203 }
204
206
207 return (0);
208}
209
210static device_method_t ehci_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, ehci_msm_probe),
213 DEVMETHOD(device_attach, ehci_msm_attach),
214 DEVMETHOD(device_detach, ehci_msm_detach),
215 DEVMETHOD(device_suspend, bus_generic_suspend),
216 DEVMETHOD(device_resume, bus_generic_resume),
217 DEVMETHOD(device_shutdown, bus_generic_shutdown),
218 DEVMETHOD_END
219};
220
221static driver_t ehci_driver = {
222 .name = "ehci",
223 .methods = ehci_methods,
224 .size = sizeof(ehci_softc_t),
225};
226
227static devclass_t ehci_devclass;
228
229DRIVER_MODULE(ehci_msm, simplebus, ehci_driver, ehci_devclass, 0, 0);
230MODULE_DEPEND(ehci_msm, usb, 1, 1, 1);
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_DONEINIT
Definition: ehci.h:347
#define EHCI_SCFLG_DONTRESET
Definition: ehci.h:346
#define EHCI_SCFLG_NORESTERM
Definition: ehci.h:341
usb_bus_mem_cb_t ehci_iterate_hw_softc
Definition: ehci.h:445
#define EHCI_MAX_DEVICES
Definition: ehci.h:36
static struct resource_spec ehci_msm_spec[]
Definition: ehci_msm.c:68
__FBSDID("$FreeBSD$")
MODULE_DEPEND(ehci_msm, usb, 1, 1, 1)
static int ehci_msm_probe(device_t dev)
Definition: ehci_msm.c:81
DRIVER_MODULE(ehci_msm, simplebus, ehci_driver, ehci_devclass, 0, 0)
static devclass_t ehci_devclass
Definition: ehci_msm.c:227
static device_detach_t ehci_msm_detach
Definition: ehci_msm.c:78
static device_method_t ehci_methods[]
Definition: ehci_msm.c:210
static device_attach_t ehci_msm_attach
Definition: ehci_msm.c:77
#define EHCI_HC_DEVSTR
Definition: ehci_msm.c:75
static driver_t ehci_driver
Definition: ehci_msm.c:221
struct @109 error
device_t dev
ehci_softc_t base
Definition: ehci_msm.c:64
struct resource * res[3]
Definition: ehci_msm.c:65
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
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
#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)
INTERFACE usb
Definition: usb_if.m:35