xref: /freebsd/sys/dev/ral/if_ral_pci.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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/mbuf.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
34 #include <sys/rman.h>
35 #include <sys/socket.h>
36 
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 
40 #include <net/ethernet.h>
41 #include <net/if.h>
42 #include <net/if_media.h>
43 #include <net/route.h>
44 
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_radiotap.h>
47 #include <net80211/ieee80211_ratectl.h>
48 
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 
52 #include <dev/ral/rt2560var.h>
53 #include <dev/ral/rt2661var.h>
54 #include <dev/ral/rt2860var.h>
55 
56 MODULE_DEPEND(ral, pci, 1, 1, 1);
57 MODULE_DEPEND(ral, firmware, 1, 1, 1);
58 MODULE_DEPEND(ral, wlan, 1, 1, 1);
59 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
60 
61 static int ral_msi_disable;
62 TUNABLE_INT("hw.ral.msi_disable", &ral_msi_disable);
63 
64 struct ral_pci_ident {
65 	uint16_t	vendor;
66 	uint16_t	device;
67 	const char	*name;
68 };
69 
70 static const struct ral_pci_ident ral_pci_ids[] = {
71 	{ 0x1432, 0x7708, "Edimax RT2860" },
72 	{ 0x1432, 0x7711, "Edimax RT3591" },
73 	{ 0x1432, 0x7722, "Edimax RT3591" },
74 	{ 0x1432, 0x7727, "Edimax RT2860" },
75 	{ 0x1432, 0x7728, "Edimax RT2860" },
76 	{ 0x1432, 0x7738, "Edimax RT2860" },
77 	{ 0x1432, 0x7748, "Edimax RT2860" },
78 	{ 0x1432, 0x7758, "Edimax RT2860" },
79 	{ 0x1432, 0x7768, "Edimax RT2860" },
80 	{ 0x1462, 0x891a, "MSI RT3090" },
81 	{ 0x1814, 0x0201, "Ralink Technology RT2560" },
82 	{ 0x1814, 0x0301, "Ralink Technology RT2561S" },
83 	{ 0x1814, 0x0302, "Ralink Technology RT2561" },
84 	{ 0x1814, 0x0401, "Ralink Technology RT2661" },
85 	{ 0x1814, 0x0601, "Ralink Technology RT2860" },
86 	{ 0x1814, 0x0681, "Ralink Technology RT2890" },
87 	{ 0x1814, 0x0701, "Ralink Technology RT2760" },
88 	{ 0x1814, 0x0781, "Ralink Technology RT2790" },
89 	{ 0x1814, 0x3060, "Ralink Technology RT3060" },
90 	{ 0x1814, 0x3062, "Ralink Technology RT3062" },
91 	{ 0x1814, 0x3090, "Ralink Technology RT3090" },
92 	{ 0x1814, 0x3091, "Ralink Technology RT3091" },
93 	{ 0x1814, 0x3092, "Ralink Technology RT3092" },
94 	{ 0x1814, 0x3390, "Ralink Technology RT3390" },
95 	{ 0x1814, 0x3562, "Ralink Technology RT3562" },
96 	{ 0x1814, 0x3592, "Ralink Technology RT3592" },
97 	{ 0x1814, 0x3593, "Ralink Technology RT3593" },
98 	{ 0x1814, 0x5360, "Ralink Technology RT5390" },
99 	{ 0x1814, 0x5362, "Ralink Technology RT5392" },
100 	{ 0x1814, 0x5390, "Ralink Technology RT5390" },
101 	{ 0x1814, 0x5392, "Ralink Technology RT5392" },
102 	{ 0x1814, 0x539a, "Ralink Technology RT5390" },
103 	{ 0x1814, 0x539b, "Ralink Technology RT5390" },
104 	{ 0x1814, 0x539f, "Ralink Technology RT5390" },
105 	{ 0x1a3b, 0x1059, "AWT RT2890" },
106 	{ 0, 0, NULL }
107 };
108 
109 static const struct ral_opns {
110 	int	(*attach)(device_t, int);
111 	int	(*detach)(void *);
112 	void	(*shutdown)(void *);
113 	void	(*suspend)(void *);
114 	void	(*resume)(void *);
115 	void	(*intr)(void *);
116 
117 }  ral_rt2560_opns = {
118 	rt2560_attach,
119 	rt2560_detach,
120 	rt2560_stop,
121 	rt2560_stop,
122 	rt2560_resume,
123 	rt2560_intr
124 
125 }, ral_rt2661_opns = {
126 	rt2661_attach,
127 	rt2661_detach,
128 	rt2661_shutdown,
129 	rt2661_suspend,
130 	rt2661_resume,
131 	rt2661_intr
132 }, ral_rt2860_opns = {
133 	rt2860_attach,
134 	rt2860_detach,
135 	rt2860_shutdown,
136 	rt2860_suspend,
137 	rt2860_resume,
138 	rt2860_intr
139 };
140 
141 struct ral_pci_softc {
142 	union {
143 		struct rt2560_softc sc_rt2560;
144 		struct rt2661_softc sc_rt2661;
145 		struct rt2860_softc sc_rt2860;
146 	} u;
147 
148 	const struct ral_opns	*sc_opns;
149 	struct resource		*irq;
150 	struct resource		*mem;
151 	void			*sc_ih;
152 };
153 
154 static int ral_pci_probe(device_t);
155 static int ral_pci_attach(device_t);
156 static int ral_pci_detach(device_t);
157 static int ral_pci_shutdown(device_t);
158 static int ral_pci_suspend(device_t);
159 static int ral_pci_resume(device_t);
160 
161 static device_method_t ral_pci_methods[] = {
162 	/* Device interface */
163 	DEVMETHOD(device_probe,		ral_pci_probe),
164 	DEVMETHOD(device_attach,	ral_pci_attach),
165 	DEVMETHOD(device_detach,	ral_pci_detach),
166 	DEVMETHOD(device_shutdown,	ral_pci_shutdown),
167 	DEVMETHOD(device_suspend,	ral_pci_suspend),
168 	DEVMETHOD(device_resume,	ral_pci_resume),
169 
170 	DEVMETHOD_END
171 };
172 
173 static driver_t ral_pci_driver = {
174 	"ral",
175 	ral_pci_methods,
176 	sizeof (struct ral_pci_softc)
177 };
178 
179 static devclass_t ral_devclass;
180 
181 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL);
182 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, ral, ral_pci_ids,
183     nitems(ral_pci_ids) - 1);
184 
185 static int
186 ral_pci_probe(device_t dev)
187 {
188 	const struct ral_pci_ident *ident;
189 
190 	for (ident = ral_pci_ids; ident->name != NULL; ident++) {
191 		if (pci_get_vendor(dev) == ident->vendor &&
192 		    pci_get_device(dev) == ident->device) {
193 			device_set_desc(dev, ident->name);
194 			return (BUS_PROBE_DEFAULT);
195 		}
196 	}
197 	return ENXIO;
198 }
199 
200 static int
201 ral_pci_attach(device_t dev)
202 {
203 	struct ral_pci_softc *psc = device_get_softc(dev);
204 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
205 	int count, error, rid;
206 
207 	pci_enable_busmaster(dev);
208 
209 	switch (pci_get_device(dev)) {
210 	case 0x0201:
211 		psc->sc_opns = &ral_rt2560_opns;
212 		break;
213 	case 0x0301:
214 	case 0x0302:
215 	case 0x0401:
216 		psc->sc_opns = &ral_rt2661_opns;
217 		break;
218 	default:
219 		psc->sc_opns = &ral_rt2860_opns;
220 		break;
221 	}
222 
223 	rid = PCIR_BAR(0);
224 	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
225 	    RF_ACTIVE);
226 	if (psc->mem == NULL) {
227 		device_printf(dev, "could not allocate memory resource\n");
228 		return ENXIO;
229 	}
230 
231 	sc->sc_st = rman_get_bustag(psc->mem);
232 	sc->sc_sh = rman_get_bushandle(psc->mem);
233 	sc->sc_invalid = 1;
234 
235 	rid = 0;
236 	if (ral_msi_disable == 0) {
237 		count = 1;
238 		if (pci_alloc_msi(dev, &count) == 0)
239 			rid = 1;
240 	}
241 	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
242 	    (rid != 0 ? 0 : RF_SHAREABLE));
243 	if (psc->irq == NULL) {
244 		device_printf(dev, "could not allocate interrupt resource\n");
245 		pci_release_msi(dev);
246 		bus_release_resource(dev, SYS_RES_MEMORY,
247 		    rman_get_rid(psc->mem), psc->mem);
248 		return ENXIO;
249 	}
250 
251 	error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
252 	if (error != 0) {
253 		(void)ral_pci_detach(dev);
254 		return error;
255 	}
256 
257 	/*
258 	 * Hook our interrupt after all initialization is complete.
259 	 */
260 	error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE,
261 	    NULL, psc->sc_opns->intr, psc, &psc->sc_ih);
262 	if (error != 0) {
263 		device_printf(dev, "could not set up interrupt\n");
264 		(void)ral_pci_detach(dev);
265 		return error;
266 	}
267 	sc->sc_invalid = 0;
268 
269 	return 0;
270 }
271 
272 static int
273 ral_pci_detach(device_t dev)
274 {
275 	struct ral_pci_softc *psc = device_get_softc(dev);
276 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
277 
278 	/* check if device was removed */
279 	sc->sc_invalid = !bus_child_present(dev);
280 
281 	if (psc->sc_ih != NULL)
282 		bus_teardown_intr(dev, psc->irq, psc->sc_ih);
283 	(*psc->sc_opns->detach)(psc);
284 
285 	bus_generic_detach(dev);
286 	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(psc->irq),
287 	    psc->irq);
288 	pci_release_msi(dev);
289 
290 	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(psc->mem),
291 	    psc->mem);
292 
293 	return 0;
294 }
295 
296 static int
297 ral_pci_shutdown(device_t dev)
298 {
299 	struct ral_pci_softc *psc = device_get_softc(dev);
300 
301 	(*psc->sc_opns->shutdown)(psc);
302 
303 	return 0;
304 }
305 
306 static int
307 ral_pci_suspend(device_t dev)
308 {
309 	struct ral_pci_softc *psc = device_get_softc(dev);
310 
311 	(*psc->sc_opns->suspend)(psc);
312 
313 	return 0;
314 }
315 
316 static int
317 ral_pci_resume(device_t dev)
318 {
319 	struct ral_pci_softc *psc = device_get_softc(dev);
320 
321 	(*psc->sc_opns->resume)(psc);
322 
323 	return 0;
324 }
325