dfs_pattern_detector.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2012 Neratec Solutions AG
  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/slab.h>
  17. #include <linux/export.h>
  18. #include "dfs_pattern_detector.h"
  19. #include "dfs_pri_detector.h"
  20. #include "ath.h"
  21. /**
  22. * struct radar_types - contains array of patterns defined for one DFS domain
  23. * @domain: DFS regulatory domain
  24. * @num_radar_types: number of radar types to follow
  25. * @radar_types: radar types array
  26. */
  27. struct radar_types {
  28. enum nl80211_dfs_regions region;
  29. u32 num_radar_types;
  30. const struct radar_detector_specs *radar_types;
  31. };
  32. /* percentage on ppb threshold to trigger detection */
  33. #define MIN_PPB_THRESH 50
  34. #define PPB_THRESH_RATE(PPB, RATE) ((PPB * RATE + 100 - RATE) / 100)
  35. #define PPB_THRESH(PPB) PPB_THRESH_RATE(PPB, MIN_PPB_THRESH)
  36. #define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
  37. /* percentage of pulse width tolerance */
  38. #define WIDTH_TOLERANCE 5
  39. #define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
  40. #define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
  41. #define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP) \
  42. { \
  43. ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
  44. (PRF2PRI(PMAX) - PRI_TOLERANCE), \
  45. (PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF, \
  46. PPB_THRESH(PPB), PRI_TOLERANCE, CHIRP \
  47. }
  48. /* radar types as defined by ETSI EN-301-893 v1.5.1 */
  49. static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
  50. ETSI_PATTERN(0, 0, 1, 700, 700, 1, 18, false),
  51. ETSI_PATTERN(1, 0, 5, 200, 1000, 1, 10, false),
  52. ETSI_PATTERN(2, 0, 15, 200, 1600, 1, 15, false),
  53. ETSI_PATTERN(3, 0, 15, 2300, 4000, 1, 25, false),
  54. ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20, false),
  55. ETSI_PATTERN(5, 0, 2, 300, 400, 3, 10, false),
  56. ETSI_PATTERN(6, 0, 2, 400, 1200, 3, 15, false),
  57. };
  58. static const struct radar_types etsi_radar_types_v15 = {
  59. .region = NL80211_DFS_ETSI,
  60. .num_radar_types = ARRAY_SIZE(etsi_radar_ref_types_v15),
  61. .radar_types = etsi_radar_ref_types_v15,
  62. };
  63. #define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP) \
  64. { \
  65. ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
  66. PMIN - PRI_TOLERANCE, \
  67. PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF, \
  68. PPB_THRESH(PPB), PRI_TOLERANCE, CHIRP \
  69. }
  70. /* radar types released on August 14, 2014
  71. * type 1 PRI values randomly selected within the range of 518 and 3066.
  72. * divide it to 3 groups is good enough for both of radar detection and
  73. * avoiding false detection based on practical test results
  74. * collected for more than a year.
  75. */
  76. static const struct radar_detector_specs fcc_radar_ref_types[] = {
  77. FCC_PATTERN(0, 0, 1, 1428, 1428, 1, 18, false),
  78. FCC_PATTERN(101, 0, 1, 518, 938, 1, 57, false),
  79. FCC_PATTERN(102, 0, 1, 938, 2000, 1, 27, false),
  80. FCC_PATTERN(103, 0, 1, 2000, 3066, 1, 18, false),
  81. FCC_PATTERN(2, 0, 5, 150, 230, 1, 23, false),
  82. FCC_PATTERN(3, 6, 10, 200, 500, 1, 16, false),
  83. FCC_PATTERN(4, 11, 20, 200, 500, 1, 12, false),
  84. FCC_PATTERN(5, 50, 100, 1000, 2000, 1, 1, true),
  85. FCC_PATTERN(6, 0, 1, 333, 333, 1, 9, false),
  86. };
  87. static const struct radar_types fcc_radar_types = {
  88. .region = NL80211_DFS_FCC,
  89. .num_radar_types = ARRAY_SIZE(fcc_radar_ref_types),
  90. .radar_types = fcc_radar_ref_types,
  91. };
  92. #define JP_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, RATE, CHIRP) \
  93. { \
  94. ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
  95. PMIN - PRI_TOLERANCE, \
  96. PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF, \
  97. PPB_THRESH_RATE(PPB, RATE), PRI_TOLERANCE, CHIRP \
  98. }
  99. static const struct radar_detector_specs jp_radar_ref_types[] = {
  100. JP_PATTERN(0, 0, 1, 1428, 1428, 1, 18, 29, false),
  101. JP_PATTERN(1, 2, 3, 3846, 3846, 1, 18, 29, false),
  102. JP_PATTERN(2, 0, 1, 1388, 1388, 1, 18, 50, false),
  103. JP_PATTERN(3, 1, 2, 4000, 4000, 1, 18, 50, false),
  104. JP_PATTERN(4, 0, 5, 150, 230, 1, 23, 50, false),
  105. JP_PATTERN(5, 6, 10, 200, 500, 1, 16, 50, false),
  106. JP_PATTERN(6, 11, 20, 200, 500, 1, 12, 50, false),
  107. JP_PATTERN(7, 50, 100, 1000, 2000, 1, 3, 50, false),
  108. JP_PATTERN(5, 0, 1, 333, 333, 1, 9, 50, false),
  109. };
  110. static const struct radar_types jp_radar_types = {
  111. .region = NL80211_DFS_JP,
  112. .num_radar_types = ARRAY_SIZE(jp_radar_ref_types),
  113. .radar_types = jp_radar_ref_types,
  114. };
  115. static const struct radar_types *dfs_domains[] = {
  116. &etsi_radar_types_v15,
  117. &fcc_radar_types,
  118. &jp_radar_types,
  119. };
  120. /**
  121. * get_dfs_domain_radar_types() - get radar types for a given DFS domain
  122. * @param domain DFS domain
  123. * @return radar_types ptr on success, NULL if DFS domain is not supported
  124. */
  125. static const struct radar_types *
  126. get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
  127. {
  128. u32 i;
  129. for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
  130. if (dfs_domains[i]->region == region)
  131. return dfs_domains[i];
  132. }
  133. return NULL;
  134. }
  135. /**
  136. * struct channel_detector - detector elements for a DFS channel
  137. * @head: list_head
  138. * @freq: frequency for this channel detector in MHz
  139. * @detectors: array of dynamically created detector elements for this freq
  140. *
  141. * Channel detectors are required to provide multi-channel DFS detection, e.g.
  142. * to support off-channel scanning. A pattern detector has a list of channels
  143. * radar pulses have been reported for in the past.
  144. */
  145. struct channel_detector {
  146. struct list_head head;
  147. u16 freq;
  148. struct pri_detector **detectors;
  149. };
  150. /* channel_detector_reset() - reset detector lines for a given channel */
  151. static void channel_detector_reset(struct dfs_pattern_detector *dpd,
  152. struct channel_detector *cd)
  153. {
  154. u32 i;
  155. if (cd == NULL)
  156. return;
  157. for (i = 0; i < dpd->num_radar_types; i++)
  158. cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
  159. }
  160. /* channel_detector_exit() - destructor */
  161. static void channel_detector_exit(struct dfs_pattern_detector *dpd,
  162. struct channel_detector *cd)
  163. {
  164. u32 i;
  165. if (cd == NULL)
  166. return;
  167. list_del(&cd->head);
  168. for (i = 0; i < dpd->num_radar_types; i++) {
  169. struct pri_detector *de = cd->detectors[i];
  170. if (de != NULL)
  171. de->exit(de);
  172. }
  173. kfree(cd->detectors);
  174. kfree(cd);
  175. }
  176. static struct channel_detector *
  177. channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
  178. {
  179. u32 sz, i;
  180. struct channel_detector *cd;
  181. cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
  182. if (cd == NULL)
  183. goto fail;
  184. INIT_LIST_HEAD(&cd->head);
  185. cd->freq = freq;
  186. sz = sizeof(cd->detectors) * dpd->num_radar_types;
  187. cd->detectors = kzalloc(sz, GFP_ATOMIC);
  188. if (cd->detectors == NULL)
  189. goto fail;
  190. for (i = 0; i < dpd->num_radar_types; i++) {
  191. const struct radar_detector_specs *rs = &dpd->radar_spec[i];
  192. struct pri_detector *de = pri_detector_init(rs);
  193. if (de == NULL)
  194. goto fail;
  195. cd->detectors[i] = de;
  196. }
  197. list_add(&cd->head, &dpd->channel_detectors);
  198. return cd;
  199. fail:
  200. ath_dbg(dpd->common, DFS,
  201. "failed to allocate channel_detector for freq=%d\n", freq);
  202. channel_detector_exit(dpd, cd);
  203. return NULL;
  204. }
  205. /**
  206. * channel_detector_get() - get channel detector for given frequency
  207. * @param dpd instance pointer
  208. * @param freq frequency in MHz
  209. * @return pointer to channel detector on success, NULL otherwise
  210. *
  211. * Return existing channel detector for the given frequency or return a
  212. * newly create one.
  213. */
  214. static struct channel_detector *
  215. channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
  216. {
  217. struct channel_detector *cd;
  218. list_for_each_entry(cd, &dpd->channel_detectors, head) {
  219. if (cd->freq == freq)
  220. return cd;
  221. }
  222. return channel_detector_create(dpd, freq);
  223. }
  224. /*
  225. * DFS Pattern Detector
  226. */
  227. /* dpd_reset(): reset all channel detectors */
  228. static void dpd_reset(struct dfs_pattern_detector *dpd)
  229. {
  230. struct channel_detector *cd;
  231. if (!list_empty(&dpd->channel_detectors))
  232. list_for_each_entry(cd, &dpd->channel_detectors, head)
  233. channel_detector_reset(dpd, cd);
  234. }
  235. static void dpd_exit(struct dfs_pattern_detector *dpd)
  236. {
  237. struct channel_detector *cd, *cd0;
  238. if (!list_empty(&dpd->channel_detectors))
  239. list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
  240. channel_detector_exit(dpd, cd);
  241. kfree(dpd);
  242. }
  243. static bool
  244. dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
  245. {
  246. u32 i;
  247. struct channel_detector *cd;
  248. /*
  249. * pulses received for a non-supported or un-initialized
  250. * domain are treated as detected radars for fail-safety
  251. */
  252. if (dpd->region == NL80211_DFS_UNSET)
  253. return true;
  254. cd = channel_detector_get(dpd, event->freq);
  255. if (cd == NULL)
  256. return false;
  257. /* reset detector on time stamp wraparound, caused by TSF reset */
  258. if (event->ts < dpd->last_pulse_ts)
  259. dpd_reset(dpd);
  260. dpd->last_pulse_ts = event->ts;
  261. /* do type individual pattern matching */
  262. for (i = 0; i < dpd->num_radar_types; i++) {
  263. struct pri_detector *pd = cd->detectors[i];
  264. struct pri_sequence *ps = pd->add_pulse(pd, event);
  265. if (ps != NULL) {
  266. ath_dbg(dpd->common, DFS,
  267. "DFS: radar found on freq=%d: id=%d, pri=%d, "
  268. "count=%d, count_false=%d\n",
  269. event->freq, pd->rs->type_id,
  270. ps->pri, ps->count, ps->count_falses);
  271. pd->reset(pd, dpd->last_pulse_ts);
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. static struct ath_dfs_pool_stats
  278. dpd_get_stats(struct dfs_pattern_detector *dpd)
  279. {
  280. return global_dfs_pool_stats;
  281. }
  282. static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
  283. enum nl80211_dfs_regions region)
  284. {
  285. const struct radar_types *rt;
  286. struct channel_detector *cd, *cd0;
  287. if (dpd->region == region)
  288. return true;
  289. dpd->region = NL80211_DFS_UNSET;
  290. rt = get_dfs_domain_radar_types(region);
  291. if (rt == NULL)
  292. return false;
  293. /* delete all channel detectors for previous DFS domain */
  294. if (!list_empty(&dpd->channel_detectors))
  295. list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
  296. channel_detector_exit(dpd, cd);
  297. dpd->radar_spec = rt->radar_types;
  298. dpd->num_radar_types = rt->num_radar_types;
  299. dpd->region = region;
  300. return true;
  301. }
  302. static struct dfs_pattern_detector default_dpd = {
  303. .exit = dpd_exit,
  304. .set_dfs_domain = dpd_set_domain,
  305. .add_pulse = dpd_add_pulse,
  306. .get_stats = dpd_get_stats,
  307. .region = NL80211_DFS_UNSET,
  308. };
  309. struct dfs_pattern_detector *
  310. dfs_pattern_detector_init(struct ath_common *common,
  311. enum nl80211_dfs_regions region)
  312. {
  313. struct dfs_pattern_detector *dpd;
  314. if (!config_enabled(CONFIG_CFG80211_CERTIFICATION_ONUS))
  315. return NULL;
  316. dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
  317. if (dpd == NULL)
  318. return NULL;
  319. *dpd = default_dpd;
  320. INIT_LIST_HEAD(&dpd->channel_detectors);
  321. dpd->common = common;
  322. if (dpd->set_dfs_domain(dpd, region))
  323. return dpd;
  324. ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
  325. kfree(dpd);
  326. return NULL;
  327. }
  328. EXPORT_SYMBOL(dfs_pattern_detector_init);