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 #include <dev/ral/rt2860var.h> 61 62 MODULE_DEPEND(ral, pci, 1, 1, 1); 63 MODULE_DEPEND(ral, firmware, 1, 1, 1); 64 MODULE_DEPEND(ral, wlan, 1, 1, 1); 65 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1); 66 67 struct ral_pci_ident { 68 uint16_t vendor; 69 uint16_t device; 70 const char *name; 71 }; 72 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, 0x5390, "Ralink Technology RT5390" }, 102 { 0x1814, 0x539a, "Ralink Technology RT5390" }, 103 { 0x1814, 0x539f, "Ralink Technology RT5390" }, 104 { 0x1a3b, 0x1059, "AWT RT2890" }, 105 { 0, 0, NULL } 106 }; 107 108 static struct ral_opns { 109 int (*attach)(device_t, int); 110 int (*detach)(void *); 111 void (*shutdown)(void *); 112 void (*suspend)(void *); 113 void (*resume)(void *); 114 void (*intr)(void *); 115 116 } ral_rt2560_opns = { 117 rt2560_attach, 118 rt2560_detach, 119 rt2560_stop, 120 rt2560_stop, 121 rt2560_resume, 122 rt2560_intr 123 124 }, ral_rt2661_opns = { 125 rt2661_attach, 126 rt2661_detach, 127 rt2661_shutdown, 128 rt2661_suspend, 129 rt2661_resume, 130 rt2661_intr 131 }, ral_rt2860_opns = { 132 rt2860_attach, 133 rt2860_detach, 134 rt2860_shutdown, 135 rt2860_suspend, 136 rt2860_resume, 137 rt2860_intr 138 }; 139 140 struct ral_pci_softc { 141 union { 142 struct rt2560_softc sc_rt2560; 143 struct rt2661_softc sc_rt2661; 144 struct rt2860_softc sc_rt2860; 145 } u; 146 147 struct ral_opns *sc_opns; 148 int irq_rid; 149 int mem_rid; 150 struct resource *irq; 151 struct resource *mem; 152 void *sc_ih; 153 }; 154 155 static int ral_pci_probe(device_t); 156 static int ral_pci_attach(device_t); 157 static int ral_pci_detach(device_t); 158 static int ral_pci_shutdown(device_t); 159 static int ral_pci_suspend(device_t); 160 static int ral_pci_resume(device_t); 161 162 static device_method_t ral_pci_methods[] = { 163 /* Device interface */ 164 DEVMETHOD(device_probe, ral_pci_probe), 165 DEVMETHOD(device_attach, ral_pci_attach), 166 DEVMETHOD(device_detach, ral_pci_detach), 167 DEVMETHOD(device_shutdown, ral_pci_shutdown), 168 DEVMETHOD(device_suspend, ral_pci_suspend), 169 DEVMETHOD(device_resume, ral_pci_resume), 170 171 { 0, 0 } 172 }; 173 174 static driver_t ral_pci_driver = { 175 "ral", 176 ral_pci_methods, 177 sizeof (struct ral_pci_softc) 178 }; 179 180 static devclass_t ral_devclass; 181 182 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0); 183 184 static int 185 ral_pci_probe(device_t dev) 186 { 187 const struct ral_pci_ident *ident; 188 189 for (ident = ral_pci_ids; ident->name != NULL; ident++) { 190 if (pci_get_vendor(dev) == ident->vendor && 191 pci_get_device(dev) == ident->device) { 192 device_set_desc(dev, ident->name); 193 return 0; 194 } 195 } 196 return ENXIO; 197 } 198 199 /* Base Address Register */ 200 #define RAL_PCI_BAR0 0x10 201 202 static int 203 ral_pci_attach(device_t dev) 204 { 205 struct ral_pci_softc *psc = device_get_softc(dev); 206 struct rt2560_softc *sc = &psc->u.sc_rt2560; 207 int error; 208 209 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 210 device_printf(dev, "chip is in D%d power mode " 211 "-- setting to D0\n", pci_get_powerstate(dev)); 212 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 213 } 214 215 /* enable bus-mastering */ 216 pci_enable_busmaster(dev); 217 218 switch (pci_get_device(dev)) { 219 case 0x0201: 220 psc->sc_opns = &ral_rt2560_opns; 221 break; 222 case 0x0301: 223 case 0x0302: 224 case 0x0401: 225 psc->sc_opns = &ral_rt2661_opns; 226 break; 227 default: 228 psc->sc_opns = &ral_rt2860_opns; 229 break; 230 } 231 232 psc->mem_rid = RAL_PCI_BAR0; 233 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid, 234 RF_ACTIVE); 235 if (psc->mem == NULL) { 236 device_printf(dev, "could not allocate memory resource\n"); 237 return ENXIO; 238 } 239 240 sc->sc_st = rman_get_bustag(psc->mem); 241 sc->sc_sh = rman_get_bushandle(psc->mem); 242 sc->sc_invalid = 1; 243 244 psc->irq_rid = 0; 245 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid, 246 RF_ACTIVE | RF_SHAREABLE); 247 if (psc->irq == NULL) { 248 device_printf(dev, "could not allocate interrupt resource\n"); 249 return ENXIO; 250 } 251 252 error = (*psc->sc_opns->attach)(dev, pci_get_device(dev)); 253 if (error != 0) 254 return error; 255 256 /* 257 * Hook our interrupt after all initialization is complete. 258 */ 259 error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE, 260 NULL, psc->sc_opns->intr, psc, &psc->sc_ih); 261 if (error != 0) { 262 device_printf(dev, "could not set up interrupt\n"); 263 return error; 264 } 265 sc->sc_invalid = 0; 266 267 return 0; 268 } 269 270 static int 271 ral_pci_detach(device_t dev) 272 { 273 struct ral_pci_softc *psc = device_get_softc(dev); 274 struct rt2560_softc *sc = &psc->u.sc_rt2560; 275 276 /* check if device was removed */ 277 sc->sc_invalid = !bus_child_present(dev); 278 279 (*psc->sc_opns->detach)(psc); 280 281 bus_generic_detach(dev); 282 bus_teardown_intr(dev, psc->irq, psc->sc_ih); 283 bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq); 284 285 bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem); 286 287 return 0; 288 } 289 290 static int 291 ral_pci_shutdown(device_t dev) 292 { 293 struct ral_pci_softc *psc = device_get_softc(dev); 294 295 (*psc->sc_opns->shutdown)(psc); 296 297 return 0; 298 } 299 300 static int 301 ral_pci_suspend(device_t dev) 302 { 303 struct ral_pci_softc *psc = device_get_softc(dev); 304 305 (*psc->sc_opns->suspend)(psc); 306 307 return 0; 308 } 309 310 static int 311 ral_pci_resume(device_t dev) 312 { 313 struct ral_pci_softc *psc = device_get_softc(dev); 314 315 (*psc->sc_opns->resume)(psc); 316 317 return 0; 318 } 319