common-spectral.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. if (!spec_priv->rfs_chan_spec_scan)
  444. return 1;
  445. /* Output buffers are full, no need to process anything
  446. * since there is no space to put the result anyway
  447. */
  448. ret = ath_cmn_is_fft_buf_full(spec_priv);
  449. if (ret == 1) {
  450. ath_dbg(common, SPECTRAL_SCAN, "FFT report ignored, no space "
  451. "left on output buffers\n");
  452. return 1;
  453. }
  454. chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef);
  455. if ((chan_type == NL80211_CHAN_HT40MINUS) ||
  456. (chan_type == NL80211_CHAN_HT40PLUS)) {
  457. fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
  458. sample_len = SPECTRAL_HT20_40_SAMPLE_LEN;
  459. num_bins = SPECTRAL_HT20_40_NUM_BINS;
  460. fft_idx_validator = &ath_cmn_max_idx_verify_ht20_40_fft;
  461. fft_handler = &ath_cmn_process_ht20_40_fft;
  462. } else {
  463. fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
  464. sample_len = SPECTRAL_HT20_SAMPLE_LEN;
  465. num_bins = SPECTRAL_HT20_NUM_BINS;
  466. fft_idx_validator = ath_cmn_max_idx_verify_ht20_fft;
  467. fft_handler = &ath_cmn_process_ht20_fft;
  468. }
  469. ath_dbg(common, SPECTRAL_SCAN, "Got radar dump bw_info: 0x%X,"
  470. "len: %i fft_len: %i\n",
  471. radar_info->pulse_bw_info,
  472. len,
  473. fft_len);
  474. sample_start = vdata;
  475. for (i = 0; i < len - 2; i++) {
  476. sample_bytes++;
  477. /* Only a single sample received, no need to look
  478. * for the sample's end, do the correction based
  479. * on the packet's length instead. Note that hw
  480. * will always put the radar_info structure on
  481. * the end.
  482. */
  483. if (len <= fft_len + 2) {
  484. sample_bytes = len - sizeof(struct ath_radar_info);
  485. got_slen = 1;
  486. }
  487. /* Search for the end of the FFT frame between
  488. * sample_len - 1 and sample_len + 2. exp_max is 3
  489. * bits long and it's the only value on the last
  490. * byte of the frame so since it'll be smaller than
  491. * the next byte (the first bin of the next sample)
  492. * 90% of the time, we can use it as a separator.
  493. */
  494. if (vdata[i] <= 0x7 && sample_bytes >= sample_len - 1) {
  495. /* Got a frame length within boundaries, there are
  496. * four scenarios here:
  497. *
  498. * a) sample_len -> We got the correct length
  499. * b) sample_len + 2 -> 2 bytes added around bin[31]
  500. * c) sample_len - 1 -> The first byte is missing
  501. * d) sample_len + 1 -> b + c at the same time
  502. *
  503. * When MAC adds 2 extra bytes, bin[31] and bin[32]
  504. * have the same value, so we can use that for further
  505. * verification in cases b and d.
  506. */
  507. /* Did we go too far ? If so we couldn't determine
  508. * this sample's boundaries, discard any further
  509. * data
  510. */
  511. if ((sample_bytes > sample_len + 2) ||
  512. ((sample_bytes > sample_len) &&
  513. (sample_start[31] != sample_start[32])))
  514. break;
  515. /* See if we got a valid frame by checking the
  516. * consistency of mag_info fields. This is to
  517. * prevent from "fixing" a correct frame.
  518. * Failure is non-fatal, later frames may
  519. * be valid.
  520. */
  521. if (!fft_idx_validator(&vdata[i], i)) {
  522. ath_dbg(common, SPECTRAL_SCAN,
  523. "Found valid fft frame at %i\n", i);
  524. got_slen = 1;
  525. }
  526. /* We expect 1 - 2 more bytes */
  527. else if ((sample_start[31] == sample_start[32]) &&
  528. (sample_bytes >= sample_len) &&
  529. (sample_bytes < sample_len + 2) &&
  530. (vdata[i + 1] <= 0x7))
  531. continue;
  532. /* Try to distinguish cases a and c */
  533. else if ((sample_bytes == sample_len - 1) &&
  534. (vdata[i + 1] <= 0x7))
  535. continue;
  536. got_slen = 1;
  537. }
  538. if (got_slen) {
  539. ath_dbg(common, SPECTRAL_SCAN, "FFT frame len: %i\n",
  540. sample_bytes);
  541. /* Only try to fix a frame if it's the only one
  542. * on the report, else just skip it.
  543. */
  544. if (sample_bytes != sample_len && len <= fft_len + 2) {
  545. ath_cmn_copy_fft_frame(sample_start,
  546. sample_buf, sample_len,
  547. sample_bytes);
  548. fft_handler(rs, spec_priv, sample_buf,
  549. tsf, freq, chan_type);
  550. memset(sample_buf, 0, SPECTRAL_SAMPLE_MAX_LEN);
  551. /* Mix the received bins to the /dev/random
  552. * pool
  553. */
  554. add_device_randomness(sample_buf, num_bins);
  555. }
  556. /* Process a normal frame */
  557. if (sample_bytes == sample_len) {
  558. ret = fft_handler(rs, spec_priv, sample_start,
  559. tsf, freq, chan_type);
  560. /* Mix the received bins to the /dev/random
  561. * pool
  562. */
  563. add_device_randomness(sample_start, num_bins);
  564. }
  565. /* Short report processed, break out of the
  566. * loop.
  567. */
  568. if (len <= fft_len + 2)
  569. break;
  570. sample_start = &vdata[i + 1];
  571. /* -1 to grab sample_len -1, -2 since
  572. * they 'll get increased by one. In case
  573. * of failure try to recover by going byte
  574. * by byte instead.
  575. */
  576. if (ret == 0) {
  577. i += num_bins - 2;
  578. sample_bytes = num_bins - 2;
  579. }
  580. got_slen = 0;
  581. }
  582. }
  583. i -= num_bins - 2;
  584. if (len - i != sizeof(struct ath_radar_info))
  585. ath_dbg(common, SPECTRAL_SCAN, "FFT report truncated"
  586. "(bytes left: %i)\n",
  587. len - i);
  588. return 1;
  589. }
  590. EXPORT_SYMBOL(ath_cmn_process_fft);
  591. /*********************/
  592. /* spectral_scan_ctl */
  593. /*********************/
  594. static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf,
  595. size_t count, loff_t *ppos)
  596. {
  597. struct ath_spec_scan_priv *spec_priv = file->private_data;
  598. char *mode = "";
  599. unsigned int len;
  600. switch (spec_priv->spectral_mode) {
  601. case SPECTRAL_DISABLED:
  602. mode = "disable";
  603. break;
  604. case SPECTRAL_BACKGROUND:
  605. mode = "background";
  606. break;
  607. case SPECTRAL_CHANSCAN:
  608. mode = "chanscan";
  609. break;
  610. case SPECTRAL_MANUAL:
  611. mode = "manual";
  612. break;
  613. }
  614. len = strlen(mode);
  615. return simple_read_from_buffer(user_buf, count, ppos, mode, len);
  616. }
  617. void ath9k_cmn_spectral_scan_trigger(struct ath_common *common,
  618. struct ath_spec_scan_priv *spec_priv)
  619. {
  620. struct ath_hw *ah = spec_priv->ah;
  621. u32 rxfilter;
  622. if (config_enabled(CONFIG_ATH9K_TX99))
  623. return;
  624. if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
  625. ath_err(common, "spectrum analyzer not implemented on this hardware\n");
  626. return;
  627. }
  628. ath_ps_ops(common)->wakeup(common);
  629. rxfilter = ath9k_hw_getrxfilter(ah);
  630. ath9k_hw_setrxfilter(ah, rxfilter |
  631. ATH9K_RX_FILTER_PHYRADAR |
  632. ATH9K_RX_FILTER_PHYERR);
  633. /* TODO: usually this should not be neccesary, but for some reason
  634. * (or in some mode?) the trigger must be called after the
  635. * configuration, otherwise the register will have its values reset
  636. * (on my ar9220 to value 0x01002310)
  637. */
  638. ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode);
  639. ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
  640. ath_ps_ops(common)->restore(common);
  641. }
  642. EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger);
  643. int ath9k_cmn_spectral_scan_config(struct ath_common *common,
  644. struct ath_spec_scan_priv *spec_priv,
  645. enum spectral_mode spectral_mode)
  646. {
  647. struct ath_hw *ah = spec_priv->ah;
  648. if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
  649. ath_err(common, "spectrum analyzer not implemented on this hardware\n");
  650. return -1;
  651. }
  652. switch (spectral_mode) {
  653. case SPECTRAL_DISABLED:
  654. spec_priv->spec_config.enabled = 0;
  655. break;
  656. case SPECTRAL_BACKGROUND:
  657. /* send endless samples.
  658. * TODO: is this really useful for "background"?
  659. */
  660. spec_priv->spec_config.endless = 1;
  661. spec_priv->spec_config.enabled = 1;
  662. break;
  663. case SPECTRAL_CHANSCAN:
  664. case SPECTRAL_MANUAL:
  665. spec_priv->spec_config.endless = 0;
  666. spec_priv->spec_config.enabled = 1;
  667. break;
  668. default:
  669. return -1;
  670. }
  671. ath_ps_ops(common)->wakeup(common);
  672. ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config);
  673. ath_ps_ops(common)->restore(common);
  674. spec_priv->spectral_mode = spectral_mode;
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config);
  678. static ssize_t write_file_spec_scan_ctl(struct file *file,
  679. const char __user *user_buf,
  680. size_t count, loff_t *ppos)
  681. {
  682. struct ath_spec_scan_priv *spec_priv = file->private_data;
  683. struct ath_common *common = ath9k_hw_common(spec_priv->ah);
  684. char buf[32];
  685. ssize_t len;
  686. if (config_enabled(CONFIG_ATH9K_TX99))
  687. return -EOPNOTSUPP;
  688. len = min(count, sizeof(buf) - 1);
  689. if (copy_from_user(buf, user_buf, len))
  690. return -EFAULT;
  691. buf[len] = '\0';
  692. if (strncmp("trigger", buf, 7) == 0) {
  693. ath9k_cmn_spectral_scan_trigger(common, spec_priv);
  694. } else if (strncmp("background", buf, 10) == 0) {
  695. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND);
  696. ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n");
  697. } else if (strncmp("chanscan", buf, 8) == 0) {
  698. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN);
  699. ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n");
  700. } else if (strncmp("manual", buf, 6) == 0) {
  701. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL);
  702. ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n");
  703. } else if (strncmp("disable", buf, 7) == 0) {
  704. ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED);
  705. ath_dbg(common, CONFIG, "spectral scan: disabled\n");
  706. } else {
  707. return -EINVAL;
  708. }
  709. return count;
  710. }
  711. static const struct file_operations fops_spec_scan_ctl = {
  712. .read = read_file_spec_scan_ctl,
  713. .write = write_file_spec_scan_ctl,
  714. .open = simple_open,
  715. .owner = THIS_MODULE,
  716. .llseek = default_llseek,
  717. };
  718. /*************************/
  719. /* spectral_short_repeat */
  720. /*************************/
  721. static ssize_t read_file_spectral_short_repeat(struct file *file,
  722. char __user *user_buf,
  723. size_t count, loff_t *ppos)
  724. {
  725. struct ath_spec_scan_priv *spec_priv = file->private_data;
  726. char buf[32];
  727. unsigned int len;
  728. len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat);
  729. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  730. }
  731. static ssize_t write_file_spectral_short_repeat(struct file *file,
  732. const char __user *user_buf,
  733. size_t count, loff_t *ppos)
  734. {
  735. struct ath_spec_scan_priv *spec_priv = file->private_data;
  736. unsigned long val;
  737. char buf[32];
  738. ssize_t len;
  739. len = min(count, sizeof(buf) - 1);
  740. if (copy_from_user(buf, user_buf, len))
  741. return -EFAULT;
  742. buf[len] = '\0';
  743. if (kstrtoul(buf, 0, &val))
  744. return -EINVAL;
  745. if (val > 1)
  746. return -EINVAL;
  747. spec_priv->spec_config.short_repeat = val;
  748. return count;
  749. }
  750. static const struct file_operations fops_spectral_short_repeat = {
  751. .read = read_file_spectral_short_repeat,
  752. .write = write_file_spectral_short_repeat,
  753. .open = simple_open,
  754. .owner = THIS_MODULE,
  755. .llseek = default_llseek,
  756. };
  757. /******************/
  758. /* spectral_count */
  759. /******************/
  760. static ssize_t read_file_spectral_count(struct file *file,
  761. char __user *user_buf,
  762. size_t count, loff_t *ppos)
  763. {
  764. struct ath_spec_scan_priv *spec_priv = file->private_data;
  765. char buf[32];
  766. unsigned int len;
  767. len = sprintf(buf, "%d\n", spec_priv->spec_config.count);
  768. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  769. }
  770. static ssize_t write_file_spectral_count(struct file *file,
  771. const char __user *user_buf,
  772. size_t count, loff_t *ppos)
  773. {
  774. struct ath_spec_scan_priv *spec_priv = file->private_data;
  775. unsigned long val;
  776. char buf[32];
  777. ssize_t len;
  778. len = min(count, sizeof(buf) - 1);
  779. if (copy_from_user(buf, user_buf, len))
  780. return -EFAULT;
  781. buf[len] = '\0';
  782. if (kstrtoul(buf, 0, &val))
  783. return -EINVAL;
  784. if (val > 255)
  785. return -EINVAL;
  786. spec_priv->spec_config.count = val;
  787. return count;
  788. }
  789. static const struct file_operations fops_spectral_count = {
  790. .read = read_file_spectral_count,
  791. .write = write_file_spectral_count,
  792. .open = simple_open,
  793. .owner = THIS_MODULE,
  794. .llseek = default_llseek,
  795. };
  796. /*******************/
  797. /* spectral_period */
  798. /*******************/
  799. static ssize_t read_file_spectral_period(struct file *file,
  800. char __user *user_buf,
  801. size_t count, loff_t *ppos)
  802. {
  803. struct ath_spec_scan_priv *spec_priv = file->private_data;
  804. char buf[32];
  805. unsigned int len;
  806. len = sprintf(buf, "%d\n", spec_priv->spec_config.period);
  807. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  808. }
  809. static ssize_t write_file_spectral_period(struct file *file,
  810. const char __user *user_buf,
  811. size_t count, loff_t *ppos)
  812. {
  813. struct ath_spec_scan_priv *spec_priv = file->private_data;
  814. unsigned long val;
  815. char buf[32];
  816. ssize_t len;
  817. len = min(count, sizeof(buf) - 1);
  818. if (copy_from_user(buf, user_buf, len))
  819. return -EFAULT;
  820. buf[len] = '\0';
  821. if (kstrtoul(buf, 0, &val))
  822. return -EINVAL;
  823. if (val > 255)
  824. return -EINVAL;
  825. spec_priv->spec_config.period = val;
  826. return count;
  827. }
  828. static const struct file_operations fops_spectral_period = {
  829. .read = read_file_spectral_period,
  830. .write = write_file_spectral_period,
  831. .open = simple_open,
  832. .owner = THIS_MODULE,
  833. .llseek = default_llseek,
  834. };
  835. /***********************/
  836. /* spectral_fft_period */
  837. /***********************/
  838. static ssize_t read_file_spectral_fft_period(struct file *file,
  839. char __user *user_buf,
  840. size_t count, loff_t *ppos)
  841. {
  842. struct ath_spec_scan_priv *spec_priv = file->private_data;
  843. char buf[32];
  844. unsigned int len;
  845. len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period);
  846. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  847. }
  848. static ssize_t write_file_spectral_fft_period(struct file *file,
  849. const char __user *user_buf,
  850. size_t count, loff_t *ppos)
  851. {
  852. struct ath_spec_scan_priv *spec_priv = file->private_data;
  853. unsigned long val;
  854. char buf[32];
  855. ssize_t len;
  856. len = min(count, sizeof(buf) - 1);
  857. if (copy_from_user(buf, user_buf, len))
  858. return -EFAULT;
  859. buf[len] = '\0';
  860. if (kstrtoul(buf, 0, &val))
  861. return -EINVAL;
  862. if (val > 15)
  863. return -EINVAL;
  864. spec_priv->spec_config.fft_period = val;
  865. return count;
  866. }
  867. static const struct file_operations fops_spectral_fft_period = {
  868. .read = read_file_spectral_fft_period,
  869. .write = write_file_spectral_fft_period,
  870. .open = simple_open,
  871. .owner = THIS_MODULE,
  872. .llseek = default_llseek,
  873. };
  874. /*******************/
  875. /* Relay interface */
  876. /*******************/
  877. static struct dentry *create_buf_file_handler(const char *filename,
  878. struct dentry *parent,
  879. umode_t mode,
  880. struct rchan_buf *buf,
  881. int *is_global)
  882. {
  883. struct dentry *buf_file;
  884. buf_file = debugfs_create_file(filename, mode, parent, buf,
  885. &relay_file_operations);
  886. *is_global = 1;
  887. return buf_file;
  888. }
  889. static int remove_buf_file_handler(struct dentry *dentry)
  890. {
  891. debugfs_remove(dentry);
  892. return 0;
  893. }
  894. static struct rchan_callbacks rfs_spec_scan_cb = {
  895. .create_buf_file = create_buf_file_handler,
  896. .remove_buf_file = remove_buf_file_handler,
  897. };
  898. /*********************/
  899. /* Debug Init/Deinit */
  900. /*********************/
  901. void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv)
  902. {
  903. if (config_enabled(CONFIG_ATH9K_DEBUGFS) && spec_priv->rfs_chan_spec_scan) {
  904. relay_close(spec_priv->rfs_chan_spec_scan);
  905. spec_priv->rfs_chan_spec_scan = NULL;
  906. }
  907. }
  908. EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug);
  909. void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv,
  910. struct dentry *debugfs_phy)
  911. {
  912. spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan",
  913. debugfs_phy,
  914. 1024, 256, &rfs_spec_scan_cb,
  915. NULL);
  916. if (!spec_priv->rfs_chan_spec_scan)
  917. return;
  918. debugfs_create_file("spectral_scan_ctl",
  919. S_IRUSR | S_IWUSR,
  920. debugfs_phy, spec_priv,
  921. &fops_spec_scan_ctl);
  922. debugfs_create_file("spectral_short_repeat",
  923. S_IRUSR | S_IWUSR,
  924. debugfs_phy, spec_priv,
  925. &fops_spectral_short_repeat);
  926. debugfs_create_file("spectral_count",
  927. S_IRUSR | S_IWUSR,
  928. debugfs_phy, spec_priv,
  929. &fops_spectral_count);
  930. debugfs_create_file("spectral_period",
  931. S_IRUSR | S_IWUSR,
  932. debugfs_phy, spec_priv,
  933. &fops_spectral_period);
  934. debugfs_create_file("spectral_fft_period",
  935. S_IRUSR | S_IWUSR,
  936. debugfs_phy, spec_priv,
  937. &fops_spectral_fft_period);
  938. }
  939. EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug);