spectral.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. #include <linux/relay.h>
  17. #include "core.h"
  18. #include "debug.h"
  19. #include "wmi-ops.h"
  20. static void send_fft_sample(struct ath10k *ar,
  21. const struct fft_sample_tlv *fft_sample_tlv)
  22. {
  23. int length;
  24. if (!ar->spectral.rfs_chan_spec_scan)
  25. return;
  26. length = __be16_to_cpu(fft_sample_tlv->length) +
  27. sizeof(*fft_sample_tlv);
  28. relay_write(ar->spectral.rfs_chan_spec_scan, fft_sample_tlv, length);
  29. }
  30. static uint8_t get_max_exp(s8 max_index, u16 max_magnitude, size_t bin_len,
  31. u8 *data)
  32. {
  33. int dc_pos;
  34. u8 max_exp;
  35. dc_pos = bin_len / 2;
  36. /* peak index outside of bins */
  37. if (dc_pos < max_index || -dc_pos >= max_index)
  38. return 0;
  39. for (max_exp = 0; max_exp < 8; max_exp++) {
  40. if (data[dc_pos + max_index] == (max_magnitude >> max_exp))
  41. break;
  42. }
  43. /* max_exp not found */
  44. if (data[dc_pos + max_index] != (max_magnitude >> max_exp))
  45. return 0;
  46. return max_exp;
  47. }
  48. int ath10k_spectral_process_fft(struct ath10k *ar,
  49. struct wmi_phyerr_ev_arg *phyerr,
  50. const struct phyerr_fft_report *fftr,
  51. size_t bin_len, u64 tsf)
  52. {
  53. struct fft_sample_ath10k *fft_sample;
  54. u8 buf[sizeof(*fft_sample) + SPECTRAL_ATH10K_MAX_NUM_BINS];
  55. u16 freq1, freq2, total_gain_db, base_pwr_db, length, peak_mag;
  56. u32 reg0, reg1;
  57. u8 chain_idx, *bins;
  58. int dc_pos;
  59. fft_sample = (struct fft_sample_ath10k *)&buf;
  60. if (bin_len < 64 || bin_len > SPECTRAL_ATH10K_MAX_NUM_BINS)
  61. return -EINVAL;
  62. /* qca99x0 reports bin size as 68 bytes (64 bytes + 4 bytes) in
  63. * report mode 2. First 64 bytes carries inband tones (-32 to +31)
  64. * and last 4 byte carries band edge detection data (+32) mainly
  65. * used in radar detection purpose. Strip last 4 byte to make bin
  66. * size is valid one.
  67. */
  68. if (bin_len == 68)
  69. bin_len -= 4;
  70. reg0 = __le32_to_cpu(fftr->reg0);
  71. reg1 = __le32_to_cpu(fftr->reg1);
  72. length = sizeof(*fft_sample) - sizeof(struct fft_sample_tlv) + bin_len;
  73. fft_sample->tlv.type = ATH_FFT_SAMPLE_ATH10K;
  74. fft_sample->tlv.length = __cpu_to_be16(length);
  75. /* TODO: there might be a reason why the hardware reports 20/40/80 MHz,
  76. * but the results/plots suggest that its actually 22/44/88 MHz.
  77. */
  78. switch (phyerr->chan_width_mhz) {
  79. case 20:
  80. fft_sample->chan_width_mhz = 22;
  81. break;
  82. case 40:
  83. fft_sample->chan_width_mhz = 44;
  84. break;
  85. case 80:
  86. /* TODO: As experiments with an analogue sender and various
  87. * configuaritions (fft-sizes of 64/128/256 and 20/40/80 Mhz)
  88. * show, the particular configuration of 80 MHz/64 bins does
  89. * not match with the other smaples at all. Until the reason
  90. * for that is found, don't report these samples.
  91. */
  92. if (bin_len == 64)
  93. return -EINVAL;
  94. fft_sample->chan_width_mhz = 88;
  95. break;
  96. default:
  97. fft_sample->chan_width_mhz = phyerr->chan_width_mhz;
  98. }
  99. fft_sample->relpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_RELPWR_DB);
  100. fft_sample->avgpwr_db = MS(reg1, SEARCH_FFT_REPORT_REG1_AVGPWR_DB);
  101. peak_mag = MS(reg1, SEARCH_FFT_REPORT_REG1_PEAK_MAG);
  102. fft_sample->max_magnitude = __cpu_to_be16(peak_mag);
  103. fft_sample->max_index = MS(reg0, SEARCH_FFT_REPORT_REG0_PEAK_SIDX);
  104. fft_sample->rssi = phyerr->rssi_combined;
  105. total_gain_db = MS(reg0, SEARCH_FFT_REPORT_REG0_TOTAL_GAIN_DB);
  106. base_pwr_db = MS(reg0, SEARCH_FFT_REPORT_REG0_BASE_PWR_DB);
  107. fft_sample->total_gain_db = __cpu_to_be16(total_gain_db);
  108. fft_sample->base_pwr_db = __cpu_to_be16(base_pwr_db);
  109. freq1 = phyerr->freq1;
  110. freq2 = phyerr->freq2;
  111. fft_sample->freq1 = __cpu_to_be16(freq1);
  112. fft_sample->freq2 = __cpu_to_be16(freq2);
  113. chain_idx = MS(reg0, SEARCH_FFT_REPORT_REG0_FFT_CHN_IDX);
  114. fft_sample->noise = __cpu_to_be16(phyerr->nf_chains[chain_idx]);
  115. bins = (u8 *)fftr;
  116. bins += sizeof(*fftr);
  117. fft_sample->tsf = __cpu_to_be64(tsf);
  118. /* max_exp has been directly reported by previous hardware (ath9k),
  119. * maybe its possible to get it by other means?
  120. */
  121. fft_sample->max_exp = get_max_exp(fft_sample->max_index, peak_mag,
  122. bin_len, bins);
  123. memcpy(fft_sample->data, bins, bin_len);
  124. /* DC value (value in the middle) is the blind spot of the spectral
  125. * sample and invalid, interpolate it.
  126. */
  127. dc_pos = bin_len / 2;
  128. fft_sample->data[dc_pos] = (fft_sample->data[dc_pos + 1] +
  129. fft_sample->data[dc_pos - 1]) / 2;
  130. send_fft_sample(ar, &fft_sample->tlv);
  131. return 0;
  132. }
  133. static struct ath10k_vif *ath10k_get_spectral_vdev(struct ath10k *ar)
  134. {
  135. struct ath10k_vif *arvif;
  136. lockdep_assert_held(&ar->conf_mutex);
  137. if (list_empty(&ar->arvifs))
  138. return NULL;
  139. /* if there already is a vif doing spectral, return that. */
  140. list_for_each_entry(arvif, &ar->arvifs, list)
  141. if (arvif->spectral_enabled)
  142. return arvif;
  143. /* otherwise, return the first vif. */
  144. return list_first_entry(&ar->arvifs, typeof(*arvif), list);
  145. }
  146. static int ath10k_spectral_scan_trigger(struct ath10k *ar)
  147. {
  148. struct ath10k_vif *arvif;
  149. int res;
  150. int vdev_id;
  151. lockdep_assert_held(&ar->conf_mutex);
  152. arvif = ath10k_get_spectral_vdev(ar);
  153. if (!arvif)
  154. return -ENODEV;
  155. vdev_id = arvif->vdev_id;
  156. if (ar->spectral.mode == SPECTRAL_DISABLED)
  157. return 0;
  158. res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
  159. WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
  160. WMI_SPECTRAL_ENABLE_CMD_ENABLE);
  161. if (res < 0)
  162. return res;
  163. res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
  164. WMI_SPECTRAL_TRIGGER_CMD_TRIGGER,
  165. WMI_SPECTRAL_ENABLE_CMD_ENABLE);
  166. if (res < 0)
  167. return res;
  168. return 0;
  169. }
  170. static int ath10k_spectral_scan_config(struct ath10k *ar,
  171. enum ath10k_spectral_mode mode)
  172. {
  173. struct wmi_vdev_spectral_conf_arg arg;
  174. struct ath10k_vif *arvif;
  175. int vdev_id, count, res = 0;
  176. lockdep_assert_held(&ar->conf_mutex);
  177. arvif = ath10k_get_spectral_vdev(ar);
  178. if (!arvif)
  179. return -ENODEV;
  180. vdev_id = arvif->vdev_id;
  181. arvif->spectral_enabled = (mode != SPECTRAL_DISABLED);
  182. ar->spectral.mode = mode;
  183. res = ath10k_wmi_vdev_spectral_enable(ar, vdev_id,
  184. WMI_SPECTRAL_TRIGGER_CMD_CLEAR,
  185. WMI_SPECTRAL_ENABLE_CMD_DISABLE);
  186. if (res < 0) {
  187. ath10k_warn(ar, "failed to enable spectral scan: %d\n", res);
  188. return res;
  189. }
  190. if (mode == SPECTRAL_DISABLED)
  191. return 0;
  192. if (mode == SPECTRAL_BACKGROUND)
  193. count = WMI_SPECTRAL_COUNT_DEFAULT;
  194. else
  195. count = max_t(u8, 1, ar->spectral.config.count);
  196. arg.vdev_id = vdev_id;
  197. arg.scan_count = count;
  198. arg.scan_period = WMI_SPECTRAL_PERIOD_DEFAULT;
  199. arg.scan_priority = WMI_SPECTRAL_PRIORITY_DEFAULT;
  200. arg.scan_fft_size = ar->spectral.config.fft_size;
  201. arg.scan_gc_ena = WMI_SPECTRAL_GC_ENA_DEFAULT;
  202. arg.scan_restart_ena = WMI_SPECTRAL_RESTART_ENA_DEFAULT;
  203. arg.scan_noise_floor_ref = WMI_SPECTRAL_NOISE_FLOOR_REF_DEFAULT;
  204. arg.scan_init_delay = WMI_SPECTRAL_INIT_DELAY_DEFAULT;
  205. arg.scan_nb_tone_thr = WMI_SPECTRAL_NB_TONE_THR_DEFAULT;
  206. arg.scan_str_bin_thr = WMI_SPECTRAL_STR_BIN_THR_DEFAULT;
  207. arg.scan_wb_rpt_mode = WMI_SPECTRAL_WB_RPT_MODE_DEFAULT;
  208. arg.scan_rssi_rpt_mode = WMI_SPECTRAL_RSSI_RPT_MODE_DEFAULT;
  209. arg.scan_rssi_thr = WMI_SPECTRAL_RSSI_THR_DEFAULT;
  210. arg.scan_pwr_format = WMI_SPECTRAL_PWR_FORMAT_DEFAULT;
  211. arg.scan_rpt_mode = WMI_SPECTRAL_RPT_MODE_DEFAULT;
  212. arg.scan_bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;
  213. arg.scan_dbm_adj = WMI_SPECTRAL_DBM_ADJ_DEFAULT;
  214. arg.scan_chn_mask = WMI_SPECTRAL_CHN_MASK_DEFAULT;
  215. res = ath10k_wmi_vdev_spectral_conf(ar, &arg);
  216. if (res < 0) {
  217. ath10k_warn(ar, "failed to configure spectral scan: %d\n", res);
  218. return res;
  219. }
  220. return 0;
  221. }
  222. static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
  223. size_t count, loff_t *ppos)
  224. {
  225. struct ath10k *ar = file->private_data;
  226. char *mode = "";
  227. unsigned int len;
  228. enum ath10k_spectral_mode spectral_mode;
  229. mutex_lock(&ar->conf_mutex);
  230. spectral_mode = ar->spectral.mode;
  231. mutex_unlock(&ar->conf_mutex);
  232. switch (spectral_mode) {
  233. case SPECTRAL_DISABLED:
  234. mode = "disable";
  235. break;
  236. case SPECTRAL_BACKGROUND:
  237. mode = "background";
  238. break;
  239. case SPECTRAL_MANUAL:
  240. mode = "manual";
  241. break;
  242. }
  243. len = strlen(mode);
  244. return simple_read_from_buffer(user_buf, count, ppos, mode, len);
  245. }
  246. static ssize_t write_file_spec_scan_ctl(struct file *file,
  247. const char __user *user_buf,
  248. size_t count, loff_t *ppos)
  249. {
  250. struct ath10k *ar = file->private_data;
  251. char buf[32];
  252. ssize_t len;
  253. int res;
  254. len = min(count, sizeof(buf) - 1);
  255. if (copy_from_user(buf, user_buf, len))
  256. return -EFAULT;
  257. buf[len] = '\0';
  258. mutex_lock(&ar->conf_mutex);
  259. if (strncmp("trigger", buf, 7) == 0) {
  260. if (ar->spectral.mode == SPECTRAL_MANUAL ||
  261. ar->spectral.mode == SPECTRAL_BACKGROUND) {
  262. /* reset the configuration to adopt possibly changed
  263. * debugfs parameters
  264. */
  265. res = ath10k_spectral_scan_config(ar,
  266. ar->spectral.mode);
  267. if (res < 0) {
  268. ath10k_warn(ar, "failed to reconfigure spectral scan: %d\n",
  269. res);
  270. }
  271. res = ath10k_spectral_scan_trigger(ar);
  272. if (res < 0) {
  273. ath10k_warn(ar, "failed to trigger spectral scan: %d\n",
  274. res);
  275. }
  276. } else {
  277. res = -EINVAL;
  278. }
  279. } else if (strncmp("background", buf, 10) == 0) {
  280. res = ath10k_spectral_scan_config(ar, SPECTRAL_BACKGROUND);
  281. } else if (strncmp("manual", buf, 6) == 0) {
  282. res = ath10k_spectral_scan_config(ar, SPECTRAL_MANUAL);
  283. } else if (strncmp("disable", buf, 7) == 0) {
  284. res = ath10k_spectral_scan_config(ar, SPECTRAL_DISABLED);
  285. } else {
  286. res = -EINVAL;
  287. }
  288. mutex_unlock(&ar->conf_mutex);
  289. if (res < 0)
  290. return res;
  291. return count;
  292. }
  293. static const struct file_operations fops_spec_scan_ctl = {
  294. .read = read_file_spec_scan_ctl,
  295. .write = write_file_spec_scan_ctl,
  296. .open = simple_open,
  297. .owner = THIS_MODULE,
  298. .llseek = default_llseek,
  299. };
  300. static ssize_t read_file_spectral_count(struct file *file,
  301. char __user *user_buf,
  302. size_t count, loff_t *ppos)
  303. {
  304. struct ath10k *ar = file->private_data;
  305. char buf[32];
  306. unsigned int len;
  307. u8 spectral_count;
  308. mutex_lock(&ar->conf_mutex);
  309. spectral_count = ar->spectral.config.count;
  310. mutex_unlock(&ar->conf_mutex);
  311. len = sprintf(buf, "%d\n", spectral_count);
  312. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  313. }
  314. static ssize_t write_file_spectral_count(struct file *file,
  315. const char __user *user_buf,
  316. size_t count, loff_t *ppos)
  317. {
  318. struct ath10k *ar = file->private_data;
  319. unsigned long val;
  320. char buf[32];
  321. ssize_t len;
  322. len = min(count, sizeof(buf) - 1);
  323. if (copy_from_user(buf, user_buf, len))
  324. return -EFAULT;
  325. buf[len] = '\0';
  326. if (kstrtoul(buf, 0, &val))
  327. return -EINVAL;
  328. if (val < 0 || val > 255)
  329. return -EINVAL;
  330. mutex_lock(&ar->conf_mutex);
  331. ar->spectral.config.count = val;
  332. mutex_unlock(&ar->conf_mutex);
  333. return count;
  334. }
  335. static const struct file_operations fops_spectral_count = {
  336. .read = read_file_spectral_count,
  337. .write = write_file_spectral_count,
  338. .open = simple_open,
  339. .owner = THIS_MODULE,
  340. .llseek = default_llseek,
  341. };
  342. static ssize_t read_file_spectral_bins(struct file *file,
  343. char __user *user_buf,
  344. size_t count, loff_t *ppos)
  345. {
  346. struct ath10k *ar = file->private_data;
  347. char buf[32];
  348. unsigned int len, bins, fft_size, bin_scale;
  349. mutex_lock(&ar->conf_mutex);
  350. fft_size = ar->spectral.config.fft_size;
  351. bin_scale = WMI_SPECTRAL_BIN_SCALE_DEFAULT;
  352. bins = 1 << (fft_size - bin_scale);
  353. mutex_unlock(&ar->conf_mutex);
  354. len = sprintf(buf, "%d\n", bins);
  355. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  356. }
  357. static ssize_t write_file_spectral_bins(struct file *file,
  358. const char __user *user_buf,
  359. size_t count, loff_t *ppos)
  360. {
  361. struct ath10k *ar = file->private_data;
  362. unsigned long val;
  363. char buf[32];
  364. ssize_t len;
  365. len = min(count, sizeof(buf) - 1);
  366. if (copy_from_user(buf, user_buf, len))
  367. return -EFAULT;
  368. buf[len] = '\0';
  369. if (kstrtoul(buf, 0, &val))
  370. return -EINVAL;
  371. if (val < 64 || val > SPECTRAL_ATH10K_MAX_NUM_BINS)
  372. return -EINVAL;
  373. if (!is_power_of_2(val))
  374. return -EINVAL;
  375. mutex_lock(&ar->conf_mutex);
  376. ar->spectral.config.fft_size = ilog2(val);
  377. ar->spectral.config.fft_size += WMI_SPECTRAL_BIN_SCALE_DEFAULT;
  378. mutex_unlock(&ar->conf_mutex);
  379. return count;
  380. }
  381. static const struct file_operations fops_spectral_bins = {
  382. .read = read_file_spectral_bins,
  383. .write = write_file_spectral_bins,
  384. .open = simple_open,
  385. .owner = THIS_MODULE,
  386. .llseek = default_llseek,
  387. };
  388. static struct dentry *create_buf_file_handler(const char *filename,
  389. struct dentry *parent,
  390. umode_t mode,
  391. struct rchan_buf *buf,
  392. int *is_global)
  393. {
  394. struct dentry *buf_file;
  395. buf_file = debugfs_create_file(filename, mode, parent, buf,
  396. &relay_file_operations);
  397. *is_global = 1;
  398. return buf_file;
  399. }
  400. static int remove_buf_file_handler(struct dentry *dentry)
  401. {
  402. debugfs_remove(dentry);
  403. return 0;
  404. }
  405. static struct rchan_callbacks rfs_spec_scan_cb = {
  406. .create_buf_file = create_buf_file_handler,
  407. .remove_buf_file = remove_buf_file_handler,
  408. };
  409. int ath10k_spectral_start(struct ath10k *ar)
  410. {
  411. struct ath10k_vif *arvif;
  412. lockdep_assert_held(&ar->conf_mutex);
  413. list_for_each_entry(arvif, &ar->arvifs, list)
  414. arvif->spectral_enabled = 0;
  415. ar->spectral.mode = SPECTRAL_DISABLED;
  416. ar->spectral.config.count = WMI_SPECTRAL_COUNT_DEFAULT;
  417. ar->spectral.config.fft_size = WMI_SPECTRAL_FFT_SIZE_DEFAULT;
  418. return 0;
  419. }
  420. int ath10k_spectral_vif_stop(struct ath10k_vif *arvif)
  421. {
  422. if (!arvif->spectral_enabled)
  423. return 0;
  424. return ath10k_spectral_scan_config(arvif->ar, SPECTRAL_DISABLED);
  425. }
  426. int ath10k_spectral_create(struct ath10k *ar)
  427. {
  428. /* The buffer size covers whole channels in dual bands up to 128 bins.
  429. * Scan with bigger than 128 bins needs to be run on single band each.
  430. */
  431. ar->spectral.rfs_chan_spec_scan = relay_open("spectral_scan",
  432. ar->debug.debugfs_phy,
  433. 1140, 2500,
  434. &rfs_spec_scan_cb, NULL);
  435. debugfs_create_file("spectral_scan_ctl",
  436. S_IRUSR | S_IWUSR,
  437. ar->debug.debugfs_phy, ar,
  438. &fops_spec_scan_ctl);
  439. debugfs_create_file("spectral_count",
  440. S_IRUSR | S_IWUSR,
  441. ar->debug.debugfs_phy, ar,
  442. &fops_spectral_count);
  443. debugfs_create_file("spectral_bins",
  444. S_IRUSR | S_IWUSR,
  445. ar->debug.debugfs_phy, ar,
  446. &fops_spectral_bins);
  447. return 0;
  448. }
  449. void ath10k_spectral_destroy(struct ath10k *ar)
  450. {
  451. if (ar->spectral.rfs_chan_spec_scan) {
  452. relay_close(ar->spectral.rfs_chan_spec_scan);
  453. ar->spectral.rfs_chan_spec_scan = NULL;
  454. }
  455. }