Path: blob/main/sys/contrib/dev/athk/dfs_pri_detector.c
48254 views
/*1* Copyright (c) 2012 Neratec Solutions AG2*3* Permission to use, copy, modify, and/or distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES8* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF9* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR10* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES11* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN12* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF13* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.14*/1516#include <linux/slab.h>17#include <linux/spinlock.h>1819#include "ath.h"20#include "dfs_pattern_detector.h"21#include "dfs_pri_detector.h"2223struct ath_dfs_pool_stats global_dfs_pool_stats = {};2425#define DFS_POOL_STAT_INC(c) (global_dfs_pool_stats.c++)26#define DFS_POOL_STAT_DEC(c) (global_dfs_pool_stats.c--)27#define GET_PRI_TO_USE(MIN, MAX, RUNTIME) \28(MIN + PRI_TOLERANCE == MAX - PRI_TOLERANCE ? \29MIN + PRI_TOLERANCE : RUNTIME)3031/*32* struct pulse_elem - elements in pulse queue33*/34struct pulse_elem {35struct list_head head;36u64 ts;37};3839/*40* pde_get_multiple() - get number of multiples considering a given tolerance41* Return value: factor if abs(val - factor*fraction) <= tolerance, 0 otherwise42*/43static u32 pde_get_multiple(u32 val, u32 fraction, u32 tolerance)44{45u32 remainder;46u32 factor;47u32 delta;4849if (fraction == 0)50return 0;5152delta = (val < fraction) ? (fraction - val) : (val - fraction);5354if (delta <= tolerance)55/* val and fraction are within tolerance */56return 1;5758factor = val / fraction;59remainder = val % fraction;60if (remainder > tolerance) {61/* no exact match */62if ((fraction - remainder) <= tolerance)63/* remainder is within tolerance */64factor++;65else66factor = 0;67}68return factor;69}7071/*72* DOC: Singleton Pulse and Sequence Pools73*74* Instances of pri_sequence and pulse_elem are kept in singleton pools to75* reduce the number of dynamic allocations. They are shared between all76* instances and grow up to the peak number of simultaneously used objects.77*78* Memory is freed after all references to the pools are released.79*/80static u32 singleton_pool_references;81static LIST_HEAD(pulse_pool);82static LIST_HEAD(pseq_pool);83static DEFINE_SPINLOCK(pool_lock);8485static void pool_register_ref(void)86{87spin_lock_bh(&pool_lock);88singleton_pool_references++;89DFS_POOL_STAT_INC(pool_reference);90spin_unlock_bh(&pool_lock);91}9293static void pool_deregister_ref(void)94{95spin_lock_bh(&pool_lock);96singleton_pool_references--;97DFS_POOL_STAT_DEC(pool_reference);98if (singleton_pool_references == 0) {99/* free singleton pools with no references left */100struct pri_sequence *ps, *ps0;101struct pulse_elem *p, *p0;102103list_for_each_entry_safe(p, p0, &pulse_pool, head) {104list_del(&p->head);105DFS_POOL_STAT_DEC(pulse_allocated);106kfree(p);107}108list_for_each_entry_safe(ps, ps0, &pseq_pool, head) {109list_del(&ps->head);110DFS_POOL_STAT_DEC(pseq_allocated);111kfree(ps);112}113}114spin_unlock_bh(&pool_lock);115}116117static void pool_put_pulse_elem(struct pulse_elem *pe)118{119spin_lock_bh(&pool_lock);120list_add(&pe->head, &pulse_pool);121DFS_POOL_STAT_DEC(pulse_used);122spin_unlock_bh(&pool_lock);123}124125static void pool_put_pseq_elem(struct pri_sequence *pse)126{127spin_lock_bh(&pool_lock);128list_add(&pse->head, &pseq_pool);129DFS_POOL_STAT_DEC(pseq_used);130spin_unlock_bh(&pool_lock);131}132133static struct pri_sequence *pool_get_pseq_elem(void)134{135struct pri_sequence *pse = NULL;136spin_lock_bh(&pool_lock);137if (!list_empty(&pseq_pool)) {138pse = list_first_entry(&pseq_pool, struct pri_sequence, head);139list_del(&pse->head);140DFS_POOL_STAT_INC(pseq_used);141}142spin_unlock_bh(&pool_lock);143return pse;144}145146static struct pulse_elem *pool_get_pulse_elem(void)147{148struct pulse_elem *pe = NULL;149spin_lock_bh(&pool_lock);150if (!list_empty(&pulse_pool)) {151pe = list_first_entry(&pulse_pool, struct pulse_elem, head);152list_del(&pe->head);153DFS_POOL_STAT_INC(pulse_used);154}155spin_unlock_bh(&pool_lock);156return pe;157}158159static struct pulse_elem *pulse_queue_get_tail(struct pri_detector *pde)160{161struct list_head *l = &pde->pulses;162if (list_empty(l))163return NULL;164return list_entry(l->prev, struct pulse_elem, head);165}166167static bool pulse_queue_dequeue(struct pri_detector *pde)168{169struct pulse_elem *p = pulse_queue_get_tail(pde);170if (p != NULL) {171list_del_init(&p->head);172pde->count--;173/* give it back to pool */174pool_put_pulse_elem(p);175}176return (pde->count > 0);177}178179/* remove pulses older than window */180static void pulse_queue_check_window(struct pri_detector *pde)181{182u64 min_valid_ts;183struct pulse_elem *p;184185/* there is no delta time with less than 2 pulses */186if (pde->count < 2)187return;188189if (pde->last_ts <= pde->window_size)190return;191192min_valid_ts = pde->last_ts - pde->window_size;193while ((p = pulse_queue_get_tail(pde)) != NULL) {194if (p->ts >= min_valid_ts)195return;196pulse_queue_dequeue(pde);197}198}199200static bool pulse_queue_enqueue(struct pri_detector *pde, u64 ts)201{202struct pulse_elem *p = pool_get_pulse_elem();203if (p == NULL) {204p = kmalloc(sizeof(*p), GFP_ATOMIC);205if (p == NULL) {206DFS_POOL_STAT_INC(pulse_alloc_error);207return false;208}209DFS_POOL_STAT_INC(pulse_allocated);210DFS_POOL_STAT_INC(pulse_used);211}212INIT_LIST_HEAD(&p->head);213p->ts = ts;214list_add(&p->head, &pde->pulses);215pde->count++;216pde->last_ts = ts;217pulse_queue_check_window(pde);218if (pde->count >= pde->max_count)219pulse_queue_dequeue(pde);220return true;221}222223static bool pseq_handler_create_sequences(struct pri_detector *pde,224u64 ts, u32 min_count)225{226struct pulse_elem *p;227list_for_each_entry(p, &pde->pulses, head) {228struct pri_sequence ps, *new_ps;229struct pulse_elem *p2;230u32 tmp_false_count;231u64 min_valid_ts;232u32 delta_ts = ts - p->ts;233234if (delta_ts < pde->rs->pri_min)235/* ignore too small pri */236continue;237238if (delta_ts > pde->rs->pri_max)239/* stop on too large pri (sorted list) */240break;241242/* build a new sequence with new potential pri */243ps.count = 2;244ps.count_falses = 0;245ps.first_ts = p->ts;246ps.last_ts = ts;247ps.pri = GET_PRI_TO_USE(pde->rs->pri_min,248pde->rs->pri_max, ts - p->ts);249ps.dur = ps.pri * (pde->rs->ppb - 1)250+ 2 * pde->rs->max_pri_tolerance;251252p2 = p;253tmp_false_count = 0;254min_valid_ts = ts - ps.dur;255/* check which past pulses are candidates for new sequence */256list_for_each_entry_continue(p2, &pde->pulses, head) {257u32 factor;258if (p2->ts < min_valid_ts)259/* stop on crossing window border */260break;261/* check if pulse match (multi)PRI */262factor = pde_get_multiple(ps.last_ts - p2->ts, ps.pri,263pde->rs->max_pri_tolerance);264if (factor > 0) {265ps.count++;266ps.first_ts = p2->ts;267/*268* on match, add the intermediate falses269* and reset counter270*/271ps.count_falses += tmp_false_count;272tmp_false_count = 0;273} else {274/* this is a potential false one */275tmp_false_count++;276}277}278if (ps.count <= min_count)279/* did not reach minimum count, drop sequence */280continue;281282/* this is a valid one, add it */283ps.deadline_ts = ps.first_ts + ps.dur;284new_ps = pool_get_pseq_elem();285if (new_ps == NULL) {286new_ps = kmalloc(sizeof(*new_ps), GFP_ATOMIC);287if (new_ps == NULL) {288DFS_POOL_STAT_INC(pseq_alloc_error);289return false;290}291DFS_POOL_STAT_INC(pseq_allocated);292DFS_POOL_STAT_INC(pseq_used);293}294memcpy(new_ps, &ps, sizeof(ps));295INIT_LIST_HEAD(&new_ps->head);296list_add(&new_ps->head, &pde->sequences);297}298return true;299}300301/* check new ts and add to all matching existing sequences */302static u32303pseq_handler_add_to_existing_seqs(struct pri_detector *pde, u64 ts)304{305u32 max_count = 0;306struct pri_sequence *ps, *ps2;307list_for_each_entry_safe(ps, ps2, &pde->sequences, head) {308u32 delta_ts;309u32 factor;310311/* first ensure that sequence is within window */312if (ts > ps->deadline_ts) {313list_del_init(&ps->head);314pool_put_pseq_elem(ps);315continue;316}317318delta_ts = ts - ps->last_ts;319factor = pde_get_multiple(delta_ts, ps->pri,320pde->rs->max_pri_tolerance);321if (factor > 0) {322ps->last_ts = ts;323ps->count++;324325if (max_count < ps->count)326max_count = ps->count;327} else {328ps->count_falses++;329}330}331return max_count;332}333334static struct pri_sequence *335pseq_handler_check_detection(struct pri_detector *pde)336{337struct pri_sequence *ps;338339if (list_empty(&pde->sequences))340return NULL;341342list_for_each_entry(ps, &pde->sequences, head) {343/*344* we assume to have enough matching confidence if we345* 1) have enough pulses346* 2) have more matching than false pulses347*/348if ((ps->count >= pde->rs->ppb_thresh) &&349(ps->count * pde->rs->num_pri >= ps->count_falses))350return ps;351}352return NULL;353}354355356/* free pulse queue and sequences list and give objects back to pools */357static void pri_detector_reset(struct pri_detector *pde, u64 ts)358{359struct pri_sequence *ps, *ps0;360struct pulse_elem *p, *p0;361list_for_each_entry_safe(ps, ps0, &pde->sequences, head) {362list_del_init(&ps->head);363pool_put_pseq_elem(ps);364}365list_for_each_entry_safe(p, p0, &pde->pulses, head) {366list_del_init(&p->head);367pool_put_pulse_elem(p);368}369pde->count = 0;370pde->last_ts = ts;371}372373static void pri_detector_exit(struct pri_detector *de)374{375pri_detector_reset(de, 0);376pool_deregister_ref();377kfree(de);378}379380static struct pri_sequence *pri_detector_add_pulse(struct pri_detector *de,381struct pulse_event *event)382{383u32 max_updated_seq;384struct pri_sequence *ps;385u64 ts = event->ts;386const struct radar_detector_specs *rs = de->rs;387388/* ignore pulses not within width range */389if ((rs->width_min > event->width) || (rs->width_max < event->width))390return NULL;391392if ((ts - de->last_ts) < rs->max_pri_tolerance)393/* if delta to last pulse is too short, don't use this pulse */394return NULL;395/* radar detector spec needs chirp, but not detected */396if (rs->chirp && rs->chirp != event->chirp)397return NULL;398399de->last_ts = ts;400401max_updated_seq = pseq_handler_add_to_existing_seqs(de, ts);402403if (!pseq_handler_create_sequences(de, ts, max_updated_seq)) {404pri_detector_reset(de, ts);405return NULL;406}407408ps = pseq_handler_check_detection(de);409410if (ps == NULL)411pulse_queue_enqueue(de, ts);412413return ps;414}415416struct pri_detector *pri_detector_init(const struct radar_detector_specs *rs)417{418struct pri_detector *de;419420de = kzalloc(sizeof(*de), GFP_ATOMIC);421if (de == NULL)422return NULL;423de->exit = pri_detector_exit;424de->add_pulse = pri_detector_add_pulse;425de->reset = pri_detector_reset;426427INIT_LIST_HEAD(&de->sequences);428INIT_LIST_HEAD(&de->pulses);429de->window_size = rs->pri_max * rs->ppb * rs->num_pri;430de->max_count = rs->ppb * 2;431de->rs = rs;432433pool_register_ref();434return de;435}436437438