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