xref: /dragonfly/sys/dev/netif/ral/if_ral_pci.c (revision 92fc8b5c)
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 <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
52 
53 #include <dev/netif/ral/rt2560var.h>
54 #include <dev/netif/ral/rt2661var.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 struct ral_pci_ident {
62 	uint16_t	vendor;
63 	uint16_t	device;
64 	const char	*name;
65 };
66 
67 static const struct ral_pci_ident ral_pci_ids[] = {
68 	{ 0x1814, 0x0201, "Ralink Technology RT2560" },
69 	{ 0x1814, 0x0301, "Ralink Technology RT2561S" },
70 	{ 0x1814, 0x0302, "Ralink Technology RT2561" },
71 	{ 0x1814, 0x0401, "Ralink Technology RT2661" },
72 
73 	{ 0, 0, NULL }
74 };
75 
76 static struct ral_opns {
77 	int	(*attach)(device_t, int);
78 	int	(*detach)(void *);
79 	void	(*shutdown)(void *);
80 	void	(*suspend)(void *);
81 	void	(*resume)(void *);
82 	void	(*intr)(void *);
83 
84 }  ral_rt2560_opns = {
85 	rt2560_attach,
86 	rt2560_detach,
87 	rt2560_stop,
88 	rt2560_stop,
89 	rt2560_resume,
90 	rt2560_intr
91 
92 }, ral_rt2661_opns = {
93 	rt2661_attach,
94 	rt2661_detach,
95 	rt2661_shutdown,
96 	rt2661_suspend,
97 	rt2661_resume,
98 	rt2661_intr
99 };
100 
101 struct ral_pci_softc {
102 	union {
103 		struct rt2560_softc sc_rt2560;
104 		struct rt2661_softc sc_rt2661;
105 	} u;
106 
107 	struct ral_opns		*sc_opns;
108 	int			irq_rid;
109 	int			mem_rid;
110 	struct resource		*irq;
111 	struct resource		*mem;
112 	void			*sc_ih;
113 };
114 
115 static int ral_pci_probe(device_t);
116 static int ral_pci_attach(device_t);
117 static int ral_pci_detach(device_t);
118 static int ral_pci_shutdown(device_t);
119 static int ral_pci_suspend(device_t);
120 static int ral_pci_resume(device_t);
121 
122 static device_method_t ral_pci_methods[] = {
123 	/* Device interface */
124 	DEVMETHOD(device_probe,		ral_pci_probe),
125 	DEVMETHOD(device_attach,	ral_pci_attach),
126 	DEVMETHOD(device_detach,	ral_pci_detach),
127 	DEVMETHOD(device_shutdown,	ral_pci_shutdown),
128 	DEVMETHOD(device_suspend,	ral_pci_suspend),
129 	DEVMETHOD(device_resume,	ral_pci_resume),
130 
131 	{ 0, 0 }
132 };
133 
134 static driver_t ral_pci_driver = {
135 	"ral",
136 	ral_pci_methods,
137 	sizeof (struct ral_pci_softc)
138 };
139 
140 static devclass_t ral_devclass;
141 
142 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
143 
144 static int
145 ral_pci_probe(device_t dev)
146 {
147 	const struct ral_pci_ident *ident;
148 
149 	wlan_serialize_enter();
150 
151 	for (ident = ral_pci_ids; ident->name != NULL; ident++) {
152 		if (pci_get_vendor(dev) == ident->vendor &&
153 		    pci_get_device(dev) == ident->device) {
154 			device_set_desc(dev, ident->name);
155 			wlan_serialize_exit();
156 			return 0;
157 		}
158 	}
159 	wlan_serialize_exit();
160 	return ENXIO;
161 }
162 
163 /* Base Address Register */
164 #define RAL_PCI_BAR0	0x10
165 
166 static int
167 ral_pci_attach(device_t dev)
168 {
169 	struct ral_pci_softc *psc = device_get_softc(dev);
170 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
171 	struct ifnet *ifp;
172 	int error;
173 
174 	wlan_serialize_enter();
175 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
176 		device_printf(dev, "chip is in D%d power mode "
177 		    "-- setting to D0\n", pci_get_powerstate(dev));
178 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
179 	}
180 
181 	/* enable bus-mastering */
182 	pci_enable_busmaster(dev);
183 
184 	psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
185 	    &ral_rt2661_opns;
186 
187 	psc->mem_rid = RAL_PCI_BAR0;
188 	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
189 	    RF_ACTIVE);
190 	if (psc->mem == NULL) {
191 		device_printf(dev, "could not allocate memory resource\n");
192 		wlan_serialize_exit();
193 		return ENXIO;
194 	}
195 
196 	sc->sc_st = rman_get_bustag(psc->mem);
197 	sc->sc_sh = rman_get_bushandle(psc->mem);
198 	sc->sc_invalid = 1;
199 
200 	psc->irq_rid = 0;
201 	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
202 	    RF_ACTIVE | RF_SHAREABLE);
203 	if (psc->irq == NULL) {
204 		device_printf(dev, "could not allocate interrupt resource\n");
205 		wlan_serialize_exit();
206 		return ENXIO;
207 	}
208 
209 	error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
210 	if (error != 0) {
211 		wlan_serialize_exit();
212 		return error;
213 	}
214 	ifp = sc->sc_ifp;
215 
216 	/*
217 	 * Hook our interrupt after all initialization is complete.
218 	 */
219 	error = bus_setup_intr(dev, psc->irq, INTR_MPSAFE,
220 	    psc->sc_opns->intr, psc, &psc->sc_ih, &wlan_global_serializer);
221 	if (error != 0) {
222 		device_printf(dev, "could not set up interrupt\n");
223 		wlan_serialize_exit();
224 		return error;
225 	}
226 	sc->sc_invalid = 0;
227 
228 	wlan_serialize_exit();
229 	return 0;
230 }
231 
232 static int
233 ral_pci_detach(device_t dev)
234 {
235 	struct ral_pci_softc *psc = device_get_softc(dev);
236 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
237 
238 	wlan_serialize_enter();
239 	/* check if device was removed */
240 	sc->sc_invalid = !bus_child_present(dev);
241 
242 	(*psc->sc_opns->detach)(psc);
243 
244 	bus_generic_detach(dev);
245 	bus_teardown_intr(dev, psc->irq, psc->sc_ih);
246 	bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
247 
248 	bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
249 
250 	wlan_serialize_exit();
251 	return 0;
252 }
253 
254 static int
255 ral_pci_shutdown(device_t dev)
256 {
257 	struct ral_pci_softc *psc = device_get_softc(dev);
258 
259 	wlan_serialize_enter();
260 	(*psc->sc_opns->shutdown)(psc);
261 	wlan_serialize_exit();
262 
263 	return 0;
264 }
265 
266 static int
267 ral_pci_suspend(device_t dev)
268 {
269 	struct ral_pci_softc *psc = device_get_softc(dev);
270 
271 	wlan_serialize_enter();
272 	(*psc->sc_opns->suspend)(psc);
273 	wlan_serialize_exit();
274 
275 	return 0;
276 }
277 
278 static int
279 ral_pci_resume(device_t dev)
280 {
281 	struct ral_pci_softc *psc = device_get_softc(dev);
282 
283 	wlan_serialize_enter();
284 	(*psc->sc_opns->resume)(psc);
285 	wlan_serialize_exit();
286 
287 	return 0;
288 }
289