spectral.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 "ath9k.h"
  18. static s8 fix_rssi_inv_only(u8 rssi_val)
  19. {
  20. if (rssi_val == 128)
  21. rssi_val = 0;
  22. return (s8) rssi_val;
  23. }
  24. static void ath_debug_send_fft_sample(struct ath_softc *sc,
  25. struct fft_sample_tlv *fft_sample_tlv)
  26. {
  27. int length;
  28. if (!sc->rfs_chan_spec_scan)
  29. return;
  30. length = __be16_to_cpu(fft_sample_tlv->length) +
  31. sizeof(*fft_sample_tlv);
  32. relay_write(sc->rfs_chan_spec_scan, fft_sample_tlv, length);
  33. }
  34. /* returns 1 if this was a spectral frame, even if not handled. */
  35. int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
  36. struct ath_rx_status *rs, u64 tsf)
  37. {
  38. struct ath_hw *ah = sc->sc_ah;
  39. u8 num_bins, *bins, *vdata = (u8 *)hdr;
  40. struct fft_sample_ht20 fft_sample_20;
  41. struct fft_sample_ht20_40 fft_sample_40;
  42. struct fft_sample_tlv *tlv;
  43. struct ath_radar_info *radar_info;
  44. int len = rs->rs_datalen;
  45. int dc_pos;
  46. u16 fft_len, length, freq = ah->curchan->chan->center_freq;
  47. enum nl80211_channel_type chan_type;
  48. /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
  49. * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
  50. * yet, but this is supposed to be possible as well.
  51. */
  52. if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
  53. rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
  54. rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
  55. return 0;
  56. /* check if spectral scan bit is set. This does not have to be checked
  57. * if received through a SPECTRAL phy error, but shouldn't hurt.
  58. */
  59. radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
  60. if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
  61. return 0;
  62. chan_type = cfg80211_get_chandef_type(&sc->hw->conf.chandef);
  63. if ((chan_type == NL80211_CHAN_HT40MINUS) ||
  64. (chan_type == NL80211_CHAN_HT40PLUS)) {
  65. fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
  66. num_bins = SPECTRAL_HT20_40_NUM_BINS;
  67. bins = (u8 *)fft_sample_40.data;
  68. } else {
  69. fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
  70. num_bins = SPECTRAL_HT20_NUM_BINS;
  71. bins = (u8 *)fft_sample_20.data;
  72. }
  73. /* Variation in the data length is possible and will be fixed later */
  74. if ((len > fft_len + 2) || (len < fft_len - 1))
  75. return 1;
  76. switch (len - fft_len) {
  77. case 0:
  78. /* length correct, nothing to do. */
  79. memcpy(bins, vdata, num_bins);
  80. break;
  81. case -1:
  82. /* first byte missing, duplicate it. */
  83. memcpy(&bins[1], vdata, num_bins - 1);
  84. bins[0] = vdata[0];
  85. break;
  86. case 2:
  87. /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
  88. memcpy(bins, vdata, 30);
  89. bins[30] = vdata[31];
  90. memcpy(&bins[31], &vdata[33], num_bins - 31);
  91. break;
  92. case 1:
  93. /* MAC added 2 extra bytes AND first byte is missing. */
  94. bins[0] = vdata[0];
  95. memcpy(&bins[1], vdata, 30);
  96. bins[31] = vdata[31];
  97. memcpy(&bins[32], &vdata[33], num_bins - 32);
  98. break;
  99. default:
  100. return 1;
  101. }
  102. /* DC value (value in the middle) is the blind spot of the spectral
  103. * sample and invalid, interpolate it.
  104. */
  105. dc_pos = num_bins / 2;
  106. bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
  107. if ((chan_type == NL80211_CHAN_HT40MINUS) ||
  108. (chan_type == NL80211_CHAN_HT40PLUS)) {
  109. s8 lower_rssi, upper_rssi;
  110. s16 ext_nf;
  111. u8 lower_max_index, upper_max_index;
  112. u8 lower_bitmap_w, upper_bitmap_w;
  113. u16 lower_mag, upper_mag;
  114. struct ath9k_hw_cal_data *caldata = ah->caldata;
  115. struct ath_ht20_40_mag_info *mag_info;
  116. if (caldata)
  117. ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
  118. caldata->nfCalHist[3].privNF);
  119. else
  120. ext_nf = ATH_DEFAULT_NOISE_FLOOR;
  121. length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv);
  122. fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40;
  123. fft_sample_40.tlv.length = __cpu_to_be16(length);
  124. fft_sample_40.freq = __cpu_to_be16(freq);
  125. fft_sample_40.channel_type = chan_type;
  126. if (chan_type == NL80211_CHAN_HT40PLUS) {
  127. lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
  128. upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
  129. fft_sample_40.lower_noise = ah->noise;
  130. fft_sample_40.upper_noise = ext_nf;
  131. } else {
  132. lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
  133. upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
  134. fft_sample_40.lower_noise = ext_nf;
  135. fft_sample_40.upper_noise = ah->noise;
  136. }
  137. fft_sample_40.lower_rssi = lower_rssi;
  138. fft_sample_40.upper_rssi = upper_rssi;
  139. mag_info = ((struct ath_ht20_40_mag_info *)radar_info) - 1;
  140. lower_mag = spectral_max_magnitude(mag_info->lower_bins);
  141. upper_mag = spectral_max_magnitude(mag_info->upper_bins);
  142. fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
  143. fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
  144. lower_max_index = spectral_max_index(mag_info->lower_bins);
  145. upper_max_index = spectral_max_index(mag_info->upper_bins);
  146. fft_sample_40.lower_max_index = lower_max_index;
  147. fft_sample_40.upper_max_index = upper_max_index;
  148. lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
  149. upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
  150. fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
  151. fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
  152. fft_sample_40.max_exp = mag_info->max_exp & 0xf;
  153. fft_sample_40.tsf = __cpu_to_be64(tsf);
  154. tlv = (struct fft_sample_tlv *)&fft_sample_40;
  155. } else {
  156. u8 max_index, bitmap_w;
  157. u16 magnitude;
  158. struct ath_ht20_mag_info *mag_info;
  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;
  165. mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
  166. magnitude = spectral_max_magnitude(mag_info->all_bins);
  167. fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
  168. max_index = spectral_max_index(mag_info->all_bins);
  169. fft_sample_20.max_index = max_index;
  170. bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
  171. fft_sample_20.bitmap_weight = bitmap_w;
  172. fft_sample_20.max_exp = mag_info->max_exp & 0xf;
  173. fft_sample_20.tsf = __cpu_to_be64(tsf);
  174. tlv = (struct fft_sample_tlv *)&fft_sample_20;
  175. }
  176. ath_debug_send_fft_sample(sc, tlv);
  177. return 1;
  178. }
  179. /*********************/
  180. /* spectral_scan_ctl */
  181. /*********************/
  182. static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
  183. size_t count, loff_t *ppos)
  184. {
  185. struct ath_softc *sc = file->private_data;
  186. char *mode = "";
  187. unsigned int len;
  188. switch (sc->spectral_mode) {
  189. case SPECTRAL_DISABLED:
  190. mode = "disable";
  191. break;
  192. case SPECTRAL_BACKGROUND:
  193. mode = "background";
  194. break;
  195. case SPECTRAL_CHANSCAN:
  196. mode = "chanscan";
  197. break;
  198. case SPECTRAL_MANUAL:
  199. mode = "manual";
  200. break;
  201. }
  202. len = strlen(mode);
  203. return simple_read_from_buffer(user_buf, count, ppos, mode, len);
  204. }
  205. static ssize_t write_file_spec_scan_ctl(struct file *file,
  206. const char __user *user_buf,
  207. size_t count, loff_t *ppos)
  208. {
  209. struct ath_softc *sc = file->private_data;
  210. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  211. char buf[32];
  212. ssize_t len;
  213. if (config_enabled(CONFIG_ATH9K_TX99))
  214. return -EOPNOTSUPP;
  215. len = min(count, sizeof(buf) - 1);
  216. if (copy_from_user(buf, user_buf, len))
  217. return -EFAULT;
  218. buf[len] = '\0';
  219. if (strncmp("trigger", buf, 7) == 0) {
  220. ath9k_spectral_scan_trigger(sc->hw);
  221. } else if (strncmp("background", buf, 9) == 0) {
  222. ath9k_spectral_scan_config(sc->hw, SPECTRAL_BACKGROUND);
  223. ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
  224. } else if (strncmp("chanscan", buf, 8) == 0) {
  225. ath9k_spectral_scan_config(sc->hw, SPECTRAL_CHANSCAN);
  226. ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
  227. } else if (strncmp("manual", buf, 6) == 0) {
  228. ath9k_spectral_scan_config(sc->hw, SPECTRAL_MANUAL);
  229. ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
  230. } else if (strncmp("disable", buf, 7) == 0) {
  231. ath9k_spectral_scan_config(sc->hw, SPECTRAL_DISABLED);
  232. ath_dbg(common, CONFIG, "spectral scan: disabled\n");
  233. } else {
  234. return -EINVAL;
  235. }
  236. return count;
  237. }
  238. static const struct file_operations fops_spec_scan_ctl = {
  239. .read = read_file_spec_scan_ctl,
  240. .write = write_file_spec_scan_ctl,
  241. .open = simple_open,
  242. .owner = THIS_MODULE,
  243. .llseek = default_llseek,
  244. };
  245. /*************************/
  246. /* spectral_short_repeat */
  247. /*************************/
  248. static ssize_t read_file_spectral_short_repeat(struct file *file,
  249. char __user *user_buf,
  250. size_t count, loff_t *ppos)
  251. {
  252. struct ath_softc *sc = file->private_data;
  253. char buf[32];
  254. unsigned int len;
  255. len = sprintf(buf, "%d\n", sc->spec_config.short_repeat);
  256. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  257. }
  258. static ssize_t write_file_spectral_short_repeat(struct file *file,
  259. const char __user *user_buf,
  260. size_t count, loff_t *ppos)
  261. {
  262. struct ath_softc *sc = file->private_data;
  263. unsigned long val;
  264. char buf[32];
  265. ssize_t len;
  266. len = min(count, sizeof(buf) - 1);
  267. if (copy_from_user(buf, user_buf, len))
  268. return -EFAULT;
  269. buf[len] = '\0';
  270. if (kstrtoul(buf, 0, &val))
  271. return -EINVAL;
  272. if (val < 0 || val > 1)
  273. return -EINVAL;
  274. sc->spec_config.short_repeat = val;
  275. return count;
  276. }
  277. static const struct file_operations fops_spectral_short_repeat = {
  278. .read = read_file_spectral_short_repeat,
  279. .write = write_file_spectral_short_repeat,
  280. .open = simple_open,
  281. .owner = THIS_MODULE,
  282. .llseek = default_llseek,
  283. };
  284. /******************/
  285. /* spectral_count */
  286. /******************/
  287. static ssize_t read_file_spectral_count(struct file *file,
  288. char __user *user_buf,
  289. size_t count, loff_t *ppos)
  290. {
  291. struct ath_softc *sc = file->private_data;
  292. char buf[32];
  293. unsigned int len;
  294. len = sprintf(buf, "%d\n", sc->spec_config.count);
  295. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  296. }
  297. static ssize_t write_file_spectral_count(struct file *file,
  298. const char __user *user_buf,
  299. size_t count, loff_t *ppos)
  300. {
  301. struct ath_softc *sc = file->private_data;
  302. unsigned long val;
  303. char buf[32];
  304. ssize_t len;
  305. len = min(count, sizeof(buf) - 1);
  306. if (copy_from_user(buf, user_buf, len))
  307. return -EFAULT;
  308. buf[len] = '\0';
  309. if (kstrtoul(buf, 0, &val))
  310. return -EINVAL;
  311. if (val < 0 || val > 255)
  312. return -EINVAL;
  313. sc->spec_config.count = val;
  314. return count;
  315. }
  316. static const struct file_operations fops_spectral_count = {
  317. .read = read_file_spectral_count,
  318. .write = write_file_spectral_count,
  319. .open = simple_open,
  320. .owner = THIS_MODULE,
  321. .llseek = default_llseek,
  322. };
  323. /*******************/
  324. /* spectral_period */
  325. /*******************/
  326. static ssize_t read_file_spectral_period(struct file *file,
  327. char __user *user_buf,
  328. size_t count, loff_t *ppos)
  329. {
  330. struct ath_softc *sc = file->private_data;
  331. char buf[32];
  332. unsigned int len;
  333. len = sprintf(buf, "%d\n", sc->spec_config.period);
  334. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  335. }
  336. static ssize_t write_file_spectral_period(struct file *file,
  337. const char __user *user_buf,
  338. size_t count, loff_t *ppos)
  339. {
  340. struct ath_softc *sc = file->private_data;
  341. unsigned long val;
  342. char buf[32];
  343. ssize_t len;
  344. len = min(count, sizeof(buf) - 1);
  345. if (copy_from_user(buf, user_buf, len))
  346. return -EFAULT;
  347. buf[len] = '\0';
  348. if (kstrtoul(buf, 0, &val))
  349. return -EINVAL;
  350. if (val < 0 || val > 255)
  351. return -EINVAL;
  352. sc->spec_config.period = val;
  353. return count;
  354. }
  355. static const struct file_operations fops_spectral_period = {
  356. .read = read_file_spectral_period,
  357. .write = write_file_spectral_period,
  358. .open = simple_open,
  359. .owner = THIS_MODULE,
  360. .llseek = default_llseek,
  361. };
  362. /***********************/
  363. /* spectral_fft_period */
  364. /***********************/
  365. static ssize_t read_file_spectral_fft_period(struct file *file,
  366. char __user *user_buf,
  367. size_t count, loff_t *ppos)
  368. {
  369. struct ath_softc *sc = file->private_data;
  370. char buf[32];
  371. unsigned int len;
  372. len = sprintf(buf, "%d\n", sc->spec_config.fft_period);
  373. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  374. }
  375. static ssize_t write_file_spectral_fft_period(struct file *file,
  376. const char __user *user_buf,
  377. size_t count, loff_t *ppos)
  378. {
  379. struct ath_softc *sc = file->private_data;
  380. unsigned long val;
  381. char buf[32];
  382. ssize_t len;
  383. len = min(count, sizeof(buf) - 1);
  384. if (copy_from_user(buf, user_buf, len))
  385. return -EFAULT;
  386. buf[len] = '\0';
  387. if (kstrtoul(buf, 0, &val))
  388. return -EINVAL;
  389. if (val < 0 || val > 15)
  390. return -EINVAL;
  391. sc->spec_config.fft_period = val;
  392. return count;
  393. }
  394. static const struct file_operations fops_spectral_fft_period = {
  395. .read = read_file_spectral_fft_period,
  396. .write = write_file_spectral_fft_period,
  397. .open = simple_open,
  398. .owner = THIS_MODULE,
  399. .llseek = default_llseek,
  400. };
  401. /*******************/
  402. /* Relay interface */
  403. /*******************/
  404. static struct dentry *create_buf_file_handler(const char *filename,
  405. struct dentry *parent,
  406. umode_t mode,
  407. struct rchan_buf *buf,
  408. int *is_global)
  409. {
  410. struct dentry *buf_file;
  411. buf_file = debugfs_create_file(filename, mode, parent, buf,
  412. &relay_file_operations);
  413. *is_global = 1;
  414. return buf_file;
  415. }
  416. static int remove_buf_file_handler(struct dentry *dentry)
  417. {
  418. debugfs_remove(dentry);
  419. return 0;
  420. }
  421. static struct rchan_callbacks rfs_spec_scan_cb = {
  422. .create_buf_file = create_buf_file_handler,
  423. .remove_buf_file = remove_buf_file_handler,
  424. };
  425. /*********************/
  426. /* Debug Init/Deinit */
  427. /*********************/
  428. void ath9k_spectral_deinit_debug(struct ath_softc *sc)
  429. {
  430. if (config_enabled(CONFIG_ATH9K_DEBUGFS) && sc->rfs_chan_spec_scan) {
  431. relay_close(sc->rfs_chan_spec_scan);
  432. sc->rfs_chan_spec_scan = NULL;
  433. }
  434. }
  435. void ath9k_spectral_init_debug(struct ath_softc *sc)
  436. {
  437. sc->rfs_chan_spec_scan = relay_open("spectral_scan",
  438. sc->debug.debugfs_phy,
  439. 1024, 256, &rfs_spec_scan_cb,
  440. NULL);
  441. debugfs_create_file("spectral_scan_ctl",
  442. S_IRUSR | S_IWUSR,
  443. sc->debug.debugfs_phy, sc,
  444. &fops_spec_scan_ctl);
  445. debugfs_create_file("spectral_short_repeat",
  446. S_IRUSR | S_IWUSR,
  447. sc->debug.debugfs_phy, sc,
  448. &fops_spectral_short_repeat);
  449. debugfs_create_file("spectral_count",
  450. S_IRUSR | S_IWUSR,
  451. sc->debug.debugfs_phy, sc,
  452. &fops_spectral_count);
  453. debugfs_create_file("spectral_period",
  454. S_IRUSR | S_IWUSR,
  455. sc->debug.debugfs_phy, sc,
  456. &fops_spectral_period);
  457. debugfs_create_file("spectral_fft_period",
  458. S_IRUSR | S_IWUSR,
  459. sc->debug.debugfs_phy, sc,
  460. &fops_spectral_fft_period);
  461. }