xref: /dflybsd-src/sys/dev/netif/ral/if_ral_pci.c (revision 99ebfb7c19301cfdf9a5a87d225b64586998d69f)
1 /*	$FreeBSD: head/sys/dev/ral/if_ral_pci.c 189575 2009-03-09 13:23:54Z imp $	*/
2 
3 /*-
4  * Copyright (c) 2005, 2006
5  *	Damien Bergamini <damien.bergamini@free.fr>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
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/sysctl.h>
27 #include <sys/sockio.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/rman.h>
37 
38 #include <net/bpf.h>
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_types.h>
45 
46 #include <netproto/802_11/ieee80211_var.h>
47 #include <netproto/802_11/ieee80211_radiotap.h>
48 #include <netproto/802_11/ieee80211_amrr.h>
49 
50 #include "pcidevs.h"
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pcivar.h>
53 
54 #include <dev/netif/ral/rt2560var.h>
55 #include <dev/netif/ral/rt2661var.h>
56 
57 MODULE_DEPEND(ral, pci, 1, 1, 1);
58 MODULE_DEPEND(ral, firmware, 1, 1, 1);
59 MODULE_DEPEND(ral, wlan, 1, 1, 1);
60 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
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 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560,
70 		"Ralink Technology RT2560" },
71 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S,
72 		"Ralink Technology RT2561S" },
73 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561,
74 		"Ralink Technology RT2561" },
75 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661,
76 		"Ralink Technology RT2661" },
77 
78 	{ 0, 0, NULL }
79 };
80 
81 static struct ral_opns {
82 	int	(*attach)(device_t, int);
83 	int	(*detach)(void *);
84 	void	(*shutdown)(void *);
85 	void	(*suspend)(void *);
86 	void	(*resume)(void *);
87 	void	(*intr)(void *);
88 
89 }  ral_rt2560_opns = {
90 	rt2560_attach,
91 	rt2560_detach,
92 	rt2560_stop,
93 	rt2560_stop,
94 	rt2560_resume,
95 	rt2560_intr
96 
97 }, ral_rt2661_opns = {
98 	rt2661_attach,
99 	rt2661_detach,
100 	rt2661_shutdown,
101 	rt2661_suspend,
102 	rt2661_resume,
103 	rt2661_intr
104 };
105 
106 struct ral_pci_softc {
107 	union {
108 		struct rt2560_softc sc_rt2560;
109 		struct rt2661_softc sc_rt2661;
110 	} u;
111 
112 	struct ral_opns		*sc_opns;
113 	int			irq_rid;
114 	int			mem_rid;
115 	struct resource		*irq;
116 	struct resource		*mem;
117 	void			*sc_ih;
118 };
119 
120 static int ral_pci_probe(device_t);
121 static int ral_pci_attach(device_t);
122 static int ral_pci_detach(device_t);
123 static int ral_pci_shutdown(device_t);
124 static int ral_pci_suspend(device_t);
125 static int ral_pci_resume(device_t);
126 
127 static device_method_t ral_pci_methods[] = {
128 	/* Device interface */
129 	DEVMETHOD(device_probe,		ral_pci_probe),
130 	DEVMETHOD(device_attach,	ral_pci_attach),
131 	DEVMETHOD(device_detach,	ral_pci_detach),
132 	DEVMETHOD(device_shutdown,	ral_pci_shutdown),
133 	DEVMETHOD(device_suspend,	ral_pci_suspend),
134 	DEVMETHOD(device_resume,	ral_pci_resume),
135 
136 	DEVMETHOD_END
137 };
138 
139 static driver_t ral_pci_driver = {
140 	"ral",
141 	ral_pci_methods,
142 	sizeof (struct ral_pci_softc)
143 };
144 
145 static devclass_t ral_devclass;
146 
147 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL);
148 
149 static int
150 ral_pci_probe(device_t dev)
151 {
152 	const struct ral_pci_ident *ident;
153 
154 	wlan_serialize_enter();
155 
156 	for (ident = ral_pci_ids; ident->name != NULL; ident++) {
157 		if (pci_get_vendor(dev) == ident->vendor &&
158 		    pci_get_device(dev) == ident->device) {
159 			device_set_desc(dev, ident->name);
160 			wlan_serialize_exit();
161 			return 0;
162 		}
163 	}
164 	wlan_serialize_exit();
165 	return ENXIO;
166 }
167 
168 /* Base Address Register */
169 #define RAL_PCI_BAR0	0x10
170 
171 static int
172 ral_pci_attach(device_t dev)
173 {
174 	struct ral_pci_softc *psc = device_get_softc(dev);
175 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
176 	int error;
177 
178 	wlan_serialize_enter();
179 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
180 		device_printf(dev, "chip is in D%d power mode "
181 		    "-- setting to D0\n", pci_get_powerstate(dev));
182 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
183 	}
184 
185 	/* enable bus-mastering */
186 	pci_enable_busmaster(dev);
187 
188 	psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
189 	    &ral_rt2661_opns;
190 
191 	psc->mem_rid = RAL_PCI_BAR0;
192 	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
193 	    RF_ACTIVE);
194 	if (psc->mem == NULL) {
195 		device_printf(dev, "could not allocate memory resource\n");
196 		wlan_serialize_exit();
197 		return ENXIO;
198 	}
199 
200 	sc->sc_st = rman_get_bustag(psc->mem);
201 	sc->sc_sh = rman_get_bushandle(psc->mem);
202 	sc->sc_invalid = 1;
203 
204 	psc->irq_rid = 0;
205 	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
206 	    RF_ACTIVE | RF_SHAREABLE);
207 	if (psc->irq == NULL) {
208 		device_printf(dev, "could not allocate interrupt resource\n");
209 		wlan_serialize_exit();
210 		return ENXIO;
211 	}
212 
213 	error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
214 	if (error != 0) {
215 		wlan_serialize_exit();
216 		return error;
217 	}
218 
219 	/*
220 	 * Hook our interrupt after all initialization is complete.
221 	 */
222 	error = bus_setup_intr(dev, psc->irq, INTR_MPSAFE,
223 	    psc->sc_opns->intr, psc, &psc->sc_ih, &wlan_global_serializer);
224 	if (error != 0) {
225 		device_printf(dev, "could not set up interrupt\n");
226 		wlan_serialize_exit();
227 		return error;
228 	}
229 	sc->sc_invalid = 0;
230 
231 	wlan_serialize_exit();
232 	return 0;
233 }
234 
235 static int
236 ral_pci_detach(device_t dev)
237 {
238 	struct ral_pci_softc *psc = device_get_softc(dev);
239 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
240 
241 	wlan_serialize_enter();
242 	/* check if device was removed */
243 	sc->sc_invalid = !bus_child_present(dev);
244 
245 	(*psc->sc_opns->detach)(psc);
246 
247 	bus_generic_detach(dev);
248 	bus_teardown_intr(dev, psc->irq, psc->sc_ih);
249 	bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
250 
251 	bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
252 
253 	wlan_serialize_exit();
254 	return 0;
255 }
256 
257 static int
258 ral_pci_shutdown(device_t dev)
259 {
260 	struct ral_pci_softc *psc = device_get_softc(dev);
261 
262 	wlan_serialize_enter();
263 	(*psc->sc_opns->shutdown)(psc);
264 	wlan_serialize_exit();
265 
266 	return 0;
267 }
268 
269 static int
270 ral_pci_suspend(device_t dev)
271 {
272 	struct ral_pci_softc *psc = device_get_softc(dev);
273 
274 	wlan_serialize_enter();
275 	(*psc->sc_opns->suspend)(psc);
276 	wlan_serialize_exit();
277 
278 	return 0;
279 }
280 
281 static int
282 ral_pci_resume(device_t dev)
283 {
284 	struct ral_pci_softc *psc = device_get_softc(dev);
285 
286 	wlan_serialize_enter();
287 	(*psc->sc_opns->resume)(psc);
288 	wlan_serialize_exit();
289 
290 	return 0;
291 }
292