networking: make skb_push & __skb_push return void pointers
[linux-2.6-block.git] / drivers / net / wireless / ath / ath9k / common-spectral.c
CommitLineData
f65c0825
SM
1/*
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>
2aa56cca 18#include <linux/random.h>
f65c0825
SM
19#include "ath9k.h"
20
21static s8 fix_rssi_inv_only(u8 rssi_val)
22{
23 if (rssi_val == 128)
24 rssi_val = 0;
25 return (s8) rssi_val;
26}
27
1111d426 28static void ath_debug_send_fft_sample(struct ath_spec_scan_priv *spec_priv,
f65c0825
SM
29 struct fft_sample_tlv *fft_sample_tlv)
30{
31 int length;
1111d426 32 if (!spec_priv->rfs_chan_spec_scan)
f65c0825
SM
33 return;
34
35 length = __be16_to_cpu(fft_sample_tlv->length) +
36 sizeof(*fft_sample_tlv);
1111d426 37 relay_write(spec_priv->rfs_chan_spec_scan, fft_sample_tlv, length);
f65c0825
SM
38}
39
72dd2cda
NK
40typedef int (ath_cmn_fft_idx_validator) (u8 *sample_end, int bytes_read);
41
42static int
43ath_cmn_max_idx_verify_ht20_fft(u8 *sample_end, int bytes_read)
44{
45 struct ath_ht20_mag_info *mag_info;
46 u8 *sample;
47 u16 max_magnitude;
48 u8 max_index;
49 u8 max_exp;
50
51 /* Sanity check so that we don't read outside the read
52 * buffer
53 */
54 if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN - 1)
55 return -1;
56
57 mag_info = (struct ath_ht20_mag_info *) (sample_end -
58 sizeof(struct ath_ht20_mag_info) + 1);
59
60 sample = sample_end - SPECTRAL_HT20_SAMPLE_LEN + 1;
61
62 max_index = spectral_max_index(mag_info->all_bins,
63 SPECTRAL_HT20_NUM_BINS);
64 max_magnitude = spectral_max_magnitude(mag_info->all_bins);
65
66 max_exp = mag_info->max_exp & 0xf;
67
68 /* Don't try to read something outside the read buffer
69 * in case of a missing byte (so bins[0] will be outside
70 * the read buffer)
71 */
72 if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN && max_index < 1)
73 return -1;
74
75 if (sample[max_index] != (max_magnitude >> max_exp))
76 return -1;
77 else
78 return 0;
79}
80
81static int
82ath_cmn_max_idx_verify_ht20_40_fft(u8 *sample_end, int bytes_read)
83{
84 struct ath_ht20_40_mag_info *mag_info;
85 u8 *sample;
86 u16 lower_mag, upper_mag;
87 u8 lower_max_index, upper_max_index;
88 u8 max_exp;
89 int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
90
91 /* Sanity check so that we don't read outside the read
92 * buffer
93 */
94 if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN - 1)
95 return -1;
96
97 mag_info = (struct ath_ht20_40_mag_info *) (sample_end -
98 sizeof(struct ath_ht20_40_mag_info) + 1);
99
100 sample = sample_end - SPECTRAL_HT20_40_SAMPLE_LEN + 1;
101
102 lower_mag = spectral_max_magnitude(mag_info->lower_bins);
103 lower_max_index = spectral_max_index(mag_info->lower_bins,
104 SPECTRAL_HT20_40_NUM_BINS);
105
106 upper_mag = spectral_max_magnitude(mag_info->upper_bins);
107 upper_max_index = spectral_max_index(mag_info->upper_bins,
108 SPECTRAL_HT20_40_NUM_BINS);
109
110 max_exp = mag_info->max_exp & 0xf;
111
112 /* Don't try to read something outside the read buffer
113 * in case of a missing byte (so bins[0] will be outside
114 * the read buffer)
115 */
116 if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN &&
117 ((upper_max_index < 1) || (lower_max_index < 1)))
118 return -1;
119
120 /* Some time hardware messes up the index and adds
121 * the index of the middle point (dc_pos). Try to fix it.
122 */
123 if ((upper_max_index - dc_pos > 0) &&
124 (sample[upper_max_index] == (upper_mag >> max_exp)))
125 upper_max_index -= dc_pos;
126
127 if ((lower_max_index - dc_pos > 0) &&
128 (sample[lower_max_index - dc_pos] == (lower_mag >> max_exp)))
129 lower_max_index -= dc_pos;
130
131 if ((sample[upper_max_index + dc_pos] != (upper_mag >> max_exp)) ||
132 (sample[lower_max_index] != (lower_mag >> max_exp)))
133 return -1;
134 else
135 return 0;
136}
137
58b5e4c7
NK
138typedef int (ath_cmn_fft_sample_handler) (struct ath_rx_status *rs,
139 struct ath_spec_scan_priv *spec_priv,
140 u8 *sample_buf, u64 tsf, u16 freq, int chan_type);
141
142static int
143ath_cmn_process_ht20_fft(struct ath_rx_status *rs,
144 struct ath_spec_scan_priv *spec_priv,
145 u8 *sample_buf,
146 u64 tsf, u16 freq, int chan_type)
147{
148 struct fft_sample_ht20 fft_sample_20;
7fa580c1 149 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
58b5e4c7
NK
150 struct ath_hw *ah = spec_priv->ah;
151 struct ath_ht20_mag_info *mag_info;
152 struct fft_sample_tlv *tlv;
7fa580c1
NK
153 int i = 0;
154 int ret = 0;
58b5e4c7 155 int dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
7fa580c1
NK
156 u16 magnitude, tmp_mag, length;
157 u8 max_index, bitmap_w, max_exp;
58b5e4c7
NK
158
159 length = sizeof(fft_sample_20) - sizeof(struct fft_sample_tlv);
160 fft_sample_20.tlv.type = ATH_FFT_SAMPLE_HT20;
161 fft_sample_20.tlv.length = __cpu_to_be16(length);
162 fft_sample_20.freq = __cpu_to_be16(freq);
163 fft_sample_20.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
164 fft_sample_20.noise = ah->noise;
165
166 mag_info = (struct ath_ht20_mag_info *) (sample_buf +
167 SPECTRAL_HT20_NUM_BINS);
168
169 magnitude = spectral_max_magnitude(mag_info->all_bins);
170 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
171
172 max_index = spectral_max_index(mag_info->all_bins,
173 SPECTRAL_HT20_NUM_BINS);
174 fft_sample_20.max_index = max_index;
175
176 bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
177 fft_sample_20.bitmap_weight = bitmap_w;
178
7fa580c1
NK
179 max_exp = mag_info->max_exp & 0xf;
180 fft_sample_20.max_exp = max_exp;
58b5e4c7
NK
181
182 fft_sample_20.tsf = __cpu_to_be64(tsf);
183
184 memcpy(fft_sample_20.data, sample_buf, SPECTRAL_HT20_NUM_BINS);
185
7fa580c1
NK
186 ath_dbg(common, SPECTRAL_SCAN, "FFT HT20 frame: max mag 0x%X,"
187 "max_mag_idx %i\n",
188 magnitude >> max_exp,
189 max_index);
190
191 if (fft_sample_20.data[max_index] != (magnitude >> max_exp)) {
192 ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
193 ret = -1;
194 }
195
58b5e4c7
NK
196 /* DC value (value in the middle) is the blind spot of the spectral
197 * sample and invalid, interpolate it.
198 */
199 fft_sample_20.data[dc_pos] = (fft_sample_20.data[dc_pos + 1] +
200 fft_sample_20.data[dc_pos - 1]) / 2;
201
7fa580c1
NK
202 /* Check if the maximum magnitude is indeed maximum,
203 * also if the maximum value was at dc_pos, calculate
204 * a new one (since value at dc_pos is invalid).
205 */
206 if (max_index == dc_pos) {
207 tmp_mag = 0;
208 for (i = 0; i < dc_pos; i++) {
209 if (fft_sample_20.data[i] > tmp_mag) {
210 tmp_mag = fft_sample_20.data[i];
211 fft_sample_20.max_index = i;
212 }
213 }
214
215 magnitude = tmp_mag << max_exp;
216 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
217
218 ath_dbg(common, SPECTRAL_SCAN,
219 "Calculated new lower max 0x%X at %i\n",
220 tmp_mag, fft_sample_20.max_index);
221 } else
222 for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
223 if (fft_sample_20.data[i] == (magnitude >> max_exp))
224 ath_dbg(common, SPECTRAL_SCAN,
225 "Got max: 0x%X at index %i\n",
226 fft_sample_20.data[i], i);
227
228 if (fft_sample_20.data[i] > (magnitude >> max_exp)) {
229 ath_dbg(common, SPECTRAL_SCAN,
230 "Got bin %i greater than max: 0x%X\n",
231 i, fft_sample_20.data[i]);
232 ret = -1;
233 }
234 }
235
236 if (ret < 0)
237 return ret;
238
58b5e4c7
NK
239 tlv = (struct fft_sample_tlv *)&fft_sample_20;
240
241 ath_debug_send_fft_sample(spec_priv, tlv);
242
243 return 0;
244}
245
246static int
247ath_cmn_process_ht20_40_fft(struct ath_rx_status *rs,
248 struct ath_spec_scan_priv *spec_priv,
249 u8 *sample_buf,
250 u64 tsf, u16 freq, int chan_type)
251{
252 struct fft_sample_ht20_40 fft_sample_40;
7fa580c1 253 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
58b5e4c7
NK
254 struct ath_hw *ah = spec_priv->ah;
255 struct ath9k_hw_cal_data *caldata = ah->caldata;
256 struct ath_ht20_40_mag_info *mag_info;
257 struct fft_sample_tlv *tlv;
258 int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
7fa580c1
NK
259 int i = 0;
260 int ret = 0;
58b5e4c7 261 s16 ext_nf;
7fa580c1 262 u16 lower_mag, upper_mag, tmp_mag, length;
58b5e4c7
NK
263 s8 lower_rssi, upper_rssi;
264 u8 lower_max_index, upper_max_index;
7fa580c1 265 u8 lower_bitmap_w, upper_bitmap_w, max_exp;
58b5e4c7
NK
266
267 if (caldata)
268 ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
269 caldata->nfCalHist[3].privNF);
270 else
271 ext_nf = ATH_DEFAULT_NOISE_FLOOR;
272
273 length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv);
274 fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40;
275 fft_sample_40.tlv.length = __cpu_to_be16(length);
276 fft_sample_40.freq = __cpu_to_be16(freq);
277 fft_sample_40.channel_type = chan_type;
278
279 if (chan_type == NL80211_CHAN_HT40PLUS) {
280 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
281 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
282
283 fft_sample_40.lower_noise = ah->noise;
284 fft_sample_40.upper_noise = ext_nf;
285 } else {
286 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
287 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
288
289 fft_sample_40.lower_noise = ext_nf;
290 fft_sample_40.upper_noise = ah->noise;
291 }
292
293 fft_sample_40.lower_rssi = lower_rssi;
294 fft_sample_40.upper_rssi = upper_rssi;
295
296 mag_info = (struct ath_ht20_40_mag_info *) (sample_buf +
297 SPECTRAL_HT20_40_NUM_BINS);
298
299 lower_mag = spectral_max_magnitude(mag_info->lower_bins);
300 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
301
302 upper_mag = spectral_max_magnitude(mag_info->upper_bins);
303 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
304
305 lower_max_index = spectral_max_index(mag_info->lower_bins,
306 SPECTRAL_HT20_40_NUM_BINS);
307 fft_sample_40.lower_max_index = lower_max_index;
308
309 upper_max_index = spectral_max_index(mag_info->upper_bins,
310 SPECTRAL_HT20_40_NUM_BINS);
311 fft_sample_40.upper_max_index = upper_max_index;
312
313 lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
314 fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
315
316 upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
317 fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
318
7fa580c1
NK
319 max_exp = mag_info->max_exp & 0xf;
320 fft_sample_40.max_exp = max_exp;
58b5e4c7
NK
321
322 fft_sample_40.tsf = __cpu_to_be64(tsf);
323
324 memcpy(fft_sample_40.data, sample_buf, SPECTRAL_HT20_40_NUM_BINS);
325
7fa580c1
NK
326 ath_dbg(common, SPECTRAL_SCAN, "FFT HT20/40 frame: lower mag 0x%X,"
327 "lower_mag_idx %i, upper mag 0x%X,"
328 "upper_mag_idx %i\n",
329 lower_mag >> max_exp,
330 lower_max_index,
331 upper_mag >> max_exp,
332 upper_max_index);
333
334 /* Some time hardware messes up the index and adds
335 * the index of the middle point (dc_pos). Try to fix it.
336 */
337 if ((upper_max_index - dc_pos > 0) &&
338 (fft_sample_40.data[upper_max_index] == (upper_mag >> max_exp))) {
339 upper_max_index -= dc_pos;
340 fft_sample_40.upper_max_index = upper_max_index;
341 }
342
343 if ((lower_max_index - dc_pos > 0) &&
344 (fft_sample_40.data[lower_max_index - dc_pos] ==
345 (lower_mag >> max_exp))) {
346 lower_max_index -= dc_pos;
347 fft_sample_40.lower_max_index = lower_max_index;
348 }
349
350 /* Check if we got the expected magnitude values at
351 * the expected bins
352 */
353 if ((fft_sample_40.data[upper_max_index + dc_pos]
354 != (upper_mag >> max_exp)) ||
355 (fft_sample_40.data[lower_max_index]
356 != (lower_mag >> max_exp))) {
357 ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
358 ret = -1;
359 }
360
58b5e4c7
NK
361 /* DC value (value in the middle) is the blind spot of the spectral
362 * sample and invalid, interpolate it.
363 */
364 fft_sample_40.data[dc_pos] = (fft_sample_40.data[dc_pos + 1] +
365 fft_sample_40.data[dc_pos - 1]) / 2;
366
7fa580c1
NK
367 /* Check if the maximum magnitudes are indeed maximum,
368 * also if the maximum value was at dc_pos, calculate
369 * a new one (since value at dc_pos is invalid).
370 */
371 if (lower_max_index == dc_pos) {
372 tmp_mag = 0;
373 for (i = 0; i < dc_pos; i++) {
374 if (fft_sample_40.data[i] > tmp_mag) {
375 tmp_mag = fft_sample_40.data[i];
376 fft_sample_40.lower_max_index = i;
377 }
378 }
379
380 lower_mag = tmp_mag << max_exp;
381 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
382
383 ath_dbg(common, SPECTRAL_SCAN,
384 "Calculated new lower max 0x%X at %i\n",
385 tmp_mag, fft_sample_40.lower_max_index);
386 } else
387 for (i = 0; i < dc_pos; i++) {
388 if (fft_sample_40.data[i] == (lower_mag >> max_exp))
389 ath_dbg(common, SPECTRAL_SCAN,
390 "Got lower mag: 0x%X at index %i\n",
391 fft_sample_40.data[i], i);
392
393 if (fft_sample_40.data[i] > (lower_mag >> max_exp)) {
394 ath_dbg(common, SPECTRAL_SCAN,
395 "Got lower bin %i higher than max: 0x%X\n",
396 i, fft_sample_40.data[i]);
397 ret = -1;
398 }
399 }
400
401 if (upper_max_index == dc_pos) {
402 tmp_mag = 0;
403 for (i = dc_pos; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
404 if (fft_sample_40.data[i] > tmp_mag) {
405 tmp_mag = fft_sample_40.data[i];
406 fft_sample_40.upper_max_index = i;
407 }
408 }
409 upper_mag = tmp_mag << max_exp;
410 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
411
412 ath_dbg(common, SPECTRAL_SCAN,
413 "Calculated new upper max 0x%X at %i\n",
414 tmp_mag, i);
415 } else
416 for (i = dc_pos; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
417 if (fft_sample_40.data[i] == (upper_mag >> max_exp))
418 ath_dbg(common, SPECTRAL_SCAN,
419 "Got upper mag: 0x%X at index %i\n",
420 fft_sample_40.data[i], i);
421
422 if (fft_sample_40.data[i] > (upper_mag >> max_exp)) {
423 ath_dbg(common, SPECTRAL_SCAN,
424 "Got upper bin %i higher than max: 0x%X\n",
425 i, fft_sample_40.data[i]);
426
427 ret = -1;
428 }
429 }
430
431 if (ret < 0)
432 return ret;
433
58b5e4c7
NK
434 tlv = (struct fft_sample_tlv *)&fft_sample_40;
435
436 ath_debug_send_fft_sample(spec_priv, tlv);
437
438 return 0;
439}
440
0f2c75de
NK
441static inline void
442ath_cmn_copy_fft_frame(u8 *in, u8 *out, int sample_len, int sample_bytes)
443{
444 switch (sample_bytes - sample_len) {
445 case -1:
446 /* First byte missing */
447 memcpy(&out[1], in,
448 sample_len - 1);
449 break;
450 case 0:
451 /* Length correct, nothing to do. */
452 memcpy(out, in, sample_len);
453 break;
454 case 1:
455 /* MAC added 2 extra bytes AND first byte
456 * is missing.
457 */
458 memcpy(&out[1], in, 30);
459 out[31] = in[31];
460 memcpy(&out[32], &in[33],
461 sample_len - 32);
462 break;
463 case 2:
464 /* MAC added 2 extra bytes at bin 30 and 32,
465 * remove them.
466 */
467 memcpy(out, in, 30);
468 out[30] = in[31];
469 memcpy(&out[31], &in[33],
470 sample_len - 31);
471 break;
472 default:
473 break;
474 }
475}
476
6b8f85a9
NK
477static int
478ath_cmn_is_fft_buf_full(struct ath_spec_scan_priv *spec_priv)
479{
480 int i = 0;
481 int ret = 0;
482 struct rchan *rc = spec_priv->rfs_chan_spec_scan;
483
484 for_each_online_cpu(i)
07460b92 485 ret += relay_buf_full(*per_cpu_ptr(rc->buf, i));
6b8f85a9
NK
486
487 i = num_online_cpus();
488
489 if (ret == i)
490 return 1;
491 else
492 return 0;
493}
494
f65c0825 495/* returns 1 if this was a spectral frame, even if not handled. */
67dc74f1 496int ath_cmn_process_fft(struct ath_spec_scan_priv *spec_priv, struct ieee80211_hdr *hdr,
f65c0825
SM
497 struct ath_rx_status *rs, u64 tsf)
498{
58b5e4c7 499 u8 sample_buf[SPECTRAL_SAMPLE_MAX_LEN] = {0};
1111d426
OR
500 struct ath_hw *ah = spec_priv->ah;
501 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
58b5e4c7 502 u8 num_bins, *vdata = (u8 *)hdr;
f65c0825
SM
503 struct ath_radar_info *radar_info;
504 int len = rs->rs_datalen;
72dd2cda
NK
505 int i;
506 int got_slen = 0;
507 u8 *sample_start;
508 int sample_bytes = 0;
509 int ret = 0;
58b5e4c7 510 u16 fft_len, sample_len, freq = ah->curchan->chan->center_freq;
f65c0825 511 enum nl80211_channel_type chan_type;
72dd2cda 512 ath_cmn_fft_idx_validator *fft_idx_validator;
58b5e4c7 513 ath_cmn_fft_sample_handler *fft_handler;
f65c0825
SM
514
515 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
516 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
517 * yet, but this is supposed to be possible as well.
518 */
519 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
520 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
521 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
522 return 0;
523
524 /* check if spectral scan bit is set. This does not have to be checked
525 * if received through a SPECTRAL phy error, but shouldn't hurt.
526 */
527 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
528 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
529 return 0;
530
40bea976
MP
531 if (!spec_priv->rfs_chan_spec_scan)
532 return 1;
533
6b8f85a9
NK
534 /* Output buffers are full, no need to process anything
535 * since there is no space to put the result anyway
536 */
537 ret = ath_cmn_is_fft_buf_full(spec_priv);
538 if (ret == 1) {
539 ath_dbg(common, SPECTRAL_SCAN, "FFT report ignored, no space "
540 "left on output buffers\n");
541 return 1;
542 }
543
1111d426 544 chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef);
f65c0825
SM
545 if ((chan_type == NL80211_CHAN_HT40MINUS) ||
546 (chan_type == NL80211_CHAN_HT40PLUS)) {
547 fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
58b5e4c7 548 sample_len = SPECTRAL_HT20_40_SAMPLE_LEN;
f65c0825 549 num_bins = SPECTRAL_HT20_40_NUM_BINS;
72dd2cda 550 fft_idx_validator = &ath_cmn_max_idx_verify_ht20_40_fft;
58b5e4c7 551 fft_handler = &ath_cmn_process_ht20_40_fft;
f65c0825
SM
552 } else {
553 fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
58b5e4c7 554 sample_len = SPECTRAL_HT20_SAMPLE_LEN;
f65c0825 555 num_bins = SPECTRAL_HT20_NUM_BINS;
72dd2cda 556 fft_idx_validator = ath_cmn_max_idx_verify_ht20_fft;
58b5e4c7 557 fft_handler = &ath_cmn_process_ht20_fft;
f65c0825
SM
558 }
559
72dd2cda
NK
560 ath_dbg(common, SPECTRAL_SCAN, "Got radar dump bw_info: 0x%X,"
561 "len: %i fft_len: %i\n",
562 radar_info->pulse_bw_info,
563 len,
564 fft_len);
565 sample_start = vdata;
566 for (i = 0; i < len - 2; i++) {
567 sample_bytes++;
568
569 /* Only a single sample received, no need to look
570 * for the sample's end, do the correction based
571 * on the packet's length instead. Note that hw
572 * will always put the radar_info structure on
573 * the end.
574 */
575 if (len <= fft_len + 2) {
576 sample_bytes = len - sizeof(struct ath_radar_info);
577 got_slen = 1;
578 }
f65c0825 579
72dd2cda
NK
580 /* Search for the end of the FFT frame between
581 * sample_len - 1 and sample_len + 2. exp_max is 3
582 * bits long and it's the only value on the last
583 * byte of the frame so since it'll be smaller than
584 * the next byte (the first bin of the next sample)
585 * 90% of the time, we can use it as a separator.
586 */
587 if (vdata[i] <= 0x7 && sample_bytes >= sample_len - 1) {
588
589 /* Got a frame length within boundaries, there are
590 * four scenarios here:
591 *
592 * a) sample_len -> We got the correct length
593 * b) sample_len + 2 -> 2 bytes added around bin[31]
594 * c) sample_len - 1 -> The first byte is missing
595 * d) sample_len + 1 -> b + c at the same time
596 *
597 * When MAC adds 2 extra bytes, bin[31] and bin[32]
598 * have the same value, so we can use that for further
599 * verification in cases b and d.
600 */
601
602 /* Did we go too far ? If so we couldn't determine
603 * this sample's boundaries, discard any further
604 * data
605 */
606 if ((sample_bytes > sample_len + 2) ||
607 ((sample_bytes > sample_len) &&
608 (sample_start[31] != sample_start[32])))
609 break;
610
611 /* See if we got a valid frame by checking the
612 * consistency of mag_info fields. This is to
613 * prevent from "fixing" a correct frame.
614 * Failure is non-fatal, later frames may
615 * be valid.
616 */
617 if (!fft_idx_validator(&vdata[i], i)) {
618 ath_dbg(common, SPECTRAL_SCAN,
619 "Found valid fft frame at %i\n", i);
620 got_slen = 1;
621 }
622
623 /* We expect 1 - 2 more bytes */
624 else if ((sample_start[31] == sample_start[32]) &&
625 (sample_bytes >= sample_len) &&
626 (sample_bytes < sample_len + 2) &&
627 (vdata[i + 1] <= 0x7))
628 continue;
f65c0825 629
72dd2cda
NK
630 /* Try to distinguish cases a and c */
631 else if ((sample_bytes == sample_len - 1) &&
632 (vdata[i + 1] <= 0x7))
633 continue;
634
635 got_slen = 1;
636 }
637
638 if (got_slen) {
639 ath_dbg(common, SPECTRAL_SCAN, "FFT frame len: %i\n",
640 sample_bytes);
0f2c75de
NK
641
642 /* Only try to fix a frame if it's the only one
643 * on the report, else just skip it.
644 */
645 if (sample_bytes != sample_len && len <= fft_len + 2) {
646 ath_cmn_copy_fft_frame(sample_start,
647 sample_buf, sample_len,
648 sample_bytes);
649
650 fft_handler(rs, spec_priv, sample_buf,
651 tsf, freq, chan_type);
3ea2ce3f
NK
652
653 memset(sample_buf, 0, SPECTRAL_SAMPLE_MAX_LEN);
2aa56cca
NK
654
655 /* Mix the received bins to the /dev/random
656 * pool
657 */
658 add_device_randomness(sample_buf, num_bins);
0f2c75de
NK
659 }
660
661 /* Process a normal frame */
2aa56cca 662 if (sample_bytes == sample_len) {
9acc98b9 663 ret = fft_handler(rs, spec_priv, sample_start,
0f2c75de 664 tsf, freq, chan_type);
72dd2cda 665
2aa56cca
NK
666 /* Mix the received bins to the /dev/random
667 * pool
668 */
669 add_device_randomness(sample_start, num_bins);
670 }
671
0f2c75de
NK
672 /* Short report processed, break out of the
673 * loop.
674 */
675 if (len <= fft_len + 2)
676 break;
677
72dd2cda 678 sample_start = &vdata[i + 1];
0f2c75de 679
72dd2cda
NK
680 /* -1 to grab sample_len -1, -2 since
681 * they 'll get increased by one. In case
682 * of failure try to recover by going byte
0f2c75de
NK
683 * by byte instead.
684 */
72dd2cda
NK
685 if (ret == 0) {
686 i += num_bins - 2;
687 sample_bytes = num_bins - 2;
688 }
689 got_slen = 0;
690 }
691 }
f65c0825 692
72dd2cda
NK
693 i -= num_bins - 2;
694 if (len - i != sizeof(struct ath_radar_info))
695 ath_dbg(common, SPECTRAL_SCAN, "FFT report truncated"
696 "(bytes left: %i)\n",
697 len - i);
f65c0825
SM
698 return 1;
699}
67dc74f1 700EXPORT_SYMBOL(ath_cmn_process_fft);
f65c0825
SM
701
702/*********************/
703/* spectral_scan_ctl */
704/*********************/
705
706static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
707 size_t count, loff_t *ppos)
708{
1111d426 709 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
710 char *mode = "";
711 unsigned int len;
712
1111d426 713 switch (spec_priv->spectral_mode) {
f65c0825
SM
714 case SPECTRAL_DISABLED:
715 mode = "disable";
716 break;
717 case SPECTRAL_BACKGROUND:
718 mode = "background";
719 break;
720 case SPECTRAL_CHANSCAN:
721 mode = "chanscan";
722 break;
723 case SPECTRAL_MANUAL:
724 mode = "manual";
725 break;
726 }
727 len = strlen(mode);
728 return simple_read_from_buffer(user_buf, count, ppos, mode, len);
729}
730
67dc74f1 731void ath9k_cmn_spectral_scan_trigger(struct ath_common *common,
f00a422c
OR
732 struct ath_spec_scan_priv *spec_priv)
733{
734 struct ath_hw *ah = spec_priv->ah;
735 u32 rxfilter;
736
97f2645f 737 if (IS_ENABLED(CONFIG_ATH9K_TX99))
f00a422c
OR
738 return;
739
740 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
741 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
742 return;
743 }
744
45c3d550
ZK
745 if (!spec_priv->spec_config.enabled)
746 return;
747
f00a422c
OR
748 ath_ps_ops(common)->wakeup(common);
749 rxfilter = ath9k_hw_getrxfilter(ah);
750 ath9k_hw_setrxfilter(ah, rxfilter |
751 ATH9K_RX_FILTER_PHYRADAR |
752 ATH9K_RX_FILTER_PHYERR);
753
754 /* TODO: usually this should not be neccesary, but for some reason
755 * (or in some mode?) the trigger must be called after the
756 * configuration, otherwise the register will have its values reset
757 * (on my ar9220 to value 0x01002310)
758 */
67dc74f1 759 ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode);
f00a422c
OR
760 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
761 ath_ps_ops(common)->restore(common);
762}
67dc74f1 763EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger);
f00a422c 764
67dc74f1 765int ath9k_cmn_spectral_scan_config(struct ath_common *common,
f00a422c
OR
766 struct ath_spec_scan_priv *spec_priv,
767 enum spectral_mode spectral_mode)
768{
769 struct ath_hw *ah = spec_priv->ah;
770
771 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
772 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
773 return -1;
774 }
775
776 switch (spectral_mode) {
777 case SPECTRAL_DISABLED:
778 spec_priv->spec_config.enabled = 0;
779 break;
780 case SPECTRAL_BACKGROUND:
781 /* send endless samples.
782 * TODO: is this really useful for "background"?
783 */
784 spec_priv->spec_config.endless = 1;
785 spec_priv->spec_config.enabled = 1;
786 break;
787 case SPECTRAL_CHANSCAN:
788 case SPECTRAL_MANUAL:
789 spec_priv->spec_config.endless = 0;
790 spec_priv->spec_config.enabled = 1;
791 break;
792 default:
793 return -1;
794 }
795
796 ath_ps_ops(common)->wakeup(common);
797 ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config);
798 ath_ps_ops(common)->restore(common);
799
800 spec_priv->spectral_mode = spectral_mode;
801
802 return 0;
803}
67dc74f1 804EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config);
f00a422c 805
f65c0825
SM
806static ssize_t write_file_spec_scan_ctl(struct file *file,
807 const char __user *user_buf,
808 size_t count, loff_t *ppos)
809{
1111d426
OR
810 struct ath_spec_scan_priv *spec_priv = file->private_data;
811 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
f65c0825
SM
812 char buf[32];
813 ssize_t len;
814
97f2645f 815 if (IS_ENABLED(CONFIG_ATH9K_TX99))
f65c0825
SM
816 return -EOPNOTSUPP;
817
818 len = min(count, sizeof(buf) - 1);
819 if (copy_from_user(buf, user_buf, len))
820 return -EFAULT;
821
822 buf[len] = '\0';
823
824 if (strncmp("trigger", buf, 7) == 0) {
67dc74f1 825 ath9k_cmn_spectral_scan_trigger(common, spec_priv);
ded3fb4c 826 } else if (strncmp("background", buf, 10) == 0) {
67dc74f1 827 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND);
f65c0825
SM
828 ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
829 } else if (strncmp("chanscan", buf, 8) == 0) {
67dc74f1 830 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN);
f65c0825
SM
831 ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
832 } else if (strncmp("manual", buf, 6) == 0) {
67dc74f1 833 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL);
f65c0825
SM
834 ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
835 } else if (strncmp("disable", buf, 7) == 0) {
67dc74f1 836 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED);
f65c0825
SM
837 ath_dbg(common, CONFIG, "spectral scan: disabled\n");
838 } else {
839 return -EINVAL;
840 }
841
842 return count;
843}
844
845static const struct file_operations fops_spec_scan_ctl = {
846 .read = read_file_spec_scan_ctl,
847 .write = write_file_spec_scan_ctl,
848 .open = simple_open,
849 .owner = THIS_MODULE,
850 .llseek = default_llseek,
851};
852
853/*************************/
854/* spectral_short_repeat */
855/*************************/
856
857static ssize_t read_file_spectral_short_repeat(struct file *file,
858 char __user *user_buf,
859 size_t count, loff_t *ppos)
860{
1111d426 861 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
862 char buf[32];
863 unsigned int len;
864
1111d426 865 len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat);
f65c0825
SM
866 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
867}
868
869static ssize_t write_file_spectral_short_repeat(struct file *file,
870 const char __user *user_buf,
871 size_t count, loff_t *ppos)
872{
1111d426 873 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
874 unsigned long val;
875 char buf[32];
876 ssize_t len;
877
878 len = min(count, sizeof(buf) - 1);
879 if (copy_from_user(buf, user_buf, len))
880 return -EFAULT;
881
882 buf[len] = '\0';
883 if (kstrtoul(buf, 0, &val))
884 return -EINVAL;
885
3f557202 886 if (val > 1)
f65c0825
SM
887 return -EINVAL;
888
1111d426 889 spec_priv->spec_config.short_repeat = val;
f65c0825
SM
890 return count;
891}
892
893static const struct file_operations fops_spectral_short_repeat = {
894 .read = read_file_spectral_short_repeat,
895 .write = write_file_spectral_short_repeat,
896 .open = simple_open,
897 .owner = THIS_MODULE,
898 .llseek = default_llseek,
899};
900
901/******************/
902/* spectral_count */
903/******************/
904
905static ssize_t read_file_spectral_count(struct file *file,
906 char __user *user_buf,
907 size_t count, loff_t *ppos)
908{
1111d426 909 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
910 char buf[32];
911 unsigned int len;
912
1111d426 913 len = sprintf(buf, "%d\n", spec_priv->spec_config.count);
f65c0825
SM
914 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
915}
916
917static ssize_t write_file_spectral_count(struct file *file,
918 const char __user *user_buf,
919 size_t count, loff_t *ppos)
920{
1111d426 921 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
922 unsigned long val;
923 char buf[32];
924 ssize_t len;
925
926 len = min(count, sizeof(buf) - 1);
927 if (copy_from_user(buf, user_buf, len))
928 return -EFAULT;
929
930 buf[len] = '\0';
931 if (kstrtoul(buf, 0, &val))
932 return -EINVAL;
933
3f557202 934 if (val > 255)
f65c0825
SM
935 return -EINVAL;
936
1111d426 937 spec_priv->spec_config.count = val;
f65c0825
SM
938 return count;
939}
940
941static const struct file_operations fops_spectral_count = {
942 .read = read_file_spectral_count,
943 .write = write_file_spectral_count,
944 .open = simple_open,
945 .owner = THIS_MODULE,
946 .llseek = default_llseek,
947};
948
949/*******************/
950/* spectral_period */
951/*******************/
952
953static ssize_t read_file_spectral_period(struct file *file,
954 char __user *user_buf,
955 size_t count, loff_t *ppos)
956{
1111d426 957 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
958 char buf[32];
959 unsigned int len;
960
1111d426 961 len = sprintf(buf, "%d\n", spec_priv->spec_config.period);
f65c0825
SM
962 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
963}
964
965static ssize_t write_file_spectral_period(struct file *file,
966 const char __user *user_buf,
967 size_t count, loff_t *ppos)
968{
1111d426 969 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
970 unsigned long val;
971 char buf[32];
972 ssize_t len;
973
974 len = min(count, sizeof(buf) - 1);
975 if (copy_from_user(buf, user_buf, len))
976 return -EFAULT;
977
978 buf[len] = '\0';
979 if (kstrtoul(buf, 0, &val))
980 return -EINVAL;
981
3f557202 982 if (val > 255)
f65c0825
SM
983 return -EINVAL;
984
1111d426 985 spec_priv->spec_config.period = val;
f65c0825
SM
986 return count;
987}
988
989static const struct file_operations fops_spectral_period = {
990 .read = read_file_spectral_period,
991 .write = write_file_spectral_period,
992 .open = simple_open,
993 .owner = THIS_MODULE,
994 .llseek = default_llseek,
995};
996
997/***********************/
998/* spectral_fft_period */
999/***********************/
1000
1001static ssize_t read_file_spectral_fft_period(struct file *file,
1002 char __user *user_buf,
1003 size_t count, loff_t *ppos)
1004{
1111d426 1005 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
1006 char buf[32];
1007 unsigned int len;
1008
1111d426 1009 len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period);
f65c0825
SM
1010 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1011}
1012
1013static ssize_t write_file_spectral_fft_period(struct file *file,
1014 const char __user *user_buf,
1015 size_t count, loff_t *ppos)
1016{
1111d426 1017 struct ath_spec_scan_priv *spec_priv = file->private_data;
f65c0825
SM
1018 unsigned long val;
1019 char buf[32];
1020 ssize_t len;
1021
1022 len = min(count, sizeof(buf) - 1);
1023 if (copy_from_user(buf, user_buf, len))
1024 return -EFAULT;
1025
1026 buf[len] = '\0';
1027 if (kstrtoul(buf, 0, &val))
1028 return -EINVAL;
1029
3f557202 1030 if (val > 15)
f65c0825
SM
1031 return -EINVAL;
1032
1111d426 1033 spec_priv->spec_config.fft_period = val;
f65c0825
SM
1034 return count;
1035}
1036
1037static const struct file_operations fops_spectral_fft_period = {
1038 .read = read_file_spectral_fft_period,
1039 .write = write_file_spectral_fft_period,
1040 .open = simple_open,
1041 .owner = THIS_MODULE,
1042 .llseek = default_llseek,
1043};
1044
1045/*******************/
1046/* Relay interface */
1047/*******************/
1048
1049static struct dentry *create_buf_file_handler(const char *filename,
1050 struct dentry *parent,
1051 umode_t mode,
1052 struct rchan_buf *buf,
1053 int *is_global)
1054{
1055 struct dentry *buf_file;
1056
1057 buf_file = debugfs_create_file(filename, mode, parent, buf,
1058 &relay_file_operations);
1059 *is_global = 1;
1060 return buf_file;
1061}
1062
1063static int remove_buf_file_handler(struct dentry *dentry)
1064{
1065 debugfs_remove(dentry);
1066
1067 return 0;
1068}
1069
f84d1b49 1070static struct rchan_callbacks rfs_spec_scan_cb = {
f65c0825
SM
1071 .create_buf_file = create_buf_file_handler,
1072 .remove_buf_file = remove_buf_file_handler,
1073};
1074
1075/*********************/
1076/* Debug Init/Deinit */
1077/*********************/
1078
67dc74f1 1079void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv)
f65c0825 1080{
1077ec47 1081 if (spec_priv->rfs_chan_spec_scan) {
1111d426
OR
1082 relay_close(spec_priv->rfs_chan_spec_scan);
1083 spec_priv->rfs_chan_spec_scan = NULL;
f65c0825
SM
1084 }
1085}
67dc74f1 1086EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug);
f65c0825 1087
67dc74f1
OR
1088void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv,
1089 struct dentry *debugfs_phy)
f65c0825 1090{
1111d426 1091 spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan",
c10b75af 1092 debugfs_phy,
f65c0825
SM
1093 1024, 256, &rfs_spec_scan_cb,
1094 NULL);
40bea976
MP
1095 if (!spec_priv->rfs_chan_spec_scan)
1096 return;
1097
f65c0825
SM
1098 debugfs_create_file("spectral_scan_ctl",
1099 S_IRUSR | S_IWUSR,
1111d426 1100 debugfs_phy, spec_priv,
f65c0825
SM
1101 &fops_spec_scan_ctl);
1102 debugfs_create_file("spectral_short_repeat",
1103 S_IRUSR | S_IWUSR,
1111d426 1104 debugfs_phy, spec_priv,
f65c0825
SM
1105 &fops_spectral_short_repeat);
1106 debugfs_create_file("spectral_count",
1107 S_IRUSR | S_IWUSR,
1111d426 1108 debugfs_phy, spec_priv,
f65c0825
SM
1109 &fops_spectral_count);
1110 debugfs_create_file("spectral_period",
1111 S_IRUSR | S_IWUSR,
1111d426 1112 debugfs_phy, spec_priv,
f65c0825
SM
1113 &fops_spectral_period);
1114 debugfs_create_file("spectral_fft_period",
1115 S_IRUSR | S_IWUSR,
1111d426 1116 debugfs_phy, spec_priv,
f65c0825
SM
1117 &fops_spectral_fft_period);
1118}
67dc74f1 1119EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug);