blob: 4d3935b6fbddca37ba25c7e062c04e6efc5eb3d1 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujithcee075a2009-03-13 09:07:23 +05302 * Copyright (c) 2008-2009 Atheros Communications Inc.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07003 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <linux/kernel.h>
18#include <linux/slab.h>
Bob Copeland3a702e42009-03-30 22:30:29 -040019#include <net/cfg80211.h>
20#include <net/mac80211.h>
Bob Copelandfc2ada32009-03-30 22:30:27 -040021#include <net/wireless.h>
Bob Copeland3a702e42009-03-30 22:30:29 -040022#include "regd.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070023#include "regd_common.h"
24
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080025/*
26 * This is a set of common rules used by our world regulatory domains.
27 * We have 12 world regulatory domains. To save space we consolidate
28 * the regulatory domains in 5 structures by frequency and change
29 * the flags on our reg_notifier() on a case by case basis.
30 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070031
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080032/* Only these channels all allow active scan on all world regulatory domains */
33#define ATH9K_2GHZ_CH01_11 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070034
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080035/* We enable active scan on these a case by case basis by regulatory domain */
36#define ATH9K_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
37 NL80211_RRF_PASSIVE_SCAN)
38#define ATH9K_2GHZ_CH14 REG_RULE(2484-10, 2484+10, 40, 0, 20,\
39 NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_OFDM)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070040
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080041/* We allow IBSS on these on a case by case basis by regulatory domain */
42#define ATH9K_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 40, 0, 30,\
43 NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
44#define ATH9K_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 40, 0, 30,\
45 NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
46#define ATH9K_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 40, 0, 30,\
47 NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
48
49#define ATH9K_2GHZ_ALL ATH9K_2GHZ_CH01_11, \
50 ATH9K_2GHZ_CH12_13, \
51 ATH9K_2GHZ_CH14
52
53#define ATH9K_5GHZ_ALL ATH9K_5GHZ_5150_5350, \
54 ATH9K_5GHZ_5470_5850
55/* This one skips what we call "mid band" */
56#define ATH9K_5GHZ_NO_MIDBAND ATH9K_5GHZ_5150_5350, \
57 ATH9K_5GHZ_5725_5850
58
59/* Can be used for:
60 * 0x60, 0x61, 0x62 */
Bob Copeland3a702e42009-03-30 22:30:29 -040061static const struct ieee80211_regdomain ath_world_regdom_60_61_62 = {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080062 .n_reg_rules = 5,
63 .alpha2 = "99",
64 .reg_rules = {
65 ATH9K_2GHZ_ALL,
66 ATH9K_5GHZ_ALL,
67 }
68};
69
70/* Can be used by 0x63 and 0x65 */
Bob Copeland3a702e42009-03-30 22:30:29 -040071static const struct ieee80211_regdomain ath_world_regdom_63_65 = {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080072 .n_reg_rules = 4,
73 .alpha2 = "99",
74 .reg_rules = {
75 ATH9K_2GHZ_CH01_11,
76 ATH9K_2GHZ_CH12_13,
77 ATH9K_5GHZ_NO_MIDBAND,
78 }
79};
80
81/* Can be used by 0x64 only */
Bob Copeland3a702e42009-03-30 22:30:29 -040082static const struct ieee80211_regdomain ath_world_regdom_64 = {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080083 .n_reg_rules = 3,
84 .alpha2 = "99",
85 .reg_rules = {
86 ATH9K_2GHZ_CH01_11,
87 ATH9K_5GHZ_NO_MIDBAND,
88 }
89};
90
91/* Can be used by 0x66 and 0x69 */
Bob Copeland3a702e42009-03-30 22:30:29 -040092static const struct ieee80211_regdomain ath_world_regdom_66_69 = {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -080093 .n_reg_rules = 3,
94 .alpha2 = "99",
95 .reg_rules = {
96 ATH9K_2GHZ_CH01_11,
97 ATH9K_5GHZ_ALL,
98 }
99};
100
101/* Can be used by 0x67, 0x6A and 0x68 */
Bob Copeland3a702e42009-03-30 22:30:29 -0400102static const struct ieee80211_regdomain ath_world_regdom_67_68_6A = {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800103 .n_reg_rules = 4,
104 .alpha2 = "99",
105 .reg_rules = {
106 ATH9K_2GHZ_CH01_11,
107 ATH9K_2GHZ_CH12_13,
108 ATH9K_5GHZ_ALL,
109 }
110};
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700111
Bob Copelandd0f48f92009-02-12 13:38:55 -0500112static inline bool is_wwr_sku(u16 regd)
113{
114 return ((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
115 (regd == WORLD);
116}
117
Bob Copeland3a702e42009-03-30 22:30:29 -0400118static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700119{
Bob Copelandc02cf372009-03-30 22:30:28 -0400120 return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700121}
122
Bob Copeland3a702e42009-03-30 22:30:29 -0400123bool ath_is_world_regd(struct ath_regulatory *reg)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800124{
Bob Copeland3a702e42009-03-30 22:30:29 -0400125 return is_wwr_sku(ath_regd_get_eepromRD(reg));
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800126}
Bob Copeland3a702e42009-03-30 22:30:29 -0400127EXPORT_SYMBOL(ath_is_world_regd);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800128
Bob Copeland3a702e42009-03-30 22:30:29 -0400129const struct ieee80211_regdomain *ath_default_world_regdomain(void)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800130{
131 /* this is the most restrictive */
Bob Copeland3a702e42009-03-30 22:30:29 -0400132 return &ath_world_regdom_64;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800133}
Bob Copeland3a702e42009-03-30 22:30:29 -0400134EXPORT_SYMBOL(ath_default_world_regdomain);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800135
Bob Copelandc02cf372009-03-30 22:30:28 -0400136const struct
Bob Copeland3a702e42009-03-30 22:30:29 -0400137ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800138{
Bob Copelandc02cf372009-03-30 22:30:28 -0400139 switch (reg->regpair->regDmnEnum) {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800140 case 0x60:
141 case 0x61:
142 case 0x62:
Bob Copeland3a702e42009-03-30 22:30:29 -0400143 return &ath_world_regdom_60_61_62;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800144 case 0x63:
145 case 0x65:
Bob Copeland3a702e42009-03-30 22:30:29 -0400146 return &ath_world_regdom_63_65;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800147 case 0x64:
Bob Copeland3a702e42009-03-30 22:30:29 -0400148 return &ath_world_regdom_64;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800149 case 0x66:
150 case 0x69:
Bob Copeland3a702e42009-03-30 22:30:29 -0400151 return &ath_world_regdom_66_69;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800152 case 0x67:
153 case 0x68:
154 case 0x6A:
Bob Copeland3a702e42009-03-30 22:30:29 -0400155 return &ath_world_regdom_67_68_6A;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800156 default:
157 WARN_ON(1);
Bob Copeland3a702e42009-03-30 22:30:29 -0400158 return ath_default_world_regdomain();
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800159 }
160}
Bob Copeland3a702e42009-03-30 22:30:29 -0400161EXPORT_SYMBOL(ath_world_regdomain);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800162
Luis R. Rodriguez547e4c22009-01-28 12:17:48 -0800163/* Frequency is one where radar detection is required */
Bob Copeland3a702e42009-03-30 22:30:29 -0400164static bool ath_is_radar_freq(u16 center_freq)
Luis R. Rodriguez547e4c22009-01-28 12:17:48 -0800165{
166 return (center_freq >= 5260 && center_freq <= 5700);
167}
168
Luis R. Rodriguez7519a8f2009-01-28 12:17:49 -0800169/*
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500170 * N.B: These exception rules do not apply radar freqs.
171 *
172 * - We enable adhoc (or beaconing) if allowed by 11d
173 * - We enable active scan if the channel is allowed by 11d
174 * - If no country IE has been processed and a we determine we have
175 * received a beacon on a channel we can enable active scan and
176 * adhoc (or beaconing).
Luis R. Rodriguez7519a8f2009-01-28 12:17:49 -0800177 */
Bob Copeland3a702e42009-03-30 22:30:29 -0400178static void ath_reg_apply_beaconing_flags(
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400179 struct wiphy *wiphy,
180 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800181{
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500182 enum ieee80211_band band;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800183 struct ieee80211_supported_band *sband;
184 const struct ieee80211_reg_rule *reg_rule;
185 struct ieee80211_channel *ch;
186 unsigned int i;
187 u32 bandwidth = 0;
188 int r;
189
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500190 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800191
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500192 if (!wiphy->bands[band])
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800193 continue;
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500194
195 sband = wiphy->bands[band];
196
197 for (i = 0; i < sband->n_channels; i++) {
198
199 ch = &sband->channels[i];
200
Bob Copeland3a702e42009-03-30 22:30:29 -0400201 if (ath_is_radar_freq(ch->center_freq) ||
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500202 (ch->flags & IEEE80211_CHAN_RADAR))
203 continue;
204
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400205 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500206 r = freq_reg_info(wiphy, ch->center_freq,
207 &bandwidth, &reg_rule);
208 if (r)
209 continue;
210 /*
211 * If 11d had a rule for this channel ensure
212 * we enable adhoc/beaconing if it allows us to
213 * use it. Note that we would have disabled it
214 * by applying our static world regdomain by
215 * default during init, prior to calling our
216 * regulatory_hint().
217 */
218 if (!(reg_rule->flags &
219 NL80211_RRF_NO_IBSS))
220 ch->flags &=
221 ~IEEE80211_CHAN_NO_IBSS;
222 if (!(reg_rule->flags &
223 NL80211_RRF_PASSIVE_SCAN))
224 ch->flags &=
225 ~IEEE80211_CHAN_PASSIVE_SCAN;
226 } else {
227 if (ch->beacon_found)
228 ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
229 IEEE80211_CHAN_PASSIVE_SCAN);
230 }
231 }
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800232 }
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500233
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800234}
235
236/* Allows active scan scan on Ch 12 and 13 */
Bob Copeland3a702e42009-03-30 22:30:29 -0400237static void ath_reg_apply_active_scan_flags(
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400238 struct wiphy *wiphy,
239 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800240{
241 struct ieee80211_supported_band *sband;
242 struct ieee80211_channel *ch;
243 const struct ieee80211_reg_rule *reg_rule;
244 u32 bandwidth = 0;
245 int r;
246
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800247 sband = wiphy->bands[IEEE80211_BAND_2GHZ];
248
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500249 /*
250 * If no country IE has been received always enable active scan
251 * on these channels. This is only done for specific regulatory SKUs
252 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400253 if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800254 ch = &sband->channels[11]; /* CH 12 */
255 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
256 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
257 ch = &sband->channels[12]; /* CH 13 */
258 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
259 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
260 return;
261 }
262
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500263 /*
264 * If a country IE has been recieved check its rule for this
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800265 * channel first before enabling active scan. The passive scan
Luis R. Rodriguez84540862009-02-21 00:20:40 -0500266 * would have been enforced by the initial processing of our
267 * custom regulatory domain.
268 */
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800269
270 ch = &sband->channels[11]; /* CH 12 */
271 r = freq_reg_info(wiphy, ch->center_freq, &bandwidth, &reg_rule);
272 if (!r) {
273 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
274 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
275 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
276 }
277
278 ch = &sband->channels[12]; /* CH 13 */
279 r = freq_reg_info(wiphy, ch->center_freq, &bandwidth, &reg_rule);
280 if (!r) {
281 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
282 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
283 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
284 }
285}
286
287/* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
Bob Copeland3a702e42009-03-30 22:30:29 -0400288void ath_reg_apply_radar_flags(struct wiphy *wiphy)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800289{
290 struct ieee80211_supported_band *sband;
291 struct ieee80211_channel *ch;
292 unsigned int i;
293
294 if (!wiphy->bands[IEEE80211_BAND_5GHZ])
295 return;
296
297 sband = wiphy->bands[IEEE80211_BAND_5GHZ];
298
299 for (i = 0; i < sband->n_channels; i++) {
300 ch = &sband->channels[i];
Bob Copeland3a702e42009-03-30 22:30:29 -0400301 if (!ath_is_radar_freq(ch->center_freq))
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800302 continue;
303 /* We always enable radar detection/DFS on this
304 * frequency range. Additionally we also apply on
305 * this frequency range:
306 * - If STA mode does not yet have DFS supports disable
307 * active scanning
308 * - If adhoc mode does not support DFS yet then
309 * disable adhoc in the frequency.
310 * - If AP mode does not yet support radar detection/DFS
311 * do not allow AP mode
312 */
313 if (!(ch->flags & IEEE80211_CHAN_DISABLED))
314 ch->flags |= IEEE80211_CHAN_RADAR |
315 IEEE80211_CHAN_NO_IBSS |
316 IEEE80211_CHAN_PASSIVE_SCAN;
317 }
318}
Bob Copeland3a702e42009-03-30 22:30:29 -0400319EXPORT_SYMBOL(ath_reg_apply_radar_flags);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800320
Bob Copeland3a702e42009-03-30 22:30:29 -0400321void ath_reg_apply_world_flags(struct wiphy *wiphy,
322 enum nl80211_reg_initiator initiator,
323 struct ath_regulatory *reg)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800324{
Bob Copelandc02cf372009-03-30 22:30:28 -0400325 switch (reg->regpair->regDmnEnum) {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800326 case 0x60:
327 case 0x63:
328 case 0x66:
329 case 0x67:
Bob Copeland3a702e42009-03-30 22:30:29 -0400330 ath_reg_apply_beaconing_flags(wiphy, initiator);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800331 break;
332 case 0x68:
Bob Copeland3a702e42009-03-30 22:30:29 -0400333 ath_reg_apply_beaconing_flags(wiphy, initiator);
334 ath_reg_apply_active_scan_flags(wiphy, initiator);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800335 break;
336 }
337 return;
338}
Bob Copeland3a702e42009-03-30 22:30:29 -0400339EXPORT_SYMBOL(ath_reg_apply_world_flags);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800340
Bob Copeland3a702e42009-03-30 22:30:29 -0400341int ath_reg_notifier_apply(struct wiphy *wiphy,
342 struct regulatory_request *request, struct ath_regulatory *reg)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800343{
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800344 /* We always apply this */
Bob Copeland3a702e42009-03-30 22:30:29 -0400345 ath_reg_apply_radar_flags(wiphy);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800346
347 switch (request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400348 case NL80211_REGDOM_SET_BY_DRIVER:
349 case NL80211_REGDOM_SET_BY_CORE:
350 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800351 break;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400352 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Bob Copeland3a702e42009-03-30 22:30:29 -0400353 if (ath_is_world_regd(reg))
354 ath_reg_apply_world_flags(wiphy, request->initiator,
355 reg);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800356 break;
357 }
358
359 return 0;
360}
Bob Copeland3a702e42009-03-30 22:30:29 -0400361EXPORT_SYMBOL(ath_reg_notifier_apply);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800362
Bob Copeland3a702e42009-03-30 22:30:29 -0400363bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700364{
Bob Copeland3a702e42009-03-30 22:30:29 -0400365 u16 rd = ath_regd_get_eepromRD(reg);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700366 int i;
367
368 if (rd & COUNTRY_ERD_FLAG) {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800369 /* EEPROM value is a country code */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700370 u16 cc = rd & ~COUNTRY_ERD_FLAG;
371 for (i = 0; i < ARRAY_SIZE(allCountries); i++)
372 if (allCountries[i].countryCode == cc)
373 return true;
374 } else {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800375 /* EEPROM value is a regpair value */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700376 for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
377 if (regDomainPairs[i].regDmnEnum == rd)
378 return true;
379 }
Bob Copelandc02cf372009-03-30 22:30:28 -0400380 printk(KERN_DEBUG
Bob Copeland3a702e42009-03-30 22:30:29 -0400381 "ath: invalid regulatory domain/country code 0x%x\n", rd);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700382 return false;
383}
Bob Copeland3a702e42009-03-30 22:30:29 -0400384EXPORT_SYMBOL(ath_regd_is_eeprom_valid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700385
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800386/* EEPROM country code to regpair mapping */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700387static struct country_code_to_enum_rd*
Bob Copeland3a702e42009-03-30 22:30:29 -0400388ath_regd_find_country(u16 countryCode)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700389{
390 int i;
391
392 for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
393 if (allCountries[i].countryCode == countryCode)
394 return &allCountries[i];
395 }
396 return NULL;
397}
398
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800399/* EEPROM rd code to regpair mapping */
400static struct country_code_to_enum_rd*
Bob Copeland3a702e42009-03-30 22:30:29 -0400401ath_regd_find_country_by_rd(int regdmn)
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800402{
403 int i;
404
405 for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
406 if (allCountries[i].regDmnEnum == regdmn)
407 return &allCountries[i];
408 }
409 return NULL;
410}
411
412/* Returns the map of the EEPROM set RD to a country code */
Bob Copeland3a702e42009-03-30 22:30:29 -0400413static u16 ath_regd_get_default_country(u16 rd)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700414{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700415 if (rd & COUNTRY_ERD_FLAG) {
416 struct country_code_to_enum_rd *country = NULL;
417 u16 cc = rd & ~COUNTRY_ERD_FLAG;
418
Bob Copeland3a702e42009-03-30 22:30:29 -0400419 country = ath_regd_find_country(cc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700420 if (country != NULL)
421 return cc;
422 }
423
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700424 return CTRY_DEFAULT;
425}
426
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800427static struct reg_dmn_pair_mapping*
Bob Copeland3a702e42009-03-30 22:30:29 -0400428ath_get_regpair(int regdmn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700429{
430 int i;
431
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800432 if (regdmn == NO_ENUMRD)
433 return NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700434 for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800435 if (regDomainPairs[i].regDmnEnum == regdmn)
436 return &regDomainPairs[i];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700437 }
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800438 return NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439}
440
Bob Copeland3a702e42009-03-30 22:30:29 -0400441int ath_regd_init(struct ath_regulatory *reg)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700442{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700443 struct country_code_to_enum_rd *country = NULL;
Bob Copelande775aaf2009-02-12 13:38:54 -0500444 u16 regdmn;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700445
Bob Copeland3a702e42009-03-30 22:30:29 -0400446 if (!ath_regd_is_eeprom_valid(reg)) {
447 printk(KERN_DEBUG "ath: Invalid EEPROM contents\n");
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800448 return -EINVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700449 }
450
Bob Copeland3a702e42009-03-30 22:30:29 -0400451 regdmn = ath_regd_get_eepromRD(reg);
452 reg->country_code = ath_regd_get_default_country(regdmn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453
Bob Copelandc02cf372009-03-30 22:30:28 -0400454 if (reg->country_code == CTRY_DEFAULT &&
Bob Copelande775aaf2009-02-12 13:38:54 -0500455 regdmn == CTRY_DEFAULT)
Bob Copelandc02cf372009-03-30 22:30:28 -0400456 reg->country_code = CTRY_UNITED_STATES;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700457
Bob Copelandc02cf372009-03-30 22:30:28 -0400458 if (reg->country_code == CTRY_DEFAULT) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700459 country = NULL;
460 } else {
Bob Copeland3a702e42009-03-30 22:30:29 -0400461 country = ath_regd_find_country(reg->country_code);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700462 if (country == NULL) {
Bob Copelandc02cf372009-03-30 22:30:28 -0400463 printk(KERN_DEBUG
Bob Copeland3a702e42009-03-30 22:30:29 -0400464 "ath: Country is NULL!!!!, cc= %d\n",
Bob Copelandc02cf372009-03-30 22:30:28 -0400465 reg->country_code);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800466 return -EINVAL;
467 } else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700468 regdmn = country->regDmnEnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700469 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700470
Bob Copeland3a702e42009-03-30 22:30:29 -0400471 reg->regpair = ath_get_regpair(regdmn);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800472
Bob Copelandc02cf372009-03-30 22:30:28 -0400473 if (!reg->regpair) {
Bob Copeland3a702e42009-03-30 22:30:29 -0400474 printk(KERN_DEBUG "ath: "
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800475 "No regulatory domain pair found, cannot continue\n");
476 return -EINVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700477 }
478
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800479 if (!country)
Bob Copeland3a702e42009-03-30 22:30:29 -0400480 country = ath_regd_find_country_by_rd(regdmn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700481
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800482 if (country) {
Bob Copelandc02cf372009-03-30 22:30:28 -0400483 reg->alpha2[0] = country->isoName[0];
484 reg->alpha2[1] = country->isoName[1];
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800485 } else {
Bob Copelandc02cf372009-03-30 22:30:28 -0400486 reg->alpha2[0] = '0';
487 reg->alpha2[1] = '0';
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800488 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700489
Bob Copeland3a702e42009-03-30 22:30:29 -0400490 printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
Bob Copelandc02cf372009-03-30 22:30:28 -0400491 reg->alpha2[0], reg->alpha2[1]);
Bob Copeland3a702e42009-03-30 22:30:29 -0400492 printk(KERN_DEBUG "ath: Regpair detected: 0x%0x\n",
Bob Copelandc02cf372009-03-30 22:30:28 -0400493 reg->regpair->regDmnEnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700494
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800495 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496}
Bob Copeland3a702e42009-03-30 22:30:29 -0400497EXPORT_SYMBOL(ath_regd_init);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700498
Bob Copeland3a702e42009-03-30 22:30:29 -0400499u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
500 enum ieee80211_band band)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700501{
Bob Copelandc02cf372009-03-30 22:30:28 -0400502 if (!reg->regpair ||
503 (reg->country_code == CTRY_DEFAULT &&
Bob Copeland3a702e42009-03-30 22:30:29 -0400504 is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
Bob Copelandfc2ada32009-03-30 22:30:27 -0400505 return SD_NO_CTL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700506 }
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800507
Bob Copelandfc2ada32009-03-30 22:30:27 -0400508 switch (band) {
509 case IEEE80211_BAND_2GHZ:
Bob Copelandc02cf372009-03-30 22:30:28 -0400510 return reg->regpair->reg_2ghz_ctl;
Bob Copelandfc2ada32009-03-30 22:30:27 -0400511 case IEEE80211_BAND_5GHZ:
Bob Copelandc02cf372009-03-30 22:30:28 -0400512 return reg->regpair->reg_5ghz_ctl;
Bob Copelandfc2ada32009-03-30 22:30:27 -0400513 default:
514 return NO_CTL;
515 }
516
517 return NO_CTL;
518}
Bob Copeland3a702e42009-03-30 22:30:29 -0400519EXPORT_SYMBOL(ath_regd_get_band_ctl);