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/mutex.h> 47 #include <sys/errno.h> 48 49 #include <machine/bus.h> 50 #include <machine/resource.h> 51 #include <sys/bus.h> 52 53 #include <sys/socket.h> 54 55 #include <net/if.h> 56 #include <net/if_media.h> 57 #include <net/if_arp.h> 58 #include <net/ethernet.h> /* XXX for ether_sprintf */ 59 60 #include <net80211/ieee80211_var.h> 61 62 #include <net/bpf.h> 63 64 #ifdef INET 65 #include <netinet/in.h> 66 #include <netinet/if_ether.h> 67 #endif 68 69 #include <dev/ath/if_athvar.h> 70 #include <dev/ath/if_ath_spectral.h> 71 72 #include <dev/ath/ath_hal/ah_desc.h> 73 74 struct ath_spectral_state { 75 HAL_SPECTRAL_PARAM spectral_state; 76 int spectral_active; 77 int spectral_enabled; 78 }; 79 80 /* 81 * Methods which are required 82 */ 83 84 /* 85 * Attach spectral to the given interface 86 */ 87 int 88 ath_spectral_attach(struct ath_softc *sc) 89 { 90 struct ath_spectral_state *ss; 91 92 /* 93 * If spectral isn't supported, don't error - just 94 * quietly complete. 95 */ 96 if (! ath_hal_spectral_supported(sc->sc_ah)) 97 return (0); 98 99 ss = malloc(sizeof(struct ath_spectral_state), 100 M_TEMP, M_WAITOK | M_ZERO); 101 102 if (ss == NULL) { 103 device_printf(sc->sc_dev, "%s: failed to alloc memory\n", 104 __func__); 105 return (-ENOMEM); 106 } 107 108 sc->sc_spectral = ss; 109 110 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state); 111 112 return (0); 113 } 114 115 /* 116 * Detach spectral from the given interface 117 */ 118 int 119 ath_spectral_detach(struct ath_softc *sc) 120 { 121 122 if (! ath_hal_spectral_supported(sc->sc_ah)) 123 return (0); 124 125 if (sc->sc_spectral != NULL) { 126 free(sc->sc_spectral, M_TEMP); 127 } 128 return (0); 129 } 130 131 /* 132 * Check whether spectral needs enabling and if so, 133 * flip it on. 134 */ 135 int 136 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch) 137 { 138 139 return (0); 140 } 141 142 /* 143 * Handle ioctl requests from the diagnostic interface. 144 * 145 * The initial part of this code resembles ath_ioctl_diag(); 146 * it's likely a good idea to reduce duplication between 147 * these two routines. 148 */ 149 int 150 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad) 151 { 152 unsigned int id = ad->ad_id & ATH_DIAG_ID; 153 void *indata = NULL; 154 void *outdata = NULL; 155 u_int32_t insize = ad->ad_in_size; 156 u_int32_t outsize = ad->ad_out_size; 157 int error = 0; 158 HAL_SPECTRAL_PARAM peout; 159 HAL_SPECTRAL_PARAM *pe; 160 struct ath_spectral_state *ss = sc->sc_spectral; 161 162 if (! ath_hal_spectral_supported(sc->sc_ah)) 163 return (EINVAL); 164 165 if (ad->ad_id & ATH_DIAG_IN) { 166 /* 167 * Copy in data. 168 */ 169 indata = malloc(insize, M_TEMP, M_NOWAIT); 170 if (indata == NULL) { 171 error = ENOMEM; 172 goto bad; 173 } 174 error = copyin(ad->ad_in_data, indata, insize); 175 if (error) 176 goto bad; 177 } 178 if (ad->ad_id & ATH_DIAG_DYN) { 179 /* 180 * Allocate a buffer for the results (otherwise the HAL 181 * returns a pointer to a buffer where we can read the 182 * results). Note that we depend on the HAL leaving this 183 * pointer for us to use below in reclaiming the buffer; 184 * may want to be more defensive. 185 */ 186 outdata = malloc(outsize, M_TEMP, M_NOWAIT); 187 if (outdata == NULL) { 188 error = ENOMEM; 189 goto bad; 190 } 191 } 192 switch (id) { 193 case SPECTRAL_CONTROL_GET_PARAMS: 194 memset(&peout, 0, sizeof(peout)); 195 outsize = sizeof(HAL_SPECTRAL_PARAM); 196 ath_hal_spectral_get_config(sc->sc_ah, &peout); 197 pe = (HAL_SPECTRAL_PARAM *) outdata; 198 memcpy(pe, &peout, sizeof(*pe)); 199 break; 200 case SPECTRAL_CONTROL_SET_PARAMS: 201 if (insize < sizeof(HAL_SPECTRAL_PARAM)) { 202 error = EINVAL; 203 break; 204 } 205 pe = (HAL_SPECTRAL_PARAM *) indata; 206 ath_hal_spectral_configure(sc->sc_ah, pe); 207 /* Save a local copy of the updated parameters */ 208 ath_hal_spectral_get_config(sc->sc_ah, 209 &ss->spectral_state); 210 break; 211 case SPECTRAL_CONTROL_START: 212 ath_hal_spectral_configure(sc->sc_ah, 213 &ss->spectral_state); 214 (void) ath_hal_spectral_start(sc->sc_ah); 215 break; 216 case SPECTRAL_CONTROL_STOP: 217 (void) ath_hal_spectral_stop(sc->sc_ah); 218 break; 219 case SPECTRAL_CONTROL_ENABLE: 220 /* XXX TODO */ 221 case SPECTRAL_CONTROL_DISABLE: 222 /* XXX TODO */ 223 break; 224 default: 225 error = EINVAL; 226 } 227 if (outsize < ad->ad_out_size) 228 ad->ad_out_size = outsize; 229 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size)) 230 error = EFAULT; 231 bad: 232 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL) 233 free(indata, M_TEMP); 234 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL) 235 free(outdata, M_TEMP); 236 return (error); 237 } 238 239