2 * Copyright (c) 2013 Qualcomm Atheros, Inc.
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.
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.
17 #include <linux/relay.h>
18 #include <linux/random.h>
21 static s8 fix_rssi_inv_only(u8 rssi_val)
28 static void ath_debug_send_fft_sample(struct ath_spec_scan_priv *spec_priv,
29 struct fft_sample_tlv *fft_sample_tlv)
32 if (!spec_priv->rfs_chan_spec_scan)
35 length = __be16_to_cpu(fft_sample_tlv->length) +
36 sizeof(*fft_sample_tlv);
37 relay_write(spec_priv->rfs_chan_spec_scan, fft_sample_tlv, length);
40 typedef int (ath_cmn_fft_idx_validator) (u8 *sample_end, int bytes_read);
43 ath_cmn_max_idx_verify_ht20_fft(u8 *sample_end, int bytes_read)
45 struct ath_ht20_mag_info *mag_info;
51 /* Sanity check so that we don't read outside the read
54 if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN - 1)
57 mag_info = (struct ath_ht20_mag_info *) (sample_end -
58 sizeof(struct ath_ht20_mag_info) + 1);
60 sample = sample_end - SPECTRAL_HT20_SAMPLE_LEN + 1;
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);
66 max_exp = mag_info->max_exp & 0xf;
68 /* Don't try to read something outside the read buffer
69 * in case of a missing byte (so bins[0] will be outside
72 if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN && max_index < 1)
75 if (sample[max_index] != (max_magnitude >> max_exp))
82 ath_cmn_max_idx_verify_ht20_40_fft(u8 *sample_end, int bytes_read)
84 struct ath_ht20_40_mag_info *mag_info;
86 u16 lower_mag, upper_mag;
87 u8 lower_max_index, upper_max_index;
89 int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
91 /* Sanity check so that we don't read outside the read
94 if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN - 1)
97 mag_info = (struct ath_ht20_40_mag_info *) (sample_end -
98 sizeof(struct ath_ht20_40_mag_info) + 1);
100 sample = sample_end - SPECTRAL_HT20_40_SAMPLE_LEN + 1;
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);
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);
110 max_exp = mag_info->max_exp & 0xf;
112 /* Don't try to read something outside the read buffer
113 * in case of a missing byte (so bins[0] will be outside
116 if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN &&
117 ((upper_max_index < 1) || (lower_max_index < 1)))
120 /* Some time hardware messes up the index and adds
121 * the index of the middle point (dc_pos). Try to fix it.
123 if ((upper_max_index - dc_pos > 0) &&
124 (sample[upper_max_index] == (upper_mag >> max_exp)))
125 upper_max_index -= dc_pos;
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;
131 if ((sample[upper_max_index + dc_pos] != (upper_mag >> max_exp)) ||
132 (sample[lower_max_index] != (lower_mag >> max_exp)))
138 typedef 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);
143 ath_cmn_process_ht20_fft(struct ath_rx_status *rs,
144 struct ath_spec_scan_priv *spec_priv,
146 u64 tsf, u16 freq, int chan_type)
148 struct fft_sample_ht20 fft_sample_20;
149 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
150 struct ath_hw *ah = spec_priv->ah;
151 struct ath_ht20_mag_info *mag_info;
152 struct fft_sample_tlv *tlv;
155 int dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
156 u16 magnitude, tmp_mag, length;
157 u8 max_index, bitmap_w, max_exp;
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;
166 mag_info = (struct ath_ht20_mag_info *) (sample_buf +
167 SPECTRAL_HT20_NUM_BINS);
169 magnitude = spectral_max_magnitude(mag_info->all_bins);
170 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
172 max_index = spectral_max_index(mag_info->all_bins,
173 SPECTRAL_HT20_NUM_BINS);
174 fft_sample_20.max_index = max_index;
176 bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
177 fft_sample_20.bitmap_weight = bitmap_w;
179 max_exp = mag_info->max_exp & 0xf;
180 fft_sample_20.max_exp = max_exp;
182 fft_sample_20.tsf = __cpu_to_be64(tsf);
184 memcpy(fft_sample_20.data, sample_buf, SPECTRAL_HT20_NUM_BINS);
186 ath_dbg(common, SPECTRAL_SCAN, "FFT HT20 frame: max mag 0x%X,"
188 magnitude >> max_exp,
191 if (fft_sample_20.data[max_index] != (magnitude >> max_exp)) {
192 ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
196 /* DC value (value in the middle) is the blind spot of the spectral
197 * sample and invalid, interpolate it.
199 fft_sample_20.data[dc_pos] = (fft_sample_20.data[dc_pos + 1] +
200 fft_sample_20.data[dc_pos - 1]) / 2;
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).
206 if (max_index == dc_pos) {
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;
215 magnitude = tmp_mag << max_exp;
216 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
218 ath_dbg(common, SPECTRAL_SCAN,
219 "Calculated new lower max 0x%X at %i\n",
220 tmp_mag, fft_sample_20.max_index);
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);
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]);
239 tlv = (struct fft_sample_tlv *)&fft_sample_20;
241 ath_debug_send_fft_sample(spec_priv, tlv);
247 ath_cmn_process_ht20_40_fft(struct ath_rx_status *rs,
248 struct ath_spec_scan_priv *spec_priv,
250 u64 tsf, u16 freq, int chan_type)
252 struct fft_sample_ht20_40 fft_sample_40;
253 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
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;
262 u16 lower_mag, upper_mag, tmp_mag, length;
263 s8 lower_rssi, upper_rssi;
264 u8 lower_max_index, upper_max_index;
265 u8 lower_bitmap_w, upper_bitmap_w, max_exp;
268 ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
269 caldata->nfCalHist[3].privNF);
271 ext_nf = ATH_DEFAULT_NOISE_FLOOR;
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;
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]);
283 fft_sample_40.lower_noise = ah->noise;
284 fft_sample_40.upper_noise = ext_nf;
286 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
287 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
289 fft_sample_40.lower_noise = ext_nf;
290 fft_sample_40.upper_noise = ah->noise;
293 fft_sample_40.lower_rssi = lower_rssi;
294 fft_sample_40.upper_rssi = upper_rssi;
296 mag_info = (struct ath_ht20_40_mag_info *) (sample_buf +
297 SPECTRAL_HT20_40_NUM_BINS);
299 lower_mag = spectral_max_magnitude(mag_info->lower_bins);
300 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
302 upper_mag = spectral_max_magnitude(mag_info->upper_bins);
303 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
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;
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;
313 lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
314 fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
316 upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
317 fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
319 max_exp = mag_info->max_exp & 0xf;
320 fft_sample_40.max_exp = max_exp;
322 fft_sample_40.tsf = __cpu_to_be64(tsf);
324 memcpy(fft_sample_40.data, sample_buf, SPECTRAL_HT20_40_NUM_BINS);
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,
331 upper_mag >> max_exp,
334 /* Some time hardware messes up the index and adds
335 * the index of the middle point (dc_pos). Try to fix it.
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;
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;
350 /* Check if we got the expected magnitude values at
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");
361 /* DC value (value in the middle) is the blind spot of the spectral
362 * sample and invalid, interpolate it.
364 fft_sample_40.data[dc_pos] = (fft_sample_40.data[dc_pos + 1] +
365 fft_sample_40.data[dc_pos - 1]) / 2;
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).
371 if (lower_max_index == dc_pos) {
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;
380 lower_mag = tmp_mag << max_exp;
381 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
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);
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);
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]);
401 if (upper_max_index == dc_pos) {
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;
409 upper_mag = tmp_mag << max_exp;
410 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
412 ath_dbg(common, SPECTRAL_SCAN,
413 "Calculated new upper max 0x%X at %i\n",
414 tmp_mag, fft_sample_40.upper_max_index);
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);
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]);
434 tlv = (struct fft_sample_tlv *)&fft_sample_40;
436 ath_debug_send_fft_sample(spec_priv, tlv);
442 ath_cmn_copy_fft_frame(u8 *in, u8 *out, int sample_len, int sample_bytes)
444 switch (sample_bytes - sample_len) {
446 /* First byte missing */
451 /* Length correct, nothing to do. */
452 memcpy(out, in, sample_len);
455 /* MAC added 2 extra bytes AND first byte
458 memcpy(&out[1], in, 30);
460 memcpy(&out[32], &in[33],
464 /* MAC added 2 extra bytes at bin 30 and 32,
469 memcpy(&out[31], &in[33],
478 ath_cmn_is_fft_buf_full(struct ath_spec_scan_priv *spec_priv)
482 struct rchan_buf *buf;
483 struct rchan *rc = spec_priv->rfs_chan_spec_scan;
485 for_each_possible_cpu(i) {
486 if ((buf = *per_cpu_ptr(rc->buf, i))) {
487 ret += relay_buf_full(buf);
497 /* returns 1 if this was a spectral frame, even if not handled. */
498 int ath_cmn_process_fft(struct ath_spec_scan_priv *spec_priv, struct ieee80211_hdr *hdr,
499 struct ath_rx_status *rs, u64 tsf)
501 u8 sample_buf[SPECTRAL_SAMPLE_MAX_LEN] = {0};
502 struct ath_hw *ah = spec_priv->ah;
503 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
504 u8 num_bins, *vdata = (u8 *)hdr;
505 struct ath_radar_info *radar_info;
506 int len = rs->rs_datalen;
510 int sample_bytes = 0;
512 u16 fft_len, sample_len, freq = ah->curchan->chan->center_freq;
513 enum nl80211_channel_type chan_type;
514 ath_cmn_fft_idx_validator *fft_idx_validator;
515 ath_cmn_fft_sample_handler *fft_handler;
517 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
518 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
519 * yet, but this is supposed to be possible as well.
521 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
522 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
523 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
526 /* check if spectral scan bit is set. This does not have to be checked
527 * if received through a SPECTRAL phy error, but shouldn't hurt.
529 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
530 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
533 if (!spec_priv->rfs_chan_spec_scan)
536 /* Output buffers are full, no need to process anything
537 * since there is no space to put the result anyway
539 ret = ath_cmn_is_fft_buf_full(spec_priv);
541 ath_dbg(common, SPECTRAL_SCAN, "FFT report ignored, no space "
542 "left on output buffers\n");
546 chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef);
547 if ((chan_type == NL80211_CHAN_HT40MINUS) ||
548 (chan_type == NL80211_CHAN_HT40PLUS)) {
549 fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
550 sample_len = SPECTRAL_HT20_40_SAMPLE_LEN;
551 num_bins = SPECTRAL_HT20_40_NUM_BINS;
552 fft_idx_validator = &ath_cmn_max_idx_verify_ht20_40_fft;
553 fft_handler = &ath_cmn_process_ht20_40_fft;
555 fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
556 sample_len = SPECTRAL_HT20_SAMPLE_LEN;
557 num_bins = SPECTRAL_HT20_NUM_BINS;
558 fft_idx_validator = ath_cmn_max_idx_verify_ht20_fft;
559 fft_handler = &ath_cmn_process_ht20_fft;
562 ath_dbg(common, SPECTRAL_SCAN, "Got radar dump bw_info: 0x%X,"
563 "len: %i fft_len: %i\n",
564 radar_info->pulse_bw_info,
567 sample_start = vdata;
568 for (i = 0; i < len - 2; i++) {
571 /* Only a single sample received, no need to look
572 * for the sample's end, do the correction based
573 * on the packet's length instead. Note that hw
574 * will always put the radar_info structure on
577 if (len <= fft_len + 2) {
578 sample_bytes = len - sizeof(struct ath_radar_info);
582 /* Search for the end of the FFT frame between
583 * sample_len - 1 and sample_len + 2. exp_max is 3
584 * bits long and it's the only value on the last
585 * byte of the frame so since it'll be smaller than
586 * the next byte (the first bin of the next sample)
587 * 90% of the time, we can use it as a separator.
589 if (vdata[i] <= 0x7 && sample_bytes >= sample_len - 1) {
591 /* Got a frame length within boundaries, there are
592 * four scenarios here:
594 * a) sample_len -> We got the correct length
595 * b) sample_len + 2 -> 2 bytes added around bin[31]
596 * c) sample_len - 1 -> The first byte is missing
597 * d) sample_len + 1 -> b + c at the same time
599 * When MAC adds 2 extra bytes, bin[31] and bin[32]
600 * have the same value, so we can use that for further
601 * verification in cases b and d.
604 /* Did we go too far ? If so we couldn't determine
605 * this sample's boundaries, discard any further
608 if ((sample_bytes > sample_len + 2) ||
609 ((sample_bytes > sample_len) &&
610 (sample_start[31] != sample_start[32])))
613 /* See if we got a valid frame by checking the
614 * consistency of mag_info fields. This is to
615 * prevent from "fixing" a correct frame.
616 * Failure is non-fatal, later frames may
619 if (!fft_idx_validator(&vdata[i], i)) {
620 ath_dbg(common, SPECTRAL_SCAN,
621 "Found valid fft frame at %i\n", i);
625 /* We expect 1 - 2 more bytes */
626 else if ((sample_start[31] == sample_start[32]) &&
627 (sample_bytes >= sample_len) &&
628 (sample_bytes < sample_len + 2) &&
629 (vdata[i + 1] <= 0x7))
632 /* Try to distinguish cases a and c */
633 else if ((sample_bytes == sample_len - 1) &&
634 (vdata[i + 1] <= 0x7))
641 ath_dbg(common, SPECTRAL_SCAN, "FFT frame len: %i\n",
644 /* Only try to fix a frame if it's the only one
645 * on the report, else just skip it.
647 if (sample_bytes != sample_len && len <= fft_len + 2) {
648 ath_cmn_copy_fft_frame(sample_start,
649 sample_buf, sample_len,
652 fft_handler(rs, spec_priv, sample_buf,
653 tsf, freq, chan_type);
655 memset(sample_buf, 0, SPECTRAL_SAMPLE_MAX_LEN);
657 /* Mix the received bins to the /dev/random
660 add_device_randomness(sample_buf, num_bins);
663 /* Process a normal frame */
664 if (sample_bytes == sample_len) {
665 ret = fft_handler(rs, spec_priv, sample_start,
666 tsf, freq, chan_type);
668 /* Mix the received bins to the /dev/random
671 add_device_randomness(sample_start, num_bins);
674 /* Short report processed, break out of the
677 if (len <= fft_len + 2)
680 sample_start = &vdata[i + 1];
682 /* -1 to grab sample_len -1, -2 since
683 * they 'll get increased by one. In case
684 * of failure try to recover by going byte
689 sample_bytes = num_bins - 2;
696 if (len - i != sizeof(struct ath_radar_info))
697 ath_dbg(common, SPECTRAL_SCAN, "FFT report truncated"
698 "(bytes left: %i)\n",
702 EXPORT_SYMBOL(ath_cmn_process_fft);
704 /*********************/
705 /* spectral_scan_ctl */
706 /*********************/
708 static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
709 size_t count, loff_t *ppos)
711 struct ath_spec_scan_priv *spec_priv = file->private_data;
715 switch (spec_priv->spectral_mode) {
716 case SPECTRAL_DISABLED:
719 case SPECTRAL_BACKGROUND:
722 case SPECTRAL_CHANSCAN:
725 case SPECTRAL_MANUAL:
730 return simple_read_from_buffer(user_buf, count, ppos, mode, len);
733 void ath9k_cmn_spectral_scan_trigger(struct ath_common *common,
734 struct ath_spec_scan_priv *spec_priv)
736 struct ath_hw *ah = spec_priv->ah;
739 if (IS_ENABLED(CONFIG_ATH9K_TX99))
742 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
743 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
747 if (!spec_priv->spec_config.enabled)
750 ath_ps_ops(common)->wakeup(common);
751 rxfilter = ath9k_hw_getrxfilter(ah);
752 ath9k_hw_setrxfilter(ah, rxfilter |
753 ATH9K_RX_FILTER_PHYRADAR |
754 ATH9K_RX_FILTER_PHYERR);
756 /* TODO: usually this should not be neccesary, but for some reason
757 * (or in some mode?) the trigger must be called after the
758 * configuration, otherwise the register will have its values reset
759 * (on my ar9220 to value 0x01002310)
761 ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode);
762 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
763 ath_ps_ops(common)->restore(common);
765 EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger);
767 int ath9k_cmn_spectral_scan_config(struct ath_common *common,
768 struct ath_spec_scan_priv *spec_priv,
769 enum spectral_mode spectral_mode)
771 struct ath_hw *ah = spec_priv->ah;
773 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
774 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
778 switch (spectral_mode) {
779 case SPECTRAL_DISABLED:
780 spec_priv->spec_config.enabled = 0;
782 case SPECTRAL_BACKGROUND:
783 /* send endless samples.
784 * TODO: is this really useful for "background"?
786 spec_priv->spec_config.endless = 1;
787 spec_priv->spec_config.enabled = 1;
789 case SPECTRAL_CHANSCAN:
790 case SPECTRAL_MANUAL:
791 spec_priv->spec_config.endless = 0;
792 spec_priv->spec_config.enabled = 1;
798 ath_ps_ops(common)->wakeup(common);
799 ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config);
800 ath_ps_ops(common)->restore(common);
802 spec_priv->spectral_mode = spectral_mode;
806 EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config);
808 static ssize_t write_file_spec_scan_ctl(struct file *file,
809 const char __user *user_buf,
810 size_t count, loff_t *ppos)
812 struct ath_spec_scan_priv *spec_priv = file->private_data;
813 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
817 if (IS_ENABLED(CONFIG_ATH9K_TX99))
820 len = min(count, sizeof(buf) - 1);
821 if (copy_from_user(buf, user_buf, len))
826 if (strncmp("trigger", buf, 7) == 0) {
827 ath9k_cmn_spectral_scan_trigger(common, spec_priv);
828 } else if (strncmp("background", buf, 10) == 0) {
829 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND);
830 ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
831 } else if (strncmp("chanscan", buf, 8) == 0) {
832 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN);
833 ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
834 } else if (strncmp("manual", buf, 6) == 0) {
835 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL);
836 ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
837 } else if (strncmp("disable", buf, 7) == 0) {
838 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED);
839 ath_dbg(common, CONFIG, "spectral scan: disabled\n");
847 static const struct file_operations fops_spec_scan_ctl = {
848 .read = read_file_spec_scan_ctl,
849 .write = write_file_spec_scan_ctl,
851 .owner = THIS_MODULE,
852 .llseek = default_llseek,
855 /*************************/
856 /* spectral_short_repeat */
857 /*************************/
859 static ssize_t read_file_spectral_short_repeat(struct file *file,
860 char __user *user_buf,
861 size_t count, loff_t *ppos)
863 struct ath_spec_scan_priv *spec_priv = file->private_data;
867 len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat);
868 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
871 static ssize_t write_file_spectral_short_repeat(struct file *file,
872 const char __user *user_buf,
873 size_t count, loff_t *ppos)
875 struct ath_spec_scan_priv *spec_priv = file->private_data;
880 len = min(count, sizeof(buf) - 1);
881 if (copy_from_user(buf, user_buf, len))
885 if (kstrtoul(buf, 0, &val))
891 spec_priv->spec_config.short_repeat = val;
895 static const struct file_operations fops_spectral_short_repeat = {
896 .read = read_file_spectral_short_repeat,
897 .write = write_file_spectral_short_repeat,
899 .owner = THIS_MODULE,
900 .llseek = default_llseek,
907 static ssize_t read_file_spectral_count(struct file *file,
908 char __user *user_buf,
909 size_t count, loff_t *ppos)
911 struct ath_spec_scan_priv *spec_priv = file->private_data;
915 len = sprintf(buf, "%d\n", spec_priv->spec_config.count);
916 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
919 static ssize_t write_file_spectral_count(struct file *file,
920 const char __user *user_buf,
921 size_t count, loff_t *ppos)
923 struct ath_spec_scan_priv *spec_priv = file->private_data;
928 len = min(count, sizeof(buf) - 1);
929 if (copy_from_user(buf, user_buf, len))
933 if (kstrtoul(buf, 0, &val))
939 spec_priv->spec_config.count = val;
943 static const struct file_operations fops_spectral_count = {
944 .read = read_file_spectral_count,
945 .write = write_file_spectral_count,
947 .owner = THIS_MODULE,
948 .llseek = default_llseek,
951 /*******************/
952 /* spectral_period */
953 /*******************/
955 static ssize_t read_file_spectral_period(struct file *file,
956 char __user *user_buf,
957 size_t count, loff_t *ppos)
959 struct ath_spec_scan_priv *spec_priv = file->private_data;
963 len = sprintf(buf, "%d\n", spec_priv->spec_config.period);
964 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
967 static ssize_t write_file_spectral_period(struct file *file,
968 const char __user *user_buf,
969 size_t count, loff_t *ppos)
971 struct ath_spec_scan_priv *spec_priv = file->private_data;
976 len = min(count, sizeof(buf) - 1);
977 if (copy_from_user(buf, user_buf, len))
981 if (kstrtoul(buf, 0, &val))
987 spec_priv->spec_config.period = val;
991 static const struct file_operations fops_spectral_period = {
992 .read = read_file_spectral_period,
993 .write = write_file_spectral_period,
995 .owner = THIS_MODULE,
996 .llseek = default_llseek,
999 /***********************/
1000 /* spectral_fft_period */
1001 /***********************/
1003 static ssize_t read_file_spectral_fft_period(struct file *file,
1004 char __user *user_buf,
1005 size_t count, loff_t *ppos)
1007 struct ath_spec_scan_priv *spec_priv = file->private_data;
1011 len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period);
1012 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1015 static ssize_t write_file_spectral_fft_period(struct file *file,
1016 const char __user *user_buf,
1017 size_t count, loff_t *ppos)
1019 struct ath_spec_scan_priv *spec_priv = file->private_data;
1024 len = min(count, sizeof(buf) - 1);
1025 if (copy_from_user(buf, user_buf, len))
1029 if (kstrtoul(buf, 0, &val))
1035 spec_priv->spec_config.fft_period = val;
1039 static const struct file_operations fops_spectral_fft_period = {
1040 .read = read_file_spectral_fft_period,
1041 .write = write_file_spectral_fft_period,
1042 .open = simple_open,
1043 .owner = THIS_MODULE,
1044 .llseek = default_llseek,
1047 /*******************/
1048 /* Relay interface */
1049 /*******************/
1051 static struct dentry *create_buf_file_handler(const char *filename,
1052 struct dentry *parent,
1054 struct rchan_buf *buf,
1057 struct dentry *buf_file;
1059 buf_file = debugfs_create_file(filename, mode, parent, buf,
1060 &relay_file_operations);
1065 static int remove_buf_file_handler(struct dentry *dentry)
1067 debugfs_remove(dentry);
1072 static struct rchan_callbacks rfs_spec_scan_cb = {
1073 .create_buf_file = create_buf_file_handler,
1074 .remove_buf_file = remove_buf_file_handler,
1077 /*********************/
1078 /* Debug Init/Deinit */
1079 /*********************/
1081 void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv)
1083 if (spec_priv->rfs_chan_spec_scan) {
1084 relay_close(spec_priv->rfs_chan_spec_scan);
1085 spec_priv->rfs_chan_spec_scan = NULL;
1088 EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug);
1090 void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv,
1091 struct dentry *debugfs_phy)
1093 spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan",
1095 1024, 256, &rfs_spec_scan_cb,
1097 if (!spec_priv->rfs_chan_spec_scan)
1100 debugfs_create_file("spectral_scan_ctl",
1102 debugfs_phy, spec_priv,
1103 &fops_spec_scan_ctl);
1104 debugfs_create_file("spectral_short_repeat",
1106 debugfs_phy, spec_priv,
1107 &fops_spectral_short_repeat);
1108 debugfs_create_file("spectral_count",
1110 debugfs_phy, spec_priv,
1111 &fops_spectral_count);
1112 debugfs_create_file("spectral_period",
1114 debugfs_phy, spec_priv,
1115 &fops_spectral_period);
1116 debugfs_create_file("spectral_fft_period",
1118 debugfs_phy, spec_priv,
1119 &fops_spectral_fft_period);
1121 EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug);