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 * $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.6 2008/01/15 09:01:13 sephe Exp $ 19 */ 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/sysctl.h> 27 #include <sys/sockio.h> 28 #include <sys/mbuf.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> 34 #include <sys/bus.h> 35 #include <sys/rman.h> 36 #include <sys/endian.h> 37 38 #include <net/bpf.h> 39 #include <net/if.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> 45 46 #include <netproto/802_11/ieee80211_var.h> 47 #include <netproto/802_11/ieee80211_radiotap.h> 48 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h> 49 #include <netproto/802_11/wlan_ratectl/sample/ieee80211_sample_param.h> 50 51 #include <bus/pci/pcidevs.h> 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 58 struct ral_opns { 59 int (*attach)(device_t, int); 60 int (*detach)(void *); 61 void (*shutdown)(void *); 62 void (*suspend)(void *); 63 void (*resume)(void *); 64 }; 65 66 static const struct ral_opns ral_rt2560_opns = { 67 .attach = rt2560_attach, 68 .detach = rt2560_detach, 69 .shutdown = rt2560_shutdown, 70 .suspend = rt2560_suspend, 71 .resume = rt2560_resume, 72 }; 73 74 static const struct ral_opns ral_rt2661_opns = { 75 .attach = rt2661_attach, 76 .detach = rt2661_detach, 77 .shutdown = rt2661_shutdown, 78 .suspend = rt2661_suspend, 79 .resume = rt2661_resume, 80 }; 81 82 static const struct ral_pci_ident { 83 uint16_t vendor; 84 uint16_t device; 85 const char *name; 86 const struct ral_opns *opns; 87 } ral_pci_ids[] = { 88 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560, 89 "Ralink Technology RT2560", &ral_rt2560_opns }, 90 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S, 91 "Ralink Technology RT2561S", &ral_rt2661_opns }, 92 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561, 93 "Ralink Technology RT2561", &ral_rt2661_opns }, 94 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661, 95 "Ralink Technology RT2661", &ral_rt2661_opns }, 96 { 0, 0, NULL, NULL } 97 }; 98 99 struct ral_pci_softc { 100 /* XXX MUST be the first field */ 101 union { 102 struct rt2560_softc sc_rt2560; 103 struct rt2661_softc sc_rt2661; 104 } u; 105 106 const struct ral_opns *sc_opns; 107 int mem_rid; 108 struct resource *mem; 109 }; 110 111 static int ral_pci_probe(device_t); 112 static int ral_pci_attach(device_t); 113 static int ral_pci_detach(device_t); 114 static int ral_pci_shutdown(device_t); 115 static int ral_pci_suspend(device_t); 116 static int ral_pci_resume(device_t); 117 118 static device_method_t ral_pci_methods[] = { 119 /* Device interface */ 120 DEVMETHOD(device_probe, ral_pci_probe), 121 DEVMETHOD(device_attach, ral_pci_attach), 122 DEVMETHOD(device_detach, ral_pci_detach), 123 DEVMETHOD(device_shutdown, ral_pci_shutdown), 124 DEVMETHOD(device_suspend, ral_pci_suspend), 125 DEVMETHOD(device_resume, ral_pci_resume), 126 127 { 0, 0 } 128 }; 129 130 static driver_t ral_pci_driver = { 131 "ral", 132 ral_pci_methods, 133 sizeof (struct ral_pci_softc) 134 }; 135 136 static devclass_t ral_devclass; 137 138 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0); 139 DRIVER_MODULE(ral, cardbus, ral_pci_driver, ral_devclass, 0, 0); 140 141 MODULE_DEPEND(ral, wlan, 1, 1, 1); 142 MODULE_DEPEND(ral, wlan_ratectl_onoe, 1, 1, 1); 143 MODULE_DEPEND(ral, wlan_ratectl_sample, 1, 1, 1); 144 MODULE_DEPEND(ral, pci, 1, 1, 1); 145 MODULE_DEPEND(ral, cardbus, 1, 1, 1); 146 147 static int 148 ral_pci_probe(device_t dev) 149 { 150 const struct ral_pci_ident *ident; 151 uint16_t vid, did; 152 153 vid = pci_get_vendor(dev); 154 did = pci_get_device(dev); 155 for (ident = ral_pci_ids; ident->name != NULL; ident++) { 156 if (vid == ident->vendor && did == ident->device) { 157 struct ral_pci_softc *psc = device_get_softc(dev); 158 159 psc->sc_opns = ident->opns; 160 device_set_desc(dev, ident->name); 161 return 0; 162 } 163 } 164 return ENXIO; 165 } 166 167 /* Base Address Register */ 168 #define RAL_PCI_BAR0 0x10 169 170 static int 171 ral_pci_attach(device_t dev) 172 { 173 struct ral_pci_softc *psc = device_get_softc(dev); 174 struct rt2560_softc *sc = &psc->u.sc_rt2560; 175 int error; 176 177 /* Assign `dev' earlier, so that we can do possible error clean up */ 178 sc->sc_dev = dev; 179 180 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 181 device_printf(dev, "chip is in D%d power mode " 182 "-- setting to D0\n", pci_get_powerstate(dev)); 183 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 184 } 185 186 /* enable bus-mastering */ 187 pci_enable_busmaster(dev); 188 189 psc->mem_rid = RAL_PCI_BAR0; 190 psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid, 191 RF_ACTIVE); 192 if (psc->mem == NULL) { 193 device_printf(dev, "could not allocate memory resource\n"); 194 return ENXIO; 195 } 196 sc->sc_st = rman_get_bustag(psc->mem); 197 sc->sc_sh = rman_get_bushandle(psc->mem); 198 199 error = psc->sc_opns->attach(dev, pci_get_device(dev)); 200 if (error != 0) 201 ral_pci_detach(dev); 202 203 return error; 204 } 205 206 static int 207 ral_pci_detach(device_t dev) 208 { 209 struct ral_pci_softc *psc = device_get_softc(dev); 210 211 if (device_is_attached(dev)) 212 psc->sc_opns->detach(psc); 213 214 bus_generic_detach(dev); 215 216 if (psc->mem != NULL) { 217 bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, 218 psc->mem); 219 } 220 return 0; 221 } 222 223 static int 224 ral_pci_shutdown(device_t dev) 225 { 226 struct ral_pci_softc *psc = device_get_softc(dev); 227 228 psc->sc_opns->shutdown(psc); 229 return 0; 230 } 231 232 static int 233 ral_pci_suspend(device_t dev) 234 { 235 struct ral_pci_softc *psc = device_get_softc(dev); 236 237 psc->sc_opns->suspend(psc); 238 return 0; 239 } 240 241 static int 242 ral_pci_resume(device_t dev) 243 { 244 struct ral_pci_softc *psc = device_get_softc(dev); 245 246 psc->sc_opns->resume(psc); 247 return 0; 248 } 249