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 <machine/resource.h>
68 #include <dev/netif/ath/ath/if_athvar.h>
69 #include <dev/netif/ath/ath/if_ath_spectral.h>
71 #include <dev/netif/ath/ath_hal/ah_desc.h>
73 struct ath_spectral_state {
74 HAL_SPECTRAL_PARAM spectral_state;
77 * Should we enable spectral scan upon
78 * each network interface reset/change?
80 * This is intended to allow spectral scan
81 * frame reporting during channel scans.
83 * Later on it can morph into a larger
84 * scale config method where it pushes
85 * a "channel scan" config into the hardware
86 * rather than just the spectral_state
89 int spectral_enable_after_reset;
93 * Methods which are required
97 * Attach spectral to the given interface
100 ath_spectral_attach(struct ath_softc *sc)
102 struct ath_spectral_state *ss;
105 * If spectral isn't supported, don't error - just
108 if (! ath_hal_spectral_supported(sc->sc_ah))
111 ss = malloc(sizeof(struct ath_spectral_state),
112 M_TEMP, M_WAITOK | M_ZERO);
115 device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
120 sc->sc_spectral = ss;
122 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
128 * Detach spectral from the given interface
131 ath_spectral_detach(struct ath_softc *sc)
134 if (! ath_hal_spectral_supported(sc->sc_ah))
137 if (sc->sc_spectral != NULL) {
138 free(sc->sc_spectral, M_TEMP);
144 * Check whether spectral needs enabling and if so,
148 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
150 struct ath_spectral_state *ss = sc->sc_spectral;
152 /* Default to disable spectral PHY reporting */
153 sc->sc_dospectral = 0;
158 if (ss->spectral_enable_after_reset) {
159 ath_hal_spectral_configure(sc->sc_ah,
160 &ss->spectral_state);
161 (void) ath_hal_spectral_start(sc->sc_ah);
162 sc->sc_dospectral = 1;
168 * Handle ioctl requests from the diagnostic interface.
170 * The initial part of this code resembles ath_ioctl_diag();
171 * it's likely a good idea to reduce duplication between
172 * these two routines.
175 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
177 unsigned int id = ad->ad_id & ATH_DIAG_ID;
179 void *outdata = NULL;
180 u_int32_t insize = ad->ad_in_size;
181 u_int32_t outsize = ad->ad_out_size;
183 HAL_SPECTRAL_PARAM peout;
184 HAL_SPECTRAL_PARAM *pe;
185 struct ath_spectral_state *ss = sc->sc_spectral;
188 if (! ath_hal_spectral_supported(sc->sc_ah))
191 if (ad->ad_id & ATH_DIAG_IN) {
195 indata = malloc(insize, M_TEMP, M_NOWAIT);
196 if (indata == NULL) {
200 error = copyin(ad->ad_in_data, indata, insize);
204 if (ad->ad_id & ATH_DIAG_DYN) {
206 * Allocate a buffer for the results (otherwise the HAL
207 * returns a pointer to a buffer where we can read the
208 * results). Note that we depend on the HAL leaving this
209 * pointer for us to use below in reclaiming the buffer;
210 * may want to be more defensive.
212 outdata = malloc(outsize, M_TEMP, M_NOWAIT);
213 if (outdata == NULL) {
219 case SPECTRAL_CONTROL_GET_PARAMS:
220 memset(&peout, 0, sizeof(peout));
221 outsize = sizeof(HAL_SPECTRAL_PARAM);
222 ath_hal_spectral_get_config(sc->sc_ah, &peout);
223 pe = (HAL_SPECTRAL_PARAM *) outdata;
224 memcpy(pe, &peout, sizeof(*pe));
226 case SPECTRAL_CONTROL_SET_PARAMS:
227 if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
231 pe = (HAL_SPECTRAL_PARAM *) indata;
232 ath_hal_spectral_configure(sc->sc_ah, pe);
233 /* Save a local copy of the updated parameters */
234 ath_hal_spectral_get_config(sc->sc_ah,
235 &ss->spectral_state);
237 case SPECTRAL_CONTROL_START:
238 ath_hal_spectral_configure(sc->sc_ah,
239 &ss->spectral_state);
240 (void) ath_hal_spectral_start(sc->sc_ah);
241 sc->sc_dospectral = 1;
242 /* XXX need to update the PHY mask in the driver */
244 case SPECTRAL_CONTROL_STOP:
245 (void) ath_hal_spectral_stop(sc->sc_ah);
246 sc->sc_dospectral = 0;
247 /* XXX need to update the PHY mask in the driver */
249 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
250 if (insize < sizeof(int)) {
251 device_printf(sc->sc_dev, "%d != %d\n",
257 if (indata == NULL) {
258 device_printf(sc->sc_dev, "indata=NULL\n");
262 val = * ((int *) indata);
264 ss->spectral_enable_after_reset = 0;
266 ss->spectral_enable_after_reset = 1;
268 case SPECTRAL_CONTROL_ENABLE:
270 case SPECTRAL_CONTROL_DISABLE:
276 if (outsize < ad->ad_out_size)
277 ad->ad_out_size = outsize;
278 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
281 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
282 free(indata, M_TEMP);
283 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
284 free(outdata, M_TEMP);