dfs_pattern_detector.c 10 KB

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