Linux Audio

Check our new training course

Loading...
v6.8
  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#include "ath.h"
 23
 24/**
 25 * struct radar_types - contains array of patterns defined for one DFS domain
 26 * @region: regulatory DFS region
 27 * @num_radar_types: number of radar types to follow
 28 * @radar_types: radar types array
 29 */
 30struct radar_types {
 31	enum nl80211_dfs_regions region;
 32	u32 num_radar_types;
 33	const struct radar_detector_specs *radar_types;
 34};
 35
 36/* percentage on ppb threshold to trigger detection */
 37#define MIN_PPB_THRESH	50
 38#define PPB_THRESH_RATE(PPB, RATE) ((PPB * RATE + 100 - RATE) / 100)
 39#define PPB_THRESH(PPB) PPB_THRESH_RATE(PPB, MIN_PPB_THRESH)
 40#define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
 41/* percentage of pulse width tolerance */
 42#define WIDTH_TOLERANCE 5
 43#define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
 44#define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
 45
 46#define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)	\
 47{								\
 48	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
 49	(PRF2PRI(PMAX) - PRI_TOLERANCE),			\
 50	(PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF,	\
 51	PPB_THRESH(PPB), PRI_TOLERANCE,	CHIRP			\
 52}
 53
 54/* radar types as defined by ETSI EN-301-893 v1.5.1 */
 55static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
 56	ETSI_PATTERN(0,  0,  1,  700,  700, 1, 18, false),
 57	ETSI_PATTERN(1,  0,  5,  200, 1000, 1, 10, false),
 58	ETSI_PATTERN(2,  0, 15,  200, 1600, 1, 15, false),
 59	ETSI_PATTERN(3,  0, 15, 2300, 4000, 1, 25, false),
 60	ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20, false),
 61	ETSI_PATTERN(5,  0,  2,  300,  400, 3, 10, false),
 62	ETSI_PATTERN(6,  0,  2,  400, 1200, 3, 15, false),
 63};
 64
 65static const struct radar_types etsi_radar_types_v15 = {
 66	.region			= NL80211_DFS_ETSI,
 67	.num_radar_types	= ARRAY_SIZE(etsi_radar_ref_types_v15),
 68	.radar_types		= etsi_radar_ref_types_v15,
 69};
 70
 71#define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)	\
 72{								\
 73	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
 74	PMIN - PRI_TOLERANCE,					\
 75	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
 76	PPB_THRESH(PPB), PRI_TOLERANCE,	CHIRP			\
 77}
 78
 79/* radar types released on August 14, 2014
 80 * type 1 PRI values randomly selected within the range of 518 and 3066.
 81 * divide it to 3 groups is good enough for both of radar detection and
 82 * avoiding false detection based on practical test results
 83 * collected for more than a year.
 84 */
 85static const struct radar_detector_specs fcc_radar_ref_types[] = {
 86	FCC_PATTERN(0, 0, 1, 1428, 1428, 1, 18, false),
 87	FCC_PATTERN(101, 0, 1, 518, 938, 1, 57, false),
 88	FCC_PATTERN(102, 0, 1, 938, 2000, 1, 27, false),
 89	FCC_PATTERN(103, 0, 1, 2000, 3066, 1, 18, false),
 90	FCC_PATTERN(2, 0, 5, 150, 230, 1, 23, false),
 91	FCC_PATTERN(3, 6, 10, 200, 500, 1, 16, false),
 92	FCC_PATTERN(4, 11, 20, 200, 500, 1, 12, false),
 93	FCC_PATTERN(5, 50, 100, 1000, 2000, 1, 1, true),
 94	FCC_PATTERN(6, 0, 1, 333, 333, 1, 9, false),
 95};
 96
 97static const struct radar_types fcc_radar_types = {
 98	.region			= NL80211_DFS_FCC,
 99	.num_radar_types	= ARRAY_SIZE(fcc_radar_ref_types),
100	.radar_types		= fcc_radar_ref_types,
101};
102
103#define JP_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, RATE, CHIRP)	\
104{								\
105	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
106	PMIN - PRI_TOLERANCE,					\
107	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
108	PPB_THRESH_RATE(PPB, RATE), PRI_TOLERANCE, CHIRP	\
109}
110static const struct radar_detector_specs jp_radar_ref_types[] = {
111	JP_PATTERN(0, 0, 1, 1428, 1428, 1, 18, 29, false),
112	JP_PATTERN(1, 2, 3, 3846, 3846, 1, 18, 29, false),
113	JP_PATTERN(2, 0, 1, 1388, 1388, 1, 18, 50, false),
114	JP_PATTERN(3, 0, 4, 4000, 4000, 1, 18, 50, false),
115	JP_PATTERN(4, 0, 5, 150, 230, 1, 23, 50, false),
116	JP_PATTERN(5, 6, 10, 200, 500, 1, 16, 50, false),
117	JP_PATTERN(6, 11, 20, 200, 500, 1, 12, 50, false),
118	JP_PATTERN(7, 50, 100, 1000, 2000, 1, 3, 50, true),
119	JP_PATTERN(5, 0, 1, 333, 333, 1, 9, 50, false),
120};
121
122static const struct radar_types jp_radar_types = {
123	.region			= NL80211_DFS_JP,
124	.num_radar_types	= ARRAY_SIZE(jp_radar_ref_types),
125	.radar_types		= jp_radar_ref_types,
126};
127
128static const struct radar_types *dfs_domains[] = {
129	&etsi_radar_types_v15,
130	&fcc_radar_types,
131	&jp_radar_types,
132};
133
134/**
135 * get_dfs_domain_radar_types() - get radar types for a given DFS domain
136 * @region: regulatory DFS region
137 *
138 * Return value: radar_types ptr on success, NULL if DFS domain is not supported
139 */
140static const struct radar_types *
141get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
142{
143	u32 i;
144	for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
145		if (dfs_domains[i]->region == region)
146			return dfs_domains[i];
147	}
148	return NULL;
149}
150
151/**
152 * struct channel_detector - detector elements for a DFS channel
153 * @head: list_head
154 * @freq: frequency for this channel detector in MHz
155 * @detectors: array of dynamically created detector elements for this freq
156 *
157 * Channel detectors are required to provide multi-channel DFS detection, e.g.
158 * to support off-channel scanning. A pattern detector has a list of channels
159 * radar pulses have been reported for in the past.
160 */
161struct channel_detector {
162	struct list_head head;
163	u16 freq;
164	struct pri_detector *detectors[];
165};
166
167/* channel_detector_reset() - reset detector lines for a given channel */
168static void channel_detector_reset(struct dfs_pattern_detector *dpd,
169				   struct channel_detector *cd)
170{
171	u32 i;
172	if (cd == NULL)
173		return;
174	for (i = 0; i < dpd->num_radar_types; i++)
175		cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
176}
177
178/* channel_detector_exit() - destructor */
179static void channel_detector_exit(struct dfs_pattern_detector *dpd,
180				  struct channel_detector *cd)
181{
182	u32 i;
183	if (cd == NULL)
184		return;
185	list_del(&cd->head);
186
187	for (i = 0; i < dpd->num_radar_types; i++) {
188		struct pri_detector *de = cd->detectors[i];
189		if (de != NULL)
190			de->exit(de);
191	}
192
193	kfree(cd);
194}
195
196static struct channel_detector *
197channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
198{
199	u32 i;
200	struct channel_detector *cd;
201
202	cd = kzalloc(struct_size(cd, detectors, dpd->num_radar_types), GFP_ATOMIC);
203	if (cd == NULL)
204		goto fail;
205
206	INIT_LIST_HEAD(&cd->head);
207	cd->freq = freq;
 
 
 
 
208
209	for (i = 0; i < dpd->num_radar_types; i++) {
210		const struct radar_detector_specs *rs = &dpd->radar_spec[i];
211		struct pri_detector *de = pri_detector_init(rs);
212		if (de == NULL)
213			goto fail;
214		cd->detectors[i] = de;
215	}
216	list_add(&cd->head, &dpd->channel_detectors);
217	return cd;
218
219fail:
220	ath_dbg(dpd->common, DFS,
221		"failed to allocate channel_detector for freq=%d\n", freq);
222	channel_detector_exit(dpd, cd);
223	return NULL;
224}
225
226/**
227 * channel_detector_get() - get channel detector for given frequency
228 * @dpd: DPD instance pointer
229 * @freq: freq frequency in MHz
230 *
231 * Return value: pointer to channel detector on success, NULL otherwise
232 *
233 * Return existing channel detector for the given frequency or return a
234 * newly create one.
235 */
236static struct channel_detector *
237channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
238{
239	struct channel_detector *cd;
240	list_for_each_entry(cd, &dpd->channel_detectors, head) {
241		if (cd->freq == freq)
242			return cd;
243	}
244	return channel_detector_create(dpd, freq);
245}
246
247/*
248 * DFS Pattern Detector
249 */
250
251/* dpd_reset(): reset all channel detectors */
252static void dpd_reset(struct dfs_pattern_detector *dpd)
253{
254	struct channel_detector *cd;
255	list_for_each_entry(cd, &dpd->channel_detectors, head)
256		channel_detector_reset(dpd, cd);
 
257
258}
259static void dpd_exit(struct dfs_pattern_detector *dpd)
260{
261	struct channel_detector *cd, *cd0;
262	list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
263		channel_detector_exit(dpd, cd);
 
264	kfree(dpd);
265}
266
267static bool
268dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event,
269	      struct radar_detector_specs *rs)
270{
271	u32 i;
272	struct channel_detector *cd;
273
274	/*
275	 * pulses received for a non-supported or un-initialized
276	 * domain are treated as detected radars for fail-safety
277	 */
278	if (dpd->region == NL80211_DFS_UNSET)
279		return true;
280
281	cd = channel_detector_get(dpd, event->freq);
282	if (cd == NULL)
283		return false;
284
285	/* reset detector on time stamp wraparound, caused by TSF reset */
286	if (event->ts < dpd->last_pulse_ts)
287		dpd_reset(dpd);
288	dpd->last_pulse_ts = event->ts;
289
290	/* do type individual pattern matching */
291	for (i = 0; i < dpd->num_radar_types; i++) {
292		struct pri_detector *pd = cd->detectors[i];
293		struct pri_sequence *ps = pd->add_pulse(pd, event);
294		if (ps != NULL) {
295			if (rs != NULL)
296				memcpy(rs, pd->rs, sizeof(*rs));
297			ath_dbg(dpd->common, DFS,
298				"DFS: radar found on freq=%d: id=%d, pri=%d, "
299				"count=%d, count_false=%d\n",
300				event->freq, pd->rs->type_id,
301				ps->pri, ps->count, ps->count_falses);
302			pd->reset(pd, dpd->last_pulse_ts);
303			return true;
304		}
305	}
306	return false;
307}
308
309static struct ath_dfs_pool_stats
310dpd_get_stats(struct dfs_pattern_detector *dpd)
311{
312	return global_dfs_pool_stats;
313}
314
315static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
316			   enum nl80211_dfs_regions region)
317{
318	const struct radar_types *rt;
319	struct channel_detector *cd, *cd0;
320
321	if (dpd->region == region)
322		return true;
323
324	dpd->region = NL80211_DFS_UNSET;
325
326	rt = get_dfs_domain_radar_types(region);
327	if (rt == NULL)
328		return false;
329
330	/* delete all channel detectors for previous DFS domain */
331	list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
332		channel_detector_exit(dpd, cd);
 
333	dpd->radar_spec = rt->radar_types;
334	dpd->num_radar_types = rt->num_radar_types;
335
336	dpd->region = region;
337	return true;
338}
339
340static const struct dfs_pattern_detector default_dpd = {
341	.exit		= dpd_exit,
342	.set_dfs_domain	= dpd_set_domain,
343	.add_pulse	= dpd_add_pulse,
344	.get_stats	= dpd_get_stats,
345	.region		= NL80211_DFS_UNSET,
346};
347
348struct dfs_pattern_detector *
349dfs_pattern_detector_init(struct ath_common *common,
350			  enum nl80211_dfs_regions region)
351{
352	struct dfs_pattern_detector *dpd;
353
354	if (!IS_ENABLED(CONFIG_CFG80211_CERTIFICATION_ONUS))
355		return NULL;
356
357	dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
358	if (dpd == NULL)
359		return NULL;
360
361	*dpd = default_dpd;
362	INIT_LIST_HEAD(&dpd->channel_detectors);
363
364	dpd->common = common;
365	if (dpd->set_dfs_domain(dpd, region))
366		return dpd;
367
368	ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
369	kfree(dpd);
370	return NULL;
371}
372EXPORT_SYMBOL(dfs_pattern_detector_init);
v4.17
  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#include "ath.h"
 23
 24/**
 25 * struct radar_types - contains array of patterns defined for one DFS domain
 26 * @domain: DFS regulatory domain
 27 * @num_radar_types: number of radar types to follow
 28 * @radar_types: radar types array
 29 */
 30struct radar_types {
 31	enum nl80211_dfs_regions region;
 32	u32 num_radar_types;
 33	const struct radar_detector_specs *radar_types;
 34};
 35
 36/* percentage on ppb threshold to trigger detection */
 37#define MIN_PPB_THRESH	50
 38#define PPB_THRESH_RATE(PPB, RATE) ((PPB * RATE + 100 - RATE) / 100)
 39#define PPB_THRESH(PPB) PPB_THRESH_RATE(PPB, MIN_PPB_THRESH)
 40#define PRF2PRI(PRF) ((1000000 + PRF / 2) / PRF)
 41/* percentage of pulse width tolerance */
 42#define WIDTH_TOLERANCE 5
 43#define WIDTH_LOWER(X) ((X*(100-WIDTH_TOLERANCE)+50)/100)
 44#define WIDTH_UPPER(X) ((X*(100+WIDTH_TOLERANCE)+50)/100)
 45
 46#define ETSI_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)	\
 47{								\
 48	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
 49	(PRF2PRI(PMAX) - PRI_TOLERANCE),			\
 50	(PRF2PRI(PMIN) * PRF + PRI_TOLERANCE), PRF, PPB * PRF,	\
 51	PPB_THRESH(PPB), PRI_TOLERANCE,	CHIRP			\
 52}
 53
 54/* radar types as defined by ETSI EN-301-893 v1.5.1 */
 55static const struct radar_detector_specs etsi_radar_ref_types_v15[] = {
 56	ETSI_PATTERN(0,  0,  1,  700,  700, 1, 18, false),
 57	ETSI_PATTERN(1,  0,  5,  200, 1000, 1, 10, false),
 58	ETSI_PATTERN(2,  0, 15,  200, 1600, 1, 15, false),
 59	ETSI_PATTERN(3,  0, 15, 2300, 4000, 1, 25, false),
 60	ETSI_PATTERN(4, 20, 30, 2000, 4000, 1, 20, false),
 61	ETSI_PATTERN(5,  0,  2,  300,  400, 3, 10, false),
 62	ETSI_PATTERN(6,  0,  2,  400, 1200, 3, 15, false),
 63};
 64
 65static const struct radar_types etsi_radar_types_v15 = {
 66	.region			= NL80211_DFS_ETSI,
 67	.num_radar_types	= ARRAY_SIZE(etsi_radar_ref_types_v15),
 68	.radar_types		= etsi_radar_ref_types_v15,
 69};
 70
 71#define FCC_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, CHIRP)	\
 72{								\
 73	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
 74	PMIN - PRI_TOLERANCE,					\
 75	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
 76	PPB_THRESH(PPB), PRI_TOLERANCE,	CHIRP			\
 77}
 78
 79/* radar types released on August 14, 2014
 80 * type 1 PRI values randomly selected within the range of 518 and 3066.
 81 * divide it to 3 groups is good enough for both of radar detection and
 82 * avoiding false detection based on practical test results
 83 * collected for more than a year.
 84 */
 85static const struct radar_detector_specs fcc_radar_ref_types[] = {
 86	FCC_PATTERN(0, 0, 1, 1428, 1428, 1, 18, false),
 87	FCC_PATTERN(101, 0, 1, 518, 938, 1, 57, false),
 88	FCC_PATTERN(102, 0, 1, 938, 2000, 1, 27, false),
 89	FCC_PATTERN(103, 0, 1, 2000, 3066, 1, 18, false),
 90	FCC_PATTERN(2, 0, 5, 150, 230, 1, 23, false),
 91	FCC_PATTERN(3, 6, 10, 200, 500, 1, 16, false),
 92	FCC_PATTERN(4, 11, 20, 200, 500, 1, 12, false),
 93	FCC_PATTERN(5, 50, 100, 1000, 2000, 1, 1, true),
 94	FCC_PATTERN(6, 0, 1, 333, 333, 1, 9, false),
 95};
 96
 97static const struct radar_types fcc_radar_types = {
 98	.region			= NL80211_DFS_FCC,
 99	.num_radar_types	= ARRAY_SIZE(fcc_radar_ref_types),
100	.radar_types		= fcc_radar_ref_types,
101};
102
103#define JP_PATTERN(ID, WMIN, WMAX, PMIN, PMAX, PRF, PPB, RATE, CHIRP)	\
104{								\
105	ID, WIDTH_LOWER(WMIN), WIDTH_UPPER(WMAX),		\
106	PMIN - PRI_TOLERANCE,					\
107	PMAX * PRF + PRI_TOLERANCE, PRF, PPB * PRF,		\
108	PPB_THRESH_RATE(PPB, RATE), PRI_TOLERANCE, CHIRP	\
109}
110static const struct radar_detector_specs jp_radar_ref_types[] = {
111	JP_PATTERN(0, 0, 1, 1428, 1428, 1, 18, 29, false),
112	JP_PATTERN(1, 2, 3, 3846, 3846, 1, 18, 29, false),
113	JP_PATTERN(2, 0, 1, 1388, 1388, 1, 18, 50, false),
114	JP_PATTERN(3, 1, 2, 4000, 4000, 1, 18, 50, false),
115	JP_PATTERN(4, 0, 5, 150, 230, 1, 23, 50, false),
116	JP_PATTERN(5, 6, 10, 200, 500, 1, 16, 50, false),
117	JP_PATTERN(6, 11, 20, 200, 500, 1, 12, 50, false),
118	JP_PATTERN(7, 50, 100, 1000, 2000, 1, 3, 50, true),
119	JP_PATTERN(5, 0, 1, 333, 333, 1, 9, 50, false),
120};
121
122static const struct radar_types jp_radar_types = {
123	.region			= NL80211_DFS_JP,
124	.num_radar_types	= ARRAY_SIZE(jp_radar_ref_types),
125	.radar_types		= jp_radar_ref_types,
126};
127
128static const struct radar_types *dfs_domains[] = {
129	&etsi_radar_types_v15,
130	&fcc_radar_types,
131	&jp_radar_types,
132};
133
134/**
135 * get_dfs_domain_radar_types() - get radar types for a given DFS domain
136 * @param domain DFS domain
137 * @return radar_types ptr on success, NULL if DFS domain is not supported
 
138 */
139static const struct radar_types *
140get_dfs_domain_radar_types(enum nl80211_dfs_regions region)
141{
142	u32 i;
143	for (i = 0; i < ARRAY_SIZE(dfs_domains); i++) {
144		if (dfs_domains[i]->region == region)
145			return dfs_domains[i];
146	}
147	return NULL;
148}
149
150/**
151 * struct channel_detector - detector elements for a DFS channel
152 * @head: list_head
153 * @freq: frequency for this channel detector in MHz
154 * @detectors: array of dynamically created detector elements for this freq
155 *
156 * Channel detectors are required to provide multi-channel DFS detection, e.g.
157 * to support off-channel scanning. A pattern detector has a list of channels
158 * radar pulses have been reported for in the past.
159 */
160struct channel_detector {
161	struct list_head head;
162	u16 freq;
163	struct pri_detector **detectors;
164};
165
166/* channel_detector_reset() - reset detector lines for a given channel */
167static void channel_detector_reset(struct dfs_pattern_detector *dpd,
168				   struct channel_detector *cd)
169{
170	u32 i;
171	if (cd == NULL)
172		return;
173	for (i = 0; i < dpd->num_radar_types; i++)
174		cd->detectors[i]->reset(cd->detectors[i], dpd->last_pulse_ts);
175}
176
177/* channel_detector_exit() - destructor */
178static void channel_detector_exit(struct dfs_pattern_detector *dpd,
179				  struct channel_detector *cd)
180{
181	u32 i;
182	if (cd == NULL)
183		return;
184	list_del(&cd->head);
 
185	for (i = 0; i < dpd->num_radar_types; i++) {
186		struct pri_detector *de = cd->detectors[i];
187		if (de != NULL)
188			de->exit(de);
189	}
190	kfree(cd->detectors);
191	kfree(cd);
192}
193
194static struct channel_detector *
195channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
196{
197	u32 sz, i;
198	struct channel_detector *cd;
199
200	cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
201	if (cd == NULL)
202		goto fail;
203
204	INIT_LIST_HEAD(&cd->head);
205	cd->freq = freq;
206	sz = sizeof(cd->detectors) * dpd->num_radar_types;
207	cd->detectors = kzalloc(sz, GFP_ATOMIC);
208	if (cd->detectors == NULL)
209		goto fail;
210
211	for (i = 0; i < dpd->num_radar_types; i++) {
212		const struct radar_detector_specs *rs = &dpd->radar_spec[i];
213		struct pri_detector *de = pri_detector_init(rs);
214		if (de == NULL)
215			goto fail;
216		cd->detectors[i] = de;
217	}
218	list_add(&cd->head, &dpd->channel_detectors);
219	return cd;
220
221fail:
222	ath_dbg(dpd->common, DFS,
223		"failed to allocate channel_detector for freq=%d\n", freq);
224	channel_detector_exit(dpd, cd);
225	return NULL;
226}
227
228/**
229 * channel_detector_get() - get channel detector for given frequency
230 * @param dpd instance pointer
231 * @param freq frequency in MHz
232 * @return pointer to channel detector on success, NULL otherwise
 
233 *
234 * Return existing channel detector for the given frequency or return a
235 * newly create one.
236 */
237static struct channel_detector *
238channel_detector_get(struct dfs_pattern_detector *dpd, u16 freq)
239{
240	struct channel_detector *cd;
241	list_for_each_entry(cd, &dpd->channel_detectors, head) {
242		if (cd->freq == freq)
243			return cd;
244	}
245	return channel_detector_create(dpd, freq);
246}
247
248/*
249 * DFS Pattern Detector
250 */
251
252/* dpd_reset(): reset all channel detectors */
253static void dpd_reset(struct dfs_pattern_detector *dpd)
254{
255	struct channel_detector *cd;
256	if (!list_empty(&dpd->channel_detectors))
257		list_for_each_entry(cd, &dpd->channel_detectors, head)
258			channel_detector_reset(dpd, cd);
259
260}
261static void dpd_exit(struct dfs_pattern_detector *dpd)
262{
263	struct channel_detector *cd, *cd0;
264	if (!list_empty(&dpd->channel_detectors))
265		list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
266			channel_detector_exit(dpd, cd);
267	kfree(dpd);
268}
269
270static bool
271dpd_add_pulse(struct dfs_pattern_detector *dpd, struct pulse_event *event)
 
