xref: /dflybsd-src/sys/dev/netif/ral/if_ral_pci.c (revision 2e7bf158f373428dba2c765c927f14d9e94f00a4)
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 	for (ident = ral_pci_ids; ident->name != NULL; ident++) {
150 		if (pci_get_vendor(dev) == ident->vendor &&
151 		    pci_get_device(dev) == ident->device) {
152 			device_set_desc(dev, ident->name);
153 			return 0;
154 		}
155 	}
156 	return ENXIO;
157 }
158 
159 /* Base Address Register */
160 #define RAL_PCI_BAR0	0x10
161 
162 static int
163 ral_pci_attach(device_t dev)
164 {
165 	struct ral_pci_softc *psc = device_get_softc(dev);
166 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
167 	struct ifnet *ifp = sc->sc_ifp;
168 	int error;
169 
170 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
171 		device_printf(dev, "chip is in D%d power mode "
172 		    "-- setting to D0\n", pci_get_powerstate(dev));
173 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
174 	}
175 
176 	/* enable bus-mastering */
177 	pci_enable_busmaster(dev);
178 
179 	psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
180 	    &ral_rt2661_opns;
181 
182 	psc->mem_rid = RAL_PCI_BAR0;
183 	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
184 	    RF_ACTIVE);
185 	if (psc->mem == NULL) {
186 		device_printf(dev, "could not allocate memory resource\n");
187 		return ENXIO;
188 	}
189 
190 	sc->sc_st = rman_get_bustag(psc->mem);
191 	sc->sc_sh = rman_get_bushandle(psc->mem);
192 	sc->sc_invalid = 1;
193 
194 	psc->irq_rid = 0;
195 	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
196 	    RF_ACTIVE | RF_SHAREABLE);
197 	if (psc->irq == NULL) {
198 		device_printf(dev, "could not allocate interrupt resource\n");
199 		return ENXIO;
200 	}
201 
202 	error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
203 	if (error != 0)
204 		return error;
205 
206 	/*
207 	 * Hook our interrupt after all initialization is complete.
208 	 */
209 	error = bus_setup_intr(dev, psc->irq, INTR_MPSAFE,
210 	    psc->sc_opns->intr, psc, &psc->sc_ih, ifp->if_serializer);
211 	if (error != 0) {
212 		device_printf(dev, "could not set up interrupt\n");
213 		return error;
214 	}
215 	sc->sc_invalid = 0;
216 
217 	return 0;
218 }
219 
220 static int
221 ral_pci_detach(device_t dev)
222 {
223 	struct ral_pci_softc *psc = device_get_softc(dev);
224 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
225 
226 	/* check if device was removed */
227 	sc->sc_invalid = !bus_child_present(dev);
228 
229 	(*psc->sc_opns->detach)(psc);
230 
231 	bus_generic_detach(dev);
232 	bus_teardown_intr(dev, psc->irq, psc->sc_ih);
233 	bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
234 
235 	bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
236 
237 	return 0;
238 }
239 
240 static int
241 ral_pci_shutdown(device_t dev)
242 {
243 	struct ral_pci_softc *psc = device_get_softc(dev);
244 
245 	(*psc->sc_opns->shutdown)(psc);
246 
247 	return 0;
248 }
249 
250 static int
251 ral_pci_suspend(device_t dev)
252 {
253 	struct ral_pci_softc *psc = device_get_softc(dev);
254 
255 	(*psc->sc_opns->suspend)(psc);
256 
257 	return 0;
258 }
259 
260 static int
261 ral_pci_resume(device_t dev)
262 {
263 	struct ral_pci_softc *psc = device_get_softc(dev);
264 
265 	(*psc->sc_opns->resume)(psc);
266 
267 	return 0;
268 }
269