xref: /freebsd/sys/dev/usb/controller/generic_xhci.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/module.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
43 #include <sys/sx.h>
44 #include <sys/unistd.h>
45 #include <sys/priv.h>
46 #include <sys/rman.h>
47 
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 
51 #include <dev/usb/usb_core.h>
52 #include <dev/usb/usb_busdma.h>
53 #include <dev/usb/usb_process.h>
54 #include <dev/usb/usb_util.h>
55 
56 #include <dev/usb/usb_controller.h>
57 #include <dev/usb/usb_bus.h>
58 #include <dev/usb/controller/xhci.h>
59 #include <dev/usb/controller/xhcireg.h>
60 
61 #include "generic_xhci.h"
62 
63 #if __SIZEOF_LONG__ == 8
64 #define	IS_DMA_32B	0
65 #elif __SIZEOF_LONG__ == 4
66 #define	IS_DMA_32B	1
67 #else
68 #error unsupported long size
69 #endif
70 
71 int
generic_xhci_attach(device_t dev)72 generic_xhci_attach(device_t dev)
73 {
74 	struct xhci_softc *sc = device_get_softc(dev);
75 	int err = 0, rid = 0;
76 
77 	sc->sc_bus.parent = dev;
78 	sc->sc_bus.devices = sc->sc_devices;
79 	sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
80 
81 	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
82 	    RF_ACTIVE);
83 	if (sc->sc_io_res == NULL) {
84 		device_printf(dev, "Failed to map memory\n");
85 		generic_xhci_detach(dev);
86 		return (ENXIO);
87 	}
88 
89 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
90 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
91 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
92 
93 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
94 	    RF_SHAREABLE | RF_ACTIVE);
95 	if (sc->sc_irq_res == NULL) {
96 		device_printf(dev, "Failed to allocate IRQ\n");
97 		generic_xhci_detach(dev);
98 		return (ENXIO);
99 	}
100 
101 	sc->sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY);
102 	if (sc->sc_bus.bdev == NULL) {
103 		device_printf(dev, "Failed to add USB device\n");
104 		generic_xhci_detach(dev);
105 		return (ENXIO);
106 	}
107 
108 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
109 
110 	sprintf(sc->sc_vendor, XHCI_HC_VENDOR);
111 	device_set_desc(sc->sc_bus.bdev, XHCI_HC_DEVSTR);
112 
113 	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
114 	    NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
115 	if (err != 0) {
116 		device_printf(dev, "Failed to setup error IRQ, %d\n", err);
117 		sc->sc_intr_hdl = NULL;
118 		generic_xhci_detach(dev);
119 		return (err);
120 	}
121 
122 	err = xhci_init(sc, dev,
123 	    (sc->sc_quirks & XHCI_QUIRK_DMA_32B) == 0 ? IS_DMA_32B : 1);
124 	if (err != 0) {
125 		device_printf(dev, "Failed to init XHCI, with error %d\n", err);
126 		generic_xhci_detach(dev);
127 		return (ENXIO);
128 	}
129 
130 	err = xhci_start_controller(sc);
131 	if (err != 0) {
132 		device_printf(dev, "Failed to start XHCI controller, with error %d\n", err);
133 		generic_xhci_detach(dev);
134 		return (ENXIO);
135 	}
136 
137 	err = device_probe_and_attach(sc->sc_bus.bdev);
138 	if (err != 0) {
139 		device_printf(dev, "Failed to initialize USB, with error %d\n", err);
140 		generic_xhci_detach(dev);
141 		return (ENXIO);
142 	}
143 
144 	return (0);
145 }
146 
147 int
generic_xhci_detach(device_t dev)148 generic_xhci_detach(device_t dev)
149 {
150 	struct xhci_softc *sc = device_get_softc(dev);
151 	int err;
152 
153 	/* during module unload there are lots of children leftover */
154 	err = bus_generic_detach(dev);
155 	if (err != 0)
156 		return (err);
157 
158 	if (sc->sc_irq_res != NULL && sc->sc_intr_hdl != NULL) {
159 		err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
160 		if (err != 0)
161 			device_printf(dev, "Could not tear down irq, %d\n",
162 			    err);
163 		sc->sc_intr_hdl = NULL;
164 	}
165 
166 	if (sc->sc_irq_res != NULL) {
167 		bus_release_resource(dev, SYS_RES_IRQ,
168 		    rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
169 		sc->sc_irq_res = NULL;
170 	}
171 
172 	if (sc->sc_io_res != NULL) {
173 		bus_release_resource(dev, SYS_RES_MEMORY,
174 		    rman_get_rid(sc->sc_io_res), sc->sc_io_res);
175 		sc->sc_io_res = NULL;
176 	}
177 
178 	xhci_uninit(sc);
179 
180 	return (0);
181 }
182 
183 static device_method_t xhci_methods[] = {
184 	/* Device interface */
185 	DEVMETHOD(device_attach, generic_xhci_attach),
186 	DEVMETHOD(device_detach, generic_xhci_detach),
187 	DEVMETHOD(device_suspend, bus_generic_suspend),
188 	DEVMETHOD(device_resume, bus_generic_resume),
189 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
190 
191 	DEVMETHOD_END
192 };
193 
194 driver_t generic_xhci_driver = {
195 	"xhci",
196 	xhci_methods,
197 	sizeof(struct xhci_softc),
198 };
199