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/mutex.h>
47 #include <sys/errno.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
53 #include <sys/socket.h>
56 #include <net/if_media.h>
57 #include <net/if_arp.h>
58 #include <net/ethernet.h> /* XXX for ether_sprintf */
60 #include <net80211/ieee80211_var.h>
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
69 #include <dev/ath/if_athvar.h>
70 #include <dev/ath/if_ath_spectral.h>
72 #include <dev/ath/ath_hal/ah_desc.h>
74 struct ath_spectral_state {
75 HAL_SPECTRAL_PARAM spectral_state;
78 * Should we enable spectral scan upon
79 * each network interface reset/change?
81 * This is intended to allow spectral scan
82 * frame reporting during channel scans.
84 * Later on it can morph into a larger
85 * scale config method where it pushes
86 * a "channel scan" config into the hardware
87 * rather than just the spectral_state
90 int spectral_enable_after_reset;
94 * Methods which are required
98 * Attach spectral to the given interface
101 ath_spectral_attach(struct ath_softc *sc)
103 struct ath_spectral_state *ss;
106 * If spectral isn't supported, don't error - just
109 if (! ath_hal_spectral_supported(sc->sc_ah))
112 ss = malloc(sizeof(struct ath_spectral_state),
113 M_TEMP, M_WAITOK | M_ZERO);
116 device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
121 sc->sc_spectral = ss;
123 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
129 * Detach spectral from the given interface
132 ath_spectral_detach(struct ath_softc *sc)
135 if (! ath_hal_spectral_supported(sc->sc_ah))
138 if (sc->sc_spectral != NULL) {
139 free(sc->sc_spectral, M_TEMP);
145 * Check whether spectral needs enabling and if so,
149 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
151 struct ath_spectral_state *ss = sc->sc_spectral;
153 /* Default to disable spectral PHY reporting */
154 sc->sc_dospectral = 0;
159 if (ss->spectral_enable_after_reset) {
160 ath_hal_spectral_configure(sc->sc_ah,
161 &ss->spectral_state);
162 (void) ath_hal_spectral_start(sc->sc_ah);
163 sc->sc_dospectral = 1;
169 * Handle ioctl requests from the diagnostic interface.
171 * The initial part of this code resembles ath_ioctl_diag();
172 * it's likely a good idea to reduce duplication between
173 * these two routines.
176 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
178 unsigned int id = ad->ad_id & ATH_DIAG_ID;
180 void *outdata = NULL;
181 u_int32_t insize = ad->ad_in_size;
182 u_int32_t outsize = ad->ad_out_size;
184 HAL_SPECTRAL_PARAM peout;
185 HAL_SPECTRAL_PARAM *pe;
186 struct ath_spectral_state *ss = sc->sc_spectral;
189 if (! ath_hal_spectral_supported(sc->sc_ah))
192 if (ad->ad_id & ATH_DIAG_IN) {
196 indata = malloc(insize, M_TEMP, M_NOWAIT);
197 if (indata == NULL) {
201 error = copyin(ad->ad_in_data, indata, insize);
205 if (ad->ad_id & ATH_DIAG_DYN) {
207 * Allocate a buffer for the results (otherwise the HAL
208 * returns a pointer to a buffer where we can read the
209 * results). Note that we depend on the HAL leaving this
210 * pointer for us to use below in reclaiming the buffer;
211 * may want to be more defensive.
213 outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
214 if (outdata == NULL) {
220 case SPECTRAL_CONTROL_GET_PARAMS:
221 memset(&peout, 0, sizeof(peout));
222 outsize = sizeof(HAL_SPECTRAL_PARAM);
223 ath_hal_spectral_get_config(sc->sc_ah, &peout);
224 pe = (HAL_SPECTRAL_PARAM *) outdata;
225 memcpy(pe, &peout, sizeof(*pe));
227 case SPECTRAL_CONTROL_SET_PARAMS:
228 if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
232 pe = (HAL_SPECTRAL_PARAM *) indata;
233 ath_hal_spectral_configure(sc->sc_ah, pe);
234 /* Save a local copy of the updated parameters */
235 ath_hal_spectral_get_config(sc->sc_ah,
236 &ss->spectral_state);
238 case SPECTRAL_CONTROL_START:
239 ath_hal_spectral_configure(sc->sc_ah,
240 &ss->spectral_state);
241 (void) ath_hal_spectral_start(sc->sc_ah);
242 sc->sc_dospectral = 1;
243 /* XXX need to update the PHY mask in the driver */
245 case SPECTRAL_CONTROL_STOP:
246 (void) ath_hal_spectral_stop(sc->sc_ah);
247 sc->sc_dospectral = 0;
248 /* XXX need to update the PHY mask in the driver */
250 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
251 if (insize < sizeof(int)) {
252 device_printf(sc->sc_dev, "%d != %d\n",
258 if (indata == NULL) {
259 device_printf(sc->sc_dev, "indata=NULL\n");
263 val = * ((int *) indata);
265 ss->spectral_enable_after_reset = 0;
267 ss->spectral_enable_after_reset = 1;
269 case SPECTRAL_CONTROL_ENABLE:
271 case SPECTRAL_CONTROL_DISABLE:
278 if (outsize < ad->ad_out_size)
279 ad->ad_out_size = outsize;
280 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
283 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
284 free(indata, M_TEMP);
285 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
286 free(outdata, M_TEMP);