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>
20 static s8 fix_rssi_inv_only(u8 rssi_val)
27 static void ath_debug_send_fft_sample(struct ath_spec_scan_priv *spec_priv,
28 struct fft_sample_tlv *fft_sample_tlv)
31 if (!spec_priv->rfs_chan_spec_scan)
34 length = __be16_to_cpu(fft_sample_tlv->length) +
35 sizeof(*fft_sample_tlv);
36 relay_write(spec_priv->rfs_chan_spec_scan, fft_sample_tlv, length);
39 /* returns 1 if this was a spectral frame, even if not handled. */
40 int ath_cmn_process_fft(struct ath_spec_scan_priv *spec_priv, struct ieee80211_hdr *hdr,
41 struct ath_rx_status *rs, u64 tsf)
43 struct ath_hw *ah = spec_priv->ah;
44 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
45 u8 num_bins, *bins, *vdata = (u8 *)hdr;
46 struct fft_sample_ht20 fft_sample_20;
47 struct fft_sample_ht20_40 fft_sample_40;
48 struct fft_sample_tlv *tlv;
49 struct ath_radar_info *radar_info;
50 int len = rs->rs_datalen;
52 u16 fft_len, length, freq = ah->curchan->chan->center_freq;
53 enum nl80211_channel_type chan_type;
55 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
56 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
57 * yet, but this is supposed to be possible as well.
59 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
60 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
61 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
64 /* check if spectral scan bit is set. This does not have to be checked
65 * if received through a SPECTRAL phy error, but shouldn't hurt.
67 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
68 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
71 chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef);
72 if ((chan_type == NL80211_CHAN_HT40MINUS) ||
73 (chan_type == NL80211_CHAN_HT40PLUS)) {
74 fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
75 num_bins = SPECTRAL_HT20_40_NUM_BINS;
76 bins = (u8 *)fft_sample_40.data;
78 fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
79 num_bins = SPECTRAL_HT20_NUM_BINS;
80 bins = (u8 *)fft_sample_20.data;
83 /* Variation in the data length is possible and will be fixed later */
84 if ((len > fft_len + 2) || (len < fft_len - 1))
87 switch (len - fft_len) {
89 /* length correct, nothing to do. */
90 memcpy(bins, vdata, num_bins);
93 /* first byte missing, duplicate it. */
94 memcpy(&bins[1], vdata, num_bins - 1);
98 /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
99 memcpy(bins, vdata, 30);
100 bins[30] = vdata[31];
101 memcpy(&bins[31], &vdata[33], num_bins - 31);
104 /* MAC added 2 extra bytes AND first byte is missing. */
106 memcpy(&bins[1], vdata, 30);
107 bins[31] = vdata[31];
108 memcpy(&bins[32], &vdata[33], num_bins - 32);
114 /* DC value (value in the middle) is the blind spot of the spectral
115 * sample and invalid, interpolate it.
117 dc_pos = num_bins / 2;
118 bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
120 if ((chan_type == NL80211_CHAN_HT40MINUS) ||
121 (chan_type == NL80211_CHAN_HT40PLUS)) {
122 s8 lower_rssi, upper_rssi;
124 u8 lower_max_index, upper_max_index;
125 u8 lower_bitmap_w, upper_bitmap_w;
126 u16 lower_mag, upper_mag;
127 struct ath9k_hw_cal_data *caldata = ah->caldata;
128 struct ath_ht20_40_mag_info *mag_info;
131 ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
132 caldata->nfCalHist[3].privNF);
134 ext_nf = ATH_DEFAULT_NOISE_FLOOR;
136 length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv);
137 fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40;
138 fft_sample_40.tlv.length = __cpu_to_be16(length);
139 fft_sample_40.freq = __cpu_to_be16(freq);
140 fft_sample_40.channel_type = chan_type;
142 if (chan_type == NL80211_CHAN_HT40PLUS) {
143 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
144 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
146 fft_sample_40.lower_noise = ah->noise;
147 fft_sample_40.upper_noise = ext_nf;
149 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
150 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
152 fft_sample_40.lower_noise = ext_nf;
153 fft_sample_40.upper_noise = ah->noise;
155 fft_sample_40.lower_rssi = lower_rssi;
156 fft_sample_40.upper_rssi = upper_rssi;
158 mag_info = ((struct ath_ht20_40_mag_info *)radar_info) - 1;
159 lower_mag = spectral_max_magnitude(mag_info->lower_bins);
160 upper_mag = spectral_max_magnitude(mag_info->upper_bins);
161 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
162 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
163 lower_max_index = spectral_max_index(mag_info->lower_bins);
164 upper_max_index = spectral_max_index(mag_info->upper_bins);
165 fft_sample_40.lower_max_index = lower_max_index;
166 fft_sample_40.upper_max_index = upper_max_index;
167 lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
168 upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
169 fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
170 fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
171 fft_sample_40.max_exp = mag_info->max_exp & 0xf;
173 fft_sample_40.tsf = __cpu_to_be64(tsf);
175 tlv = (struct fft_sample_tlv *)&fft_sample_40;
177 u8 max_index, bitmap_w;
179 struct ath_ht20_mag_info *mag_info;
181 length = sizeof(fft_sample_20) - sizeof(struct fft_sample_tlv);
182 fft_sample_20.tlv.type = ATH_FFT_SAMPLE_HT20;
183 fft_sample_20.tlv.length = __cpu_to_be16(length);
184 fft_sample_20.freq = __cpu_to_be16(freq);
186 fft_sample_20.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
187 fft_sample_20.noise = ah->noise;
189 mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
190 magnitude = spectral_max_magnitude(mag_info->all_bins);
191 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
192 max_index = spectral_max_index(mag_info->all_bins);
193 fft_sample_20.max_index = max_index;
194 bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
195 fft_sample_20.bitmap_weight = bitmap_w;
196 fft_sample_20.max_exp = mag_info->max_exp & 0xf;
198 fft_sample_20.tsf = __cpu_to_be64(tsf);
200 tlv = (struct fft_sample_tlv *)&fft_sample_20;
203 ath_debug_send_fft_sample(spec_priv, tlv);
207 EXPORT_SYMBOL(ath_cmn_process_fft);
209 /*********************/
210 /* spectral_scan_ctl */
211 /*********************/
213 static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
214 size_t count, loff_t *ppos)
216 struct ath_spec_scan_priv *spec_priv = file->private_data;
220 switch (spec_priv->spectral_mode) {
221 case SPECTRAL_DISABLED:
224 case SPECTRAL_BACKGROUND:
227 case SPECTRAL_CHANSCAN:
230 case SPECTRAL_MANUAL:
235 return simple_read_from_buffer(user_buf, count, ppos, mode, len);
238 void ath9k_cmn_spectral_scan_trigger(struct ath_common *common,
239 struct ath_spec_scan_priv *spec_priv)
241 struct ath_hw *ah = spec_priv->ah;
244 if (config_enabled(CONFIG_ATH9K_TX99))
247 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
248 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
252 ath_ps_ops(common)->wakeup(common);
253 rxfilter = ath9k_hw_getrxfilter(ah);
254 ath9k_hw_setrxfilter(ah, rxfilter |
255 ATH9K_RX_FILTER_PHYRADAR |
256 ATH9K_RX_FILTER_PHYERR);
258 /* TODO: usually this should not be neccesary, but for some reason
259 * (or in some mode?) the trigger must be called after the
260 * configuration, otherwise the register will have its values reset
261 * (on my ar9220 to value 0x01002310)
263 ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode);
264 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
265 ath_ps_ops(common)->restore(common);
267 EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger);
269 int ath9k_cmn_spectral_scan_config(struct ath_common *common,
270 struct ath_spec_scan_priv *spec_priv,
271 enum spectral_mode spectral_mode)
273 struct ath_hw *ah = spec_priv->ah;
275 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
276 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
280 switch (spectral_mode) {
281 case SPECTRAL_DISABLED:
282 spec_priv->spec_config.enabled = 0;
284 case SPECTRAL_BACKGROUND:
285 /* send endless samples.
286 * TODO: is this really useful for "background"?
288 spec_priv->spec_config.endless = 1;
289 spec_priv->spec_config.enabled = 1;
291 case SPECTRAL_CHANSCAN:
292 case SPECTRAL_MANUAL:
293 spec_priv->spec_config.endless = 0;
294 spec_priv->spec_config.enabled = 1;
300 ath_ps_ops(common)->wakeup(common);
301 ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config);
302 ath_ps_ops(common)->restore(common);
304 spec_priv->spectral_mode = spectral_mode;
308 EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config);
310 static ssize_t write_file_spec_scan_ctl(struct file *file,
311 const char __user *user_buf,
312 size_t count, loff_t *ppos)
314 struct ath_spec_scan_priv *spec_priv = file->private_data;
315 struct ath_common *common = ath9k_hw_common(spec_priv->ah);
319 if (config_enabled(CONFIG_ATH9K_TX99))
322 len = min(count, sizeof(buf) - 1);
323 if (copy_from_user(buf, user_buf, len))
328 if (strncmp("trigger", buf, 7) == 0) {
329 ath9k_cmn_spectral_scan_trigger(common, spec_priv);
330 } else if (strncmp("background", buf, 10) == 0) {
331 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND);
332 ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
333 } else if (strncmp("chanscan", buf, 8) == 0) {
334 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN);
335 ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
336 } else if (strncmp("manual", buf, 6) == 0) {
337 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL);
338 ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
339 } else if (strncmp("disable", buf, 7) == 0) {
340 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED);
341 ath_dbg(common, CONFIG, "spectral scan: disabled\n");
349 static const struct file_operations fops_spec_scan_ctl = {
350 .read = read_file_spec_scan_ctl,
351 .write = write_file_spec_scan_ctl,
353 .owner = THIS_MODULE,
354 .llseek = default_llseek,
357 /*************************/
358 /* spectral_short_repeat */
359 /*************************/
361 static ssize_t read_file_spectral_short_repeat(struct file *file,
362 char __user *user_buf,
363 size_t count, loff_t *ppos)
365 struct ath_spec_scan_priv *spec_priv = file->private_data;
369 len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat);
370 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
373 static ssize_t write_file_spectral_short_repeat(struct file *file,
374 const char __user *user_buf,
375 size_t count, loff_t *ppos)
377 struct ath_spec_scan_priv *spec_priv = file->private_data;
382 len = min(count, sizeof(buf) - 1);
383 if (copy_from_user(buf, user_buf, len))
387 if (kstrtoul(buf, 0, &val))
393 spec_priv->spec_config.short_repeat = val;
397 static const struct file_operations fops_spectral_short_repeat = {
398 .read = read_file_spectral_short_repeat,
399 .write = write_file_spectral_short_repeat,
401 .owner = THIS_MODULE,
402 .llseek = default_llseek,
409 static ssize_t read_file_spectral_count(struct file *file,
410 char __user *user_buf,
411 size_t count, loff_t *ppos)
413 struct ath_spec_scan_priv *spec_priv = file->private_data;
417 len = sprintf(buf, "%d\n", spec_priv->spec_config.count);
418 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
421 static ssize_t write_file_spectral_count(struct file *file,
422 const char __user *user_buf,
423 size_t count, loff_t *ppos)
425 struct ath_spec_scan_priv *spec_priv = file->private_data;
430 len = min(count, sizeof(buf) - 1);
431 if (copy_from_user(buf, user_buf, len))
435 if (kstrtoul(buf, 0, &val))
441 spec_priv->spec_config.count = val;
445 static const struct file_operations fops_spectral_count = {
446 .read = read_file_spectral_count,
447 .write = write_file_spectral_count,
449 .owner = THIS_MODULE,
450 .llseek = default_llseek,
453 /*******************/
454 /* spectral_period */
455 /*******************/
457 static ssize_t read_file_spectral_period(struct file *file,
458 char __user *user_buf,
459 size_t count, loff_t *ppos)
461 struct ath_spec_scan_priv *spec_priv = file->private_data;
465 len = sprintf(buf, "%d\n", spec_priv->spec_config.period);
466 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
469 static ssize_t write_file_spectral_period(struct file *file,
470 const char __user *user_buf,
471 size_t count, loff_t *ppos)
473 struct ath_spec_scan_priv *spec_priv = file->private_data;
478 len = min(count, sizeof(buf) - 1);
479 if (copy_from_user(buf, user_buf, len))
483 if (kstrtoul(buf, 0, &val))
489 spec_priv->spec_config.period = val;
493 static const struct file_operations fops_spectral_period = {
494 .read = read_file_spectral_period,
495 .write = write_file_spectral_period,
497 .owner = THIS_MODULE,
498 .llseek = default_llseek,
501 /***********************/
502 /* spectral_fft_period */
503 /***********************/
505 static ssize_t read_file_spectral_fft_period(struct file *file,
506 char __user *user_buf,
507 size_t count, loff_t *ppos)
509 struct ath_spec_scan_priv *spec_priv = file->private_data;
513 len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period);
514 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
517 static ssize_t write_file_spectral_fft_period(struct file *file,
518 const char __user *user_buf,
519 size_t count, loff_t *ppos)
521 struct ath_spec_scan_priv *spec_priv = file->private_data;
526 len = min(count, sizeof(buf) - 1);
527 if (copy_from_user(buf, user_buf, len))
531 if (kstrtoul(buf, 0, &val))
537 spec_priv->spec_config.fft_period = val;
541 static const struct file_operations fops_spectral_fft_period = {
542 .read = read_file_spectral_fft_period,
543 .write = write_file_spectral_fft_period,
545 .owner = THIS_MODULE,
546 .llseek = default_llseek,
549 /*******************/
550 /* Relay interface */
551 /*******************/
553 static struct dentry *create_buf_file_handler(const char *filename,
554 struct dentry *parent,
556 struct rchan_buf *buf,
559 struct dentry *buf_file;
561 buf_file = debugfs_create_file(filename, mode, parent, buf,
562 &relay_file_operations);
567 static int remove_buf_file_handler(struct dentry *dentry)
569 debugfs_remove(dentry);
574 static struct rchan_callbacks rfs_spec_scan_cb = {
575 .create_buf_file = create_buf_file_handler,
576 .remove_buf_file = remove_buf_file_handler,
579 /*********************/
580 /* Debug Init/Deinit */
581 /*********************/
583 void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv)
585 if (config_enabled(CONFIG_ATH9K_DEBUGFS)) {
586 relay_close(spec_priv->rfs_chan_spec_scan);
587 spec_priv->rfs_chan_spec_scan = NULL;
590 EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug);
592 void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv,
593 struct dentry *debugfs_phy)
595 spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan",
597 1024, 256, &rfs_spec_scan_cb,
599 debugfs_create_file("spectral_scan_ctl",
601 debugfs_phy, spec_priv,
602 &fops_spec_scan_ctl);
603 debugfs_create_file("spectral_short_repeat",
605 debugfs_phy, spec_priv,
606 &fops_spectral_short_repeat);
607 debugfs_create_file("spectral_count",
609 debugfs_phy, spec_priv,
610 &fops_spectral_count);
611 debugfs_create_file("spectral_period",
613 debugfs_phy, spec_priv,
614 &fops_spectral_period);
615 debugfs_create_file("spectral_fft_period",
617 debugfs_phy, spec_priv,
618 &fops_spectral_fft_period);
620 EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug);