dfs_pri_detector.c 11 KB

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