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.
17 * $FreeBSD: src/sys/dev/ral/if_ral_pci.c,v 1.4 2006/03/05 23:27:51 silby Exp $
18 * $DragonFly: src/sys/dev/netif/ral/if_ral_pci.c,v 1.1 2006/05/20 09:13:09 sephe Exp $
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>
37 #include <machine/bus.h>
38 #include <machine/resource.h>
43 #include <net/if_arp.h>
44 #include <net/ethernet.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_types.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/if_ralrate.h>
56 #include <dev/netif/ral/rt2560var.h>
57 #include <dev/netif/ral/rt2661var.h>
60 int (*attach)(device_t, int);
61 int (*detach)(void *);
62 void (*shutdown)(void *);
63 void (*suspend)(void *);
64 void (*resume)(void *);
67 static const struct ral_opns ral_rt2560_opns = {
68 .attach = rt2560_attach,
69 .detach = rt2560_detach,
70 .shutdown = rt2560_shutdown,
71 .suspend = rt2560_suspend,
72 .resume = rt2560_resume,
75 static const struct ral_opns ral_rt2661_opns = {
76 .attach = rt2661_attach,
77 .detach = rt2661_detach,
78 .shutdown = rt2661_shutdown,
79 .suspend = rt2661_suspend,
80 .resume = rt2661_resume,
83 static const struct ral_pci_ident {
87 const struct ral_opns *opns;
89 { 0x1814, 0x0201, "Ralink Technology RT2560", &ral_rt2560_opns },
90 { 0x1814, 0x0301, "Ralink Technology RT2561S", &ral_rt2661_opns },
91 { 0x1814, 0x0302, "Ralink Technology RT2561", &ral_rt2661_opns },
92 { 0x1814, 0x0401, "Ralink Technology RT2661", &ral_rt2661_opns },
96 struct ral_pci_softc {
97 /* XXX MUST be the first field */
99 struct rt2560_softc sc_rt2560;
100 struct rt2661_softc sc_rt2661;
103 const struct ral_opns *sc_opns;
105 struct resource *mem;
108 static int ral_pci_probe(device_t);
109 static int ral_pci_attach(device_t);
110 static int ral_pci_detach(device_t);
111 static int ral_pci_shutdown(device_t);
112 static int ral_pci_suspend(device_t);
113 static int ral_pci_resume(device_t);
115 static device_method_t ral_pci_methods[] = {
116 /* Device interface */
117 DEVMETHOD(device_probe, ral_pci_probe),
118 DEVMETHOD(device_attach, ral_pci_attach),
119 DEVMETHOD(device_detach, ral_pci_detach),
120 DEVMETHOD(device_shutdown, ral_pci_shutdown),
121 DEVMETHOD(device_suspend, ral_pci_suspend),
122 DEVMETHOD(device_resume, ral_pci_resume),
127 static driver_t ral_pci_driver = {
130 sizeof (struct ral_pci_softc)
133 static devclass_t ral_devclass;
135 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
136 DRIVER_MODULE(ral, cardbus, ral_pci_driver, ral_devclass, 0, 0);
138 MODULE_DEPEND(ral, wlan, 1, 1, 1);
139 MODULE_DEPEND(ral, pci, 1, 1, 1);
140 MODULE_DEPEND(ral, cardbus, 1, 1, 1);
143 ral_pci_probe(device_t dev)
145 const struct ral_pci_ident *ident;
148 vid = pci_get_vendor(dev);
149 did = pci_get_device(dev);
150 for (ident = ral_pci_ids; ident->name != NULL; ident++) {
151 if (vid == ident->vendor && did == ident->device) {
152 struct ral_pci_softc *psc = device_get_softc(dev);
154 psc->sc_opns = ident->opns;
155 device_set_desc(dev, ident->name);
162 /* Base Address Register */
163 #define RAL_PCI_BAR0 0x10
166 ral_pci_attach(device_t dev)
168 struct ral_pci_softc *psc = device_get_softc(dev);
169 struct rt2560_softc *sc = &psc->u.sc_rt2560;
172 /* Assign `dev' earlier, so that we can do possible error clean up */
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);
181 /* enable bus-mastering */
182 pci_enable_busmaster(dev);
184 psc->mem_rid = RAL_PCI_BAR0;
185 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
187 if (psc->mem == NULL) {
188 device_printf(dev, "could not allocate memory resource\n");
191 sc->sc_st = rman_get_bustag(psc->mem);
192 sc->sc_sh = rman_get_bushandle(psc->mem);
194 error = psc->sc_opns->attach(dev, pci_get_device(dev));
202 ral_pci_detach(device_t dev)
204 struct ral_pci_softc *psc = device_get_softc(dev);
206 if (device_is_attached(dev))
207 psc->sc_opns->detach(psc);
209 bus_generic_detach(dev);
211 if (psc->mem != NULL) {
212 bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid,
219 ral_pci_shutdown(device_t dev)
221 struct ral_pci_softc *psc = device_get_softc(dev);
223 psc->sc_opns->shutdown(psc);
228 ral_pci_suspend(device_t dev)
230 struct ral_pci_softc *psc = device_get_softc(dev);
232 psc->sc_opns->suspend(psc);
237 ral_pci_resume(device_t dev)
239 struct ral_pci_softc *psc = device_get_softc(dev);
241 psc->sc_opns->resume(psc);