2 * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Implement some basic spectral scan control logic.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/errno.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
54 #include <sys/socket.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/if_arp.h>
60 #include <net/ethernet.h> /* XXX for ether_sprintf */
62 #include <net80211/ieee80211_var.h>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
71 #include <dev/ath/if_athvar.h>
72 #include <dev/ath/if_ath_spectral.h>
74 #include <dev/ath/ath_hal/ah_desc.h>
76 struct ath_spectral_state {
77 HAL_SPECTRAL_PARAM spectral_state;
80 * Should we enable spectral scan upon
81 * each network interface reset/change?
83 * This is intended to allow spectral scan
84 * frame reporting during channel scans.
86 * Later on it can morph into a larger
87 * scale config method where it pushes
88 * a "channel scan" config into the hardware
89 * rather than just the spectral_state
92 int spectral_enable_after_reset;
96 * Methods which are required
100 * Attach spectral to the given interface
103 ath_spectral_attach(struct ath_softc *sc)
105 struct ath_spectral_state *ss;
108 * If spectral isn't supported, don't error - just
111 if (! ath_hal_spectral_supported(sc->sc_ah))
114 ss = malloc(sizeof(struct ath_spectral_state),
115 M_TEMP, M_WAITOK | M_ZERO);
118 device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
123 sc->sc_spectral = ss;
125 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
131 * Detach spectral from the given interface
134 ath_spectral_detach(struct ath_softc *sc)
137 if (! ath_hal_spectral_supported(sc->sc_ah))
140 if (sc->sc_spectral != NULL) {
141 free(sc->sc_spectral, M_TEMP);
147 * Check whether spectral needs enabling and if so,
151 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
153 struct ath_spectral_state *ss = sc->sc_spectral;
155 /* Default to disable spectral PHY reporting */
156 sc->sc_dospectral = 0;
161 if (ss->spectral_enable_after_reset) {
162 ath_hal_spectral_configure(sc->sc_ah,
163 &ss->spectral_state);
164 (void) ath_hal_spectral_start(sc->sc_ah);
165 sc->sc_dospectral = 1;
171 * Handle ioctl requests from the diagnostic interface.
173 * The initial part of this code resembles ath_ioctl_diag();
174 * it's likely a good idea to reduce duplication between
175 * these two routines.
178 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
180 unsigned int id = ad->ad_id & ATH_DIAG_ID;
182 void *outdata = NULL;
183 u_int32_t insize = ad->ad_in_size;
184 u_int32_t outsize = ad->ad_out_size;
186 HAL_SPECTRAL_PARAM peout;
187 HAL_SPECTRAL_PARAM *pe;
188 struct ath_spectral_state *ss = sc->sc_spectral;
191 if (! ath_hal_spectral_supported(sc->sc_ah))
194 if (ad->ad_id & ATH_DIAG_IN) {
198 indata = malloc(insize, M_TEMP, M_NOWAIT);
199 if (indata == NULL) {
203 error = copyin(ad->ad_in_data, indata, insize);
207 if (ad->ad_id & ATH_DIAG_DYN) {
209 * Allocate a buffer for the results (otherwise the HAL
210 * returns a pointer to a buffer where we can read the
211 * results). Note that we depend on the HAL leaving this
212 * pointer for us to use below in reclaiming the buffer;
213 * may want to be more defensive.
215 outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
216 if (outdata == NULL) {
222 case SPECTRAL_CONTROL_GET_PARAMS:
223 memset(&peout, 0, sizeof(peout));
224 outsize = sizeof(HAL_SPECTRAL_PARAM);
225 ath_hal_spectral_get_config(sc->sc_ah, &peout);
226 pe = (HAL_SPECTRAL_PARAM *) outdata;
227 memcpy(pe, &peout, sizeof(*pe));
229 case SPECTRAL_CONTROL_SET_PARAMS:
230 if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
234 pe = (HAL_SPECTRAL_PARAM *) indata;
235 ath_hal_spectral_configure(sc->sc_ah, pe);
236 /* Save a local copy of the updated parameters */
237 ath_hal_spectral_get_config(sc->sc_ah,
238 &ss->spectral_state);
240 case SPECTRAL_CONTROL_START:
241 ath_hal_spectral_configure(sc->sc_ah,
242 &ss->spectral_state);
243 (void) ath_hal_spectral_start(sc->sc_ah);
244 sc->sc_dospectral = 1;
245 /* XXX need to update the PHY mask in the driver */
247 case SPECTRAL_CONTROL_STOP:
248 (void) ath_hal_spectral_stop(sc->sc_ah);
249 sc->sc_dospectral = 0;
250 /* XXX need to update the PHY mask in the driver */
252 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
253 if (insize < sizeof(int)) {
254 device_printf(sc->sc_dev, "%d != %d\n",
260 if (indata == NULL) {
261 device_printf(sc->sc_dev, "indata=NULL\n");
265 val = * ((int *) indata);
267 ss->spectral_enable_after_reset = 0;
269 ss->spectral_enable_after_reset = 1;
271 case SPECTRAL_CONTROL_ENABLE:
273 case SPECTRAL_CONTROL_DISABLE:
280 if (outsize < ad->ad_out_size)
281 ad->ad_out_size = outsize;
282 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
285 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
286 free(indata, M_TEMP);
287 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
288 free(outdata, M_TEMP);