Deleted Added
full compact
ehci_pci.c (184610) ehci_pci.c (184824)
1/*-
2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (augustss@carlstedt.se) at
7 * Carlstedt Research & Technology.
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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (augustss@carlstedt.se) at
7 * Carlstedt Research & Technology.
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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/dev/usb2/controller/ehci2_pci.c 184610 2008-11-04 02:31:03Z alfred $");
39__FBSDID("$FreeBSD: head/sys/dev/usb2/controller/ehci2_pci.c 184824 2008-11-10 20:54:31Z thompsa $");
40
41/*
42 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
43 *
44 * The EHCI 1.0 spec can be found at
45 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
46 * and the USB 2.0 spec at
47 * http://www.usb.org/developers/docs/usb_20.zip
48 */
49
50/* The low level controller code for EHCI has been split into
51 * PCI probes and EHCI specific code. This was done to facilitate the
52 * sharing of code between *BSD's
53 */
54
55#include <dev/usb2/include/usb2_mfunc.h>
56#include <dev/usb2/include/usb2_defs.h>
57#include <dev/usb2/include/usb2_standard.h>
58
59#include <dev/usb2/core/usb2_core.h>
60#include <dev/usb2/core/usb2_busdma.h>
61#include <dev/usb2/core/usb2_process.h>
62#include <dev/usb2/core/usb2_config_td.h>
63#include <dev/usb2/core/usb2_sw_transfer.h>
64#include <dev/usb2/core/usb2_util.h>
65
66#include <dev/usb2/controller/usb2_controller.h>
67#include <dev/usb2/controller/usb2_bus.h>
68#include <dev/usb2/controller/usb2_pci.h>
69#include <dev/usb2/controller/ehci2.h>
70
71#define PCI_EHCI_VENDORID_ACERLABS 0x10b9
72#define PCI_EHCI_VENDORID_AMD 0x1022
73#define PCI_EHCI_VENDORID_APPLE 0x106b
74#define PCI_EHCI_VENDORID_ATI 0x1002
75#define PCI_EHCI_VENDORID_CMDTECH 0x1095
76#define PCI_EHCI_VENDORID_INTEL 0x8086
77#define PCI_EHCI_VENDORID_NEC 0x1033
78#define PCI_EHCI_VENDORID_OPTI 0x1045
79#define PCI_EHCI_VENDORID_PHILIPS 0x1131
80#define PCI_EHCI_VENDORID_SIS 0x1039
81#define PCI_EHCI_VENDORID_NVIDIA 0x12D2
82#define PCI_EHCI_VENDORID_NVIDIA2 0x10DE
83#define PCI_EHCI_VENDORID_VIA 0x1106
84
85#define PCI_EHCI_BASE_REG 0x10
86
87static void ehci_pci_takecontroller(device_t self);
88
89static device_probe_t ehci_pci_probe;
90static device_attach_t ehci_pci_attach;
91static device_detach_t ehci_pci_detach;
92static device_suspend_t ehci_pci_suspend;
93static device_resume_t ehci_pci_resume;
94static device_shutdown_t ehci_pci_shutdown;
95
96static int
97ehci_pci_suspend(device_t self)
98{
99 ehci_softc_t *sc = device_get_softc(self);
100 int err;
101
102 err = bus_generic_suspend(self);
103 if (err)
104 return (err);
105 ehci_suspend(sc);
106 return (0);
107}
108
109static int
110ehci_pci_resume(device_t self)
111{
112 ehci_softc_t *sc = device_get_softc(self);
113
114 ehci_pci_takecontroller(self);
115 ehci_resume(sc);
116
117 bus_generic_resume(self);
118
119 return (0);
120}
121
122static int
123ehci_pci_shutdown(device_t self)
124{
125 ehci_softc_t *sc = device_get_softc(self);
126 int err;
127
128 err = bus_generic_shutdown(self);
129 if (err)
130 return (err);
131 ehci_shutdown(sc);
132
133 return (0);
134}
135
136static const char *
137ehci_pci_match(device_t self)
138{
139 uint32_t device_id = pci_get_devid(self);
140
141 switch (device_id) {
142 case 0x268c8086:
143 return ("Intel 63XXESB USB 2.0 controller");
144
145 case 0x523910b9:
146 return "ALi M5239 USB 2.0 controller";
147
148 case 0x10227463:
149 return "AMD 8111 USB 2.0 controller";
150
151 case 0x20951022:
152 return ("AMD CS5536 (Geode) USB 2.0 controller");
153
154 case 0x43451002:
155 return "ATI SB200 USB 2.0 controller";
156 case 0x43731002:
157 return "ATI SB400 USB 2.0 controller";
158
159 case 0x25ad8086:
160 return "Intel 6300ESB USB 2.0 controller";
161 case 0x24cd8086:
162 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
163 case 0x24dd8086:
164 return "Intel 82801EB/R (ICH5) USB 2.0 controller";
165 case 0x265c8086:
166 return "Intel 82801FB (ICH6) USB 2.0 controller";
167 case 0x27cc8086:
168 return "Intel 82801GB/R (ICH7) USB 2.0 controller";
169
170 case 0x28368086:
171 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
172 case 0x283a8086:
173 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
174 case 0x293a8086:
175 return "Intel 82801I (ICH9) USB 2.0 controller";
176 case 0x293c8086:
177 return "Intel 82801I (ICH9) USB 2.0 controller";
178
179 case 0x00e01033:
180 return ("NEC uPD 720100 USB 2.0 controller");
181
182 case 0x006810de:
183 return "NVIDIA nForce2 USB 2.0 controller";
184 case 0x008810de:
185 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
186 case 0x00d810de:
187 return "NVIDIA nForce3 USB 2.0 controller";
188 case 0x00e810de:
189 return "NVIDIA nForce3 250 USB 2.0 controller";
190 case 0x005b10de:
191 return "NVIDIA nForce4 USB 2.0 controller";
192
193 case 0x15621131:
194 return "Philips ISP156x USB 2.0 controller";
195
196 case 0x31041106:
197 return ("VIA VT6202 USB 2.0 controller");
198
199 default:
200 break;
201 }
202
203 if ((pci_get_class(self) == PCIC_SERIALBUS)
204 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
205 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
206 return ("EHCI (generic) USB 2.0 controller");
207 }
208 return (NULL); /* dunno */
209}
210
211static int
212ehci_pci_probe(device_t self)
213{
214 const char *desc = ehci_pci_match(self);
215
216 if (desc) {
217 device_set_desc(self, desc);
218 return (0);
219 } else {
220 return (ENXIO);
221 }
222}
223
224static int
225ehci_pci_attach(device_t self)
226{
227 ehci_softc_t *sc = device_get_softc(self);
228 int err;
229 int rid;
230
231 if (sc == NULL) {
232 device_printf(self, "Could not allocate sc\n");
233 return (ENXIO);
234 }
235 /* get all DMA memory */
236
237 if (usb2_bus_mem_alloc_all(&sc->sc_bus,
238 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
239 return ENOMEM;
240 }
241 sc->sc_dev = self;
242
243 pci_enable_busmaster(self);
244
245 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
246 case PCI_USB_REV_PRE_1_0:
247 case PCI_USB_REV_1_0:
248 case PCI_USB_REV_1_1:
249 /*
250 * NOTE: some EHCI USB controllers have the wrong USB
251 * revision number. It appears those controllers are
252 * fully compliant so we just ignore this value in
253 * some common cases.
254 */
255 device_printf(self, "pre-2.0 USB revision (ignored)\n");
256 /* fallthrough */
257 case PCI_USB_REV_2_0:
258 sc->sc_bus.usbrev = USB_REV_2_0;
259 break;
260 default:
261 sc->sc_bus.usbrev = USB_REV_UNKNOWN;
262 break;
263 }
264
265 rid = PCI_CBMEM;
266 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
267 RF_ACTIVE);
268 if (!sc->sc_io_res) {
269 device_printf(self, "Could not map memory\n");
270 goto error;
271 }
272 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
273 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
274 sc->sc_io_size = rman_get_size(sc->sc_io_res);
275
276 rid = 0;
277 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
278 RF_SHAREABLE | RF_ACTIVE);
279 if (sc->sc_irq_res == NULL) {
280 device_printf(self, "Could not allocate irq\n");
281 goto error;
282 }
283 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
284 if (!sc->sc_bus.bdev) {
285 device_printf(self, "Could not add USB device\n");
286 goto error;
287 }
288 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
289
290 /*
291 * ehci_pci_match will never return NULL if ehci_pci_probe
292 * succeeded
293 */
294 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
295 switch (pci_get_vendor(self)) {
296 case PCI_EHCI_VENDORID_ACERLABS:
297 sprintf(sc->sc_vendor, "AcerLabs");
298 break;
299 case PCI_EHCI_VENDORID_AMD:
300 sprintf(sc->sc_vendor, "AMD");
301 break;
302 case PCI_EHCI_VENDORID_APPLE:
303 sprintf(sc->sc_vendor, "Apple");
304 break;
305 case PCI_EHCI_VENDORID_ATI:
306 sprintf(sc->sc_vendor, "ATI");
307 break;
308 case PCI_EHCI_VENDORID_CMDTECH:
309 sprintf(sc->sc_vendor, "CMDTECH");
310 break;
311 case PCI_EHCI_VENDORID_INTEL:
312 sprintf(sc->sc_vendor, "Intel");
313 break;
314 case PCI_EHCI_VENDORID_NEC:
315 sprintf(sc->sc_vendor, "NEC");
316 break;
317 case PCI_EHCI_VENDORID_OPTI:
318 sprintf(sc->sc_vendor, "OPTi");
319 break;
320 case PCI_EHCI_VENDORID_PHILIPS:
321 sprintf(sc->sc_vendor, "Philips");
322 break;
323 case PCI_EHCI_VENDORID_SIS:
324 sprintf(sc->sc_vendor, "SiS");
325 break;
326 case PCI_EHCI_VENDORID_NVIDIA:
327 case PCI_EHCI_VENDORID_NVIDIA2:
328 sprintf(sc->sc_vendor, "nVidia");
329 break;
330 case PCI_EHCI_VENDORID_VIA:
331 sprintf(sc->sc_vendor, "VIA");
332 break;
333 default:
334 if (bootverbose)
335 device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
336 pci_get_devid(self));
337 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
338 }
339
40
41/*
42 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
43 *
44 * The EHCI 1.0 spec can be found at
45 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
46 * and the USB 2.0 spec at
47 * http://www.usb.org/developers/docs/usb_20.zip
48 */
49
50/* The low level controller code for EHCI has been split into
51 * PCI probes and EHCI specific code. This was done to facilitate the
52 * sharing of code between *BSD's
53 */
54
55#include <dev/usb2/include/usb2_mfunc.h>
56#include <dev/usb2/include/usb2_defs.h>
57#include <dev/usb2/include/usb2_standard.h>
58
59#include <dev/usb2/core/usb2_core.h>
60#include <dev/usb2/core/usb2_busdma.h>
61#include <dev/usb2/core/usb2_process.h>
62#include <dev/usb2/core/usb2_config_td.h>
63#include <dev/usb2/core/usb2_sw_transfer.h>
64#include <dev/usb2/core/usb2_util.h>
65
66#include <dev/usb2/controller/usb2_controller.h>
67#include <dev/usb2/controller/usb2_bus.h>
68#include <dev/usb2/controller/usb2_pci.h>
69#include <dev/usb2/controller/ehci2.h>
70
71#define PCI_EHCI_VENDORID_ACERLABS 0x10b9
72#define PCI_EHCI_VENDORID_AMD 0x1022
73#define PCI_EHCI_VENDORID_APPLE 0x106b
74#define PCI_EHCI_VENDORID_ATI 0x1002
75#define PCI_EHCI_VENDORID_CMDTECH 0x1095
76#define PCI_EHCI_VENDORID_INTEL 0x8086
77#define PCI_EHCI_VENDORID_NEC 0x1033
78#define PCI_EHCI_VENDORID_OPTI 0x1045
79#define PCI_EHCI_VENDORID_PHILIPS 0x1131
80#define PCI_EHCI_VENDORID_SIS 0x1039
81#define PCI_EHCI_VENDORID_NVIDIA 0x12D2
82#define PCI_EHCI_VENDORID_NVIDIA2 0x10DE
83#define PCI_EHCI_VENDORID_VIA 0x1106
84
85#define PCI_EHCI_BASE_REG 0x10
86
87static void ehci_pci_takecontroller(device_t self);
88
89static device_probe_t ehci_pci_probe;
90static device_attach_t ehci_pci_attach;
91static device_detach_t ehci_pci_detach;
92static device_suspend_t ehci_pci_suspend;
93static device_resume_t ehci_pci_resume;
94static device_shutdown_t ehci_pci_shutdown;
95
96static int
97ehci_pci_suspend(device_t self)
98{
99 ehci_softc_t *sc = device_get_softc(self);
100 int err;
101
102 err = bus_generic_suspend(self);
103 if (err)
104 return (err);
105 ehci_suspend(sc);
106 return (0);
107}
108
109static int
110ehci_pci_resume(device_t self)
111{
112 ehci_softc_t *sc = device_get_softc(self);
113
114 ehci_pci_takecontroller(self);
115 ehci_resume(sc);
116
117 bus_generic_resume(self);
118
119 return (0);
120}
121
122static int
123ehci_pci_shutdown(device_t self)
124{
125 ehci_softc_t *sc = device_get_softc(self);
126 int err;
127
128 err = bus_generic_shutdown(self);
129 if (err)
130 return (err);
131 ehci_shutdown(sc);
132
133 return (0);
134}
135
136static const char *
137ehci_pci_match(device_t self)
138{
139 uint32_t device_id = pci_get_devid(self);
140
141 switch (device_id) {
142 case 0x268c8086:
143 return ("Intel 63XXESB USB 2.0 controller");
144
145 case 0x523910b9:
146 return "ALi M5239 USB 2.0 controller";
147
148 case 0x10227463:
149 return "AMD 8111 USB 2.0 controller";
150
151 case 0x20951022:
152 return ("AMD CS5536 (Geode) USB 2.0 controller");
153
154 case 0x43451002:
155 return "ATI SB200 USB 2.0 controller";
156 case 0x43731002:
157 return "ATI SB400 USB 2.0 controller";
158
159 case 0x25ad8086:
160 return "Intel 6300ESB USB 2.0 controller";
161 case 0x24cd8086:
162 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
163 case 0x24dd8086:
164 return "Intel 82801EB/R (ICH5) USB 2.0 controller";
165 case 0x265c8086:
166 return "Intel 82801FB (ICH6) USB 2.0 controller";
167 case 0x27cc8086:
168 return "Intel 82801GB/R (ICH7) USB 2.0 controller";
169
170 case 0x28368086:
171 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
172 case 0x283a8086:
173 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
174 case 0x293a8086:
175 return "Intel 82801I (ICH9) USB 2.0 controller";
176 case 0x293c8086:
177 return "Intel 82801I (ICH9) USB 2.0 controller";
178
179 case 0x00e01033:
180 return ("NEC uPD 720100 USB 2.0 controller");
181
182 case 0x006810de:
183 return "NVIDIA nForce2 USB 2.0 controller";
184 case 0x008810de:
185 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
186 case 0x00d810de:
187 return "NVIDIA nForce3 USB 2.0 controller";
188 case 0x00e810de:
189 return "NVIDIA nForce3 250 USB 2.0 controller";
190 case 0x005b10de:
191 return "NVIDIA nForce4 USB 2.0 controller";
192
193 case 0x15621131:
194 return "Philips ISP156x USB 2.0 controller";
195
196 case 0x31041106:
197 return ("VIA VT6202 USB 2.0 controller");
198
199 default:
200 break;
201 }
202
203 if ((pci_get_class(self) == PCIC_SERIALBUS)
204 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
205 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
206 return ("EHCI (generic) USB 2.0 controller");
207 }
208 return (NULL); /* dunno */
209}
210
211static int
212ehci_pci_probe(device_t self)
213{
214 const char *desc = ehci_pci_match(self);
215
216 if (desc) {
217 device_set_desc(self, desc);
218 return (0);
219 } else {
220 return (ENXIO);
221 }
222}
223
224static int
225ehci_pci_attach(device_t self)
226{
227 ehci_softc_t *sc = device_get_softc(self);
228 int err;
229 int rid;
230
231 if (sc == NULL) {
232 device_printf(self, "Could not allocate sc\n");
233 return (ENXIO);
234 }
235 /* get all DMA memory */
236
237 if (usb2_bus_mem_alloc_all(&sc->sc_bus,
238 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
239 return ENOMEM;
240 }
241 sc->sc_dev = self;
242
243 pci_enable_busmaster(self);
244
245 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
246 case PCI_USB_REV_PRE_1_0:
247 case PCI_USB_REV_1_0:
248 case PCI_USB_REV_1_1:
249 /*
250 * NOTE: some EHCI USB controllers have the wrong USB
251 * revision number. It appears those controllers are
252 * fully compliant so we just ignore this value in
253 * some common cases.
254 */
255 device_printf(self, "pre-2.0 USB revision (ignored)\n");
256 /* fallthrough */
257 case PCI_USB_REV_2_0:
258 sc->sc_bus.usbrev = USB_REV_2_0;
259 break;
260 default:
261 sc->sc_bus.usbrev = USB_REV_UNKNOWN;
262 break;
263 }
264
265 rid = PCI_CBMEM;
266 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
267 RF_ACTIVE);
268 if (!sc->sc_io_res) {
269 device_printf(self, "Could not map memory\n");
270 goto error;
271 }
272 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
273 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
274 sc->sc_io_size = rman_get_size(sc->sc_io_res);
275
276 rid = 0;
277 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
278 RF_SHAREABLE | RF_ACTIVE);
279 if (sc->sc_irq_res == NULL) {
280 device_printf(self, "Could not allocate irq\n");
281 goto error;
282 }
283 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
284 if (!sc->sc_bus.bdev) {
285 device_printf(self, "Could not add USB device\n");
286 goto error;
287 }
288 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
289
290 /*
291 * ehci_pci_match will never return NULL if ehci_pci_probe
292 * succeeded
293 */
294 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
295 switch (pci_get_vendor(self)) {
296 case PCI_EHCI_VENDORID_ACERLABS:
297 sprintf(sc->sc_vendor, "AcerLabs");
298 break;
299 case PCI_EHCI_VENDORID_AMD:
300 sprintf(sc->sc_vendor, "AMD");
301 break;
302 case PCI_EHCI_VENDORID_APPLE:
303 sprintf(sc->sc_vendor, "Apple");
304 break;
305 case PCI_EHCI_VENDORID_ATI:
306 sprintf(sc->sc_vendor, "ATI");
307 break;
308 case PCI_EHCI_VENDORID_CMDTECH:
309 sprintf(sc->sc_vendor, "CMDTECH");
310 break;
311 case PCI_EHCI_VENDORID_INTEL:
312 sprintf(sc->sc_vendor, "Intel");
313 break;
314 case PCI_EHCI_VENDORID_NEC:
315 sprintf(sc->sc_vendor, "NEC");
316 break;
317 case PCI_EHCI_VENDORID_OPTI:
318 sprintf(sc->sc_vendor, "OPTi");
319 break;
320 case PCI_EHCI_VENDORID_PHILIPS:
321 sprintf(sc->sc_vendor, "Philips");
322 break;
323 case PCI_EHCI_VENDORID_SIS:
324 sprintf(sc->sc_vendor, "SiS");
325 break;
326 case PCI_EHCI_VENDORID_NVIDIA:
327 case PCI_EHCI_VENDORID_NVIDIA2:
328 sprintf(sc->sc_vendor, "nVidia");
329 break;
330 case PCI_EHCI_VENDORID_VIA:
331 sprintf(sc->sc_vendor, "VIA");
332 break;
333 default:
334 if (bootverbose)
335 device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
336 pci_get_devid(self));
337 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
338 }
339
340 err = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_bus.mtx,
340 err = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_bus.bus_mtx,
341 NULL, 0, 4);
342 if (err) {
343 device_printf(self, "could not setup config thread!\n");
344 goto error;
345 }
346#if (__FreeBSD_version >= 700031)
347 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
348 NULL, (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl);
349#else
350 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
351 (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl);
352#endif
353 if (err) {
354 device_printf(self, "Could not setup irq, %d\n", err);
355 sc->sc_intr_hdl = NULL;
356 goto error;
357 }
358 ehci_pci_takecontroller(self);
359 err = ehci_init(sc);
360 if (!err) {
361 err = device_probe_and_attach(sc->sc_bus.bdev);
362 }
363 if (err) {
364 device_printf(self, "USB init failed err=%d\n", err);
365 goto error;
366 }
367 return (0);
368
369error:
370 ehci_pci_detach(self);
371 return (ENXIO);
372}
373
374static int
375ehci_pci_detach(device_t self)
376{
377 ehci_softc_t *sc = device_get_softc(self);
378 device_t bdev;
379
380 usb2_config_td_drain(&sc->sc_config_td);
381
382 if (sc->sc_bus.bdev) {
383 bdev = sc->sc_bus.bdev;
384 device_detach(bdev);
385 device_delete_child(self, bdev);
386 }
387 /* during module unload there are lots of children leftover */
388 device_delete_all_children(self);
389
390 pci_disable_busmaster(self);
391
392 /*
393 * disable interrupts that might have been switched on in ehci_init
394 */
395 if (sc->sc_io_res) {
396 EWRITE4(sc, EHCI_USBINTR, 0);
397 }
398 if (sc->sc_irq_res && sc->sc_intr_hdl) {
399 /*
400 * only call ehci_detach() after ehci_init()
401 */
402 ehci_detach(sc);
403
404 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
405
406 if (err)
407 /* XXX or should we panic? */
408 device_printf(self, "Could not tear down irq, %d\n",
409 err);
410 sc->sc_intr_hdl = NULL;
411 }
412 if (sc->sc_irq_res) {
413 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
414 sc->sc_irq_res = NULL;
415 }
416 if (sc->sc_io_res) {
417 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
418 sc->sc_io_res);
419 sc->sc_io_res = NULL;
420 }
421 usb2_config_td_unsetup(&sc->sc_config_td);
422
423 usb2_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
424
425 return (0);
426}
427
428static void
429ehci_pci_takecontroller(device_t self)
430{
431 ehci_softc_t *sc = device_get_softc(self);
432 uint32_t cparams;
433 uint32_t eec;
434 uint16_t to;
435 uint8_t eecp;
436 uint8_t bios_sem;
437
438 cparams = EREAD4(sc, EHCI_HCCPARAMS);
439
440 /* Synchronise with the BIOS if it owns the controller. */
441 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
442 eecp = EHCI_EECP_NEXT(eec)) {
443 eec = pci_read_config(self, eecp, 4);
444 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
445 continue;
446 }
447 bios_sem = pci_read_config(self, eecp +
448 EHCI_LEGSUP_BIOS_SEM, 1);
449 if (bios_sem == 0) {
450 continue;
451 }
452 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
453 "to give up control\n");
454 pci_write_config(self, eecp +
455 EHCI_LEGSUP_OS_SEM, 1, 1);
456 to = 500;
457 while (1) {
458 bios_sem = pci_read_config(self, eecp +
459 EHCI_LEGSUP_BIOS_SEM, 1);
460 if (bios_sem == 0)
461 break;
462
463 if (--to == 0) {
464 device_printf(sc->sc_bus.bdev,
465 "timed out waiting for BIOS\n");
466 break;
467 }
468 usb2_pause_mtx(NULL, 10); /* wait 10ms */
469 }
470 }
471 return;
472}
473
474static driver_t ehci_driver =
475{
476 .name = "ehci",
477 .methods = (device_method_t[]){
478 /* device interface */
479 DEVMETHOD(device_probe, ehci_pci_probe),
480 DEVMETHOD(device_attach, ehci_pci_attach),
481 DEVMETHOD(device_detach, ehci_pci_detach),
482 DEVMETHOD(device_suspend, ehci_pci_suspend),
483 DEVMETHOD(device_resume, ehci_pci_resume),
484 DEVMETHOD(device_shutdown, ehci_pci_shutdown),
485 /* bus interface */
486 DEVMETHOD(bus_print_child, bus_generic_print_child),
487
488 {0, 0}
489 },
490 .size = sizeof(struct ehci_softc),
491};
492
493static devclass_t ehci_devclass;
494
495DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
496DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);
497MODULE_DEPEND(ehci, usb2_controller, 1, 1, 1);
498MODULE_DEPEND(ehci, usb2_core, 1, 1, 1);
341 NULL, 0, 4);
342 if (err) {
343 device_printf(self, "could not setup config thread!\n");
344 goto error;
345 }
346#if (__FreeBSD_version >= 700031)
347 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
348 NULL, (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl);
349#else
350 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
351 (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl);
352#endif
353 if (err) {
354 device_printf(self, "Could not setup irq, %d\n", err);
355 sc->sc_intr_hdl = NULL;
356 goto error;
357 }
358 ehci_pci_takecontroller(self);
359 err = ehci_init(sc);
360 if (!err) {
361 err = device_probe_and_attach(sc->sc_bus.bdev);
362 }
363 if (err) {
364 device_printf(self, "USB init failed err=%d\n", err);
365 goto error;
366 }
367 return (0);
368
369error:
370 ehci_pci_detach(self);
371 return (ENXIO);
372}
373
374static int
375ehci_pci_detach(device_t self)
376{
377 ehci_softc_t *sc = device_get_softc(self);
378 device_t bdev;
379
380 usb2_config_td_drain(&sc->sc_config_td);
381
382 if (sc->sc_bus.bdev) {
383 bdev = sc->sc_bus.bdev;
384 device_detach(bdev);
385 device_delete_child(self, bdev);
386 }
387 /* during module unload there are lots of children leftover */
388 device_delete_all_children(self);
389
390 pci_disable_busmaster(self);
391
392 /*
393 * disable interrupts that might have been switched on in ehci_init
394 */
395 if (sc->sc_io_res) {
396 EWRITE4(sc, EHCI_USBINTR, 0);
397 }
398 if (sc->sc_irq_res && sc->sc_intr_hdl) {
399 /*
400 * only call ehci_detach() after ehci_init()
401 */
402 ehci_detach(sc);
403
404 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
405
406 if (err)
407 /* XXX or should we panic? */
408 device_printf(self, "Could not tear down irq, %d\n",
409 err);
410 sc->sc_intr_hdl = NULL;
411 }
412 if (sc->sc_irq_res) {
413 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
414 sc->sc_irq_res = NULL;
415 }
416 if (sc->sc_io_res) {
417 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
418 sc->sc_io_res);
419 sc->sc_io_res = NULL;
420 }
421 usb2_config_td_unsetup(&sc->sc_config_td);
422
423 usb2_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
424
425 return (0);
426}
427
428static void
429ehci_pci_takecontroller(device_t self)
430{
431 ehci_softc_t *sc = device_get_softc(self);
432 uint32_t cparams;
433 uint32_t eec;
434 uint16_t to;
435 uint8_t eecp;
436 uint8_t bios_sem;
437
438 cparams = EREAD4(sc, EHCI_HCCPARAMS);
439
440 /* Synchronise with the BIOS if it owns the controller. */
441 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
442 eecp = EHCI_EECP_NEXT(eec)) {
443 eec = pci_read_config(self, eecp, 4);
444 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
445 continue;
446 }
447 bios_sem = pci_read_config(self, eecp +
448 EHCI_LEGSUP_BIOS_SEM, 1);
449 if (bios_sem == 0) {
450 continue;
451 }
452 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
453 "to give up control\n");
454 pci_write_config(self, eecp +
455 EHCI_LEGSUP_OS_SEM, 1, 1);
456 to = 500;
457 while (1) {
458 bios_sem = pci_read_config(self, eecp +
459 EHCI_LEGSUP_BIOS_SEM, 1);
460 if (bios_sem == 0)
461 break;
462
463 if (--to == 0) {
464 device_printf(sc->sc_bus.bdev,
465 "timed out waiting for BIOS\n");
466 break;
467 }
468 usb2_pause_mtx(NULL, 10); /* wait 10ms */
469 }
470 }
471 return;
472}
473
474static driver_t ehci_driver =
475{
476 .name = "ehci",
477 .methods = (device_method_t[]){
478 /* device interface */
479 DEVMETHOD(device_probe, ehci_pci_probe),
480 DEVMETHOD(device_attach, ehci_pci_attach),
481 DEVMETHOD(device_detach, ehci_pci_detach),
482 DEVMETHOD(device_suspend, ehci_pci_suspend),
483 DEVMETHOD(device_resume, ehci_pci_resume),
484 DEVMETHOD(device_shutdown, ehci_pci_shutdown),
485 /* bus interface */
486 DEVMETHOD(bus_print_child, bus_generic_print_child),
487
488 {0, 0}
489 },
490 .size = sizeof(struct ehci_softc),
491};
492
493static devclass_t ehci_devclass;
494
495DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
496DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);
497MODULE_DEPEND(ehci, usb2_controller, 1, 1, 1);
498MODULE_DEPEND(ehci, usb2_core, 1, 1, 1);