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, MERCHANTABILITY
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/errno.h>
48 
49 #if defined(__DragonFly__)
50 /* empty */
51 #else
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #endif
55 #include <sys/bus.h>
56 
57 #include <sys/socket.h>
58 
59 #include <net/if.h>
60 #include <net/if_var.h>
61 #include <net/if_media.h>
62 #include <net/if_arp.h>
63 #include <net/ethernet.h>		/* XXX for ether_sprintf */
64 
65 #include <netproto/802_11/ieee80211_var.h>
66 
67 #include <net/bpf.h>
68 
69 #ifdef INET
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #endif
73 
74 #include <dev/netif/ath/ath/if_athvar.h>
75 #include <dev/netif/ath/ath/if_ath_spectral.h>
76 
77 #include <dev/netif/ath/ath_hal/ah_desc.h>
78 
79 struct ath_spectral_state {
80 	HAL_SPECTRAL_PARAM	spectral_state;
81 
82 	/*
83 	 * Should we enable spectral scan upon
84 	 * each network interface reset/change?
85 	 *
86 	 * This is intended to allow spectral scan
87 	 * frame reporting during channel scans.
88 	 *
89 	 * Later on it can morph into a larger
90 	 * scale config method where it pushes
91 	 * a "channel scan" config into the hardware
92 	 * rather than just the spectral_state
93 	 * config.
94 	 */
95 	int spectral_enable_after_reset;
96 };
97 
98 /*
99  * Methods which are required
100  */
101 
102 /*
103  * Attach spectral to the given interface
104  */
105 int
106 ath_spectral_attach(struct ath_softc *sc)
107 {
108 	struct ath_spectral_state *ss;
109 
110 	/*
111 	 * If spectral isn't supported, don't error - just
112 	 * quietly complete.
113 	 */
114 	if (! ath_hal_spectral_supported(sc->sc_ah))
115 		return (0);
116 
117 	ss = kmalloc(sizeof(struct ath_spectral_state),
118 	    M_TEMP, M_WAITOK | M_ZERO);
119 
120 	if (ss == NULL) {
121 		device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
122 		    __func__);
123 		return (-ENOMEM);
124 	}
125 
126 	sc->sc_spectral = ss;
127 
128 	(void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
129 
130 	return (0);
131 }
132 
133 /*
134  * Detach spectral from the given interface
135  */
136 int
137 ath_spectral_detach(struct ath_softc *sc)
138 {
139 
140 	if (! ath_hal_spectral_supported(sc->sc_ah))
141 		return (0);
142 
143 	if (sc->sc_spectral != NULL) {
144 		kfree(sc->sc_spectral, M_TEMP);
145 	}
146 	return (0);
147 }
148 
149 /*
150  * Check whether spectral needs enabling and if so,
151  * flip it on.
152  */
153 int
154 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
155 {
156 	struct ath_spectral_state *ss = sc->sc_spectral;
157 
158 	/* Default to disable spectral PHY reporting */
159 	sc->sc_dospectral = 0;
160 
161 	if (ss == NULL)
162 		return (0);
163 
164 	if (ss->spectral_enable_after_reset) {
165 		ath_hal_spectral_configure(sc->sc_ah,
166 		    &ss->spectral_state);
167 		(void) ath_hal_spectral_start(sc->sc_ah);
168 		sc->sc_dospectral = 1;
169 	}
170 	return (0);
171 }
172 
173 /*
174  * Handle ioctl requests from the diagnostic interface.
175  *
176  * The initial part of this code resembles ath_ioctl_diag();
177  * it's likely a good idea to reduce duplication between
178  * these two routines.
179  */
180 int
181 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
182 {
183 	unsigned int id = ad->ad_id & ATH_DIAG_ID;
184 	void *indata = NULL;
185 	void *outdata = NULL;
186 	u_int32_t insize = ad->ad_in_size;
187 	u_int32_t outsize = ad->ad_out_size;
188 	int error = 0;
189 	HAL_SPECTRAL_PARAM peout;
190 	HAL_SPECTRAL_PARAM *pe;
191 	struct ath_spectral_state *ss = sc->sc_spectral;
192 	int val;
193 
194 	if (! ath_hal_spectral_supported(sc->sc_ah))
195 		return (EINVAL);
196 
197 	if (ad->ad_id & ATH_DIAG_IN) {
198 		/*
199 		 * Copy in data.
200 		 */
201 		indata = kmalloc(insize, M_TEMP, M_INTWAIT);
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 = kmalloc(outsize, M_TEMP, M_INTWAIT);
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 	}
282 	if (outsize < ad->ad_out_size)
283 		ad->ad_out_size = outsize;
284 	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
285 		error = EFAULT;
286 bad:
287 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
288 		kfree(indata, M_TEMP);
289 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
290 		kfree(outdata, M_TEMP);
291 	return (error);
292 }
293 
294