272{
273	u32 i;
274	struct channel_detector *cd;
275
276	/*
277	 * pulses received for a non-supported or un-initialized
278	 * domain are treated as detected radars for fail-safety
279	 */
280	if (dpd->region == NL80211_DFS_UNSET)
281		return true;
282
283	cd = channel_detector_get(dpd, event->freq);
284	if (cd == NULL)
285		return false;
286
287	/* reset detector on time stamp wraparound, caused by TSF reset */
288	if (event->ts < dpd->last_pulse_ts)
289		dpd_reset(dpd);
290	dpd->last_pulse_ts = event->ts;
291
292	/* do type individual pattern matching */
293	for (i = 0; i < dpd->num_radar_types; i++) {
294		struct pri_detector *pd = cd->detectors[i];
295		struct pri_sequence *ps = pd->add_pulse(pd, event);
296		if (ps != NULL) {
 
 
297			ath_dbg(dpd->common, DFS,
298				"DFS: radar found on freq=%d: id=%d, pri=%d, "
299				"count=%d, count_false=%d\n",
300				event->freq, pd->rs->type_id,
301				ps->pri, ps->count, ps->count_falses);
302			pd->reset(pd, dpd->last_pulse_ts);
303			return true;
304		}
305	}
306	return false;
307}
308
309static struct ath_dfs_pool_stats
310dpd_get_stats(struct dfs_pattern_detector *dpd)
311{
312	return global_dfs_pool_stats;
313}
314
315static bool dpd_set_domain(struct dfs_pattern_detector *dpd,
316			   enum nl80211_dfs_regions region)
317{
318	const struct radar_types *rt;
319	struct channel_detector *cd, *cd0;
320
321	if (dpd->region == region)
322		return true;
323
324	dpd->region = NL80211_DFS_UNSET;
325
326	rt = get_dfs_domain_radar_types(region);
327	if (rt == NULL)
328		return false;
329
330	/* delete all channel detectors for previous DFS domain */
331	if (!list_empty(&dpd->channel_detectors))
332		list_for_each_entry_safe(cd, cd0, &dpd->channel_detectors, head)
333			channel_detector_exit(dpd, cd);
334	dpd->radar_spec = rt->radar_types;
335	dpd->num_radar_types = rt->num_radar_types;
336
337	dpd->region = region;
338	return true;
339}
340
341static const struct dfs_pattern_detector default_dpd = {
342	.exit		= dpd_exit,
343	.set_dfs_domain	= dpd_set_domain,
344	.add_pulse	= dpd_add_pulse,
345	.get_stats	= dpd_get_stats,
346	.region		= NL80211_DFS_UNSET,
347};
348
349struct dfs_pattern_detector *
350dfs_pattern_detector_init(struct ath_common *common,
351			  enum nl80211_dfs_regions region)
352{
353	struct dfs_pattern_detector *dpd;
354
355	if (!IS_ENABLED(CONFIG_CFG80211_CERTIFICATION_ONUS))
356		return NULL;
357
358	dpd = kmalloc(sizeof(*dpd), GFP_KERNEL);
359	if (dpd == NULL)
360		return NULL;
361
362	*dpd = default_dpd;
363	INIT_LIST_HEAD(&dpd->channel_detectors);
364
365	dpd->common = common;
366	if (dpd->set_dfs_domain(dpd, region))
367		return dpd;
368
369	ath_dbg(common, DFS,"Could not set DFS domain to %d", region);
370	kfree(dpd);
371	return NULL;
372}
373EXPORT_SYMBOL(dfs_pattern_detector_init);