2 * Copyright (c) 2008-2009 Atheros Communications Inc.
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.
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/kernel.h>
20 #include <linux/export.h>
21 #include <net/cfg80211.h>
22 #include <net/mac80211.h>
24 #include "regd_common.h"
26 static int __ath_regd_init(struct ath_regulatory
*reg
);
29 * This is a set of common rules used by our world regulatory domains.
30 * We have 12 world regulatory domains. To save space we consolidate
31 * the regulatory domains in 5 structures by frequency and change
32 * the flags on our reg_notifier() on a case by case basis.
35 /* Only these channels all allow active scan on all world regulatory domains */
36 #define ATH9K_2GHZ_CH01_11 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
38 /* We enable active scan on these a case by case basis by regulatory domain */
39 #define ATH9K_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
41 #define ATH9K_2GHZ_CH14 REG_RULE(2484-10, 2484+10, 40, 0, 20,\
45 /* We allow IBSS on these on a case by case basis by regulatory domain */
46 #define ATH9K_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 80, 0, 30,\
48 #define ATH9K_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 80, 0, 30,\
50 #define ATH9K_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 80, 0, 30,\
53 #define ATH9K_2GHZ_ALL ATH9K_2GHZ_CH01_11, \
57 #define ATH9K_5GHZ_ALL ATH9K_5GHZ_5150_5350, \
60 /* This one skips what we call "mid band" */
61 #define ATH9K_5GHZ_NO_MIDBAND ATH9K_5GHZ_5150_5350, \
66 static const struct ieee80211_regdomain ath_world_regdom_60_61_62
= {
75 /* Can be used by 0x63 and 0x65 */
76 static const struct ieee80211_regdomain ath_world_regdom_63_65
= {
82 ATH9K_5GHZ_NO_MIDBAND
,
86 /* Can be used by 0x64 only */
87 static const struct ieee80211_regdomain ath_world_regdom_64
= {
92 ATH9K_5GHZ_NO_MIDBAND
,
96 /* Can be used by 0x66 and 0x69 */
97 static const struct ieee80211_regdomain ath_world_regdom_66_69
= {
106 /* Can be used by 0x67, 0x68, 0x6A and 0x6C */
107 static const struct ieee80211_regdomain ath_world_regdom_67_68_6A_6C
= {
117 static bool dynamic_country_user_possible(struct ath_regulatory
*reg
)
119 if (config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_CERT_TESTING
))
122 switch (reg
->country_code
) {
123 case CTRY_UNITED_STATES
:
189 static bool ath_reg_dyn_country_user_allow(struct ath_regulatory
*reg
)
191 if (!config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS
))
193 if (!dynamic_country_user_possible(reg
))
198 static inline bool is_wwr_sku(u16 regd
)
200 return ((regd
& COUNTRY_ERD_FLAG
) != COUNTRY_ERD_FLAG
) &&
201 (((regd
& WORLD_SKU_MASK
) == WORLD_SKU_PREFIX
) ||
205 static u16
ath_regd_get_eepromRD(struct ath_regulatory
*reg
)
207 return reg
->current_rd
& ~WORLDWIDE_ROAMING_FLAG
;
210 bool ath_is_world_regd(struct ath_regulatory
*reg
)
212 return is_wwr_sku(ath_regd_get_eepromRD(reg
));
214 EXPORT_SYMBOL(ath_is_world_regd
);
216 static const struct ieee80211_regdomain
*ath_default_world_regdomain(void)
218 /* this is the most restrictive */
219 return &ath_world_regdom_64
;
223 ieee80211_regdomain
*ath_world_regdomain(struct ath_regulatory
*reg
)
225 switch (reg
->regpair
->regDmnEnum
) {
229 return &ath_world_regdom_60_61_62
;
232 return &ath_world_regdom_63_65
;
234 return &ath_world_regdom_64
;
237 return &ath_world_regdom_66_69
;
242 return &ath_world_regdom_67_68_6A_6C
;
245 return ath_default_world_regdomain();
249 bool ath_is_49ghz_allowed(u16 regdomain
)
252 return regdomain
== MKK9_MKKC
;
254 EXPORT_SYMBOL(ath_is_49ghz_allowed
);
256 /* Frequency is one where radar detection is required */
257 static bool ath_is_radar_freq(u16 center_freq
)
259 return (center_freq
>= 5260 && center_freq
<= 5700);
262 static void ath_force_clear_no_ir_chan(struct wiphy
*wiphy
,
263 struct ieee80211_channel
*ch
)
265 const struct ieee80211_reg_rule
*reg_rule
;
267 reg_rule
= freq_reg_info(wiphy
, MHZ_TO_KHZ(ch
->center_freq
));
268 if (IS_ERR(reg_rule
))
271 if (!(reg_rule
->flags
& NL80211_RRF_NO_IR
))
272 if (ch
->flags
& IEEE80211_CHAN_NO_IR
)
273 ch
->flags
&= ~IEEE80211_CHAN_NO_IR
;
276 static void ath_force_clear_no_ir_freq(struct wiphy
*wiphy
, u16 center_freq
)
278 struct ieee80211_channel
*ch
;
280 ch
= ieee80211_get_channel(wiphy
, center_freq
);
284 ath_force_clear_no_ir_chan(wiphy
, ch
);
287 static void ath_force_no_ir_chan(struct ieee80211_channel
*ch
)
289 ch
->flags
|= IEEE80211_CHAN_NO_IR
;
292 static void ath_force_no_ir_freq(struct wiphy
*wiphy
, u16 center_freq
)
294 struct ieee80211_channel
*ch
;
296 ch
= ieee80211_get_channel(wiphy
, center_freq
);
300 ath_force_no_ir_chan(ch
);
304 __ath_reg_apply_beaconing_flags(struct wiphy
*wiphy
,
305 struct ath_regulatory
*reg
,
306 enum nl80211_reg_initiator initiator
,
307 struct ieee80211_channel
*ch
)
309 if (ath_is_radar_freq(ch
->center_freq
) ||
310 (ch
->flags
& IEEE80211_CHAN_RADAR
))
314 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
315 ath_force_clear_no_ir_chan(wiphy
, ch
);
317 case NL80211_REGDOM_SET_BY_USER
:
318 if (ath_reg_dyn_country_user_allow(reg
))
319 ath_force_clear_no_ir_chan(wiphy
, ch
);
322 if (ch
->beacon_found
)
323 ch
->flags
&= ~IEEE80211_CHAN_NO_IR
;
328 * These exception rules do not apply radar frequencies.
330 * - We enable initiating radiation if the country IE says its fine:
331 * - If no country IE has been processed and a we determine we have
332 * received a beacon on a channel we can enable initiating radiation.
335 ath_reg_apply_beaconing_flags(struct wiphy
*wiphy
,
336 struct ath_regulatory
*reg
,
337 enum nl80211_reg_initiator initiator
)
339 enum ieee80211_band band
;
340 struct ieee80211_supported_band
*sband
;
341 struct ieee80211_channel
*ch
;
344 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
345 if (!wiphy
->bands
[band
])
347 sband
= wiphy
->bands
[band
];
348 for (i
= 0; i
< sband
->n_channels
; i
++) {
349 ch
= &sband
->channels
[i
];
350 __ath_reg_apply_beaconing_flags(wiphy
, reg
,
357 * ath_reg_apply_ir_flags()
358 * @wiphy: the wiphy to use
359 * @initiator: the regulatory hint initiator
361 * If no country IE has been received always enable passive scan
362 * and no-ibss on these channels. This is only done for specific
365 * If a country IE has been received check its rule for this
366 * channel first before enabling active scan. The passive scan
367 * would have been enforced by the initial processing of our
368 * custom regulatory domain.
371 ath_reg_apply_ir_flags(struct wiphy
*wiphy
,
372 struct ath_regulatory
*reg
,
373 enum nl80211_reg_initiator initiator
)
375 struct ieee80211_supported_band
*sband
;
377 sband
= wiphy
->bands
[IEEE80211_BAND_2GHZ
];
382 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
383 ath_force_clear_no_ir_freq(wiphy
, 2467);
384 ath_force_clear_no_ir_freq(wiphy
, 2472);
386 case NL80211_REGDOM_SET_BY_USER
:
387 if (!ath_reg_dyn_country_user_allow(reg
))
389 ath_force_clear_no_ir_freq(wiphy
, 2467);
390 ath_force_clear_no_ir_freq(wiphy
, 2472);
393 ath_force_no_ir_freq(wiphy
, 2467);
394 ath_force_no_ir_freq(wiphy
, 2472);
398 /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
399 static void ath_reg_apply_radar_flags(struct wiphy
*wiphy
)
401 struct ieee80211_supported_band
*sband
;
402 struct ieee80211_channel
*ch
;
405 if (!wiphy
->bands
[IEEE80211_BAND_5GHZ
])
408 sband
= wiphy
->bands
[IEEE80211_BAND_5GHZ
];
410 for (i
= 0; i
< sband
->n_channels
; i
++) {
411 ch
= &sband
->channels
[i
];
412 if (!ath_is_radar_freq(ch
->center_freq
))
414 /* We always enable radar detection/DFS on this
415 * frequency range. Additionally we also apply on
416 * this frequency range:
417 * - If STA mode does not yet have DFS supports disable
419 * - If adhoc mode does not support DFS yet then
420 * disable adhoc in the frequency.
421 * - If AP mode does not yet support radar detection/DFS
422 * do not allow AP mode
424 if (!(ch
->flags
& IEEE80211_CHAN_DISABLED
))
425 ch
->flags
|= IEEE80211_CHAN_RADAR
|
426 IEEE80211_CHAN_NO_IR
;
430 static void ath_reg_apply_world_flags(struct wiphy
*wiphy
,
431 enum nl80211_reg_initiator initiator
,
432 struct ath_regulatory
*reg
)
434 switch (reg
->regpair
->regDmnEnum
) {
440 ath_reg_apply_beaconing_flags(wiphy
, reg
, initiator
);
443 ath_reg_apply_beaconing_flags(wiphy
, reg
, initiator
);
444 ath_reg_apply_ir_flags(wiphy
, reg
, initiator
);
447 if (ath_reg_dyn_country_user_allow(reg
))
448 ath_reg_apply_beaconing_flags(wiphy
, reg
, initiator
);
452 static u16
ath_regd_find_country_by_name(char *alpha2
)
456 for (i
= 0; i
< ARRAY_SIZE(allCountries
); i
++) {
457 if (!memcmp(allCountries
[i
].isoName
, alpha2
, 2))
458 return allCountries
[i
].countryCode
;
464 static int __ath_reg_dyn_country(struct wiphy
*wiphy
,
465 struct ath_regulatory
*reg
,
466 struct regulatory_request
*request
)
470 if (request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
471 !ath_is_world_regd(reg
))
474 country_code
= ath_regd_find_country_by_name(request
->alpha2
);
475 if (country_code
== (u16
) -1)
478 reg
->current_rd
= COUNTRY_ERD_FLAG
;
479 reg
->current_rd
|= country_code
;
481 __ath_regd_init(reg
);
483 ath_reg_apply_world_flags(wiphy
, request
->initiator
, reg
);
488 static void ath_reg_dyn_country(struct wiphy
*wiphy
,
489 struct ath_regulatory
*reg
,
490 struct regulatory_request
*request
)
492 if (__ath_reg_dyn_country(wiphy
, reg
, request
))
495 printk(KERN_DEBUG
"ath: regdomain 0x%0x "
496 "dynamically updated by %s\n",
498 reg_initiator_name(request
->initiator
));
501 void ath_reg_notifier_apply(struct wiphy
*wiphy
,
502 struct regulatory_request
*request
,
503 struct ath_regulatory
*reg
)
505 struct ath_common
*common
= container_of(reg
, struct ath_common
,
507 /* We always apply this */
508 ath_reg_apply_radar_flags(wiphy
);
511 * This would happen when we have sent a custom regulatory request
512 * a world regulatory domain and the scheduler hasn't yet processed
513 * any pending requests in the queue.
518 switch (request
->initiator
) {
519 case NL80211_REGDOM_SET_BY_CORE
:
521 * If common->reg_world_copy is world roaming it means we *were*
522 * world roaming... so we now have to restore that data.
524 if (!ath_is_world_regd(&common
->reg_world_copy
))
527 memcpy(reg
, &common
->reg_world_copy
,
528 sizeof(struct ath_regulatory
));
530 case NL80211_REGDOM_SET_BY_DRIVER
:
532 case NL80211_REGDOM_SET_BY_USER
:
533 if (ath_reg_dyn_country_user_allow(reg
))
534 ath_reg_dyn_country(wiphy
, reg
, request
);
536 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
537 ath_reg_dyn_country(wiphy
, reg
, request
);
541 EXPORT_SYMBOL(ath_reg_notifier_apply
);
543 static bool ath_regd_is_eeprom_valid(struct ath_regulatory
*reg
)
545 u16 rd
= ath_regd_get_eepromRD(reg
);
548 if (rd
& COUNTRY_ERD_FLAG
) {
549 /* EEPROM value is a country code */
550 u16 cc
= rd
& ~COUNTRY_ERD_FLAG
;
552 "ath: EEPROM indicates we should expect "
554 for (i
= 0; i
< ARRAY_SIZE(allCountries
); i
++)
555 if (allCountries
[i
].countryCode
== cc
)
558 /* EEPROM value is a regpair value */
559 if (rd
!= CTRY_DEFAULT
)
560 printk(KERN_DEBUG
"ath: EEPROM indicates we "
561 "should expect a direct regpair map\n");
562 for (i
= 0; i
< ARRAY_SIZE(regDomainPairs
); i
++)
563 if (regDomainPairs
[i
].regDmnEnum
== rd
)
567 "ath: invalid regulatory domain/country code 0x%x\n", rd
);
571 /* EEPROM country code to regpair mapping */
572 static struct country_code_to_enum_rd
*
573 ath_regd_find_country(u16 countryCode
)
577 for (i
= 0; i
< ARRAY_SIZE(allCountries
); i
++) {
578 if (allCountries
[i
].countryCode
== countryCode
)
579 return &allCountries
[i
];
584 /* EEPROM rd code to regpair mapping */
585 static struct country_code_to_enum_rd
*
586 ath_regd_find_country_by_rd(int regdmn
)
590 for (i
= 0; i
< ARRAY_SIZE(allCountries
); i
++) {
591 if (allCountries
[i
].regDmnEnum
== regdmn
)
592 return &allCountries
[i
];
597 /* Returns the map of the EEPROM set RD to a country code */
598 static u16
ath_regd_get_default_country(u16 rd
)
600 if (rd
& COUNTRY_ERD_FLAG
) {
601 struct country_code_to_enum_rd
*country
= NULL
;
602 u16 cc
= rd
& ~COUNTRY_ERD_FLAG
;
604 country
= ath_regd_find_country(cc
);
612 static struct reg_dmn_pair_mapping
*
613 ath_get_regpair(int regdmn
)
617 if (regdmn
== NO_ENUMRD
)
619 for (i
= 0; i
< ARRAY_SIZE(regDomainPairs
); i
++) {
620 if (regDomainPairs
[i
].regDmnEnum
== regdmn
)
621 return ®DomainPairs
[i
];
627 ath_regd_init_wiphy(struct ath_regulatory
*reg
,
629 void (*reg_notifier
)(struct wiphy
*wiphy
,
630 struct regulatory_request
*request
))
632 const struct ieee80211_regdomain
*regd
;
634 wiphy
->reg_notifier
= reg_notifier
;
635 wiphy
->regulatory_flags
|= REGULATORY_STRICT_REG
|
636 REGULATORY_CUSTOM_REG
;
638 if (ath_is_world_regd(reg
)) {
640 * Anything applied here (prior to wiphy registration) gets
641 * saved on the wiphy orig_* parameters
643 regd
= ath_world_regdomain(reg
);
644 wiphy
->regulatory_flags
|= REGULATORY_COUNTRY_IE_FOLLOW_POWER
;
647 * This gets applied in the case of the absence of CRDA,
648 * it's our own custom world regulatory domain, similar to
649 * cfg80211's but we enable passive scanning.
651 regd
= ath_default_world_regdomain();
654 wiphy_apply_custom_regulatory(wiphy
, regd
);
655 ath_reg_apply_radar_flags(wiphy
);
656 ath_reg_apply_world_flags(wiphy
, NL80211_REGDOM_SET_BY_DRIVER
, reg
);
661 * Some users have reported their EEPROM programmed with
662 * 0x8000 set, this is not a supported regulatory domain
663 * but since we have more than one user with it we need
664 * a solution for them. We default to 0x64, which is the
665 * default Atheros world regulatory domain.
667 static void ath_regd_sanitize(struct ath_regulatory
*reg
)
669 if (reg
->current_rd
!= COUNTRY_ERD_FLAG
)
671 printk(KERN_DEBUG
"ath: EEPROM regdomain sanitized\n");
672 reg
->current_rd
= 0x64;
675 static int __ath_regd_init(struct ath_regulatory
*reg
)
677 struct country_code_to_enum_rd
*country
= NULL
;
683 ath_regd_sanitize(reg
);
685 printk(KERN_DEBUG
"ath: EEPROM regdomain: 0x%0x\n", reg
->current_rd
);
687 if (!ath_regd_is_eeprom_valid(reg
)) {
688 pr_err("Invalid EEPROM contents\n");
692 regdmn
= ath_regd_get_eepromRD(reg
);
693 reg
->country_code
= ath_regd_get_default_country(regdmn
);
695 if (reg
->country_code
== CTRY_DEFAULT
&&
696 regdmn
== CTRY_DEFAULT
) {
697 printk(KERN_DEBUG
"ath: EEPROM indicates default "
698 "country code should be used\n");
699 reg
->country_code
= CTRY_UNITED_STATES
;
702 if (reg
->country_code
== CTRY_DEFAULT
) {
705 printk(KERN_DEBUG
"ath: doing EEPROM country->regdmn "
707 country
= ath_regd_find_country(reg
->country_code
);
708 if (country
== NULL
) {
710 "ath: no valid country maps found for "
711 "country code: 0x%0x\n",
715 regdmn
= country
->regDmnEnum
;
716 printk(KERN_DEBUG
"ath: country maps to "
717 "regdmn code: 0x%0x\n",
722 reg
->regpair
= ath_get_regpair(regdmn
);
725 printk(KERN_DEBUG
"ath: "
726 "No regulatory domain pair found, cannot continue\n");
731 country
= ath_regd_find_country_by_rd(regdmn
);
734 reg
->alpha2
[0] = country
->isoName
[0];
735 reg
->alpha2
[1] = country
->isoName
[1];
737 reg
->alpha2
[0] = '0';
738 reg
->alpha2
[1] = '0';
741 printk(KERN_DEBUG
"ath: Country alpha2 being used: %c%c\n",
742 reg
->alpha2
[0], reg
->alpha2
[1]);
743 printk(KERN_DEBUG
"ath: Regpair used: 0x%0x\n",
744 reg
->regpair
->regDmnEnum
);
750 ath_regd_init(struct ath_regulatory
*reg
,
752 void (*reg_notifier
)(struct wiphy
*wiphy
,
753 struct regulatory_request
*request
))
755 struct ath_common
*common
= container_of(reg
, struct ath_common
,
759 r
= __ath_regd_init(reg
);
763 if (ath_is_world_regd(reg
))
764 memcpy(&common
->reg_world_copy
, reg
,
765 sizeof(struct ath_regulatory
));
767 ath_regd_init_wiphy(reg
, wiphy
, reg_notifier
);
771 EXPORT_SYMBOL(ath_regd_init
);
773 u32
ath_regd_get_band_ctl(struct ath_regulatory
*reg
,
774 enum ieee80211_band band
)
777 (reg
->country_code
== CTRY_DEFAULT
&&
778 is_wwr_sku(ath_regd_get_eepromRD(reg
)))) {
783 case IEEE80211_BAND_2GHZ
:
784 return reg
->regpair
->reg_2ghz_ctl
;
785 case IEEE80211_BAND_5GHZ
:
786 return reg
->regpair
->reg_5ghz_ctl
;
791 EXPORT_SYMBOL(ath_regd_get_band_ctl
);