1 /*- 2 * Copyright (c) 2005, 2006 3 * Damien Bergamini <damien.bergamini@free.fr> 4 * 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. 8 * 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. 16 */ 17 18 #include <sys/cdefs.h> 19 __FBSDID("$FreeBSD$"); 20 21 /* 22 * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver. 23 */ 24 25 #include <sys/param.h> 26 #include <sys/systm.h> 27 #include <sys/bus.h> 28 #include <sys/kernel.h> 29 #include <sys/lock.h> 30 #include <sys/malloc.h> 31 #include <sys/mbuf.h> 32 #include <sys/module.h> 33 #include <sys/mutex.h> 34 #include <sys/rman.h> 35 #include <sys/socket.h> 36 37 #if defined(__DragonFly__) 38 /* empty */ 39 #else 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 #endif 43 44 #include <net/ethernet.h> 45 #include <net/if.h> 46 #include <net/if_media.h> 47 #include <net/route.h> 48 49 #include <netproto/802_11/ieee80211_var.h> 50 #include <netproto/802_11/ieee80211_radiotap.h> 51 52 #include <bus/pci/pcireg.h> 53 #include <bus/pci/pcivar.h> 54 55 #include <dev/netif/ral/rt2560var.h> 56 #include <dev/netif/ral/rt2661var.h> 57 #include <dev/netif/ral/rt2860var.h> 58 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); 63 64 static int ral_msi_disable; 65 TUNABLE_INT("hw.ral.msi_disable", &ral_msi_disable); 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, 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" }, 108 { 0, 0, NULL } 109 }; 110 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 *); 118 119 } ral_rt2560_opns = { 120 rt2560_attach, 121 rt2560_detach, 122 rt2560_stop, 123 rt2560_stop, 124 rt2560_resume, 125 rt2560_intr 126 127 }, ral_rt2661_opns = { 128 rt2661_attach, 129 rt2661_detach, 130 rt2661_shutdown, 131 rt2661_suspend, 132 rt2661_resume, 133 rt2661_intr 134 }, ral_rt2860_opns = { 135 rt2860_attach, 136 rt2860_detach, 137 rt2860_shutdown, 138 rt2860_suspend, 139 rt2860_resume, 140 rt2860_intr 141 }; 142 143 struct ral_pci_softc { 144 union { 145 struct rt2560_softc sc_rt2560; 146 struct rt2661_softc sc_rt2661; 147 struct rt2860_softc sc_rt2860; 148 } u; 149 150 const struct ral_opns *sc_opns; 151 struct resource *irq; 152 struct resource *mem; 153 void *sc_ih; 154 int sc_irq_type; 155 }; 156 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); 163 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), 172 173 DEVMETHOD_END 174 }; 175 176 static driver_t ral_pci_driver = { 177 "ral", 178 ral_pci_methods, 179 sizeof (struct ral_pci_softc) 180 }; 181 182 static devclass_t ral_devclass; 183 184 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL); 185 186 static int 187 ral_pci_probe(device_t dev) 188 { 189 const struct ral_pci_ident *ident; 190 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); 196 } 197 } 198 return ENXIO; 199 } 200 201 static int 202 ral_pci_attach(device_t dev) 203 { 204 struct ral_pci_softc *psc = device_get_softc(dev); 205 struct rt2560_softc *sc = &psc->u.sc_rt2560; 206 #if defined(__DragonFly__) 207 int error, rid; 208 #else 209 int count, error, rid; 210 #endif 211 #if defined(__DragonFly__) 212 int irq_flags; 213 #endif 214 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); 221 } 222 #endif 223 224 pci_enable_busmaster(dev); 225 226 switch (pci_get_device(dev)) { 227 case 0x0201: 228 psc->sc_opns = &ral_rt2560_opns; 229 break; 230 case 0x0301: 231 case 0x0302: 232 case 0x0401: 233 psc->sc_opns = &ral_rt2661_opns; 234 break; 235 default: 236 psc->sc_opns = &ral_rt2860_opns; 237 break; 238 } 239 240 rid = PCIR_BAR(0); 241 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 242 RF_ACTIVE); 243 if (psc->mem == NULL) { 244 device_printf(dev, "could not allocate memory resource\n"); 245 return ENXIO; 246 } 247 248 sc->sc_st = rman_get_bustag(psc->mem); 249 sc->sc_sh = rman_get_bushandle(psc->mem); 250 sc->sc_invalid = 1; 251 252 #if defined(__DragonFly__) 253 psc->sc_irq_type = pci_alloc_1intr(dev, ~ral_msi_disable, 254 &rid, &irq_flags); 255 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, irq_flags); 256 #else 257 rid = 0; 258 if (ral_msi_disable == 0) { 259 count = 1; 260 if (pci_alloc_msi(dev, &count) == 0) 261 rid = 1; 262 } 263 psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | 264 (rid != 0 ? 0 : RF_SHAREABLE)); 265 #endif 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); 271 return ENXIO; 272 } 273 274 error = (*psc->sc_opns->attach)(dev, pci_get_device(dev)); 275 if (error != 0) { 276 (void)ral_pci_detach(dev); 277 return error; 278 } 279 280 /* 281 * Hook our interrupt after all initialization is complete. 282 */ 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); 286 #else 287 error = bus_setup_intr(dev, psc->irq, INTR_TYPE_NET | INTR_MPSAFE, 288 NULL, psc->sc_opns->intr, psc, &psc->sc_ih); 289 #endif 290 if (error != 0) { 291 device_printf(dev, "could not set up interrupt\n"); 292 (void)ral_pci_detach(dev); 293 return error; 294 } 295 sc->sc_invalid = 0; 296 297 return 0; 298 } 299 300 static int 301 ral_pci_detach(device_t dev) 302 { 303 struct ral_pci_softc *psc = device_get_softc(dev); 304 struct rt2560_softc *sc = &psc->u.sc_rt2560; 305 306 /* check if device was removed */ 307 sc->sc_invalid = !bus_child_present(dev); 308 309 if (psc->sc_ih != NULL) 310 bus_teardown_intr(dev, psc->irq, psc->sc_ih); 311 (*psc->sc_opns->detach)(psc); 312 313 bus_generic_detach(dev); 314 315 #if defined(__DragonFly__) 316 if (psc->sc_irq_type == PCI_INTR_TYPE_MSI) 317 pci_release_msi(dev); 318 #endif 319 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(psc->irq), 320 psc->irq); 321 pci_release_msi(dev); 322 323 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(psc->mem), 324 psc->mem); 325 326 return 0; 327 } 328 329 static int 330 ral_pci_shutdown(device_t dev) 331 { 332 struct ral_pci_softc *psc = device_get_softc(dev); 333 334 (*psc->sc_opns->shutdown)(psc); 335 336 return 0; 337 } 338 339 static int 340 ral_pci_suspend(device_t dev) 341 { 342 struct ral_pci_softc *psc = device_get_softc(dev); 343 344 (*psc->sc_opns->suspend)(psc); 345 346 return 0; 347 } 348 349 static int 350 ral_pci_resume(device_t dev) 351 { 352 struct ral_pci_softc *psc = device_get_softc(dev); 353 354 (*psc->sc_opns->resume)(psc); 355 356 return 0; 357 } 358