xref: /freebsd/sys/dev/ath/if_ath_spectral.c (revision 685dc743)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 #include <sys/cdefs.h>
32 /*
33  * Implement some basic spectral scan control logic.
34  */
35 #include "opt_ath.h"
36 #include "opt_inet.h"
37 #include "opt_wlan.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/errno.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/bus.h>
51 
52 #include <sys/socket.h>
53 
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_media.h>
57 #include <net/if_arp.h>
58 #include <net/ethernet.h>		/* XXX for ether_sprintf */
59 
60 #include <net80211/ieee80211_var.h>
61 
62 #include <net/bpf.h>
63 
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #endif
68 
69 #include <dev/ath/if_athvar.h>
70 #include <dev/ath/if_ath_spectral.h>
71 #include <dev/ath/if_ath_misc.h>
72 
73 #include <dev/ath/ath_hal/ah_desc.h>
74 
75 struct ath_spectral_state {
76 	HAL_SPECTRAL_PARAM	spectral_state;
77 
78 	/*
79 	 * Should we enable spectral scan upon
80 	 * each network interface reset/change?
81 	 *
82 	 * This is intended to allow spectral scan
83 	 * frame reporting during channel scans.
84 	 *
85 	 * Later on it can morph into a larger
86 	 * scale config method where it pushes
87 	 * a "channel scan" config into the hardware
88 	 * rather than just the spectral_state
89 	 * config.
90 	 */
91 	int spectral_enable_after_reset;
92 };
93 
94 /*
95  * Methods which are required
96  */
97 
98 /*
99  * Attach spectral to the given interface
100  */
101 int
ath_spectral_attach(struct ath_softc * sc)102 ath_spectral_attach(struct ath_softc *sc)
103 {
104 	struct ath_spectral_state *ss;
105 
106 	/*
107 	 * If spectral isn't supported, don't error - just
108 	 * quietly complete.
109 	 */
110 	if (! ath_hal_spectral_supported(sc->sc_ah))
111 		return (0);
112 
113 	ss = malloc(sizeof(struct ath_spectral_state),
114 	    M_TEMP, M_WAITOK | M_ZERO);
115 
116 	if (ss == NULL) {
117 		device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
118 		    __func__);
119 		return (-ENOMEM);
120 	}
121 
122 	sc->sc_spectral = ss;
123 
124 	(void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
125 
126 	return (0);
127 }
128 
129 /*
130  * Detach spectral from the given interface
131  */
132 int
ath_spectral_detach(struct ath_softc * sc)133 ath_spectral_detach(struct ath_softc *sc)
134 {
135 
136 	if (! ath_hal_spectral_supported(sc->sc_ah))
137 		return (0);
138 
139 	if (sc->sc_spectral != NULL) {
140 		free(sc->sc_spectral, M_TEMP);
141 	}
142 	return (0);
143 }
144 
145 /*
146  * Check whether spectral needs enabling and if so,
147  * flip it on.
148  */
149 int
ath_spectral_enable(struct ath_softc * sc,struct ieee80211_channel * ch)150 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
151 {
152 	struct ath_spectral_state *ss = sc->sc_spectral;
153 
154 	/* Default to disable spectral PHY reporting */
155 	sc->sc_dospectral = 0;
156 
157 	if (ss == NULL)
158 		return (0);
159 
160 	if (ss->spectral_enable_after_reset) {
161 		ath_hal_spectral_configure(sc->sc_ah,
162 		    &ss->spectral_state);
163 		(void) ath_hal_spectral_start(sc->sc_ah);
164 		sc->sc_dospectral = 1;
165 	}
166 	return (0);
167 }
168 
169 /*
170  * Handle ioctl requests from the diagnostic interface.
171  *
172  * The initial part of this code resembles ath_ioctl_diag();
173  * it's likely a good idea to reduce duplication between
174  * these two routines.
175  */
176 int
ath_ioctl_spectral(struct ath_softc * sc,struct ath_diag * ad)177 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
178 {
179 	unsigned int id = ad->ad_id & ATH_DIAG_ID;
180 	void *indata = NULL;
181 	void *outdata = NULL;
182 	u_int32_t insize = ad->ad_in_size;
183 	u_int32_t outsize = ad->ad_out_size;
184 	int error = 0;
185 	HAL_SPECTRAL_PARAM peout;
186 	HAL_SPECTRAL_PARAM *pe;
187 	struct ath_spectral_state *ss = sc->sc_spectral;
188 	int val;
189 
190 	if (! ath_hal_spectral_supported(sc->sc_ah))
191 		return (EINVAL);
192 
193 	ATH_LOCK(sc);
194 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
195 	ATH_UNLOCK(sc);
196 
197 	if (ad->ad_id & ATH_DIAG_IN) {
198 		/*
199 		 * Copy in data.
200 		 */
201 		indata = malloc(insize, M_TEMP, M_NOWAIT);
202 		if (indata == NULL) {
203 			error = ENOMEM;
204 			goto bad;
205 		}
206 		error = copyin(ad->ad_in_data, indata, insize);
207 		if (error)
208 			goto bad;
209 	}
210 	if (ad->ad_id & ATH_DIAG_DYN) {
211 		/*
212 		 * Allocate a buffer for the results (otherwise the HAL
213 		 * returns a pointer to a buffer where we can read the
214 		 * results).  Note that we depend on the HAL leaving this
215 		 * pointer for us to use below in reclaiming the buffer;
216 		 * may want to be more defensive.
217 		 */
218 		outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
219 		if (outdata == NULL) {
220 			error = ENOMEM;
221 			goto bad;
222 		}
223 	}
224 	switch (id) {
225 		case SPECTRAL_CONTROL_GET_PARAMS:
226 			memset(&peout, 0, sizeof(peout));
227 			outsize = sizeof(HAL_SPECTRAL_PARAM);
228 			ath_hal_spectral_get_config(sc->sc_ah, &peout);
229 			pe = (HAL_SPECTRAL_PARAM *) outdata;
230 			memcpy(pe, &peout, sizeof(*pe));
231 			break;
232 		case SPECTRAL_CONTROL_SET_PARAMS:
233 			if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
234 				error = EINVAL;
235 				break;
236 			}
237 			pe = (HAL_SPECTRAL_PARAM *) indata;
238 			ath_hal_spectral_configure(sc->sc_ah, pe);
239 			/* Save a local copy of the updated parameters */
240 			ath_hal_spectral_get_config(sc->sc_ah,
241 			    &ss->spectral_state);
242 			break;
243 		case SPECTRAL_CONTROL_START:
244 			ath_hal_spectral_configure(sc->sc_ah,
245 			    &ss->spectral_state);
246 			(void) ath_hal_spectral_start(sc->sc_ah);
247 			sc->sc_dospectral = 1;
248 			/* XXX need to update the PHY mask in the driver */
249 			break;
250 		case SPECTRAL_CONTROL_STOP:
251 			(void) ath_hal_spectral_stop(sc->sc_ah);
252 			sc->sc_dospectral = 0;
253 			/* XXX need to update the PHY mask in the driver */
254 			break;
255 		case SPECTRAL_CONTROL_ENABLE_AT_RESET:
256 			if (insize < sizeof(int)) {
257 				device_printf(sc->sc_dev, "%d != %d\n",
258 				    insize,
259 				    (int) sizeof(int));
260 				error = EINVAL;
261 				break;
262 			}
263 			if (indata == NULL) {
264 				device_printf(sc->sc_dev, "indata=NULL\n");
265 				error = EINVAL;
266 				break;
267 			}
268 			val = * ((int *) indata);
269 			if (val == 0)
270 				ss->spectral_enable_after_reset = 0;
271 			else
272 				ss->spectral_enable_after_reset = 1;
273 			break;
274 		case SPECTRAL_CONTROL_ENABLE:
275 			/* XXX TODO */
276 		case SPECTRAL_CONTROL_DISABLE:
277 			/* XXX TODO */
278 		break;
279 		default:
280 			error = EINVAL;
281 			goto bad;
282 	}
283 	if (outsize < ad->ad_out_size)
284 		ad->ad_out_size = outsize;
285 	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
286 		error = EFAULT;
287 bad:
288 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
289 		free(indata, M_TEMP);
290 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
291 		free(outdata, M_TEMP);
292 	ATH_LOCK(sc);
293 	ath_power_restore_power_state(sc);
294 	ATH_UNLOCK(sc);
295 
296 	return (error);
297 }
298