kernel/ral: Bring in support for further chips (RT2700/2800/3090 series).
[dragonfly.git] / sys / dev / netif / ral / if_ral_pci.c
1 /*      $FreeBSD: src/sys/dev/ral/if_ral_pci.c,v 1.10 2012/05/10 17:41:16 bschmidt Exp $        */
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 #include <dev/netif/ral/rt2860var.h>
57
58 MODULE_DEPEND(ral, pci, 1, 1, 1);
59 MODULE_DEPEND(ral, firmware, 1, 1, 1);
60 MODULE_DEPEND(ral, wlan, 1, 1, 1);
61 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
62
63 struct ral_pci_ident {
64         uint16_t        vendor;
65         uint16_t        device;
66         const char      *name;
67 };
68
69 static const struct ral_pci_ident ral_pci_ids[] = {
70         { PCI_VENDOR_AWT, PCI_PRODUCT_AWT_RT2890,
71                 "Ralink Technology RT2890" },
72         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_1,
73                 "Ralink Technology RT2860" },
74         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_2,
75                 "Ralink Technology RT2860" },
76         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_3,
77                 "Ralink Technology RT2860" },
78         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_4,
79                 "Ralink Technology RT2860" },
80         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_5,
81                 "Ralink Technology RT2860" },
82         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_6,
83                 "Ralink Technology RT2860" },
84         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_7,
85                 "Ralink Technology RT2860" },
86         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_1,
87                 "Ralink Technology RT3591" },
88         { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_2,
89                 "Ralink Technology RT3591" },
90         { PCI_VENDOR_MSI, PCI_PRODUCT_MSI_RT3090,
91                 "Ralink Technology RT3090" },
92         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560,
93                 "Ralink Technology RT2560" },
94         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S,
95                 "Ralink Technology RT2561S" },
96         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561,
97                 "Ralink Technology RT2561" },
98         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661,
99                 "Ralink Technology RT2661" },
100         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760,
101                 "Ralink Technology RT2760" },
102         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790,
103                 "Ralink Technology RT2790" },
104         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860,
105                 "Ralink Technology RT2860" },
106         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2890,
107                 "Ralink Technology RT2890" },
108         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3060,
109                 "Ralink Technology RT3060" },
110         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3062,
111                 "Ralink Technology RT3062" },
112         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3090,
113                 "Ralink Technology RT3090" },
114         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3091,
115                 "Ralink Technology RT3091" },
116         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3092,
117                 "Ralink Technology RT3092" },
118         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3390,
119                 "Ralink Technology RT3390" },
120         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3562,
121                 "Ralink Technology RT3562" },
122         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3592,
123                 "Ralink Technology RT3592" },
124         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3593,
125                 "Ralink Technology RT3593" },
126         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_1,
127                 "Ralink Technology RT5390" },
128         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_2,
129                 "Ralink Technology RT5390" },
130         { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_3,
131                 "Ralink Technology RT5390" },
132
133         { 0, 0, NULL }
134 };
135
136 static struct ral_opns {
137         int     (*attach)(device_t, int);
138         int     (*detach)(void *);
139         void    (*shutdown)(void *);
140         void    (*suspend)(void *);
141         void    (*resume)(void *);
142         void    (*intr)(void *);
143
144 }  ral_rt2560_opns = {
145         rt2560_attach,
146         rt2560_detach,
147         rt2560_stop,
148         rt2560_stop,
149         rt2560_resume,
150         rt2560_intr
151
152 }, ral_rt2661_opns = {
153         rt2661_attach,
154         rt2661_detach,
155         rt2661_shutdown,
156         rt2661_suspend,
157         rt2661_resume,
158         rt2661_intr
159 }, ral_rt2860_opns = {
160         rt2860_attach,
161         rt2860_detach,
162         rt2860_shutdown,
163         rt2860_suspend,
164         rt2860_resume,
165         rt2860_intr
166 };
167
168 struct ral_pci_softc {
169         union {
170                 struct rt2560_softc sc_rt2560;
171                 struct rt2661_softc sc_rt2661;
172                 struct rt2860_softc sc_rt2860;
173         } u;
174
175         struct ral_opns         *sc_opns;
176         int                     irq_rid;
177         int                     mem_rid;
178         struct resource         *irq;
179         struct resource         *mem;
180         void                    *sc_ih;
181 };
182
183 static int ral_pci_probe(device_t);
184 static int ral_pci_attach(device_t);
185 static int ral_pci_detach(device_t);
186 static int ral_pci_shutdown(device_t);
187 static int ral_pci_suspend(device_t);
188 static int ral_pci_resume(device_t);
189
190 static device_method_t ral_pci_methods[] = {
191         /* Device interface */
192         DEVMETHOD(device_probe,         ral_pci_probe),
193         DEVMETHOD(device_attach,        ral_pci_attach),
194         DEVMETHOD(device_detach,        ral_pci_detach),
195         DEVMETHOD(device_shutdown,      ral_pci_shutdown),
196         DEVMETHOD(device_suspend,       ral_pci_suspend),
197         DEVMETHOD(device_resume,        ral_pci_resume),
198
199         DEVMETHOD_END
200 };
201
202 static driver_t ral_pci_driver = {
203         "ral",
204         ral_pci_methods,
205         sizeof (struct ral_pci_softc)
206 };
207
208 static devclass_t ral_devclass;
209
210 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL);
211
212 static int
213 ral_pci_probe(device_t dev)
214 {
215         const struct ral_pci_ident *ident;
216
217         wlan_serialize_enter();
218
219         for (ident = ral_pci_ids; ident->name != NULL; ident++) {
220                 if (pci_get_vendor(dev) == ident->vendor &&
221                     pci_get_device(dev) == ident->device) {
222                         device_set_desc(dev, ident->name);
223                         wlan_serialize_exit();
224                         return 0;
225                 }
226         }
227         wlan_serialize_exit();
228         return ENXIO;
229 }
230
231 /* Base Address Register */
232 #define RAL_PCI_BAR0    0x10
233
234 static int
235 ral_pci_attach(device_t dev)
236 {
237         struct ral_pci_softc *psc = device_get_softc(dev);
238         struct rt2560_softc *sc = &psc->u.sc_rt2560;
239         int error;
240
241         wlan_serialize_enter();
242         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
243                 device_printf(dev, "chip is in D%d power mode "
244                     "-- setting to D0\n", pci_get_powerstate(dev));
245                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
246         }
247
248         /* enable bus-mastering */
249         pci_enable_busmaster(dev);
250
251         switch (pci_get_device(dev)) {
252         case PCI_PRODUCT_RALINK_RT2560:
253                 psc->sc_opns = &ral_rt2560_opns;
254                 break;
255         case PCI_PRODUCT_RALINK_RT2561S:
256         case PCI_PRODUCT_RALINK_RT2561:
257         case PCI_PRODUCT_RALINK_RT2661:
258                 psc->sc_opns = &ral_rt2661_opns;
259                 break;
260         default:
261                 psc->sc_opns = &ral_rt2860_opns;
262                 break;
263         }
264
265         psc->mem_rid = RAL_PCI_BAR0;
266         psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
267             RF_ACTIVE);
268         if (psc->mem == NULL) {
269                 device_printf(dev, "could not allocate memory resource\n");
270                 wlan_serialize_exit();
271                 return ENXIO;
272         }
273
274         sc->sc_st = rman_get_bustag(psc->mem);
275         sc->sc_sh = rman_get_bushandle(psc->mem);
276         sc->sc_invalid = 1;
277         
278         psc->irq_rid = 0;
279         psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
280             RF_ACTIVE | RF_SHAREABLE);
281         if (psc->irq == NULL) {
282                 device_printf(dev, "could not allocate interrupt resource\n");
283                 wlan_serialize_exit();
284                 return ENXIO;
285         }
286
287         error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
288         if (error != 0) {
289                 wlan_serialize_exit();
290                 return error;
291         }
292
293         /*
294          * Hook our interrupt after all initialization is complete.
295          */
296         error = bus_setup_intr(dev, psc->irq, INTR_MPSAFE,
297             psc->sc_opns->intr, psc, &psc->sc_ih, &wlan_global_serializer);
298         if (error != 0) {
299                 device_printf(dev, "could not set up interrupt\n");
300                 wlan_serialize_exit();
301                 return error;
302         }
303         sc->sc_invalid = 0;
304         
305         wlan_serialize_exit();
306         return 0;
307 }
308
309 static int
310 ral_pci_detach(device_t dev)
311 {
312         struct ral_pci_softc *psc = device_get_softc(dev);
313         struct rt2560_softc *sc = &psc->u.sc_rt2560;
314         
315         wlan_serialize_enter();
316         /* check if device was removed */
317         sc->sc_invalid = !bus_child_present(dev);
318         
319         (*psc->sc_opns->detach)(psc);
320
321         bus_generic_detach(dev);
322         bus_teardown_intr(dev, psc->irq, psc->sc_ih);
323         bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
324
325         bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
326
327         wlan_serialize_exit();
328         return 0;
329 }
330
331 static int
332 ral_pci_shutdown(device_t dev)
333 {
334         struct ral_pci_softc *psc = device_get_softc(dev);
335
336         wlan_serialize_enter();
337         (*psc->sc_opns->shutdown)(psc);
338         wlan_serialize_exit();
339
340         return 0;
341 }
342
343 static int
344 ral_pci_suspend(device_t dev)
345 {
346         struct ral_pci_softc *psc = device_get_softc(dev);
347
348         wlan_serialize_enter();
349         (*psc->sc_opns->suspend)(psc);
350         wlan_serialize_exit();
351
352         return 0;
353 }
354
355 static int
356 ral_pci_resume(device_t dev)
357 {
358         struct ral_pci_softc *psc = device_get_softc(dev);
359
360         wlan_serialize_enter();
361         (*psc->sc_opns->resume)(psc);
362         wlan_serialize_exit();
363
364         return 0;
365 }