xref: /openbmc/linux/net/wireless/reg.c (revision c989bb15)
18318d78aSJohannes Berg /*
28318d78aSJohannes Berg  * Copyright 2002-2005, Instant802 Networks, Inc.
38318d78aSJohannes Berg  * Copyright 2005-2006, Devicescape Software, Inc.
48318d78aSJohannes Berg  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5b2e1b302SLuis R. Rodriguez  * Copyright 2008	Luis R. Rodriguez <lrodriguz@atheros.com>
68318d78aSJohannes Berg  *
78318d78aSJohannes Berg  * This program is free software; you can redistribute it and/or modify
88318d78aSJohannes Berg  * it under the terms of the GNU General Public License version 2 as
98318d78aSJohannes Berg  * published by the Free Software Foundation.
108318d78aSJohannes Berg  */
118318d78aSJohannes Berg 
12b2e1b302SLuis R. Rodriguez /**
13b2e1b302SLuis R. Rodriguez  * DOC: Wireless regulatory infrastructure
148318d78aSJohannes Berg  *
158318d78aSJohannes Berg  * The usual implementation is for a driver to read a device EEPROM to
168318d78aSJohannes Berg  * determine which regulatory domain it should be operating under, then
178318d78aSJohannes Berg  * looking up the allowable channels in a driver-local table and finally
188318d78aSJohannes Berg  * registering those channels in the wiphy structure.
198318d78aSJohannes Berg  *
20b2e1b302SLuis R. Rodriguez  * Another set of compliance enforcement is for drivers to use their
21b2e1b302SLuis R. Rodriguez  * own compliance limits which can be stored on the EEPROM. The host
22b2e1b302SLuis R. Rodriguez  * driver or firmware may ensure these are used.
23b2e1b302SLuis R. Rodriguez  *
24b2e1b302SLuis R. Rodriguez  * In addition to all this we provide an extra layer of regulatory
25b2e1b302SLuis R. Rodriguez  * conformance. For drivers which do not have any regulatory
26b2e1b302SLuis R. Rodriguez  * information CRDA provides the complete regulatory solution.
27b2e1b302SLuis R. Rodriguez  * For others it provides a community effort on further restrictions
28b2e1b302SLuis R. Rodriguez  * to enhance compliance.
29b2e1b302SLuis R. Rodriguez  *
30b2e1b302SLuis R. Rodriguez  * Note: When number of rules --> infinity we will not be able to
31b2e1b302SLuis R. Rodriguez  * index on alpha2 any more, instead we'll probably have to
32b2e1b302SLuis R. Rodriguez  * rely on some SHA1 checksum of the regdomain for example.
33b2e1b302SLuis R. Rodriguez  *
348318d78aSJohannes Berg  */
35e9c0268fSJoe Perches 
36e9c0268fSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37e9c0268fSJoe Perches 
388318d78aSJohannes Berg #include <linux/kernel.h>
395a0e3ad6STejun Heo #include <linux/slab.h>
40b2e1b302SLuis R. Rodriguez #include <linux/list.h>
41b2e1b302SLuis R. Rodriguez #include <linux/random.h>
42c61029c7SJohn W. Linville #include <linux/ctype.h>
43b2e1b302SLuis R. Rodriguez #include <linux/nl80211.h>
44b2e1b302SLuis R. Rodriguez #include <linux/platform_device.h>
45b2e1b302SLuis R. Rodriguez #include <net/cfg80211.h>
468318d78aSJohannes Berg #include "core.h"
47b2e1b302SLuis R. Rodriguez #include "reg.h"
483b377ea9SJohn W. Linville #include "regdb.h"
4973d54c9eSLuis R. Rodriguez #include "nl80211.h"
508318d78aSJohannes Berg 
514113f751SLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
528271195eSJohn W. Linville #define REG_DBG_PRINT(format, args...) \
534113f751SLuis R. Rodriguez 	do { \
54e9c0268fSJoe Perches 		printk(KERN_DEBUG pr_fmt(format), ##args);	\
554113f751SLuis R. Rodriguez 	} while (0)
564113f751SLuis R. Rodriguez #else
578271195eSJohn W. Linville #define REG_DBG_PRINT(args...)
584113f751SLuis R. Rodriguez #endif
594113f751SLuis R. Rodriguez 
605166ccd2SLuis R. Rodriguez /* Receipt of information from last regulatory request */
61f6037d09SJohannes Berg static struct regulatory_request *last_request;
62734366deSJohannes Berg 
63b2e1b302SLuis R. Rodriguez /* To trigger userspace events */
64b2e1b302SLuis R. Rodriguez static struct platform_device *reg_pdev;
658318d78aSJohannes Berg 
664d9d88d1SScott James Remnant static struct device_type reg_device_type = {
674d9d88d1SScott James Remnant 	.uevent = reg_device_uevent,
684d9d88d1SScott James Remnant };
694d9d88d1SScott James Remnant 
70fb1fc7adSLuis R. Rodriguez /*
71fb1fc7adSLuis R. Rodriguez  * Central wireless core regulatory domains, we only need two,
72734366deSJohannes Berg  * the current one and a world regulatory domain in case we have no
73fb1fc7adSLuis R. Rodriguez  * information to give us an alpha2
74fb1fc7adSLuis R. Rodriguez  */
75f130347cSLuis R. Rodriguez const struct ieee80211_regdomain *cfg80211_regdomain;
76734366deSJohannes Berg 
77fb1fc7adSLuis R. Rodriguez /*
78abc7381bSLuis R. Rodriguez  * Protects static reg.c components:
79abc7381bSLuis R. Rodriguez  *     - cfg80211_world_regdom
80abc7381bSLuis R. Rodriguez  *     - cfg80211_regdom
81abc7381bSLuis R. Rodriguez  *     - last_request
82abc7381bSLuis R. Rodriguez  */
83670b7f11SJohn W. Linville static DEFINE_MUTEX(reg_mutex);
8446a5ebafSJohannes Berg 
8546a5ebafSJohannes Berg static inline void assert_reg_lock(void)
8646a5ebafSJohannes Berg {
8746a5ebafSJohannes Berg 	lockdep_assert_held(&reg_mutex);
8846a5ebafSJohannes Berg }
89abc7381bSLuis R. Rodriguez 
90e38f8a7aSLuis R. Rodriguez /* Used to queue up regulatory hints */
91fe33eb39SLuis R. Rodriguez static LIST_HEAD(reg_requests_list);
92fe33eb39SLuis R. Rodriguez static spinlock_t reg_requests_lock;
93fe33eb39SLuis R. Rodriguez 
94e38f8a7aSLuis R. Rodriguez /* Used to queue up beacon hints for review */
95e38f8a7aSLuis R. Rodriguez static LIST_HEAD(reg_pending_beacons);
96e38f8a7aSLuis R. Rodriguez static spinlock_t reg_pending_beacons_lock;
97e38f8a7aSLuis R. Rodriguez 
98e38f8a7aSLuis R. Rodriguez /* Used to keep track of processed beacon hints */
99e38f8a7aSLuis R. Rodriguez static LIST_HEAD(reg_beacon_list);
100e38f8a7aSLuis R. Rodriguez 
101e38f8a7aSLuis R. Rodriguez struct reg_beacon {
102e38f8a7aSLuis R. Rodriguez 	struct list_head list;
103e38f8a7aSLuis R. Rodriguez 	struct ieee80211_channel chan;
104e38f8a7aSLuis R. Rodriguez };
105e38f8a7aSLuis R. Rodriguez 
106f333a7a2SLuis R. Rodriguez static void reg_todo(struct work_struct *work);
107f333a7a2SLuis R. Rodriguez static DECLARE_WORK(reg_work, reg_todo);
108f333a7a2SLuis R. Rodriguez 
109a90c7a31SLuis R. Rodriguez static void reg_timeout_work(struct work_struct *work);
110a90c7a31SLuis R. Rodriguez static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
111a90c7a31SLuis R. Rodriguez 
112734366deSJohannes Berg /* We keep a static world regulatory domain in case of the absence of CRDA */
113734366deSJohannes Berg static const struct ieee80211_regdomain world_regdom = {
114611b6a82SLuis R. Rodriguez 	.n_reg_rules = 5,
115734366deSJohannes Berg 	.alpha2 =  "00",
116734366deSJohannes Berg 	.reg_rules = {
11768798a62SLuis R. Rodriguez 		/* IEEE 802.11b/g, channels 1..11 */
11868798a62SLuis R. Rodriguez 		REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
119611b6a82SLuis R. Rodriguez 		/* IEEE 802.11b/g, channels 12..13. No HT40
120611b6a82SLuis R. Rodriguez 		 * channel fits here. */
121611b6a82SLuis R. Rodriguez 		REG_RULE(2467-10, 2472+10, 20, 6, 20,
122611b6a82SLuis R. Rodriguez 			NL80211_RRF_PASSIVE_SCAN |
123611b6a82SLuis R. Rodriguez 			NL80211_RRF_NO_IBSS),
124611b6a82SLuis R. Rodriguez 		/* IEEE 802.11 channel 14 - Only JP enables
125611b6a82SLuis R. Rodriguez 		 * this and for 802.11b only */
126611b6a82SLuis R. Rodriguez 		REG_RULE(2484-10, 2484+10, 20, 6, 20,
127611b6a82SLuis R. Rodriguez 			NL80211_RRF_PASSIVE_SCAN |
128611b6a82SLuis R. Rodriguez 			NL80211_RRF_NO_IBSS |
129611b6a82SLuis R. Rodriguez 			NL80211_RRF_NO_OFDM),
1303fc71f77SLuis R. Rodriguez 		/* IEEE 802.11a, channel 36..48 */
131ec329aceSLuis R. Rodriguez 		REG_RULE(5180-10, 5240+10, 40, 6, 20,
1323fc71f77SLuis R. Rodriguez                         NL80211_RRF_PASSIVE_SCAN |
1333fc71f77SLuis R. Rodriguez                         NL80211_RRF_NO_IBSS),
1343fc71f77SLuis R. Rodriguez 
1353fc71f77SLuis R. Rodriguez 		/* NB: 5260 MHz - 5700 MHz requies DFS */
1363fc71f77SLuis R. Rodriguez 
1373fc71f77SLuis R. Rodriguez 		/* IEEE 802.11a, channel 149..165 */
138ec329aceSLuis R. Rodriguez 		REG_RULE(5745-10, 5825+10, 40, 6, 20,
1393fc71f77SLuis R. Rodriguez 			NL80211_RRF_PASSIVE_SCAN |
1403fc71f77SLuis R. Rodriguez 			NL80211_RRF_NO_IBSS),
141734366deSJohannes Berg 	}
142734366deSJohannes Berg };
143734366deSJohannes Berg 
144a3d2eaf0SJohannes Berg static const struct ieee80211_regdomain *cfg80211_world_regdom =
145a3d2eaf0SJohannes Berg 	&world_regdom;
146734366deSJohannes Berg 
1476ee7d330SLuis R. Rodriguez static char *ieee80211_regdom = "00";
14809d989d1SLuis R. Rodriguez static char user_alpha2[2];
1496ee7d330SLuis R. Rodriguez 
150734366deSJohannes Berg module_param(ieee80211_regdom, charp, 0444);
151734366deSJohannes Berg MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
152734366deSJohannes Berg 
153734366deSJohannes Berg static void reset_regdomains(void)
154734366deSJohannes Berg {
155942b25cfSJohannes Berg 	/* avoid freeing static information or freeing something twice */
156942b25cfSJohannes Berg 	if (cfg80211_regdomain == cfg80211_world_regdom)
157942b25cfSJohannes Berg 		cfg80211_regdomain = NULL;
158942b25cfSJohannes Berg 	if (cfg80211_world_regdom == &world_regdom)
159942b25cfSJohannes Berg 		cfg80211_world_regdom = NULL;
160942b25cfSJohannes Berg 	if (cfg80211_regdomain == &world_regdom)
161942b25cfSJohannes Berg 		cfg80211_regdomain = NULL;
162942b25cfSJohannes Berg 
163734366deSJohannes Berg 	kfree(cfg80211_regdomain);
164734366deSJohannes Berg 	kfree(cfg80211_world_regdom);
165734366deSJohannes Berg 
166a3d2eaf0SJohannes Berg 	cfg80211_world_regdom = &world_regdom;
167734366deSJohannes Berg 	cfg80211_regdomain = NULL;
168734366deSJohannes Berg }
169734366deSJohannes Berg 
170fb1fc7adSLuis R. Rodriguez /*
171fb1fc7adSLuis R. Rodriguez  * Dynamic world regulatory domain requested by the wireless
172fb1fc7adSLuis R. Rodriguez  * core upon initialization
173fb1fc7adSLuis R. Rodriguez  */
174a3d2eaf0SJohannes Berg static void update_world_regdomain(const struct ieee80211_regdomain *rd)
175734366deSJohannes Berg {
176f6037d09SJohannes Berg 	BUG_ON(!last_request);
177734366deSJohannes Berg 
178734366deSJohannes Berg 	reset_regdomains();
179734366deSJohannes Berg 
180734366deSJohannes Berg 	cfg80211_world_regdom = rd;
181734366deSJohannes Berg 	cfg80211_regdomain = rd;
182734366deSJohannes Berg }
183734366deSJohannes Berg 
184a3d2eaf0SJohannes Berg bool is_world_regdom(const char *alpha2)
185b2e1b302SLuis R. Rodriguez {
186b2e1b302SLuis R. Rodriguez 	if (!alpha2)
187b2e1b302SLuis R. Rodriguez 		return false;
188b2e1b302SLuis R. Rodriguez 	if (alpha2[0] == '0' && alpha2[1] == '0')
189b2e1b302SLuis R. Rodriguez 		return true;
190b2e1b302SLuis R. Rodriguez 	return false;
191b2e1b302SLuis R. Rodriguez }
192b2e1b302SLuis R. Rodriguez 
193a3d2eaf0SJohannes Berg static bool is_alpha2_set(const char *alpha2)
194b2e1b302SLuis R. Rodriguez {
195b2e1b302SLuis R. Rodriguez 	if (!alpha2)
196b2e1b302SLuis R. Rodriguez 		return false;
197b2e1b302SLuis R. Rodriguez 	if (alpha2[0] != 0 && alpha2[1] != 0)
198b2e1b302SLuis R. Rodriguez 		return true;
199b2e1b302SLuis R. Rodriguez 	return false;
200b2e1b302SLuis R. Rodriguez }
201b2e1b302SLuis R. Rodriguez 
202a3d2eaf0SJohannes Berg static bool is_unknown_alpha2(const char *alpha2)
203b2e1b302SLuis R. Rodriguez {
204b2e1b302SLuis R. Rodriguez 	if (!alpha2)
205b2e1b302SLuis R. Rodriguez 		return false;
206fb1fc7adSLuis R. Rodriguez 	/*
207fb1fc7adSLuis R. Rodriguez 	 * Special case where regulatory domain was built by driver
208fb1fc7adSLuis R. Rodriguez 	 * but a specific alpha2 cannot be determined
209fb1fc7adSLuis R. Rodriguez 	 */
210b2e1b302SLuis R. Rodriguez 	if (alpha2[0] == '9' && alpha2[1] == '9')
211b2e1b302SLuis R. Rodriguez 		return true;
212b2e1b302SLuis R. Rodriguez 	return false;
213b2e1b302SLuis R. Rodriguez }
214b2e1b302SLuis R. Rodriguez 
2153f2355cbSLuis R. Rodriguez static bool is_intersected_alpha2(const char *alpha2)
2163f2355cbSLuis R. Rodriguez {
2173f2355cbSLuis R. Rodriguez 	if (!alpha2)
2183f2355cbSLuis R. Rodriguez 		return false;
219fb1fc7adSLuis R. Rodriguez 	/*
220fb1fc7adSLuis R. Rodriguez 	 * Special case where regulatory domain is the
2213f2355cbSLuis R. Rodriguez 	 * result of an intersection between two regulatory domain
222fb1fc7adSLuis R. Rodriguez 	 * structures
223fb1fc7adSLuis R. Rodriguez 	 */
2243f2355cbSLuis R. Rodriguez 	if (alpha2[0] == '9' && alpha2[1] == '8')
2253f2355cbSLuis R. Rodriguez 		return true;
2263f2355cbSLuis R. Rodriguez 	return false;
2273f2355cbSLuis R. Rodriguez }
2283f2355cbSLuis R. Rodriguez 
229a3d2eaf0SJohannes Berg static bool is_an_alpha2(const char *alpha2)
230b2e1b302SLuis R. Rodriguez {
231b2e1b302SLuis R. Rodriguez 	if (!alpha2)
232b2e1b302SLuis R. Rodriguez 		return false;
233c61029c7SJohn W. Linville 	if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
234b2e1b302SLuis R. Rodriguez 		return true;
235b2e1b302SLuis R. Rodriguez 	return false;
236b2e1b302SLuis R. Rodriguez }
237b2e1b302SLuis R. Rodriguez 
238a3d2eaf0SJohannes Berg static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
239b2e1b302SLuis R. Rodriguez {
240b2e1b302SLuis R. Rodriguez 	if (!alpha2_x || !alpha2_y)
241b2e1b302SLuis R. Rodriguez 		return false;
242b2e1b302SLuis R. Rodriguez 	if (alpha2_x[0] == alpha2_y[0] &&
243b2e1b302SLuis R. Rodriguez 		alpha2_x[1] == alpha2_y[1])
244b2e1b302SLuis R. Rodriguez 		return true;
245b2e1b302SLuis R. Rodriguez 	return false;
246b2e1b302SLuis R. Rodriguez }
247b2e1b302SLuis R. Rodriguez 
24869b1572bSLuis R. Rodriguez static bool regdom_changes(const char *alpha2)
249b2e1b302SLuis R. Rodriguez {
250761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
251761cf7ecSLuis R. Rodriguez 
252b2e1b302SLuis R. Rodriguez 	if (!cfg80211_regdomain)
253b2e1b302SLuis R. Rodriguez 		return true;
254b2e1b302SLuis R. Rodriguez 	if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
255b2e1b302SLuis R. Rodriguez 		return false;
256b2e1b302SLuis R. Rodriguez 	return true;
257b2e1b302SLuis R. Rodriguez }
258b2e1b302SLuis R. Rodriguez 
25909d989d1SLuis R. Rodriguez /*
26009d989d1SLuis R. Rodriguez  * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
26109d989d1SLuis R. Rodriguez  * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
26209d989d1SLuis R. Rodriguez  * has ever been issued.
26309d989d1SLuis R. Rodriguez  */
26409d989d1SLuis R. Rodriguez static bool is_user_regdom_saved(void)
26509d989d1SLuis R. Rodriguez {
26609d989d1SLuis R. Rodriguez 	if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
26709d989d1SLuis R. Rodriguez 		return false;
26809d989d1SLuis R. Rodriguez 
26909d989d1SLuis R. Rodriguez 	/* This would indicate a mistake on the design */
27009d989d1SLuis R. Rodriguez 	if (WARN((!is_world_regdom(user_alpha2) &&
27109d989d1SLuis R. Rodriguez 		  !is_an_alpha2(user_alpha2)),
27209d989d1SLuis R. Rodriguez 		 "Unexpected user alpha2: %c%c\n",
27309d989d1SLuis R. Rodriguez 		 user_alpha2[0],
27409d989d1SLuis R. Rodriguez 	         user_alpha2[1]))
27509d989d1SLuis R. Rodriguez 		return false;
27609d989d1SLuis R. Rodriguez 
27709d989d1SLuis R. Rodriguez 	return true;
27809d989d1SLuis R. Rodriguez }
27909d989d1SLuis R. Rodriguez 
2803b377ea9SJohn W. Linville static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
2813b377ea9SJohn W. Linville 			 const struct ieee80211_regdomain *src_regd)
2823b377ea9SJohn W. Linville {
2833b377ea9SJohn W. Linville 	struct ieee80211_regdomain *regd;
2843b377ea9SJohn W. Linville 	int size_of_regd = 0;
2853b377ea9SJohn W. Linville 	unsigned int i;
2863b377ea9SJohn W. Linville 
2873b377ea9SJohn W. Linville 	size_of_regd = sizeof(struct ieee80211_regdomain) +
2883b377ea9SJohn W. Linville 	  ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
2893b377ea9SJohn W. Linville 
2903b377ea9SJohn W. Linville 	regd = kzalloc(size_of_regd, GFP_KERNEL);
2913b377ea9SJohn W. Linville 	if (!regd)
2923b377ea9SJohn W. Linville 		return -ENOMEM;
2933b377ea9SJohn W. Linville 
2943b377ea9SJohn W. Linville 	memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
2953b377ea9SJohn W. Linville 
2963b377ea9SJohn W. Linville 	for (i = 0; i < src_regd->n_reg_rules; i++)
2973b377ea9SJohn W. Linville 		memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
2983b377ea9SJohn W. Linville 			sizeof(struct ieee80211_reg_rule));
2993b377ea9SJohn W. Linville 
3003b377ea9SJohn W. Linville 	*dst_regd = regd;
3013b377ea9SJohn W. Linville 	return 0;
3023b377ea9SJohn W. Linville }
3033b377ea9SJohn W. Linville 
3043b377ea9SJohn W. Linville #ifdef CONFIG_CFG80211_INTERNAL_REGDB
3053b377ea9SJohn W. Linville struct reg_regdb_search_request {
3063b377ea9SJohn W. Linville 	char alpha2[2];
3073b377ea9SJohn W. Linville 	struct list_head list;
3083b377ea9SJohn W. Linville };
3093b377ea9SJohn W. Linville 
3103b377ea9SJohn W. Linville static LIST_HEAD(reg_regdb_search_list);
311368d06f5SJohn W. Linville static DEFINE_MUTEX(reg_regdb_search_mutex);
3123b377ea9SJohn W. Linville 
3133b377ea9SJohn W. Linville static void reg_regdb_search(struct work_struct *work)
3143b377ea9SJohn W. Linville {
3153b377ea9SJohn W. Linville 	struct reg_regdb_search_request *request;
3163b377ea9SJohn W. Linville 	const struct ieee80211_regdomain *curdom, *regdom;
3173b377ea9SJohn W. Linville 	int i, r;
3183b377ea9SJohn W. Linville 
319368d06f5SJohn W. Linville 	mutex_lock(&reg_regdb_search_mutex);
3203b377ea9SJohn W. Linville 	while (!list_empty(&reg_regdb_search_list)) {
3213b377ea9SJohn W. Linville 		request = list_first_entry(&reg_regdb_search_list,
3223b377ea9SJohn W. Linville 					   struct reg_regdb_search_request,
3233b377ea9SJohn W. Linville 					   list);
3243b377ea9SJohn W. Linville 		list_del(&request->list);
3253b377ea9SJohn W. Linville 
3263b377ea9SJohn W. Linville 		for (i=0; i<reg_regdb_size; i++) {
3273b377ea9SJohn W. Linville 			curdom = reg_regdb[i];
3283b377ea9SJohn W. Linville 
3293b377ea9SJohn W. Linville 			if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
3303b377ea9SJohn W. Linville 				r = reg_copy_regd(&regdom, curdom);
3313b377ea9SJohn W. Linville 				if (r)
3323b377ea9SJohn W. Linville 					break;
3333b377ea9SJohn W. Linville 				mutex_lock(&cfg80211_mutex);
3343b377ea9SJohn W. Linville 				set_regdom(regdom);
3353b377ea9SJohn W. Linville 				mutex_unlock(&cfg80211_mutex);
3363b377ea9SJohn W. Linville 				break;
3373b377ea9SJohn W. Linville 			}
3383b377ea9SJohn W. Linville 		}
3393b377ea9SJohn W. Linville 
3403b377ea9SJohn W. Linville 		kfree(request);
3413b377ea9SJohn W. Linville 	}
342368d06f5SJohn W. Linville 	mutex_unlock(&reg_regdb_search_mutex);
3433b377ea9SJohn W. Linville }
3443b377ea9SJohn W. Linville 
3453b377ea9SJohn W. Linville static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
3463b377ea9SJohn W. Linville 
3473b377ea9SJohn W. Linville static void reg_regdb_query(const char *alpha2)
3483b377ea9SJohn W. Linville {
3493b377ea9SJohn W. Linville 	struct reg_regdb_search_request *request;
3503b377ea9SJohn W. Linville 
3513b377ea9SJohn W. Linville 	if (!alpha2)
3523b377ea9SJohn W. Linville 		return;
3533b377ea9SJohn W. Linville 
3543b377ea9SJohn W. Linville 	request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
3553b377ea9SJohn W. Linville 	if (!request)
3563b377ea9SJohn W. Linville 		return;
3573b377ea9SJohn W. Linville 
3583b377ea9SJohn W. Linville 	memcpy(request->alpha2, alpha2, 2);
3593b377ea9SJohn W. Linville 
360368d06f5SJohn W. Linville 	mutex_lock(&reg_regdb_search_mutex);
3613b377ea9SJohn W. Linville 	list_add_tail(&request->list, &reg_regdb_search_list);
362368d06f5SJohn W. Linville 	mutex_unlock(&reg_regdb_search_mutex);
3633b377ea9SJohn W. Linville 
3643b377ea9SJohn W. Linville 	schedule_work(&reg_regdb_work);
3653b377ea9SJohn W. Linville }
3663b377ea9SJohn W. Linville #else
3673b377ea9SJohn W. Linville static inline void reg_regdb_query(const char *alpha2) {}
3683b377ea9SJohn W. Linville #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
3693b377ea9SJohn W. Linville 
370fb1fc7adSLuis R. Rodriguez /*
371fb1fc7adSLuis R. Rodriguez  * This lets us keep regulatory code which is updated on a regulatory
3724d9d88d1SScott James Remnant  * basis in userspace. Country information is filled in by
3734d9d88d1SScott James Remnant  * reg_device_uevent
374fb1fc7adSLuis R. Rodriguez  */
375b2e1b302SLuis R. Rodriguez static int call_crda(const char *alpha2)
376b2e1b302SLuis R. Rodriguez {
377b2e1b302SLuis R. Rodriguez 	if (!is_world_regdom((char *) alpha2))
378e9c0268fSJoe Perches 		pr_info("Calling CRDA for country: %c%c\n",
379b2e1b302SLuis R. Rodriguez 			alpha2[0], alpha2[1]);
380b2e1b302SLuis R. Rodriguez 	else
381e9c0268fSJoe Perches 		pr_info("Calling CRDA to update world regulatory domain\n");
3828318d78aSJohannes Berg 
3833b377ea9SJohn W. Linville 	/* query internal regulatory database (if it exists) */
3843b377ea9SJohn W. Linville 	reg_regdb_query(alpha2);
3853b377ea9SJohn W. Linville 
3864d9d88d1SScott James Remnant 	return kobject_uevent(&reg_pdev->dev.kobj, KOBJ_CHANGE);
387b2e1b302SLuis R. Rodriguez }
388b2e1b302SLuis R. Rodriguez 
389b2e1b302SLuis R. Rodriguez /* Used by nl80211 before kmalloc'ing our regulatory domain */
390a3d2eaf0SJohannes Berg bool reg_is_valid_request(const char *alpha2)
391b2e1b302SLuis R. Rodriguez {
39261405e97SLuis R. Rodriguez 	assert_cfg80211_lock();
39361405e97SLuis R. Rodriguez 
394f6037d09SJohannes Berg 	if (!last_request)
395f6037d09SJohannes Berg 		return false;
396f6037d09SJohannes Berg 
397f6037d09SJohannes Berg 	return alpha2_equal(last_request->alpha2, alpha2);
398b2e1b302SLuis R. Rodriguez }
399b2e1b302SLuis R. Rodriguez 
400b2e1b302SLuis R. Rodriguez /* Sanity check on a regulatory rule */
401a3d2eaf0SJohannes Berg static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
402b2e1b302SLuis R. Rodriguez {
403a3d2eaf0SJohannes Berg 	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
404b2e1b302SLuis R. Rodriguez 	u32 freq_diff;
405b2e1b302SLuis R. Rodriguez 
40691e99004SLuis R. Rodriguez 	if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
407b2e1b302SLuis R. Rodriguez 		return false;
408b2e1b302SLuis R. Rodriguez 
409b2e1b302SLuis R. Rodriguez 	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
410b2e1b302SLuis R. Rodriguez 		return false;
411b2e1b302SLuis R. Rodriguez 
412b2e1b302SLuis R. Rodriguez 	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
413b2e1b302SLuis R. Rodriguez 
414bd05f28eSRoel Kluin 	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
415bd05f28eSRoel Kluin 			freq_range->max_bandwidth_khz > freq_diff)
416b2e1b302SLuis R. Rodriguez 		return false;
417b2e1b302SLuis R. Rodriguez 
418b2e1b302SLuis R. Rodriguez 	return true;
419b2e1b302SLuis R. Rodriguez }
420b2e1b302SLuis R. Rodriguez 
421a3d2eaf0SJohannes Berg static bool is_valid_rd(const struct ieee80211_regdomain *rd)
422b2e1b302SLuis R. Rodriguez {
423a3d2eaf0SJohannes Berg 	const struct ieee80211_reg_rule *reg_rule = NULL;
424b2e1b302SLuis R. Rodriguez 	unsigned int i;
425b2e1b302SLuis R. Rodriguez 
426b2e1b302SLuis R. Rodriguez 	if (!rd->n_reg_rules)
427b2e1b302SLuis R. Rodriguez 		return false;
428b2e1b302SLuis R. Rodriguez 
42988dc1c3fSLuis R. Rodriguez 	if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
43088dc1c3fSLuis R. Rodriguez 		return false;
43188dc1c3fSLuis R. Rodriguez 
432b2e1b302SLuis R. Rodriguez 	for (i = 0; i < rd->n_reg_rules; i++) {
433b2e1b302SLuis R. Rodriguez 		reg_rule = &rd->reg_rules[i];
434b2e1b302SLuis R. Rodriguez 		if (!is_valid_reg_rule(reg_rule))
435b2e1b302SLuis R. Rodriguez 			return false;
436b2e1b302SLuis R. Rodriguez 	}
437b2e1b302SLuis R. Rodriguez 
438b2e1b302SLuis R. Rodriguez 	return true;
439b2e1b302SLuis R. Rodriguez }
440b2e1b302SLuis R. Rodriguez 
441038659e7SLuis R. Rodriguez static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
442038659e7SLuis R. Rodriguez 			    u32 center_freq_khz,
443038659e7SLuis R. Rodriguez 			    u32 bw_khz)
444b2e1b302SLuis R. Rodriguez {
445038659e7SLuis R. Rodriguez 	u32 start_freq_khz, end_freq_khz;
446038659e7SLuis R. Rodriguez 
447038659e7SLuis R. Rodriguez 	start_freq_khz = center_freq_khz - (bw_khz/2);
448038659e7SLuis R. Rodriguez 	end_freq_khz = center_freq_khz + (bw_khz/2);
449038659e7SLuis R. Rodriguez 
450b2e1b302SLuis R. Rodriguez 	if (start_freq_khz >= freq_range->start_freq_khz &&
451b2e1b302SLuis R. Rodriguez 	    end_freq_khz <= freq_range->end_freq_khz)
452038659e7SLuis R. Rodriguez 		return true;
453038659e7SLuis R. Rodriguez 
454038659e7SLuis R. Rodriguez 	return false;
455b2e1b302SLuis R. Rodriguez }
456b2e1b302SLuis R. Rodriguez 
4570c7dc45dSLuis R. Rodriguez /**
4580c7dc45dSLuis R. Rodriguez  * freq_in_rule_band - tells us if a frequency is in a frequency band
4590c7dc45dSLuis R. Rodriguez  * @freq_range: frequency rule we want to query
4600c7dc45dSLuis R. Rodriguez  * @freq_khz: frequency we are inquiring about
4610c7dc45dSLuis R. Rodriguez  *
4620c7dc45dSLuis R. Rodriguez  * This lets us know if a specific frequency rule is or is not relevant to
4630c7dc45dSLuis R. Rodriguez  * a specific frequency's band. Bands are device specific and artificial
4640c7dc45dSLuis R. Rodriguez  * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
4650c7dc45dSLuis R. Rodriguez  * safe for now to assume that a frequency rule should not be part of a
4660c7dc45dSLuis R. Rodriguez  * frequency's band if the start freq or end freq are off by more than 2 GHz.
4670c7dc45dSLuis R. Rodriguez  * This resolution can be lowered and should be considered as we add
4680c7dc45dSLuis R. Rodriguez  * regulatory rule support for other "bands".
4690c7dc45dSLuis R. Rodriguez  **/
4700c7dc45dSLuis R. Rodriguez static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
4710c7dc45dSLuis R. Rodriguez 	u32 freq_khz)
4720c7dc45dSLuis R. Rodriguez {
4730c7dc45dSLuis R. Rodriguez #define ONE_GHZ_IN_KHZ	1000000
4740c7dc45dSLuis R. Rodriguez 	if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
4750c7dc45dSLuis R. Rodriguez 		return true;
4760c7dc45dSLuis R. Rodriguez 	if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
4770c7dc45dSLuis R. Rodriguez 		return true;
4780c7dc45dSLuis R. Rodriguez 	return false;
4790c7dc45dSLuis R. Rodriguez #undef ONE_GHZ_IN_KHZ
4800c7dc45dSLuis R. Rodriguez }
4810c7dc45dSLuis R. Rodriguez 
482fb1fc7adSLuis R. Rodriguez /*
483fb1fc7adSLuis R. Rodriguez  * Helper for regdom_intersect(), this does the real
484fb1fc7adSLuis R. Rodriguez  * mathematical intersection fun
485fb1fc7adSLuis R. Rodriguez  */
4869c96477dSLuis R. Rodriguez static int reg_rules_intersect(
4879c96477dSLuis R. Rodriguez 	const struct ieee80211_reg_rule *rule1,
4889c96477dSLuis R. Rodriguez 	const struct ieee80211_reg_rule *rule2,
4899c96477dSLuis R. Rodriguez 	struct ieee80211_reg_rule *intersected_rule)
4909c96477dSLuis R. Rodriguez {
4919c96477dSLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range1, *freq_range2;
4929c96477dSLuis R. Rodriguez 	struct ieee80211_freq_range *freq_range;
4939c96477dSLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule1, *power_rule2;
4949c96477dSLuis R. Rodriguez 	struct ieee80211_power_rule *power_rule;
4959c96477dSLuis R. Rodriguez 	u32 freq_diff;
4969c96477dSLuis R. Rodriguez 
4979c96477dSLuis R. Rodriguez 	freq_range1 = &rule1->freq_range;
4989c96477dSLuis R. Rodriguez 	freq_range2 = &rule2->freq_range;
4999c96477dSLuis R. Rodriguez 	freq_range = &intersected_rule->freq_range;
5009c96477dSLuis R. Rodriguez 
5019c96477dSLuis R. Rodriguez 	power_rule1 = &rule1->power_rule;
5029c96477dSLuis R. Rodriguez 	power_rule2 = &rule2->power_rule;
5039c96477dSLuis R. Rodriguez 	power_rule = &intersected_rule->power_rule;
5049c96477dSLuis R. Rodriguez 
5059c96477dSLuis R. Rodriguez 	freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
5069c96477dSLuis R. Rodriguez 		freq_range2->start_freq_khz);
5079c96477dSLuis R. Rodriguez 	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
5089c96477dSLuis R. Rodriguez 		freq_range2->end_freq_khz);
5099c96477dSLuis R. Rodriguez 	freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
5109c96477dSLuis R. Rodriguez 		freq_range2->max_bandwidth_khz);
5119c96477dSLuis R. Rodriguez 
5129c96477dSLuis R. Rodriguez 	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
5139c96477dSLuis R. Rodriguez 	if (freq_range->max_bandwidth_khz > freq_diff)
5149c96477dSLuis R. Rodriguez 		freq_range->max_bandwidth_khz = freq_diff;
5159c96477dSLuis R. Rodriguez 
5169c96477dSLuis R. Rodriguez 	power_rule->max_eirp = min(power_rule1->max_eirp,
5179c96477dSLuis R. Rodriguez 		power_rule2->max_eirp);
5189c96477dSLuis R. Rodriguez 	power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
5199c96477dSLuis R. Rodriguez 		power_rule2->max_antenna_gain);
5209c96477dSLuis R. Rodriguez 
5219c96477dSLuis R. Rodriguez 	intersected_rule->flags = (rule1->flags | rule2->flags);
5229c96477dSLuis R. Rodriguez 
5239c96477dSLuis R. Rodriguez 	if (!is_valid_reg_rule(intersected_rule))
5249c96477dSLuis R. Rodriguez 		return -EINVAL;
5259c96477dSLuis R. Rodriguez 
5269c96477dSLuis R. Rodriguez 	return 0;
5279c96477dSLuis R. Rodriguez }
5289c96477dSLuis R. Rodriguez 
5299c96477dSLuis R. Rodriguez /**
5309c96477dSLuis R. Rodriguez  * regdom_intersect - do the intersection between two regulatory domains
5319c96477dSLuis R. Rodriguez  * @rd1: first regulatory domain
5329c96477dSLuis R. Rodriguez  * @rd2: second regulatory domain
5339c96477dSLuis R. Rodriguez  *
5349c96477dSLuis R. Rodriguez  * Use this function to get the intersection between two regulatory domains.
5359c96477dSLuis R. Rodriguez  * Once completed we will mark the alpha2 for the rd as intersected, "98",
5369c96477dSLuis R. Rodriguez  * as no one single alpha2 can represent this regulatory domain.
5379c96477dSLuis R. Rodriguez  *
5389c96477dSLuis R. Rodriguez  * Returns a pointer to the regulatory domain structure which will hold the
5399c96477dSLuis R. Rodriguez  * resulting intersection of rules between rd1 and rd2. We will
5409c96477dSLuis R. Rodriguez  * kzalloc() this structure for you.
5419c96477dSLuis R. Rodriguez  */
5429c96477dSLuis R. Rodriguez static struct ieee80211_regdomain *regdom_intersect(
5439c96477dSLuis R. Rodriguez 	const struct ieee80211_regdomain *rd1,
5449c96477dSLuis R. Rodriguez 	const struct ieee80211_regdomain *rd2)
5459c96477dSLuis R. Rodriguez {
5469c96477dSLuis R. Rodriguez 	int r, size_of_regd;
5479c96477dSLuis R. Rodriguez 	unsigned int x, y;
5489c96477dSLuis R. Rodriguez 	unsigned int num_rules = 0, rule_idx = 0;
5499c96477dSLuis R. Rodriguez 	const struct ieee80211_reg_rule *rule1, *rule2;
5509c96477dSLuis R. Rodriguez 	struct ieee80211_reg_rule *intersected_rule;
5519c96477dSLuis R. Rodriguez 	struct ieee80211_regdomain *rd;
5529c96477dSLuis R. Rodriguez 	/* This is just a dummy holder to help us count */
5539c96477dSLuis R. Rodriguez 	struct ieee80211_reg_rule irule;
5549c96477dSLuis R. Rodriguez 
5559c96477dSLuis R. Rodriguez 	/* Uses the stack temporarily for counter arithmetic */
5569c96477dSLuis R. Rodriguez 	intersected_rule = &irule;
5579c96477dSLuis R. Rodriguez 
5589c96477dSLuis R. Rodriguez 	memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
5599c96477dSLuis R. Rodriguez 
5609c96477dSLuis R. Rodriguez 	if (!rd1 || !rd2)
5619c96477dSLuis R. Rodriguez 		return NULL;
5629c96477dSLuis R. Rodriguez 
563fb1fc7adSLuis R. Rodriguez 	/*
564fb1fc7adSLuis R. Rodriguez 	 * First we get a count of the rules we'll need, then we actually
5659c96477dSLuis R. Rodriguez 	 * build them. This is to so we can malloc() and free() a
5669c96477dSLuis R. Rodriguez 	 * regdomain once. The reason we use reg_rules_intersect() here
5679c96477dSLuis R. Rodriguez 	 * is it will return -EINVAL if the rule computed makes no sense.
568fb1fc7adSLuis R. Rodriguez 	 * All rules that do check out OK are valid.
569fb1fc7adSLuis R. Rodriguez 	 */
5709c96477dSLuis R. Rodriguez 
5719c96477dSLuis R. Rodriguez 	for (x = 0; x < rd1->n_reg_rules; x++) {
5729c96477dSLuis R. Rodriguez 		rule1 = &rd1->reg_rules[x];
5739c96477dSLuis R. Rodriguez 		for (y = 0; y < rd2->n_reg_rules; y++) {
5749c96477dSLuis R. Rodriguez 			rule2 = &rd2->reg_rules[y];
5759c96477dSLuis R. Rodriguez 			if (!reg_rules_intersect(rule1, rule2,
5769c96477dSLuis R. Rodriguez 					intersected_rule))
5779c96477dSLuis R. Rodriguez 				num_rules++;
5789c96477dSLuis R. Rodriguez 			memset(intersected_rule, 0,
5799c96477dSLuis R. Rodriguez 					sizeof(struct ieee80211_reg_rule));
5809c96477dSLuis R. Rodriguez 		}
5819c96477dSLuis R. Rodriguez 	}
5829c96477dSLuis R. Rodriguez 
5839c96477dSLuis R. Rodriguez 	if (!num_rules)
5849c96477dSLuis R. Rodriguez 		return NULL;
5859c96477dSLuis R. Rodriguez 
5869c96477dSLuis R. Rodriguez 	size_of_regd = sizeof(struct ieee80211_regdomain) +
5879c96477dSLuis R. Rodriguez 		((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
5889c96477dSLuis R. Rodriguez 
5899c96477dSLuis R. Rodriguez 	rd = kzalloc(size_of_regd, GFP_KERNEL);
5909c96477dSLuis R. Rodriguez 	if (!rd)
5919c96477dSLuis R. Rodriguez 		return NULL;
5929c96477dSLuis R. Rodriguez 
5939c96477dSLuis R. Rodriguez 	for (x = 0; x < rd1->n_reg_rules; x++) {
5949c96477dSLuis R. Rodriguez 		rule1 = &rd1->reg_rules[x];
5959c96477dSLuis R. Rodriguez 		for (y = 0; y < rd2->n_reg_rules; y++) {
5969c96477dSLuis R. Rodriguez 			rule2 = &rd2->reg_rules[y];
597fb1fc7adSLuis R. Rodriguez 			/*
598fb1fc7adSLuis R. Rodriguez 			 * This time around instead of using the stack lets
5999c96477dSLuis R. Rodriguez 			 * write to the target rule directly saving ourselves
600fb1fc7adSLuis R. Rodriguez 			 * a memcpy()
601fb1fc7adSLuis R. Rodriguez 			 */
6029c96477dSLuis R. Rodriguez 			intersected_rule = &rd->reg_rules[rule_idx];
6039c96477dSLuis R. Rodriguez 			r = reg_rules_intersect(rule1, rule2,
6049c96477dSLuis R. Rodriguez 				intersected_rule);
605fb1fc7adSLuis R. Rodriguez 			/*
606fb1fc7adSLuis R. Rodriguez 			 * No need to memset here the intersected rule here as
607fb1fc7adSLuis R. Rodriguez 			 * we're not using the stack anymore
608fb1fc7adSLuis R. Rodriguez 			 */
6099c96477dSLuis R. Rodriguez 			if (r)
6109c96477dSLuis R. Rodriguez 				continue;
6119c96477dSLuis R. Rodriguez 			rule_idx++;
6129c96477dSLuis R. Rodriguez 		}
6139c96477dSLuis R. Rodriguez 	}
6149c96477dSLuis R. Rodriguez 
6159c96477dSLuis R. Rodriguez 	if (rule_idx != num_rules) {
6169c96477dSLuis R. Rodriguez 		kfree(rd);
6179c96477dSLuis R. Rodriguez 		return NULL;
6189c96477dSLuis R. Rodriguez 	}
6199c96477dSLuis R. Rodriguez 
6209c96477dSLuis R. Rodriguez 	rd->n_reg_rules = num_rules;
6219c96477dSLuis R. Rodriguez 	rd->alpha2[0] = '9';
6229c96477dSLuis R. Rodriguez 	rd->alpha2[1] = '8';
6239c96477dSLuis R. Rodriguez 
6249c96477dSLuis R. Rodriguez 	return rd;
6259c96477dSLuis R. Rodriguez }
6269c96477dSLuis R. Rodriguez 
627fb1fc7adSLuis R. Rodriguez /*
628fb1fc7adSLuis R. Rodriguez  * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
629fb1fc7adSLuis R. Rodriguez  * want to just have the channel structure use these
630fb1fc7adSLuis R. Rodriguez  */
631b2e1b302SLuis R. Rodriguez static u32 map_regdom_flags(u32 rd_flags)
632b2e1b302SLuis R. Rodriguez {
633b2e1b302SLuis R. Rodriguez 	u32 channel_flags = 0;
634b2e1b302SLuis R. Rodriguez 	if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
635b2e1b302SLuis R. Rodriguez 		channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
636b2e1b302SLuis R. Rodriguez 	if (rd_flags & NL80211_RRF_NO_IBSS)
637b2e1b302SLuis R. Rodriguez 		channel_flags |= IEEE80211_CHAN_NO_IBSS;
638b2e1b302SLuis R. Rodriguez 	if (rd_flags & NL80211_RRF_DFS)
639b2e1b302SLuis R. Rodriguez 		channel_flags |= IEEE80211_CHAN_RADAR;
640b2e1b302SLuis R. Rodriguez 	return channel_flags;
641b2e1b302SLuis R. Rodriguez }
642b2e1b302SLuis R. Rodriguez 
6431fa25e41SLuis R. Rodriguez static int freq_reg_info_regd(struct wiphy *wiphy,
6441fa25e41SLuis R. Rodriguez 			      u32 center_freq,
645038659e7SLuis R. Rodriguez 			      u32 desired_bw_khz,
6461fa25e41SLuis R. Rodriguez 			      const struct ieee80211_reg_rule **reg_rule,
6471fa25e41SLuis R. Rodriguez 			      const struct ieee80211_regdomain *custom_regd)
6488318d78aSJohannes Berg {
6498318d78aSJohannes Berg 	int i;
6500c7dc45dSLuis R. Rodriguez 	bool band_rule_found = false;
6513e0c3ff3SLuis R. Rodriguez 	const struct ieee80211_regdomain *regd;
652038659e7SLuis R. Rodriguez 	bool bw_fits = false;
653038659e7SLuis R. Rodriguez 
654038659e7SLuis R. Rodriguez 	if (!desired_bw_khz)
655038659e7SLuis R. Rodriguez 		desired_bw_khz = MHZ_TO_KHZ(20);
6568318d78aSJohannes Berg 
6571fa25e41SLuis R. Rodriguez 	regd = custom_regd ? custom_regd : cfg80211_regdomain;
6583e0c3ff3SLuis R. Rodriguez 
659fb1fc7adSLuis R. Rodriguez 	/*
660fb1fc7adSLuis R. Rodriguez 	 * Follow the driver's regulatory domain, if present, unless a country
661fb1fc7adSLuis R. Rodriguez 	 * IE has been processed or a user wants to help complaince further
662fb1fc7adSLuis R. Rodriguez 	 */
6632784fe91SLuis R. Rodriguez 	if (!custom_regd &&
6642784fe91SLuis R. Rodriguez 	    last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
6657db90f4aSLuis R. Rodriguez 	    last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
6663e0c3ff3SLuis R. Rodriguez 	    wiphy->regd)
6673e0c3ff3SLuis R. Rodriguez 		regd = wiphy->regd;
6683e0c3ff3SLuis R. Rodriguez 
6693e0c3ff3SLuis R. Rodriguez 	if (!regd)
670b2e1b302SLuis R. Rodriguez 		return -EINVAL;
671b2e1b302SLuis R. Rodriguez 
6723e0c3ff3SLuis R. Rodriguez 	for (i = 0; i < regd->n_reg_rules; i++) {
673b2e1b302SLuis R. Rodriguez 		const struct ieee80211_reg_rule *rr;
674b2e1b302SLuis R. Rodriguez 		const struct ieee80211_freq_range *fr = NULL;
675b2e1b302SLuis R. Rodriguez 		const struct ieee80211_power_rule *pr = NULL;
676b2e1b302SLuis R. Rodriguez 
6773e0c3ff3SLuis R. Rodriguez 		rr = &regd->reg_rules[i];
678b2e1b302SLuis R. Rodriguez 		fr = &rr->freq_range;
679b2e1b302SLuis R. Rodriguez 		pr = &rr->power_rule;
6800c7dc45dSLuis R. Rodriguez 
681fb1fc7adSLuis R. Rodriguez 		/*
682fb1fc7adSLuis R. Rodriguez 		 * We only need to know if one frequency rule was
6830c7dc45dSLuis R. Rodriguez 		 * was in center_freq's band, that's enough, so lets
684fb1fc7adSLuis R. Rodriguez 		 * not overwrite it once found
685fb1fc7adSLuis R. Rodriguez 		 */
6860c7dc45dSLuis R. Rodriguez 		if (!band_rule_found)
6870c7dc45dSLuis R. Rodriguez 			band_rule_found = freq_in_rule_band(fr, center_freq);
6880c7dc45dSLuis R. Rodriguez 
689038659e7SLuis R. Rodriguez 		bw_fits = reg_does_bw_fit(fr,
690038659e7SLuis R. Rodriguez 					  center_freq,
691038659e7SLuis R. Rodriguez 					  desired_bw_khz);
6920c7dc45dSLuis R. Rodriguez 
693038659e7SLuis R. Rodriguez 		if (band_rule_found && bw_fits) {
694b2e1b302SLuis R. Rodriguez 			*reg_rule = rr;
695038659e7SLuis R. Rodriguez 			return 0;
6968318d78aSJohannes Berg 		}
6978318d78aSJohannes Berg 	}
6988318d78aSJohannes Berg 
6990c7dc45dSLuis R. Rodriguez 	if (!band_rule_found)
7000c7dc45dSLuis R. Rodriguez 		return -ERANGE;
7010c7dc45dSLuis R. Rodriguez 
702038659e7SLuis R. Rodriguez 	return -EINVAL;
703b2e1b302SLuis R. Rodriguez }
704b2e1b302SLuis R. Rodriguez 
705038659e7SLuis R. Rodriguez int freq_reg_info(struct wiphy *wiphy,
706038659e7SLuis R. Rodriguez 		  u32 center_freq,
707038659e7SLuis R. Rodriguez 		  u32 desired_bw_khz,
7081fa25e41SLuis R. Rodriguez 		  const struct ieee80211_reg_rule **reg_rule)
7091fa25e41SLuis R. Rodriguez {
710ac46d48eSLuis R. Rodriguez 	assert_cfg80211_lock();
711038659e7SLuis R. Rodriguez 	return freq_reg_info_regd(wiphy,
712038659e7SLuis R. Rodriguez 				  center_freq,
713038659e7SLuis R. Rodriguez 				  desired_bw_khz,
714038659e7SLuis R. Rodriguez 				  reg_rule,
715038659e7SLuis R. Rodriguez 				  NULL);
7161fa25e41SLuis R. Rodriguez }
7174f366c5dSJohn W. Linville EXPORT_SYMBOL(freq_reg_info);
718b2e1b302SLuis R. Rodriguez 
719926a0a09SLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
720926a0a09SLuis R. Rodriguez static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
721926a0a09SLuis R. Rodriguez {
722926a0a09SLuis R. Rodriguez 	switch (initiator) {
723926a0a09SLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_CORE:
724926a0a09SLuis R. Rodriguez 		return "Set by core";
725926a0a09SLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_USER:
726926a0a09SLuis R. Rodriguez 		return "Set by user";
727926a0a09SLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_DRIVER:
728926a0a09SLuis R. Rodriguez 		return "Set by driver";
729926a0a09SLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
730926a0a09SLuis R. Rodriguez 		return "Set by country IE";
731926a0a09SLuis R. Rodriguez 	default:
732926a0a09SLuis R. Rodriguez 		WARN_ON(1);
733926a0a09SLuis R. Rodriguez 		return "Set by bug";
734926a0a09SLuis R. Rodriguez 	}
735926a0a09SLuis R. Rodriguez }
736e702d3cfSLuis R. Rodriguez 
737e702d3cfSLuis R. Rodriguez static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
738e702d3cfSLuis R. Rodriguez 				    u32 desired_bw_khz,
739e702d3cfSLuis R. Rodriguez 				    const struct ieee80211_reg_rule *reg_rule)
740e702d3cfSLuis R. Rodriguez {
741e702d3cfSLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule;
742e702d3cfSLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range;
743e702d3cfSLuis R. Rodriguez 	char max_antenna_gain[32];
744e702d3cfSLuis R. Rodriguez 
745e702d3cfSLuis R. Rodriguez 	power_rule = &reg_rule->power_rule;
746e702d3cfSLuis R. Rodriguez 	freq_range = &reg_rule->freq_range;
747e702d3cfSLuis R. Rodriguez 
748e702d3cfSLuis R. Rodriguez 	if (!power_rule->max_antenna_gain)
749e702d3cfSLuis R. Rodriguez 		snprintf(max_antenna_gain, 32, "N/A");
750e702d3cfSLuis R. Rodriguez 	else
751e702d3cfSLuis R. Rodriguez 		snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain);
752e702d3cfSLuis R. Rodriguez 
753d91e41b6SLuis R. Rodriguez 	REG_DBG_PRINT("Updating information on frequency %d MHz "
754ff039c6fSBob Copeland 		      "for a %d MHz width channel with regulatory rule:\n",
755e702d3cfSLuis R. Rodriguez 		      chan->center_freq,
756e702d3cfSLuis R. Rodriguez 		      KHZ_TO_MHZ(desired_bw_khz));
757e702d3cfSLuis R. Rodriguez 
758d91e41b6SLuis R. Rodriguez 	REG_DBG_PRINT("%d KHz - %d KHz @  KHz), (%s mBi, %d mBm)\n",
759e702d3cfSLuis R. Rodriguez 		      freq_range->start_freq_khz,
760e702d3cfSLuis R. Rodriguez 		      freq_range->end_freq_khz,
761e702d3cfSLuis R. Rodriguez 		      max_antenna_gain,
762e702d3cfSLuis R. Rodriguez 		      power_rule->max_eirp);
763e702d3cfSLuis R. Rodriguez }
764e702d3cfSLuis R. Rodriguez #else
765e702d3cfSLuis R. Rodriguez static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
766e702d3cfSLuis R. Rodriguez 				    u32 desired_bw_khz,
767e702d3cfSLuis R. Rodriguez 				    const struct ieee80211_reg_rule *reg_rule)
768e702d3cfSLuis R. Rodriguez {
769e702d3cfSLuis R. Rodriguez 	return;
770e702d3cfSLuis R. Rodriguez }
771926a0a09SLuis R. Rodriguez #endif
772926a0a09SLuis R. Rodriguez 
773038659e7SLuis R. Rodriguez /*
774038659e7SLuis R. Rodriguez  * Note that right now we assume the desired channel bandwidth
775038659e7SLuis R. Rodriguez  * is always 20 MHz for each individual channel (HT40 uses 20 MHz
776038659e7SLuis R. Rodriguez  * per channel, the primary and the extension channel). To support
777038659e7SLuis R. Rodriguez  * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
778038659e7SLuis R. Rodriguez  * new ieee80211_channel.target_bw and re run the regulatory check
779038659e7SLuis R. Rodriguez  * on the wiphy with the target_bw specified. Then we can simply use
780038659e7SLuis R. Rodriguez  * that below for the desired_bw_khz below.
781038659e7SLuis R. Rodriguez  */
7827ca43d03SLuis R. Rodriguez static void handle_channel(struct wiphy *wiphy,
7837ca43d03SLuis R. Rodriguez 			   enum nl80211_reg_initiator initiator,
7847ca43d03SLuis R. Rodriguez 			   enum ieee80211_band band,
785a92a3ce7SLuis R. Rodriguez 			   unsigned int chan_idx)
786b2e1b302SLuis R. Rodriguez {
787b2e1b302SLuis R. Rodriguez 	int r;
788038659e7SLuis R. Rodriguez 	u32 flags, bw_flags = 0;
789038659e7SLuis R. Rodriguez 	u32 desired_bw_khz = MHZ_TO_KHZ(20);
790b2e1b302SLuis R. Rodriguez 	const struct ieee80211_reg_rule *reg_rule = NULL;
791b2e1b302SLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule = NULL;
792038659e7SLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range = NULL;
793a92a3ce7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
794a92a3ce7SLuis R. Rodriguez 	struct ieee80211_channel *chan;
795fe33eb39SLuis R. Rodriguez 	struct wiphy *request_wiphy = NULL;
796a92a3ce7SLuis R. Rodriguez 
797761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
798761cf7ecSLuis R. Rodriguez 
799806a9e39SLuis R. Rodriguez 	request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
800806a9e39SLuis R. Rodriguez 
801a92a3ce7SLuis R. Rodriguez 	sband = wiphy->bands[band];
802a92a3ce7SLuis R. Rodriguez 	BUG_ON(chan_idx >= sband->n_channels);
803a92a3ce7SLuis R. Rodriguez 	chan = &sband->channels[chan_idx];
804a92a3ce7SLuis R. Rodriguez 
805a92a3ce7SLuis R. Rodriguez 	flags = chan->orig_flags;
806b2e1b302SLuis R. Rodriguez 
807038659e7SLuis R. Rodriguez 	r = freq_reg_info(wiphy,
808038659e7SLuis R. Rodriguez 			  MHZ_TO_KHZ(chan->center_freq),
809038659e7SLuis R. Rodriguez 			  desired_bw_khz,
810038659e7SLuis R. Rodriguez 			  &reg_rule);
811b2e1b302SLuis R. Rodriguez 
812ca4ffe8fSLuis R. Rodriguez 	if (r) {
813ca4ffe8fSLuis R. Rodriguez 		/*
814ca4ffe8fSLuis R. Rodriguez 		 * We will disable all channels that do not match our
815ca4ffe8fSLuis R. Rodriguez 		 * recieved regulatory rule unless the hint is coming
816ca4ffe8fSLuis R. Rodriguez 		 * from a Country IE and the Country IE had no information
817ca4ffe8fSLuis R. Rodriguez 		 * about a band. The IEEE 802.11 spec allows for an AP
818ca4ffe8fSLuis R. Rodriguez 		 * to send only a subset of the regulatory rules allowed,
819ca4ffe8fSLuis R. Rodriguez 		 * so an AP in the US that only supports 2.4 GHz may only send
820ca4ffe8fSLuis R. Rodriguez 		 * a country IE with information for the 2.4 GHz band
821ca4ffe8fSLuis R. Rodriguez 		 * while 5 GHz is still supported.
822ca4ffe8fSLuis R. Rodriguez 		 */
823ca4ffe8fSLuis R. Rodriguez 		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
824ca4ffe8fSLuis R. Rodriguez 		    r == -ERANGE)
8258318d78aSJohannes Berg 			return;
8268318d78aSJohannes Berg 
827d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
828ca4ffe8fSLuis R. Rodriguez 		chan->flags = IEEE80211_CHAN_DISABLED;
829ca4ffe8fSLuis R. Rodriguez 		return;
830ca4ffe8fSLuis R. Rodriguez 	}
831ca4ffe8fSLuis R. Rodriguez 
832e702d3cfSLuis R. Rodriguez 	chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
833e702d3cfSLuis R. Rodriguez 
834b2e1b302SLuis R. Rodriguez 	power_rule = &reg_rule->power_rule;
835038659e7SLuis R. Rodriguez 	freq_range = &reg_rule->freq_range;
836038659e7SLuis R. Rodriguez 
837038659e7SLuis R. Rodriguez 	if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
838038659e7SLuis R. Rodriguez 		bw_flags = IEEE80211_CHAN_NO_HT40;
839b2e1b302SLuis R. Rodriguez 
8407db90f4aSLuis R. Rodriguez 	if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
841806a9e39SLuis R. Rodriguez 	    request_wiphy && request_wiphy == wiphy &&
8425be83de5SJohannes Berg 	    request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
843fb1fc7adSLuis R. Rodriguez 		/*
844fb1fc7adSLuis R. Rodriguez 		 * This gaurantees the driver's requested regulatory domain
845f976376dSLuis R. Rodriguez 		 * will always be used as a base for further regulatory
846fb1fc7adSLuis R. Rodriguez 		 * settings
847fb1fc7adSLuis R. Rodriguez 		 */
848f976376dSLuis R. Rodriguez 		chan->flags = chan->orig_flags =
849038659e7SLuis R. Rodriguez 			map_regdom_flags(reg_rule->flags) | bw_flags;
850f976376dSLuis R. Rodriguez 		chan->max_antenna_gain = chan->orig_mag =
851f976376dSLuis R. Rodriguez 			(int) MBI_TO_DBI(power_rule->max_antenna_gain);
852f976376dSLuis R. Rodriguez 		chan->max_power = chan->orig_mpwr =
853f976376dSLuis R. Rodriguez 			(int) MBM_TO_DBM(power_rule->max_eirp);
854f976376dSLuis R. Rodriguez 		return;
855f976376dSLuis R. Rodriguez 	}
856f976376dSLuis R. Rodriguez 
857038659e7SLuis R. Rodriguez 	chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
8588318d78aSJohannes Berg 	chan->max_antenna_gain = min(chan->orig_mag,
859b2e1b302SLuis R. Rodriguez 		(int) MBI_TO_DBI(power_rule->max_antenna_gain));
860253898c4SJohn W. Linville 	if (chan->orig_mpwr)
861b2e1b302SLuis R. Rodriguez 		chan->max_power = min(chan->orig_mpwr,
862b2e1b302SLuis R. Rodriguez 			(int) MBM_TO_DBM(power_rule->max_eirp));
863253898c4SJohn W. Linville 	else
864b2e1b302SLuis R. Rodriguez 		chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
8658318d78aSJohannes Berg }
8668318d78aSJohannes Berg 
8677ca43d03SLuis R. Rodriguez static void handle_band(struct wiphy *wiphy,
8687ca43d03SLuis R. Rodriguez 			enum ieee80211_band band,
8697ca43d03SLuis R. Rodriguez 			enum nl80211_reg_initiator initiator)
8708318d78aSJohannes Berg {
871a92a3ce7SLuis R. Rodriguez 	unsigned int i;
872a92a3ce7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
873a92a3ce7SLuis R. Rodriguez 
874a92a3ce7SLuis R. Rodriguez 	BUG_ON(!wiphy->bands[band]);
875a92a3ce7SLuis R. Rodriguez 	sband = wiphy->bands[band];
8768318d78aSJohannes Berg 
8778318d78aSJohannes Berg 	for (i = 0; i < sband->n_channels; i++)
8787ca43d03SLuis R. Rodriguez 		handle_channel(wiphy, initiator, band, i);
8798318d78aSJohannes Berg }
8808318d78aSJohannes Berg 
8817db90f4aSLuis R. Rodriguez static bool ignore_reg_update(struct wiphy *wiphy,
8827db90f4aSLuis R. Rodriguez 			      enum nl80211_reg_initiator initiator)
88314b9815aSLuis R. Rodriguez {
884926a0a09SLuis R. Rodriguez 	if (!last_request) {
885d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Ignoring regulatory request %s since "
886926a0a09SLuis R. Rodriguez 			      "last_request is not set\n",
887926a0a09SLuis R. Rodriguez 			      reg_initiator_name(initiator));
88814b9815aSLuis R. Rodriguez 		return true;
889926a0a09SLuis R. Rodriguez 	}
890926a0a09SLuis R. Rodriguez 
8917db90f4aSLuis R. Rodriguez 	if (initiator == NL80211_REGDOM_SET_BY_CORE &&
892926a0a09SLuis R. Rodriguez 	    wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
893d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Ignoring regulatory request %s "
894926a0a09SLuis R. Rodriguez 			      "since the driver uses its own custom "
895926a0a09SLuis R. Rodriguez 			      "regulatory domain ",
896926a0a09SLuis R. Rodriguez 			      reg_initiator_name(initiator));
89714b9815aSLuis R. Rodriguez 		return true;
898926a0a09SLuis R. Rodriguez 	}
899926a0a09SLuis R. Rodriguez 
900fb1fc7adSLuis R. Rodriguez 	/*
901fb1fc7adSLuis R. Rodriguez 	 * wiphy->regd will be set once the device has its own
902fb1fc7adSLuis R. Rodriguez 	 * desired regulatory domain set
903fb1fc7adSLuis R. Rodriguez 	 */
9045be83de5SJohannes Berg 	if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
905749b527bSLuis R. Rodriguez 	    initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
906926a0a09SLuis R. Rodriguez 	    !is_world_regdom(last_request->alpha2)) {
907d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Ignoring regulatory request %s "
908926a0a09SLuis R. Rodriguez 			      "since the driver requires its own regulaotry "
909926a0a09SLuis R. Rodriguez 			      "domain to be set first",
910926a0a09SLuis R. Rodriguez 			      reg_initiator_name(initiator));
91114b9815aSLuis R. Rodriguez 		return true;
912926a0a09SLuis R. Rodriguez 	}
913926a0a09SLuis R. Rodriguez 
91414b9815aSLuis R. Rodriguez 	return false;
91514b9815aSLuis R. Rodriguez }
91614b9815aSLuis R. Rodriguez 
9177db90f4aSLuis R. Rodriguez static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
918b2e1b302SLuis R. Rodriguez {
91979c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev;
920b2e1b302SLuis R. Rodriguez 
92179c97e97SJohannes Berg 	list_for_each_entry(rdev, &cfg80211_rdev_list, list)
92279c97e97SJohannes Berg 		wiphy_update_regulatory(&rdev->wiphy, initiator);
923b2e1b302SLuis R. Rodriguez }
924b2e1b302SLuis R. Rodriguez 
925e38f8a7aSLuis R. Rodriguez static void handle_reg_beacon(struct wiphy *wiphy,
926e38f8a7aSLuis R. Rodriguez 			      unsigned int chan_idx,
927e38f8a7aSLuis R. Rodriguez 			      struct reg_beacon *reg_beacon)
928e38f8a7aSLuis R. Rodriguez {
929e38f8a7aSLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
930e38f8a7aSLuis R. Rodriguez 	struct ieee80211_channel *chan;
9316bad8766SLuis R. Rodriguez 	bool channel_changed = false;
9326bad8766SLuis R. Rodriguez 	struct ieee80211_channel chan_before;
933e38f8a7aSLuis R. Rodriguez 
934e38f8a7aSLuis R. Rodriguez 	assert_cfg80211_lock();
935e38f8a7aSLuis R. Rodriguez 
936e38f8a7aSLuis R. Rodriguez 	sband = wiphy->bands[reg_beacon->chan.band];
937e38f8a7aSLuis R. Rodriguez 	chan = &sband->channels[chan_idx];
938e38f8a7aSLuis R. Rodriguez 
939e38f8a7aSLuis R. Rodriguez 	if (likely(chan->center_freq != reg_beacon->chan.center_freq))
940e38f8a7aSLuis R. Rodriguez 		return;
941e38f8a7aSLuis R. Rodriguez 
9426bad8766SLuis R. Rodriguez 	if (chan->beacon_found)
9436bad8766SLuis R. Rodriguez 		return;
9446bad8766SLuis R. Rodriguez 
9456bad8766SLuis R. Rodriguez 	chan->beacon_found = true;
9466bad8766SLuis R. Rodriguez 
9475be83de5SJohannes Berg 	if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
94837184244SLuis R. Rodriguez 		return;
94937184244SLuis R. Rodriguez 
9506bad8766SLuis R. Rodriguez 	chan_before.center_freq = chan->center_freq;
9516bad8766SLuis R. Rodriguez 	chan_before.flags = chan->flags;
9526bad8766SLuis R. Rodriguez 
95337184244SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
954e38f8a7aSLuis R. Rodriguez 		chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
9556bad8766SLuis R. Rodriguez 		channel_changed = true;
956e38f8a7aSLuis R. Rodriguez 	}
957e38f8a7aSLuis R. Rodriguez 
95837184244SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
959e38f8a7aSLuis R. Rodriguez 		chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
9606bad8766SLuis R. Rodriguez 		channel_changed = true;
961e38f8a7aSLuis R. Rodriguez 	}
962e38f8a7aSLuis R. Rodriguez 
9636bad8766SLuis R. Rodriguez 	if (channel_changed)
9646bad8766SLuis R. Rodriguez 		nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
965e38f8a7aSLuis R. Rodriguez }
966e38f8a7aSLuis R. Rodriguez 
967e38f8a7aSLuis R. Rodriguez /*
968e38f8a7aSLuis R. Rodriguez  * Called when a scan on a wiphy finds a beacon on
969e38f8a7aSLuis R. Rodriguez  * new channel
970e38f8a7aSLuis R. Rodriguez  */
971e38f8a7aSLuis R. Rodriguez static void wiphy_update_new_beacon(struct wiphy *wiphy,
972e38f8a7aSLuis R. Rodriguez 				    struct reg_beacon *reg_beacon)
973e38f8a7aSLuis R. Rodriguez {
974e38f8a7aSLuis R. Rodriguez 	unsigned int i;
975e38f8a7aSLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
976e38f8a7aSLuis R. Rodriguez 
977e38f8a7aSLuis R. Rodriguez 	assert_cfg80211_lock();
978e38f8a7aSLuis R. Rodriguez 
979e38f8a7aSLuis R. Rodriguez 	if (!wiphy->bands[reg_beacon->chan.band])
980e38f8a7aSLuis R. Rodriguez 		return;
981e38f8a7aSLuis R. Rodriguez 
982e38f8a7aSLuis R. Rodriguez 	sband = wiphy->bands[reg_beacon->chan.band];
983e38f8a7aSLuis R. Rodriguez 
984e38f8a7aSLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++)
985e38f8a7aSLuis R. Rodriguez 		handle_reg_beacon(wiphy, i, reg_beacon);
986e38f8a7aSLuis R. Rodriguez }
987e38f8a7aSLuis R. Rodriguez 
988e38f8a7aSLuis R. Rodriguez /*
989e38f8a7aSLuis R. Rodriguez  * Called upon reg changes or a new wiphy is added
990e38f8a7aSLuis R. Rodriguez  */
991e38f8a7aSLuis R. Rodriguez static void wiphy_update_beacon_reg(struct wiphy *wiphy)
992e38f8a7aSLuis R. Rodriguez {
993e38f8a7aSLuis R. Rodriguez 	unsigned int i;
994e38f8a7aSLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
995e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *reg_beacon;
996e38f8a7aSLuis R. Rodriguez 
997e38f8a7aSLuis R. Rodriguez 	assert_cfg80211_lock();
998e38f8a7aSLuis R. Rodriguez 
999e38f8a7aSLuis R. Rodriguez 	if (list_empty(&reg_beacon_list))
1000e38f8a7aSLuis R. Rodriguez 		return;
1001e38f8a7aSLuis R. Rodriguez 
1002e38f8a7aSLuis R. Rodriguez 	list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1003e38f8a7aSLuis R. Rodriguez 		if (!wiphy->bands[reg_beacon->chan.band])
1004e38f8a7aSLuis R. Rodriguez 			continue;
1005e38f8a7aSLuis R. Rodriguez 		sband = wiphy->bands[reg_beacon->chan.band];
1006e38f8a7aSLuis R. Rodriguez 		for (i = 0; i < sband->n_channels; i++)
1007e38f8a7aSLuis R. Rodriguez 			handle_reg_beacon(wiphy, i, reg_beacon);
1008e38f8a7aSLuis R. Rodriguez 	}
1009e38f8a7aSLuis R. Rodriguez }
1010e38f8a7aSLuis R. Rodriguez 
1011e38f8a7aSLuis R. Rodriguez static bool reg_is_world_roaming(struct wiphy *wiphy)
1012e38f8a7aSLuis R. Rodriguez {
1013e38f8a7aSLuis R. Rodriguez 	if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1014e38f8a7aSLuis R. Rodriguez 	    (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1015e38f8a7aSLuis R. Rodriguez 		return true;
1016b1ed8dddSLuis R. Rodriguez 	if (last_request &&
1017b1ed8dddSLuis R. Rodriguez 	    last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
10185be83de5SJohannes Berg 	    wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1019e38f8a7aSLuis R. Rodriguez 		return true;
1020e38f8a7aSLuis R. Rodriguez 	return false;
1021e38f8a7aSLuis R. Rodriguez }
1022e38f8a7aSLuis R. Rodriguez 
1023e38f8a7aSLuis R. Rodriguez /* Reap the advantages of previously found beacons */
1024e38f8a7aSLuis R. Rodriguez static void reg_process_beacons(struct wiphy *wiphy)
1025e38f8a7aSLuis R. Rodriguez {
1026b1ed8dddSLuis R. Rodriguez 	/*
1027b1ed8dddSLuis R. Rodriguez 	 * Means we are just firing up cfg80211, so no beacons would
1028b1ed8dddSLuis R. Rodriguez 	 * have been processed yet.
1029b1ed8dddSLuis R. Rodriguez 	 */
1030b1ed8dddSLuis R. Rodriguez 	if (!last_request)
1031b1ed8dddSLuis R. Rodriguez 		return;
1032e38f8a7aSLuis R. Rodriguez 	if (!reg_is_world_roaming(wiphy))
1033e38f8a7aSLuis R. Rodriguez 		return;
1034e38f8a7aSLuis R. Rodriguez 	wiphy_update_beacon_reg(wiphy);
1035e38f8a7aSLuis R. Rodriguez }
1036e38f8a7aSLuis R. Rodriguez 
1037038659e7SLuis R. Rodriguez static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1038038659e7SLuis R. Rodriguez {
1039038659e7SLuis R. Rodriguez 	if (!chan)
1040038659e7SLuis R. Rodriguez 		return true;
1041038659e7SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_DISABLED)
1042038659e7SLuis R. Rodriguez 		return true;
1043038659e7SLuis R. Rodriguez 	/* This would happen when regulatory rules disallow HT40 completely */
1044038659e7SLuis R. Rodriguez 	if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1045038659e7SLuis R. Rodriguez 		return true;
1046038659e7SLuis R. Rodriguez 	return false;
1047038659e7SLuis R. Rodriguez }
1048038659e7SLuis R. Rodriguez 
1049038659e7SLuis R. Rodriguez static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1050038659e7SLuis R. Rodriguez 					 enum ieee80211_band band,
1051038659e7SLuis R. Rodriguez 					 unsigned int chan_idx)
1052038659e7SLuis R. Rodriguez {
1053038659e7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1054038659e7SLuis R. Rodriguez 	struct ieee80211_channel *channel;
1055038659e7SLuis R. Rodriguez 	struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1056038659e7SLuis R. Rodriguez 	unsigned int i;
1057038659e7SLuis R. Rodriguez 
1058038659e7SLuis R. Rodriguez 	assert_cfg80211_lock();
1059038659e7SLuis R. Rodriguez 
1060038659e7SLuis R. Rodriguez 	sband = wiphy->bands[band];
1061038659e7SLuis R. Rodriguez 	BUG_ON(chan_idx >= sband->n_channels);
1062038659e7SLuis R. Rodriguez 	channel = &sband->channels[chan_idx];
1063038659e7SLuis R. Rodriguez 
1064038659e7SLuis R. Rodriguez 	if (is_ht40_not_allowed(channel)) {
1065038659e7SLuis R. Rodriguez 		channel->flags |= IEEE80211_CHAN_NO_HT40;
1066038659e7SLuis R. Rodriguez 		return;
1067038659e7SLuis R. Rodriguez 	}
1068038659e7SLuis R. Rodriguez 
1069038659e7SLuis R. Rodriguez 	/*
1070038659e7SLuis R. Rodriguez 	 * We need to ensure the extension channels exist to
1071038659e7SLuis R. Rodriguez 	 * be able to use HT40- or HT40+, this finds them (or not)
1072038659e7SLuis R. Rodriguez 	 */
1073038659e7SLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++) {
1074038659e7SLuis R. Rodriguez 		struct ieee80211_channel *c = &sband->channels[i];
1075038659e7SLuis R. Rodriguez 		if (c->center_freq == (channel->center_freq - 20))
1076038659e7SLuis R. Rodriguez 			channel_before = c;
1077038659e7SLuis R. Rodriguez 		if (c->center_freq == (channel->center_freq + 20))
1078038659e7SLuis R. Rodriguez 			channel_after = c;
1079038659e7SLuis R. Rodriguez 	}
1080038659e7SLuis R. Rodriguez 
1081038659e7SLuis R. Rodriguez 	/*
1082038659e7SLuis R. Rodriguez 	 * Please note that this assumes target bandwidth is 20 MHz,
1083038659e7SLuis R. Rodriguez 	 * if that ever changes we also need to change the below logic
1084038659e7SLuis R. Rodriguez 	 * to include that as well.
1085038659e7SLuis R. Rodriguez 	 */
1086038659e7SLuis R. Rodriguez 	if (is_ht40_not_allowed(channel_before))
1087689da1b3SLuis R. Rodriguez 		channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1088038659e7SLuis R. Rodriguez 	else
1089689da1b3SLuis R. Rodriguez 		channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1090038659e7SLuis R. Rodriguez 
1091038659e7SLuis R. Rodriguez 	if (is_ht40_not_allowed(channel_after))
1092689da1b3SLuis R. Rodriguez 		channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1093038659e7SLuis R. Rodriguez 	else
1094689da1b3SLuis R. Rodriguez 		channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1095038659e7SLuis R. Rodriguez }
1096038659e7SLuis R. Rodriguez 
1097038659e7SLuis R. Rodriguez static void reg_process_ht_flags_band(struct wiphy *wiphy,
1098038659e7SLuis R. Rodriguez 				      enum ieee80211_band band)
1099038659e7SLuis R. Rodriguez {
1100038659e7SLuis R. Rodriguez 	unsigned int i;
1101038659e7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1102038659e7SLuis R. Rodriguez 
1103038659e7SLuis R. Rodriguez 	BUG_ON(!wiphy->bands[band]);
1104038659e7SLuis R. Rodriguez 	sband = wiphy->bands[band];
1105038659e7SLuis R. Rodriguez 
1106038659e7SLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++)
1107038659e7SLuis R. Rodriguez 		reg_process_ht_flags_channel(wiphy, band, i);
1108038659e7SLuis R. Rodriguez }
1109038659e7SLuis R. Rodriguez 
1110038659e7SLuis R. Rodriguez static void reg_process_ht_flags(struct wiphy *wiphy)
1111038659e7SLuis R. Rodriguez {
1112038659e7SLuis R. Rodriguez 	enum ieee80211_band band;
1113038659e7SLuis R. Rodriguez 
1114038659e7SLuis R. Rodriguez 	if (!wiphy)
1115038659e7SLuis R. Rodriguez 		return;
1116038659e7SLuis R. Rodriguez 
1117038659e7SLuis R. Rodriguez 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1118038659e7SLuis R. Rodriguez 		if (wiphy->bands[band])
1119038659e7SLuis R. Rodriguez 			reg_process_ht_flags_band(wiphy, band);
1120038659e7SLuis R. Rodriguez 	}
1121038659e7SLuis R. Rodriguez 
1122038659e7SLuis R. Rodriguez }
1123038659e7SLuis R. Rodriguez 
11247db90f4aSLuis R. Rodriguez void wiphy_update_regulatory(struct wiphy *wiphy,
11257db90f4aSLuis R. Rodriguez 			     enum nl80211_reg_initiator initiator)
11268318d78aSJohannes Berg {
11278318d78aSJohannes Berg 	enum ieee80211_band band;
1128d46e5b1dSLuis R. Rodriguez 
11297db90f4aSLuis R. Rodriguez 	if (ignore_reg_update(wiphy, initiator))
1130e38f8a7aSLuis R. Rodriguez 		goto out;
1131b2e1b302SLuis R. Rodriguez 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
11328318d78aSJohannes Berg 		if (wiphy->bands[band])
11337ca43d03SLuis R. Rodriguez 			handle_band(wiphy, band, initiator);
1134b2e1b302SLuis R. Rodriguez 	}
1135e38f8a7aSLuis R. Rodriguez out:
1136e38f8a7aSLuis R. Rodriguez 	reg_process_beacons(wiphy);
1137038659e7SLuis R. Rodriguez 	reg_process_ht_flags(wiphy);
1138b2e1b302SLuis R. Rodriguez 	if (wiphy->reg_notifier)
1139716f9392SLuis R. Rodriguez 		wiphy->reg_notifier(wiphy, last_request);
1140b2e1b302SLuis R. Rodriguez }
1141b2e1b302SLuis R. Rodriguez 
11421fa25e41SLuis R. Rodriguez static void handle_channel_custom(struct wiphy *wiphy,
11431fa25e41SLuis R. Rodriguez 				  enum ieee80211_band band,
11441fa25e41SLuis R. Rodriguez 				  unsigned int chan_idx,
11451fa25e41SLuis R. Rodriguez 				  const struct ieee80211_regdomain *regd)
11461fa25e41SLuis R. Rodriguez {
11471fa25e41SLuis R. Rodriguez 	int r;
1148038659e7SLuis R. Rodriguez 	u32 desired_bw_khz = MHZ_TO_KHZ(20);
1149038659e7SLuis R. Rodriguez 	u32 bw_flags = 0;
11501fa25e41SLuis R. Rodriguez 	const struct ieee80211_reg_rule *reg_rule = NULL;
11511fa25e41SLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule = NULL;
1152038659e7SLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range = NULL;
11531fa25e41SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
11541fa25e41SLuis R. Rodriguez 	struct ieee80211_channel *chan;
11551fa25e41SLuis R. Rodriguez 
1156abc7381bSLuis R. Rodriguez 	assert_reg_lock();
1157ac46d48eSLuis R. Rodriguez 
11581fa25e41SLuis R. Rodriguez 	sband = wiphy->bands[band];
11591fa25e41SLuis R. Rodriguez 	BUG_ON(chan_idx >= sband->n_channels);
11601fa25e41SLuis R. Rodriguez 	chan = &sband->channels[chan_idx];
11611fa25e41SLuis R. Rodriguez 
1162038659e7SLuis R. Rodriguez 	r = freq_reg_info_regd(wiphy,
1163038659e7SLuis R. Rodriguez 			       MHZ_TO_KHZ(chan->center_freq),
1164038659e7SLuis R. Rodriguez 			       desired_bw_khz,
1165038659e7SLuis R. Rodriguez 			       &reg_rule,
1166038659e7SLuis R. Rodriguez 			       regd);
11671fa25e41SLuis R. Rodriguez 
11681fa25e41SLuis R. Rodriguez 	if (r) {
1169d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Disabling freq %d MHz as custom "
1170a6518536SLuis R. Rodriguez 			      "regd has no rule that fits a %d MHz "
1171a6518536SLuis R. Rodriguez 			      "wide channel\n",
1172a6518536SLuis R. Rodriguez 			      chan->center_freq,
1173a6518536SLuis R. Rodriguez 			      KHZ_TO_MHZ(desired_bw_khz));
11741fa25e41SLuis R. Rodriguez 		chan->flags = IEEE80211_CHAN_DISABLED;
11751fa25e41SLuis R. Rodriguez 		return;
11761fa25e41SLuis R. Rodriguez 	}
11771fa25e41SLuis R. Rodriguez 
1178e702d3cfSLuis R. Rodriguez 	chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
1179e702d3cfSLuis R. Rodriguez 
11801fa25e41SLuis R. Rodriguez 	power_rule = &reg_rule->power_rule;
1181038659e7SLuis R. Rodriguez 	freq_range = &reg_rule->freq_range;
11821fa25e41SLuis R. Rodriguez 
1183038659e7SLuis R. Rodriguez 	if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1184038659e7SLuis R. Rodriguez 		bw_flags = IEEE80211_CHAN_NO_HT40;
1185038659e7SLuis R. Rodriguez 
1186038659e7SLuis R. Rodriguez 	chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
11871fa25e41SLuis R. Rodriguez 	chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
11881fa25e41SLuis R. Rodriguez 	chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
11891fa25e41SLuis R. Rodriguez }
11901fa25e41SLuis R. Rodriguez 
11911fa25e41SLuis R. Rodriguez static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
11921fa25e41SLuis R. Rodriguez 			       const struct ieee80211_regdomain *regd)
11931fa25e41SLuis R. Rodriguez {
11941fa25e41SLuis R. Rodriguez 	unsigned int i;
11951fa25e41SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
11961fa25e41SLuis R. Rodriguez 
11971fa25e41SLuis R. Rodriguez 	BUG_ON(!wiphy->bands[band]);
11981fa25e41SLuis R. Rodriguez 	sband = wiphy->bands[band];
11991fa25e41SLuis R. Rodriguez 
12001fa25e41SLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++)
12011fa25e41SLuis R. Rodriguez 		handle_channel_custom(wiphy, band, i, regd);
12021fa25e41SLuis R. Rodriguez }
12031fa25e41SLuis R. Rodriguez 
12041fa25e41SLuis R. Rodriguez /* Used by drivers prior to wiphy registration */
12051fa25e41SLuis R. Rodriguez void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
12061fa25e41SLuis R. Rodriguez 				   const struct ieee80211_regdomain *regd)
12071fa25e41SLuis R. Rodriguez {
12081fa25e41SLuis R. Rodriguez 	enum ieee80211_band band;
1209bbcf3f02SLuis R. Rodriguez 	unsigned int bands_set = 0;
1210ac46d48eSLuis R. Rodriguez 
1211abc7381bSLuis R. Rodriguez 	mutex_lock(&reg_mutex);
12121fa25e41SLuis R. Rodriguez 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1213bbcf3f02SLuis R. Rodriguez 		if (!wiphy->bands[band])
1214bbcf3f02SLuis R. Rodriguez 			continue;
12151fa25e41SLuis R. Rodriguez 		handle_band_custom(wiphy, band, regd);
1216bbcf3f02SLuis R. Rodriguez 		bands_set++;
12171fa25e41SLuis R. Rodriguez 	}
1218abc7381bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
1219bbcf3f02SLuis R. Rodriguez 
1220bbcf3f02SLuis R. Rodriguez 	/*
1221bbcf3f02SLuis R. Rodriguez 	 * no point in calling this if it won't have any effect
1222bbcf3f02SLuis R. Rodriguez 	 * on your device's supportd bands.
1223bbcf3f02SLuis R. Rodriguez 	 */
1224bbcf3f02SLuis R. Rodriguez 	WARN_ON(!bands_set);
12251fa25e41SLuis R. Rodriguez }
12261fa25e41SLuis R. Rodriguez EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
12271fa25e41SLuis R. Rodriguez 
1228fb1fc7adSLuis R. Rodriguez /*
1229fb1fc7adSLuis R. Rodriguez  * Return value which can be used by ignore_request() to indicate
1230fb1fc7adSLuis R. Rodriguez  * it has been determined we should intersect two regulatory domains
1231fb1fc7adSLuis R. Rodriguez  */
12329c96477dSLuis R. Rodriguez #define REG_INTERSECT	1
12339c96477dSLuis R. Rodriguez 
123484fa4f43SJohannes Berg /* This has the logic which determines when a new request
123584fa4f43SJohannes Berg  * should be ignored. */
12362f92cd2eSLuis R. Rodriguez static int ignore_request(struct wiphy *wiphy,
12372f92cd2eSLuis R. Rodriguez 			  struct regulatory_request *pending_request)
123884fa4f43SJohannes Berg {
1239806a9e39SLuis R. Rodriguez 	struct wiphy *last_wiphy = NULL;
1240761cf7ecSLuis R. Rodriguez 
1241761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
1242761cf7ecSLuis R. Rodriguez 
124384fa4f43SJohannes Berg 	/* All initial requests are respected */
124484fa4f43SJohannes Berg 	if (!last_request)
124584fa4f43SJohannes Berg 		return 0;
124684fa4f43SJohannes Berg 
12472f92cd2eSLuis R. Rodriguez 	switch (pending_request->initiator) {
12487db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_CORE:
124909d989d1SLuis R. Rodriguez 		return 0;
12507db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1251806a9e39SLuis R. Rodriguez 
1252806a9e39SLuis R. Rodriguez 		last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1253806a9e39SLuis R. Rodriguez 
12542f92cd2eSLuis R. Rodriguez 		if (unlikely(!is_an_alpha2(pending_request->alpha2)))
125584fa4f43SJohannes Berg 			return -EINVAL;
12567db90f4aSLuis R. Rodriguez 		if (last_request->initiator ==
12577db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1258806a9e39SLuis R. Rodriguez 			if (last_wiphy != wiphy) {
125984fa4f43SJohannes Berg 				/*
126084fa4f43SJohannes Berg 				 * Two cards with two APs claiming different
12611fe90b03SThadeu Lima de Souza Cascardo 				 * Country IE alpha2s. We could
126284fa4f43SJohannes Berg 				 * intersect them, but that seems unlikely
126384fa4f43SJohannes Berg 				 * to be correct. Reject second one for now.
126484fa4f43SJohannes Berg 				 */
12652f92cd2eSLuis R. Rodriguez 				if (regdom_changes(pending_request->alpha2))
126684fa4f43SJohannes Berg 					return -EOPNOTSUPP;
126784fa4f43SJohannes Berg 				return -EALREADY;
126884fa4f43SJohannes Berg 			}
1269fb1fc7adSLuis R. Rodriguez 			/*
1270fb1fc7adSLuis R. Rodriguez 			 * Two consecutive Country IE hints on the same wiphy.
1271fb1fc7adSLuis R. Rodriguez 			 * This should be picked up early by the driver/stack
1272fb1fc7adSLuis R. Rodriguez 			 */
12732f92cd2eSLuis R. Rodriguez 			if (WARN_ON(regdom_changes(pending_request->alpha2)))
127484fa4f43SJohannes Berg 				return 0;
127584fa4f43SJohannes Berg 			return -EALREADY;
127684fa4f43SJohannes Berg 		}
1277a171fba4SLuis R. Rodriguez 		return 0;
12787db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_DRIVER:
12797db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
12802f92cd2eSLuis R. Rodriguez 			if (regdom_changes(pending_request->alpha2))
1281e74b1e7fSLuis R. Rodriguez 				return 0;
1282e74b1e7fSLuis R. Rodriguez 			return -EALREADY;
1283e74b1e7fSLuis R. Rodriguez 		}
1284fff32c04SLuis R. Rodriguez 
1285fff32c04SLuis R. Rodriguez 		/*
1286fff32c04SLuis R. Rodriguez 		 * This would happen if you unplug and plug your card
1287fff32c04SLuis R. Rodriguez 		 * back in or if you add a new device for which the previously
1288fff32c04SLuis R. Rodriguez 		 * loaded card also agrees on the regulatory domain.
1289fff32c04SLuis R. Rodriguez 		 */
12907db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
12912f92cd2eSLuis R. Rodriguez 		    !regdom_changes(pending_request->alpha2))
1292fff32c04SLuis R. Rodriguez 			return -EALREADY;
1293fff32c04SLuis R. Rodriguez 
12943e0c3ff3SLuis R. Rodriguez 		return REG_INTERSECT;
12957db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_USER:
12967db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
12979c96477dSLuis R. Rodriguez 			return REG_INTERSECT;
1298fb1fc7adSLuis R. Rodriguez 		/*
1299fb1fc7adSLuis R. Rodriguez 		 * If the user knows better the user should set the regdom
1300fb1fc7adSLuis R. Rodriguez 		 * to their country before the IE is picked up
1301fb1fc7adSLuis R. Rodriguez 		 */
13027db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
13033f2355cbSLuis R. Rodriguez 			  last_request->intersect)
13043f2355cbSLuis R. Rodriguez 			return -EOPNOTSUPP;
1305fb1fc7adSLuis R. Rodriguez 		/*
1306fb1fc7adSLuis R. Rodriguez 		 * Process user requests only after previous user/driver/core
1307fb1fc7adSLuis R. Rodriguez 		 * requests have been processed
1308fb1fc7adSLuis R. Rodriguez 		 */
13097db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
13107db90f4aSLuis R. Rodriguez 		    last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
13117db90f4aSLuis R. Rodriguez 		    last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
131269b1572bSLuis R. Rodriguez 			if (regdom_changes(last_request->alpha2))
13135eebade6SLuis R. Rodriguez 				return -EAGAIN;
13145eebade6SLuis R. Rodriguez 		}
13155eebade6SLuis R. Rodriguez 
1316baeb66feSJohn W. Linville 		if (!regdom_changes(pending_request->alpha2))
1317e74b1e7fSLuis R. Rodriguez 			return -EALREADY;
1318e74b1e7fSLuis R. Rodriguez 
131984fa4f43SJohannes Berg 		return 0;
132084fa4f43SJohannes Berg 	}
132184fa4f43SJohannes Berg 
132284fa4f43SJohannes Berg 	return -EINVAL;
132384fa4f43SJohannes Berg }
132484fa4f43SJohannes Berg 
1325b2e253cfSLuis R. Rodriguez static void reg_set_request_processed(void)
1326b2e253cfSLuis R. Rodriguez {
1327b2e253cfSLuis R. Rodriguez 	bool need_more_processing = false;
1328b2e253cfSLuis R. Rodriguez 
1329b2e253cfSLuis R. Rodriguez 	last_request->processed = true;
1330b2e253cfSLuis R. Rodriguez 
1331b2e253cfSLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
1332b2e253cfSLuis R. Rodriguez 	if (!list_empty(&reg_requests_list))
1333b2e253cfSLuis R. Rodriguez 		need_more_processing = true;
1334b2e253cfSLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
1335b2e253cfSLuis R. Rodriguez 
1336a90c7a31SLuis R. Rodriguez 	if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
1337a90c7a31SLuis R. Rodriguez 		cancel_delayed_work_sync(&reg_timeout);
1338a90c7a31SLuis R. Rodriguez 
1339b2e253cfSLuis R. Rodriguez 	if (need_more_processing)
1340b2e253cfSLuis R. Rodriguez 		schedule_work(&reg_work);
1341b2e253cfSLuis R. Rodriguez }
1342b2e253cfSLuis R. Rodriguez 
1343d1c96a9aSLuis R. Rodriguez /**
1344d1c96a9aSLuis R. Rodriguez  * __regulatory_hint - hint to the wireless core a regulatory domain
1345d1c96a9aSLuis R. Rodriguez  * @wiphy: if the hint comes from country information from an AP, this
1346d1c96a9aSLuis R. Rodriguez  *	is required to be set to the wiphy that received the information
134728da32d7SLuis R. Rodriguez  * @pending_request: the regulatory request currently being processed
1348d1c96a9aSLuis R. Rodriguez  *
1349d1c96a9aSLuis R. Rodriguez  * The Wireless subsystem can use this function to hint to the wireless core
135028da32d7SLuis R. Rodriguez  * what it believes should be the current regulatory domain.
1351d1c96a9aSLuis R. Rodriguez  *
1352d1c96a9aSLuis R. Rodriguez  * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1353d1c96a9aSLuis R. Rodriguez  * already been set or other standard error codes.
1354d1c96a9aSLuis R. Rodriguez  *
1355abc7381bSLuis R. Rodriguez  * Caller must hold &cfg80211_mutex and &reg_mutex
1356d1c96a9aSLuis R. Rodriguez  */
135728da32d7SLuis R. Rodriguez static int __regulatory_hint(struct wiphy *wiphy,
135828da32d7SLuis R. Rodriguez 			     struct regulatory_request *pending_request)
1359b2e1b302SLuis R. Rodriguez {
13609c96477dSLuis R. Rodriguez 	bool intersect = false;
1361b2e1b302SLuis R. Rodriguez 	int r = 0;
1362b2e1b302SLuis R. Rodriguez 
1363761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
1364761cf7ecSLuis R. Rodriguez 
13652f92cd2eSLuis R. Rodriguez 	r = ignore_request(wiphy, pending_request);
13669c96477dSLuis R. Rodriguez 
13673e0c3ff3SLuis R. Rodriguez 	if (r == REG_INTERSECT) {
13687db90f4aSLuis R. Rodriguez 		if (pending_request->initiator ==
13697db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_DRIVER) {
13703e0c3ff3SLuis R. Rodriguez 			r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1371d951c1ddSLuis R. Rodriguez 			if (r) {
1372d951c1ddSLuis R. Rodriguez 				kfree(pending_request);
1373b2e1b302SLuis R. Rodriguez 				return r;
13743e0c3ff3SLuis R. Rodriguez 			}
1375d951c1ddSLuis R. Rodriguez 		}
13763e0c3ff3SLuis R. Rodriguez 		intersect = true;
13773e0c3ff3SLuis R. Rodriguez 	} else if (r) {
1378fb1fc7adSLuis R. Rodriguez 		/*
1379fb1fc7adSLuis R. Rodriguez 		 * If the regulatory domain being requested by the
13803e0c3ff3SLuis R. Rodriguez 		 * driver has already been set just copy it to the
1381fb1fc7adSLuis R. Rodriguez 		 * wiphy
1382fb1fc7adSLuis R. Rodriguez 		 */
138328da32d7SLuis R. Rodriguez 		if (r == -EALREADY &&
13847db90f4aSLuis R. Rodriguez 		    pending_request->initiator ==
13857db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_DRIVER) {
13863e0c3ff3SLuis R. Rodriguez 			r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1387d951c1ddSLuis R. Rodriguez 			if (r) {
1388d951c1ddSLuis R. Rodriguez 				kfree(pending_request);
13893e0c3ff3SLuis R. Rodriguez 				return r;
1390d951c1ddSLuis R. Rodriguez 			}
13913e0c3ff3SLuis R. Rodriguez 			r = -EALREADY;
13923e0c3ff3SLuis R. Rodriguez 			goto new_request;
13933e0c3ff3SLuis R. Rodriguez 		}
1394d951c1ddSLuis R. Rodriguez 		kfree(pending_request);
13953e0c3ff3SLuis R. Rodriguez 		return r;
13963e0c3ff3SLuis R. Rodriguez 	}
1397b2e1b302SLuis R. Rodriguez 
13983e0c3ff3SLuis R. Rodriguez new_request:
1399f6037d09SJohannes Berg 	kfree(last_request);
1400d951c1ddSLuis R. Rodriguez 
1401d951c1ddSLuis R. Rodriguez 	last_request = pending_request;
1402d951c1ddSLuis R. Rodriguez 	last_request->intersect = intersect;
1403d951c1ddSLuis R. Rodriguez 
1404d951c1ddSLuis R. Rodriguez 	pending_request = NULL;
14053e0c3ff3SLuis R. Rodriguez 
140609d989d1SLuis R. Rodriguez 	if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
140709d989d1SLuis R. Rodriguez 		user_alpha2[0] = last_request->alpha2[0];
140809d989d1SLuis R. Rodriguez 		user_alpha2[1] = last_request->alpha2[1];
140909d989d1SLuis R. Rodriguez 	}
141009d989d1SLuis R. Rodriguez 
14113e0c3ff3SLuis R. Rodriguez 	/* When r == REG_INTERSECT we do need to call CRDA */
141273d54c9eSLuis R. Rodriguez 	if (r < 0) {
141373d54c9eSLuis R. Rodriguez 		/*
141473d54c9eSLuis R. Rodriguez 		 * Since CRDA will not be called in this case as we already
141573d54c9eSLuis R. Rodriguez 		 * have applied the requested regulatory domain before we just
141673d54c9eSLuis R. Rodriguez 		 * inform userspace we have processed the request
141773d54c9eSLuis R. Rodriguez 		 */
1418b2e253cfSLuis R. Rodriguez 		if (r == -EALREADY) {
141973d54c9eSLuis R. Rodriguez 			nl80211_send_reg_change_event(last_request);
1420b2e253cfSLuis R. Rodriguez 			reg_set_request_processed();
1421b2e253cfSLuis R. Rodriguez 		}
14223e0c3ff3SLuis R. Rodriguez 		return r;
142373d54c9eSLuis R. Rodriguez 	}
14243e0c3ff3SLuis R. Rodriguez 
1425d951c1ddSLuis R. Rodriguez 	return call_crda(last_request->alpha2);
1426b2e1b302SLuis R. Rodriguez }
1427b2e1b302SLuis R. Rodriguez 
142830a548c7SLuis R. Rodriguez /* This processes *all* regulatory hints */
1429d951c1ddSLuis R. Rodriguez static void reg_process_hint(struct regulatory_request *reg_request)
1430fe33eb39SLuis R. Rodriguez {
1431fe33eb39SLuis R. Rodriguez 	int r = 0;
1432fe33eb39SLuis R. Rodriguez 	struct wiphy *wiphy = NULL;
1433c4c32294SYuri Ershov 	enum nl80211_reg_initiator initiator = reg_request->initiator;
1434fe33eb39SLuis R. Rodriguez 
1435fe33eb39SLuis R. Rodriguez 	BUG_ON(!reg_request->alpha2);
1436fe33eb39SLuis R. Rodriguez 
1437fe33eb39SLuis R. Rodriguez 	if (wiphy_idx_valid(reg_request->wiphy_idx))
1438fe33eb39SLuis R. Rodriguez 		wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1439fe33eb39SLuis R. Rodriguez 
14407db90f4aSLuis R. Rodriguez 	if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1441fe33eb39SLuis R. Rodriguez 	    !wiphy) {
1442d951c1ddSLuis R. Rodriguez 		kfree(reg_request);
1443b0e2880bSLuis R. Rodriguez 		return;
1444fe33eb39SLuis R. Rodriguez 	}
1445fe33eb39SLuis R. Rodriguez 
144628da32d7SLuis R. Rodriguez 	r = __regulatory_hint(wiphy, reg_request);
1447fe33eb39SLuis R. Rodriguez 	/* This is required so that the orig_* parameters are saved */
14485be83de5SJohannes Berg 	if (r == -EALREADY && wiphy &&
1449a90c7a31SLuis R. Rodriguez 	    wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
1450c4c32294SYuri Ershov 		wiphy_update_regulatory(wiphy, initiator);
1451a90c7a31SLuis R. Rodriguez 		return;
1452a90c7a31SLuis R. Rodriguez 	}
1453a90c7a31SLuis R. Rodriguez 
1454a90c7a31SLuis R. Rodriguez 	/*
1455a90c7a31SLuis R. Rodriguez 	 * We only time out user hints, given that they should be the only
1456a90c7a31SLuis R. Rodriguez 	 * source of bogus requests.
1457a90c7a31SLuis R. Rodriguez 	 */
1458c989bb15SLuis R. Rodriguez 	if (r != -EALREADY &&
1459c989bb15SLuis R. Rodriguez 	    reg_request->initiator == NL80211_REGDOM_SET_BY_USER)
1460a90c7a31SLuis R. Rodriguez 		schedule_delayed_work(&reg_timeout, msecs_to_jiffies(3142));
1461fe33eb39SLuis R. Rodriguez }
1462fe33eb39SLuis R. Rodriguez 
1463b2e253cfSLuis R. Rodriguez /*
1464b2e253cfSLuis R. Rodriguez  * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1465b2e253cfSLuis R. Rodriguez  * Regulatory hints come on a first come first serve basis and we
1466b2e253cfSLuis R. Rodriguez  * must process each one atomically.
1467b2e253cfSLuis R. Rodriguez  */
1468fe33eb39SLuis R. Rodriguez static void reg_process_pending_hints(void)
1469fe33eb39SLuis R. Rodriguez {
1470fe33eb39SLuis R. Rodriguez 	struct regulatory_request *reg_request;
1471fe33eb39SLuis R. Rodriguez 
1472b0e2880bSLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
1473b0e2880bSLuis R. Rodriguez 	mutex_lock(&reg_mutex);
1474b0e2880bSLuis R. Rodriguez 
1475b2e253cfSLuis R. Rodriguez 	/* When last_request->processed becomes true this will be rescheduled */
1476b2e253cfSLuis R. Rodriguez 	if (last_request && !last_request->processed) {
1477b2e253cfSLuis R. Rodriguez 		REG_DBG_PRINT("Pending regulatory request, waiting "
1478b2e253cfSLuis R. Rodriguez 			      "for it to be processed...");
1479b2e253cfSLuis R. Rodriguez 		goto out;
1480b2e253cfSLuis R. Rodriguez 	}
1481b2e253cfSLuis R. Rodriguez 
1482fe33eb39SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
1483b2e253cfSLuis R. Rodriguez 
1484b2e253cfSLuis R. Rodriguez 	if (list_empty(&reg_requests_list)) {
1485b2e253cfSLuis R. Rodriguez 		spin_unlock(&reg_requests_lock);
1486b2e253cfSLuis R. Rodriguez 		goto out;
1487b2e253cfSLuis R. Rodriguez 	}
1488b2e253cfSLuis R. Rodriguez 
1489fe33eb39SLuis R. Rodriguez 	reg_request = list_first_entry(&reg_requests_list,
1490fe33eb39SLuis R. Rodriguez 				       struct regulatory_request,
1491fe33eb39SLuis R. Rodriguez 				       list);
1492fe33eb39SLuis R. Rodriguez 	list_del_init(&reg_request->list);
1493fe33eb39SLuis R. Rodriguez 
1494d951c1ddSLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
1495b0e2880bSLuis R. Rodriguez 
1496b2e253cfSLuis R. Rodriguez 	reg_process_hint(reg_request);
1497b2e253cfSLuis R. Rodriguez 
1498b2e253cfSLuis R. Rodriguez out:
1499b0e2880bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
1500b0e2880bSLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
1501fe33eb39SLuis R. Rodriguez }
1502fe33eb39SLuis R. Rodriguez 
1503e38f8a7aSLuis R. Rodriguez /* Processes beacon hints -- this has nothing to do with country IEs */
1504e38f8a7aSLuis R. Rodriguez static void reg_process_pending_beacon_hints(void)
1505e38f8a7aSLuis R. Rodriguez {
150679c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev;
1507e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *pending_beacon, *tmp;
1508e38f8a7aSLuis R. Rodriguez 
1509abc7381bSLuis R. Rodriguez 	/*
1510abc7381bSLuis R. Rodriguez 	 * No need to hold the reg_mutex here as we just touch wiphys
1511abc7381bSLuis R. Rodriguez 	 * and do not read or access regulatory variables.
1512abc7381bSLuis R. Rodriguez 	 */
1513e38f8a7aSLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
1514e38f8a7aSLuis R. Rodriguez 
1515e38f8a7aSLuis R. Rodriguez 	/* This goes through the _pending_ beacon list */
1516e38f8a7aSLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
1517e38f8a7aSLuis R. Rodriguez 
1518e38f8a7aSLuis R. Rodriguez 	if (list_empty(&reg_pending_beacons)) {
1519e38f8a7aSLuis R. Rodriguez 		spin_unlock_bh(&reg_pending_beacons_lock);
1520e38f8a7aSLuis R. Rodriguez 		goto out;
1521e38f8a7aSLuis R. Rodriguez 	}
1522e38f8a7aSLuis R. Rodriguez 
1523e38f8a7aSLuis R. Rodriguez 	list_for_each_entry_safe(pending_beacon, tmp,
1524e38f8a7aSLuis R. Rodriguez 				 &reg_pending_beacons, list) {
1525e38f8a7aSLuis R. Rodriguez 
1526e38f8a7aSLuis R. Rodriguez 		list_del_init(&pending_beacon->list);
1527e38f8a7aSLuis R. Rodriguez 
1528e38f8a7aSLuis R. Rodriguez 		/* Applies the beacon hint to current wiphys */
152979c97e97SJohannes Berg 		list_for_each_entry(rdev, &cfg80211_rdev_list, list)
153079c97e97SJohannes Berg 			wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1531e38f8a7aSLuis R. Rodriguez 
1532e38f8a7aSLuis R. Rodriguez 		/* Remembers the beacon hint for new wiphys or reg changes */
1533e38f8a7aSLuis R. Rodriguez 		list_add_tail(&pending_beacon->list, &reg_beacon_list);
1534e38f8a7aSLuis R. Rodriguez 	}
1535e38f8a7aSLuis R. Rodriguez 
1536e38f8a7aSLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
1537e38f8a7aSLuis R. Rodriguez out:
1538e38f8a7aSLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
1539e38f8a7aSLuis R. Rodriguez }
1540e38f8a7aSLuis R. Rodriguez 
1541fe33eb39SLuis R. Rodriguez static void reg_todo(struct work_struct *work)
1542fe33eb39SLuis R. Rodriguez {
1543fe33eb39SLuis R. Rodriguez 	reg_process_pending_hints();
1544e38f8a7aSLuis R. Rodriguez 	reg_process_pending_beacon_hints();
1545fe33eb39SLuis R. Rodriguez }
1546fe33eb39SLuis R. Rodriguez 
1547fe33eb39SLuis R. Rodriguez static void queue_regulatory_request(struct regulatory_request *request)
1548fe33eb39SLuis R. Rodriguez {
1549c61029c7SJohn W. Linville 	if (isalpha(request->alpha2[0]))
1550c61029c7SJohn W. Linville 		request->alpha2[0] = toupper(request->alpha2[0]);
1551c61029c7SJohn W. Linville 	if (isalpha(request->alpha2[1]))
1552c61029c7SJohn W. Linville 		request->alpha2[1] = toupper(request->alpha2[1]);
1553c61029c7SJohn W. Linville 
1554fe33eb39SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
1555fe33eb39SLuis R. Rodriguez 	list_add_tail(&request->list, &reg_requests_list);
1556fe33eb39SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
1557fe33eb39SLuis R. Rodriguez 
1558fe33eb39SLuis R. Rodriguez 	schedule_work(&reg_work);
1559fe33eb39SLuis R. Rodriguez }
1560fe33eb39SLuis R. Rodriguez 
156109d989d1SLuis R. Rodriguez /*
156209d989d1SLuis R. Rodriguez  * Core regulatory hint -- happens during cfg80211_init()
156309d989d1SLuis R. Rodriguez  * and when we restore regulatory settings.
156409d989d1SLuis R. Rodriguez  */
1565ba25c141SLuis R. Rodriguez static int regulatory_hint_core(const char *alpha2)
1566ba25c141SLuis R. Rodriguez {
1567ba25c141SLuis R. Rodriguez 	struct regulatory_request *request;
1568ba25c141SLuis R. Rodriguez 
156909d989d1SLuis R. Rodriguez 	kfree(last_request);
157009d989d1SLuis R. Rodriguez 	last_request = NULL;
1571ba25c141SLuis R. Rodriguez 
1572ba25c141SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request),
1573ba25c141SLuis R. Rodriguez 			  GFP_KERNEL);
1574ba25c141SLuis R. Rodriguez 	if (!request)
1575ba25c141SLuis R. Rodriguez 		return -ENOMEM;
1576ba25c141SLuis R. Rodriguez 
1577ba25c141SLuis R. Rodriguez 	request->alpha2[0] = alpha2[0];
1578ba25c141SLuis R. Rodriguez 	request->alpha2[1] = alpha2[1];
15797db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_CORE;
1580ba25c141SLuis R. Rodriguez 
158131e99729SLuis R. Rodriguez 	queue_regulatory_request(request);
15825078b2e3SLuis R. Rodriguez 
1583fe33eb39SLuis R. Rodriguez 	return 0;
1584ba25c141SLuis R. Rodriguez }
1585ba25c141SLuis R. Rodriguez 
1586fe33eb39SLuis R. Rodriguez /* User hints */
1587fe33eb39SLuis R. Rodriguez int regulatory_hint_user(const char *alpha2)
1588b2e1b302SLuis R. Rodriguez {
1589fe33eb39SLuis R. Rodriguez 	struct regulatory_request *request;
1590fe33eb39SLuis R. Rodriguez 
1591be3d4810SJohannes Berg 	BUG_ON(!alpha2);
1592b2e1b302SLuis R. Rodriguez 
1593fe33eb39SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1594fe33eb39SLuis R. Rodriguez 	if (!request)
1595fe33eb39SLuis R. Rodriguez 		return -ENOMEM;
1596fe33eb39SLuis R. Rodriguez 
1597fe33eb39SLuis R. Rodriguez 	request->wiphy_idx = WIPHY_IDX_STALE;
1598fe33eb39SLuis R. Rodriguez 	request->alpha2[0] = alpha2[0];
1599fe33eb39SLuis R. Rodriguez 	request->alpha2[1] = alpha2[1];
1600e12822e1SLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_USER;
1601fe33eb39SLuis R. Rodriguez 
1602fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1603fe33eb39SLuis R. Rodriguez 
1604fe33eb39SLuis R. Rodriguez 	return 0;
1605fe33eb39SLuis R. Rodriguez }
1606fe33eb39SLuis R. Rodriguez 
1607fe33eb39SLuis R. Rodriguez /* Driver hints */
1608fe33eb39SLuis R. Rodriguez int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1609fe33eb39SLuis R. Rodriguez {
1610fe33eb39SLuis R. Rodriguez 	struct regulatory_request *request;
1611fe33eb39SLuis R. Rodriguez 
1612fe33eb39SLuis R. Rodriguez 	BUG_ON(!alpha2);
1613fe33eb39SLuis R. Rodriguez 	BUG_ON(!wiphy);
1614fe33eb39SLuis R. Rodriguez 
1615fe33eb39SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1616fe33eb39SLuis R. Rodriguez 	if (!request)
1617fe33eb39SLuis R. Rodriguez 		return -ENOMEM;
1618fe33eb39SLuis R. Rodriguez 
1619fe33eb39SLuis R. Rodriguez 	request->wiphy_idx = get_wiphy_idx(wiphy);
1620fe33eb39SLuis R. Rodriguez 
1621fe33eb39SLuis R. Rodriguez 	/* Must have registered wiphy first */
1622fe33eb39SLuis R. Rodriguez 	BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1623fe33eb39SLuis R. Rodriguez 
1624fe33eb39SLuis R. Rodriguez 	request->alpha2[0] = alpha2[0];
1625fe33eb39SLuis R. Rodriguez 	request->alpha2[1] = alpha2[1];
16267db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1627fe33eb39SLuis R. Rodriguez 
1628fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1629fe33eb39SLuis R. Rodriguez 
1630fe33eb39SLuis R. Rodriguez 	return 0;
1631b2e1b302SLuis R. Rodriguez }
1632b2e1b302SLuis R. Rodriguez EXPORT_SYMBOL(regulatory_hint);
1633b2e1b302SLuis R. Rodriguez 
16344b44c8bcSLuis R. Rodriguez /*
16354b44c8bcSLuis R. Rodriguez  * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
16364b44c8bcSLuis R. Rodriguez  * therefore cannot iterate over the rdev list here.
16374b44c8bcSLuis R. Rodriguez  */
16383f2355cbSLuis R. Rodriguez void regulatory_hint_11d(struct wiphy *wiphy,
163984920e3eSLuis R. Rodriguez 			 enum ieee80211_band band,
16403f2355cbSLuis R. Rodriguez 			 u8 *country_ie,
16413f2355cbSLuis R. Rodriguez 			 u8 country_ie_len)
16423f2355cbSLuis R. Rodriguez {
16433f2355cbSLuis R. Rodriguez 	char alpha2[2];
16443f2355cbSLuis R. Rodriguez 	enum environment_cap env = ENVIRON_ANY;
1645fe33eb39SLuis R. Rodriguez 	struct regulatory_request *request;
16463f2355cbSLuis R. Rodriguez 
1647abc7381bSLuis R. Rodriguez 	mutex_lock(&reg_mutex);
16483f2355cbSLuis R. Rodriguez 
16499828b017SLuis R. Rodriguez 	if (unlikely(!last_request))
16509828b017SLuis R. Rodriguez 		goto out;
1651d335fe63SLuis R. Rodriguez 
16523f2355cbSLuis R. Rodriguez 	/* IE len must be evenly divisible by 2 */
16533f2355cbSLuis R. Rodriguez 	if (country_ie_len & 0x01)
16543f2355cbSLuis R. Rodriguez 		goto out;
16553f2355cbSLuis R. Rodriguez 
16563f2355cbSLuis R. Rodriguez 	if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
16573f2355cbSLuis R. Rodriguez 		goto out;
16583f2355cbSLuis R. Rodriguez 
16593f2355cbSLuis R. Rodriguez 	alpha2[0] = country_ie[0];
16603f2355cbSLuis R. Rodriguez 	alpha2[1] = country_ie[1];
16613f2355cbSLuis R. Rodriguez 
16623f2355cbSLuis R. Rodriguez 	if (country_ie[2] == 'I')
16633f2355cbSLuis R. Rodriguez 		env = ENVIRON_INDOOR;
16643f2355cbSLuis R. Rodriguez 	else if (country_ie[2] == 'O')
16653f2355cbSLuis R. Rodriguez 		env = ENVIRON_OUTDOOR;
16663f2355cbSLuis R. Rodriguez 
1667fb1fc7adSLuis R. Rodriguez 	/*
16688b19e6caSLuis R. Rodriguez 	 * We will run this only upon a successful connection on cfg80211.
16694b44c8bcSLuis R. Rodriguez 	 * We leave conflict resolution to the workqueue, where can hold
16704b44c8bcSLuis R. Rodriguez 	 * cfg80211_mutex.
1671fb1fc7adSLuis R. Rodriguez 	 */
1672cc0b6fe8SLuis R. Rodriguez 	if (likely(last_request->initiator ==
1673cc0b6fe8SLuis R. Rodriguez 	    NL80211_REGDOM_SET_BY_COUNTRY_IE &&
16744b44c8bcSLuis R. Rodriguez 	    wiphy_idx_valid(last_request->wiphy_idx)))
16753f2355cbSLuis R. Rodriguez 		goto out;
16763f2355cbSLuis R. Rodriguez 
1677fe33eb39SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1678fe33eb39SLuis R. Rodriguez 	if (!request)
1679f9f9b6e3SDan Carpenter 		goto out;
1680fe33eb39SLuis R. Rodriguez 
1681fe33eb39SLuis R. Rodriguez 	request->wiphy_idx = get_wiphy_idx(wiphy);
16824f366c5dSJohn W. Linville 	request->alpha2[0] = alpha2[0];
16834f366c5dSJohn W. Linville 	request->alpha2[1] = alpha2[1];
16847db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1685fe33eb39SLuis R. Rodriguez 	request->country_ie_env = env;
16863f2355cbSLuis R. Rodriguez 
1687abc7381bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
1688fe33eb39SLuis R. Rodriguez 
1689fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1690fe33eb39SLuis R. Rodriguez 
1691fe33eb39SLuis R. Rodriguez 	return;
16920441d6ffSLuis R. Rodriguez 
16933f2355cbSLuis R. Rodriguez out:
1694abc7381bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
16953f2355cbSLuis R. Rodriguez }
1696b2e1b302SLuis R. Rodriguez 
169709d989d1SLuis R. Rodriguez static void restore_alpha2(char *alpha2, bool reset_user)
169809d989d1SLuis R. Rodriguez {
169909d989d1SLuis R. Rodriguez 	/* indicates there is no alpha2 to consider for restoration */
170009d989d1SLuis R. Rodriguez 	alpha2[0] = '9';
170109d989d1SLuis R. Rodriguez 	alpha2[1] = '7';
170209d989d1SLuis R. Rodriguez 
170309d989d1SLuis R. Rodriguez 	/* The user setting has precedence over the module parameter */
170409d989d1SLuis R. Rodriguez 	if (is_user_regdom_saved()) {
170509d989d1SLuis R. Rodriguez 		/* Unless we're asked to ignore it and reset it */
170609d989d1SLuis R. Rodriguez 		if (reset_user) {
1707d91e41b6SLuis R. Rodriguez 			REG_DBG_PRINT("Restoring regulatory settings "
170809d989d1SLuis R. Rodriguez 			       "including user preference\n");
170909d989d1SLuis R. Rodriguez 			user_alpha2[0] = '9';
171009d989d1SLuis R. Rodriguez 			user_alpha2[1] = '7';
171109d989d1SLuis R. Rodriguez 
171209d989d1SLuis R. Rodriguez 			/*
171309d989d1SLuis R. Rodriguez 			 * If we're ignoring user settings, we still need to
171409d989d1SLuis R. Rodriguez 			 * check the module parameter to ensure we put things
171509d989d1SLuis R. Rodriguez 			 * back as they were for a full restore.
171609d989d1SLuis R. Rodriguez 			 */
171709d989d1SLuis R. Rodriguez 			if (!is_world_regdom(ieee80211_regdom)) {
1718d91e41b6SLuis R. Rodriguez 				REG_DBG_PRINT("Keeping preference on "
171909d989d1SLuis R. Rodriguez 				       "module parameter ieee80211_regdom: %c%c\n",
172009d989d1SLuis R. Rodriguez 				       ieee80211_regdom[0],
172109d989d1SLuis R. Rodriguez 				       ieee80211_regdom[1]);
172209d989d1SLuis R. Rodriguez 				alpha2[0] = ieee80211_regdom[0];
172309d989d1SLuis R. Rodriguez 				alpha2[1] = ieee80211_regdom[1];
172409d989d1SLuis R. Rodriguez 			}
172509d989d1SLuis R. Rodriguez 		} else {
1726d91e41b6SLuis R. Rodriguez 			REG_DBG_PRINT("Restoring regulatory settings "
172709d989d1SLuis R. Rodriguez 			       "while preserving user preference for: %c%c\n",
172809d989d1SLuis R. Rodriguez 			       user_alpha2[0],
172909d989d1SLuis R. Rodriguez 			       user_alpha2[1]);
173009d989d1SLuis R. Rodriguez 			alpha2[0] = user_alpha2[0];
173109d989d1SLuis R. Rodriguez 			alpha2[1] = user_alpha2[1];
173209d989d1SLuis R. Rodriguez 		}
173309d989d1SLuis R. Rodriguez 	} else if (!is_world_regdom(ieee80211_regdom)) {
1734d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Keeping preference on "
173509d989d1SLuis R. Rodriguez 		       "module parameter ieee80211_regdom: %c%c\n",
173609d989d1SLuis R. Rodriguez 		       ieee80211_regdom[0],
173709d989d1SLuis R. Rodriguez 		       ieee80211_regdom[1]);
173809d989d1SLuis R. Rodriguez 		alpha2[0] = ieee80211_regdom[0];
173909d989d1SLuis R. Rodriguez 		alpha2[1] = ieee80211_regdom[1];
174009d989d1SLuis R. Rodriguez 	} else
1741d91e41b6SLuis R. Rodriguez 		REG_DBG_PRINT("Restoring regulatory settings\n");
174209d989d1SLuis R. Rodriguez }
174309d989d1SLuis R. Rodriguez 
174409d989d1SLuis R. Rodriguez /*
174509d989d1SLuis R. Rodriguez  * Restoring regulatory settings involves ingoring any
174609d989d1SLuis R. Rodriguez  * possibly stale country IE information and user regulatory
174709d989d1SLuis R. Rodriguez  * settings if so desired, this includes any beacon hints
174809d989d1SLuis R. Rodriguez  * learned as we could have traveled outside to another country
174909d989d1SLuis R. Rodriguez  * after disconnection. To restore regulatory settings we do
175009d989d1SLuis R. Rodriguez  * exactly what we did at bootup:
175109d989d1SLuis R. Rodriguez  *
175209d989d1SLuis R. Rodriguez  *   - send a core regulatory hint
175309d989d1SLuis R. Rodriguez  *   - send a user regulatory hint if applicable
175409d989d1SLuis R. Rodriguez  *
175509d989d1SLuis R. Rodriguez  * Device drivers that send a regulatory hint for a specific country
175609d989d1SLuis R. Rodriguez  * keep their own regulatory domain on wiphy->regd so that does does
175709d989d1SLuis R. Rodriguez  * not need to be remembered.
175809d989d1SLuis R. Rodriguez  */
175909d989d1SLuis R. Rodriguez static void restore_regulatory_settings(bool reset_user)
176009d989d1SLuis R. Rodriguez {
176109d989d1SLuis R. Rodriguez 	char alpha2[2];
176209d989d1SLuis R. Rodriguez 	struct reg_beacon *reg_beacon, *btmp;
176314609555SLuis R. Rodriguez 	struct regulatory_request *reg_request, *tmp;
176414609555SLuis R. Rodriguez 	LIST_HEAD(tmp_reg_req_list);
176509d989d1SLuis R. Rodriguez 
176609d989d1SLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
176709d989d1SLuis R. Rodriguez 	mutex_lock(&reg_mutex);
176809d989d1SLuis R. Rodriguez 
176909d989d1SLuis R. Rodriguez 	reset_regdomains();
177009d989d1SLuis R. Rodriguez 	restore_alpha2(alpha2, reset_user);
177109d989d1SLuis R. Rodriguez 
177214609555SLuis R. Rodriguez 	/*
177314609555SLuis R. Rodriguez 	 * If there's any pending requests we simply
177414609555SLuis R. Rodriguez 	 * stash them to a temporary pending queue and
177514609555SLuis R. Rodriguez 	 * add then after we've restored regulatory
177614609555SLuis R. Rodriguez 	 * settings.
177714609555SLuis R. Rodriguez 	 */
177814609555SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
177914609555SLuis R. Rodriguez 	if (!list_empty(&reg_requests_list)) {
178014609555SLuis R. Rodriguez 		list_for_each_entry_safe(reg_request, tmp,
178114609555SLuis R. Rodriguez 					 &reg_requests_list, list) {
178214609555SLuis R. Rodriguez 			if (reg_request->initiator !=
178314609555SLuis R. Rodriguez 			    NL80211_REGDOM_SET_BY_USER)
178414609555SLuis R. Rodriguez 				continue;
178514609555SLuis R. Rodriguez 			list_del(&reg_request->list);
178614609555SLuis R. Rodriguez 			list_add_tail(&reg_request->list, &tmp_reg_req_list);
178714609555SLuis R. Rodriguez 		}
178814609555SLuis R. Rodriguez 	}
178914609555SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
179014609555SLuis R. Rodriguez 
179109d989d1SLuis R. Rodriguez 	/* Clear beacon hints */
179209d989d1SLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
179309d989d1SLuis R. Rodriguez 	if (!list_empty(&reg_pending_beacons)) {
179409d989d1SLuis R. Rodriguez 		list_for_each_entry_safe(reg_beacon, btmp,
179509d989d1SLuis R. Rodriguez 					 &reg_pending_beacons, list) {
179609d989d1SLuis R. Rodriguez 			list_del(&reg_beacon->list);
179709d989d1SLuis R. Rodriguez 			kfree(reg_beacon);
179809d989d1SLuis R. Rodriguez 		}
179909d989d1SLuis R. Rodriguez 	}
180009d989d1SLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
180109d989d1SLuis R. Rodriguez 
180209d989d1SLuis R. Rodriguez 	if (!list_empty(&reg_beacon_list)) {
180309d989d1SLuis R. Rodriguez 		list_for_each_entry_safe(reg_beacon, btmp,
180409d989d1SLuis R. Rodriguez 					 &reg_beacon_list, list) {
180509d989d1SLuis R. Rodriguez 			list_del(&reg_beacon->list);
180609d989d1SLuis R. Rodriguez 			kfree(reg_beacon);
180709d989d1SLuis R. Rodriguez 		}
180809d989d1SLuis R. Rodriguez 	}
180909d989d1SLuis R. Rodriguez 
181009d989d1SLuis R. Rodriguez 	/* First restore to the basic regulatory settings */
181109d989d1SLuis R. Rodriguez 	cfg80211_regdomain = cfg80211_world_regdom;
181209d989d1SLuis R. Rodriguez 
181309d989d1SLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
181409d989d1SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
181509d989d1SLuis R. Rodriguez 
181609d989d1SLuis R. Rodriguez 	regulatory_hint_core(cfg80211_regdomain->alpha2);
181709d989d1SLuis R. Rodriguez 
181809d989d1SLuis R. Rodriguez 	/*
181909d989d1SLuis R. Rodriguez 	 * This restores the ieee80211_regdom module parameter
182009d989d1SLuis R. Rodriguez 	 * preference or the last user requested regulatory
182109d989d1SLuis R. Rodriguez 	 * settings, user regulatory settings takes precedence.
182209d989d1SLuis R. Rodriguez 	 */
182309d989d1SLuis R. Rodriguez 	if (is_an_alpha2(alpha2))
182409d989d1SLuis R. Rodriguez 		regulatory_hint_user(user_alpha2);
182509d989d1SLuis R. Rodriguez 
182614609555SLuis R. Rodriguez 	if (list_empty(&tmp_reg_req_list))
182714609555SLuis R. Rodriguez 		return;
182814609555SLuis R. Rodriguez 
182914609555SLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
183014609555SLuis R. Rodriguez 	mutex_lock(&reg_mutex);
183114609555SLuis R. Rodriguez 
183214609555SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
183314609555SLuis R. Rodriguez 	list_for_each_entry_safe(reg_request, tmp, &tmp_reg_req_list, list) {
183414609555SLuis R. Rodriguez 		REG_DBG_PRINT("Adding request for country %c%c back "
183514609555SLuis R. Rodriguez 			      "into the queue\n",
183614609555SLuis R. Rodriguez 			      reg_request->alpha2[0],
183714609555SLuis R. Rodriguez 			      reg_request->alpha2[1]);
183814609555SLuis R. Rodriguez 		list_del(&reg_request->list);
183914609555SLuis R. Rodriguez 		list_add_tail(&reg_request->list, &reg_requests_list);
184014609555SLuis R. Rodriguez 	}
184114609555SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
184214609555SLuis R. Rodriguez 
184314609555SLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
184414609555SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
184514609555SLuis R. Rodriguez 
184614609555SLuis R. Rodriguez 	REG_DBG_PRINT("Kicking the queue\n");
184714609555SLuis R. Rodriguez 
184814609555SLuis R. Rodriguez 	schedule_work(&reg_work);
184914609555SLuis R. Rodriguez }
185009d989d1SLuis R. Rodriguez 
185109d989d1SLuis R. Rodriguez void regulatory_hint_disconnect(void)
185209d989d1SLuis R. Rodriguez {
1853d91e41b6SLuis R. Rodriguez 	REG_DBG_PRINT("All devices are disconnected, going to "
185409d989d1SLuis R. Rodriguez 		      "restore regulatory settings\n");
185509d989d1SLuis R. Rodriguez 	restore_regulatory_settings(false);
185609d989d1SLuis R. Rodriguez }
185709d989d1SLuis R. Rodriguez 
1858e38f8a7aSLuis R. Rodriguez static bool freq_is_chan_12_13_14(u16 freq)
1859e38f8a7aSLuis R. Rodriguez {
186059eb21a6SBruno Randolf 	if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
186159eb21a6SBruno Randolf 	    freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
186259eb21a6SBruno Randolf 	    freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
1863e38f8a7aSLuis R. Rodriguez 		return true;
1864e38f8a7aSLuis R. Rodriguez 	return false;
1865e38f8a7aSLuis R. Rodriguez }
1866e38f8a7aSLuis R. Rodriguez 
1867e38f8a7aSLuis R. Rodriguez int regulatory_hint_found_beacon(struct wiphy *wiphy,
1868e38f8a7aSLuis R. Rodriguez 				 struct ieee80211_channel *beacon_chan,
1869e38f8a7aSLuis R. Rodriguez 				 gfp_t gfp)
1870e38f8a7aSLuis R. Rodriguez {
1871e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *reg_beacon;
1872e38f8a7aSLuis R. Rodriguez 
1873e38f8a7aSLuis R. Rodriguez 	if (likely((beacon_chan->beacon_found ||
1874e38f8a7aSLuis R. Rodriguez 	    (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1875e38f8a7aSLuis R. Rodriguez 	    (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1876e38f8a7aSLuis R. Rodriguez 	     !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1877e38f8a7aSLuis R. Rodriguez 		return 0;
1878e38f8a7aSLuis R. Rodriguez 
1879e38f8a7aSLuis R. Rodriguez 	reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1880e38f8a7aSLuis R. Rodriguez 	if (!reg_beacon)
1881e38f8a7aSLuis R. Rodriguez 		return -ENOMEM;
1882e38f8a7aSLuis R. Rodriguez 
1883d91e41b6SLuis R. Rodriguez 	REG_DBG_PRINT("Found new beacon on "
1884e38f8a7aSLuis R. Rodriguez 		      "frequency: %d MHz (Ch %d) on %s\n",
1885e38f8a7aSLuis R. Rodriguez 		      beacon_chan->center_freq,
1886e38f8a7aSLuis R. Rodriguez 		      ieee80211_frequency_to_channel(beacon_chan->center_freq),
1887e38f8a7aSLuis R. Rodriguez 		      wiphy_name(wiphy));
18884113f751SLuis R. Rodriguez 
1889e38f8a7aSLuis R. Rodriguez 	memcpy(&reg_beacon->chan, beacon_chan,
1890e38f8a7aSLuis R. Rodriguez 		sizeof(struct ieee80211_channel));
1891e38f8a7aSLuis R. Rodriguez 
1892e38f8a7aSLuis R. Rodriguez 
1893e38f8a7aSLuis R. Rodriguez 	/*
1894e38f8a7aSLuis R. Rodriguez 	 * Since we can be called from BH or and non-BH context
1895e38f8a7aSLuis R. Rodriguez 	 * we must use spin_lock_bh()
1896e38f8a7aSLuis R. Rodriguez 	 */
1897e38f8a7aSLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
1898e38f8a7aSLuis R. Rodriguez 	list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1899e38f8a7aSLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
1900e38f8a7aSLuis R. Rodriguez 
1901e38f8a7aSLuis R. Rodriguez 	schedule_work(&reg_work);
1902e38f8a7aSLuis R. Rodriguez 
1903e38f8a7aSLuis R. Rodriguez 	return 0;
1904e38f8a7aSLuis R. Rodriguez }
1905e38f8a7aSLuis R. Rodriguez 
1906a3d2eaf0SJohannes Berg static void print_rd_rules(const struct ieee80211_regdomain *rd)
1907b2e1b302SLuis R. Rodriguez {
1908b2e1b302SLuis R. Rodriguez 	unsigned int i;
1909a3d2eaf0SJohannes Berg 	const struct ieee80211_reg_rule *reg_rule = NULL;
1910a3d2eaf0SJohannes Berg 	const struct ieee80211_freq_range *freq_range = NULL;
1911a3d2eaf0SJohannes Berg 	const struct ieee80211_power_rule *power_rule = NULL;
1912b2e1b302SLuis R. Rodriguez 
1913e9c0268fSJoe Perches 	pr_info("    (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
1914b2e1b302SLuis R. Rodriguez 
1915b2e1b302SLuis R. Rodriguez 	for (i = 0; i < rd->n_reg_rules; i++) {
1916b2e1b302SLuis R. Rodriguez 		reg_rule = &rd->reg_rules[i];
1917b2e1b302SLuis R. Rodriguez 		freq_range = &reg_rule->freq_range;
1918b2e1b302SLuis R. Rodriguez 		power_rule = &reg_rule->power_rule;
1919b2e1b302SLuis R. Rodriguez 
1920fb1fc7adSLuis R. Rodriguez 		/*
1921fb1fc7adSLuis R. Rodriguez 		 * There may not be documentation for max antenna gain
1922fb1fc7adSLuis R. Rodriguez 		 * in certain regions
1923fb1fc7adSLuis R. Rodriguez 		 */
1924b2e1b302SLuis R. Rodriguez 		if (power_rule->max_antenna_gain)
1925e9c0268fSJoe Perches 			pr_info("    (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
1926b2e1b302SLuis R. Rodriguez 				freq_range->start_freq_khz,
1927b2e1b302SLuis R. Rodriguez 				freq_range->end_freq_khz,
1928b2e1b302SLuis R. Rodriguez 				freq_range->max_bandwidth_khz,
1929b2e1b302SLuis R. Rodriguez 				power_rule->max_antenna_gain,
1930b2e1b302SLuis R. Rodriguez 				power_rule->max_eirp);
1931b2e1b302SLuis R. Rodriguez 		else
1932e9c0268fSJoe Perches 			pr_info("    (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
1933b2e1b302SLuis R. Rodriguez 				freq_range->start_freq_khz,
1934b2e1b302SLuis R. Rodriguez 				freq_range->end_freq_khz,
1935b2e1b302SLuis R. Rodriguez 				freq_range->max_bandwidth_khz,
1936b2e1b302SLuis R. Rodriguez 				power_rule->max_eirp);
1937b2e1b302SLuis R. Rodriguez 	}
1938b2e1b302SLuis R. Rodriguez }
1939b2e1b302SLuis R. Rodriguez 
1940a3d2eaf0SJohannes Berg static void print_regdomain(const struct ieee80211_regdomain *rd)
1941b2e1b302SLuis R. Rodriguez {
1942b2e1b302SLuis R. Rodriguez 
19433f2355cbSLuis R. Rodriguez 	if (is_intersected_alpha2(rd->alpha2)) {
19443f2355cbSLuis R. Rodriguez 
19457db90f4aSLuis R. Rodriguez 		if (last_request->initiator ==
19467db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_COUNTRY_IE) {
194779c97e97SJohannes Berg 			struct cfg80211_registered_device *rdev;
194879c97e97SJohannes Berg 			rdev = cfg80211_rdev_by_wiphy_idx(
1949806a9e39SLuis R. Rodriguez 				last_request->wiphy_idx);
195079c97e97SJohannes Berg 			if (rdev) {
1951e9c0268fSJoe Perches 				pr_info("Current regulatory domain updated by AP to: %c%c\n",
195279c97e97SJohannes Berg 					rdev->country_ie_alpha2[0],
195379c97e97SJohannes Berg 					rdev->country_ie_alpha2[1]);
19543f2355cbSLuis R. Rodriguez 			} else
1955e9c0268fSJoe Perches 				pr_info("Current regulatory domain intersected:\n");
19563f2355cbSLuis R. Rodriguez 		} else
1957e9c0268fSJoe Perches 			pr_info("Current regulatory domain intersected:\n");
19583f2355cbSLuis R. Rodriguez 	} else if (is_world_regdom(rd->alpha2))
1959e9c0268fSJoe Perches 		pr_info("World regulatory domain updated:\n");
1960b2e1b302SLuis R. Rodriguez 	else {
1961b2e1b302SLuis R. Rodriguez 		if (is_unknown_alpha2(rd->alpha2))
1962e9c0268fSJoe Perches 			pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
1963b2e1b302SLuis R. Rodriguez 		else
1964e9c0268fSJoe Perches 			pr_info("Regulatory domain changed to country: %c%c\n",
1965b2e1b302SLuis R. Rodriguez 				rd->alpha2[0], rd->alpha2[1]);
1966b2e1b302SLuis R. Rodriguez 	}
1967b2e1b302SLuis R. Rodriguez 	print_rd_rules(rd);
1968b2e1b302SLuis R. Rodriguez }
1969b2e1b302SLuis R. Rodriguez 
19702df78167SJohannes Berg static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1971b2e1b302SLuis R. Rodriguez {
1972e9c0268fSJoe Perches 	pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
1973b2e1b302SLuis R. Rodriguez 	print_rd_rules(rd);
1974b2e1b302SLuis R. Rodriguez }
1975b2e1b302SLuis R. Rodriguez 
1976d2372b31SJohannes Berg /* Takes ownership of rd only if it doesn't fail */
1977a3d2eaf0SJohannes Berg static int __set_regdom(const struct ieee80211_regdomain *rd)
1978b2e1b302SLuis R. Rodriguez {
19799c96477dSLuis R. Rodriguez 	const struct ieee80211_regdomain *intersected_rd = NULL;
198079c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev = NULL;
1981806a9e39SLuis R. Rodriguez 	struct wiphy *request_wiphy;
1982b2e1b302SLuis R. Rodriguez 	/* Some basic sanity checks first */
1983b2e1b302SLuis R. Rodriguez 
1984b2e1b302SLuis R. Rodriguez 	if (is_world_regdom(rd->alpha2)) {
1985f6037d09SJohannes Berg 		if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1986b2e1b302SLuis R. Rodriguez 			return -EINVAL;
1987b2e1b302SLuis R. Rodriguez 		update_world_regdomain(rd);
1988b2e1b302SLuis R. Rodriguez 		return 0;
1989b2e1b302SLuis R. Rodriguez 	}
1990b2e1b302SLuis R. Rodriguez 
1991b2e1b302SLuis R. Rodriguez 	if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1992b2e1b302SLuis R. Rodriguez 			!is_unknown_alpha2(rd->alpha2))
1993b2e1b302SLuis R. Rodriguez 		return -EINVAL;
1994b2e1b302SLuis R. Rodriguez 
1995f6037d09SJohannes Berg 	if (!last_request)
1996b2e1b302SLuis R. Rodriguez 		return -EINVAL;
1997b2e1b302SLuis R. Rodriguez 
1998fb1fc7adSLuis R. Rodriguez 	/*
1999fb1fc7adSLuis R. Rodriguez 	 * Lets only bother proceeding on the same alpha2 if the current
20003f2355cbSLuis R. Rodriguez 	 * rd is non static (it means CRDA was present and was used last)
2001fb1fc7adSLuis R. Rodriguez 	 * and the pending request came in from a country IE
2002fb1fc7adSLuis R. Rodriguez 	 */
20037db90f4aSLuis R. Rodriguez 	if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2004fb1fc7adSLuis R. Rodriguez 		/*
2005fb1fc7adSLuis R. Rodriguez 		 * If someone else asked us to change the rd lets only bother
2006fb1fc7adSLuis R. Rodriguez 		 * checking if the alpha2 changes if CRDA was already called
2007fb1fc7adSLuis R. Rodriguez 		 */
2008baeb66feSJohn W. Linville 		if (!regdom_changes(rd->alpha2))
2009b2e1b302SLuis R. Rodriguez 			return -EINVAL;
20103f2355cbSLuis R. Rodriguez 	}
20113f2355cbSLuis R. Rodriguez 
2012fb1fc7adSLuis R. Rodriguez 	/*
2013fb1fc7adSLuis R. Rodriguez 	 * Now lets set the regulatory domain, update all driver channels
2014b2e1b302SLuis R. Rodriguez 	 * and finally inform them of what we have done, in case they want
2015b2e1b302SLuis R. Rodriguez 	 * to review or adjust their own settings based on their own
2016fb1fc7adSLuis R. Rodriguez 	 * internal EEPROM data
2017fb1fc7adSLuis R. Rodriguez 	 */
2018b2e1b302SLuis R. Rodriguez 
2019f6037d09SJohannes Berg 	if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2020b2e1b302SLuis R. Rodriguez 		return -EINVAL;
2021b2e1b302SLuis R. Rodriguez 
2022b2e1b302SLuis R. Rodriguez 	if (!is_valid_rd(rd)) {
2023e9c0268fSJoe Perches 		pr_err("Invalid regulatory domain detected:\n");
2024b2e1b302SLuis R. Rodriguez 		print_regdomain_info(rd);
2025b2e1b302SLuis R. Rodriguez 		return -EINVAL;
2026b2e1b302SLuis R. Rodriguez 	}
2027b2e1b302SLuis R. Rodriguez 
2028806a9e39SLuis R. Rodriguez 	request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2029806a9e39SLuis R. Rodriguez 
2030b8295acdSLuis R. Rodriguez 	if (!last_request->intersect) {
20313e0c3ff3SLuis R. Rodriguez 		int r;
20323e0c3ff3SLuis R. Rodriguez 
20337db90f4aSLuis R. Rodriguez 		if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
20343e0c3ff3SLuis R. Rodriguez 			reset_regdomains();
20353e0c3ff3SLuis R. Rodriguez 			cfg80211_regdomain = rd;
20363e0c3ff3SLuis R. Rodriguez 			return 0;
20373e0c3ff3SLuis R. Rodriguez 		}
20383e0c3ff3SLuis R. Rodriguez 
2039fb1fc7adSLuis R. Rodriguez 		/*
2040fb1fc7adSLuis R. Rodriguez 		 * For a driver hint, lets copy the regulatory domain the
2041fb1fc7adSLuis R. Rodriguez 		 * driver wanted to the wiphy to deal with conflicts
2042fb1fc7adSLuis R. Rodriguez 		 */
20433e0c3ff3SLuis R. Rodriguez 
2044558f6d32SLuis R. Rodriguez 		/*
2045558f6d32SLuis R. Rodriguez 		 * Userspace could have sent two replies with only
2046558f6d32SLuis R. Rodriguez 		 * one kernel request.
2047558f6d32SLuis R. Rodriguez 		 */
2048558f6d32SLuis R. Rodriguez 		if (request_wiphy->regd)
2049558f6d32SLuis R. Rodriguez 			return -EALREADY;
20503e0c3ff3SLuis R. Rodriguez 
2051806a9e39SLuis R. Rodriguez 		r = reg_copy_regd(&request_wiphy->regd, rd);
20523e0c3ff3SLuis R. Rodriguez 		if (r)
20533e0c3ff3SLuis R. Rodriguez 			return r;
20543e0c3ff3SLuis R. Rodriguez 
2055b8295acdSLuis R. Rodriguez 		reset_regdomains();
2056b8295acdSLuis R. Rodriguez 		cfg80211_regdomain = rd;
2057b8295acdSLuis R. Rodriguez 		return 0;
2058b8295acdSLuis R. Rodriguez 	}
2059b8295acdSLuis R. Rodriguez 
2060b8295acdSLuis R. Rodriguez 	/* Intersection requires a bit more work */
2061b8295acdSLuis R. Rodriguez 
20627db90f4aSLuis R. Rodriguez 	if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2063b8295acdSLuis R. Rodriguez 
20649c96477dSLuis R. Rodriguez 		intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
20659c96477dSLuis R. Rodriguez 		if (!intersected_rd)
20669c96477dSLuis R. Rodriguez 			return -EINVAL;
2067b8295acdSLuis R. Rodriguez 
2068fb1fc7adSLuis R. Rodriguez 		/*
2069fb1fc7adSLuis R. Rodriguez 		 * We can trash what CRDA provided now.
20703e0c3ff3SLuis R. Rodriguez 		 * However if a driver requested this specific regulatory
2071fb1fc7adSLuis R. Rodriguez 		 * domain we keep it for its private use
2072fb1fc7adSLuis R. Rodriguez 		 */
20737db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2074806a9e39SLuis R. Rodriguez 			request_wiphy->regd = rd;
20753e0c3ff3SLuis R. Rodriguez 		else
20769c96477dSLuis R. Rodriguez 			kfree(rd);
20773e0c3ff3SLuis R. Rodriguez 
2078b8295acdSLuis R. Rodriguez 		rd = NULL;
2079b8295acdSLuis R. Rodriguez 
2080b8295acdSLuis R. Rodriguez 		reset_regdomains();
2081b8295acdSLuis R. Rodriguez 		cfg80211_regdomain = intersected_rd;
2082b8295acdSLuis R. Rodriguez 
2083b8295acdSLuis R. Rodriguez 		return 0;
20849c96477dSLuis R. Rodriguez 	}
20859c96477dSLuis R. Rodriguez 
20863f2355cbSLuis R. Rodriguez 	if (!intersected_rd)
20873f2355cbSLuis R. Rodriguez 		return -EINVAL;
20883f2355cbSLuis R. Rodriguez 
208979c97e97SJohannes Berg 	rdev = wiphy_to_dev(request_wiphy);
20903f2355cbSLuis R. Rodriguez 
209179c97e97SJohannes Berg 	rdev->country_ie_alpha2[0] = rd->alpha2[0];
209279c97e97SJohannes Berg 	rdev->country_ie_alpha2[1] = rd->alpha2[1];
209379c97e97SJohannes Berg 	rdev->env = last_request->country_ie_env;
20943f2355cbSLuis R. Rodriguez 
20953f2355cbSLuis R. Rodriguez 	BUG_ON(intersected_rd == rd);
20963f2355cbSLuis R. Rodriguez 
20973f2355cbSLuis R. Rodriguez 	kfree(rd);
20983f2355cbSLuis R. Rodriguez 	rd = NULL;
20993f2355cbSLuis R. Rodriguez 
2100b8295acdSLuis R. Rodriguez 	reset_regdomains();
21013f2355cbSLuis R. Rodriguez 	cfg80211_regdomain = intersected_rd;
2102b2e1b302SLuis R. Rodriguez 
2103b2e1b302SLuis R. Rodriguez 	return 0;
2104b2e1b302SLuis R. Rodriguez }
2105b2e1b302SLuis R. Rodriguez 
2106b2e1b302SLuis R. Rodriguez 
2107fb1fc7adSLuis R. Rodriguez /*
2108fb1fc7adSLuis R. Rodriguez  * Use this call to set the current regulatory domain. Conflicts with
2109b2e1b302SLuis R. Rodriguez  * multiple drivers can be ironed out later. Caller must've already
2110fb1fc7adSLuis R. Rodriguez  * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2111fb1fc7adSLuis R. Rodriguez  */
2112a3d2eaf0SJohannes Berg int set_regdom(const struct ieee80211_regdomain *rd)
2113b2e1b302SLuis R. Rodriguez {
2114b2e1b302SLuis R. Rodriguez 	int r;
2115b2e1b302SLuis R. Rodriguez 
2116761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
2117761cf7ecSLuis R. Rodriguez 
2118abc7381bSLuis R. Rodriguez 	mutex_lock(&reg_mutex);
2119abc7381bSLuis R. Rodriguez 
2120b2e1b302SLuis R. Rodriguez 	/* Note that this doesn't update the wiphys, this is done below */
2121b2e1b302SLuis R. Rodriguez 	r = __set_regdom(rd);
2122d2372b31SJohannes Berg 	if (r) {
2123d2372b31SJohannes Berg 		kfree(rd);
2124abc7381bSLuis R. Rodriguez 		mutex_unlock(&reg_mutex);
2125b2e1b302SLuis R. Rodriguez 		return r;
2126d2372b31SJohannes Berg 	}
2127b2e1b302SLuis R. Rodriguez 
2128b2e1b302SLuis R. Rodriguez 	/* This would make this whole thing pointless */
2129a01ddafdSLuis R. Rodriguez 	if (!last_request->intersect)
2130b2e1b302SLuis R. Rodriguez 		BUG_ON(rd != cfg80211_regdomain);
2131b2e1b302SLuis R. Rodriguez 
2132b2e1b302SLuis R. Rodriguez 	/* update all wiphys now with the new established regulatory domain */
2133f6037d09SJohannes Berg 	update_all_wiphy_regulatory(last_request->initiator);
2134b2e1b302SLuis R. Rodriguez 
2135a01ddafdSLuis R. Rodriguez 	print_regdomain(cfg80211_regdomain);
2136b2e1b302SLuis R. Rodriguez 
213773d54c9eSLuis R. Rodriguez 	nl80211_send_reg_change_event(last_request);
213873d54c9eSLuis R. Rodriguez 
2139b2e253cfSLuis R. Rodriguez 	reg_set_request_processed();
2140b2e253cfSLuis R. Rodriguez 
2141abc7381bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
2142abc7381bSLuis R. Rodriguez 
2143b2e1b302SLuis R. Rodriguez 	return r;
2144b2e1b302SLuis R. Rodriguez }
2145b2e1b302SLuis R. Rodriguez 
21464d9d88d1SScott James Remnant #ifdef CONFIG_HOTPLUG
21474d9d88d1SScott James Remnant int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
21484d9d88d1SScott James Remnant {
21494d9d88d1SScott James Remnant 	if (last_request && !last_request->processed) {
21504d9d88d1SScott James Remnant 		if (add_uevent_var(env, "COUNTRY=%c%c",
21514d9d88d1SScott James Remnant 				   last_request->alpha2[0],
21524d9d88d1SScott James Remnant 				   last_request->alpha2[1]))
21534d9d88d1SScott James Remnant 			return -ENOMEM;
21544d9d88d1SScott James Remnant 	}
21554d9d88d1SScott James Remnant 
21564d9d88d1SScott James Remnant 	return 0;
21574d9d88d1SScott James Remnant }
21584d9d88d1SScott James Remnant #else
21594d9d88d1SScott James Remnant int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
21604d9d88d1SScott James Remnant {
21614d9d88d1SScott James Remnant 	return -ENODEV;
21624d9d88d1SScott James Remnant }
21634d9d88d1SScott James Remnant #endif /* CONFIG_HOTPLUG */
21644d9d88d1SScott James Remnant 
2165a1794390SLuis R. Rodriguez /* Caller must hold cfg80211_mutex */
21663f2355cbSLuis R. Rodriguez void reg_device_remove(struct wiphy *wiphy)
21673f2355cbSLuis R. Rodriguez {
21680ad8acafSLuis R. Rodriguez 	struct wiphy *request_wiphy = NULL;
2169806a9e39SLuis R. Rodriguez 
2170761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
2171761cf7ecSLuis R. Rodriguez 
2172abc7381bSLuis R. Rodriguez 	mutex_lock(&reg_mutex);
2173abc7381bSLuis R. Rodriguez 
21740ef9ccddSChris Wright 	kfree(wiphy->regd);
21750ef9ccddSChris Wright 
21760ad8acafSLuis R. Rodriguez 	if (last_request)
2177806a9e39SLuis R. Rodriguez 		request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2178806a9e39SLuis R. Rodriguez 
21790ef9ccddSChris Wright 	if (!request_wiphy || request_wiphy != wiphy)
2180abc7381bSLuis R. Rodriguez 		goto out;
21810ef9ccddSChris Wright 
2182806a9e39SLuis R. Rodriguez 	last_request->wiphy_idx = WIPHY_IDX_STALE;
21833f2355cbSLuis R. Rodriguez 	last_request->country_ie_env = ENVIRON_ANY;
2184abc7381bSLuis R. Rodriguez out:
2185abc7381bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
21863f2355cbSLuis R. Rodriguez }
21873f2355cbSLuis R. Rodriguez 
2188a90c7a31SLuis R. Rodriguez static void reg_timeout_work(struct work_struct *work)
2189a90c7a31SLuis R. Rodriguez {
2190a90c7a31SLuis R. Rodriguez 	REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
2191a90c7a31SLuis R. Rodriguez 		      "restoring regulatory settings");
2192a90c7a31SLuis R. Rodriguez 	restore_regulatory_settings(true);
2193a90c7a31SLuis R. Rodriguez }
2194a90c7a31SLuis R. Rodriguez 
21952fcc9f73SUwe Kleine-König int __init regulatory_init(void)
2196b2e1b302SLuis R. Rodriguez {
2197bcf4f99bSLuis R. Rodriguez 	int err = 0;
2198734366deSJohannes Berg 
2199b2e1b302SLuis R. Rodriguez 	reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2200b2e1b302SLuis R. Rodriguez 	if (IS_ERR(reg_pdev))
2201b2e1b302SLuis R. Rodriguez 		return PTR_ERR(reg_pdev);
2202734366deSJohannes Berg 
22034d9d88d1SScott James Remnant 	reg_pdev->dev.type = &reg_device_type;
22044d9d88d1SScott James Remnant 
2205fe33eb39SLuis R. Rodriguez 	spin_lock_init(&reg_requests_lock);
2206e38f8a7aSLuis R. Rodriguez 	spin_lock_init(&reg_pending_beacons_lock);
2207fe33eb39SLuis R. Rodriguez 
2208a3d2eaf0SJohannes Berg 	cfg80211_regdomain = cfg80211_world_regdom;
2209734366deSJohannes Berg 
221009d989d1SLuis R. Rodriguez 	user_alpha2[0] = '9';
221109d989d1SLuis R. Rodriguez 	user_alpha2[1] = '7';
221209d989d1SLuis R. Rodriguez 
2213ae9e4b0dSLuis R. Rodriguez 	/* We always try to get an update for the static regdomain */
2214ae9e4b0dSLuis R. Rodriguez 	err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2215bcf4f99bSLuis R. Rodriguez 	if (err) {
2216bcf4f99bSLuis R. Rodriguez 		if (err == -ENOMEM)
2217bcf4f99bSLuis R. Rodriguez 			return err;
2218bcf4f99bSLuis R. Rodriguez 		/*
2219bcf4f99bSLuis R. Rodriguez 		 * N.B. kobject_uevent_env() can fail mainly for when we're out
2220bcf4f99bSLuis R. Rodriguez 		 * memory which is handled and propagated appropriately above
2221bcf4f99bSLuis R. Rodriguez 		 * but it can also fail during a netlink_broadcast() or during
2222bcf4f99bSLuis R. Rodriguez 		 * early boot for call_usermodehelper(). For now treat these
2223bcf4f99bSLuis R. Rodriguez 		 * errors as non-fatal.
2224bcf4f99bSLuis R. Rodriguez 		 */
2225e9c0268fSJoe Perches 		pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
2226bcf4f99bSLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
2227bcf4f99bSLuis R. Rodriguez 		/* We want to find out exactly why when debugging */
2228bcf4f99bSLuis R. Rodriguez 		WARN_ON(err);
2229bcf4f99bSLuis R. Rodriguez #endif
2230bcf4f99bSLuis R. Rodriguez 	}
2231734366deSJohannes Berg 
2232ae9e4b0dSLuis R. Rodriguez 	/*
2233ae9e4b0dSLuis R. Rodriguez 	 * Finally, if the user set the module parameter treat it
2234ae9e4b0dSLuis R. Rodriguez 	 * as a user hint.
2235ae9e4b0dSLuis R. Rodriguez 	 */
2236ae9e4b0dSLuis R. Rodriguez 	if (!is_world_regdom(ieee80211_regdom))
2237ae9e4b0dSLuis R. Rodriguez 		regulatory_hint_user(ieee80211_regdom);
2238ae9e4b0dSLuis R. Rodriguez 
2239b2e1b302SLuis R. Rodriguez 	return 0;
2240b2e1b302SLuis R. Rodriguez }
2241b2e1b302SLuis R. Rodriguez 
22422fcc9f73SUwe Kleine-König void /* __init_or_exit */ regulatory_exit(void)
2243b2e1b302SLuis R. Rodriguez {
2244fe33eb39SLuis R. Rodriguez 	struct regulatory_request *reg_request, *tmp;
2245e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *reg_beacon, *btmp;
2246fe33eb39SLuis R. Rodriguez 
2247fe33eb39SLuis R. Rodriguez 	cancel_work_sync(&reg_work);
2248a90c7a31SLuis R. Rodriguez 	cancel_delayed_work_sync(&reg_timeout);
2249fe33eb39SLuis R. Rodriguez 
2250a1794390SLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
2251abc7381bSLuis R. Rodriguez 	mutex_lock(&reg_mutex);
2252734366deSJohannes Berg 
2253b2e1b302SLuis R. Rodriguez 	reset_regdomains();
2254734366deSJohannes Berg 
2255f6037d09SJohannes Berg 	kfree(last_request);
2256f6037d09SJohannes Berg 
2257b2e1b302SLuis R. Rodriguez 	platform_device_unregister(reg_pdev);
2258734366deSJohannes Berg 
2259e38f8a7aSLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
2260e38f8a7aSLuis R. Rodriguez 	if (!list_empty(&reg_pending_beacons)) {
2261e38f8a7aSLuis R. Rodriguez 		list_for_each_entry_safe(reg_beacon, btmp,
2262e38f8a7aSLuis R. Rodriguez 					 &reg_pending_beacons, list) {
2263e38f8a7aSLuis R. Rodriguez 			list_del(&reg_beacon->list);
2264e38f8a7aSLuis R. Rodriguez 			kfree(reg_beacon);
2265e38f8a7aSLuis R. Rodriguez 		}
2266e38f8a7aSLuis R. Rodriguez 	}
2267e38f8a7aSLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
2268e38f8a7aSLuis R. Rodriguez 
2269e38f8a7aSLuis R. Rodriguez 	if (!list_empty(&reg_beacon_list)) {
2270e38f8a7aSLuis R. Rodriguez 		list_for_each_entry_safe(reg_beacon, btmp,
2271e38f8a7aSLuis R. Rodriguez 					 &reg_beacon_list, list) {
2272e38f8a7aSLuis R. Rodriguez 			list_del(&reg_beacon->list);
2273e38f8a7aSLuis R. Rodriguez 			kfree(reg_beacon);
2274e38f8a7aSLuis R. Rodriguez 		}
2275e38f8a7aSLuis R. Rodriguez 	}
2276e38f8a7aSLuis R. Rodriguez 
2277fe33eb39SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
2278fe33eb39SLuis R. Rodriguez 	if (!list_empty(&reg_requests_list)) {
2279fe33eb39SLuis R. Rodriguez 		list_for_each_entry_safe(reg_request, tmp,
2280fe33eb39SLuis R. Rodriguez 					 &reg_requests_list, list) {
2281fe33eb39SLuis R. Rodriguez 			list_del(&reg_request->list);
2282fe33eb39SLuis R. Rodriguez 			kfree(reg_request);
2283fe33eb39SLuis R. Rodriguez 		}
2284fe33eb39SLuis R. Rodriguez 	}
2285fe33eb39SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
2286fe33eb39SLuis R. Rodriguez 
2287abc7381bSLuis R. Rodriguez 	mutex_unlock(&reg_mutex);
2288a1794390SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
22898318d78aSJohannes Berg }
2290