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