2 * Copyright (c) 2012 Neratec Solutions AG
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.
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.
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
21 #include "dfs_pattern_detector.h"
22 #include "dfs_pri_detector.h"
23 #include "dfs_debug.h"
26 * struct pri_sequence - sequence of pulses matching one PRI
28 * @pri: pulse repetition interval (PRI) in usecs
29 * @dur: duration of sequence in usecs
30 * @count: number of pulses in this sequence
31 * @count_falses: number of not matching pulses in this sequence
32 * @first_ts: time stamp of first pulse in usecs
33 * @last_ts: time stamp of last pulse in usecs
34 * @deadline_ts: deadline when this sequence becomes invalid (first_ts + dur)
37 struct list_head head
;
48 * struct pulse_elem - elements in pulse queue
49 * @ts: time stamp in usecs
52 struct list_head head
;
57 * pde_get_multiple() - get number of multiples considering a given tolerance
58 * @return factor if abs(val - factor*fraction) <= tolerance, 0 otherwise
60 static u32
pde_get_multiple(u32 val
, u32 fraction
, u32 tolerance
)
69 delta
= (val
< fraction
) ? (fraction
- val
) : (val
- fraction
);
71 if (delta
<= tolerance
)
72 /* val and fraction are within tolerance */
75 factor
= val
/ fraction
;
76 remainder
= val
% fraction
;
77 if (remainder
> tolerance
) {
79 if ((fraction
- remainder
) <= tolerance
)
80 /* remainder is within tolerance */
89 * DOC: Singleton Pulse and Sequence Pools
91 * Instances of pri_sequence and pulse_elem are kept in singleton pools to
92 * reduce the number of dynamic allocations. They are shared between all
93 * instances and grow up to the peak number of simultaneously used objects.
95 * Memory is freed after all references to the pools are released.
97 static u32 singleton_pool_references
;
98 static LIST_HEAD(pulse_pool
);
99 static LIST_HEAD(pseq_pool
);
100 static DEFINE_SPINLOCK(pool_lock
);
102 static void pool_register_ref(void)
104 spin_lock_bh(&pool_lock
);
105 singleton_pool_references
++;
106 DFS_POOL_STAT_INC(pool_reference
);
107 spin_unlock_bh(&pool_lock
);
110 static void pool_deregister_ref(void)
112 spin_lock_bh(&pool_lock
);
113 singleton_pool_references
--;
114 DFS_POOL_STAT_DEC(pool_reference
);
115 if (singleton_pool_references
== 0) {
116 /* free singleton pools with no references left */
117 struct pri_sequence
*ps
, *ps0
;
118 struct pulse_elem
*p
, *p0
;
120 list_for_each_entry_safe(p
, p0
, &pulse_pool
, head
) {
122 DFS_POOL_STAT_DEC(pulse_allocated
);
125 list_for_each_entry_safe(ps
, ps0
, &pseq_pool
, head
) {
127 DFS_POOL_STAT_DEC(pseq_allocated
);
131 spin_unlock_bh(&pool_lock
);
134 static void pool_put_pulse_elem(struct pulse_elem
*pe
)
136 spin_lock_bh(&pool_lock
);
137 list_add(&pe
->head
, &pulse_pool
);
138 DFS_POOL_STAT_DEC(pulse_used
);
139 spin_unlock_bh(&pool_lock
);
142 static void pool_put_pseq_elem(struct pri_sequence
*pse
)
144 spin_lock_bh(&pool_lock
);
145 list_add(&pse
->head
, &pseq_pool
);
146 DFS_POOL_STAT_DEC(pseq_used
);
147 spin_unlock_bh(&pool_lock
);
150 static struct pri_sequence
*pool_get_pseq_elem(void)
152 struct pri_sequence
*pse
= NULL
;
153 spin_lock_bh(&pool_lock
);
154 if (!list_empty(&pseq_pool
)) {
155 pse
= list_first_entry(&pseq_pool
, struct pri_sequence
, head
);
156 list_del(&pse
->head
);
157 DFS_POOL_STAT_INC(pseq_used
);
159 spin_unlock_bh(&pool_lock
);
163 static struct pulse_elem
*pool_get_pulse_elem(void)
165 struct pulse_elem
*pe
= NULL
;
166 spin_lock_bh(&pool_lock
);
167 if (!list_empty(&pulse_pool
)) {
168 pe
= list_first_entry(&pulse_pool
, struct pulse_elem
, head
);
170 DFS_POOL_STAT_INC(pulse_used
);
172 spin_unlock_bh(&pool_lock
);
176 static struct pulse_elem
*pulse_queue_get_tail(struct pri_detector
*pde
)
178 struct list_head
*l
= &pde
->pulses
;
181 return list_entry(l
->prev
, struct pulse_elem
, head
);
184 static bool pulse_queue_dequeue(struct pri_detector
*pde
)
186 struct pulse_elem
*p
= pulse_queue_get_tail(pde
);
188 list_del_init(&p
->head
);
190 /* give it back to pool */
191 pool_put_pulse_elem(p
);
193 return (pde
->count
> 0);
196 /* remove pulses older than window */
197 static void pulse_queue_check_window(struct pri_detector
*pde
)
200 struct pulse_elem
*p
;
202 /* there is no delta time with less than 2 pulses */
206 if (pde
->last_ts
<= pde
->window_size
)
209 min_valid_ts
= pde
->last_ts
- pde
->window_size
;
210 while ((p
= pulse_queue_get_tail(pde
)) != NULL
) {
211 if (p
->ts
>= min_valid_ts
)
213 pulse_queue_dequeue(pde
);
217 static bool pulse_queue_enqueue(struct pri_detector
*pde
, u64 ts
)
219 struct pulse_elem
*p
= pool_get_pulse_elem();
221 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
223 DFS_POOL_STAT_INC(pulse_alloc_error
);
226 DFS_POOL_STAT_INC(pulse_allocated
);
227 DFS_POOL_STAT_INC(pulse_used
);
229 INIT_LIST_HEAD(&p
->head
);
231 list_add(&p
->head
, &pde
->pulses
);
234 pulse_queue_check_window(pde
);
235 if (pde
->count
>= pde
->max_count
)
236 pulse_queue_dequeue(pde
);
240 static bool pseq_handler_create_sequences(struct pri_detector
*pde
,
241 u64 ts
, u32 min_count
)
243 struct pulse_elem
*p
;
244 list_for_each_entry(p
, &pde
->pulses
, head
) {
245 struct pri_sequence ps
, *new_ps
;
246 struct pulse_elem
*p2
;
249 u32 delta_ts
= ts
- p
->ts
;
251 if (delta_ts
< pde
->rs
->pri_min
)
252 /* ignore too small pri */
255 if (delta_ts
> pde
->rs
->pri_max
)
256 /* stop on too large pri (sorted list) */
259 /* build a new sequence with new potential pri */
265 ps
.dur
= ps
.pri
* (pde
->rs
->ppb
- 1)
266 + 2 * pde
->rs
->max_pri_tolerance
;
270 min_valid_ts
= ts
- ps
.dur
;
271 /* check which past pulses are candidates for new sequence */
272 list_for_each_entry_continue(p2
, &pde
->pulses
, head
) {
274 if (p2
->ts
< min_valid_ts
)
275 /* stop on crossing window border */
277 /* check if pulse match (multi)PRI */
278 factor
= pde_get_multiple(ps
.last_ts
- p2
->ts
, ps
.pri
,
279 pde
->rs
->max_pri_tolerance
);
282 ps
.first_ts
= p2
->ts
;
284 * on match, add the intermediate falses
287 ps
.count_falses
+= tmp_false_count
;
290 /* this is a potential false one */
294 if (ps
.count
< min_count
)
295 /* did not reach minimum count, drop sequence */
298 /* this is a valid one, add it */
299 ps
.deadline_ts
= ps
.first_ts
+ ps
.dur
;
300 new_ps
= pool_get_pseq_elem();
301 if (new_ps
== NULL
) {
302 new_ps
= kmalloc(sizeof(*new_ps
), GFP_KERNEL
);
303 if (new_ps
== NULL
) {
304 DFS_POOL_STAT_INC(pseq_alloc_error
);
307 DFS_POOL_STAT_INC(pseq_allocated
);
308 DFS_POOL_STAT_INC(pseq_used
);
310 memcpy(new_ps
, &ps
, sizeof(ps
));
311 INIT_LIST_HEAD(&new_ps
->head
);
312 list_add(&new_ps
->head
, &pde
->sequences
);
317 /* check new ts and add to all matching existing sequences */
319 pseq_handler_add_to_existing_seqs(struct pri_detector
*pde
, u64 ts
)
322 struct pri_sequence
*ps
, *ps2
;
323 list_for_each_entry_safe(ps
, ps2
, &pde
->sequences
, head
) {
327 /* first ensure that sequence is within window */
328 if (ts
> ps
->deadline_ts
) {
329 list_del_init(&ps
->head
);
330 pool_put_pseq_elem(ps
);
334 delta_ts
= ts
- ps
->last_ts
;
335 factor
= pde_get_multiple(delta_ts
, ps
->pri
,
336 pde
->rs
->max_pri_tolerance
);
341 if (max_count
< ps
->count
)
342 max_count
= ps
->count
;
350 static struct pri_sequence
*
351 pseq_handler_check_detection(struct pri_detector
*pde
)
353 struct pri_sequence
*ps
;
355 if (list_empty(&pde
->sequences
))
358 list_for_each_entry(ps
, &pde
->sequences
, head
) {
360 * we assume to have enough matching confidence if we
361 * 1) have enough pulses
362 * 2) have more matching than false pulses
364 if ((ps
->count
>= pde
->rs
->ppb_thresh
) &&
365 (ps
->count
* pde
->rs
->num_pri
>= ps
->count_falses
))
372 /* free pulse queue and sequences list and give objects back to pools */
373 static void pri_detector_reset(struct pri_detector
*pde
, u64 ts
)
375 struct pri_sequence
*ps
, *ps0
;
376 struct pulse_elem
*p
, *p0
;
377 list_for_each_entry_safe(ps
, ps0
, &pde
->sequences
, head
) {
378 list_del_init(&ps
->head
);
379 pool_put_pseq_elem(ps
);
381 list_for_each_entry_safe(p
, p0
, &pde
->pulses
, head
) {
382 list_del_init(&p
->head
);
383 pool_put_pulse_elem(p
);
389 static void pri_detector_exit(struct pri_detector
*de
)
391 pri_detector_reset(de
, 0);
392 pool_deregister_ref();
396 static bool pri_detector_add_pulse(struct pri_detector
*de
,
397 struct pulse_event
*event
)
400 struct pri_sequence
*ps
;
402 const struct radar_detector_specs
*rs
= de
->rs
;
404 /* ignore pulses not within width range */
405 if ((rs
->width_min
> event
->width
) || (rs
->width_max
< event
->width
))
408 if ((ts
- de
->last_ts
) < rs
->max_pri_tolerance
)
409 /* if delta to last pulse is too short, don't use this pulse */
413 max_updated_seq
= pseq_handler_add_to_existing_seqs(de
, ts
);
415 if (!pseq_handler_create_sequences(de
, ts
, max_updated_seq
)) {
416 pr_err("failed to create pulse sequences\n");
417 pri_detector_reset(de
, ts
);
421 ps
= pseq_handler_check_detection(de
);
424 pr_info("DFS: radar found: pri=%d, count=%d, count_false=%d\n",
425 ps
->pri
, ps
->count
, ps
->count_falses
);
426 pri_detector_reset(de
, ts
);
429 pulse_queue_enqueue(de
, ts
);
433 struct pri_detector
*
434 pri_detector_init(const struct radar_detector_specs
*rs
)
436 struct pri_detector
*de
;
437 de
= kzalloc(sizeof(*de
), GFP_KERNEL
);
440 de
->exit
= pri_detector_exit
;
441 de
->add_pulse
= pri_detector_add_pulse
;
442 de
->reset
= pri_detector_reset
;
444 INIT_LIST_HEAD(&de
->sequences
);
445 INIT_LIST_HEAD(&de
->pulses
);
446 de
->window_size
= rs
->pri_max
* rs
->ppb
* rs
->num_pri
;
447 de
->max_count
= rs
->ppb
* 2;