dfs_pri_detector.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/spinlock.h>
  18. #include "ath9k.h"
  19. #include "dfs_pattern_detector.h"
  20. #include "dfs_pri_detector.h"
  21. #include "dfs_debug.h"
  22. /**
  23. * struct pri_sequence - sequence of pulses matching one PRI
  24. * @head: list_head
  25. * @pri: pulse repetition interval (PRI) in usecs
  26. * @dur: duration of sequence in usecs
  27. * @count: number of pulses in this sequence
  28. * @count_falses: number of not matching pulses in this sequence
  29. * @first_ts: time stamp of first pulse in usecs
  30. * @last_ts: time stamp of last pulse in usecs
  31. * @deadline_ts: deadline when this sequence becomes invalid (first_ts + dur)
  32. */
  33. struct pri_sequence {
  34. struct list_head head;
  35. u32 pri;
  36. u32 dur;
  37. u32 count;
  38. u32 count_falses;
  39. u64 first_ts;
  40. u64 last_ts;
  41. u64 deadline_ts;
  42. };
  43. /**
  44. * struct pulse_elem - elements in pulse queue
  45. * @ts: time stamp in usecs
  46. */
  47. struct pulse_elem {
  48. struct list_head head;
  49. u64 ts;
  50. };
  51. /**
  52. * pde_get_multiple() - get number of multiples considering a given tolerance
  53. * @return factor if abs(val - factor*fraction) <= tolerance, 0 otherwise
  54. */
  55. static u32 pde_get_multiple(u32 val, u32 fraction, u32 tolerance)
  56. {
  57. u32 remainder;
  58. u32 factor;
  59. u32 delta;
  60. if (fraction == 0)
  61. return 0;
  62. delta = (val < fraction) ? (fraction - val) : (val - fraction);
  63. if (delta <= tolerance)
  64. /* val and fraction are within tolerance */
  65. return 1;
  66. factor = val / fraction;
  67. remainder = val % fraction;
  68. if (remainder > tolerance) {
  69. /* no exact match */
  70. if ((fraction - remainder) <= tolerance)
  71. /* remainder is within tolerance */
  72. factor++;
  73. else
  74. factor = 0;
  75. }
  76. return factor;
  77. }
  78. /**
  79. * DOC: Singleton Pulse and Sequence Pools
  80. *
  81. * Instances of pri_sequence and pulse_elem are kept in singleton pools to
  82. * reduce the number of dynamic allocations. They are shared between all
  83. * instances and grow up to the peak number of simultaneously used objects.
  84. *
  85. * Memory is freed after all references to the pools are released.
  86. */
  87. static u32 singleton_pool_references;
  88. static LIST_HEAD(pulse_pool);
  89. static LIST_HEAD(pseq_pool);
  90. static DEFINE_SPINLOCK(pool_lock);
  91. static void pool_register_ref(void)
  92. {
  93. spin_lock_bh(&pool_lock);
  94. singleton_pool_references++;
  95. DFS_POOL_STAT_INC(pool_reference);
  96. spin_unlock_bh(&pool_lock);
  97. }
  98. static void pool_deregister_ref(void)
  99. {
  100. spin_lock_bh(&pool_lock);
  101. singleton_pool_references--;
  102. DFS_POOL_STAT_DEC(pool_reference);
  103. if (singleton_pool_references == 0) {
  104. /* free singleton pools with no references left */
  105. struct pri_sequence *ps, *ps0;
  106. struct pulse_elem *p, *p0;
  107. list_for_each_entry_safe(p, p0, &pulse_pool, head) {
  108. list_del(&p->head);
  109. DFS_POOL_STAT_DEC(pulse_allocated);
  110. kfree(p);
  111. }
  112. list_for_each_entry_safe(ps, ps0, &pseq_pool, head) {
  113. list_del(&ps->head);
  114. DFS_POOL_STAT_DEC(pseq_allocated);
  115. kfree(ps);
  116. }
  117. }
  118. spin_unlock_bh(&pool_lock);
  119. }
  120. static void pool_put_pulse_elem(struct pulse_elem *pe)
  121. {
  122. spin_lock_bh(&pool_lock);
  123. list_add(&pe->head, &pulse_pool);
  124. DFS_POOL_STAT_DEC(pulse_used);
  125. spin_unlock_bh(&pool_lock);
  126. }
  127. static void pool_put_pseq_elem(struct pri_sequence *pse)
  128. {
  129. spin_lock_bh(&pool_lock);
  130. list_add(&pse->head, &pseq_pool);
  131. DFS_POOL_STAT_DEC(pseq_used);
  132. spin_unlock_bh(&pool_lock);
  133. }
  134. static struct pri_sequence *pool_get_pseq_elem(void)
  135. {
  136. struct pri_sequence *pse = NULL;
  137. spin_lock_bh(&pool_lock);
  138. if (!list_empty(&pseq_pool)) {
  139. pse = list_first_entry(&pseq_pool, struct pri_sequence, head);
  140. list_del(&pse->head);
  141. DFS_POOL_STAT_INC(pseq_used);
  142. }
  143. spin_unlock_bh(&pool_lock);
  144. return pse;
  145. }
  146. static struct pulse_elem *pool_get_pulse_elem(void)
  147. {
  148. struct pulse_elem *pe = NULL;
  149. spin_lock_bh(&pool_lock);
  150. if (!list_empty(&pulse_pool)) {
  151. pe = list_first_entry(&pulse_pool, struct pulse_elem, head);
  152. list_del(&pe->head);
  153. DFS_POOL_STAT_INC(pulse_used);
  154. }
  155. spin_unlock_bh(&pool_lock);
  156. return pe;
  157. }
  158. static struct pulse_elem *pulse_queue_get_tail(struct pri_detector *pde)
  159. {
  160. struct list_head *l = &pde->pulses;
  161. if (list_empty(l))
  162. return NULL;
  163. return list_entry(l->prev, struct pulse_elem, head);
  164. }
  165. static bool pulse_queue_dequeue(struct pri_detector *pde)
  166. {
  167. struct pulse_elem *p = pulse_queue_get_tail(pde);
  168. if (p != NULL) {
  169. list_del_init(&p->head);
  170. pde->count--;
  171. /* give it back to pool */
  172. pool_put_pulse_elem(p);
  173. }
  174. return (pde->count > 0);
  175. }
  176. /* remove pulses older than window */
  177. static void pulse_queue_check_window(struct pri_detector *pde)
  178. {
  179. u64 min_valid_ts;
  180. struct pulse_elem *p;
  181. /* there is no delta time with less than 2 pulses */
  182. if (pde->count < 2)
  183. return;
  184. if (pde->last_ts <= pde->window_size)
  185. return;
  186. min_valid_ts = pde->last_ts - pde->window_size;
  187. while ((p = pulse_queue_get_tail(pde)) != NULL) {
  188. if (p->ts >= min_valid_ts)
  189. return;
  190. pulse_queue_dequeue(pde);
  191. }
  192. }
  193. static bool pulse_queue_enqueue(struct pri_detector *pde, u64 ts)
  194. {
  195. struct pulse_elem *p = pool_get_pulse_elem();
  196. if (p == NULL) {
  197. p = kmalloc(sizeof(*p), GFP_KERNEL);
  198. if (p == NULL) {
  199. DFS_POOL_STAT_INC(pulse_alloc_error);
  200. return false;
  201. }
  202. DFS_POOL_STAT_INC(pulse_allocated);
  203. DFS_POOL_STAT_INC(pulse_used);
  204. }
  205. INIT_LIST_HEAD(&p->head);
  206. p->ts = ts;
  207. list_add(&p->head, &pde->pulses);
  208. pde->count++;
  209. pde->last_ts = ts;
  210. pulse_queue_check_window(pde);
  211. if (pde->count >= pde->max_count)
  212. pulse_queue_dequeue(pde);
  213. return true;
  214. }
  215. static bool pseq_handler_create_sequences(struct pri_detector *pde,
  216. u64 ts, u32 min_count)
  217. {
  218. struct pulse_elem *p;
  219. list_for_each_entry(p, &pde->pulses, head) {
  220. struct pri_sequence ps, *new_ps;
  221. struct pulse_elem *p2;
  222. u32 tmp_false_count;
  223. u64 min_valid_ts;
  224. u32 delta_ts = ts - p->ts;
  225. if (delta_ts < pde->rs->pri_min)
  226. /* ignore too small pri */
  227. continue;
  228. if (delta_ts > pde->rs->pri_max)
  229. /* stop on too large pri (sorted list) */
  230. break;
  231. /* build a new sequence with new potential pri */
  232. ps.count = 2;
  233. ps.count_falses = 0;
  234. ps.first_ts = p->ts;
  235. ps.last_ts = ts;
  236. ps.pri = ts - p->ts;
  237. ps.dur = ps.pri * (pde->rs->ppb - 1)
  238. + 2 * pde->rs->max_pri_tolerance;
  239. p2 = p;
  240. tmp_false_count = 0;
  241. min_valid_ts = ts - ps.dur;
  242. /* check which past pulses are candidates for new sequence */
  243. list_for_each_entry_continue(p2, &pde->pulses, head) {
  244. u32 factor;
  245. if (p2->ts < min_valid_ts)
  246. /* stop on crossing window border */
  247. break;
  248. /* check if pulse match (multi)PRI */
  249. factor = pde_get_multiple(ps.last_ts - p2->ts, ps.pri,
  250. pde->rs->max_pri_tolerance);
  251. if (factor > 0) {
  252. ps.count++;
  253. ps.first_ts = p2->ts;
  254. /*
  255. * on match, add the intermediate falses
  256. * and reset counter
  257. */
  258. ps.count_falses += tmp_false_count;
  259. tmp_false_count = 0;
  260. } else {
  261. /* this is a potential false one */
  262. tmp_false_count++;
  263. }
  264. }
  265. if (ps.count < min_count)
  266. /* did not reach minimum count, drop sequence */
  267. continue;
  268. /* this is a valid one, add it */
  269. ps.deadline_ts = ps.first_ts + ps.dur;
  270. new_ps = pool_get_pseq_elem();
  271. if (new_ps == NULL) {
  272. new_ps = kmalloc(sizeof(*new_ps), GFP_KERNEL);
  273. if (new_ps == NULL) {
  274. DFS_POOL_STAT_INC(pseq_alloc_error);
  275. return false;
  276. }
  277. DFS_POOL_STAT_INC(pseq_allocated);
  278. DFS_POOL_STAT_INC(pseq_used);
  279. }
  280. memcpy(new_ps, &ps, sizeof(ps));
  281. INIT_LIST_HEAD(&new_ps->head);
  282. list_add(&new_ps->head, &pde->sequences);
  283. }
  284. return true;
  285. }
  286. /* check new ts and add to all matching existing sequences */
  287. static u32
  288. pseq_handler_add_to_existing_seqs(struct pri_detector *pde, u64 ts)
  289. {
  290. u32 max_count = 0;
  291. struct pri_sequence *ps, *ps2;
  292. list_for_each_entry_safe(ps, ps2, &pde->sequences, head) {
  293. u32 delta_ts;
  294. u32 factor;
  295. /* first ensure that sequence is within window */
  296. if (ts > ps->deadline_ts) {
  297. list_del_init(&ps->head);
  298. pool_put_pseq_elem(ps);
  299. continue;
  300. }
  301. delta_ts = ts - ps->last_ts;
  302. factor = pde_get_multiple(delta_ts, ps->pri,
  303. pde->rs->max_pri_tolerance);
  304. if (factor > 0) {
  305. ps->last_ts = ts;
  306. ps->count++;
  307. if (max_count < ps->count)
  308. max_count = ps->count;
  309. } else {
  310. ps->count_falses++;
  311. }
  312. }
  313. return max_count;
  314. }
  315. static struct pri_sequence *
  316. pseq_handler_check_detection(struct pri_detector *pde)
  317. {
  318. struct pri_sequence *ps;
  319. if (list_empty(&pde->sequences))
  320. return NULL;
  321. list_for_each_entry(ps, &pde->sequences, head) {
  322. /*
  323. * we assume to have enough matching confidence if we
  324. * 1) have enough pulses
  325. * 2) have more matching than false pulses
  326. */
  327. if ((ps->count >= pde->rs->ppb_thresh) &&
  328. (ps->count * pde->rs->num_pri >= ps->count_falses))
  329. return ps;
  330. }
  331. return NULL;
  332. }
  333. /* free pulse queue and sequences list and give objects back to pools */
  334. static void pri_detector_reset(struct pri_detector *pde, u64 ts)
  335. {
  336. struct pri_sequence *ps, *ps0;
  337. struct pulse_elem *p, *p0;
  338. list_for_each_entry_safe(ps, ps0, &pde->sequences, head) {
  339. list_del_init(&ps->head);
  340. pool_put_pseq_elem(ps);
  341. }
  342. list_for_each_entry_safe(p, p0, &pde->pulses, head) {
  343. list_del_init(&p->head);
  344. pool_put_pulse_elem(p);
  345. }
  346. pde->count = 0;
  347. pde->last_ts = ts;
  348. }
  349. static void pri_detector_exit(struct pri_detector *de)
  350. {
  351. pri_detector_reset(de, 0);
  352. pool_deregister_ref();
  353. kfree(de);
  354. }
  355. static bool pri_detector_add_pulse(struct pri_detector *de,
  356. struct pulse_event *event)
  357. {
  358. u32 max_updated_seq;
  359. struct pri_sequence *ps;
  360. u64 ts = event->ts;
  361. const struct radar_detector_specs *rs = de->rs;
  362. /* ignore pulses not within width range */
  363. if ((rs->width_min > event->width) || (rs->width_max < event->width))
  364. return false;
  365. if ((ts - de->last_ts) < rs->max_pri_tolerance)
  366. /* if delta to last pulse is too short, don't use this pulse */
  367. return false;
  368. de->last_ts = ts;
  369. max_updated_seq = pseq_handler_add_to_existing_seqs(de, ts);
  370. if (!pseq_handler_create_sequences(de, ts, max_updated_seq)) {
  371. pr_err("failed to create pulse sequences\n");
  372. pri_detector_reset(de, ts);
  373. return false;
  374. }
  375. ps = pseq_handler_check_detection(de);
  376. if (ps != NULL) {
  377. pr_info("DFS: radar found: pri=%d, count=%d, count_false=%d\n",
  378. ps->pri, ps->count, ps->count_falses);
  379. pri_detector_reset(de, ts);
  380. return true;
  381. }
  382. pulse_queue_enqueue(de, ts);
  383. return false;
  384. }
  385. struct pri_detector *
  386. pri_detector_init(const struct radar_detector_specs *rs)
  387. {
  388. struct pri_detector *de;
  389. de = kzalloc(sizeof(*de), GFP_KERNEL);
  390. if (de == NULL)
  391. return NULL;
  392. de->exit = pri_detector_exit;
  393. de->add_pulse = pri_detector_add_pulse;
  394. de->reset = pri_detector_reset;
  395. INIT_LIST_HEAD(&de->sequences);
  396. INIT_LIST_HEAD(&de->pulses);
  397. de->window_size = rs->pri_max * rs->ppb * rs->num_pri;
  398. de->max_count = rs->ppb * 2;
  399. de->rs = rs;
  400. pool_register_ref();
  401. return de;
  402. }