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>
22 static void send_fft_sample(struct ath10k *ar,
23 const struct fft_sample_tlv *fft_sample_tlv)
27 if (!ar->spectral.rfs_chan_spec_scan)
30 length = __be16_to_cpu(fft_sample_tlv->length) +
31 sizeof(*fft_sample_tlv);
32 relay_write(ar->spectral.rfs_chan_spec_scan, fft_sample_tlv, length);
35 static uint8_t get_max_exp(s8 max_index, u16 max_magnitude, size_t bin_len,
43 /* peak index outside of bins */
44 if (dc_pos < max_index || -dc_pos >= max_index)
47 for (max_exp = 0; max_exp < 8; max_exp++) {
48 if (data[dc_pos + max_index] == (max_magnitude >> max_exp))
52 /* max_exp not found */
53 if (data[dc_pos + max_index] != (max_magnitude >> max_exp))
59 int ath10k_spectral_process_fft(struct ath10k *ar,
60 const struct wmi_phyerr *phyerr,
61 const struct phyerr_fft_report *fftr,
62 size_t bin_len, u64 tsf)
64 struct fft_sample_ath10k *fft_sample;
65 u8 buf[sizeof(*fft_sample) + SPECTRAL_ATH10K_MAX_NUM_BINS];
66 u16 freq1, freq2, total_gain_db, base_pwr_db, length, peak_mag;
71 fft_sample = (struct fft_sample_ath10k *)&buf;
73 if (bin_len < 64 || bin_len > SPECTRAL_ATH10K_MAX_NUM_BINS)
76 reg0 = __le32_to_cpu(fftr->reg0);
77 reg1 = __le32_to_cpu(fftr->reg1);
79 length = sizeof(*fft_sample) - sizeof(struct fft_sample_tlv) + bin_len;
80 fft_sample->tlv.type = ATH_FFT_SAMPLE_ATH10K;
81 fft_sample->tlv.length = __cpu_to_be16(length);
83 /* TODO: there might be a reason why the hardware reports 20/40/80 MHz,
84 * but the results/plots suggest that its actually 22/44/88 MHz.
86 switch (phyerr->chan_width_mhz) {
88 fft_sample->chan_width_mhz = 22;
91 fft_sample->chan_width_mhz = 44;
94 /* TODO: As experiments with an analogue sender and various
95 * configuaritions (fft-sizes of 64/128/256 and 20/40/80 Mhz)
96 * show, the particular configuration of 80 MHz/64 bins does
97 * not match with the other smaples at all. Until the reason
98 * for that is found, don't report these samples.
102 fft_sample->chan_width_mhz = 88;
105 fft_sample->chan_width_mhz = phyerr->chan_width_mhz;
108 fft_sample->relpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_RELPWR_DB);
109 fft_sample->avgpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_AVGPWR_DB);
111 peak_mag = MS(reg1, SEARCH_FFT_REPORT_REG1_PEAK_MAG);
112 fft_sample->max_magnitude = __cpu_to_be16(peak_mag);
113 fft_sample->max_index = MS(reg0, SEARCH_FFT_REPORT_REG0_PEAK_SIDX);
114 fft_sample->rssi = phyerr->rssi_combined;
116 total_gain_db = MS(reg0, SEARCH_FFT_REPORT_REG0_TOTAL_GAIN_DB);
117 base_pwr_db = MS(reg0, SEARCH_FFT_REPORT_REG0_BASE_PWR_DB);
118 fft_sample->total_gain_db = __cpu_to_be16(total_gain_db);
119 fft_sample->base_pwr_db = __cpu_to_be16(base_pwr_db);
121 freq1 = __le16_to_cpu(phyerr->freq1);
122 freq2 = __le16_to_cpu(phyerr->freq2);
123 fft_sample->freq1 = __cpu_to_be16(freq1);
124 fft_sample->freq2 = __cpu_to_be16(freq2);
126 chain_idx = MS(reg0, SEARCH_FFT_REPORT_REG0_FFT_CHN_IDX);
128 fft_sample->noise = __cpu_to_be16(
129 __le16_to_cpu(phyerr->nf_chains[chain_idx]));
132 bins += sizeof(*fftr);
134 fft_sample->tsf = __cpu_to_be64(tsf);
136 /* max_exp has been directly reported by previous hardware (ath9k),
137 * maybe its possible to get it by other means?
139 fft_sample->max_exp = get_max_exp(fft_sample->max_index, peak_mag,
142 memcpy(fft_sample->data, bins, bin_len);
144 /* DC value (value in the middle) is the blind spot of the spectral
145 * sample and invalid, interpolate it.
147 dc_pos = bin_len / 2;
148 fft_sample->data[dc_pos] = (fft_sample->data[dc_pos + 1] +
149 fft_sample->data[dc_pos - 1]) / 2;
151 send_fft_sample(ar, &fft_sample->tlv);
156 static struct ath10k_vif *ath10k_get_spectral_vdev(struct ath10k *ar)
158 struct ath10k_vif *arvif;
160 lockdep_assert_held(&ar->conf_mutex);
162 if (list_empty(&ar->arvifs))
165 /* if there already is a vif doing spectral, return that. */
166 list_for_each_entry(arvif, &ar->arvifs, list)
167 if (arvif->spectral_enabled)
170 /* otherwise, return the first vif. */
171 return list_first_entry(&ar->arvifs, typeof(*arvif), list);
174 static int ath10k_spectral_scan_trigger(struct ath10k *ar)
176 struct ath10k_vif *arvif;
180 lockdep_assert_held(&ar->conf_mutex);
182 arvif = ath10k_get_spectral_vdev(ar);
185 vdev_id = arvif->vdev_id;
187 if (ar->spectral.mode == SPECTRAL_DISABLED)
190 res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
191 WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
192 WMI_SPECTRAL_ENABLE_CMD_ENABLE);
196 res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
197 WMI_SPECTRAL_TRIGGER_CMD_TRIGGER,
198 WMI_SPECTRAL_ENABLE_CMD_ENABLE);
205 static int ath10k_spectral_scan_config(struct ath10k *ar,
206 enum ath10k_spectral_mode mode)
208 struct wmi_vdev_spectral_conf_arg arg;
209 struct ath10k_vif *arvif;
210 int vdev_id, count, res = 0;
212 lockdep_assert_held(&ar->conf_mutex);
214 arvif = ath10k_get_spectral_vdev(ar);
218 vdev_id = arvif->vdev_id;
220 arvif->spectral_enabled = (mode != SPECTRAL_DISABLED);
221 ar->spectral.mode = mode;
223 res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
224 WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
225 WMI_SPECTRAL_ENABLE_CMD_DISABLE);
227 ath10k_warn(ar, "failed to enable spectral scan: %d\n", res);
231 if (mode == SPECTRAL_DISABLED)
234 if (mode == SPECTRAL_BACKGROUND)
235 count = WMI_SPECTRAL_COUNT_DEFAULT;
237 count = max_t(u8, 1, ar->spectral.config.count);
239 arg.vdev_id = vdev_id;
240 arg.scan_count = count;
241 arg.scan_period = WMI_SPECTRAL_PERIOD_DEFAULT;
242 arg.scan_priority = WMI_SPECTRAL_PRIORITY_DEFAULT;
243 arg.scan_fft_size = ar->spectral.config.fft_size;
244 arg.scan_gc_ena = WMI_SPECTRAL_GC_ENA_DEFAULT;
245 arg.scan_restart_ena = WMI_SPECTRAL_RESTART_ENA_DEFAULT;
246 arg.scan_noise_floor_ref = WMI_SPECTRAL_NOISE_FLOOR_REF_DEFAULT;
247 arg.scan_init_delay = WMI_SPECTRAL_INIT_DELAY_DEFAULT;
248 arg.scan_nb_tone_thr = WMI_SPECTRAL_NB_TONE_THR_DEFAULT;
249 arg.scan_str_bin_thr = WMI_SPECTRAL_STR_BIN_THR_DEFAULT;
250 arg.scan_wb_rpt_mode = WMI_SPECTRAL_WB_RPT_MODE_DEFAULT;
251 arg.scan_rssi_rpt_mode = WMI_SPECTRAL_RSSI_RPT_MODE_DEFAULT;
252 arg.scan_rssi_thr = WMI_SPECTRAL_RSSI_THR_DEFAULT;
253 arg.scan_pwr_format = WMI_SPECTRAL_PWR_FORMAT_DEFAULT;
254 arg.scan_rpt_mode = WMI_SPECTRAL_RPT_MODE_DEFAULT;
255 arg.scan_bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;
256 arg.scan_dbm_adj = WMI_SPECTRAL_DBM_ADJ_DEFAULT;
257 arg.scan_chn_mask = WMI_SPECTRAL_CHN_MASK_DEFAULT;
259 res = ath10k_wmi_vdev_spectral_conf(ar, &arg);
261 ath10k_warn(ar, "failed to configure spectral scan: %d\n", res);
268 static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
269 size_t count, loff_t *ppos)
271 struct ath10k *ar = file->private_data;
274 enum ath10k_spectral_mode spectral_mode;
276 mutex_lock(&ar->conf_mutex);
277 spectral_mode = ar->spectral.mode;
278 mutex_unlock(&ar->conf_mutex);
280 switch (spectral_mode) {
281 case SPECTRAL_DISABLED:
284 case SPECTRAL_BACKGROUND:
287 case SPECTRAL_MANUAL:
293 return simple_read_from_buffer(user_buf, count, ppos, mode, len);
296 static ssize_t write_file_spec_scan_ctl(struct file *file,
297 const char __user *user_buf,
298 size_t count, loff_t *ppos)
300 struct ath10k *ar = file->private_data;
305 len = min(count, sizeof(buf) - 1);
306 if (copy_from_user(buf, user_buf, len))
311 mutex_lock(&ar->conf_mutex);
313 if (strncmp("trigger", buf, 7) == 0) {
314 if (ar->spectral.mode == SPECTRAL_MANUAL ||
315 ar->spectral.mode == SPECTRAL_BACKGROUND) {
316 /* reset the configuration to adopt possibly changed
319 res = ath10k_spectral_scan_config(ar,
322 ath10k_warn(ar, "failed to reconfigure spectral scan: %d\n",
325 res = ath10k_spectral_scan_trigger(ar);
327 ath10k_warn(ar, "failed to trigger spectral scan: %d\n",
333 } else if (strncmp("background", buf, 9) == 0) {
334 res = ath10k_spectral_scan_config(ar, SPECTRAL_BACKGROUND);
335 } else if (strncmp("manual", buf, 6) == 0) {
336 res = ath10k_spectral_scan_config(ar, SPECTRAL_MANUAL);
337 } else if (strncmp("disable", buf, 7) == 0) {
338 res = ath10k_spectral_scan_config(ar, SPECTRAL_DISABLED);
343 mutex_unlock(&ar->conf_mutex);
351 static const struct file_operations fops_spec_scan_ctl = {
352 .read = read_file_spec_scan_ctl,
353 .write = write_file_spec_scan_ctl,
355 .owner = THIS_MODULE,
356 .llseek = default_llseek,
359 static ssize_t read_file_spectral_count(struct file *file,
360 char __user *user_buf,
361 size_t count, loff_t *ppos)
363 struct ath10k *ar = file->private_data;
368 mutex_lock(&ar->conf_mutex);
369 spectral_count = ar->spectral.config.count;
370 mutex_unlock(&ar->conf_mutex);
372 len = sprintf(buf, "%d\n", spectral_count);
373 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
376 static ssize_t write_file_spectral_count(struct file *file,
377 const char __user *user_buf,
378 size_t count, loff_t *ppos)
380 struct ath10k *ar = file->private_data;
385 len = min(count, sizeof(buf) - 1);
386 if (copy_from_user(buf, user_buf, len))
390 if (kstrtoul(buf, 0, &val))
393 if (val < 0 || val > 255)
396 mutex_lock(&ar->conf_mutex);
397 ar->spectral.config.count = val;
398 mutex_unlock(&ar->conf_mutex);
403 static const struct file_operations fops_spectral_count = {
404 .read = read_file_spectral_count,
405 .write = write_file_spectral_count,
407 .owner = THIS_MODULE,
408 .llseek = default_llseek,
411 static ssize_t read_file_spectral_bins(struct file *file,
412 char __user *user_buf,
413 size_t count, loff_t *ppos)
415 struct ath10k *ar = file->private_data;
417 unsigned int len, bins, fft_size, bin_scale;
419 mutex_lock(&ar->conf_mutex);
421 fft_size = ar->spectral.config.fft_size;
422 bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;
423 bins = 1 << (fft_size - bin_scale);
425 mutex_unlock(&ar->conf_mutex);
427 len = sprintf(buf, "%d\n", bins);
428 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
431 static ssize_t write_file_spectral_bins(struct file *file,
432 const char __user *user_buf,
433 size_t count, loff_t *ppos)
435 struct ath10k *ar = file->private_data;
440 len = min(count, sizeof(buf) - 1);
441 if (copy_from_user(buf, user_buf, len))
445 if (kstrtoul(buf, 0, &val))
448 if (val < 64 || val > SPECTRAL_ATH10K_MAX_NUM_BINS)
451 if (!is_power_of_2(val))
454 mutex_lock(&ar->conf_mutex);
455 ar->spectral.config.fft_size = ilog2(val);
456 ar->spectral.config.fft_size += WMI_SPECTRAL_BIN_SCALE_DEFAULT;
457 mutex_unlock(&ar->conf_mutex);
462 static const struct file_operations fops_spectral_bins = {
463 .read = read_file_spectral_bins,
464 .write = write_file_spectral_bins,
466 .owner = THIS_MODULE,
467 .llseek = default_llseek,
470 static struct dentry *create_buf_file_handler(const char *filename,
471 struct dentry *parent,
473 struct rchan_buf *buf,
476 struct dentry *buf_file;
478 buf_file = debugfs_create_file(filename, mode, parent, buf,
479 &relay_file_operations);
484 static int remove_buf_file_handler(struct dentry *dentry)
486 debugfs_remove(dentry);
491 static struct rchan_callbacks rfs_spec_scan_cb = {
492 .create_buf_file = create_buf_file_handler,
493 .remove_buf_file = remove_buf_file_handler,
496 int ath10k_spectral_start(struct ath10k *ar)
498 struct ath10k_vif *arvif;
500 lockdep_assert_held(&ar->conf_mutex);
502 list_for_each_entry(arvif, &ar->arvifs, list)
503 arvif->spectral_enabled = 0;
505 ar->spectral.mode = SPECTRAL_DISABLED;
506 ar->spectral.config.count = WMI_SPECTRAL_COUNT_DEFAULT;
507 ar->spectral.config.fft_size = WMI_SPECTRAL_FFT_SIZE_DEFAULT;
512 int ath10k_spectral_vif_stop(struct ath10k_vif *arvif)
514 if (!arvif->spectral_enabled)
517 return ath10k_spectral_scan_config(arvif->ar, SPECTRAL_DISABLED);
520 int ath10k_spectral_create(struct ath10k *ar)
522 /* The buffer size covers whole channels in dual bands up to 128 bins.
523 * Scan with bigger than 128 bins needs to be run on single band each.
525 ar->spectral.rfs_chan_spec_scan = relay_open("spectral_scan",
526 ar->debug.debugfs_phy,
528 &rfs_spec_scan_cb, NULL);
529 debugfs_create_file("spectral_scan_ctl",
531 ar->debug.debugfs_phy, ar,
532 &fops_spec_scan_ctl);
533 debugfs_create_file("spectral_count",
535 ar->debug.debugfs_phy, ar,
536 &fops_spectral_count);
537 debugfs_create_file("spectral_bins",
539 ar->debug.debugfs_phy, ar,
540 &fops_spectral_bins);
545 void ath10k_spectral_destroy(struct ath10k *ar)
547 if (ar->spectral.rfs_chan_spec_scan) {
548 relay_close(ar->spectral.rfs_chan_spec_scan);
549 ar->spectral.rfs_chan_spec_scan = NULL;