FreeBSD kernel usb device Code
ohci_pci.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3 *
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss@carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36/*
37 * USB Open Host Controller driver.
38 *
39 * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
40 */
41
42/* The low level controller code for OHCI has been split into
43 * PCI probes and OHCI specific code. This was done to facilitate the
44 * sharing of code between *BSD's
45 */
46
47#include <sys/stdint.h>
48#include <sys/stddef.h>
49#include <sys/param.h>
50#include <sys/queue.h>
51#include <sys/types.h>
52#include <sys/systm.h>
53#include <sys/kernel.h>
54#include <sys/bus.h>
55#include <sys/module.h>
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#include <sys/condvar.h>
59#include <sys/sysctl.h>
60#include <sys/sx.h>
61#include <sys/unistd.h>
62#include <sys/callout.h>
63#include <sys/malloc.h>
64#include <sys/priv.h>
65
66#include <dev/usb/usb.h>
67#include <dev/usb/usbdi.h>
68
69#include <dev/usb/usb_core.h>
70#include <dev/usb/usb_busdma.h>
71#include <dev/usb/usb_process.h>
72#include <dev/usb/usb_util.h>
73
75#include <dev/usb/usb_bus.h>
76#include <dev/usb/usb_pci.h>
79#include "usb_if.h"
80
81#define PCI_OHCI_VENDORID_ACERLABS 0x10b9
82#define PCI_OHCI_VENDORID_AMD 0x1022
83#define PCI_OHCI_VENDORID_APPLE 0x106b
84#define PCI_OHCI_VENDORID_ATI 0x1002
85#define PCI_OHCI_VENDORID_CMDTECH 0x1095
86#define PCI_OHCI_VENDORID_HYGON 0x1d94
87#define PCI_OHCI_VENDORID_NEC 0x1033
88#define PCI_OHCI_VENDORID_NVIDIA 0x12D2
89#define PCI_OHCI_VENDORID_NVIDIA2 0x10DE
90#define PCI_OHCI_VENDORID_OPTI 0x1045
91#define PCI_OHCI_VENDORID_SIS 0x1039
92
93#define PCI_OHCI_BASE_REG 0x10
94
95static device_probe_t ohci_pci_probe;
96static device_attach_t ohci_pci_attach;
97static device_detach_t ohci_pci_detach;
98static usb_take_controller_t ohci_pci_take_controller;
99
100static int
102{
103 uint32_t reg;
104 uint32_t int_line;
105
106 if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
107 device_printf(self, "chip is in D%d mode "
108 "-- setting to D0\n", pci_get_powerstate(self));
109 reg = pci_read_config(self, PCI_CBMEM, 4);
110 int_line = pci_read_config(self, PCIR_INTLINE, 4);
111 pci_set_powerstate(self, PCI_POWERSTATE_D0);
112 pci_write_config(self, PCI_CBMEM, reg, 4);
113 pci_write_config(self, PCIR_INTLINE, int_line, 4);
114 }
115 return (0);
116}
117
118static const char *
119ohci_pci_match(device_t self)
120{
121 uint32_t device_id = pci_get_devid(self);
122
123 switch (device_id) {
124 case 0x523710b9:
125 return ("AcerLabs M5237 (Aladdin-V) USB controller");
126
127 case 0x740c1022:
128 return ("AMD-756 USB Controller");
129 case 0x74141022:
130 return ("AMD-766 USB Controller");
131 case 0x78071022:
132 return ("AMD FCH USB Controller");
133
134 case 0x43741002:
135 return "ATI SB400 USB Controller";
136 case 0x43751002:
137 return "ATI SB400 USB Controller";
138 case 0x43971002:
139 return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
140 case 0x43981002:
141 return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
142 case 0x43991002:
143 return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
144
145 case 0x06701095:
146 return ("CMD Tech 670 (USB0670) USB controller");
147
148 case 0x06731095:
149 return ("CMD Tech 673 (USB0673) USB controller");
150
151 case 0xc8611045:
152 return ("OPTi 82C861 (FireLink) USB controller");
153
154 case 0x00351033:
155 return ("NEC uPD 9210 USB controller");
156
157 case 0x00d710de:
158 return ("nVidia nForce3 USB Controller");
159
160 case 0x005a10de:
161 return ("nVidia nForce CK804 USB Controller");
162 case 0x036c10de:
163 return ("nVidia nForce MCP55 USB Controller");
164 case 0x03f110de:
165 return ("nVidia nForce MCP61 USB Controller");
166 case 0x0aa510de:
167 return ("nVidia nForce MCP79 USB Controller");
168 case 0x0aa710de:
169 return ("nVidia nForce MCP79 USB Controller");
170 case 0x0aa810de:
171 return ("nVidia nForce MCP79 USB Controller");
172
173 case 0x70011039:
174 return ("SiS 5571 USB controller");
175
176 case 0x0019106b:
177 return ("Apple KeyLargo USB controller");
178 case 0x003f106b:
179 return ("Apple KeyLargo/Intrepid USB controller");
180
181 default:
182 break;
183 }
184 if ((pci_get_class(self) == PCIC_SERIALBUS) &&
185 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
186 (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
187 return ("OHCI (generic) USB controller");
188 }
189 return (NULL);
190}
191
192static int
193ohci_pci_probe(device_t self)
194{
195 const char *desc = ohci_pci_match(self);
196
197 if (desc) {
198 device_set_desc(self, desc);
199 return (0);
200 } else {
201 return (ENXIO);
202 }
203}
204
205static int
206ohci_pci_attach(device_t self)
207{
208 ohci_softc_t *sc = device_get_softc(self);
209 int rid;
210 int err;
211
212 /* initialise some bus fields */
213 sc->sc_bus.parent = self;
214 sc->sc_bus.devices = sc->sc_devices;
216 sc->sc_bus.dma_bits = 32;
217
218 /* get all DMA memory */
221 return (ENOMEM);
222 }
223 sc->sc_dev = self;
224
225 pci_enable_busmaster(self);
226
227 rid = PCI_CBMEM;
228 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
229 RF_ACTIVE);
230 if (!sc->sc_io_res) {
231 device_printf(self, "Could not map memory\n");
232 goto error;
233 }
234 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
235 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
236 sc->sc_io_size = rman_get_size(sc->sc_io_res);
237
238 rid = 0;
239 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
240 RF_SHAREABLE | RF_ACTIVE);
241 if (sc->sc_irq_res == NULL) {
242 device_printf(self, "Could not allocate irq\n");
243 goto error;
244 }
245 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
246 if (!sc->sc_bus.bdev) {
247 device_printf(self, "Could not add USB device\n");
248 goto error;
249 }
250 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
251
252 /*
253 * ohci_pci_match will never return NULL if ohci_pci_probe
254 * succeeded
255 */
256 device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
257 switch (pci_get_vendor(self)) {
259 sprintf(sc->sc_vendor, "AcerLabs");
260 break;
262 sprintf(sc->sc_vendor, "AMD");
263 break;
265 sprintf(sc->sc_vendor, "Apple");
266 break;
268 sprintf(sc->sc_vendor, "ATI");
269 break;
271 sprintf(sc->sc_vendor, "CMDTECH");
272 break;
274 sprintf(sc->sc_vendor, "Hygon");
275 break;
277 sprintf(sc->sc_vendor, "NEC");
278 break;
281 sprintf(sc->sc_vendor, "nVidia");
282 break;
284 sprintf(sc->sc_vendor, "OPTi");
285 break;
287 sprintf(sc->sc_vendor, "SiS");
288 break;
289 default:
290 if (bootverbose) {
291 device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
292 pci_get_devid(self));
293 }
294 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
295 }
296
297 /* sc->sc_bus.usbrev; set by ohci_init() */
298
299#if (__FreeBSD_version >= 700031)
300 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
301 NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
302#else
303 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
304 (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
305#endif
306 if (err) {
307 device_printf(self, "Could not setup irq, %d\n", err);
308 sc->sc_intr_hdl = NULL;
309 goto error;
310 }
311 err = ohci_init(sc);
312 if (!err) {
313 err = device_probe_and_attach(sc->sc_bus.bdev);
314 }
315 if (err) {
316 device_printf(self, "USB init failed\n");
317 goto error;
318 }
319 return (0);
320
321error:
322 ohci_pci_detach(self);
323 return (ENXIO);
324}
325
326static int
327ohci_pci_detach(device_t self)
328{
329 ohci_softc_t *sc = device_get_softc(self);
330
331 /* during module unload there are lots of children leftover */
332 device_delete_children(self);
333
334 pci_disable_busmaster(self);
335
336 if (sc->sc_irq_res && sc->sc_intr_hdl) {
337 /*
338 * only call ohci_detach() after ohci_init()
339 */
340 ohci_detach(sc);
341
342 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
343
344 if (err) {
345 /* XXX or should we panic? */
346 device_printf(self, "Could not tear down irq, %d\n",
347 err);
348 }
349 sc->sc_intr_hdl = NULL;
350 }
351 if (sc->sc_irq_res) {
352 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
353 sc->sc_irq_res = NULL;
354 }
355 if (sc->sc_io_res) {
356 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
357 sc->sc_io_res);
358 sc->sc_io_res = NULL;
359 }
361
362 return (0);
363}
364
365static device_method_t ohci_pci_methods[] = {
366 /* Device interface */
367 DEVMETHOD(device_probe, ohci_pci_probe),
368 DEVMETHOD(device_attach, ohci_pci_attach),
369 DEVMETHOD(device_detach, ohci_pci_detach),
370 DEVMETHOD(device_suspend, bus_generic_suspend),
371 DEVMETHOD(device_resume, bus_generic_resume),
372 DEVMETHOD(device_shutdown, bus_generic_shutdown),
373 DEVMETHOD(usb_take_controller, ohci_pci_take_controller),
374
375 DEVMETHOD_END
376};
377
378static driver_t ohci_driver = {
379 .name = "ohci",
380 .methods = ohci_pci_methods,
381 .size = sizeof(struct ohci_softc),
382};
383
384static devclass_t ohci_devclass;
385
387MODULE_DEPEND(ohci, usb, 1, 1, 1);
#define PCI_CBMEM
Definition: ehcireg.h:37
uint32_t reg
Definition: if_rum.c:283
struct @109 error
void ohci_interrupt(ohci_softc_t *sc)
Definition: ohci.c:1106
void ohci_detach(struct ohci_softc *sc)
Definition: ohci.c:437
usb_error_t ohci_init(ohci_softc_t *sc)
Definition: ohci.c:323
#define OHCI_MAX_DEVICES
Definition: ohci.h:37
usb_bus_mem_cb_t ohci_iterate_hw_softc
Definition: ohci.h:259
#define PCI_OHCI_VENDORID_APPLE
Definition: ohci_pci.c:83
static driver_t ohci_driver
Definition: ohci_pci.c:378
#define PCI_OHCI_VENDORID_AMD
Definition: ohci_pci.c:82
#define PCI_OHCI_VENDORID_NVIDIA
Definition: ohci_pci.c:88
static device_detach_t ohci_pci_detach
Definition: ohci_pci.c:97
#define PCI_OHCI_VENDORID_HYGON
Definition: ohci_pci.c:86
DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0)
static usb_take_controller_t ohci_pci_take_controller
Definition: ohci_pci.c:98
MODULE_DEPEND(ohci, usb, 1, 1, 1)
#define PCI_OHCI_VENDORID_NVIDIA2
Definition: ohci_pci.c:89
static device_method_t ohci_pci_methods[]
Definition: ohci_pci.c:365
#define PCI_OHCI_VENDORID_SIS
Definition: ohci_pci.c:91
#define PCI_OHCI_VENDORID_ATI
Definition: ohci_pci.c:84
#define PCI_OHCI_VENDORID_OPTI
Definition: ohci_pci.c:90
static device_probe_t ohci_pci_probe
Definition: ohci_pci.c:95
static const char * ohci_pci_match(device_t self)
Definition: ohci_pci.c:119
__FBSDID("$FreeBSD$")
#define PCI_OHCI_VENDORID_ACERLABS
Definition: ohci_pci.c:81
static devclass_t ohci_devclass
Definition: ohci_pci.c:384
#define PCI_OHCI_VENDORID_NEC
Definition: ohci_pci.c:87
#define PCI_OHCI_VENDORID_CMDTECH
Definition: ohci_pci.c:85
static device_attach_t ohci_pci_attach
Definition: ohci_pci.c:96
#define PCI_INTERFACE_OHCI
Definition: ohcireg.h:39
uint16_t rid
#define PCIR_INTLINE
#define PCIS_SERIALBUS_USB
#define PCIC_SERIALBUS
bus_space_tag_t sc_io_tag
Definition: ohci.h:242
struct usb_bus sc_bus
Definition: ohci.h:227
struct usb_device * sc_devices[OHCI_MAX_DEVICES]
Definition: ohci.h:231
struct resource * sc_irq_res
Definition: ohci.h:233
bus_size_t sc_io_size
Definition: ohci.h:241
struct resource * sc_io_res
Definition: ohci.h:232
device_t sc_dev
Definition: ohci.h:240
char sc_vendor[16]
Definition: ohci.h:255
bus_space_handle_t sc_io_hdl
Definition: ohci.h:243
void * sc_intr_hdl
Definition: ohci.h:239
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)
struct usb_endpoint_descriptor desc
Definition: usb_device.h:0
INTERFACE usb
Definition: usb_if.m:35