common-spectral.c 30 KB

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