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 sc->sc_spectral = ss;
116
117 (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
118
119 return (0);
120 }
121
122 /*
123 * Detach spectral from the given interface
124 */
125 int
ath_spectral_detach(struct ath_softc * sc)126 ath_spectral_detach(struct ath_softc *sc)
127 {
128
129 if (! ath_hal_spectral_supported(sc->sc_ah))
130 return (0);
131
132 if (sc->sc_spectral != NULL) {
133 free(sc->sc_spectral, M_TEMP);
134 }
135 return (0);
136 }
137
138 /*
139 * Check whether spectral needs enabling and if so,
140 * flip it on.
141 */
142 int
ath_spectral_enable(struct ath_softc * sc,struct ieee80211_channel * ch)143 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
144 {
145 struct ath_spectral_state *ss = sc->sc_spectral;
146
147 /* Default to disable spectral PHY reporting */
148 sc->sc_dospectral = 0;
149
150 if (ss == NULL)
151 return (0);
152
153 if (ss->spectral_enable_after_reset) {
154 ath_hal_spectral_configure(sc->sc_ah,
155 &ss->spectral_state);
156 (void) ath_hal_spectral_start(sc->sc_ah);
157 sc->sc_dospectral = 1;
158 }
159 return (0);
160 }
161
162 /*
163 * Handle ioctl requests from the diagnostic interface.
164 *
165 * The initial part of this code resembles ath_ioctl_diag();
166 * it's likely a good idea to reduce duplication between
167 * these two routines.
168 */
169 int
ath_ioctl_spectral(struct ath_softc * sc,struct ath_diag * ad)170 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
171 {
172 unsigned int id = ad->ad_id & ATH_DIAG_ID;
173 void *indata = NULL;
174 void *outdata = NULL;
175 u_int32_t insize = ad->ad_in_size;
176 u_int32_t outsize = ad->ad_out_size;
177 int error = 0;
178 HAL_SPECTRAL_PARAM peout;
179 HAL_SPECTRAL_PARAM *pe;
180 struct ath_spectral_state *ss = sc->sc_spectral;
181 int val;
182
183 if (! ath_hal_spectral_supported(sc->sc_ah))
184 return (EINVAL);
185
186 ATH_LOCK(sc);
187 ath_power_set_power_state(sc, HAL_PM_AWAKE);
188 ATH_UNLOCK(sc);
189
190 if (ad->ad_id & ATH_DIAG_IN) {
191 /*
192 * Copy in data.
193 */
194 indata = malloc(insize, M_TEMP, M_NOWAIT);
195 if (indata == NULL) {
196 error = ENOMEM;
197 goto bad;
198 }
199 error = copyin(ad->ad_in_data, indata, insize);
200 if (error)
201 goto bad;
202 }
203 if (ad->ad_id & ATH_DIAG_DYN) {
204 /*
205 * Allocate a buffer for the results (otherwise the HAL
206 * returns a pointer to a buffer where we can read the
207 * results). Note that we depend on the HAL leaving this
208 * pointer for us to use below in reclaiming the buffer;
209 * may want to be more defensive.
210 */
211 outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
212 if (outdata == NULL) {
213 error = ENOMEM;
214 goto bad;
215 }
216 }
217 switch (id) {
218 case SPECTRAL_CONTROL_GET_PARAMS:
219 memset(&peout, 0, sizeof(peout));
220 outsize = sizeof(HAL_SPECTRAL_PARAM);
221 ath_hal_spectral_get_config(sc->sc_ah, &peout);
222 pe = (HAL_SPECTRAL_PARAM *) outdata;
223 memcpy(pe, &peout, sizeof(*pe));
224 break;
225 case SPECTRAL_CONTROL_SET_PARAMS:
226 if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
227 error = EINVAL;
228 break;
229 }
230 pe = (HAL_SPECTRAL_PARAM *) indata;
231 ath_hal_spectral_configure(sc->sc_ah, pe);
232 /* Save a local copy of the updated parameters */
233 ath_hal_spectral_get_config(sc->sc_ah,
234 &ss->spectral_state);
235 break;
236 case SPECTRAL_CONTROL_START:
237 ath_hal_spectral_configure(sc->sc_ah,
238 &ss->spectral_state);
239 (void) ath_hal_spectral_start(sc->sc_ah);
240 sc->sc_dospectral = 1;
241 /* XXX need to update the PHY mask in the driver */
242 break;
243 case SPECTRAL_CONTROL_STOP:
244 (void) ath_hal_spectral_stop(sc->sc_ah);
245 sc->sc_dospectral = 0;
246 /* XXX need to update the PHY mask in the driver */
247 break;
248 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
249 if (insize < sizeof(int)) {
250 device_printf(sc->sc_dev, "%d != %d\n",
251 insize,
252 (int) sizeof(int));
253 error = EINVAL;
254 break;
255 }
256 if (indata == NULL) {
257 device_printf(sc->sc_dev, "indata=NULL\n");
258 error = EINVAL;
259 break;
260 }
261 val = * ((int *) indata);
262 if (val == 0)
263 ss->spectral_enable_after_reset = 0;
264 else
265 ss->spectral_enable_after_reset = 1;
266 break;
267 case SPECTRAL_CONTROL_ENABLE:
268 /* XXX TODO */
269 case SPECTRAL_CONTROL_DISABLE:
270 /* XXX TODO */
271 break;
272 default:
273 error = EINVAL;
274 goto bad;
275 }
276 if (outsize < ad->ad_out_size)
277 ad->ad_out_size = outsize;
278 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
279 error = EFAULT;
280 bad:
281 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
282 free(indata, M_TEMP);
283 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
284 free(outdata, M_TEMP);
285 ATH_LOCK(sc);
286 ath_power_restore_power_state(sc);
287 ATH_UNLOCK(sc);
288
289 return (error);
290 }
291