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