blob: 5840b3731d5ba030b46815c0087be9d41a11d069 [file] [log] [blame]
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301/*
2 * Copyright (c) 2013 Qualcomm Atheros, Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <linux/relay.h>
18#include "ath9k.h"
19
20static s8 fix_rssi_inv_only(u8 rssi_val)
21{
22 if (rssi_val == 128)
23 rssi_val = 0;
24 return (s8) rssi_val;
25}
26
Oleksij Rempel1111d422014-11-06 08:53:23 +010027static void ath_debug_send_fft_sample(struct ath_spec_scan_priv *spec_priv,
Sujith Manoharanf65c0822013-12-18 09:53:18 +053028 struct fft_sample_tlv *fft_sample_tlv)
29{
30 int length;
Oleksij Rempel1111d422014-11-06 08:53:23 +010031 if (!spec_priv->rfs_chan_spec_scan)
Sujith Manoharanf65c0822013-12-18 09:53:18 +053032 return;
33
34 length = __be16_to_cpu(fft_sample_tlv->length) +
35 sizeof(*fft_sample_tlv);
Oleksij Rempel1111d422014-11-06 08:53:23 +010036 relay_write(spec_priv->rfs_chan_spec_scan, fft_sample_tlv, length);
Sujith Manoharanf65c0822013-12-18 09:53:18 +053037}
38
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +000039typedef int (ath_cmn_fft_idx_validator) (u8 *sample_end, int bytes_read);
40
41static int
42ath_cmn_max_idx_verify_ht20_fft(u8 *sample_end, int bytes_read)
43{
44 struct ath_ht20_mag_info *mag_info;
45 u8 *sample;
46 u16 max_magnitude;
47 u8 max_index;
48 u8 max_exp;
49
50 /* Sanity check so that we don't read outside the read
51 * buffer
52 */
53 if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN - 1)
54 return -1;
55
56 mag_info = (struct ath_ht20_mag_info *) (sample_end -
57 sizeof(struct ath_ht20_mag_info) + 1);
58
59 sample = sample_end - SPECTRAL_HT20_SAMPLE_LEN + 1;
60
61 max_index = spectral_max_index(mag_info->all_bins,
62 SPECTRAL_HT20_NUM_BINS);
63 max_magnitude = spectral_max_magnitude(mag_info->all_bins);
64
65 max_exp = mag_info->max_exp & 0xf;
66
67 /* Don't try to read something outside the read buffer
68 * in case of a missing byte (so bins[0] will be outside
69 * the read buffer)
70 */
71 if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN && max_index < 1)
72 return -1;
73
74 if (sample[max_index] != (max_magnitude >> max_exp))
75 return -1;
76 else
77 return 0;
78}
79
80static int
81ath_cmn_max_idx_verify_ht20_40_fft(u8 *sample_end, int bytes_read)
82{
83 struct ath_ht20_40_mag_info *mag_info;
84 u8 *sample;
85 u16 lower_mag, upper_mag;
86 u8 lower_max_index, upper_max_index;
87 u8 max_exp;
88 int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
89
90 /* Sanity check so that we don't read outside the read
91 * buffer
92 */
93 if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN - 1)
94 return -1;
95
96 mag_info = (struct ath_ht20_40_mag_info *) (sample_end -
97 sizeof(struct ath_ht20_40_mag_info) + 1);
98
99 sample = sample_end - SPECTRAL_HT20_40_SAMPLE_LEN + 1;
100
101 lower_mag = spectral_max_magnitude(mag_info->lower_bins);
102 lower_max_index = spectral_max_index(mag_info->lower_bins,
103 SPECTRAL_HT20_40_NUM_BINS);
104
105 upper_mag = spectral_max_magnitude(mag_info->upper_bins);
106 upper_max_index = spectral_max_index(mag_info->upper_bins,
107 SPECTRAL_HT20_40_NUM_BINS);
108
109 max_exp = mag_info->max_exp & 0xf;
110
111 /* Don't try to read something outside the read buffer
112 * in case of a missing byte (so bins[0] will be outside
113 * the read buffer)
114 */
115 if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN &&
116 ((upper_max_index < 1) || (lower_max_index < 1)))
117 return -1;
118
119 /* Some time hardware messes up the index and adds
120 * the index of the middle point (dc_pos). Try to fix it.
121 */
122 if ((upper_max_index - dc_pos > 0) &&
123 (sample[upper_max_index] == (upper_mag >> max_exp)))
124 upper_max_index -= dc_pos;
125
126 if ((lower_max_index - dc_pos > 0) &&
127 (sample[lower_max_index - dc_pos] == (lower_mag >> max_exp)))
128 lower_max_index -= dc_pos;
129
130 if ((sample[upper_max_index + dc_pos] != (upper_mag >> max_exp)) ||
131 (sample[lower_max_index] != (lower_mag >> max_exp)))
132 return -1;
133 else
134 return 0;
135}
136
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000137typedef int (ath_cmn_fft_sample_handler) (struct ath_rx_status *rs,
138 struct ath_spec_scan_priv *spec_priv,
139 u8 *sample_buf, u64 tsf, u16 freq, int chan_type);
140
141static int
142ath_cmn_process_ht20_fft(struct ath_rx_status *rs,
143 struct ath_spec_scan_priv *spec_priv,
144 u8 *sample_buf,
145 u64 tsf, u16 freq, int chan_type)
146{
147 struct fft_sample_ht20 fft_sample_20;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000148 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000149 struct ath_hw *ah = spec_priv->ah;
150 struct ath_ht20_mag_info *mag_info;
151 struct fft_sample_tlv *tlv;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000152 int i = 0;
153 int ret = 0;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000154 int dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000155 u16 magnitude, tmp_mag, length;
156 u8 max_index, bitmap_w, max_exp;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000157
158 length = sizeof(fft_sample_20) - sizeof(struct fft_sample_tlv);
159 fft_sample_20.tlv.type = ATH_FFT_SAMPLE_HT20;
160 fft_sample_20.tlv.length = __cpu_to_be16(length);
161 fft_sample_20.freq = __cpu_to_be16(freq);
162 fft_sample_20.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
163 fft_sample_20.noise = ah->noise;
164
165 mag_info = (struct ath_ht20_mag_info *) (sample_buf +
166 SPECTRAL_HT20_NUM_BINS);
167
168 magnitude = spectral_max_magnitude(mag_info->all_bins);
169 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
170
171 max_index = spectral_max_index(mag_info->all_bins,
172 SPECTRAL_HT20_NUM_BINS);
173 fft_sample_20.max_index = max_index;
174
175 bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
176 fft_sample_20.bitmap_weight = bitmap_w;
177
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000178 max_exp = mag_info->max_exp & 0xf;
179 fft_sample_20.max_exp = max_exp;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000180
181 fft_sample_20.tsf = __cpu_to_be64(tsf);
182
183 memcpy(fft_sample_20.data, sample_buf, SPECTRAL_HT20_NUM_BINS);
184
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000185 ath_dbg(common, SPECTRAL_SCAN, "FFT HT20 frame: max mag 0x%X,"
186 "max_mag_idx %i\n",
187 magnitude >> max_exp,
188 max_index);
189
190 if (fft_sample_20.data[max_index] != (magnitude >> max_exp)) {
191 ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
192 ret = -1;
193 }
194
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000195 /* DC value (value in the middle) is the blind spot of the spectral
196 * sample and invalid, interpolate it.
197 */
198 fft_sample_20.data[dc_pos] = (fft_sample_20.data[dc_pos + 1] +
199 fft_sample_20.data[dc_pos - 1]) / 2;
200
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000201 /* Check if the maximum magnitude is indeed maximum,
202 * also if the maximum value was at dc_pos, calculate
203 * a new one (since value at dc_pos is invalid).
204 */
205 if (max_index == dc_pos) {
206 tmp_mag = 0;
207 for (i = 0; i < dc_pos; i++) {
208 if (fft_sample_20.data[i] > tmp_mag) {
209 tmp_mag = fft_sample_20.data[i];
210 fft_sample_20.max_index = i;
211 }
212 }
213
214 magnitude = tmp_mag << max_exp;
215 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
216
217 ath_dbg(common, SPECTRAL_SCAN,
218 "Calculated new lower max 0x%X at %i\n",
219 tmp_mag, fft_sample_20.max_index);
220 } else
221 for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
222 if (fft_sample_20.data[i] == (magnitude >> max_exp))
223 ath_dbg(common, SPECTRAL_SCAN,
224 "Got max: 0x%X at index %i\n",
225 fft_sample_20.data[i], i);
226
227 if (fft_sample_20.data[i] > (magnitude >> max_exp)) {
228 ath_dbg(common, SPECTRAL_SCAN,
229 "Got bin %i greater than max: 0x%X\n",
230 i, fft_sample_20.data[i]);
231 ret = -1;
232 }
233 }
234
235 if (ret < 0)
236 return ret;
237
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000238 tlv = (struct fft_sample_tlv *)&fft_sample_20;
239
240 ath_debug_send_fft_sample(spec_priv, tlv);
241
242 return 0;
243}
244
245static int
246ath_cmn_process_ht20_40_fft(struct ath_rx_status *rs,
247 struct ath_spec_scan_priv *spec_priv,
248 u8 *sample_buf,
249 u64 tsf, u16 freq, int chan_type)
250{
251 struct fft_sample_ht20_40 fft_sample_40;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000252 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000253 struct ath_hw *ah = spec_priv->ah;
254 struct ath9k_hw_cal_data *caldata = ah->caldata;
255 struct ath_ht20_40_mag_info *mag_info;
256 struct fft_sample_tlv *tlv;
257 int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000258 int i = 0;
259 int ret = 0;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000260 s16 ext_nf;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000261 u16 lower_mag, upper_mag, tmp_mag, length;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000262 s8 lower_rssi, upper_rssi;
263 u8 lower_max_index, upper_max_index;
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000264 u8 lower_bitmap_w, upper_bitmap_w, max_exp;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000265
266 if (caldata)
267 ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
268 caldata->nfCalHist[3].privNF);
269 else
270 ext_nf = ATH_DEFAULT_NOISE_FLOOR;
271
272 length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv);
273 fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40;
274 fft_sample_40.tlv.length = __cpu_to_be16(length);
275 fft_sample_40.freq = __cpu_to_be16(freq);
276 fft_sample_40.channel_type = chan_type;
277
278 if (chan_type == NL80211_CHAN_HT40PLUS) {
279 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
280 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
281
282 fft_sample_40.lower_noise = ah->noise;
283 fft_sample_40.upper_noise = ext_nf;
284 } else {
285 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
286 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
287
288 fft_sample_40.lower_noise = ext_nf;
289 fft_sample_40.upper_noise = ah->noise;
290 }
291
292 fft_sample_40.lower_rssi = lower_rssi;
293 fft_sample_40.upper_rssi = upper_rssi;
294
295 mag_info = (struct ath_ht20_40_mag_info *) (sample_buf +
296 SPECTRAL_HT20_40_NUM_BINS);
297
298 lower_mag = spectral_max_magnitude(mag_info->lower_bins);
299 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
300
301 upper_mag = spectral_max_magnitude(mag_info->upper_bins);
302 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
303
304 lower_max_index = spectral_max_index(mag_info->lower_bins,
305 SPECTRAL_HT20_40_NUM_BINS);
306 fft_sample_40.lower_max_index = lower_max_index;
307
308 upper_max_index = spectral_max_index(mag_info->upper_bins,
309 SPECTRAL_HT20_40_NUM_BINS);
310 fft_sample_40.upper_max_index = upper_max_index;
311
312 lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
313 fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
314
315 upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
316 fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
317
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000318 max_exp = mag_info->max_exp & 0xf;
319 fft_sample_40.max_exp = max_exp;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000320
321 fft_sample_40.tsf = __cpu_to_be64(tsf);
322
323 memcpy(fft_sample_40.data, sample_buf, SPECTRAL_HT20_40_NUM_BINS);
324
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000325 ath_dbg(common, SPECTRAL_SCAN, "FFT HT20/40 frame: lower mag 0x%X,"
326 "lower_mag_idx %i, upper mag 0x%X,"
327 "upper_mag_idx %i\n",
328 lower_mag >> max_exp,
329 lower_max_index,
330 upper_mag >> max_exp,
331 upper_max_index);
332
333 /* Some time hardware messes up the index and adds
334 * the index of the middle point (dc_pos). Try to fix it.
335 */
336 if ((upper_max_index - dc_pos > 0) &&
337 (fft_sample_40.data[upper_max_index] == (upper_mag >> max_exp))) {
338 upper_max_index -= dc_pos;
339 fft_sample_40.upper_max_index = upper_max_index;
340 }
341
342 if ((lower_max_index - dc_pos > 0) &&
343 (fft_sample_40.data[lower_max_index - dc_pos] ==
344 (lower_mag >> max_exp))) {
345 lower_max_index -= dc_pos;
346 fft_sample_40.lower_max_index = lower_max_index;
347 }
348
349 /* Check if we got the expected magnitude values at
350 * the expected bins
351 */
352 if ((fft_sample_40.data[upper_max_index + dc_pos]
353 != (upper_mag >> max_exp)) ||
354 (fft_sample_40.data[lower_max_index]
355 != (lower_mag >> max_exp))) {
356 ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
357 ret = -1;
358 }
359
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000360 /* DC value (value in the middle) is the blind spot of the spectral
361 * sample and invalid, interpolate it.
362 */
363 fft_sample_40.data[dc_pos] = (fft_sample_40.data[dc_pos + 1] +
364 fft_sample_40.data[dc_pos - 1]) / 2;
365
Nick Kossifidis7fa580c2015-04-29 23:51:15 +0000366 /* Check if the maximum magnitudes are indeed maximum,
367 * also if the maximum value was at dc_pos, calculate
368 * a new one (since value at dc_pos is invalid).
369 */
370 if (lower_max_index == dc_pos) {
371 tmp_mag = 0;
372 for (i = 0; i < dc_pos; i++) {
373 if (fft_sample_40.data[i] > tmp_mag) {
374 tmp_mag = fft_sample_40.data[i];
375 fft_sample_40.lower_max_index = i;
376 }
377 }
378
379 lower_mag = tmp_mag << max_exp;
380 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
381
382 ath_dbg(common, SPECTRAL_SCAN,
383 "Calculated new lower max 0x%X at %i\n",
384 tmp_mag, fft_sample_40.lower_max_index);
385 } else
386 for (i = 0; i < dc_pos; i++) {
387 if (fft_sample_40.data[i] == (lower_mag >> max_exp))
388 ath_dbg(common, SPECTRAL_SCAN,
389 "Got lower mag: 0x%X at index %i\n",
390 fft_sample_40.data[i], i);
391
392 if (fft_sample_40.data[i] > (lower_mag >> max_exp)) {
393 ath_dbg(common, SPECTRAL_SCAN,
394 "Got lower bin %i higher than max: 0x%X\n",
395 i, fft_sample_40.data[i]);
396 ret = -1;
397 }
398 }
399
400 if (upper_max_index == dc_pos) {
401 tmp_mag = 0;
402 for (i = dc_pos; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
403 if (fft_sample_40.data[i] > tmp_mag) {
404 tmp_mag = fft_sample_40.data[i];
405 fft_sample_40.upper_max_index = i;
406 }
407 }
408 upper_mag = tmp_mag << max_exp;
409 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
410
411 ath_dbg(common, SPECTRAL_SCAN,
412 "Calculated new upper max 0x%X at %i\n",
413 tmp_mag, i);
414 } else
415 for (i = dc_pos; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
416 if (fft_sample_40.data[i] == (upper_mag >> max_exp))
417 ath_dbg(common, SPECTRAL_SCAN,
418 "Got upper mag: 0x%X at index %i\n",
419 fft_sample_40.data[i], i);
420
421 if (fft_sample_40.data[i] > (upper_mag >> max_exp)) {
422 ath_dbg(common, SPECTRAL_SCAN,
423 "Got upper bin %i higher than max: 0x%X\n",
424 i, fft_sample_40.data[i]);
425
426 ret = -1;
427 }
428 }
429
430 if (ret < 0)
431 return ret;
432
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000433 tlv = (struct fft_sample_tlv *)&fft_sample_40;
434
435 ath_debug_send_fft_sample(spec_priv, tlv);
436
437 return 0;
438}
439
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000440static inline void
441ath_cmn_copy_fft_frame(u8 *in, u8 *out, int sample_len, int sample_bytes)
442{
443 switch (sample_bytes - sample_len) {
444 case -1:
445 /* First byte missing */
446 memcpy(&out[1], in,
447 sample_len - 1);
448 break;
449 case 0:
450 /* Length correct, nothing to do. */
451 memcpy(out, in, sample_len);
452 break;
453 case 1:
454 /* MAC added 2 extra bytes AND first byte
455 * is missing.
456 */
457 memcpy(&out[1], in, 30);
458 out[31] = in[31];
459 memcpy(&out[32], &in[33],
460 sample_len - 32);
461 break;
462 case 2:
463 /* MAC added 2 extra bytes at bin 30 and 32,
464 * remove them.
465 */
466 memcpy(out, in, 30);
467 out[30] = in[31];
468 memcpy(&out[31], &in[33],
469 sample_len - 31);
470 break;
471 default:
472 break;
473 }
474}
475
Nick Kossifidis6b8f85a2015-04-29 23:51:19 +0000476static int
477ath_cmn_is_fft_buf_full(struct ath_spec_scan_priv *spec_priv)
478{
479 int i = 0;
480 int ret = 0;
481 struct rchan *rc = spec_priv->rfs_chan_spec_scan;
482
483 for_each_online_cpu(i)
484 ret += relay_buf_full(rc->buf[i]);
485
486 i = num_online_cpus();
487
488 if (ret == i)
489 return 1;
490 else
491 return 0;
492}
493
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530494/* returns 1 if this was a spectral frame, even if not handled. */
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100495int ath_cmn_process_fft(struct ath_spec_scan_priv *spec_priv, struct ieee80211_hdr *hdr,
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530496 struct ath_rx_status *rs, u64 tsf)
497{
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000498 u8 sample_buf[SPECTRAL_SAMPLE_MAX_LEN] = {0};
Oleksij Rempel1111d422014-11-06 08:53:23 +0100499 struct ath_hw *ah = spec_priv->ah;
500 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000501 u8 num_bins, *vdata = (u8 *)hdr;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530502 struct ath_radar_info *radar_info;
503 int len = rs->rs_datalen;
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000504 int i;
505 int got_slen = 0;
506 u8 *sample_start;
507 int sample_bytes = 0;
508 int ret = 0;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000509 u16 fft_len, sample_len, freq = ah->curchan->chan->center_freq;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530510 enum nl80211_channel_type chan_type;
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000511 ath_cmn_fft_idx_validator *fft_idx_validator;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000512 ath_cmn_fft_sample_handler *fft_handler;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530513
514 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
515 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
516 * yet, but this is supposed to be possible as well.
517 */
518 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
519 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
520 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
521 return 0;
522
523 /* check if spectral scan bit is set. This does not have to be checked
524 * if received through a SPECTRAL phy error, but shouldn't hurt.
525 */
526 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
527 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
528 return 0;
529
Nick Kossifidis6b8f85a2015-04-29 23:51:19 +0000530 /* Output buffers are full, no need to process anything
531 * since there is no space to put the result anyway
532 */
533 ret = ath_cmn_is_fft_buf_full(spec_priv);
534 if (ret == 1) {
535 ath_dbg(common, SPECTRAL_SCAN, "FFT report ignored, no space "
536 "left on output buffers\n");
537 return 1;
538 }
539
Oleksij Rempel1111d422014-11-06 08:53:23 +0100540 chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530541 if ((chan_type == NL80211_CHAN_HT40MINUS) ||
542 (chan_type == NL80211_CHAN_HT40PLUS)) {
543 fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000544 sample_len = SPECTRAL_HT20_40_SAMPLE_LEN;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530545 num_bins = SPECTRAL_HT20_40_NUM_BINS;
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000546 fft_idx_validator = &ath_cmn_max_idx_verify_ht20_40_fft;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000547 fft_handler = &ath_cmn_process_ht20_40_fft;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530548 } else {
549 fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000550 sample_len = SPECTRAL_HT20_SAMPLE_LEN;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530551 num_bins = SPECTRAL_HT20_NUM_BINS;
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000552 fft_idx_validator = ath_cmn_max_idx_verify_ht20_fft;
Nick Kossifidis58b5e4c2015-04-29 23:51:14 +0000553 fft_handler = &ath_cmn_process_ht20_fft;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530554 }
555
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000556 ath_dbg(common, SPECTRAL_SCAN, "Got radar dump bw_info: 0x%X,"
557 "len: %i fft_len: %i\n",
558 radar_info->pulse_bw_info,
559 len,
560 fft_len);
561 sample_start = vdata;
562 for (i = 0; i < len - 2; i++) {
563 sample_bytes++;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530564
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000565 /* Only a single sample received, no need to look
566 * for the sample's end, do the correction based
567 * on the packet's length instead. Note that hw
568 * will always put the radar_info structure on
569 * the end.
570 */
571 if (len <= fft_len + 2) {
572 sample_bytes = len - sizeof(struct ath_radar_info);
573 got_slen = 1;
574 }
575
576 /* Search for the end of the FFT frame between
577 * sample_len - 1 and sample_len + 2. exp_max is 3
578 * bits long and it's the only value on the last
579 * byte of the frame so since it'll be smaller than
580 * the next byte (the first bin of the next sample)
581 * 90% of the time, we can use it as a separator.
582 */
583 if (vdata[i] <= 0x7 && sample_bytes >= sample_len - 1) {
584
585 /* Got a frame length within boundaries, there are
586 * four scenarios here:
587 *
588 * a) sample_len -> We got the correct length
589 * b) sample_len + 2 -> 2 bytes added around bin[31]
590 * c) sample_len - 1 -> The first byte is missing
591 * d) sample_len + 1 -> b + c at the same time
592 *
593 * When MAC adds 2 extra bytes, bin[31] and bin[32]
594 * have the same value, so we can use that for further
595 * verification in cases b and d.
596 */
597
598 /* Did we go too far ? If so we couldn't determine
599 * this sample's boundaries, discard any further
600 * data
601 */
602 if ((sample_bytes > sample_len + 2) ||
603 ((sample_bytes > sample_len) &&
604 (sample_start[31] != sample_start[32])))
605 break;
606
607 /* See if we got a valid frame by checking the
608 * consistency of mag_info fields. This is to
609 * prevent from "fixing" a correct frame.
610 * Failure is non-fatal, later frames may
611 * be valid.
612 */
613 if (!fft_idx_validator(&vdata[i], i)) {
614 ath_dbg(common, SPECTRAL_SCAN,
615 "Found valid fft frame at %i\n", i);
616 got_slen = 1;
617 }
618
619 /* We expect 1 - 2 more bytes */
620 else if ((sample_start[31] == sample_start[32]) &&
621 (sample_bytes >= sample_len) &&
622 (sample_bytes < sample_len + 2) &&
623 (vdata[i + 1] <= 0x7))
624 continue;
625
626 /* Try to distinguish cases a and c */
627 else if ((sample_bytes == sample_len - 1) &&
628 (vdata[i + 1] <= 0x7))
629 continue;
630
631 got_slen = 1;
632 }
633
634 if (got_slen) {
635 ath_dbg(common, SPECTRAL_SCAN, "FFT frame len: %i\n",
636 sample_bytes);
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000637
638 /* Only try to fix a frame if it's the only one
639 * on the report, else just skip it.
640 */
641 if (sample_bytes != sample_len && len <= fft_len + 2) {
642 ath_cmn_copy_fft_frame(sample_start,
643 sample_buf, sample_len,
644 sample_bytes);
645
646 fft_handler(rs, spec_priv, sample_buf,
647 tsf, freq, chan_type);
Nick Kossifidis3ea2ce32015-04-29 23:51:20 +0000648
649 memset(sample_buf, 0, SPECTRAL_SAMPLE_MAX_LEN);
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000650 }
651
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000652 /* Process a normal frame */
Nick Kossifidis9acc98b2015-04-29 23:51:18 +0000653 if (sample_bytes == sample_len)
654 ret = fft_handler(rs, spec_priv, sample_start,
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000655 tsf, freq, chan_type);
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000656
657 /* Short report processed, break out of the
658 * loop.
659 */
660 if (len <= fft_len + 2)
661 break;
662
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000663 sample_start = &vdata[i + 1];
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000664
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000665 /* -1 to grab sample_len -1, -2 since
666 * they 'll get increased by one. In case
667 * of failure try to recover by going byte
Nick Kossifidis0f2c75d2015-04-29 23:51:17 +0000668 * by byte instead.
669 */
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000670 if (ret == 0) {
671 i += num_bins - 2;
672 sample_bytes = num_bins - 2;
673 }
674 got_slen = 0;
675 }
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530676 }
677
Nick Kossifidis72dd2cd2015-04-29 23:51:16 +0000678 i -= num_bins - 2;
679 if (len - i != sizeof(struct ath_radar_info))
680 ath_dbg(common, SPECTRAL_SCAN, "FFT report truncated"
681 "(bytes left: %i)\n",
682 len - i);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530683 return 1;
684}
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100685EXPORT_SYMBOL(ath_cmn_process_fft);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530686
687/*********************/
688/* spectral_scan_ctl */
689/*********************/
690
691static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
692 size_t count, loff_t *ppos)
693{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100694 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530695 char *mode = "";
696 unsigned int len;
697
Oleksij Rempel1111d422014-11-06 08:53:23 +0100698 switch (spec_priv->spectral_mode) {
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530699 case SPECTRAL_DISABLED:
700 mode = "disable";
701 break;
702 case SPECTRAL_BACKGROUND:
703 mode = "background";
704 break;
705 case SPECTRAL_CHANSCAN:
706 mode = "chanscan";
707 break;
708 case SPECTRAL_MANUAL:
709 mode = "manual";
710 break;
711 }
712 len = strlen(mode);
713 return simple_read_from_buffer(user_buf, count, ppos, mode, len);
714}
715
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100716void ath9k_cmn_spectral_scan_trigger(struct ath_common *common,
Oleksij Rempelf00a4222014-11-06 08:53:29 +0100717 struct ath_spec_scan_priv *spec_priv)
718{
719 struct ath_hw *ah = spec_priv->ah;
720 u32 rxfilter;
721
722 if (config_enabled(CONFIG_ATH9K_TX99))
723 return;
724
725 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
726 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
727 return;
728 }
729
730 ath_ps_ops(common)->wakeup(common);
731 rxfilter = ath9k_hw_getrxfilter(ah);
732 ath9k_hw_setrxfilter(ah, rxfilter |
733 ATH9K_RX_FILTER_PHYRADAR |
734 ATH9K_RX_FILTER_PHYERR);
735
736 /* TODO: usually this should not be neccesary, but for some reason
737 * (or in some mode?) the trigger must be called after the
738 * configuration, otherwise the register will have its values reset
739 * (on my ar9220 to value 0x01002310)
740 */
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100741 ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode);
Oleksij Rempelf00a4222014-11-06 08:53:29 +0100742 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
743 ath_ps_ops(common)->restore(common);
744}
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100745EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger);
Oleksij Rempelf00a4222014-11-06 08:53:29 +0100746
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100747int ath9k_cmn_spectral_scan_config(struct ath_common *common,
Oleksij Rempelf00a4222014-11-06 08:53:29 +0100748 struct ath_spec_scan_priv *spec_priv,
749 enum spectral_mode spectral_mode)
750{
751 struct ath_hw *ah = spec_priv->ah;
752
753 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
754 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
755 return -1;
756 }
757
758 switch (spectral_mode) {
759 case SPECTRAL_DISABLED:
760 spec_priv->spec_config.enabled = 0;
761 break;
762 case SPECTRAL_BACKGROUND:
763 /* send endless samples.
764 * TODO: is this really useful for "background"?
765 */
766 spec_priv->spec_config.endless = 1;
767 spec_priv->spec_config.enabled = 1;
768 break;
769 case SPECTRAL_CHANSCAN:
770 case SPECTRAL_MANUAL:
771 spec_priv->spec_config.endless = 0;
772 spec_priv->spec_config.enabled = 1;
773 break;
774 default:
775 return -1;
776 }
777
778 ath_ps_ops(common)->wakeup(common);
779 ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config);
780 ath_ps_ops(common)->restore(common);
781
782 spec_priv->spectral_mode = spectral_mode;
783
784 return 0;
785}
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100786EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config);
Oleksij Rempelf00a4222014-11-06 08:53:29 +0100787
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530788static ssize_t write_file_spec_scan_ctl(struct file *file,
789 const char __user *user_buf,
790 size_t count, loff_t *ppos)
791{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100792 struct ath_spec_scan_priv *spec_priv = file->private_data;
793 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530794 char buf[32];
795 ssize_t len;
796
797 if (config_enabled(CONFIG_ATH9K_TX99))
798 return -EOPNOTSUPP;
799
800 len = min(count, sizeof(buf) - 1);
801 if (copy_from_user(buf, user_buf, len))
802 return -EFAULT;
803
804 buf[len] = '\0';
805
806 if (strncmp("trigger", buf, 7) == 0) {
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100807 ath9k_cmn_spectral_scan_trigger(common, spec_priv);
Maks Naumovded3fb42014-08-16 00:41:07 -0700808 } else if (strncmp("background", buf, 10) == 0) {
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100809 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530810 ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
811 } else if (strncmp("chanscan", buf, 8) == 0) {
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100812 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530813 ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
814 } else if (strncmp("manual", buf, 6) == 0) {
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100815 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530816 ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
817 } else if (strncmp("disable", buf, 7) == 0) {
Oleksij Rempel67dc74f2014-11-06 08:53:30 +0100818 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530819 ath_dbg(common, CONFIG, "spectral scan: disabled\n");
820 } else {
821 return -EINVAL;
822 }
823
824 return count;
825}
826
827static const struct file_operations fops_spec_scan_ctl = {
828 .read = read_file_spec_scan_ctl,
829 .write = write_file_spec_scan_ctl,
830 .open = simple_open,
831 .owner = THIS_MODULE,
832 .llseek = default_llseek,
833};
834
835/*************************/
836/* spectral_short_repeat */
837/*************************/
838
839static ssize_t read_file_spectral_short_repeat(struct file *file,
840 char __user *user_buf,
841 size_t count, loff_t *ppos)
842{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100843 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530844 char buf[32];
845 unsigned int len;
846
Oleksij Rempel1111d422014-11-06 08:53:23 +0100847 len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530848 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
849}
850
851static ssize_t write_file_spectral_short_repeat(struct file *file,
852 const char __user *user_buf,
853 size_t count, loff_t *ppos)
854{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100855 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530856 unsigned long val;
857 char buf[32];
858 ssize_t len;
859
860 len = min(count, sizeof(buf) - 1);
861 if (copy_from_user(buf, user_buf, len))
862 return -EFAULT;
863
864 buf[len] = '\0';
865 if (kstrtoul(buf, 0, &val))
866 return -EINVAL;
867
Andrey Utkin3f557202014-07-17 16:56:37 +0300868 if (val > 1)
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530869 return -EINVAL;
870
Oleksij Rempel1111d422014-11-06 08:53:23 +0100871 spec_priv->spec_config.short_repeat = val;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530872 return count;
873}
874
875static const struct file_operations fops_spectral_short_repeat = {
876 .read = read_file_spectral_short_repeat,
877 .write = write_file_spectral_short_repeat,
878 .open = simple_open,
879 .owner = THIS_MODULE,
880 .llseek = default_llseek,
881};
882
883/******************/
884/* spectral_count */
885/******************/
886
887static ssize_t read_file_spectral_count(struct file *file,
888 char __user *user_buf,
889 size_t count, loff_t *ppos)
890{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100891 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530892 char buf[32];
893 unsigned int len;
894
Oleksij Rempel1111d422014-11-06 08:53:23 +0100895 len = sprintf(buf, "%d\n", spec_priv->spec_config.count);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530896 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
897}
898
899static ssize_t write_file_spectral_count(struct file *file,
900 const char __user *user_buf,
901 size_t count, loff_t *ppos)
902{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100903 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530904 unsigned long val;
905 char buf[32];
906 ssize_t len;
907
908 len = min(count, sizeof(buf) - 1);
909 if (copy_from_user(buf, user_buf, len))
910 return -EFAULT;
911
912 buf[len] = '\0';
913 if (kstrtoul(buf, 0, &val))
914 return -EINVAL;
915
Andrey Utkin3f557202014-07-17 16:56:37 +0300916 if (val > 255)
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530917 return -EINVAL;
918
Oleksij Rempel1111d422014-11-06 08:53:23 +0100919 spec_priv->spec_config.count = val;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530920 return count;
921}
922
923static const struct file_operations fops_spectral_count = {
924 .read = read_file_spectral_count,
925 .write = write_file_spectral_count,
926 .open = simple_open,
927 .owner = THIS_MODULE,
928 .llseek = default_llseek,
929};
930
931/*******************/
932/* spectral_period */
933/*******************/
934
935static ssize_t read_file_spectral_period(struct file *file,
936 char __user *user_buf,
937 size_t count, loff_t *ppos)
938{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100939 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530940 char buf[32];
941 unsigned int len;
942
Oleksij Rempel1111d422014-11-06 08:53:23 +0100943 len = sprintf(buf, "%d\n", spec_priv->spec_config.period);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530944 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
945}
946
947static ssize_t write_file_spectral_period(struct file *file,
948 const char __user *user_buf,
949 size_t count, loff_t *ppos)
950{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100951 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530952 unsigned long val;
953 char buf[32];
954 ssize_t len;
955
956 len = min(count, sizeof(buf) - 1);
957 if (copy_from_user(buf, user_buf, len))
958 return -EFAULT;
959
960 buf[len] = '\0';
961 if (kstrtoul(buf, 0, &val))
962 return -EINVAL;
963
Andrey Utkin3f557202014-07-17 16:56:37 +0300964 if (val > 255)
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530965 return -EINVAL;
966
Oleksij Rempel1111d422014-11-06 08:53:23 +0100967 spec_priv->spec_config.period = val;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530968 return count;
969}
970
971static const struct file_operations fops_spectral_period = {
972 .read = read_file_spectral_period,
973 .write = write_file_spectral_period,
974 .open = simple_open,
975 .owner = THIS_MODULE,
976 .llseek = default_llseek,
977};
978
979/***********************/
980/* spectral_fft_period */
981/***********************/
982
983static ssize_t read_file_spectral_fft_period(struct file *file,
984 char __user *user_buf,
985 size_t count, loff_t *ppos)
986{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100987 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530988 char buf[32];
989 unsigned int len;
990
Oleksij Rempel1111d422014-11-06 08:53:23 +0100991 len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period);
Sujith Manoharanf65c0822013-12-18 09:53:18 +0530992 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
993}
994
995static ssize_t write_file_spectral_fft_period(struct file *file,
996 const char __user *user_buf,
997 size_t count, loff_t *ppos)
998{
Oleksij Rempel1111d422014-11-06 08:53:23 +0100999 struct ath_spec_scan_priv *spec_priv = file->private_data;
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301000 unsigned long val;
1001 char buf[32];
1002 ssize_t len;
1003
1004 len = min(count, sizeof(buf) - 1);
1005 if (copy_from_user(buf, user_buf, len))
1006 return -EFAULT;
1007
1008 buf[len] = '\0';
1009 if (kstrtoul(buf, 0, &val))
1010 return -EINVAL;
1011
Andrey Utkin3f557202014-07-17 16:56:37 +03001012 if (val > 15)
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301013 return -EINVAL;
1014
Oleksij Rempel1111d422014-11-06 08:53:23 +01001015 spec_priv->spec_config.fft_period = val;
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301016 return count;
1017}
1018
1019static const struct file_operations fops_spectral_fft_period = {
1020 .read = read_file_spectral_fft_period,
1021 .write = write_file_spectral_fft_period,
1022 .open = simple_open,
1023 .owner = THIS_MODULE,
1024 .llseek = default_llseek,
1025};
1026
1027/*******************/
1028/* Relay interface */
1029/*******************/
1030
1031static struct dentry *create_buf_file_handler(const char *filename,
1032 struct dentry *parent,
1033 umode_t mode,
1034 struct rchan_buf *buf,
1035 int *is_global)
1036{
1037 struct dentry *buf_file;
1038
1039 buf_file = debugfs_create_file(filename, mode, parent, buf,
1040 &relay_file_operations);
1041 *is_global = 1;
1042 return buf_file;
1043}
1044
1045static int remove_buf_file_handler(struct dentry *dentry)
1046{
1047 debugfs_remove(dentry);
1048
1049 return 0;
1050}
1051
Wei Yongjunf84d1b42013-12-20 10:22:51 +08001052static struct rchan_callbacks rfs_spec_scan_cb = {
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301053 .create_buf_file = create_buf_file_handler,
1054 .remove_buf_file = remove_buf_file_handler,
1055};
1056
1057/*********************/
1058/* Debug Init/Deinit */
1059/*********************/
1060
Oleksij Rempel67dc74f2014-11-06 08:53:30 +01001061void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv)
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301062{
Markus Elfringc0420ea2015-02-04 18:48:28 +01001063 if (config_enabled(CONFIG_ATH9K_DEBUGFS)) {
Oleksij Rempel1111d422014-11-06 08:53:23 +01001064 relay_close(spec_priv->rfs_chan_spec_scan);
1065 spec_priv->rfs_chan_spec_scan = NULL;
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301066 }
1067}
Oleksij Rempel67dc74f2014-11-06 08:53:30 +01001068EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug);
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301069
Oleksij Rempel67dc74f2014-11-06 08:53:30 +01001070void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv,
1071 struct dentry *debugfs_phy)
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301072{
Oleksij Rempel1111d422014-11-06 08:53:23 +01001073 spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan",
Oleksij Rempelc10b75a2014-11-06 08:53:21 +01001074 debugfs_phy,
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301075 1024, 256, &rfs_spec_scan_cb,
1076 NULL);
1077 debugfs_create_file("spectral_scan_ctl",
1078 S_IRUSR | S_IWUSR,
Oleksij Rempel1111d422014-11-06 08:53:23 +01001079 debugfs_phy, spec_priv,
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301080 &fops_spec_scan_ctl);
1081 debugfs_create_file("spectral_short_repeat",
1082 S_IRUSR | S_IWUSR,
Oleksij Rempel1111d422014-11-06 08:53:23 +01001083 debugfs_phy, spec_priv,
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301084 &fops_spectral_short_repeat);
1085 debugfs_create_file("spectral_count",
1086 S_IRUSR | S_IWUSR,
Oleksij Rempel1111d422014-11-06 08:53:23 +01001087 debugfs_phy, spec_priv,
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301088 &fops_spectral_count);
1089 debugfs_create_file("spectral_period",
1090 S_IRUSR | S_IWUSR,
Oleksij Rempel1111d422014-11-06 08:53:23 +01001091 debugfs_phy, spec_priv,
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301092 &fops_spectral_period);
1093 debugfs_create_file("spectral_fft_period",
1094 S_IRUSR | S_IWUSR,
Oleksij Rempel1111d422014-11-06 08:53:23 +01001095 debugfs_phy, spec_priv,
Sujith Manoharanf65c0822013-12-18 09:53:18 +05301096 &fops_spectral_fft_period);
1097}
Oleksij Rempel67dc74f2014-11-06 08:53:30 +01001098EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug);