1 /* $FreeBSD$ */
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 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22
23 /*
24 * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
25 */
26
27 #include <sys/param.h>
28 #include <sys/sysctl.h>
29 #include <sys/sockio.h>
30 #include <sys/mbuf.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42
43 #include <net/bpf.h>
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 #include <net/ethernet.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
50
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_radiotap.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56
57 #include <dev/ral/if_ralrate.h>
58 #include <dev/ral/rt2560var.h>
59 #include <dev/ral/rt2661var.h>
60
61 MODULE_DEPEND(ral, pci, 1, 1, 1);
62 MODULE_DEPEND(ral, wlan, 1, 1, 1);
63
64 struct ral_pci_ident {
65 uint16_t vendor;
66 uint16_t device;
67 const char *name;
68 };
69
70 static const struct ral_pci_ident ral_pci_ids[] = {
71 { 0x1814, 0x0201, "Ralink Technology RT2560" },
72 { 0x1814, 0x0301, "Ralink Technology RT2561S" },
73 { 0x1814, 0x0302, "Ralink Technology RT2561" },
74 { 0x1814, 0x0401, "Ralink Technology RT2661" },
75
76 { 0, 0, NULL }
77 };
78
79 static struct ral_opns {
80 int (*attach)(device_t, int);
81 int (*detach)(void *);
82 void (*shutdown)(void *);
83 void (*suspend)(void *);
84 void (*resume)(void *);
85 void (*intr)(void *);
86
87 } ral_rt2560_opns = {
88 rt2560_attach,
89 rt2560_detach,
90 rt2560_shutdown,
91 rt2560_suspend,
92 rt2560_resume,
93 rt2560_intr
94
95 }, ral_rt2661_opns = {
96 rt2661_attach,
97 rt2661_detach,
98 rt2661_shutdown,
99 rt2661_suspend,
100 rt2661_resume,
101 rt2661_intr
102 };
103
104 struct ral_pci_softc {
105 union {
106 struct rt2560_softc sc_rt2560;
107 struct rt2661_softc sc_rt2661;
108 } u;
109
110 struct ral_opns *sc_opns;
111 int irq_rid;
112 int mem_rid;
113 struct resource *irq;
114 struct resource *mem;
115 void *sc_ih;
116 };
117
118 static int ral_pci_probe(device_t);
119 static int ral_pci_attach(device_t);
120 static int ral_pci_detach(device_t);
121 static int ral_pci_shutdown(device_t);
122 static int ral_pci_suspend(device_t);
123 static int ral_pci_resume(device_t);
124
125 static device_method_t ral_pci_methods[] = {
126 /* Device interface */
127 DEVMETHOD(device_probe, ral_pci_probe),
128 DEVMETHOD(device_attach, ral_pci_attach),
129 DEVMETHOD(device_detach, ral_pci_detach),
130 DEVMETHOD(device_shutdown, ral_pci_shutdown),
131 DEVMETHOD(device_suspend, ral_pci_suspend),
132 DEVMETHOD(device_resume, ral_pci_resume),
133
134 { 0, 0 }
135 };
136
137 static driver_t ral_pci_driver = {
138 "ral",
139 ral_pci_methods,
140 sizeof (struct ral_pci_softc)
141 };
142
143 static devclass_t ral_devclass;
144
145 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
146 DRIVER_MODULE(ral, cardbus, ral_pci_driver, ral_devclass, 0, 0);
147
148 static int
149 ral_pci_probe(device_t dev)
150 {
151 const struct ral_pci_ident *ident;
152
153 for (ident = ral_pci_ids; ident->name != NULL; ident++) {
154 if (pci_get_vendor(dev) == ident->vendor &&
155 pci_get_device(dev) == ident->device) {
156 device_set_desc(dev, ident->name);
157 return 0;
158 }
159 }
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 int error;
172
173 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
174 device_printf(dev, "chip is in D%d power mode "
175 "-- setting to D0\n", pci_get_powerstate(dev));
176 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
177 }
178
179 /* enable bus-mastering */
180 pci_enable_busmaster(dev);
181
182 psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
183 &ral_rt2661_opns;
184
185 psc->mem_rid = RAL_PCI_BAR0;
186 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
187 RF_ACTIVE);
188 if (psc->mem == NULL) {
189 device_printf(dev, "could not allocate memory resource\n");
190 return ENXIO;
191 }
192
193 sc->sc_st = rman_get_bustag(psc->mem);
194 sc->sc_sh = rman_get_bushandle(psc->mem);
195
196 psc->irq_rid = 0;
197 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
198 RF_ACTIVE | RF_SHAREABLE);
199 if (psc->irq == NULL) {
200 device_printf(dev, "could not allocate interrupt resource\n");
201 return ENXIO;
202 }
203
204 error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
205 if (error != 0)
206 return error;
207
208 /*
209 * Hook our interrupt after all initialization is complete.
210 */
211 error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE,
212 psc->sc_opns->intr, psc, &psc->sc_ih);
213 if (error != 0) {
214 device_printf(dev, "could not set up interrupt\n");
215 return error;
216 }
217
218 return 0;
219 }
220
221 static int
222 ral_pci_detach(device_t dev)
223 {
224 struct ral_pci_softc *psc = device_get_softc(dev);
225
226 (*psc->sc_opns->detach)(psc);
227
228 bus_generic_detach(dev);
229 bus_teardown_intr(dev, psc->irq, psc->sc_ih);
230 bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
231
232 bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
233
234 return 0;
235 }
236
237 static int
238 ral_pci_shutdown(device_t dev)
239 {
240 struct ral_pci_softc *psc = device_get_softc(dev);
241
242 (*psc->sc_opns->shutdown)(psc);
243
244 return 0;
245 }
246
247 static int
248 ral_pci_suspend(device_t dev)
249 {
250 struct ral_pci_softc *psc = device_get_softc(dev);
251
252 (*psc->sc_opns->suspend)(psc);
253
254 return 0;
255 }
256
257 static int
258 ral_pci_resume(device_t dev)
259 {
260 struct ral_pci_softc *psc = device_get_softc(dev);
261
262 (*psc->sc_opns->resume)(psc);
263
264 return 0;
265 }
Cache object: ac50a0ca4035b8602c20cab0c8d3872e
|