2 * Copyright (c) 2005, 2006
3 * Damien Bergamini <damien.bergamini@free.fr>
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.
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.
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
22 * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
25 #include <sys/param.h>
26 #include <sys/systm.h>
28 #include <sys/kernel.h>
30 #include <sys/malloc.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
35 #include <sys/socket.h>
37 #if defined(__DragonFly__)
40 #include <machine/bus.h>
41 #include <machine/resource.h>
44 #include <net/ethernet.h>
46 #include <net/if_media.h>
47 #include <net/route.h>
49 #include <netproto/802_11/ieee80211_var.h>
50 #include <netproto/802_11/ieee80211_radiotap.h>
52 #include <bus/pci/pcireg.h>
53 #include <bus/pci/pcivar.h>
55 #include <dev/netif/ral/rt2560var.h>
56 #include <dev/netif/ral/rt2661var.h>
57 #include <dev/netif/ral/rt2860var.h>
59 MODULE_DEPEND(ral, pci, 1, 1, 1);
60 MODULE_DEPEND(ral, firmware, 1, 1, 1);
61 MODULE_DEPEND(ral, wlan, 1, 1, 1);
62 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
64 static int ral_msi_disable;
65 TUNABLE_INT("hw.ral.msi_disable", &ral_msi_disable);
67 struct ral_pci_ident {
73 static const struct ral_pci_ident ral_pci_ids[] = {
74 { 0x1432, 0x7708, "Edimax RT2860" },
75 { 0x1432, 0x7711, "Edimax RT3591" },
76 { 0x1432, 0x7722, "Edimax RT3591" },
77 { 0x1432, 0x7727, "Edimax RT2860" },
78 { 0x1432, 0x7728, "Edimax RT2860" },
79 { 0x1432, 0x7738, "Edimax RT2860" },
80 { 0x1432, 0x7748, "Edimax RT2860" },
81 { 0x1432, 0x7758, "Edimax RT2860" },
82 { 0x1432, 0x7768, "Edimax RT2860" },
83 { 0x1462, 0x891a, "MSI RT3090" },
84 { 0x1814, 0x0201, "Ralink Technology RT2560" },
85 { 0x1814, 0x0301, "Ralink Technology RT2561S" },
86 { 0x1814, 0x0302, "Ralink Technology RT2561" },
87 { 0x1814, 0x0401, "Ralink Technology RT2661" },
88 { 0x1814, 0x0601, "Ralink Technology RT2860" },
89 { 0x1814, 0x0681, "Ralink Technology RT2890" },
90 { 0x1814, 0x0701, "Ralink Technology RT2760" },
91 { 0x1814, 0x0781, "Ralink Technology RT2790" },
92 { 0x1814, 0x3060, "Ralink Technology RT3060" },
93 { 0x1814, 0x3062, "Ralink Technology RT3062" },
94 { 0x1814, 0x3090, "Ralink Technology RT3090" },
95 { 0x1814, 0x3091, "Ralink Technology RT3091" },
96 { 0x1814, 0x3092, "Ralink Technology RT3092" },
97 { 0x1814, 0x3390, "Ralink Technology RT3390" },
98 { 0x1814, 0x3562, "Ralink Technology RT3562" },
99 { 0x1814, 0x3592, "Ralink Technology RT3592" },
100 { 0x1814, 0x3593, "Ralink Technology RT3593" },
101 { 0x1814, 0x5360, "Ralink Technology RT5390" },
102 { 0x1814, 0x5362, "Ralink Technology RT5392" },
103 { 0x1814, 0x5390, "Ralink Technology RT5390" },
104 { 0x1814, 0x5392, "Ralink Technology RT5392" },
105 { 0x1814, 0x539a, "Ralink Technology RT5390" },
106 { 0x1814, 0x539f, "Ralink Technology RT5390" },
107 { 0x1a3b, 0x1059, "AWT RT2890" },
111 static const struct ral_opns {
112 int (*attach)(device_t, int);
113 int (*detach)(void *);
114 void (*shutdown)(void *);
115 void (*suspend)(void *);
116 void (*resume)(void *);
117 void (*intr)(void *);
119 } ral_rt2560_opns = {
127 }, ral_rt2661_opns = {
134 }, ral_rt2860_opns = {
143 struct ral_pci_softc {
145 struct rt2560_softc sc_rt2560;
146 struct rt2661_softc sc_rt2661;
147 struct rt2860_softc sc_rt2860;
150 const struct ral_opns *sc_opns;
151 struct resource *irq;
152 struct resource *mem;
157 static int ral_pci_probe(device_t);
158 static int ral_pci_attach(device_t);
159 static int ral_pci_detach(device_t);
160 static int ral_pci_shutdown(device_t);
161 static int ral_pci_suspend(device_t);
162 static int ral_pci_resume(device_t);
164 static device_method_t ral_pci_methods[] = {
165 /* Device interface */
166 DEVMETHOD(device_probe, ral_pci_probe),
167 DEVMETHOD(device_attach, ral_pci_attach),
168 DEVMETHOD(device_detach, ral_pci_detach),
169 DEVMETHOD(device_shutdown, ral_pci_shutdown),
170 DEVMETHOD(device_suspend, ral_pci_suspend),
171 DEVMETHOD(device_resume, ral_pci_resume),
176 static driver_t ral_pci_driver = {
179 sizeof (struct ral_pci_softc)
182 static devclass_t ral_devclass;
184 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL);
187 ral_pci_probe(device_t dev)
189 const struct ral_pci_ident *ident;
191 for (ident = ral_pci_ids; ident->name != NULL; ident++) {
192 if (pci_get_vendor(dev) == ident->vendor &&
193 pci_get_device(dev) == ident->device) {
194 device_set_desc(dev, ident->name);
195 return (BUS_PROBE_DEFAULT);
202 ral_pci_attach(device_t dev)
204 struct ral_pci_softc *psc = device_get_softc(dev);
205 struct rt2560_softc *sc = &psc->u.sc_rt2560;
206 #if defined(__DragonFly__)
209 int count, error, rid;
211 #if defined(__DragonFly__)
215 #if defined(__DragonFly__)
216 /* still needed? XXX */
217 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
218 device_printf(dev, "chip is in D%d power mode "
219 "-- setting to D0\n", pci_get_powerstate(dev));
220 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
224 pci_enable_busmaster(dev);
226 switch (pci_get_device(dev)) {
228 psc->sc_opns = &ral_rt2560_opns;
233 psc->sc_opns = &ral_rt2661_opns;
236 psc->sc_opns = &ral_rt2860_opns;
241 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
243 if (psc->mem == NULL) {
244 device_printf(dev, "could not allocate memory resource\n");
248 sc->sc_st = rman_get_bustag(psc->mem);
249 sc->sc_sh = rman_get_bushandle(psc->mem);
252 #if defined(__DragonFly__)
253 psc->sc_irq_type = pci_alloc_1intr(dev, ~ral_msi_disable,
255 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, irq_flags);
258 if (ral_msi_disable == 0) {
260 if (pci_alloc_msi(dev, &count) == 0)
263 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
264 (rid != 0 ? 0 : RF_SHAREABLE));
266 if (psc->irq == NULL) {
267 device_printf(dev, "could not allocate interrupt resource\n");
268 pci_release_msi(dev);
269 bus_release_resource(dev, SYS_RES_MEMORY,
270 rman_get_rid(psc->mem), psc->mem);
274 error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
276 (void)ral_pci_detach(dev);
281 * Hook our interrupt after all initialization is complete.
283 #if defined(__DragonFly__)
284 error = bus_setup_intr(dev, psc->irq, INTR_MPSAFE,
285 psc->sc_opns->intr, psc, &psc->sc_ih, &wlan_global_serializer);
287 error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE,
288 NULL, psc->sc_opns->intr, psc, &psc->sc_ih);
291 device_printf(dev, "could not set up interrupt\n");
292 (void)ral_pci_detach(dev);
301 ral_pci_detach(device_t dev)
303 struct ral_pci_softc *psc = device_get_softc(dev);
304 struct rt2560_softc *sc = &psc->u.sc_rt2560;
306 /* check if device was removed */
307 sc->sc_invalid = !bus_child_present(dev);
309 if (psc->sc_ih != NULL)
310 bus_teardown_intr(dev, psc->irq, psc->sc_ih);
311 (*psc->sc_opns->detach)(psc);
313 bus_generic_detach(dev);
315 #if defined(__DragonFly__)
316 if (psc->sc_irq_type == PCI_INTR_TYPE_MSI)
317 pci_release_msi(dev);
319 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(psc->irq),
321 pci_release_msi(dev);
323 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(psc->mem),
330 ral_pci_shutdown(device_t dev)
332 struct ral_pci_softc *psc = device_get_softc(dev);
334 (*psc->sc_opns->shutdown)(psc);
340 ral_pci_suspend(device_t dev)
342 struct ral_pci_softc *psc = device_get_softc(dev);
344 (*psc->sc_opns->suspend)(psc);
350 ral_pci_resume(device_t dev)
352 struct ral_pci_softc *psc = device_get_softc(dev);
354 (*psc->sc_opns->resume)(psc);