common-spectral.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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 <linux/random.h>
  18. #include "ath9k.h"
  19. static s8 fix_rssi_inv_only(u8 rssi_val)
  20. {
  21. if (rssi_val == 128)
  22. rssi_val = 0;
  23. return (s8) rssi_val;
  24. }
  25. static void ath_debug_send_fft_sample(struct ath_spec_scan_priv *spec_priv,
  26. struct fft_sample_tlv *fft_sample_tlv)
  27. {
  28. int length;
  29. if (!spec_priv->rfs_chan_spec_scan)
  30. return;
  31. length = __be16_to_cpu(fft_sample_tlv->length) +
  32. sizeof(*fft_sample_tlv);
  33. relay_write(spec_priv->rfs_chan_spec_scan, fft_sample_tlv, length);
  34. }
  35. typedef int (ath_cmn_fft_idx_validator) (u8 *sample_end, int bytes_read);
  36. static int
  37. ath_cmn_max_idx_verify_ht20_fft(u8 *sample_end, int bytes_read)
  38. {
  39. struct ath_ht20_mag_info *mag_info;
  40. u8 *sample;
  41. u16 max_magnitude;
  42. u8 max_index;
  43. u8 max_exp;
  44. /* Sanity check so that we don't read outside the read
  45. * buffer
  46. */
  47. if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN - 1)
  48. return -1;
  49. mag_info = (struct ath_ht20_mag_info *) (sample_end -
  50. sizeof(struct ath_ht20_mag_info) + 1);
  51. sample = sample_end - SPECTRAL_HT20_SAMPLE_LEN + 1;
  52. max_index = spectral_max_index_ht20(mag_info->all_bins);
  53. max_magnitude = spectral_max_magnitude(mag_info->all_bins);
  54. max_exp = mag_info->max_exp & 0xf;
  55. /* Don't try to read something outside the read buffer
  56. * in case of a missing byte (so bins[0] will be outside
  57. * the read buffer)
  58. */
  59. if (bytes_read < SPECTRAL_HT20_SAMPLE_LEN && max_index < 1)
  60. return -1;
  61. if ((sample[max_index] & 0xf8) != ((max_magnitude >> max_exp) & 0xf8))
  62. return -1;
  63. else
  64. return 0;
  65. }
  66. static int
  67. ath_cmn_max_idx_verify_ht20_40_fft(u8 *sample_end, int bytes_read)
  68. {
  69. struct ath_ht20_40_mag_info *mag_info;
  70. u8 *sample;
  71. u16 lower_mag, upper_mag;
  72. u8 lower_max_index, upper_max_index;
  73. u8 max_exp;
  74. int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
  75. /* Sanity check so that we don't read outside the read
  76. * buffer
  77. */
  78. if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN - 1)
  79. return -1;
  80. mag_info = (struct ath_ht20_40_mag_info *) (sample_end -
  81. sizeof(struct ath_ht20_40_mag_info) + 1);
  82. sample = sample_end - SPECTRAL_HT20_40_SAMPLE_LEN + 1;
  83. lower_mag = spectral_max_magnitude(mag_info->lower_bins);
  84. lower_max_index = spectral_max_index_ht40(mag_info->lower_bins);
  85. upper_mag = spectral_max_magnitude(mag_info->upper_bins);
  86. upper_max_index = spectral_max_index_ht40(mag_info->upper_bins);
  87. max_exp = mag_info->max_exp & 0xf;
  88. /* Don't try to read something outside the read buffer
  89. * in case of a missing byte (so bins[0] will be outside
  90. * the read buffer)
  91. */
  92. if (bytes_read < SPECTRAL_HT20_40_SAMPLE_LEN &&
  93. ((upper_max_index < 1) || (lower_max_index < 1)))
  94. return -1;
  95. if (((sample[upper_max_index + dc_pos] & 0xf8) !=
  96. ((upper_mag >> max_exp) & 0xf8)) ||
  97. ((sample[lower_max_index] & 0xf8) !=
  98. ((lower_mag >> max_exp) & 0xf8)))
  99. return -1;
  100. else
  101. return 0;
  102. }
  103. typedef int (ath_cmn_fft_sample_handler) (struct ath_rx_status *rs,
  104. struct ath_spec_scan_priv *spec_priv,
  105. u8 *sample_buf, u64 tsf, u16 freq, int chan_type);
  106. static int
  107. ath_cmn_process_ht20_fft(struct ath_rx_status *rs,
  108. struct ath_spec_scan_priv *spec_priv,
  109. u8 *sample_buf,
  110. u64 tsf, u16 freq, int chan_type)
  111. {
  112. struct fft_sample_ht20 fft_sample_20;
  113. struct ath_common *common = ath9k_hw_common(spec_priv->ah);
  114. struct ath_hw *ah = spec_priv->ah;
  115. struct ath_ht20_mag_info *mag_info;
  116. struct fft_sample_tlv *tlv;
  117. int i = 0;
  118. int ret = 0;
  119. int dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
  120. u16 magnitude, tmp_mag, length;
  121. u8 max_index, bitmap_w, max_exp;
  122. length = sizeof(fft_sample_20) - sizeof(struct fft_sample_tlv);
  123. fft_sample_20.tlv.type = ATH_FFT_SAMPLE_HT20;
  124. fft_sample_20.tlv.length = __cpu_to_be16(length);
  125. fft_sample_20.freq = __cpu_to_be16(freq);
  126. fft_sample_20.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
  127. fft_sample_20.noise = ah->noise;
  128. mag_info = (struct ath_ht20_mag_info *) (sample_buf +
  129. SPECTRAL_HT20_NUM_BINS);
  130. magnitude = spectral_max_magnitude(mag_info->all_bins);
  131. fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
  132. max_index = spectral_max_index_ht20(mag_info->all_bins);
  133. fft_sample_20.max_index = max_index;
  134. bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
  135. fft_sample_20.bitmap_weight = bitmap_w;
  136. max_exp = mag_info->max_exp & 0xf;
  137. fft_sample_20.max_exp = max_exp;
  138. fft_sample_20.tsf = __cpu_to_be64(tsf);
  139. memcpy(fft_sample_20.data, sample_buf, SPECTRAL_HT20_NUM_BINS);
  140. ath_dbg(common, SPECTRAL_SCAN, "FFT HT20 frame: max mag 0x%X,"
  141. "max_mag_idx %i\n",
  142. magnitude >> max_exp,
  143. max_index);
  144. if ((fft_sample_20.data[max_index] & 0xf8) !=
  145. ((magnitude >> max_exp) & 0xf8)) {
  146. ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
  147. ret = -1;
  148. }
  149. /* DC value (value in the middle) is the blind spot of the spectral
  150. * sample and invalid, interpolate it.
  151. */
  152. fft_sample_20.data[dc_pos] = (fft_sample_20.data[dc_pos + 1] +
  153. fft_sample_20.data[dc_pos - 1]) / 2;
  154. /* Check if the maximum magnitude is indeed maximum,
  155. * also if the maximum value was at dc_pos, calculate
  156. * a new one (since value at dc_pos is invalid).
  157. */
  158. if (max_index == dc_pos) {
  159. tmp_mag = 0;
  160. for (i = 0; i < dc_pos; i++) {
  161. if (fft_sample_20.data[i] > tmp_mag) {
  162. tmp_mag = fft_sample_20.data[i];
  163. fft_sample_20.max_index = i;
  164. }
  165. }
  166. magnitude = tmp_mag << max_exp;
  167. fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
  168. ath_dbg(common, SPECTRAL_SCAN,
  169. "Calculated new lower max 0x%X at %i\n",
  170. tmp_mag, fft_sample_20.max_index);
  171. } else
  172. for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
  173. if (fft_sample_20.data[i] == (magnitude >> max_exp))
  174. ath_dbg(common, SPECTRAL_SCAN,
  175. "Got max: 0x%X at index %i\n",
  176. fft_sample_20.data[i], i);
  177. if (fft_sample_20.data[i] > (magnitude >> max_exp)) {
  178. ath_dbg(common, SPECTRAL_SCAN,
  179. "Got bin %i greater than max: 0x%X\n",
  180. i, fft_sample_20.data[i]);
  181. ret = -1;
  182. }
  183. }
  184. if (ret < 0)
  185. return ret;
  186. tlv = (struct fft_sample_tlv *)&fft_sample_20;
  187. ath_debug_send_fft_sample(spec_priv, tlv);
  188. return 0;
  189. }
  190. static int
  191. ath_cmn_process_ht20_40_fft(struct ath_rx_status *rs,
  192. struct ath_spec_scan_priv *spec_priv,
  193. u8 *sample_buf,
  194. u64 tsf, u16 freq, int chan_type)
  195. {
  196. struct fft_sample_ht20_40 fft_sample_40;
  197. struct ath_common *common = ath9k_hw_common(spec_priv->ah);
  198. struct ath_hw *ah = spec_priv->ah;
  199. struct ath9k_hw_cal_data *caldata = ah->caldata;
  200. struct ath_ht20_40_mag_info *mag_info;
  201. struct fft_sample_tlv *tlv;
  202. int dc_pos = SPECTRAL_HT20_40_NUM_BINS / 2;
  203. int i = 0;
  204. int ret = 0;
  205. s16 ext_nf;
  206. u16 lower_mag, upper_mag, tmp_mag, length;
  207. s8 lower_rssi, upper_rssi;
  208. u8 lower_max_index, upper_max_index;
  209. u8 lower_bitmap_w, upper_bitmap_w, max_exp;
  210. if (caldata)
  211. ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
  212. caldata->nfCalHist[3].privNF);
  213. else
  214. ext_nf = ATH_DEFAULT_NOISE_FLOOR;
  215. length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv);
  216. fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40;
  217. fft_sample_40.tlv.length = __cpu_to_be16(length);
  218. fft_sample_40.freq = __cpu_to_be16(freq);
  219. fft_sample_40.channel_type = chan_type;
  220. if (chan_type == NL80211_CHAN_HT40PLUS) {
  221. lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
  222. upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
  223. fft_sample_40.lower_noise = ah->noise;
  224. fft_sample_40.upper_noise = ext_nf;
  225. } else {
  226. lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]);
  227. upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]);
  228. fft_sample_40.lower_noise = ext_nf;
  229. fft_sample_40.upper_noise = ah->noise;
  230. }
  231. fft_sample_40.lower_rssi = lower_rssi;
  232. fft_sample_40.upper_rssi = upper_rssi;
  233. mag_info = (struct ath_ht20_40_mag_info *) (sample_buf +
  234. SPECTRAL_HT20_40_NUM_BINS);
  235. lower_mag = spectral_max_magnitude(mag_info->lower_bins);
  236. fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
  237. upper_mag = spectral_max_magnitude(mag_info->upper_bins);
  238. fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
  239. lower_max_index = spectral_max_index_ht40(mag_info->lower_bins);
  240. fft_sample_40.lower_max_index = lower_max_index;
  241. upper_max_index = spectral_max_index_ht40(mag_info->upper_bins);
  242. fft_sample_40.upper_max_index = upper_max_index;
  243. lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
  244. fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
  245. upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
  246. fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
  247. max_exp = mag_info->max_exp & 0xf;
  248. fft_sample_40.max_exp = max_exp;
  249. fft_sample_40.tsf = __cpu_to_be64(tsf);
  250. memcpy(fft_sample_40.data, sample_buf, SPECTRAL_HT20_40_NUM_BINS);
  251. ath_dbg(common, SPECTRAL_SCAN, "FFT HT20/40 frame: lower mag 0x%X,"
  252. "lower_mag_idx %i, upper mag 0x%X,"
  253. "upper_mag_idx %i\n",
  254. lower_mag >> max_exp,
  255. lower_max_index,
  256. upper_mag >> max_exp,
  257. upper_max_index);
  258. /* Check if we got the expected magnitude values at
  259. * the expected bins
  260. */
  261. if (((fft_sample_40.data[upper_max_index + dc_pos] & 0xf8)
  262. != ((upper_mag >> max_exp) & 0xf8)) ||
  263. ((fft_sample_40.data[lower_max_index] & 0xf8)
  264. != ((lower_mag >> max_exp) & 0xf8))) {
  265. ath_dbg(common, SPECTRAL_SCAN, "Magnitude mismatch !\n");
  266. ret = -1;
  267. }
  268. /* DC value (value in the middle) is the blind spot of the spectral
  269. * sample and invalid, interpolate it.
  270. */
  271. fft_sample_40.data[dc_pos] = (fft_sample_40.data[dc_pos + 1] +
  272. fft_sample_40.data[dc_pos - 1]) / 2;
  273. /* Check if the maximum magnitudes are indeed maximum,
  274. * also if the maximum value was at dc_pos, calculate
  275. * a new one (since value at dc_pos is invalid).
  276. */
  277. if (lower_max_index == dc_pos) {
  278. tmp_mag = 0;
  279. for (i = 0; i < dc_pos; i++) {
  280. if (fft_sample_40.data[i] > tmp_mag) {
  281. tmp_mag = fft_sample_40.data[i];
  282. fft_sample_40.lower_max_index = i;
  283. }
  284. }
  285. lower_mag = tmp_mag << max_exp;
  286. fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
  287. ath_dbg(common, SPECTRAL_SCAN,
  288. "Calculated new lower max 0x%X at %i\n",
  289. tmp_mag, fft_sample_40.lower_max_index);
  290. } else
  291. for (i = 0; i < dc_pos; i++) {
  292. if (fft_sample_40.data[i] == (lower_mag >> max_exp))
  293. ath_dbg(common, SPECTRAL_SCAN,
  294. "Got lower mag: 0x%X at index %i\n",
  295. fft_sample_40.data[i], i);
  296. if (fft_sample_40.data[i] > (lower_mag >> max_exp)) {
  297. ath_dbg(common, SPECTRAL_SCAN,
  298. "Got lower bin %i higher than max: 0x%X\n",
  299. i, fft_sample_40.data[i]);
  300. ret = -1;
  301. }
  302. }
  303. if (upper_max_index == dc_pos) {
  304. tmp_mag = 0;
  305. for (i = dc_pos; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
  306. if (fft_sample_40.data[i] > tmp_mag) {
  307. tmp_mag = fft_sample_40.data[i];
  308. fft_sample_40.upper_max_index = i;
  309. }
  310. }
  311. upper_mag = tmp_mag << max_exp;
  312. fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
  313. ath_dbg(common, SPECTRAL_SCAN,
  314. "Calculated new upper max 0x%X at %i\n",
  315. tmp_mag, fft_sample_40.upper_max_index);
  316. } else
  317. for (i = dc_pos; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
  318. if (fft_sample_40.data[i] == (upper_mag >> max_exp))
  319. ath_dbg(common, SPECTRAL_SCAN,
  320. "Got upper mag: 0x%X at index %i\n",
  321. fft_sample_40.data[i], i);
  322. if (fft_sample_40.data[i] > (upper_mag >> max_exp)) {
  323. ath_dbg(common, SPECTRAL_SCAN,
  324. "Got upper bin %i higher than max: 0x%X\n",
  325. i, fft_sample_40.data[i]);
  326. ret = -1;
  327. }
  328. }
  329. if (ret < 0)
  330. return ret;
  331. tlv = (struct fft_sample_tlv *)&fft_sample_40;
  332. ath_debug_send_fft_sample(spec_priv, tlv);
  333. return 0;
  334. }
  335. static inline void
  336. ath_cmn_copy_fft_frame(u8 *in, u8 *out, int sample_len, int sample_bytes)
  337. {
  338. switch (sample_bytes - sample_len) {
  339. case -1:
  340. /* First byte missing */
  341. memcpy(&out[1], in,
  342. sample_len - 1);
  343. break;
  344. case 0:
  345. /* Length correct, nothing to do. */
  346. memcpy(out, in, sample_len);
  347. break;
  348. case 1:
  349. /* MAC added 2 extra bytes AND first byte
  350. * is missing.
  351. */
  352. memcpy(&out[1], in, 30);
  353. out[31] = in[31];
  354. memcpy(&out[32], &in[33],
  355. sample_len - 32);
  356. break;
  357. case 2:
  358. /* MAC added 2 extra bytes at bin 30 and 32,
  359. * remove them.
  360. */
  361. memcpy(out, in, 30);
  362. out[30] = in[31];
  363. memcpy(&out[31], &in[33],
  364. sample_len - 31);
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. static int
  371. ath_cmn_is_fft_buf_full(struct ath_spec_scan_priv *spec_priv)
  372. {
  373. int i = 0;
  374. int ret = 0;
  375. struct rchan_buf *buf;
  376. struct rchan *rc = spec_priv->rfs_chan_spec_scan;
  377. for_each_possible_cpu(i) {
  378. if ((buf = *per_cpu_ptr(rc->buf, i))) {
  379. ret += relay_buf_full(buf);
  380. }
  381. }
  382. if (ret)
  383. return 1;
  384. else
  385. return 0;
  386. }
  387. /* returns 1 if this was a spectral frame, even if not handled. */
  388. int ath_cmn_process_fft(struct ath_spec_scan_priv *spec_priv, struct ieee80211_hdr *hdr,
  389. struct ath_rx_status *rs, u64 tsf)
  390. {
  391. u8 sample_buf[SPECTRAL_SAMPLE_MAX_LEN] = {0};
  392. struct ath_hw *ah = spec_priv->ah;
  393. struct ath_common *common = ath9k_hw_common(spec_priv->ah);
  394. struct ath_softc *sc = (struct ath_softc *)common->priv;
  395. u8 num_bins, *vdata = (u8 *)hdr;
  396. struct ath_radar_info *radar_info;
  397. int len = rs->rs_datalen;
  398. int i;
  399. int got_slen = 0;
  400. u8 *sample_start;
  401. int sample_bytes = 0;
  402. int ret = 0;
  403. u16 fft_len, sample_len, freq = ah->curchan->chan->center_freq;
  404. enum nl80211_channel_type chan_type;
  405. ath_cmn_fft_idx_validator *fft_idx_validator;
  406. ath_cmn_fft_sample_handler *fft_handler;
  407. /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
  408. * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
  409. * yet, but this is supposed to be possible as well.
  410. */
  411. if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
  412. rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
  413. rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
  414. return 0;
  415. /* check if spectral scan bit is set. This does not have to be checked
  416. * if received through a SPECTRAL phy error, but shouldn't hurt.
  417. */
  418. radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
  419. if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
  420. return 0;
  421. if (!spec_priv->rfs_chan_spec_scan)
  422. return 1;
  423. /* Output buffers are full, no need to process anything
  424. * since there is no space to put the result anyway
  425. */
  426. ret = ath_cmn_is_fft_buf_full(spec_priv);
  427. if (ret == 1) {
  428. ath_dbg(common, SPECTRAL_SCAN, "FFT report ignored, no space "
  429. "left on output buffers\n");
  430. return 1;
  431. }
  432. chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef);
  433. if ((chan_type == NL80211_CHAN_HT40MINUS) ||
  434. (chan_type == NL80211_CHAN_HT40PLUS)) {
  435. fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
  436. sample_len = SPECTRAL_HT20_40_SAMPLE_LEN;
  437. num_bins = SPECTRAL_HT20_40_NUM_BINS;
  438. fft_idx_validator = &ath_cmn_max_idx_verify_ht20_40_fft;
  439. fft_handler = &ath_cmn_process_ht20_40_fft;
  440. } else {
  441. fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
  442. sample_len = SPECTRAL_HT20_SAMPLE_LEN;
  443. num_bins = SPECTRAL_HT20_NUM_BINS;
  444. fft_idx_validator = ath_cmn_max_idx_verify_ht20_fft;
  445. fft_handler = &ath_cmn_process_ht20_fft;
  446. }
  447. ath_dbg(common, SPECTRAL_SCAN, "Got radar dump bw_info: 0x%X,"
  448. "len: %i fft_len: %i\n",
  449. radar_info->pulse_bw_info,
  450. len,
  451. fft_len);
  452. sample_start = vdata;
  453. for (i = 0; i < len - 2; i++) {
  454. sample_bytes++;
  455. /* Only a single sample received, no need to look
  456. * for the sample's end, do the correction based
  457. * on the packet's length instead. Note that hw
  458. * will always put the radar_info structure on
  459. * the end.
  460. */
  461. if (len <= fft_len + 2) {
  462. sample_bytes = len - sizeof(struct ath_radar_info);
  463. got_slen = 1;
  464. }
  465. /* Search for the end of the FFT frame between
  466. * sample_len - 1 and sample_len + 2. exp_max is 3
  467. * bits long and it's the only value on the last
  468. * byte of the frame so since it'll be smaller than
  469. * the next byte (the first bin of the next sample)
  470. * 90% of the time, we can use it as a separator.
  471. */
  472. if (vdata[i] <= 0x7 && sample_bytes >= sample_len - 1) {
  473. /* Got a frame length within boundaries, there are
  474. * four scenarios here:
  475. *
  476. * a) sample_len -> We got the correct length
  477. * b) sample_len + 2 -> 2 bytes added around bin[31]
  478. * c) sample_len - 1 -> The first byte is missing
  479. * d) sample_len + 1 -> b + c at the same time
  480. *
  481. * When MAC adds 2 extra bytes, bin[31] and bin[32]
  482. * have the same value, so we can use that for further
  483. * verification in cases b and d.
  484. */
  485. /* Did we go too far ? If so we couldn't determine
  486. * this sample's boundaries, discard any further
  487. * data
  488. */
  489. if ((sample_bytes > sample_len + 2) ||
  490. ((sample_bytes > sample_len) &&
  491. (sample_start[31] != sample_start[32])))
  492. break;
  493. /* See if we got a valid frame by checking the
  494. * consistency of mag_info fields. This is to
  495. * prevent from "fixing" a correct frame.
  496. * Failure is non-fatal, later frames may
  497. * be valid.
  498. */
  499. if (!fft_idx_validator(&vdata[i], i)) {
  500. ath_dbg(common, SPECTRAL_SCAN,
  501. "Found valid fft frame at %i\n", i);
  502. got_slen = 1;
  503. }
  504. /* We expect 1 - 2 more bytes */
  505. else if ((sample_start[31] == sample_start[32]) &&
  506. (sample_bytes >= sample_len) &&
  507. (sample_bytes < sample_len + 2) &&
  508. (vdata[i + 1] <= 0x7))
  509. continue;
  510. /* Try to distinguish cases a and c */
  511. else if ((sample_bytes == sample_len - 1) &&
  512. (vdata[i + 1] <= 0x7))
  513. continue;
  514. got_slen = 1;
  515. }
  516. if (got_slen) {
  517. ath_dbg(common, SPECTRAL_SCAN, "FFT frame len: %i\n",
  518. sample_bytes);
  519. /* Only try to fix a frame if it's the only one
  520. * on the report, else just skip it.
  521. */
  522. if (sample_bytes != sample_len && len <= fft_len + 2) {
  523. ath_cmn_copy_fft_frame(sample_start,
  524. sample_buf, sample_len,
  525. sample_bytes);
  526. ret = fft_handler(rs, spec_priv, sample_buf,
  527. tsf, freq, chan_type);
  528. if (ret == 0)
  529. RX_STAT_INC(sc, rx_spectral_sample_good);
  530. else
  531. RX_STAT_INC(sc, rx_spectral_sample_err);
  532. memset(sample_buf, 0, SPECTRAL_SAMPLE_MAX_LEN);
  533. /* Mix the received bins to the /dev/random
  534. * pool
  535. */
  536. add_device_randomness(sample_buf, num_bins);
  537. }
  538. /* Process a normal frame */
  539. if (sample_bytes == sample_len) {
  540. ret = fft_handler(rs, spec_priv, sample_start,
  541. tsf, freq, chan_type);
  542. if (ret == 0)
  543. RX_STAT_INC(sc, rx_spectral_sample_good);
  544. else
  545. RX_STAT_INC(sc, rx_spectral_sample_err);
  546. /* Mix the received bins to the /dev/random
  547. * pool
  548. */
  549. add_device_randomness(sample_start, num_bins);
  550. }
  551. /* Short report processed, break out of the
  552. * loop.
  553. */
  554. if (len <= fft_len + 2)
  555. return 1;
  556. sample_start = &vdata[i + 1];
  557. /* -1 to grab sample_len -1, -2 since
  558. * they 'll get increased by one. In case
  559. * of failure try to recover by going byte
  560. * by byte instead.
  561. */
  562. if (ret == 0) {
  563. i += num_bins - 2;
  564. sample_bytes = num_bins - 2;
  565. }
  566. got_slen = 0;
  567. }
  568. }
  569. i -= num_bins - 2;
  570. if (len - i != sizeof(struct ath_radar_info))
  571. ath_dbg(common, SPECTRAL_SCAN, "FFT report truncated"
  572. "(bytes left: %i)\n",
  573. len - i);
  574. return 1;
  575. }
  576. EXPORT_SYMBOL(ath_cmn_process_fft);
  577. /*********************/
  578. /* spectral_scan_ctl */
  579. /*********************/
  580. static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
  581. size_t count, loff_t *ppos)
  582. {
  583. struct ath_spec_scan_priv *spec_priv = file->private_data;
  584. char *mode = "";
  585. unsigned int len;
  586. switch (spec_priv->spectral_mode) {
  587. case SPECTRAL_DISABLED:
  588. mode = "disable";
  589. break;
  590. case SPECTRAL_BACKGROUND:
  591. mode = "background";
  592. break;
  593. case SPECTRAL_CHANSCAN:
  594. mode = "chanscan";
  595. break;
  596. case SPECTRAL_MANUAL:
  597. mode = "manual";
  598. break;
  599. }
  600. len = strlen(mode);
  601. return simple_read_from_buffer(user_buf, count, ppos, mode, len);
  602. }
  603. void ath9k_cmn_spectral_scan_trigger(struct ath_common *common,
  604. struct ath_spec_scan_priv *spec_priv)
  605. {
  606. struct ath_hw *ah = spec_priv->ah;
  607. u32 rxfilter;
  608. if (IS_ENABLED(CONFIG_ATH9K_TX99))
  609. return;
  610. if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
  611. ath_err(common, "spectrum analyzer not implemented on this hardware\n");
  612. return;
  613. }
  614. if (!spec_priv->spec_config.enabled)
  615. return;
  616. ath_ps_ops(common)->wakeup(common);
  617. rxfilter = ath9k_hw_getrxfilter(ah);
  618. ath9k_hw_setrxfilter(ah, rxfilter |
  619. ATH9K_RX_FILTER_PHYRADAR |
  620. ATH9K_RX_FILTER_PHYERR);
  621. /* TODO: usually this should not be neccesary, but for some reason
  622. * (or in some mode?) the trigger must be called after the
  623. * configuration, otherwise the register will have its values reset
  624. * (on my ar9220 to value 0x01002310)
  625. */
  626. ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode);
  627. ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
  628. ath_ps_ops(common)->restore(common);
  629. }
  630. EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger);
  631. int ath9k_cmn_spectral_scan_config(struct ath_common *common,
  632. struct ath_spec_scan_priv *spec_priv,
  633. enum spectral_mode spectral_mode)
  634. {
  635. struct ath_hw *ah = spec_priv->ah;
  636. if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
  637. ath_err(common, "spectrum analyzer not implemented on this hardware\n");
  638. return -1;
  639. }
  640. switch (spectral_mode) {
  641. case SPECTRAL_DISABLED:
  642. spec_priv->spec_config.enabled = 0;
  643. break;
  644. case SPECTRAL_BACKGROUND:
  645. /* send endless samples.
  646. * TODO: is this really useful for "background"?
  647. */
  648. spec_priv->spec_config.endless = 1;
  649. spec_priv->spec_config.enabled = 1;
  650. break;
  651. case SPECTRAL_CHANSCAN:
  652. case SPECTRAL_MANUAL:
  653. spec_priv->spec_config.endless = 0;
  654. spec_priv->spec_config.enabled = 1;
  655. break;
  656. default:
  657. return -1;
  658. }
  659. ath_ps_ops(common)->wakeup(common);
  660. ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config);
  661. ath_ps_ops(common)->restore(common);
  662. spec_priv->spectral_mode = spectral_mode;
  663. return 0;
  664. }
  665. EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config);
  666. static ssize_t write_file_spec_scan_ctl(struct file *file,
  667. const char __user *user_buf,
  668. size_t count, loff_t *ppos)
  669. {
  670. struct ath_spec_scan_priv *spec_priv = file->private_data;
  671. struct ath_common *common = ath9k_hw_common(spec_priv->ah);
  672. char buf[32];
  673. ssize_t len;
  674. if (IS_ENABLED(CONFIG_ATH9K_TX99))
  675. return -EOPNOTSUPP;
  676. len = min(count, sizeof(buf) - 1);
  677. if (copy_from_user(buf, user_buf, len))
  678. return -EFAULT;
  679. buf[len] = '\0';
  680. if (strncmp("trigger", buf, 7) == 0) {
  681. ath9k_cmn_spectral_scan_trigger(common, spec_priv);
  682. } else if (strncmp("background", buf, 10) == 0) {
  683. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND);
  684. ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
  685. } else if (strncmp("chanscan", buf, 8) == 0) {
  686. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN);
  687. ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
  688. } else if (strncmp("manual", buf, 6) == 0) {
  689. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL);
  690. ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
  691. } else if (strncmp("disable", buf, 7) == 0) {
  692. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED);
  693. ath_dbg(common, CONFIG, "spectral scan: disabled\n");
  694. } else {
  695. return -EINVAL;
  696. }
  697. return count;
  698. }
  699. static const struct file_operations fops_spec_scan_ctl = {
  700. .read = read_file_spec_scan_ctl,
  701. .write = write_file_spec_scan_ctl,
  702. .open = simple_open,
  703. .owner = THIS_MODULE,
  704. .llseek = default_llseek,
  705. };
  706. /*************************/
  707. /* spectral_short_repeat */
  708. /*************************/
  709. static ssize_t read_file_spectral_short_repeat(struct file *file,
  710. char __user *user_buf,
  711. size_t count, loff_t *ppos)
  712. {
  713. struct ath_spec_scan_priv *spec_priv = file->private_data;
  714. char buf[32];
  715. unsigned int len;
  716. len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat);
  717. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  718. }
  719. static ssize_t write_file_spectral_short_repeat(struct file *file,
  720. const char __user *user_buf,
  721. size_t count, loff_t *ppos)
  722. {
  723. struct ath_spec_scan_priv *spec_priv = file->private_data;
  724. unsigned long val;
  725. char buf[32];
  726. ssize_t len;
  727. len = min(count, sizeof(buf) - 1);
  728. if (copy_from_user(buf, user_buf, len))
  729. return -EFAULT;
  730. buf[len] = '\0';
  731. if (kstrtoul(buf, 0, &val))
  732. return -EINVAL;
  733. if (val > 1)
  734. return -EINVAL;
  735. spec_priv->spec_config.short_repeat = val;
  736. return count;
  737. }
  738. static const struct file_operations fops_spectral_short_repeat = {
  739. .read = read_file_spectral_short_repeat,
  740. .write = write_file_spectral_short_repeat,
  741. .open = simple_open,
  742. .owner = THIS_MODULE,
  743. .llseek = default_llseek,
  744. };
  745. /******************/
  746. /* spectral_count */
  747. /******************/
  748. static ssize_t read_file_spectral_count(struct file *file,
  749. char __user *user_buf,
  750. size_t count, loff_t *ppos)
  751. {
  752. struct ath_spec_scan_priv *spec_priv = file->private_data;
  753. char buf[32];
  754. unsigned int len;
  755. len = sprintf(buf, "%d\n", spec_priv->spec_config.count);
  756. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  757. }
  758. static ssize_t write_file_spectral_count(struct file *file,
  759. const char __user *user_buf,
  760. size_t count, loff_t *ppos)
  761. {
  762. struct ath_spec_scan_priv *spec_priv = file->private_data;
  763. unsigned long val;
  764. char buf[32];
  765. ssize_t len;
  766. len = min(count, sizeof(buf) - 1);
  767. if (copy_from_user(buf, user_buf, len))
  768. return -EFAULT;
  769. buf[len] = '\0';
  770. if (kstrtoul(buf, 0, &val))
  771. return -EINVAL;
  772. if (val > 255)
  773. return -EINVAL;
  774. spec_priv->spec_config.count = val;
  775. return count;
  776. }
  777. static const struct file_operations fops_spectral_count = {
  778. .read = read_file_spectral_count,
  779. .write = write_file_spectral_count,
  780. .open = simple_open,
  781. .owner = THIS_MODULE,
  782. .llseek = default_llseek,
  783. };
  784. /*******************/
  785. /* spectral_period */
  786. /*******************/
  787. static ssize_t read_file_spectral_period(struct file *file,
  788. char __user *user_buf,
  789. size_t count, loff_t *ppos)
  790. {
  791. struct ath_spec_scan_priv *spec_priv = file->private_data;
  792. char buf[32];
  793. unsigned int len;
  794. len = sprintf(buf, "%d\n", spec_priv->spec_config.period);
  795. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  796. }
  797. static ssize_t write_file_spectral_period(struct file *file,
  798. const char __user *user_buf,
  799. size_t count, loff_t *ppos)
  800. {
  801. struct ath_spec_scan_priv *spec_priv = file->private_data;
  802. unsigned long val;
  803. char buf[32];
  804. ssize_t len;
  805. len = min(count, sizeof(buf) - 1);
  806. if (copy_from_user(buf, user_buf, len))
  807. return -EFAULT;
  808. buf[len] = '\0';
  809. if (kstrtoul(buf, 0, &val))
  810. return -EINVAL;
  811. if (val > 255)
  812. return -EINVAL;
  813. spec_priv->spec_config.period = val;
  814. return count;
  815. }
  816. static const struct file_operations fops_spectral_period = {
  817. .read = read_file_spectral_period,
  818. .write = write_file_spectral_period,
  819. .open = simple_open,
  820. .owner = THIS_MODULE,
  821. .llseek = default_llseek,
  822. };
  823. /***********************/
  824. /* spectral_fft_period */
  825. /***********************/
  826. static ssize_t read_file_spectral_fft_period(struct file *file,
  827. char __user *user_buf,
  828. size_t count, loff_t *ppos)
  829. {
  830. struct ath_spec_scan_priv *spec_priv = file->private_data;
  831. char buf[32];
  832. unsigned int len;
  833. len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period);
  834. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  835. }
  836. static ssize_t write_file_spectral_fft_period(struct file *file,
  837. const char __user *user_buf,
  838. size_t count, loff_t *ppos)
  839. {
  840. struct ath_spec_scan_priv *spec_priv = file->private_data;
  841. unsigned long val;
  842. char buf[32];
  843. ssize_t len;
  844. len = min(count, sizeof(buf) - 1);
  845. if (copy_from_user(buf, user_buf, len))
  846. return -EFAULT;
  847. buf[len] = '\0';
  848. if (kstrtoul(buf, 0, &val))
  849. return -EINVAL;
  850. if (val > 15)
  851. return -EINVAL;
  852. spec_priv->spec_config.fft_period = val;
  853. return count;
  854. }
  855. static const struct file_operations fops_spectral_fft_period = {
  856. .read = read_file_spectral_fft_period,
  857. .write = write_file_spectral_fft_period,
  858. .open = simple_open,
  859. .owner = THIS_MODULE,
  860. .llseek = default_llseek,
  861. };
  862. /*******************/
  863. /* Relay interface */
  864. /*******************/
  865. static struct dentry *create_buf_file_handler(const char *filename,
  866. struct dentry *parent,
  867. umode_t mode,
  868. struct rchan_buf *buf,
  869. int *is_global)
  870. {
  871. struct dentry *buf_file;
  872. buf_file = debugfs_create_file(filename, mode, parent, buf,
  873. &relay_file_operations);
  874. if (IS_ERR(buf_file))
  875. return NULL;
  876. *is_global = 1;
  877. return buf_file;
  878. }
  879. static int remove_buf_file_handler(struct dentry *dentry)
  880. {
  881. debugfs_remove(dentry);
  882. return 0;
  883. }
  884. static const struct rchan_callbacks rfs_spec_scan_cb = {
  885. .create_buf_file = create_buf_file_handler,
  886. .remove_buf_file = remove_buf_file_handler,
  887. };
  888. /*********************/
  889. /* Debug Init/Deinit */
  890. /*********************/
  891. void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv)
  892. {
  893. if (spec_priv->rfs_chan_spec_scan) {
  894. relay_close(spec_priv->rfs_chan_spec_scan);
  895. spec_priv->rfs_chan_spec_scan = NULL;
  896. }
  897. }
  898. EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug);
  899. void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv,
  900. struct dentry *debugfs_phy)
  901. {
  902. spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan",
  903. debugfs_phy,
  904. 1024, 256, &rfs_spec_scan_cb,
  905. NULL);
  906. if (!spec_priv->rfs_chan_spec_scan)
  907. return;
  908. debugfs_create_file("spectral_scan_ctl",
  909. 0600,
  910. debugfs_phy, spec_priv,
  911. &fops_spec_scan_ctl);
  912. debugfs_create_file("spectral_short_repeat",
  913. 0600,
  914. debugfs_phy, spec_priv,
  915. &fops_spectral_short_repeat);
  916. debugfs_create_file("spectral_count",
  917. 0600,
  918. debugfs_phy, spec_priv,
  919. &fops_spectral_count);
  920. debugfs_create_file("spectral_period",
  921. 0600,
  922. debugfs_phy, spec_priv,
  923. &fops_spectral_period);
  924. debugfs_create_file("spectral_fft_period",
  925. 0600,
  926. debugfs_phy, spec_priv,
  927. &fops_spectral_fft_period);
  928. }
  929. EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug);