]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/net/wireless/ath/ath9k/dfs_pattern_detector.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[can-eth-gw-linux.git] / drivers / net / wireless / ath / ath9k / dfs_pattern_detector.c
1 /*
2  * Copyright (c) 2012 Neratec Solutions AG
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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
23 /*
24  * tolerated deviation of radar time stamp in usecs on both sides
25  * TODO: this might need to be HW-dependent
26  */
27 #define PRI_TOLERANCE   16
28
29 /**
30  * struct radar_types - contains array of patterns defined for one DFS domain
31  * @domain: DFS regulatory domain
32  * @num_radar_types: number of radar types to follow
33  * @radar_types: radar types array
34  */
35 struct radar_types {
36         enum nl80211_dfs_regions region;
37         u32 num_radar_types;
38         const struct radar_detector_specs *radar_types;
39 };
40
41 /* percentage on ppb threshold to trigger detection */
42 #define MIN_PPB_THRESH  50
43 #define PPB_THRESH(PPB) ((PPB * MIN_PPB_THRESH + 50) / 100)
44 #define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
45 /* percentage of pulse width tolerance */
46 #define WIDTH_TOLERANCE 5
47 #define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
48 #define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
49
50 #define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB)      \
51 {                                                               \
52         ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),               \
53         (PRF2PRI(PMAX) - PRI_TOLERANCE),                        \
54         (PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF,  \
55         PPB_THRESH(PPB), PRI_TOLERANCE,                         \
56 }
57
58 /* radar types as defined by ETSI EN-301-893 v1.5.1 */
59 static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
60         ETSI_PATTERN(0,  0,  1,  700,  700, 1, 18),
61         ETSI_PATTERN(1,  0,  5,  200, 1000, 1, 10),
62         ETSI_PATTERN(2,  0, 15,  200, 1600, 1, 15),
63         ETSI_PATTERN(3,  0, 15, 2300, 4000, 1, 25),
64         ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20),
65         ETSI_PATTERN(5,  0,  2,  300,  400, 3, 10),
66         ETSI_PATTERN(6,  0,  2,  400, 1200, 3, 15),
67 };
68
69 static const struct radar_types etsi_radar_types_v15 = {
70         .region                 = NL80211_DFS_ETSI,
71         .num_radar_types        = ARRAY_SIZE(etsi_radar_ref_types_v15),
72         .radar_types            = etsi_radar_ref_types_v15,
73 };
74
75 /* for now, we support ETSI radar types, FCC and JP are TODO */
76 static const struct radar_types *dfs_domains[] = {
77         &etsi_radar_types_v15,
78 };
79
80 /**
81  * get_dfs_domain_radar_types() - get radar types for a given DFS domain
82  * @param domain DFS domain
83  * @return radar_types ptr on success, NULL if DFS domain is not supported
84  */
85 static const struct radar_types *
86 get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
87 {
88         u32 i;
89         for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
90                 if (dfs_domains[i]->region == region)
91                         return dfs_domains[i];
92         }
93         return NULL;
94 }
95
96 /**
97  * struct channel_detector - detector elements for a DFS channel
98  * @head: list_head
99  * @freq: frequency for this channel detector in MHz
100  * @detectors: array of dynamically created detector elements for this freq
101  *
102  * Channel detectors are required to provide multi-channel DFS detection, e.g.
103  * to support off-channel scanning. A pattern detector has a list of channels
104  * radar pulses have been reported for in the past.
105  */
106 struct channel_detector {
107         struct list_head head;
108         u16 freq;
109         struct pri_detector **detectors;
110 };
111
112 /* channel_detector_reset() - reset detector lines for a given channel */
113 static void channel_detector_reset(struct dfs_pattern_detector *dpd,
114                                    struct channel_detector *cd)
115 {
116         u32 i;
117         if (cd == NULL)
118                 return;
119         for (i = 0; i < dpd->num_radar_types; i++)
120                 cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
121 }
122
123 /* channel_detector_exit() - destructor */
124 static void channel_detector_exit(struct dfs_pattern_detector *dpd,
125                                   struct channel_detector *cd)
126 {
127         u32 i;
128         if (cd == NULL)
129                 return;
130         list_del(&cd->head);
131         for (i = 0; i < dpd->num_radar_types; i++) {
132                 struct pri_detector *de = cd->detectors[i];
133                 if (de != NULL)
134                         de->exit(de);
135         }
136         kfree(cd->detectors);
137         kfree(cd);
138 }
139
140 static struct channel_detector *
141 channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
142 {
143         u32 sz, i;
144         struct channel_detector *cd;
145
146         cd = kmalloc(sizeof(*cd), GFP_KERNEL);
147         if (cd == NULL)
148                 goto fail;
149
150         INIT_LIST_HEAD(&cd->head);
151         cd->freq = freq;
152         sz = sizeof(cd->detectors) * dpd->num_radar_types;
153         cd->detectors = kzalloc(sz, GFP_KERNEL);
154         if (cd->detectors == NULL)
155                 goto fail;
156
157         for (i = 0; i < dpd->num_radar_types; i++) {
158                 const struct radar_detector_specs *rs = &dpd->radar_spec[i];
159                 struct pri_detector *de = pri_detector_init(rs);
160                 if (de == NULL)
161                         goto fail;
162                 cd->detectors[i] = de;
163         }
164         list_add(&cd->head, &dpd->channel_detectors);
165         return cd;
166
167 fail:
168         pr_err("failed to allocate channel_detector for freq=%d\n", freq);
169         channel_detector_exit(dpd, cd);
170         return NULL;
171 }
172
173 /**
174  * channel_detector_get() - get channel detector for given frequency
175  * @param dpd instance pointer
176  * @param freq frequency in MHz
177  * @return pointer to channel detector on success, NULL otherwise
178  *
179  * Return existing channel detector for the given frequency or return a
180  * newly create one.
181  */
182 static struct channel_detector *
183 channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
184 {
185         struct channel_detector *cd;
186         list_for_each_entry(cd, &dpd->channel_detectors, head) {
187                 if (cd->freq == freq)
188                         return cd;
189         }
190         return channel_detector_create(dpd, freq);
191 }
192
193 /*
194  * DFS Pattern Detector
195  */
196
197 /* dpd_reset(): reset all channel detectors */
198 static void dpd_reset(struct dfs_pattern_detector *dpd)
199 {
200         struct channel_detector *cd;
201         if (!list_empty(&dpd->channel_detectors))
202                 list_for_each_entry(cd, &dpd->channel_detectors, head)
203                         channel_detector_reset(dpd, cd);
204
205 }
206 static void dpd_exit(struct dfs_pattern_detector *dpd)
207 {
208         struct channel_detector *cd, *cd0;
209         if (!list_empty(&dpd->channel_detectors))
210                 list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
211                         channel_detector_exit(dpd, cd);
212         kfree(dpd);
213 }
214
215 static bool
216 dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
217 {
218         u32 i;
219         bool ts_wraparound;
220         struct channel_detector *cd;
221
222         if (dpd->region == NL80211_DFS_UNSET) {
223                 /*
224                  * pulses received for a non-supported or un-initialized
225                  * domain are treated as detected radars
226                  */
227                 return true;
228         }
229
230         cd = channel_detector_get(dpd, event->freq);
231         if (cd == NULL)
232                 return false;
233
234         ts_wraparound = (event->ts < dpd->last_pulse_ts);
235         dpd->last_pulse_ts = event->ts;
236         if (ts_wraparound) {
237                 /*
238                  * reset detector on time stamp wraparound
239                  * with monotonic time stamps, this should never happen
240                  */
241                 pr_warn("DFS: time stamp wraparound detected, resetting\n");
242                 dpd_reset(dpd);
243         }
244         /* do type individual pattern matching */
245         for (i = 0; i < dpd->num_radar_types; i++) {
246                 if (cd->detectors[i]->add_pulse(cd->detectors[i], event) != 0) {
247                         channel_detector_reset(dpd, cd);
248                         return true;
249                 }
250         }
251         return false;
252 }
253
254 static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
255                            enum nl80211_dfs_regions region)
256 {
257         const struct radar_types *rt;
258         struct channel_detector *cd, *cd0;
259
260         if (dpd->region == region)
261                 return true;
262
263         dpd->region = NL80211_DFS_UNSET;
264
265         rt = get_dfs_domain_radar_types(region);
266         if (rt == NULL)
267                 return false;
268
269         /* delete all channel detectors for previous DFS domain */
270         if (!list_empty(&dpd->channel_detectors))
271                 list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
272                         channel_detector_exit(dpd, cd);
273         dpd->radar_spec = rt->radar_types;
274         dpd->num_radar_types = rt->num_radar_types;
275
276         dpd->region = region;
277         return true;
278 }
279
280 static struct dfs_pattern_detector default_dpd = {
281         .exit           = dpd_exit,
282         .set_dfs_domain = dpd_set_domain,
283         .add_pulse      = dpd_add_pulse,
284         .region         = NL80211_DFS_UNSET,
285 };
286
287 struct dfs_pattern_detector *
288 dfs_pattern_detector_init(enum nl80211_dfs_regions region)
289 {
290         struct dfs_pattern_detector *dpd;
291         dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
292         if (dpd == NULL) {
293                 pr_err("allocation of dfs_pattern_detector failed\n");
294                 return NULL;
295         }
296         *dpd = default_dpd;
297         INIT_LIST_HEAD(&dpd->channel_detectors);
298
299         if (dpd->set_dfs_domain(dpd, region))
300                 return dpd;
301
302         pr_err("Could not set DFS domain to %d. ", region);
303         kfree(dpd);
304         return NULL;
305 }
306 EXPORT_SYMBOL(dfs_pattern_detector_init);