1 /* $FreeBSD: head/sys/dev/ral/if_ral_pci.c 189575 2009-03-09 13:23:54Z imp $ */
4 * Copyright (c) 2005, 2006
5 * Damien Bergamini <damien.bergamini@free.fr>
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.
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.
22 * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
25 #include <sys/param.h>
26 #include <sys/sysctl.h>
27 #include <sys/sockio.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>
35 #include <sys/endian.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>
46 #include <netproto/802_11/ieee80211_var.h>
47 #include <netproto/802_11/ieee80211_radiotap.h>
48 #include <netproto/802_11/ieee80211_amrr.h>
50 #include <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
53 #include <dev/netif/ral/rt2560var.h>
54 #include <dev/netif/ral/rt2661var.h>
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);
61 struct ral_pci_ident {
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" },
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 *);
92 }, ral_rt2661_opns = {
101 struct ral_pci_softc {
103 struct rt2560_softc sc_rt2560;
104 struct rt2661_softc sc_rt2661;
107 struct ral_opns *sc_opns;
110 struct resource *irq;
111 struct resource *mem;
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);
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),
134 static driver_t ral_pci_driver = {
137 sizeof (struct ral_pci_softc)
140 static devclass_t ral_devclass;
142 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
145 ral_pci_probe(device_t dev)
147 const struct ral_pci_ident *ident;
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);
159 /* Base Address Register */
160 #define RAL_PCI_BAR0 0x10
163 ral_pci_attach(device_t dev)
165 struct ral_pci_softc *psc = device_get_softc(dev);
166 struct rt2560_softc *sc = &psc->u.sc_rt2560;
170 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
171 device_printf(dev, "chip is in D%d power mode "
172 "-- setting to D0\n", pci_get_powerstate(dev));
173 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
176 /* enable bus-mastering */
177 pci_enable_busmaster(dev);
179 psc->sc_opns = (pci_get_device(dev) == 0x0201) ? &ral_rt2560_opns :
182 psc->mem_rid = RAL_PCI_BAR0;
183 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
185 if (psc->mem == NULL) {
186 device_printf(dev, "could not allocate memory resource\n");
190 sc->sc_st = rman_get_bustag(psc->mem);
191 sc->sc_sh = rman_get_bushandle(psc->mem);
195 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
196 RF_ACTIVE | RF_SHAREABLE);
197 if (psc->irq == NULL) {
198 device_printf(dev, "could not allocate interrupt resource\n");
202 error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
208 * Hook our interrupt after all initialization is complete.
210 error = bus_setup_intr(dev, psc->irq, INTR_MPSAFE,
211 psc->sc_opns->intr, psc, &psc->sc_ih, ifp->if_serializer);
213 device_printf(dev, "could not set up interrupt\n");
222 ral_pci_detach(device_t dev)
224 struct ral_pci_softc *psc = device_get_softc(dev);
225 struct rt2560_softc *sc = &psc->u.sc_rt2560;
227 /* check if device was removed */
228 sc->sc_invalid = !bus_child_present(dev);
230 (*psc->sc_opns->detach)(psc);
232 bus_generic_detach(dev);
233 bus_teardown_intr(dev, psc->irq, psc->sc_ih);
234 bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
236 bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
242 ral_pci_shutdown(device_t dev)
244 struct ral_pci_softc *psc = device_get_softc(dev);
246 (*psc->sc_opns->shutdown)(psc);
252 ral_pci_suspend(device_t dev)
254 struct ral_pci_softc *psc = device_get_softc(dev);
256 (*psc->sc_opns->suspend)(psc);
262 ral_pci_resume(device_t dev)
264 struct ral_pci_softc *psc = device_get_softc(dev);
266 (*psc->sc_opns->resume)(psc);