dfs_pattern_detector.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "ath9k.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. /* for now, we support ETSI radar types, FCC and JP are TODO */
  68. static const struct radar_types *dfs_domains[] = {
  69. &etsi_radar_types_v15,
  70. };
  71. /**
  72. * get_dfs_domain_radar_types() - get radar types for a given DFS domain
  73. * @param domain DFS domain
  74. * @return radar_types ptr on success, NULL if DFS domain is not supported
  75. */
  76. static const struct radar_types *
  77. get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
  78. {
  79. u32 i;
  80. for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
  81. if (dfs_domains[i]->region == region)
  82. return dfs_domains[i];
  83. }
  84. return NULL;
  85. }
  86. /**
  87. * struct channel_detector - detector elements for a DFS channel
  88. * @head: list_head
  89. * @freq: frequency for this channel detector in MHz
  90. * @detectors: array of dynamically created detector elements for this freq
  91. *
  92. * Channel detectors are required to provide multi-channel DFS detection, e.g.
  93. * to support off-channel scanning. A pattern detector has a list of channels
  94. * radar pulses have been reported for in the past.
  95. */
  96. struct channel_detector {
  97. struct list_head head;
  98. u16 freq;
  99. struct pri_detector **detectors;
  100. };
  101. /* channel_detector_reset() - reset detector lines for a given channel */
  102. static void channel_detector_reset(struct dfs_pattern_detector *dpd,
  103. struct channel_detector *cd)
  104. {
  105. u32 i;
  106. if (cd == NULL)
  107. return;
  108. for (i = 0; i < dpd->num_radar_types; i++)
  109. cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
  110. }
  111. /* channel_detector_exit() - destructor */
  112. static void channel_detector_exit(struct dfs_pattern_detector *dpd,
  113. struct channel_detector *cd)
  114. {
  115. u32 i;
  116. if (cd == NULL)
  117. return;
  118. list_del(&cd->head);
  119. for (i = 0; i < dpd->num_radar_types; i++) {
  120. struct pri_detector *de = cd->detectors[i];
  121. if (de != NULL)
  122. de->exit(de);
  123. }
  124. kfree(cd->detectors);
  125. kfree(cd);
  126. }
  127. static struct channel_detector *
  128. channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
  129. {
  130. u32 sz, i;
  131. struct channel_detector *cd;
  132. struct ath_common *common = ath9k_hw_common(dpd->ah);
  133. cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
  134. if (cd == NULL)
  135. goto fail;
  136. INIT_LIST_HEAD(&cd->head);
  137. cd->freq = freq;
  138. sz = sizeof(cd->detectors) * dpd->num_radar_types;
  139. cd->detectors = kzalloc(sz, GFP_ATOMIC);
  140. if (cd->detectors == NULL)
  141. goto fail;
  142. for (i = 0; i < dpd->num_radar_types; i++) {
  143. const struct radar_detector_specs *rs = &dpd->radar_spec[i];
  144. struct pri_detector *de = pri_detector_init(rs);
  145. if (de == NULL)
  146. goto fail;
  147. cd->detectors[i] = de;
  148. }
  149. list_add(&cd->head, &dpd->channel_detectors);
  150. return cd;
  151. fail:
  152. ath_dbg(common, DFS,
  153. "failed to allocate channel_detector for freq=%d\n", freq);
  154. channel_detector_exit(dpd, cd);
  155. return NULL;
  156. }
  157. /**
  158. * channel_detector_get() - get channel detector for given frequency
  159. * @param dpd instance pointer
  160. * @param freq frequency in MHz
  161. * @return pointer to channel detector on success, NULL otherwise
  162. *
  163. * Return existing channel detector for the given frequency or return a
  164. * newly create one.
  165. */
  166. static struct channel_detector *
  167. channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
  168. {
  169. struct channel_detector *cd;
  170. list_for_each_entry(cd, &dpd->channel_detectors, head) {
  171. if (cd->freq == freq)
  172. return cd;
  173. }
  174. return channel_detector_create(dpd, freq);
  175. }
  176. /*
  177. * DFS Pattern Detector
  178. */
  179. /* dpd_reset(): reset all channel detectors */
  180. static void dpd_reset(struct dfs_pattern_detector *dpd)
  181. {
  182. struct channel_detector *cd;
  183. if (!list_empty(&dpd->channel_detectors))
  184. list_for_each_entry(cd, &dpd->channel_detectors, head)
  185. channel_detector_reset(dpd, cd);
  186. }
  187. static void dpd_exit(struct dfs_pattern_detector *dpd)
  188. {
  189. struct channel_detector *cd, *cd0;
  190. if (!list_empty(&dpd->channel_detectors))
  191. list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
  192. channel_detector_exit(dpd, cd);
  193. kfree(dpd);
  194. }
  195. static bool
  196. dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
  197. {
  198. u32 i;
  199. struct channel_detector *cd;
  200. /*
  201. * pulses received for a non-supported or un-initialized
  202. * domain are treated as detected radars for fail-safety
  203. */
  204. if (dpd->region == NL80211_DFS_UNSET)
  205. return true;
  206. cd = channel_detector_get(dpd, event->freq);
  207. if (cd == NULL)
  208. return false;
  209. dpd->last_pulse_ts = event->ts;
  210. /* reset detector on time stamp wraparound, caused by TSF reset */
  211. if (event->ts < dpd->last_pulse_ts)
  212. dpd_reset(dpd);
  213. /* do type individual pattern matching */
  214. for (i = 0; i < dpd->num_radar_types; i++) {
  215. struct pri_detector *pd = cd->detectors[i];
  216. struct pri_sequence *ps = pd->add_pulse(pd, event);
  217. if (ps != NULL) {
  218. ath_dbg(ath9k_hw_common(dpd->ah), DFS,
  219. "DFS: radar found on freq=%d: id=%d, pri=%d, "
  220. "count=%d, count_false=%d\n",
  221. event->freq, pd->rs->type_id,
  222. ps->pri, ps->count, ps->count_falses);
  223. channel_detector_reset(dpd, cd);
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
  230. enum nl80211_dfs_regions region)
  231. {
  232. const struct radar_types *rt;
  233. struct channel_detector *cd, *cd0;
  234. if (dpd->region == region)
  235. return true;
  236. dpd->region = NL80211_DFS_UNSET;
  237. rt = get_dfs_domain_radar_types(region);
  238. if (rt == NULL)
  239. return false;
  240. /* delete all channel detectors for previous DFS domain */
  241. if (!list_empty(&dpd->channel_detectors))
  242. list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
  243. channel_detector_exit(dpd, cd);
  244. dpd->radar_spec = rt->radar_types;
  245. dpd->num_radar_types = rt->num_radar_types;
  246. dpd->region = region;
  247. return true;
  248. }
  249. static struct dfs_pattern_detector default_dpd = {
  250. .exit = dpd_exit,
  251. .set_dfs_domain = dpd_set_domain,
  252. .add_pulse = dpd_add_pulse,
  253. .region = NL80211_DFS_UNSET,
  254. };
  255. struct dfs_pattern_detector *
  256. dfs_pattern_detector_init(struct ath_hw *ah, enum nl80211_dfs_regions region)
  257. {
  258. struct dfs_pattern_detector *dpd;
  259. struct ath_common *common = ath9k_hw_common(ah);
  260. dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
  261. if (dpd == NULL)
  262. return NULL;
  263. *dpd = default_dpd;
  264. INIT_LIST_HEAD(&dpd->channel_detectors);
  265. dpd->ah = ah;
  266. if (dpd->set_dfs_domain(dpd, region))
  267. return dpd;
  268. ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
  269. kfree(dpd);
  270. return NULL;
  271. }
  272. EXPORT_SYMBOL(dfs_pattern_detector_init);