]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/ral/if_ral_pci.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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
149 static int
150 ral_pci_probe(device_t dev)
151 {
152         const struct ral_pci_ident *ident;
153
154         for (ident = ral_pci_ids; ident->name != NULL; ident++) {
155                 if (pci_get_vendor(dev) == ident->vendor &&
156                     pci_get_device(dev) == ident->device) {
157                         device_set_desc(dev, ident->name);
158                         return 0;
159                 }
160         }
161         return ENXIO;
162 }
163
164 /* Base Address Register */
165 #define RAL_PCI_BAR0    0x10
166
167 static int
168 ral_pci_attach(device_t dev)
169 {
170         struct ral_pci_softc *psc = device_get_softc(dev);
171         struct rt2560_softc *sc = &psc->u.sc_rt2560;
172         int error;
173
174         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
175                 device_printf(dev, "chip is in D%d power mode "
176                     "-- setting to D0\n", pci_get_powerstate(dev));
177                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
178         }
179
180         /* enable bus-mastering */
181         pci_enable_busmaster(dev);
182
183         psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
184             &ral_rt2661_opns;
185
186         psc->mem_rid = RAL_PCI_BAR0;
187         psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
188             RF_ACTIVE);
189         if (psc->mem == NULL) {
190                 device_printf(dev, "could not allocate memory resource\n");
191                 return ENXIO;
192         }
193
194         sc->sc_st = rman_get_bustag(psc->mem);
195         sc->sc_sh = rman_get_bushandle(psc->mem);
196         sc->sc_invalid = 1;
197         
198         psc->irq_rid = 0;
199         psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
200             RF_ACTIVE | RF_SHAREABLE);
201         if (psc->irq == NULL) {
202                 device_printf(dev, "could not allocate interrupt resource\n");
203                 return ENXIO;
204         }
205
206         error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
207         if (error != 0)
208                 return error;
209
210         /*
211          * Hook our interrupt after all initialization is complete.
212          */
213         error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE,
214             NULL, psc->sc_opns->intr, psc, &psc->sc_ih);
215         if (error != 0) {
216                 device_printf(dev, "could not set up interrupt\n");
217                 return error;
218         }
219         sc->sc_invalid = 0;
220         
221         return 0;
222 }
223
224 static int
225 ral_pci_detach(device_t dev)
226 {
227         struct ral_pci_softc *psc = device_get_softc(dev);
228         struct rt2560_softc *sc = &psc->u.sc_rt2560;
229         
230         /* check if device was removed */
231         sc->sc_invalid = !bus_child_present(dev);
232         
233         (*psc->sc_opns->detach)(psc);
234
235         bus_generic_detach(dev);
236         bus_teardown_intr(dev, psc->irq, psc->sc_ih);
237         bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
238
239         bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
240
241         return 0;
242 }
243
244 static int
245 ral_pci_shutdown(device_t dev)
246 {
247         struct ral_pci_softc *psc = device_get_softc(dev);
248
249         (*psc->sc_opns->shutdown)(psc);
250
251         return 0;
252 }
253
254 static int
255 ral_pci_suspend(device_t dev)
256 {
257         struct ral_pci_softc *psc = device_get_softc(dev);
258
259         (*psc->sc_opns->suspend)(psc);
260
261         return 0;
262 }
263
264 static int
265 ral_pci_resume(device_t dev)
266 {
267         struct ral_pci_softc *psc = device_get_softc(dev);
268
269         (*psc->sc_opns->resume)(psc);
270
271         return 0;
272 }