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 <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 <netproto/802_11/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/netif/ath/ath/if_athvar.h>
70 #include <dev/netif/ath/ath/if_ath_spectral.h>
71 
72 #include <dev/netif/ath/ath_hal/ah_desc.h>
73 
74 struct ath_spectral_state {
75 	HAL_SPECTRAL_PARAM	spectral_state;
76 
77 	/*
78 	 * Should we enable spectral scan upon
79 	 * each network interface reset/change?
80 	 *
81 	 * This is intended to allow spectral scan
82 	 * frame reporting during channel scans.
83 	 *
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
88 	 * config.
89 	 */
90 	int spectral_enable_after_reset;
91 };
92 
93 /*
94  * Methods which are required
95  */
96 
97 /*
98  * Attach spectral to the given interface
99  */
100 int
101 ath_spectral_attach(struct ath_softc *sc)
102 {
103 	struct ath_spectral_state *ss;
104 
105 	/*
106 	 * If spectral isn't supported, don't error - just
107 	 * quietly complete.
108 	 */
109 	if (! ath_hal_spectral_supported(sc->sc_ah))
110 		return (0);
111 
112 	ss = kmalloc(sizeof(struct ath_spectral_state),
113 	    M_TEMP, M_WAITOK | M_ZERO);
114 
115 	if (ss == NULL) {
116 		device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
117 		    __func__);
118 		return (-ENOMEM);
119 	}
120 
121 	sc->sc_spectral = ss;
122 
123 	(void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
124 
125 	return (0);
126 }
127 
128 /*
129  * Detach spectral from the given interface
130  */
131 int
132 ath_spectral_detach(struct ath_softc *sc)
133 {
134 
135 	if (! ath_hal_spectral_supported(sc->sc_ah))
136 		return (0);
137 
138 	if (sc->sc_spectral != NULL) {
139 		kfree(sc->sc_spectral, M_TEMP);
140 	}
141 	return (0);
142 }
143 
144 /*
145  * Check whether spectral needs enabling and if so,
146  * flip it on.
147  */
148 int
149 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
150 {
151 	struct ath_spectral_state *ss = sc->sc_spectral;
152 
153 	/* Default to disable spectral PHY reporting */
154 	sc->sc_dospectral = 0;
155 
156 	if (ss == NULL)
157 		return (0);
158 
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;
164 	}
165 	return (0);
166 }
167 
168 /*
169  * Handle ioctl requests from the diagnostic interface.
170  *
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.
174  */
175 int
176 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
177 {
178 	unsigned int id = ad->ad_id & ATH_DIAG_ID;
179 	void *indata = NULL;
180 	void *outdata = NULL;
181 	u_int32_t insize = ad->ad_in_size;
182 	u_int32_t outsize = ad->ad_out_size;
183 	int error = 0;
184 	HAL_SPECTRAL_PARAM peout;
185 	HAL_SPECTRAL_PARAM *pe;
186 	struct ath_spectral_state *ss = sc->sc_spectral;
187 	int val;
188 
189 	if (! ath_hal_spectral_supported(sc->sc_ah))
190 		return (EINVAL);
191 
192 	if (ad->ad_id & ATH_DIAG_IN) {
193 		/*
194 		 * Copy in data.
195 		 */
196 		indata = kmalloc(insize, M_TEMP, M_INTWAIT);
197 		if (indata == NULL) {
198 			error = ENOMEM;
199 			goto bad;
200 		}
201 		error = copyin(ad->ad_in_data, indata, insize);
202 		if (error)
203 			goto bad;
204 	}
205 	if (ad->ad_id & ATH_DIAG_DYN) {
206 		/*
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.
212 		 */
213 		outdata = kmalloc(outsize, M_TEMP, M_INTWAIT);
214 		if (outdata == NULL) {
215 			error = ENOMEM;
216 			goto bad;
217 		}
218 	}
219 	switch (id) {
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));
226 			break;
227 		case SPECTRAL_CONTROL_SET_PARAMS:
228 			if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
229 				error = EINVAL;
230 				break;
231 			}
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);
237 			break;
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 */
244 			break;
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 */
249 			break;
250 		case SPECTRAL_CONTROL_ENABLE_AT_RESET:
251 			if (insize < sizeof(int)) {
252 				device_printf(sc->sc_dev, "%d != %d\n",
253 				    insize,
254 				    (int) sizeof(int));
255 				error = EINVAL;
256 				break;
257 			}
258 			if (indata == NULL) {
259 				device_printf(sc->sc_dev, "indata=NULL\n");
260 				error = EINVAL;
261 				break;
262 			}
263 			val = * ((int *) indata);
264 			if (val == 0)
265 				ss->spectral_enable_after_reset = 0;
266 			else
267 				ss->spectral_enable_after_reset = 1;
268 			break;
269 		case SPECTRAL_CONTROL_ENABLE:
270 			/* XXX TODO */
271 		case SPECTRAL_CONTROL_DISABLE:
272 			/* XXX TODO */
273 		break;
274 		default:
275 			error = EINVAL;
276 	}
277 	if (outsize < ad->ad_out_size)
278 		ad->ad_out_size = outsize;
279 	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
280 		error = EFAULT;
281 bad:
282 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
283 		kfree(indata, M_TEMP);
284 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
285 		kfree(outdata, M_TEMP);
286 	return (error);
287 }
288 
289