1 /*- 2 * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD$ 30 */ 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * Implement some basic spectral scan control logic. 36 */ 37 #include "opt_ath.h" 38 #include "opt_inet.h" 39 #include "opt_wlan.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/sysctl.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mutex.h> 48 #include <sys/errno.h> 49 50 #if defined(__DragonFly__) 51 /* empty */ 52 #else 53 #include <machine/bus.h> 54 #include <machine/resource.h> 55 #endif 56 #include <sys/bus.h> 57 58 #include <sys/socket.h> 59 60 #include <net/if.h> 61 #include <net/if_var.h> 62 #include <net/if_media.h> 63 #include <net/if_arp.h> 64 #include <net/ethernet.h> /* XXX for ether_sprintf */ 65 66 #include <netproto/802_11/ieee80211_var.h> 67 68 #include <net/bpf.h> 69 70 #ifdef INET 71 #include <netinet/in.h> 72 #include <netinet/if_ether.h> 73 #endif 74 75 #include <dev/netif/ath/ath/if_athvar.h> 76 #include <dev/netif/ath/ath/if_ath_spectral.h> 77 78 #include <dev/netif/ath/ath_hal/ah_desc.h> 79 80 struct ath_spectral_state { 81 HAL_SPECTRAL_PARAM spectral_state; 82 83 /* 84 * Should we enable spectral scan upon 85 * each network interface reset/change? 86 * 87 * This is intended to allow spectral scan 88 * frame reporting during channel scans. 89 * 90 * Later on it can morph into a larger 91 * scale config method where it pushes 92 * a "channel scan" config into the hardware 93 * rather than just the spectral_state 94 * config. 95 */ 96 int spectral_enable_after_reset; 97 }; 98 99 /* 100 * Methods which are required 101 */ 102 103 /* 104 * Attach spectral to the given interface 105 */ 106 int 107 ath_spectral_attach(struct ath_softc *sc) 108 { 109 struct ath_spectral_state *ss; 110 111 /* 112 * If spectral isn't supported, don't error - just 113 * quietly complete. 114 */ 115 if (! ath_hal_spectral_supported(sc->sc_ah)) 116 return (0); 117 118 ss = kmalloc(sizeof(struct ath_spectral_state), 119 M_TEMP, M_WAITOK | M_ZERO); 120 121 if (ss == NULL) { 122 device_printf(sc->sc_dev, "%s: failed to alloc memory\n", 123 __func__); 124 return (-ENOMEM); 125 } 126 127 sc->sc_spectral = ss; 128 129 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state); 130 131 return (0); 132 } 133 134 /* 135 * Detach spectral from the given interface 136 */ 137 int 138 ath_spectral_detach(struct ath_softc *sc) 139 { 140 141 if (! ath_hal_spectral_supported(sc->sc_ah)) 142 return (0); 143 144 if (sc->sc_spectral != NULL) { 145 kfree(sc->sc_spectral, M_TEMP); 146 } 147 return (0); 148 } 149 150 /* 151 * Check whether spectral needs enabling and if so, 152 * flip it on. 153 */ 154 int 155 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch) 156 { 157 struct ath_spectral_state *ss = sc->sc_spectral; 158 159 /* Default to disable spectral PHY reporting */ 160 sc->sc_dospectral = 0; 161 162 if (ss == NULL) 163 return (0); 164 165 if (ss->spectral_enable_after_reset) { 166 ath_hal_spectral_configure(sc->sc_ah, 167 &ss->spectral_state); 168 (void) ath_hal_spectral_start(sc->sc_ah); 169 sc->sc_dospectral = 1; 170 } 171 return (0); 172 } 173 174 /* 175 * Handle ioctl requests from the diagnostic interface. 176 * 177 * The initial part of this code resembles ath_ioctl_diag(); 178 * it's likely a good idea to reduce duplication between 179 * these two routines. 180 */ 181 int 182 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad) 183 { 184 unsigned int id = ad->ad_id & ATH_DIAG_ID; 185 void *indata = NULL; 186 void *outdata = NULL; 187 u_int32_t insize = ad->ad_in_size; 188 u_int32_t outsize = ad->ad_out_size; 189 int error = 0; 190 HAL_SPECTRAL_PARAM peout; 191 HAL_SPECTRAL_PARAM *pe; 192 struct ath_spectral_state *ss = sc->sc_spectral; 193 int val; 194 195 if (! ath_hal_spectral_supported(sc->sc_ah)) 196 return (EINVAL); 197 198 if (ad->ad_id & ATH_DIAG_IN) { 199 /* 200 * Copy in data. 201 */ 202 indata = kmalloc(insize, M_TEMP, M_INTWAIT); 203 if (indata == NULL) { 204 error = ENOMEM; 205 goto bad; 206 } 207 error = copyin(ad->ad_in_data, indata, insize); 208 if (error) 209 goto bad; 210 } 211 if (ad->ad_id & ATH_DIAG_DYN) { 212 /* 213 * Allocate a buffer for the results (otherwise the HAL 214 * returns a pointer to a buffer where we can read the 215 * results). Note that we depend on the HAL leaving this 216 * pointer for us to use below in reclaiming the buffer; 217 * may want to be more defensive. 218 */ 219 outdata = kmalloc(outsize, M_TEMP, M_INTWAIT); 220 if (outdata == NULL) { 221 error = ENOMEM; 222 goto bad; 223 } 224 } 225 switch (id) { 226 case SPECTRAL_CONTROL_GET_PARAMS: 227 memset(&peout, 0, sizeof(peout)); 228 outsize = sizeof(HAL_SPECTRAL_PARAM); 229 ath_hal_spectral_get_config(sc->sc_ah, &peout); 230 pe = (HAL_SPECTRAL_PARAM *) outdata; 231 memcpy(pe, &peout, sizeof(*pe)); 232 break; 233 case SPECTRAL_CONTROL_SET_PARAMS: 234 if (insize < sizeof(HAL_SPECTRAL_PARAM)) { 235 error = EINVAL; 236 break; 237 } 238 pe = (HAL_SPECTRAL_PARAM *) indata; 239 ath_hal_spectral_configure(sc->sc_ah, pe); 240 /* Save a local copy of the updated parameters */ 241 ath_hal_spectral_get_config(sc->sc_ah, 242 &ss->spectral_state); 243 break; 244 case SPECTRAL_CONTROL_START: 245 ath_hal_spectral_configure(sc->sc_ah, 246 &ss->spectral_state); 247 (void) ath_hal_spectral_start(sc->sc_ah); 248 sc->sc_dospectral = 1; 249 /* XXX need to update the PHY mask in the driver */ 250 break; 251 case SPECTRAL_CONTROL_STOP: 252 (void) ath_hal_spectral_stop(sc->sc_ah); 253 sc->sc_dospectral = 0; 254 /* XXX need to update the PHY mask in the driver */ 255 break; 256 case SPECTRAL_CONTROL_ENABLE_AT_RESET: 257 if (insize < sizeof(int)) { 258 device_printf(sc->sc_dev, "%d != %d\n", 259 insize, 260 (int) sizeof(int)); 261 error = EINVAL; 262 break; 263 } 264 if (indata == NULL) { 265 device_printf(sc->sc_dev, "indata=NULL\n"); 266 error = EINVAL; 267 break; 268 } 269 val = * ((int *) indata); 270 if (val == 0) 271 ss->spectral_enable_after_reset = 0; 272 else 273 ss->spectral_enable_after_reset = 1; 274 break; 275 case SPECTRAL_CONTROL_ENABLE: 276 /* XXX TODO */ 277 case SPECTRAL_CONTROL_DISABLE: 278 /* XXX TODO */ 279 break; 280 default: 281 error = EINVAL; 282 } 283 if (outsize < ad->ad_out_size) 284 ad->ad_out_size = outsize; 285 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size)) 286 error = EFAULT; 287 bad: 288 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL) 289 kfree(indata, M_TEMP); 290 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL) 291 kfree(outdata, M_TEMP); 292 return (error); 293 } 294 295