Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2012 Neratec Solutions AG
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #include <linux/slab.h>
0018 #include <linux/export.h>
0019 
0020 #include "dfs_pattern_detector.h"
0021 #include "dfs_pri_detector.h"
0022 #include "ath.h"
0023 
0024 /**
0025  * struct radar_types - contains array of patterns defined for one DFS domain
0026  * @region: regulatory DFS region
0027  * @num_radar_types: number of radar types to follow
0028  * @radar_types: radar types array
0029  */
0030 struct radar_types {
0031     enum nl80211_dfs_regions region;
0032     u32 num_radar_types;
0033     const struct radar_detector_specs *radar_types;
0034 };
0035 
0036 /* percentage on ppb threshold to trigger detection */
0037 #define MIN_PPB_THRESH  50
0038 #define PPB_THRESH_RATE(PPB, RATE) ((PPB * RATE + 100 - RATE) / 100)
0039 #define PPB_THRESH(PPB) PPB_THRESH_RATE(PPB, MIN_PPB_THRESH)
0040 #define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
0041 /* percentage of pulse width tolerance */
0042 #define WIDTH_TOLERANCE 5
0043 #define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
0044 #define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
0045 
0046 #define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)   \
0047 {                               \
0048     ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),       \
0049     (PRF2PRI(PMAX) - PRI_TOLERANCE),            \
0050     (PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF,  \
0051     PPB_THRESH(PPB), PRI_TOLERANCE, CHIRP           \
0052 }
0053 
0054 /* radar types as defined by ETSI EN-301-893 v1.5.1 */
0055 static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
0056     ETSI_PATTERN(0,  0,  1,  700,  700, 1, 18, false),
0057     ETSI_PATTERN(1,  0,  5,  200, 1000, 1, 10, false),
0058     ETSI_PATTERN(2,  0, 15,  200, 1600, 1, 15, false),
0059     ETSI_PATTERN(3,  0, 15, 2300, 4000, 1, 25, false),
0060     ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20, false),
0061     ETSI_PATTERN(5,  0,  2,  300,  400, 3, 10, false),
0062     ETSI_PATTERN(6,  0,  2,  400, 1200, 3, 15, false),
0063 };
0064 
0065 static const struct radar_types etsi_radar_types_v15 = {
0066     .region         = NL80211_DFS_ETSI,
0067     .num_radar_types    = ARRAY_SIZE(etsi_radar_ref_types_v15),
0068     .radar_types        = etsi_radar_ref_types_v15,
0069 };
0070 
0071 #define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)    \
0072 {                               \
0073     ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),       \
0074     PMIN - PRI_TOLERANCE,                   \
0075     PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,     \
0076     PPB_THRESH(PPB), PRI_TOLERANCE, CHIRP           \
0077 }
0078 
0079 /* radar types released on August 14, 2014
0080  * type 1 PRI values randomly selected within the range of 518 and 3066.
0081  * divide it to 3 groups is good enough for both of radar detection and
0082  * avoiding false detection based on practical test results
0083  * collected for more than a year.
0084  */
0085 static const struct radar_detector_specs fcc_radar_ref_types[] = {
0086     FCC_PATTERN(0, 0, 1, 1428, 1428, 1, 18, false),
0087     FCC_PATTERN(101, 0, 1, 518, 938, 1, 57, false),
0088     FCC_PATTERN(102, 0, 1, 938, 2000, 1, 27, false),
0089     FCC_PATTERN(103, 0, 1, 2000, 3066, 1, 18, false),
0090     FCC_PATTERN(2, 0, 5, 150, 230, 1, 23, false),
0091     FCC_PATTERN(3, 6, 10, 200, 500, 1, 16, false),
0092     FCC_PATTERN(4, 11, 20, 200, 500, 1, 12, false),
0093     FCC_PATTERN(5, 50, 100, 1000, 2000, 1, 1, true),
0094     FCC_PATTERN(6, 0, 1, 333, 333, 1, 9, false),
0095 };
0096 
0097 static const struct radar_types fcc_radar_types = {
0098     .region         = NL80211_DFS_FCC,
0099     .num_radar_types    = ARRAY_SIZE(fcc_radar_ref_types),
0100     .radar_types        = fcc_radar_ref_types,
0101 };
0102 
0103 #define JP_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, RATE, CHIRP)   \
0104 {                               \
0105     ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),       \
0106     PMIN - PRI_TOLERANCE,                   \
0107     PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,     \
0108     PPB_THRESH_RATE(PPB, RATE), PRI_TOLERANCE, CHIRP    \
0109 }
0110 static const struct radar_detector_specs jp_radar_ref_types[] = {
0111     JP_PATTERN(0, 0, 1, 1428, 1428, 1, 18, 29, false),
0112     JP_PATTERN(1, 2, 3, 3846, 3846, 1, 18, 29, false),
0113     JP_PATTERN(2, 0, 1, 1388, 1388, 1, 18, 50, false),
0114     JP_PATTERN(3, 0, 4, 4000, 4000, 1, 18, 50, false),
0115     JP_PATTERN(4, 0, 5, 150, 230, 1, 23, 50, false),
0116     JP_PATTERN(5, 6, 10, 200, 500, 1, 16, 50, false),
0117     JP_PATTERN(6, 11, 20, 200, 500, 1, 12, 50, false),
0118     JP_PATTERN(7, 50, 100, 1000, 2000, 1, 3, 50, true),
0119     JP_PATTERN(5, 0, 1, 333, 333, 1, 9, 50, false),
0120 };
0121 
0122 static const struct radar_types jp_radar_types = {
0123     .region         = NL80211_DFS_JP,
0124     .num_radar_types    = ARRAY_SIZE(jp_radar_ref_types),
0125     .radar_types        = jp_radar_ref_types,
0126 };
0127 
0128 static const struct radar_types *dfs_domains[] = {
0129     &etsi_radar_types_v15,
0130     &fcc_radar_types,
0131     &jp_radar_types,
0132 };
0133 
0134 /**
0135  * get_dfs_domain_radar_types() - get radar types for a given DFS domain
0136  * @region: regulatory DFS region
0137  *
0138  * Return value: radar_types ptr on success, NULL if DFS domain is not supported
0139  */
0140 static const struct radar_types *
0141 get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
0142 {
0143     u32 i;
0144     for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
0145         if (dfs_domains[i]->region == region)
0146             return dfs_domains[i];
0147     }
0148     return NULL;
0149 }
0150 
0151 /**
0152  * struct channel_detector - detector elements for a DFS channel
0153  * @head: list_head
0154  * @freq: frequency for this channel detector in MHz
0155  * @detectors: array of dynamically created detector elements for this freq
0156  *
0157  * Channel detectors are required to provide multi-channel DFS detection, e.g.
0158  * to support off-channel scanning. A pattern detector has a list of channels
0159  * radar pulses have been reported for in the past.
0160  */
0161 struct channel_detector {
0162     struct list_head head;
0163     u16 freq;
0164     struct pri_detector **detectors;
0165 };
0166 
0167 /* channel_detector_reset() - reset detector lines for a given channel */
0168 static void channel_detector_reset(struct dfs_pattern_detector *dpd,
0169                    struct channel_detector *cd)
0170 {
0171     u32 i;
0172     if (cd == NULL)
0173         return;
0174     for (i = 0; i < dpd->num_radar_types; i++)
0175         cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
0176 }
0177 
0178 /* channel_detector_exit() - destructor */
0179 static void channel_detector_exit(struct dfs_pattern_detector *dpd,
0180                   struct channel_detector *cd)
0181 {
0182     u32 i;
0183     if (cd == NULL)
0184         return;
0185     list_del(&cd->head);
0186     if (cd->detectors) {
0187         for (i = 0; i < dpd->num_radar_types; i++) {
0188             struct pri_detector *de = cd->detectors[i];
0189             if (de != NULL)
0190                 de->exit(de);
0191         }
0192     }
0193     kfree(cd->detectors);
0194     kfree(cd);
0195 }
0196 
0197 static struct channel_detector *
0198 channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
0199 {
0200     u32 i;
0201     struct channel_detector *cd;
0202 
0203     cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
0204     if (cd == NULL)
0205         goto fail;
0206 
0207     INIT_LIST_HEAD(&cd->head);
0208     cd->freq = freq;
0209     cd->detectors = kmalloc_array(dpd->num_radar_types,
0210                       sizeof(*cd->detectors), GFP_ATOMIC);
0211     if (cd->detectors == NULL)
0212         goto fail;
0213 
0214     for (i = 0; i < dpd->num_radar_types; i++) {
0215         const struct radar_detector_specs *rs = &dpd->radar_spec[i];
0216         struct pri_detector *de = pri_detector_init(rs);
0217         if (de == NULL)
0218             goto fail;
0219         cd->detectors[i] = de;
0220     }
0221     list_add(&cd->head, &dpd->channel_detectors);
0222     return cd;
0223 
0224 fail:
0225     ath_dbg(dpd->common, DFS,
0226         "failed to allocate channel_detector for freq=%d\n", freq);
0227     channel_detector_exit(dpd, cd);
0228     return NULL;
0229 }
0230 
0231 /**
0232  * channel_detector_get() - get channel detector for given frequency
0233  * @dpd: DPD instance pointer
0234  * @freq: freq frequency in MHz
0235  *
0236  * Return value: pointer to channel detector on success, NULL otherwise
0237  *
0238  * Return existing channel detector for the given frequency or return a
0239  * newly create one.
0240  */
0241 static struct channel_detector *
0242 channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
0243 {
0244     struct channel_detector *cd;
0245     list_for_each_entry(cd, &dpd->channel_detectors, head) {
0246         if (cd->freq == freq)
0247             return cd;
0248     }
0249     return channel_detector_create(dpd, freq);
0250 }
0251 
0252 /*
0253  * DFS Pattern Detector
0254  */
0255 
0256 /* dpd_reset(): reset all channel detectors */
0257 static void dpd_reset(struct dfs_pattern_detector *dpd)
0258 {
0259     struct channel_detector *cd;
0260     list_for_each_entry(cd, &dpd->channel_detectors, head)
0261         channel_detector_reset(dpd, cd);
0262 
0263 }
0264 static void dpd_exit(struct dfs_pattern_detector *dpd)
0265 {
0266     struct channel_detector *cd, *cd0;
0267     list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
0268         channel_detector_exit(dpd, cd);
0269     kfree(dpd);
0270 }
0271 
0272 static bool
0273 dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event,
0274           struct radar_detector_specs *rs)
0275 {
0276     u32 i;
0277     struct channel_detector *cd;
0278 
0279     /*
0280      * pulses received for a non-supported or un-initialized
0281      * domain are treated as detected radars for fail-safety
0282      */
0283     if (dpd->region == NL80211_DFS_UNSET)
0284         return true;
0285 
0286     cd = channel_detector_get(dpd, event->freq);
0287     if (cd == NULL)
0288         return false;
0289 
0290     /* reset detector on time stamp wraparound, caused by TSF reset */
0291     if (event->ts < dpd->last_pulse_ts)
0292         dpd_reset(dpd);
0293     dpd->last_pulse_ts = event->ts;
0294 
0295     /* do type individual pattern matching */
0296     for (i = 0; i < dpd->num_radar_types; i++) {
0297         struct pri_detector *pd = cd->detectors[i];
0298         struct pri_sequence *ps = pd->add_pulse(pd, event);
0299         if (ps != NULL) {
0300             if (rs != NULL)
0301                 memcpy(rs, pd->rs, sizeof(*rs));
0302             ath_dbg(dpd->common, DFS,
0303                 "DFS: radar found on freq=%d: id=%d, pri=%d, "
0304                 "count=%d, count_false=%d\n",
0305                 event->freq, pd->rs->type_id,
0306                 ps->pri, ps->count, ps->count_falses);
0307             pd->reset(pd, dpd->last_pulse_ts);
0308             return true;
0309         }
0310     }
0311     return false;
0312 }
0313 
0314 static struct ath_dfs_pool_stats
0315 dpd_get_stats(struct dfs_pattern_detector *dpd)
0316 {
0317     return global_dfs_pool_stats;
0318 }
0319 
0320 static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
0321                enum nl80211_dfs_regions region)
0322 {
0323     const struct radar_types *rt;
0324     struct channel_detector *cd, *cd0;
0325 
0326     if (dpd->region == region)
0327         return true;
0328 
0329     dpd->region = NL80211_DFS_UNSET;
0330 
0331     rt = get_dfs_domain_radar_types(region);
0332     if (rt == NULL)
0333         return false;
0334 
0335     /* delete all channel detectors for previous DFS domain */
0336     list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
0337         channel_detector_exit(dpd, cd);
0338     dpd->radar_spec = rt->radar_types;
0339     dpd->num_radar_types = rt->num_radar_types;
0340 
0341     dpd->region = region;
0342     return true;
0343 }
0344 
0345 static const struct dfs_pattern_detector default_dpd = {
0346     .exit       = dpd_exit,
0347     .set_dfs_domain = dpd_set_domain,
0348     .add_pulse  = dpd_add_pulse,
0349     .get_stats  = dpd_get_stats,
0350     .region     = NL80211_DFS_UNSET,
0351 };
0352 
0353 struct dfs_pattern_detector *
0354 dfs_pattern_detector_init(struct ath_common *common,
0355               enum nl80211_dfs_regions region)
0356 {
0357     struct dfs_pattern_detector *dpd;
0358 
0359     if (!IS_ENABLED(CONFIG_CFG80211_CERTIFICATION_ONUS))
0360         return NULL;
0361 
0362     dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
0363     if (dpd == NULL)
0364         return NULL;
0365 
0366     *dpd = default_dpd;
0367     INIT_LIST_HEAD(&dpd->channel_detectors);
0368 
0369     dpd->common = common;
0370     if (dpd->set_dfs_domain(dpd, region))
0371         return dpd;
0372 
0373     ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
0374     kfree(dpd);
0375     return NULL;
0376 }
0377 EXPORT_SYMBOL(dfs_pattern_detector_init);