xref: /freebsd/sys/dev/ath/if_ath_spectral.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
16  * NO WARRANTY
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.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * Implement some basic spectral scan control logic.
36  */
37 #include "opt_ath.h"
38 #include "opt_inet.h"
39 #include "opt_wlan.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/errno.h>
49 
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/bus.h>
53 
54 #include <sys/socket.h>
55 
56 #include <net/if.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 */
61 
62 #include <net80211/ieee80211_var.h>
63 
64 #include <net/bpf.h>
65 
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #endif
70 
71 #include <dev/ath/if_athvar.h>
72 #include <dev/ath/if_ath_spectral.h>
73 
74 #include <dev/ath/ath_hal/ah_desc.h>
75 
76 struct ath_spectral_state {
77 	HAL_SPECTRAL_PARAM	spectral_state;
78 
79 	/*
80 	 * Should we enable spectral scan upon
81 	 * each network interface reset/change?
82 	 *
83 	 * This is intended to allow spectral scan
84 	 * frame reporting during channel scans.
85 	 *
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
90 	 * config.
91 	 */
92 	int spectral_enable_after_reset;
93 };
94 
95 /*
96  * Methods which are required
97  */
98 
99 /*
100  * Attach spectral to the given interface
101  */
102 int
103 ath_spectral_attach(struct ath_softc *sc)
104 {
105 	struct ath_spectral_state *ss;
106 
107 	/*
108 	 * If spectral isn't supported, don't error - just
109 	 * quietly complete.
110 	 */
111 	if (! ath_hal_spectral_supported(sc->sc_ah))
112 		return (0);
113 
114 	ss = malloc(sizeof(struct ath_spectral_state),
115 	    M_TEMP, M_WAITOK | M_ZERO);
116 
117 	if (ss == NULL) {
118 		device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
119 		    __func__);
120 		return (-ENOMEM);
121 	}
122 
123 	sc->sc_spectral = ss;
124 
125 	(void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
126 
127 	return (0);
128 }
129 
130 /*
131  * Detach spectral from the given interface
132  */
133 int
134 ath_spectral_detach(struct ath_softc *sc)
135 {
136 
137 	if (! ath_hal_spectral_supported(sc->sc_ah))
138 		return (0);
139 
140 	if (sc->sc_spectral != NULL) {
141 		free(sc->sc_spectral, M_TEMP);
142 	}
143 	return (0);
144 }
145 
146 /*
147  * Check whether spectral needs enabling and if so,
148  * flip it on.
149  */
150 int
151 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
152 {
153 	struct ath_spectral_state *ss = sc->sc_spectral;
154 
155 	/* Default to disable spectral PHY reporting */
156 	sc->sc_dospectral = 0;
157 
158 	if (ss == NULL)
159 		return (0);
160 
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;
166 	}
167 	return (0);
168 }
169 
170 /*
171  * Handle ioctl requests from the diagnostic interface.
172  *
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.
176  */
177 int
178 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
179 {
180 	unsigned int id = ad->ad_id & ATH_DIAG_ID;
181 	void *indata = NULL;
182 	void *outdata = NULL;
183 	u_int32_t insize = ad->ad_in_size;
184 	u_int32_t outsize = ad->ad_out_size;
185 	int error = 0;
186 	HAL_SPECTRAL_PARAM peout;
187 	HAL_SPECTRAL_PARAM *pe;
188 	struct ath_spectral_state *ss = sc->sc_spectral;
189 	int val;
190 
191 	if (! ath_hal_spectral_supported(sc->sc_ah))
192 		return (EINVAL);
193 
194 	if (ad->ad_id & ATH_DIAG_IN) {
195 		/*
196 		 * Copy in data.
197 		 */
198 		indata = malloc(insize, M_TEMP, M_NOWAIT);
199 		if (indata == NULL) {
200 			error = ENOMEM;
201 			goto bad;
202 		}
203 		error = copyin(ad->ad_in_data, indata, insize);
204 		if (error)
205 			goto bad;
206 	}
207 	if (ad->ad_id & ATH_DIAG_DYN) {
208 		/*
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.
214 		 */
215 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
216 		if (outdata == NULL) {
217 			error = ENOMEM;
218 			goto bad;
219 		}
220 	}
221 	switch (id) {
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));
228 			break;
229 		case SPECTRAL_CONTROL_SET_PARAMS:
230 			if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
231 				error = EINVAL;
232 				break;
233 			}
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);
239 			break;
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 */
246 			break;
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 */
251 			break;
252 		case SPECTRAL_CONTROL_ENABLE_AT_RESET:
253 			if (insize < sizeof(int)) {
254 				device_printf(sc->sc_dev, "%d != %d\n",
255 				    insize,
256 				    (int) sizeof(int));
257 				error = EINVAL;
258 				break;
259 			}
260 			if (indata == NULL) {
261 				device_printf(sc->sc_dev, "indata=NULL\n");
262 				error = EINVAL;
263 				break;
264 			}
265 			val = * ((int *) indata);
266 			if (val == 0)
267 				ss->spectral_enable_after_reset = 0;
268 			else
269 				ss->spectral_enable_after_reset = 1;
270 			break;
271 		case SPECTRAL_CONTROL_ENABLE:
272 			/* XXX TODO */
273 		case SPECTRAL_CONTROL_DISABLE:
274 			/* XXX TODO */
275 		break;
276 		default:
277 			error = EINVAL;
278 	}
279 	if (outsize < ad->ad_out_size)
280 		ad->ad_out_size = outsize;
281 	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
282 		error = EFAULT;
283 bad:
284 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
285 		free(indata, M_TEMP);
286 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
287 		free(outdata, M_TEMP);
288 	return (error);
289 }
290 
291