1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/slab.h>
18#include <linux/export.h>
19
20#include "dfs_pattern_detector.h"
21#include "dfs_pri_detector.h"
22#include "ath9k.h"
23
24
25
26
27
28#define PRI_TOLERANCE 16
29
30
31
32
33
34
35
36struct radar_types {
37 enum nl80211_dfs_regions region;
38 u32 num_radar_types;
39 const struct radar_detector_specs *radar_types;
40};
41
42
43#define MIN_PPB_THRESH 50
44#define PPB_THRESH(PPB) ((PPB * MIN_PPB_THRESH + 50) / 100)
45#define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
46
47#define WIDTH_TOLERANCE 5
48#define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
49#define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
50
51#define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB) \
52{ \
53 ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX), \
54 (PRF2PRI(PMAX) - PRI_TOLERANCE), \
55 (PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF, \
56 PPB_THRESH(PPB), PRI_TOLERANCE, \
57}
58
59
60static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
61 ETSI_PATTERN(0, 0, 1, 700, 700, 1, 18),
62 ETSI_PATTERN(1, 0, 5, 200, 1000, 1, 10),
63 ETSI_PATTERN(2, 0, 15, 200, 1600, 1, 15),
64 ETSI_PATTERN(3, 0, 15, 2300, 4000, 1, 25),
65 ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20),
66 ETSI_PATTERN(5, 0, 2, 300, 400, 3, 10),
67 ETSI_PATTERN(6, 0, 2, 400, 1200, 3, 15),
68};
69
70static const struct radar_types etsi_radar_types_v15 = {
71 .region = NL80211_DFS_ETSI,
72 .num_radar_types = ARRAY_SIZE(etsi_radar_ref_types_v15),
73 .radar_types = etsi_radar_ref_types_v15,
74};
75
76
77static const struct radar_types *dfs_domains[] = {
78 &etsi_radar_types_v15,
79};
80
81
82
83
84
85
86static const struct radar_types *
87get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
88{
89 u32 i;
90 for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
91 if (dfs_domains[i]->region == region)
92 return dfs_domains[i];
93 }
94 return NULL;
95}
96
97
98
99
100
101
102
103
104
105
106
107struct channel_detector {
108 struct list_head head;
109 u16 freq;
110 struct pri_detector **detectors;
111};
112
113
114static void channel_detector_reset(struct dfs_pattern_detector *dpd,
115 struct channel_detector *cd)
116{
117 u32 i;
118 if (cd == NULL)
119 return;
120 for (i = 0; i < dpd->num_radar_types; i++)
121 cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
122}
123
124
125static void channel_detector_exit(struct dfs_pattern_detector *dpd,
126 struct channel_detector *cd)
127{
128 u32 i;
129 if (cd == NULL)
130 return;
131 list_del(&cd->head);
132 for (i = 0; i < dpd->num_radar_types; i++) {
133 struct pri_detector *de = cd->detectors[i];
134 if (de != NULL)
135 de->exit(de);
136 }
137 kfree(cd->detectors);
138 kfree(cd);
139}
140
141static struct channel_detector *
142channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
143{
144 u32 sz, i;
145 struct channel_detector *cd;
146 struct ath_common *common = ath9k_hw_common(dpd->ah);
147
148 cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
149 if (cd == NULL)
150 goto fail;
151
152 INIT_LIST_HEAD(&cd->head);
153 cd->freq = freq;
154 sz = sizeof(cd->detectors) * dpd->num_radar_types;
155 cd->detectors = kzalloc(sz, GFP_ATOMIC);
156 if (cd->detectors == NULL)
157 goto fail;
158
159 for (i = 0; i < dpd->num_radar_types; i++) {
160 const struct radar_detector_specs *rs = &dpd->radar_spec[i];
161 struct pri_detector *de = pri_detector_init(rs);
162 if (de == NULL)
163 goto fail;
164 cd->detectors[i] = de;
165 }
166 list_add(&cd->head, &dpd->channel_detectors);
167 return cd;
168
169fail:
170 ath_dbg(common, DFS,
171 "failed to allocate channel_detector for freq=%d\n", freq);
172 channel_detector_exit(dpd, cd);
173 return NULL;
174}
175
176
177
178
179
180
181
182
183
184
185static struct channel_detector *
186channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
187{
188 struct channel_detector *cd;
189 list_for_each_entry(cd, &dpd->channel_detectors, head) {
190 if (cd->freq == freq)
191 return cd;
192 }
193 return channel_detector_create(dpd, freq);
194}
195
196
197
198
199
200
201static void dpd_reset(struct dfs_pattern_detector *dpd)
202{
203 struct channel_detector *cd;
204 if (!list_empty(&dpd->channel_detectors))
205 list_for_each_entry(cd, &dpd->channel_detectors, head)
206 channel_detector_reset(dpd, cd);
207
208}
209static void dpd_exit(struct dfs_pattern_detector *dpd)
210{
211 struct channel_detector *cd, *cd0;
212 if (!list_empty(&dpd->channel_detectors))
213 list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
214 channel_detector_exit(dpd, cd);
215 kfree(dpd);
216}
217
218static bool
219dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
220{
221 u32 i;
222 struct channel_detector *cd;
223
224
225
226
227
228 if (dpd->region == NL80211_DFS_UNSET)
229 return true;
230
231 cd = channel_detector_get(dpd, event->freq);
232 if (cd == NULL)
233 return false;
234
235 dpd->last_pulse_ts = event->ts;
236
237 if (event->ts < dpd->last_pulse_ts)
238 dpd_reset(dpd);
239
240
241 for (i = 0; i < dpd->num_radar_types; i++) {
242 struct pri_detector *pd = cd->detectors[i];
243 struct pri_sequence *ps = pd->add_pulse(pd, event);
244 if (ps != NULL) {
245 ath_dbg(ath9k_hw_common(dpd->ah), DFS,
246 "DFS: radar found on freq=%d: id=%d, pri=%d, "
247 "count=%d, count_false=%d\n",
248 event->freq, pd->rs->type_id,
249 ps->pri, ps->count, ps->count_falses);
250 channel_detector_reset(dpd, cd);
251 return true;
252 }
253 }
254 return false;
255}
256
257static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
258 enum nl80211_dfs_regions region)
259{
260 const struct radar_types *rt;
261 struct channel_detector *cd, *cd0;
262
263 if (dpd->region == region)
264 return true;
265
266 dpd->region = NL80211_DFS_UNSET;
267
268 rt = get_dfs_domain_radar_types(region);
269 if (rt == NULL)
270 return false;
271
272
273 if (!list_empty(&dpd->channel_detectors))
274 list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
275 channel_detector_exit(dpd, cd);
276 dpd->radar_spec = rt->radar_types;
277 dpd->num_radar_types = rt->num_radar_types;
278
279 dpd->region = region;
280 return true;
281}
282
283static struct dfs_pattern_detector default_dpd = {
284 .exit = dpd_exit,
285 .set_dfs_domain = dpd_set_domain,
286 .add_pulse = dpd_add_pulse,
287 .region = NL80211_DFS_UNSET,
288};
289
290struct dfs_pattern_detector *
291dfs_pattern_detector_init(struct ath_hw *ah, enum nl80211_dfs_regions region)
292{
293 struct dfs_pattern_detector *dpd;
294 struct ath_common *common = ath9k_hw_common(ah);
295
296 dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
297 if (dpd == NULL)
298 return NULL;
299
300 *dpd = default_dpd;
301 INIT_LIST_HEAD(&dpd->channel_detectors);
302
303 dpd->ah = ah;
304 if (dpd->set_dfs_domain(dpd, region))
305 return dpd;
306
307 ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
308 kfree(dpd);
309 return NULL;
310}
311EXPORT_SYMBOL(dfs_pattern_detector_init);
312