-
Notifications
You must be signed in to change notification settings - Fork 9
/
ecrnx_radar.c
executable file
·1639 lines (1435 loc) · 50.8 KB
/
ecrnx_radar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
*
* @file ecrnx_radar.c
*
* @brief Functions to handle radar detection
* Radar detection is copied (and adapted) from ath driver source code.
*
* Copyright (c) 2012 Neratec Solutions AG
* Copyright (C) ESWIN 2015-2020
*
******************************************************************************
*/
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/ktime.h>
#include <net/mac80211.h>
#include "ecrnx_radar.h"
#include "ecrnx_defs.h"
#include "ecrnx_msg_tx.h"
#include "ecrnx_events.h"
#include "ecrnx_compat.h"
/*
* tolerated deviation of radar time stamp in usecs on both sides
* TODO: this might need to be HW-dependent
*/
#define PRI_TOLERANCE 16
/**
* struct radar_types - contains array of patterns defined for one DFS domain
* @domain: DFS regulatory domain
* @num_radar_types: number of radar types to follow
* @radar_types: radar types array
*/
struct radar_types {
enum nl80211_dfs_regions region;
u32 num_radar_types;
const struct radar_detector_specs *spec_riu;
const struct radar_detector_specs *spec_fcu;
};
/**
* Type of radar waveform:
* RADAR_WAVEFORM_SHORT : waveform defined by
* - pulse width
* - pulse interval in a burst (pri)
* - number of pulses in a burst (ppb)
*
* RADAR_WAVEFORM_WEATHER :
* same than SHORT except that ppb is dependent of pri
*
* RADAR_WAVEFORM_INTERLEAVED :
* same than SHORT except there are several value of pri (interleaved)
*
* RADAR_WAVEFORM_LONG :
*
*/
enum radar_waveform_type {
RADAR_WAVEFORM_SHORT,
RADAR_WAVEFORM_WEATHER,
RADAR_WAVEFORM_INTERLEAVED,
RADAR_WAVEFORM_LONG
};
/**
* struct radar_detector_specs - detector specs for a radar pattern type
* @type_id: pattern type, as defined by regulatory
* @width_min: minimum radar pulse width in [us]
* @width_max: maximum radar pulse width in [us]
* @pri_min: minimum pulse repetition interval in [us] (including tolerance)
* @pri_max: minimum pri in [us] (including tolerance)
* @num_pri: maximum number of different pri for this type
* @ppb: pulses per bursts for this type
* @ppb_thresh: number of pulses required to trigger detection
* @max_pri_tolerance: pulse time stamp tolerance on both sides [us]
* @type: Type of radar waveform
*/
struct radar_detector_specs {
u8 type_id;
u8 width_min;
u8 width_max;
u16 pri_min;
u16 pri_max;
u8 num_pri;
u8 ppb;
u8 ppb_thresh;
u8 max_pri_tolerance;
enum radar_waveform_type type;
};
/* percentage on ppb threshold to trigger detection */
#define MIN_PPB_THRESH 50
#define PPB_THRESH(PPB) ((PPB * MIN_PPB_THRESH + 50) / 100)
#define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
/* width tolerance */
#define WIDTH_TOLERANCE 2
#define WIDTH_LOWER(X) (X)
#define WIDTH_UPPER(X) (X)
#define ETSI_PATTERN_SHORT(ID, WMIN, WMAX, PMIN, PMAX, PPB) \
{ \
ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
(PRF2PRI(PMAX) - PRI_TOLERANCE), \
(PRF2PRI(PMIN) + PRI_TOLERANCE), 1, PPB, \
PPB_THRESH(PPB), PRI_TOLERANCE, RADAR_WAVEFORM_SHORT \
}
#define ETSI_PATTERN_INTERLEAVED(ID, WMIN, WMAX, PMIN, PMAX, PRFMIN, PRFMAX, PPB) \
{ \
ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
(PRF2PRI(PMAX) * PRFMIN- PRI_TOLERANCE), \
(PRF2PRI(PMIN) * PRFMAX + PRI_TOLERANCE), \
PRFMAX, PPB * PRFMAX, \
PPB_THRESH(PPB), PRI_TOLERANCE, RADAR_WAVEFORM_INTERLEAVED \
}
/* radar types as defined by ETSI EN-301-893 v1.7.1 */
static const struct radar_detector_specs etsi_radar_ref_types_v17_riu[] = {
ETSI_PATTERN_SHORT(0, 0, 8, 700, 700, 18),
ETSI_PATTERN_SHORT(1, 0, 10, 200, 1000, 10),
ETSI_PATTERN_SHORT(2, 0, 22, 200, 1600, 15),
ETSI_PATTERN_SHORT(3, 0, 22, 2300, 4000, 25),
ETSI_PATTERN_SHORT(4, 20, 38, 2000, 4000, 20),
ETSI_PATTERN_INTERLEAVED(5, 0, 8, 300, 400, 2, 3, 10),
ETSI_PATTERN_INTERLEAVED(6, 0, 8, 400, 1200, 2, 3, 15),
};
static const struct radar_detector_specs etsi_radar_ref_types_v17_fcu[] = {
ETSI_PATTERN_SHORT(0, 0, 8, 700, 700, 18),
ETSI_PATTERN_SHORT(1, 0, 8, 200, 1000, 10),
ETSI_PATTERN_SHORT(2, 0, 16, 200, 1600, 15),
ETSI_PATTERN_SHORT(3, 0, 16, 2300, 4000, 25),
ETSI_PATTERN_SHORT(4, 20, 34, 2000, 4000, 20),
ETSI_PATTERN_INTERLEAVED(5, 0, 8, 300, 400, 2, 3, 10),
ETSI_PATTERN_INTERLEAVED(6, 0, 8, 400, 1200, 2, 3, 15),
};
static const struct radar_types etsi_radar_types_v17 = {
.region = NL80211_DFS_ETSI,
.num_radar_types = ARRAY_SIZE(etsi_radar_ref_types_v17_riu),
.spec_riu = etsi_radar_ref_types_v17_riu,
.spec_fcu = etsi_radar_ref_types_v17_fcu,
};
#define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, TYPE) \
{ \
ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
PMIN - PRI_TOLERANCE, \
PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF, \
PPB_THRESH(PPB), PRI_TOLERANCE, TYPE \
}
static const struct radar_detector_specs fcc_radar_ref_types_riu[] = {
FCC_PATTERN(0, 0, 8, 1428, 1428, 1, 18, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(1, 0, 8, 518, 3066, 1, 102, RADAR_WAVEFORM_WEATHER),
FCC_PATTERN(2, 0, 8, 150, 230, 1, 23, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(3, 6, 20, 200, 500, 1, 16, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(4, 10, 28, 200, 500, 1, 12, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(5, 50, 110, 1000, 2000, 1, 8, RADAR_WAVEFORM_LONG),
FCC_PATTERN(6, 0, 8, 333, 333, 1, 9, RADAR_WAVEFORM_SHORT),
};
static const struct radar_detector_specs fcc_radar_ref_types_fcu[] = {
FCC_PATTERN(0, 0, 8, 1428, 1428, 1, 18, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(1, 0, 8, 518, 3066, 1, 102, RADAR_WAVEFORM_WEATHER),
FCC_PATTERN(2, 0, 8, 150, 230, 1, 23, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(3, 6, 12, 200, 500, 1, 16, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(4, 10, 22, 200, 500, 1, 12, RADAR_WAVEFORM_SHORT),
FCC_PATTERN(5, 50, 104, 1000, 2000, 1, 8, RADAR_WAVEFORM_LONG),
FCC_PATTERN(6, 0, 8, 333, 333, 1, 9, RADAR_WAVEFORM_SHORT),
};
static const struct radar_types fcc_radar_types = {
.region = NL80211_DFS_FCC,
.num_radar_types = ARRAY_SIZE(fcc_radar_ref_types_riu),
.spec_riu = fcc_radar_ref_types_riu,
.spec_fcu = fcc_radar_ref_types_fcu,
};
#define JP_PATTERN FCC_PATTERN
static const struct radar_detector_specs jp_radar_ref_types_riu[] = {
JP_PATTERN(0, 0, 8, 1428, 1428, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(1, 2, 8, 3846, 3846, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(2, 0, 8, 1388, 1388, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(3, 0, 8, 4000, 4000, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(4, 0, 8, 150, 230, 1, 23, RADAR_WAVEFORM_SHORT),
JP_PATTERN(5, 6, 20, 200, 500, 1, 16, RADAR_WAVEFORM_SHORT),
JP_PATTERN(6, 10, 28, 200, 500, 1, 12, RADAR_WAVEFORM_SHORT),
JP_PATTERN(7, 50, 110, 1000, 2000, 1, 8, RADAR_WAVEFORM_LONG),
JP_PATTERN(8, 0, 8, 333, 333, 1, 9, RADAR_WAVEFORM_SHORT),
};
static const struct radar_detector_specs jp_radar_ref_types_fcu[] = {
JP_PATTERN(0, 0, 8, 1428, 1428, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(1, 2, 6, 3846, 3846, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(2, 0, 8, 1388, 1388, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(3, 2, 2, 4000, 4000, 1, 18, RADAR_WAVEFORM_SHORT),
JP_PATTERN(4, 0, 8, 150, 230, 1, 23, RADAR_WAVEFORM_SHORT),
JP_PATTERN(5, 6, 12, 200, 500, 1, 16, RADAR_WAVEFORM_SHORT),
JP_PATTERN(6, 10, 22, 200, 500, 1, 12, RADAR_WAVEFORM_SHORT),
JP_PATTERN(7, 50, 104, 1000, 2000, 1, 8, RADAR_WAVEFORM_LONG),
JP_PATTERN(8, 0, 8, 333, 333, 1, 9, RADAR_WAVEFORM_SHORT),
};
static const struct radar_types jp_radar_types = {
.region = NL80211_DFS_JP,
.num_radar_types = ARRAY_SIZE(jp_radar_ref_types_riu),
.spec_riu = jp_radar_ref_types_riu,
.spec_fcu = jp_radar_ref_types_fcu,
};
static const struct radar_types *dfs_domains[] = {
&etsi_radar_types_v17,
&fcc_radar_types,
&jp_radar_types,
};
/**
* struct pri_sequence - sequence of pulses matching one PRI
* @head: list_head
* @pri: pulse repetition interval (PRI) in usecs
* @dur: duration of sequence in usecs
* @count: number of pulses in this sequence
* @count_falses: number of not matching pulses in this sequence
* @first_ts: time stamp of first pulse in usecs
* @last_ts: time stamp of last pulse in usecs
* @deadline_ts: deadline when this sequence becomes invalid (first_ts + dur)
* @ppb_thresh: Number of pulses to validate detection
* (need for weather radar whose value depends of pri)
*/
struct pri_sequence {
struct list_head head;
u32 pri;
u32 dur;
u32 count;
u32 count_falses;
u64 first_ts;
u64 last_ts;
u64 deadline_ts;
u8 ppb_thresh;
};
/**
* struct pulse_elem - elements in pulse queue
* @ts: time stamp in usecs
*/
struct pulse_elem {
struct list_head head;
u64 ts;
};
/**
* struct pri_detector - PRI detector element for a dedicated radar type
* @head:
* @rs: detector specs for this detector element
* @last_ts: last pulse time stamp considered for this element in usecs
* @sequences: list_head holding potential pulse sequences
* @pulses: list connecting pulse_elem objects
* @count: number of pulses in queue
* @max_count: maximum number of pulses to be queued
* @window_size: window size back from newest pulse time stamp in usecs
* @freq:
*/
struct pri_detector {
struct list_head head;
const struct radar_detector_specs *rs;
u64 last_ts;
struct list_head sequences;
struct list_head pulses;
u32 count;
u32 max_count;
u32 window_size;
struct pri_detector_ops *ops;
u16 freq;
};
/**
* struct pri_detector_ops - PRI detector ops (dependent of waveform type)
* @init : Initialize pri_detector structure
* @add_pulse : Add a pulse to the pri-detector
* @reset_on_pri_overflow : Should the pri_detector be resetted when pri overflow
*/
struct pri_detector_ops {
void (*init)(struct pri_detector *pde);
struct pri_sequence * (*add_pulse)(struct pri_detector *pde, u16 len, u64 ts, u16 pri);
int reset_on_pri_overflow;
};
/******************************************************************************
* PRI (pulse repetition interval) sequence detection
*****************************************************************************/
/**
* Singleton Pulse and Sequence Pools
*
* Instances of pri_sequence and pulse_elem are kept in singleton pools to
* reduce the number of dynamic allocations. They are shared between all
* instances and grow up to the peak number of simultaneously used objects.
*
* Memory is freed after all references to the pools are released.
*/
static u32 singleton_pool_references;
static LIST_HEAD(pulse_pool);
static LIST_HEAD(pseq_pool);
static DEFINE_SPINLOCK(pool_lock);
static void pool_register_ref(void)
{
spin_lock_bh(&pool_lock);
singleton_pool_references++;
spin_unlock_bh(&pool_lock);
}
static void pool_deregister_ref(void)
{
spin_lock_bh(&pool_lock);
singleton_pool_references--;
if (singleton_pool_references == 0) {
/* free singleton pools with no references left */
struct pri_sequence *ps, *ps0;
struct pulse_elem *p, *p0;
list_for_each_entry_safe(p, p0, &pulse_pool, head) {
list_del(&p->head);
kfree(p);
}
list_for_each_entry_safe(ps, ps0, &pseq_pool, head) {
list_del(&ps->head);
kfree(ps);
}
}
spin_unlock_bh(&pool_lock);
}
static void pool_put_pulse_elem(struct pulse_elem *pe)
{
spin_lock_bh(&pool_lock);
list_add(&pe->head, &pulse_pool);
spin_unlock_bh(&pool_lock);
}
static void pool_put_pseq_elem(struct pri_sequence *pse)
{
spin_lock_bh(&pool_lock);
list_add(&pse->head, &pseq_pool);
spin_unlock_bh(&pool_lock);
}
static struct pri_sequence *pool_get_pseq_elem(void)
{
struct pri_sequence *pse = NULL;
spin_lock_bh(&pool_lock);
if (!list_empty(&pseq_pool)) {
pse = list_first_entry(&pseq_pool, struct pri_sequence, head);
list_del(&pse->head);
}
spin_unlock_bh(&pool_lock);
if (pse == NULL) {
pse = kmalloc(sizeof(*pse), GFP_ATOMIC);
}
return pse;
}
static struct pulse_elem *pool_get_pulse_elem(void)
{
struct pulse_elem *pe = NULL;
spin_lock_bh(&pool_lock);
if (!list_empty(&pulse_pool)) {
pe = list_first_entry(&pulse_pool, struct pulse_elem, head);
list_del(&pe->head);
}
spin_unlock_bh(&pool_lock);
return pe;
}
static struct pulse_elem *pulse_queue_get_tail(struct pri_detector *pde)
{
struct list_head *l = &pde->pulses;
if (list_empty(l))
return NULL;
return list_entry(l->prev, struct pulse_elem, head);
}
static bool pulse_queue_dequeue(struct pri_detector *pde)
{
struct pulse_elem *p = pulse_queue_get_tail(pde);
if (p != NULL) {
list_del_init(&p->head);
pde->count--;
/* give it back to pool */
pool_put_pulse_elem(p);
}
return (pde->count > 0);
}
/**
* pulse_queue_check_window - remove pulses older than window
* @pde: pointer on pri_detector
*
* dequeue pulse that are too old.
*/
static
void pulse_queue_check_window(struct pri_detector *pde)
{
u64 min_valid_ts;
struct pulse_elem *p;
/* there is no delta time with less than 2 pulses */
if (pde->count < 2)
return;
if (pde->last_ts <= pde->window_size)
return;
min_valid_ts = pde->last_ts - pde->window_size;
while ((p = pulse_queue_get_tail(pde)) != NULL) {
if (p->ts >= min_valid_ts)
return;
pulse_queue_dequeue(pde);
}
}
/**
* pulse_queue_enqueue - Queue one pulse
* @pde: pointer on pri_detector
*
* Add one pulse to the list. If the maximum number of pulses
* if reached, remove oldest one.
*/
static
bool pulse_queue_enqueue(struct pri_detector *pde, u64 ts)
{
struct pulse_elem *p = pool_get_pulse_elem();
if (p == NULL) {
p = kmalloc(sizeof(*p), GFP_ATOMIC);
if (p == NULL) {
return false;
}
}
INIT_LIST_HEAD(&p->head);
p->ts = ts;
list_add(&p->head, &pde->pulses);
pde->count++;
pde->last_ts = ts;
pulse_queue_check_window(pde);
if (pde->count >= pde->max_count)
pulse_queue_dequeue(pde);
return true;
}
/***************************************************************************
* Short waveform
**************************************************************************/
/**
* pde_get_multiple() - get number of multiples considering a given tolerance
* @return factor if abs(val - factor*fraction) <= tolerance, 0 otherwise
*/
static
u32 pde_get_multiple(u32 val, u32 fraction, u32 tolerance)
{
u32 remainder;
u32 factor;
u32 delta;
if (fraction == 0)
return 0;
delta = (val < fraction) ? (fraction - val) : (val - fraction);
if (delta <= tolerance)
/* val and fraction are within tolerance */
return 1;
factor = val / fraction;
remainder = val % fraction;
if (remainder > tolerance) {
/* no exact match */
if ((fraction - remainder) <= tolerance)
/* remainder is within tolerance */
factor++;
else
factor = 0;
}
return factor;
}
/**
* pde_short_create_sequences - create_sequences function for
* SHORT/WEATHER/INTERLEAVED radar waveform
* @pde: pointer on pri_detector
* @ts: timestamp of the pulse
* @min_count: Minimum number of pulse to be present in the sequence.
* (With this pulse there is already a sequence with @min_count
* pulse, so if we can't create a sequence with more pulse don't
* create it)
* @return: false if an error occured (memory allocation) true otherwise
*
* For each pulses queued check if we can create a sequence with
* pri = (ts - pulse_queued.ts) which contains more than @min_count pulses.
*
*/
static
bool pde_short_create_sequences(struct pri_detector *pde,
u64 ts, u32 min_count)
{
struct pulse_elem *p;
u16 pulse_idx = 0;
list_for_each_entry(p, &pde->pulses, head) {
struct pri_sequence ps, *new_ps;
struct pulse_elem *p2;
u32 tmp_false_count;
u64 min_valid_ts;
u32 delta_ts = ts - p->ts;
pulse_idx++;
if (delta_ts < pde->rs->pri_min)
/* ignore too small pri */
continue;
if (delta_ts > pde->rs->pri_max)
/* stop on too large pri (sorted list) */
break;
/* build a new sequence with new potential pri */
ps.count = 2;
ps.count_falses = pulse_idx - 1;
ps.first_ts = p->ts;
ps.last_ts = ts;
ps.pri = ts - p->ts;
ps.dur = ps.pri * (pde->rs->ppb - 1)
+ 2 * pde->rs->max_pri_tolerance;
p2 = p;
tmp_false_count = 0;
if (ps.dur > ts)
min_valid_ts = 0;
else
min_valid_ts = ts - ps.dur;
/* check which past pulses are candidates for new sequence */
list_for_each_entry_continue(p2, &pde->pulses, head) {
u32 factor;
if (p2->ts < min_valid_ts)
/* stop on crossing window border */
break;
/* check if pulse match (multi)PRI */
factor = pde_get_multiple(ps.last_ts - p2->ts, ps.pri,
pde->rs->max_pri_tolerance);
if (factor > 0) {
ps.count++;
ps.first_ts = p2->ts;
/*
* on match, add the intermediate falses
* and reset counter
*/
ps.count_falses += tmp_false_count;
tmp_false_count = 0;
} else {
/* this is a potential false one */
tmp_false_count++;
}
}
if (ps.count <= min_count) {
/* did not reach minimum count, drop sequence */
continue;
}
/* this is a valid one, add it */
ps.deadline_ts = ps.first_ts + ps.dur;
if (pde->rs->type == RADAR_WAVEFORM_WEATHER) {
ps.ppb_thresh = 19000000 / (360 * ps.pri);
ps.ppb_thresh = PPB_THRESH(ps.ppb_thresh);
} else {
ps.ppb_thresh = pde->rs->ppb_thresh;
}
new_ps = pool_get_pseq_elem();
if (new_ps == NULL) {
return false;
}
memcpy(new_ps, &ps, sizeof(ps));
INIT_LIST_HEAD(&new_ps->head);
list_add(&new_ps->head, &pde->sequences);
}
return true;
}
/**
* pde_short_add_to_existing_seqs - add_to_existing_seqs function for
* SHORT/WEATHER/INTERLEAVED radar waveform
* @pde: pointer on pri_detector
* @ts: timestamp of the pulse
*
* Check all sequemces created for this pde.
* - If the sequence is too old delete it.
* - Else if the delta with the previous pulse match the pri of the sequence
* add the pulse to this sequence. If the pulse cannot be added it is added
* to the false pulses for this sequence
*
* @return the length of the longest sequence in which the pulse has been added
*/
static
u32 pde_short_add_to_existing_seqs(struct pri_detector *pde, u64 ts)
{
u32 max_count = 0;
struct pri_sequence *ps, *ps2;
list_for_each_entry_safe(ps, ps2, &pde->sequences, head) {
u32 delta_ts;
u32 factor;
/* first ensure that sequence is within window */
if (ts > ps->deadline_ts) {
list_del_init(&ps->head);
pool_put_pseq_elem(ps);
continue;
}
delta_ts = ts - ps->last_ts;
factor = pde_get_multiple(delta_ts, ps->pri,
pde->rs->max_pri_tolerance);
if (factor > 0) {
ps->last_ts = ts;
ps->count++;
if (max_count < ps->count)
max_count = ps->count;
} else {
ps->count_falses++;
}
}
return max_count;
}
/**
* pde_short_check_detection - check_detection function for
* SHORT/WEATHER/INTERLEAVED radar waveform
* @pde: pointer on pri_detector
*
* Check all sequemces created for this pde.
* - If a sequence contains more pulses than the threshold and more matching
* that false pulses.
*
* @return The first complete sequence, and NULL if no sequence is complete.
*/
static
struct pri_sequence * pde_short_check_detection(struct pri_detector *pde)
{
struct pri_sequence *ps;
if (list_empty(&pde->sequences))
return NULL;
list_for_each_entry(ps, &pde->sequences, head) {
/*
* we assume to have enough matching confidence if we
* 1) have enough pulses
* 2) have more matching than false pulses
*/
if ((ps->count >= ps->ppb_thresh) &&
(ps->count * pde->rs->num_pri > ps->count_falses)) {
return ps;
}
}
return NULL;
}
/**
* pde_short_init - init function for
* SHORT/WEATHER/INTERLEAVED radar waveform
* @pde: pointer on pri_detector
*
* Initialize pri_detector window size to the maximun size of one burst
* for the radar specification associated.
*/
static
void pde_short_init(struct pri_detector *pde)
{
pde->window_size = pde->rs->pri_max * pde->rs->ppb * pde->rs->num_pri;
pde->max_count = pde->rs->ppb * 2;
}
static void pri_detector_reset(struct pri_detector *pde, u64 ts);
/**
* pde_short_add_pulse - Add pulse to a pri_detector for
* SHORT/WEATHER/INTERLEAVED radar waveform
*
* @pde : pointer on pri_detector
* @len : width of the pulse
* @ts : timestamp of the pulse received
* @pri : Delta in us with the previous pulse.
* (0 means that delta in bigger than 65535 us)
*
* Process on pulse within this pri_detector
* - First try to add it to existing sequence
* - Then try to create a new and longest sequence
* - Check if this pulse complete a sequence
* - If not save this pulse in the list
*/
static
struct pri_sequence *pde_short_add_pulse(struct pri_detector *pde,
u16 len, u64 ts, u16 pri)
{
u32 max_updated_seq;
struct pri_sequence *ps;
const struct radar_detector_specs *rs = pde->rs;
if (pde->count == 0) {
/* This is the first pulse after reset, no need to check sequences */
pulse_queue_enqueue(pde, ts);
return NULL;
}
if ((ts - pde->last_ts) < rs->max_pri_tolerance) {
/* if delta to last pulse is too short, don't use this pulse */
return NULL;
}
max_updated_seq = pde_short_add_to_existing_seqs(pde, ts);
if (!pde_short_create_sequences(pde, ts, max_updated_seq)) {
pri_detector_reset(pde, ts);
return NULL;
}
ps = pde_short_check_detection(pde);
if (ps == NULL)
pulse_queue_enqueue(pde, ts);
return ps;
}
/**
* pri detector ops to detect short radar waveform
* A Short waveform is defined by :
* The width of pulses.
* The interval between two pulses inside a burst (called pri)
* (some waveform may have or 2/3 interleaved pri)
* The number of pulses per burst (ppb)
*/
static struct pri_detector_ops pri_detector_short = {
.init = pde_short_init,
.add_pulse = pde_short_add_pulse,
.reset_on_pri_overflow = 1,
};
/***************************************************************************
* Long waveform
**************************************************************************/
#define LONG_RADAR_DURATION 12000000
#define LONG_RADAR_BURST_MIN_DURATION (12000000 / 20)
#define LONG_RADAR_MAX_BURST 20
/**
* pde_long_init - init function for LONG radar waveform
* @pde: pointer on pri_detector
*
* Initialize pri_detector window size to the long waveform radar
* waveform (ie. 12s) and max_count
*/
static
void pde_long_init(struct pri_detector *pde)
{
pde->window_size = LONG_RADAR_DURATION;
pde->max_count = LONG_RADAR_MAX_BURST; /* only count burst not pulses */
}
/**
* pde_long_add_pulse - Add pulse to a pri_detector for
* LONG radar waveform
*
* @pde : pointer on pri_detector
* @len : width of the pulse
* @ts : timestamp of the pulse received
* @pri : Delta in us with the previous pulse.
*
*
* For long pulse we only handle one sequence. Since each burst
* have a different set of parameters (number of pulse, pri) than
* the previous one we only use pulse width to add the pulse in the
* sequence.
* We only queue one pulse per burst and valid the radar when enough burst
* has been detected.
*/
static
struct pri_sequence *pde_long_add_pulse(struct pri_detector *pde,
u16 len, u64 ts, u16 pri)
{
struct pri_sequence *ps;
const struct radar_detector_specs *rs = pde->rs;
if (list_empty(&pde->sequences)) {
/* First pulse, create a new sequence */
ps = pool_get_pseq_elem();
if (ps == NULL) {
return NULL;
}
/*For long waveform, "count" represents the number of burst detected */
ps->count = 1;
/*"count_false" represents the number of pulse in the current burst */
ps->count_falses = 1;
ps->first_ts = ts;
ps->last_ts = ts;
ps->deadline_ts = ts + pde->window_size;
ps->pri = 0;
INIT_LIST_HEAD(&ps->head);
list_add(&ps->head, &pde->sequences);
pulse_queue_enqueue(pde, ts);
} else {
u32 delta_ts;
ps = (struct pri_sequence *)pde->sequences.next;
delta_ts = ts - ps->last_ts;
ps->last_ts = ts;
if (delta_ts < rs->pri_max) {
/* ignore pulse too close from previous one */
} else if ((delta_ts >= rs->pri_min) &&
(delta_ts <= rs->pri_max)) {
/* this is a new pulse in the current burst, ignore it
(i.e don't queue it) */
ps->count_falses++;
} else if ((ps->count > 2) &&
(ps->dur + delta_ts) < LONG_RADAR_BURST_MIN_DURATION) {
/* not enough time between burst, ignore pulse */
} else {
/* a new burst */
ps->count++;
ps->count_falses = 1;
/* reset the start of the sequence if deadline reached */
if (ts > ps->deadline_ts) {
struct pulse_elem *p;
u64 min_valid_ts;
min_valid_ts = ts - pde->window_size;
while ((p = pulse_queue_get_tail(pde)) != NULL) {
if (p->ts >= min_valid_ts) {
ps->first_ts = p->ts;
ps->deadline_ts = p->ts + pde->window_size;
break;
}
pulse_queue_dequeue(pde);
ps->count--;
}
}
/* valid radar if enough burst detected and delta with first burst
is at least duration/2 */
if (ps->count > pde->rs->ppb_thresh &&
(ts - ps->first_ts) > (pde->window_size / 2)) {
return ps;
} else {
pulse_queue_enqueue(pde, ts);
ps->dur = delta_ts;
}
}
}
return NULL;
}
/**
* pri detector ops to detect long radar waveform
*/
static struct pri_detector_ops pri_detector_long = {
.init = pde_long_init,
.add_pulse = pde_long_add_pulse,
.reset_on_pri_overflow = 0,
};
/***************************************************************************
* PRI detector init/reset/exit/get
**************************************************************************/
/**
* pri_detector_init- Create a new pri_detector
*
* @dpd: dfs_pattern_detector instance pointer
* @radar_type: index of radar pattern
* @freq: Frequency of the pri detector
*/
struct pri_detector *pri_detector_init(struct dfs_pattern_detector *dpd,
u16 radar_type, u16 freq)
{
struct pri_detector *pde;
pde = kzalloc(sizeof(*pde), GFP_ATOMIC);
if (pde == NULL)
return NULL;
INIT_LIST_HEAD(&pde->sequences);
INIT_LIST_HEAD(&pde->pulses);
INIT_LIST_HEAD(&pde->head);
list_add(&pde->head, &dpd->detectors[radar_type]);
pde->rs = &dpd->radar_spec[radar_type];
pde->freq = freq;
if (pde->rs->type == RADAR_WAVEFORM_LONG) {
/* for LONG WAVEFORM */
pde->ops = &pri_detector_long;
} else {
/* for SHORT, WEATHER and INTERLEAVED */
pde->ops = &pri_detector_short;
}
/* Init dependent of specs */
pde->ops->init(pde);
pool_register_ref();
return pde;
}
/**
* pri_detector_reset - Reset pri_detector
*
* @pde: pointer on pri_detector
* @ts: New ts reference for the pri_detector
*
* free pulse queue and sequences list and give objects back to pools
*/
static
void pri_detector_reset(struct pri_detector *pde, u64 ts)
{
struct pri_sequence *ps, *ps0;
struct pulse_elem *p, *p0;
list_for_each_entry_safe(ps, ps0, &pde->sequences, head) {
list_del_init(&ps->head);
pool_put_pseq_elem(ps);
}
list_for_each_entry_safe(p, p0, &pde->pulses, head) {
list_del_init(&p->head);
pool_put_pulse_elem(p);
}
pde->count = 0;
pde->last_ts = ts;
}
/**
* pri_detector_exit - Delete pri_detector
*
* @pde: pointer on pri_detector
*/
static
void pri_detector_exit(struct pri_detector *pde)
{
pri_detector_reset(pde, 0);
pool_deregister_ref();
list_del(&pde->head);
kfree(pde);
}
/**
* pri_detector_get() - get pri detector for a given frequency and type
* @dpd: dfs_pattern_detector instance pointer
* @freq: frequency in MHz
* @radar_type: index of radar pattern
* @return pointer to pri detector on success, NULL otherwise
*
* Return existing pri detector for the given frequency or return a
* newly create one.
* Pri detector are "merged" by frequency so that if a pri detector for a freq
* of +/- 2Mhz already exists don't create a new one.
*
* Maybe will need to adapt frequency merge for pattern with chirp.
*/
static struct pri_detector *
pri_detector_get(struct dfs_pattern_detector *dpd, u16 freq, u16 radar_type)
{
struct pri_detector *pde, *cur = NULL;
list_for_each_entry(pde, &dpd->detectors[radar_type], head) {
if (pde->freq == freq) {
if (pde->count)
return pde;
else
cur = pde;
} else if (pde->freq - 2 == freq && pde->count) {
return pde;
} else if (pde->freq + 2 == freq && pde->count) {
return pde;
}
}