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