FreeBSD kernel usb device Code
generic_xhci.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2015 Semihalf.
5 * Copyright (c) 2015 Stormshield.
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 <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/stdint.h>
34#include <sys/stddef.h>
35#include <sys/param.h>
36#include <sys/queue.h>
37#include <sys/types.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/bus.h>
41#include <sys/module.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/condvar.h>
45#include <sys/sysctl.h>
46#include <sys/sx.h>
47#include <sys/unistd.h>
48#include <sys/priv.h>
49#include <sys/rman.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
60#include <dev/usb/usb_bus.h>
63
64#include "generic_xhci.h"
65
66#define IS_DMA_32B 1
67
68int
70{
71 struct xhci_softc *sc = device_get_softc(dev);
72 int err = 0, rid = 0;
73
74 sc->sc_bus.parent = dev;
75 sc->sc_bus.devices = sc->sc_devices;
77
78 sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
79 RF_ACTIVE);
80 if (sc->sc_io_res == NULL) {
81 device_printf(dev, "Failed to map memory\n");
83 return (ENXIO);
84 }
85
86 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
87 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
88 sc->sc_io_size = rman_get_size(sc->sc_io_res);
89
90 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
91 RF_SHAREABLE | RF_ACTIVE);
92 if (sc->sc_irq_res == NULL) {
93 device_printf(dev, "Failed to allocate IRQ\n");
95 return (ENXIO);
96 }
97
98 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
99 if (sc->sc_bus.bdev == NULL) {
100 device_printf(dev, "Failed to add USB device\n");
102 return (ENXIO);
103 }
104
105 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
106
107 sprintf(sc->sc_vendor, XHCI_HC_VENDOR);
108 device_set_desc(sc->sc_bus.bdev, XHCI_HC_DEVSTR);
109
110 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
111 NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
112 if (err != 0) {
113 device_printf(dev, "Failed to setup error IRQ, %d\n", err);
114 sc->sc_intr_hdl = NULL;
116 return (err);
117 }
118
119 err = xhci_init(sc, dev, IS_DMA_32B);
120 if (err != 0) {
121 device_printf(dev, "Failed to init XHCI, with error %d\n", err);
123 return (ENXIO);
124 }
125
126 err = xhci_start_controller(sc);
127 if (err != 0) {
128 device_printf(dev, "Failed to start XHCI controller, with error %d\n", err);
130 return (ENXIO);
131 }
132
133 err = device_probe_and_attach(sc->sc_bus.bdev);
134 if (err != 0) {
135 device_printf(dev, "Failed to initialize USB, with error %d\n", err);
137 return (ENXIO);
138 }
139
140 return (0);
141}
142
143int
145{
146 struct xhci_softc *sc = device_get_softc(dev);
147 int err;
148
149 /* during module unload there are lots of children leftover */
150 device_delete_children(dev);
151
152 if (sc->sc_irq_res != NULL && sc->sc_intr_hdl != NULL) {
153 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
154 if (err != 0)
155 device_printf(dev, "Could not tear down irq, %d\n",
156 err);
157 sc->sc_intr_hdl = NULL;
158 }
159
160 if (sc->sc_irq_res != NULL) {
161 bus_release_resource(dev, SYS_RES_IRQ,
162 rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
163 sc->sc_irq_res = NULL;
164 }
165
166 if (sc->sc_io_res != NULL) {
167 bus_release_resource(dev, SYS_RES_MEMORY,
168 rman_get_rid(sc->sc_io_res), sc->sc_io_res);
169 sc->sc_io_res = NULL;
170 }
171
172 xhci_uninit(sc);
173
174 return (0);
175}
176
177static device_method_t xhci_methods[] = {
178 /* Device interface */
179 DEVMETHOD(device_attach, generic_xhci_attach),
180 DEVMETHOD(device_detach, generic_xhci_detach),
181 DEVMETHOD(device_suspend, bus_generic_suspend),
182 DEVMETHOD(device_resume, bus_generic_resume),
183 DEVMETHOD(device_shutdown, bus_generic_shutdown),
184
185 DEVMETHOD_END
186};
187
189 "xhci",
191 sizeof(struct xhci_softc),
192};
int generic_xhci_detach(device_t dev)
Definition: generic_xhci.c:144
#define IS_DMA_32B
Definition: generic_xhci.c:66
driver_t generic_xhci_driver
Definition: generic_xhci.c:188
__FBSDID("$FreeBSD$")
static device_method_t xhci_methods[]
Definition: generic_xhci.c:177
int generic_xhci_attach(device_t dev)
Definition: generic_xhci.c:69
#define XHCI_HC_DEVSTR
Definition: generic_xhci.h:36
#define XHCI_HC_VENDOR
Definition: generic_xhci.h:37
uint16_t rid
device_t dev
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
char sc_vendor[16]
Definition: xhci.h:565
bus_space_tag_t sc_io_tag
Definition: xhci.h:513
struct usb_bus sc_bus
Definition: xhci.h:493
void * sc_intr_hdl
Definition: xhci.h:511
struct resource * sc_io_res
Definition: xhci.h:507
struct usb_device * sc_devices[XHCI_MAX_DEVICES]
Definition: xhci.h:506
bus_space_handle_t sc_io_hdl
Definition: xhci.h:514
bus_size_t sc_io_size
Definition: xhci.h:512
struct resource * sc_irq_res
Definition: xhci.h:508
usb_error_t xhci_init(struct xhci_softc *sc, device_t self, uint8_t dma32)
Definition: xhci.c:511
void xhci_uninit(struct xhci_softc *sc)
Definition: xhci.c:644
usb_error_t xhci_start_controller(struct xhci_softc *sc)
Definition: xhci.c:304
void xhci_interrupt(struct xhci_softc *sc)
Definition: xhci.c:1603
#define XHCI_MAX_DEVICES
Definition: xhci.h:33