]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ral/if_ral_pci.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / dev / ral / if_ral_pci.c
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 #include <net80211/ieee80211_amrr.h>
54
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57
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, firmware, 1, 1, 1);
63 MODULE_DEPEND(ral, wlan, 1, 1, 1);
64 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
65
66 struct ral_pci_ident {
67         uint16_t        vendor;
68         uint16_t        device;
69         const char      *name;
70 };
71
72 static const struct ral_pci_ident ral_pci_ids[] = {
73         { 0x1814, 0x0201, "Ralink Technology RT2560" },
74         { 0x1814, 0x0301, "Ralink Technology RT2561S" },
75         { 0x1814, 0x0302, "Ralink Technology RT2561" },
76         { 0x1814, 0x0401, "Ralink Technology RT2661" },
77
78         { 0, 0, NULL }
79 };
80
81 static struct ral_opns {
82         int     (*attach)(device_t, int);
83         int     (*detach)(void *);
84         void    (*shutdown)(void *);
85         void    (*suspend)(void *);
86         void    (*resume)(void *);
87         void    (*intr)(void *);
88
89 }  ral_rt2560_opns = {
90         rt2560_attach,
91         rt2560_detach,
92         rt2560_stop,
93         rt2560_stop,
94         rt2560_resume,
95         rt2560_intr
96
97 }, ral_rt2661_opns = {
98         rt2661_attach,
99         rt2661_detach,
100         rt2661_shutdown,
101         rt2661_suspend,
102         rt2661_resume,
103         rt2661_intr
104 };
105
106 struct ral_pci_softc {
107         union {
108                 struct rt2560_softc sc_rt2560;
109                 struct rt2661_softc sc_rt2661;
110         } u;
111
112         struct ral_opns         *sc_opns;
113         int                     irq_rid;
114         int                     mem_rid;
115         struct resource         *irq;
116         struct resource         *mem;
117         void                    *sc_ih;
118 };
119
120 static int ral_pci_probe(device_t);
121 static int ral_pci_attach(device_t);
122 static int ral_pci_detach(device_t);
123 static int ral_pci_shutdown(device_t);
124 static int ral_pci_suspend(device_t);
125 static int ral_pci_resume(device_t);
126
127 static device_method_t ral_pci_methods[] = {
128         /* Device interface */
129         DEVMETHOD(device_probe,         ral_pci_probe),
130         DEVMETHOD(device_attach,        ral_pci_attach),
131         DEVMETHOD(device_detach,        ral_pci_detach),
132         DEVMETHOD(device_shutdown,      ral_pci_shutdown),
133         DEVMETHOD(device_suspend,       ral_pci_suspend),
134         DEVMETHOD(device_resume,        ral_pci_resume),
135
136         { 0, 0 }
137 };
138
139 static driver_t ral_pci_driver = {
140         "ral",
141         ral_pci_methods,
142         sizeof (struct ral_pci_softc)
143 };
144
145 static devclass_t ral_devclass;
146
147 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
148 DRIVER_MODULE(ral, cardbus, ral_pci_driver, ral_devclass, 0, 0);
149
150 static int
151 ral_pci_probe(device_t dev)
152 {
153         const struct ral_pci_ident *ident;
154
155         for (ident = ral_pci_ids; ident->name != NULL; ident++) {
156                 if (pci_get_vendor(dev) == ident->vendor &&
157                     pci_get_device(dev) == ident->device) {
158                         device_set_desc(dev, ident->name);
159                         return 0;
160                 }
161         }
162         return ENXIO;
163 }
164
165 /* Base Address Register */
166 #define RAL_PCI_BAR0    0x10
167
168 static int
169 ral_pci_attach(device_t dev)
170 {
171         struct ral_pci_softc *psc = device_get_softc(dev);
172         struct rt2560_softc *sc = &psc->u.sc_rt2560;
173         int error;
174
175         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
176                 device_printf(dev, "chip is in D%d power mode "
177                     "-- setting to D0\n", pci_get_powerstate(dev));
178                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
179         }
180
181         /* enable bus-mastering */
182         pci_enable_busmaster(dev);
183
184         psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
185             &ral_rt2661_opns;
186
187         psc->mem_rid = RAL_PCI_BAR0;
188         psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
189             RF_ACTIVE);
190         if (psc->mem == NULL) {
191                 device_printf(dev, "could not allocate memory resource\n");
192                 return ENXIO;
193         }
194
195         sc->sc_st = rman_get_bustag(psc->mem);
196         sc->sc_sh = rman_get_bushandle(psc->mem);
197         sc->sc_invalid = 1;
198         
199         psc->irq_rid = 0;
200         psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
201             RF_ACTIVE | RF_SHAREABLE);
202         if (psc->irq == NULL) {
203                 device_printf(dev, "could not allocate interrupt resource\n");
204                 return ENXIO;
205         }
206
207         error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
208         if (error != 0)
209                 return error;
210
211         /*
212          * Hook our interrupt after all initialization is complete.
213          */
214         error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE,
215             NULL, psc->sc_opns->intr, psc, &psc->sc_ih);
216         if (error != 0) {
217                 device_printf(dev, "could not set up interrupt\n");
218                 return error;
219         }
220         sc->sc_invalid = 0;
221         
222         return 0;
223 }
224
225 static int
226 ral_pci_detach(device_t dev)
227 {
228         struct ral_pci_softc *psc = device_get_softc(dev);
229         struct rt2560_softc *sc = &psc->u.sc_rt2560;
230         
231         /* check if device was removed */
232         sc->sc_invalid = !bus_child_present(dev);
233         
234         (*psc->sc_opns->detach)(psc);
235
236         bus_generic_detach(dev);
237         bus_teardown_intr(dev, psc->irq, psc->sc_ih);
238         bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
239
240         bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
241
242         return 0;
243 }
244
245 static int
246 ral_pci_shutdown(device_t dev)
247 {
248         struct ral_pci_softc *psc = device_get_softc(dev);
249
250         (*psc->sc_opns->shutdown)(psc);
251
252         return 0;
253 }
254
255 static int
256 ral_pci_suspend(device_t dev)
257 {
258         struct ral_pci_softc *psc = device_get_softc(dev);
259
260         (*psc->sc_opns->suspend)(psc);
261
262         return 0;
263 }
264
265 static int
266 ral_pci_resume(device_t dev)
267 {
268         struct ral_pci_softc *psc = device_get_softc(dev);
269
270         (*psc->sc_opns->resume)(psc);
271
272         return 0;
273 }