spectral.c 14 KB

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