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>
73 #include <dev/ath/if_ath_misc.h>
75 #include <dev/ath/ath_hal/ah_desc.h>
77 struct ath_spectral_state {
78 HAL_SPECTRAL_PARAM spectral_state;
81 * Should we enable spectral scan upon
82 * each network interface reset/change?
84 * This is intended to allow spectral scan
85 * frame reporting during channel scans.
87 * Later on it can morph into a larger
88 * scale config method where it pushes
89 * a "channel scan" config into the hardware
90 * rather than just the spectral_state
93 int spectral_enable_after_reset;
97 * Methods which are required
101 * Attach spectral to the given interface
104 ath_spectral_attach(struct ath_softc *sc)
106 struct ath_spectral_state *ss;
109 * If spectral isn't supported, don't error - just
112 if (! ath_hal_spectral_supported(sc->sc_ah))
115 ss = malloc(sizeof(struct ath_spectral_state),
116 M_TEMP, M_WAITOK | M_ZERO);
119 device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
124 sc->sc_spectral = ss;
126 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
132 * Detach spectral from the given interface
135 ath_spectral_detach(struct ath_softc *sc)
138 if (! ath_hal_spectral_supported(sc->sc_ah))
141 if (sc->sc_spectral != NULL) {
142 free(sc->sc_spectral, M_TEMP);
148 * Check whether spectral needs enabling and if so,
152 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
154 struct ath_spectral_state *ss = sc->sc_spectral;
156 /* Default to disable spectral PHY reporting */
157 sc->sc_dospectral = 0;
162 if (ss->spectral_enable_after_reset) {
163 ath_hal_spectral_configure(sc->sc_ah,
164 &ss->spectral_state);
165 (void) ath_hal_spectral_start(sc->sc_ah);
166 sc->sc_dospectral = 1;
172 * Handle ioctl requests from the diagnostic interface.
174 * The initial part of this code resembles ath_ioctl_diag();
175 * it's likely a good idea to reduce duplication between
176 * these two routines.
179 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
181 unsigned int id = ad->ad_id & ATH_DIAG_ID;
183 void *outdata = NULL;
184 u_int32_t insize = ad->ad_in_size;
185 u_int32_t outsize = ad->ad_out_size;
187 HAL_SPECTRAL_PARAM peout;
188 HAL_SPECTRAL_PARAM *pe;
189 struct ath_spectral_state *ss = sc->sc_spectral;
192 if (! ath_hal_spectral_supported(sc->sc_ah))
196 ath_power_set_power_state(sc, HAL_PM_AWAKE);
199 if (ad->ad_id & ATH_DIAG_IN) {
203 indata = malloc(insize, M_TEMP, M_NOWAIT);
204 if (indata == NULL) {
208 error = copyin(ad->ad_in_data, indata, insize);
212 if (ad->ad_id & ATH_DIAG_DYN) {
214 * Allocate a buffer for the results (otherwise the HAL
215 * returns a pointer to a buffer where we can read the
216 * results). Note that we depend on the HAL leaving this
217 * pointer for us to use below in reclaiming the buffer;
218 * may want to be more defensive.
220 outdata = malloc(outsize, M_TEMP, M_NOWAIT);
221 if (outdata == NULL) {
227 case SPECTRAL_CONTROL_GET_PARAMS:
228 memset(&peout, 0, sizeof(peout));
229 outsize = sizeof(HAL_SPECTRAL_PARAM);
230 ath_hal_spectral_get_config(sc->sc_ah, &peout);
231 pe = (HAL_SPECTRAL_PARAM *) outdata;
232 memcpy(pe, &peout, sizeof(*pe));
234 case SPECTRAL_CONTROL_SET_PARAMS:
235 if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
239 pe = (HAL_SPECTRAL_PARAM *) indata;
240 ath_hal_spectral_configure(sc->sc_ah, pe);
241 /* Save a local copy of the updated parameters */
242 ath_hal_spectral_get_config(sc->sc_ah,
243 &ss->spectral_state);
245 case SPECTRAL_CONTROL_START:
246 ath_hal_spectral_configure(sc->sc_ah,
247 &ss->spectral_state);
248 (void) ath_hal_spectral_start(sc->sc_ah);
249 sc->sc_dospectral = 1;
250 /* XXX need to update the PHY mask in the driver */
252 case SPECTRAL_CONTROL_STOP:
253 (void) ath_hal_spectral_stop(sc->sc_ah);
254 sc->sc_dospectral = 0;
255 /* XXX need to update the PHY mask in the driver */
257 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
258 if (insize < sizeof(int)) {
259 device_printf(sc->sc_dev, "%d != %d\n",
265 if (indata == NULL) {
266 device_printf(sc->sc_dev, "indata=NULL\n");
270 val = * ((int *) indata);
272 ss->spectral_enable_after_reset = 0;
274 ss->spectral_enable_after_reset = 1;
276 case SPECTRAL_CONTROL_ENABLE:
278 case SPECTRAL_CONTROL_DISABLE:
284 if (outsize < ad->ad_out_size)
285 ad->ad_out_size = outsize;
286 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
289 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
290 free(indata, M_TEMP);
291 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
292 free(outdata, M_TEMP);
294 ath_power_restore_power_state(sc);