xref: /freebsd/sys/dev/ral/if_ral_pci.c (revision b78ee15e9f04ae15c3e1200df974473167524d17)
1 /*-
2  * Copyright (c) 2005, 2006
3  *	Damien Bergamini <damien.bergamini@free.fr>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
20 
21 /*
22  * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
23  */
24 
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/bus.h>
28 #include <sys/kernel.h>
29 #include <sys/lock.h>
30 #include <sys/malloc.h>
31 #include <sys/module.h>
32 #include <sys/mutex.h>
33 #include <sys/rman.h>
34 #include <sys/socket.h>
35 
36 #include <machine/bus.h>
37 #include <machine/resource.h>
38 
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <net/route.h>
43 
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
46 
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 
50 #include <dev/ral/rt2560var.h>
51 #include <dev/ral/rt2661var.h>
52 #include <dev/ral/rt2860var.h>
53 
54 MODULE_DEPEND(ral, pci, 1, 1, 1);
55 MODULE_DEPEND(ral, firmware, 1, 1, 1);
56 MODULE_DEPEND(ral, wlan, 1, 1, 1);
57 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
58 
59 static int ral_msi_disable;
60 TUNABLE_INT("hw.ral.msi_disable", &ral_msi_disable);
61 
62 struct ral_pci_ident {
63 	uint16_t	vendor;
64 	uint16_t	device;
65 	const char	*name;
66 };
67 
68 static const struct ral_pci_ident ral_pci_ids[] = {
69 	{ 0x1432, 0x7708, "Edimax RT2860" },
70 	{ 0x1432, 0x7711, "Edimax RT3591" },
71 	{ 0x1432, 0x7722, "Edimax RT3591" },
72 	{ 0x1432, 0x7727, "Edimax RT2860" },
73 	{ 0x1432, 0x7728, "Edimax RT2860" },
74 	{ 0x1432, 0x7738, "Edimax RT2860" },
75 	{ 0x1432, 0x7748, "Edimax RT2860" },
76 	{ 0x1432, 0x7758, "Edimax RT2860" },
77 	{ 0x1432, 0x7768, "Edimax RT2860" },
78 	{ 0x1462, 0x891a, "MSI RT3090" },
79 	{ 0x1814, 0x0201, "Ralink Technology RT2560" },
80 	{ 0x1814, 0x0301, "Ralink Technology RT2561S" },
81 	{ 0x1814, 0x0302, "Ralink Technology RT2561" },
82 	{ 0x1814, 0x0401, "Ralink Technology RT2661" },
83 	{ 0x1814, 0x0601, "Ralink Technology RT2860" },
84 	{ 0x1814, 0x0681, "Ralink Technology RT2890" },
85 	{ 0x1814, 0x0701, "Ralink Technology RT2760" },
86 	{ 0x1814, 0x0781, "Ralink Technology RT2790" },
87 	{ 0x1814, 0x3060, "Ralink Technology RT3060" },
88 	{ 0x1814, 0x3062, "Ralink Technology RT3062" },
89 	{ 0x1814, 0x3090, "Ralink Technology RT3090" },
90 	{ 0x1814, 0x3091, "Ralink Technology RT3091" },
91 	{ 0x1814, 0x3092, "Ralink Technology RT3092" },
92 	{ 0x1814, 0x3390, "Ralink Technology RT3390" },
93 	{ 0x1814, 0x3562, "Ralink Technology RT3562" },
94 	{ 0x1814, 0x3592, "Ralink Technology RT3592" },
95 	{ 0x1814, 0x3593, "Ralink Technology RT3593" },
96 	{ 0x1814, 0x5360, "Ralink Technology RT5390" },
97 	{ 0x1814, 0x5362, "Ralink Technology RT5392" },
98 	{ 0x1814, 0x5390, "Ralink Technology RT5390" },
99 	{ 0x1814, 0x5392, "Ralink Technology RT5392" },
100 	{ 0x1814, 0x539a, "Ralink Technology RT5390" },
101 	{ 0x1814, 0x539f, "Ralink Technology RT5390" },
102 	{ 0x1a3b, 0x1059, "AWT RT2890" },
103 	{ 0, 0, NULL }
104 };
105 
106 static const struct ral_opns {
107 	int	(*attach)(device_t, int);
108 	int	(*detach)(void *);
109 	void	(*shutdown)(void *);
110 	void	(*suspend)(void *);
111 	void	(*resume)(void *);
112 	void	(*intr)(void *);
113 
114 }  ral_rt2560_opns = {
115 	rt2560_attach,
116 	rt2560_detach,
117 	rt2560_stop,
118 	rt2560_stop,
119 	rt2560_resume,
120 	rt2560_intr
121 
122 }, ral_rt2661_opns = {
123 	rt2661_attach,
124 	rt2661_detach,
125 	rt2661_shutdown,
126 	rt2661_suspend,
127 	rt2661_resume,
128 	rt2661_intr
129 }, ral_rt2860_opns = {
130 	rt2860_attach,
131 	rt2860_detach,
132 	rt2860_shutdown,
133 	rt2860_suspend,
134 	rt2860_resume,
135 	rt2860_intr
136 };
137 
138 struct ral_pci_softc {
139 	union {
140 		struct rt2560_softc sc_rt2560;
141 		struct rt2661_softc sc_rt2661;
142 		struct rt2860_softc sc_rt2860;
143 	} u;
144 
145 	const struct ral_opns	*sc_opns;
146 	struct resource		*irq;
147 	struct resource		*mem;
148 	void			*sc_ih;
149 };
150 
151 static int ral_pci_probe(device_t);
152 static int ral_pci_attach(device_t);
153 static int ral_pci_detach(device_t);
154 static int ral_pci_shutdown(device_t);
155 static int ral_pci_suspend(device_t);
156 static int ral_pci_resume(device_t);
157 
158 static device_method_t ral_pci_methods[] = {
159 	/* Device interface */
160 	DEVMETHOD(device_probe,		ral_pci_probe),
161 	DEVMETHOD(device_attach,	ral_pci_attach),
162 	DEVMETHOD(device_detach,	ral_pci_detach),
163 	DEVMETHOD(device_shutdown,	ral_pci_shutdown),
164 	DEVMETHOD(device_suspend,	ral_pci_suspend),
165 	DEVMETHOD(device_resume,	ral_pci_resume),
166 
167 	DEVMETHOD_END
168 };
169 
170 static driver_t ral_pci_driver = {
171 	"ral",
172 	ral_pci_methods,
173 	sizeof (struct ral_pci_softc)
174 };
175 
176 static devclass_t ral_devclass;
177 
178 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL);
179 
180 static int
181 ral_pci_probe(device_t dev)
182 {
183 	const struct ral_pci_ident *ident;
184 
185 	for (ident = ral_pci_ids; ident->name != NULL; ident++) {
186 		if (pci_get_vendor(dev) == ident->vendor &&
187 		    pci_get_device(dev) == ident->device) {
188 			device_set_desc(dev, ident->name);
189 			return (BUS_PROBE_DEFAULT);
190 		}
191 	}
192 	return ENXIO;
193 }
194 
195 static int
196 ral_pci_attach(device_t dev)
197 {
198 	struct ral_pci_softc *psc = device_get_softc(dev);
199 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
200 	int count, error, rid;
201 
202 	pci_enable_busmaster(dev);
203 
204 	switch (pci_get_device(dev)) {
205 	case 0x0201:
206 		psc->sc_opns = &ral_rt2560_opns;
207 		break;
208 	case 0x0301:
209 	case 0x0302:
210 	case 0x0401:
211 		psc->sc_opns = &ral_rt2661_opns;
212 		break;
213 	default:
214 		psc->sc_opns = &ral_rt2860_opns;
215 		break;
216 	}
217 
218 	rid = PCIR_BAR(0);
219 	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
220 	    RF_ACTIVE);
221 	if (psc->mem == NULL) {
222 		device_printf(dev, "could not allocate memory resource\n");
223 		return ENXIO;
224 	}
225 
226 	sc->sc_st = rman_get_bustag(psc->mem);
227 	sc->sc_sh = rman_get_bushandle(psc->mem);
228 	sc->sc_invalid = 1;
229 
230 	rid = 0;
231 	if (ral_msi_disable == 0) {
232 		count = 1;
233 		if (pci_alloc_msi(dev, &count) == 0)
234 			rid = 1;
235 	}
236 	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
237 	    (rid != 0 ? 0 : RF_SHAREABLE));
238 	if (psc->irq == NULL) {
239 		device_printf(dev, "could not allocate interrupt resource\n");
240 		pci_release_msi(dev);
241 		bus_release_resource(dev, SYS_RES_MEMORY,
242 		    rman_get_rid(psc->mem), psc->mem);
243 		return ENXIO;
244 	}
245 
246 	error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
247 	if (error != 0) {
248 		(void)ral_pci_detach(dev);
249 		return error;
250 	}
251 
252 	/*
253 	 * Hook our interrupt after all initialization is complete.
254 	 */
255 	error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE,
256 	    NULL, psc->sc_opns->intr, psc, &psc->sc_ih);
257 	if (error != 0) {
258 		device_printf(dev, "could not set up interrupt\n");
259 		(void)ral_pci_detach(dev);
260 		return error;
261 	}
262 	sc->sc_invalid = 0;
263 
264 	return 0;
265 }
266 
267 static int
268 ral_pci_detach(device_t dev)
269 {
270 	struct ral_pci_softc *psc = device_get_softc(dev);
271 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
272 
273 	/* check if device was removed */
274 	sc->sc_invalid = !bus_child_present(dev);
275 
276 	if (psc->sc_ih != NULL)
277 		bus_teardown_intr(dev, psc->irq, psc->sc_ih);
278 	(*psc->sc_opns->detach)(psc);
279 
280 	bus_generic_detach(dev);
281 	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(psc->irq),
282 	    psc->irq);
283 	pci_release_msi(dev);
284 
285 	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(psc->mem),
286 	    psc->mem);
287 
288 	return 0;
289 }
290 
291 static int
292 ral_pci_shutdown(device_t dev)
293 {
294 	struct ral_pci_softc *psc = device_get_softc(dev);
295 
296 	(*psc->sc_opns->shutdown)(psc);
297 
298 	return 0;
299 }
300 
301 static int
302 ral_pci_suspend(device_t dev)
303 {
304 	struct ral_pci_softc *psc = device_get_softc(dev);
305 
306 	(*psc->sc_opns->suspend)(psc);
307 
308 	return 0;
309 }
310 
311 static int
312 ral_pci_resume(device_t dev)
313 {
314 	struct ral_pci_softc *psc = device_get_softc(dev);
315 
316 	(*psc->sc_opns->resume)(psc);
317 
318 	return 0;
319 }
320