0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0026
0027
0028
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
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
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
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
0080
0081
0082
0083
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
0136
0137
0138
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
0153
0154
0155
0156
0157
0158
0159
0160
0161 struct channel_detector {
0162 struct list_head head;
0163 u16 freq;
0164 struct pri_detector **detectors;
0165 };
0166
0167
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
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
0233
0234
0235
0236
0237
0238
0239
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
0254
0255
0256
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
0281
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
0291 if (event->ts < dpd->last_pulse_ts)
0292 dpd_reset(dpd);
0293 dpd->last_pulse_ts = event->ts;
0294
0295
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
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);