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>
34 * Implement some basic spectral scan control logic.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/errno.h>
49 #include <sys/socket.h>
52 #include <net/if_var.h>
53 #include <net/if_media.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h> /* XXX for ether_sprintf */
57 #include <netproto/802_11/ieee80211_var.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
66 #include <dev/netif/ath/ath/if_athvar.h>
67 #include <dev/netif/ath/ath/if_ath_spectral.h>
69 #include <dev/netif/ath/ath_hal/ah_desc.h>
71 struct ath_spectral_state {
72 HAL_SPECTRAL_PARAM spectral_state;
75 * Should we enable spectral scan upon
76 * each network interface reset/change?
78 * This is intended to allow spectral scan
79 * frame reporting during channel scans.
81 * Later on it can morph into a larger
82 * scale config method where it pushes
83 * a "channel scan" config into the hardware
84 * rather than just the spectral_state
87 int spectral_enable_after_reset;
91 * Methods which are required
95 * Attach spectral to the given interface
98 ath_spectral_attach(struct ath_softc *sc)
100 struct ath_spectral_state *ss;
103 * If spectral isn't supported, don't error - just
106 if (! ath_hal_spectral_supported(sc->sc_ah))
109 ss = kmalloc(sizeof(struct ath_spectral_state),
110 M_TEMP, M_WAITOK | M_ZERO);
113 device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
118 sc->sc_spectral = ss;
120 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
126 * Detach spectral from the given interface
129 ath_spectral_detach(struct ath_softc *sc)
132 if (! ath_hal_spectral_supported(sc->sc_ah))
135 if (sc->sc_spectral != NULL) {
136 kfree(sc->sc_spectral, M_TEMP);
142 * Check whether spectral needs enabling and if so,
146 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
148 struct ath_spectral_state *ss = sc->sc_spectral;
150 /* Default to disable spectral PHY reporting */
151 sc->sc_dospectral = 0;
156 if (ss->spectral_enable_after_reset) {
157 ath_hal_spectral_configure(sc->sc_ah,
158 &ss->spectral_state);
159 (void) ath_hal_spectral_start(sc->sc_ah);
160 sc->sc_dospectral = 1;
166 * Handle ioctl requests from the diagnostic interface.
168 * The initial part of this code resembles ath_ioctl_diag();
169 * it's likely a good idea to reduce duplication between
170 * these two routines.
173 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
175 unsigned int id = ad->ad_id & ATH_DIAG_ID;
177 void *outdata = NULL;
178 u_int32_t insize = ad->ad_in_size;
179 u_int32_t outsize = ad->ad_out_size;
181 HAL_SPECTRAL_PARAM peout;
182 HAL_SPECTRAL_PARAM *pe;
183 struct ath_spectral_state *ss = sc->sc_spectral;
186 if (! ath_hal_spectral_supported(sc->sc_ah))
189 if (ad->ad_id & ATH_DIAG_IN) {
193 indata = kmalloc(insize, M_TEMP, M_INTWAIT);
194 if (indata == NULL) {
198 error = copyin(ad->ad_in_data, indata, insize);
202 if (ad->ad_id & ATH_DIAG_DYN) {
204 * Allocate a buffer for the results (otherwise the HAL
205 * returns a pointer to a buffer where we can read the
206 * results). Note that we depend on the HAL leaving this
207 * pointer for us to use below in reclaiming the buffer;
208 * may want to be more defensive.
210 outdata = kmalloc(outsize, M_TEMP, M_INTWAIT);
211 if (outdata == NULL) {
217 case SPECTRAL_CONTROL_GET_PARAMS:
218 memset(&peout, 0, sizeof(peout));
219 outsize = sizeof(HAL_SPECTRAL_PARAM);
220 ath_hal_spectral_get_config(sc->sc_ah, &peout);
221 pe = (HAL_SPECTRAL_PARAM *) outdata;
222 memcpy(pe, &peout, sizeof(*pe));
224 case SPECTRAL_CONTROL_SET_PARAMS:
225 if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
229 pe = (HAL_SPECTRAL_PARAM *) indata;
230 ath_hal_spectral_configure(sc->sc_ah, pe);
231 /* Save a local copy of the updated parameters */
232 ath_hal_spectral_get_config(sc->sc_ah,
233 &ss->spectral_state);
235 case SPECTRAL_CONTROL_START:
236 ath_hal_spectral_configure(sc->sc_ah,
237 &ss->spectral_state);
238 (void) ath_hal_spectral_start(sc->sc_ah);
239 sc->sc_dospectral = 1;
240 /* XXX need to update the PHY mask in the driver */
242 case SPECTRAL_CONTROL_STOP:
243 (void) ath_hal_spectral_stop(sc->sc_ah);
244 sc->sc_dospectral = 0;
245 /* XXX need to update the PHY mask in the driver */
247 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
248 if (insize < sizeof(int)) {
249 device_printf(sc->sc_dev, "%d != %d\n",
255 if (indata == NULL) {
256 device_printf(sc->sc_dev, "indata=NULL\n");
260 val = * ((int *) indata);
262 ss->spectral_enable_after_reset = 0;
264 ss->spectral_enable_after_reset = 1;
266 case SPECTRAL_CONTROL_ENABLE:
268 case SPECTRAL_CONTROL_DISABLE:
274 if (outsize < ad->ad_out_size)
275 ad->ad_out_size = outsize;
276 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
279 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
280 kfree(indata, M_TEMP);
281 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
282 kfree(outdata, M_TEMP);