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