Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2008-2009 Atheros Communications Inc.
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018 
0019 #include <linux/kernel.h>
0020 #include <linux/export.h>
0021 #include <net/cfg80211.h>
0022 #include <net/mac80211.h>
0023 #include "regd.h"
0024 #include "regd_common.h"
0025 
0026 static int __ath_regd_init(struct ath_regulatory *reg);
0027 
0028 /*
0029  * This is a set of common rules used by our world regulatory domains.
0030  * We have 12 world regulatory domains. To save space we consolidate
0031  * the regulatory domains in 5 structures by frequency and change
0032  * the flags on our reg_notifier() on a case by case basis.
0033  */
0034 
0035 /* Only these channels all allow active scan on all world regulatory domains */
0036 #define ATH_2GHZ_CH01_11    REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
0037 
0038 /* We enable active scan on these a case by case basis by regulatory domain */
0039 #define ATH_2GHZ_CH12_13    REG_RULE(2467-10, 2472+10, 40, 0, 20,\
0040                      NL80211_RRF_NO_IR)
0041 #define ATH_2GHZ_CH14       REG_RULE(2484-10, 2484+10, 40, 0, 20,\
0042                      NL80211_RRF_NO_IR | \
0043                      NL80211_RRF_NO_OFDM)
0044 
0045 /* We allow IBSS on these on a case by case basis by regulatory domain */
0046 #define ATH_5GHZ_5150_5350  REG_RULE(5150-10, 5350+10, 80, 0, 30,\
0047                      NL80211_RRF_NO_IR)
0048 #define ATH_5GHZ_5470_5850  REG_RULE(5470-10, 5850+10, 80, 0, 30,\
0049                      NL80211_RRF_NO_IR)
0050 #define ATH_5GHZ_5725_5850  REG_RULE(5725-10, 5850+10, 80, 0, 30,\
0051                      NL80211_RRF_NO_IR)
0052 
0053 #define ATH_2GHZ_ALL        ATH_2GHZ_CH01_11, \
0054                 ATH_2GHZ_CH12_13, \
0055                 ATH_2GHZ_CH14
0056 
0057 #define ATH_5GHZ_ALL        ATH_5GHZ_5150_5350, \
0058                 ATH_5GHZ_5470_5850
0059 
0060 /* This one skips what we call "mid band" */
0061 #define ATH_5GHZ_NO_MIDBAND ATH_5GHZ_5150_5350, \
0062                 ATH_5GHZ_5725_5850
0063 
0064 /* Can be used for:
0065  * 0x60, 0x61, 0x62 */
0066 static const struct ieee80211_regdomain ath_world_regdom_60_61_62 = {
0067     .n_reg_rules = 5,
0068     .alpha2 =  "99",
0069     .reg_rules = {
0070         ATH_2GHZ_ALL,
0071         ATH_5GHZ_ALL,
0072     }
0073 };
0074 
0075 /* Can be used by 0x63 and 0x65 */
0076 static const struct ieee80211_regdomain ath_world_regdom_63_65 = {
0077     .n_reg_rules = 4,
0078     .alpha2 =  "99",
0079     .reg_rules = {
0080         ATH_2GHZ_CH01_11,
0081         ATH_2GHZ_CH12_13,
0082         ATH_5GHZ_NO_MIDBAND,
0083     }
0084 };
0085 
0086 /* Can be used by 0x64 only */
0087 static const struct ieee80211_regdomain ath_world_regdom_64 = {
0088     .n_reg_rules = 3,
0089     .alpha2 =  "99",
0090     .reg_rules = {
0091         ATH_2GHZ_CH01_11,
0092         ATH_5GHZ_NO_MIDBAND,
0093     }
0094 };
0095 
0096 /* Can be used by 0x66 and 0x69 */
0097 static const struct ieee80211_regdomain ath_world_regdom_66_69 = {
0098     .n_reg_rules = 3,
0099     .alpha2 =  "99",
0100     .reg_rules = {
0101         ATH_2GHZ_CH01_11,
0102         ATH_5GHZ_ALL,
0103     }
0104 };
0105 
0106 /* Can be used by 0x67, 0x68, 0x6A and 0x6C */
0107 static const struct ieee80211_regdomain ath_world_regdom_67_68_6A_6C = {
0108     .n_reg_rules = 4,
0109     .alpha2 =  "99",
0110     .reg_rules = {
0111         ATH_2GHZ_CH01_11,
0112         ATH_2GHZ_CH12_13,
0113         ATH_5GHZ_ALL,
0114     }
0115 };
0116 
0117 static bool dynamic_country_user_possible(struct ath_regulatory *reg)
0118 {
0119     if (IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_CERT_TESTING))
0120         return true;
0121 
0122     switch (reg->country_code) {
0123     case CTRY_UNITED_STATES:
0124     case CTRY_JAPAN1:
0125     case CTRY_JAPAN2:
0126     case CTRY_JAPAN3:
0127     case CTRY_JAPAN4:
0128     case CTRY_JAPAN5:
0129     case CTRY_JAPAN6:
0130     case CTRY_JAPAN7:
0131     case CTRY_JAPAN8:
0132     case CTRY_JAPAN9:
0133     case CTRY_JAPAN10:
0134     case CTRY_JAPAN11:
0135     case CTRY_JAPAN12:
0136     case CTRY_JAPAN13:
0137     case CTRY_JAPAN14:
0138     case CTRY_JAPAN15:
0139     case CTRY_JAPAN16:
0140     case CTRY_JAPAN17:
0141     case CTRY_JAPAN18:
0142     case CTRY_JAPAN19:
0143     case CTRY_JAPAN20:
0144     case CTRY_JAPAN21:
0145     case CTRY_JAPAN22:
0146     case CTRY_JAPAN23:
0147     case CTRY_JAPAN24:
0148     case CTRY_JAPAN25:
0149     case CTRY_JAPAN26:
0150     case CTRY_JAPAN27:
0151     case CTRY_JAPAN28:
0152     case CTRY_JAPAN29:
0153     case CTRY_JAPAN30:
0154     case CTRY_JAPAN31:
0155     case CTRY_JAPAN32:
0156     case CTRY_JAPAN33:
0157     case CTRY_JAPAN34:
0158     case CTRY_JAPAN35:
0159     case CTRY_JAPAN36:
0160     case CTRY_JAPAN37:
0161     case CTRY_JAPAN38:
0162     case CTRY_JAPAN39:
0163     case CTRY_JAPAN40:
0164     case CTRY_JAPAN41:
0165     case CTRY_JAPAN42:
0166     case CTRY_JAPAN43:
0167     case CTRY_JAPAN44:
0168     case CTRY_JAPAN45:
0169     case CTRY_JAPAN46:
0170     case CTRY_JAPAN47:
0171     case CTRY_JAPAN48:
0172     case CTRY_JAPAN49:
0173     case CTRY_JAPAN50:
0174     case CTRY_JAPAN51:
0175     case CTRY_JAPAN52:
0176     case CTRY_JAPAN53:
0177     case CTRY_JAPAN54:
0178     case CTRY_JAPAN55:
0179     case CTRY_JAPAN56:
0180     case CTRY_JAPAN57:
0181     case CTRY_JAPAN58:
0182     case CTRY_JAPAN59:
0183         return false;
0184     }
0185 
0186     return true;
0187 }
0188 
0189 static bool ath_reg_dyn_country_user_allow(struct ath_regulatory *reg)
0190 {
0191     if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS))
0192         return false;
0193     if (!dynamic_country_user_possible(reg))
0194         return false;
0195     return true;
0196 }
0197 
0198 static inline bool is_wwr_sku(u16 regd)
0199 {
0200     return ((regd & COUNTRY_ERD_FLAG) != COUNTRY_ERD_FLAG) &&
0201         (((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
0202         (regd == WORLD));
0203 }
0204 
0205 static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
0206 {
0207     return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
0208 }
0209 
0210 bool ath_is_world_regd(struct ath_regulatory *reg)
0211 {
0212     return is_wwr_sku(ath_regd_get_eepromRD(reg));
0213 }
0214 EXPORT_SYMBOL(ath_is_world_regd);
0215 
0216 static const struct ieee80211_regdomain *ath_default_world_regdomain(void)
0217 {
0218     /* this is the most restrictive */
0219     return &ath_world_regdom_64;
0220 }
0221 
0222 static const struct
0223 ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
0224 {
0225     switch (reg->regpair->reg_domain) {
0226     case 0x60:
0227     case 0x61:
0228     case 0x62:
0229         return &ath_world_regdom_60_61_62;
0230     case 0x63:
0231     case 0x65:
0232         return &ath_world_regdom_63_65;
0233     case 0x64:
0234         return &ath_world_regdom_64;
0235     case 0x66:
0236     case 0x69:
0237         return &ath_world_regdom_66_69;
0238     case 0x67:
0239     case 0x68:
0240     case 0x6A:
0241     case 0x6C:
0242         return &ath_world_regdom_67_68_6A_6C;
0243     default:
0244         WARN_ON(1);
0245         return ath_default_world_regdomain();
0246     }
0247 }
0248 
0249 bool ath_is_49ghz_allowed(u16 regdomain)
0250 {
0251     /* possibly more */
0252     return regdomain == MKK9_MKKC;
0253 }
0254 EXPORT_SYMBOL(ath_is_49ghz_allowed);
0255 
0256 /* Frequency is one where radar detection is required */
0257 static bool ath_is_radar_freq(u16 center_freq,
0258                   struct ath_regulatory *reg)
0259 
0260 {
0261     if (reg->country_code == CTRY_INDIA)
0262         return (center_freq >= 5500 && center_freq <= 5700);
0263     return (center_freq >= 5260 && center_freq <= 5700);
0264 }
0265 
0266 static void ath_force_clear_no_ir_chan(struct wiphy *wiphy,
0267                        struct ieee80211_channel *ch)
0268 {
0269     const struct ieee80211_reg_rule *reg_rule;
0270 
0271     reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(ch->center_freq));
0272     if (IS_ERR(reg_rule))
0273         return;
0274 
0275     if (!(reg_rule->flags & NL80211_RRF_NO_IR))
0276         if (ch->flags & IEEE80211_CHAN_NO_IR)
0277             ch->flags &= ~IEEE80211_CHAN_NO_IR;
0278 }
0279 
0280 static void ath_force_clear_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
0281 {
0282     struct ieee80211_channel *ch;
0283 
0284     ch = ieee80211_get_channel(wiphy, center_freq);
0285     if (!ch)
0286         return;
0287 
0288     ath_force_clear_no_ir_chan(wiphy, ch);
0289 }
0290 
0291 static void ath_force_no_ir_chan(struct ieee80211_channel *ch)
0292 {
0293     ch->flags |= IEEE80211_CHAN_NO_IR;
0294 }
0295 
0296 static void ath_force_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
0297 {
0298     struct ieee80211_channel *ch;
0299 
0300     ch = ieee80211_get_channel(wiphy, center_freq);
0301     if (!ch)
0302         return;
0303 
0304     ath_force_no_ir_chan(ch);
0305 }
0306 
0307 static void
0308 __ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
0309                 struct ath_regulatory *reg,
0310                 enum nl80211_reg_initiator initiator,
0311                 struct ieee80211_channel *ch)
0312 {
0313     if (ath_is_radar_freq(ch->center_freq, reg) ||
0314         (ch->flags & IEEE80211_CHAN_RADAR))
0315         return;
0316 
0317     switch (initiator) {
0318     case NL80211_REGDOM_SET_BY_COUNTRY_IE:
0319         ath_force_clear_no_ir_chan(wiphy, ch);
0320         break;
0321     case NL80211_REGDOM_SET_BY_USER:
0322         if (ath_reg_dyn_country_user_allow(reg))
0323             ath_force_clear_no_ir_chan(wiphy, ch);
0324         break;
0325     default:
0326         if (ch->beacon_found)
0327             ch->flags &= ~IEEE80211_CHAN_NO_IR;
0328     }
0329 }
0330 
0331 /*
0332  * These exception rules do not apply radar frequencies.
0333  *
0334  * - We enable initiating radiation if the country IE says its fine:
0335  * - If no country IE has been processed and a we determine we have
0336  *   received a beacon on a channel we can enable initiating radiation.
0337  */
0338 static void
0339 ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
0340                   struct ath_regulatory *reg,
0341                   enum nl80211_reg_initiator initiator)
0342 {
0343     enum nl80211_band band;
0344     struct ieee80211_supported_band *sband;
0345     struct ieee80211_channel *ch;
0346     unsigned int i;
0347 
0348     for (band = 0; band < NUM_NL80211_BANDS; band++) {
0349         if (!wiphy->bands[band])
0350             continue;
0351         sband = wiphy->bands[band];
0352         for (i = 0; i < sband->n_channels; i++) {
0353             ch = &sband->channels[i];
0354             __ath_reg_apply_beaconing_flags(wiphy, reg,
0355                             initiator, ch);
0356         }
0357     }
0358 }
0359 
0360 /**
0361  * ath_reg_apply_ir_flags()
0362  * @wiphy: the wiphy to use
0363  * @reg: regulatory structure - used for country selection
0364  * @initiator: the regulatory hint initiator
0365  *
0366  * If no country IE has been received always enable passive scan
0367  * and no-ibss on these channels. This is only done for specific
0368  * regulatory SKUs.
0369  *
0370  * If a country IE has been received check its rule for this
0371  * channel first before enabling active scan. The passive scan
0372  * would have been enforced by the initial processing of our
0373  * custom regulatory domain.
0374  */
0375 static void
0376 ath_reg_apply_ir_flags(struct wiphy *wiphy,
0377                struct ath_regulatory *reg,
0378                enum nl80211_reg_initiator initiator)
0379 {
0380     struct ieee80211_supported_band *sband;
0381 
0382     sband = wiphy->bands[NL80211_BAND_2GHZ];
0383     if (!sband)
0384         return;
0385 
0386     switch(initiator) {
0387     case NL80211_REGDOM_SET_BY_COUNTRY_IE:
0388         ath_force_clear_no_ir_freq(wiphy, 2467);
0389         ath_force_clear_no_ir_freq(wiphy, 2472);
0390         break;
0391     case NL80211_REGDOM_SET_BY_USER:
0392         if (!ath_reg_dyn_country_user_allow(reg))
0393             break;
0394         ath_force_clear_no_ir_freq(wiphy, 2467);
0395         ath_force_clear_no_ir_freq(wiphy, 2472);
0396         break;
0397     default:
0398         ath_force_no_ir_freq(wiphy, 2467);
0399         ath_force_no_ir_freq(wiphy, 2472);
0400     }
0401 }
0402 
0403 /* Always apply Radar/DFS rules on freq range 5500 MHz - 5700 MHz */
0404 static void ath_reg_apply_radar_flags(struct wiphy *wiphy,
0405                       struct ath_regulatory *reg)
0406 {
0407     struct ieee80211_supported_band *sband;
0408     struct ieee80211_channel *ch;
0409     unsigned int i;
0410 
0411     if (!wiphy->bands[NL80211_BAND_5GHZ])
0412         return;
0413 
0414     sband = wiphy->bands[NL80211_BAND_5GHZ];
0415 
0416     for (i = 0; i < sband->n_channels; i++) {
0417         ch = &sband->channels[i];
0418         if (!ath_is_radar_freq(ch->center_freq, reg))
0419             continue;
0420         /* We always enable radar detection/DFS on this
0421          * frequency range. Additionally we also apply on
0422          * this frequency range:
0423          * - If STA mode does not yet have DFS supports disable
0424          *   active scanning
0425          * - If adhoc mode does not support DFS yet then
0426          *   disable adhoc in the frequency.
0427          * - If AP mode does not yet support radar detection/DFS
0428          *   do not allow AP mode
0429          */
0430         if (!(ch->flags & IEEE80211_CHAN_DISABLED))
0431             ch->flags |= IEEE80211_CHAN_RADAR |
0432                      IEEE80211_CHAN_NO_IR;
0433     }
0434 }
0435 
0436 static void ath_reg_apply_world_flags(struct wiphy *wiphy,
0437                       enum nl80211_reg_initiator initiator,
0438                       struct ath_regulatory *reg)
0439 {
0440     switch (reg->regpair->reg_domain) {
0441     case 0x60:
0442     case 0x63:
0443     case 0x66:
0444     case 0x67:
0445     case 0x6C:
0446         ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
0447         break;
0448     case 0x68:
0449         ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
0450         ath_reg_apply_ir_flags(wiphy, reg, initiator);
0451         break;
0452     default:
0453         if (ath_reg_dyn_country_user_allow(reg))
0454             ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
0455     }
0456 }
0457 
0458 u16 ath_regd_find_country_by_name(char *alpha2)
0459 {
0460     unsigned int i;
0461 
0462     for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
0463         if (!memcmp(allCountries[i].isoName, alpha2, 2))
0464             return allCountries[i].countryCode;
0465     }
0466 
0467     return -1;
0468 }
0469 EXPORT_SYMBOL(ath_regd_find_country_by_name);
0470 
0471 static int __ath_reg_dyn_country(struct wiphy *wiphy,
0472                  struct ath_regulatory *reg,
0473                  struct regulatory_request *request)
0474 {
0475     u16 country_code;
0476 
0477     if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
0478         !ath_is_world_regd(reg))
0479         return -EINVAL;
0480 
0481     country_code = ath_regd_find_country_by_name(request->alpha2);
0482     if (country_code == (u16) -1)
0483         return -EINVAL;
0484 
0485     reg->current_rd = COUNTRY_ERD_FLAG;
0486     reg->current_rd |= country_code;
0487 
0488     __ath_regd_init(reg);
0489 
0490     ath_reg_apply_world_flags(wiphy, request->initiator, reg);
0491 
0492     return 0;
0493 }
0494 
0495 static void ath_reg_dyn_country(struct wiphy *wiphy,
0496                 struct ath_regulatory *reg,
0497                 struct regulatory_request *request)
0498 {
0499     if (__ath_reg_dyn_country(wiphy, reg, request))
0500         return;
0501 
0502     printk(KERN_DEBUG "ath: regdomain 0x%0x "
0503               "dynamically updated by %s\n",
0504            reg->current_rd,
0505            reg_initiator_name(request->initiator));
0506 }
0507 
0508 void ath_reg_notifier_apply(struct wiphy *wiphy,
0509                 struct regulatory_request *request,
0510                 struct ath_regulatory *reg)
0511 {
0512     struct ath_common *common = container_of(reg, struct ath_common,
0513                          regulatory);
0514     /* We always apply this */
0515     ath_reg_apply_radar_flags(wiphy, reg);
0516 
0517     /*
0518      * This would happen when we have sent a custom regulatory request
0519      * a world regulatory domain and the scheduler hasn't yet processed
0520      * any pending requests in the queue.
0521      */
0522     if (!request)
0523         return;
0524 
0525     reg->region = request->dfs_region;
0526     switch (request->initiator) {
0527     case NL80211_REGDOM_SET_BY_CORE:
0528         /*
0529          * If common->reg_world_copy is world roaming it means we *were*
0530          * world roaming... so we now have to restore that data.
0531          */
0532         if (!ath_is_world_regd(&common->reg_world_copy))
0533             break;
0534 
0535         memcpy(reg, &common->reg_world_copy,
0536                sizeof(struct ath_regulatory));
0537         break;
0538     case NL80211_REGDOM_SET_BY_DRIVER:
0539         break;
0540     case NL80211_REGDOM_SET_BY_USER:
0541         if (ath_reg_dyn_country_user_allow(reg))
0542             ath_reg_dyn_country(wiphy, reg, request);
0543         break;
0544     case NL80211_REGDOM_SET_BY_COUNTRY_IE:
0545         ath_reg_dyn_country(wiphy, reg, request);
0546         break;
0547     }
0548 }
0549 EXPORT_SYMBOL(ath_reg_notifier_apply);
0550 
0551 static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
0552 {
0553     u16 rd = ath_regd_get_eepromRD(reg);
0554     int i;
0555 
0556     if (rd & COUNTRY_ERD_FLAG) {
0557         /* EEPROM value is a country code */
0558         u16 cc = rd & ~COUNTRY_ERD_FLAG;
0559         printk(KERN_DEBUG
0560                "ath: EEPROM indicates we should expect "
0561             "a country code\n");
0562         for (i = 0; i < ARRAY_SIZE(allCountries); i++)
0563             if (allCountries[i].countryCode == cc)
0564                 return true;
0565     } else {
0566         /* EEPROM value is a regpair value */
0567         if (rd != CTRY_DEFAULT)
0568             printk(KERN_DEBUG "ath: EEPROM indicates we "
0569                    "should expect a direct regpair map\n");
0570         for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
0571             if (regDomainPairs[i].reg_domain == rd)
0572                 return true;
0573     }
0574     printk(KERN_DEBUG
0575          "ath: invalid regulatory domain/country code 0x%x\n", rd);
0576     return false;
0577 }
0578 
0579 /* EEPROM country code to regpair mapping */
0580 static struct country_code_to_enum_rd*
0581 ath_regd_find_country(u16 countryCode)
0582 {
0583     int i;
0584 
0585     for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
0586         if (allCountries[i].countryCode == countryCode)
0587             return &allCountries[i];
0588     }
0589     return NULL;
0590 }
0591 
0592 /* EEPROM rd code to regpair mapping */
0593 static struct country_code_to_enum_rd*
0594 ath_regd_find_country_by_rd(int regdmn)
0595 {
0596     int i;
0597 
0598     for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
0599         if (allCountries[i].regDmnEnum == regdmn)
0600             return &allCountries[i];
0601     }
0602     return NULL;
0603 }
0604 
0605 /* Returns the map of the EEPROM set RD to a country code */
0606 static u16 ath_regd_get_default_country(u16 rd)
0607 {
0608     if (rd & COUNTRY_ERD_FLAG) {
0609         struct country_code_to_enum_rd *country = NULL;
0610         u16 cc = rd & ~COUNTRY_ERD_FLAG;
0611 
0612         country = ath_regd_find_country(cc);
0613         if (country != NULL)
0614             return cc;
0615     }
0616 
0617     return CTRY_DEFAULT;
0618 }
0619 
0620 static struct reg_dmn_pair_mapping*
0621 ath_get_regpair(int regdmn)
0622 {
0623     int i;
0624 
0625     if (regdmn == NO_ENUMRD)
0626         return NULL;
0627     for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
0628         if (regDomainPairs[i].reg_domain == regdmn)
0629             return &regDomainPairs[i];
0630     }
0631     return NULL;
0632 }
0633 
0634 static int
0635 ath_regd_init_wiphy(struct ath_regulatory *reg,
0636             struct wiphy *wiphy,
0637             void (*reg_notifier)(struct wiphy *wiphy,
0638                      struct regulatory_request *request))
0639 {
0640     const struct ieee80211_regdomain *regd;
0641 
0642     wiphy->reg_notifier = reg_notifier;
0643     wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
0644                    REGULATORY_CUSTOM_REG;
0645 
0646     if (ath_is_world_regd(reg)) {
0647         /*
0648          * Anything applied here (prior to wiphy registration) gets
0649          * saved on the wiphy orig_* parameters
0650          */
0651         regd = ath_world_regdomain(reg);
0652         wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_FOLLOW_POWER;
0653     } else {
0654         /*
0655          * This gets applied in the case of the absence of CRDA,
0656          * it's our own custom world regulatory domain, similar to
0657          * cfg80211's but we enable passive scanning.
0658          */
0659         regd = ath_default_world_regdomain();
0660     }
0661 
0662     wiphy_apply_custom_regulatory(wiphy, regd);
0663     ath_reg_apply_radar_flags(wiphy, reg);
0664     ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
0665     return 0;
0666 }
0667 
0668 /*
0669  * Some users have reported their EEPROM programmed with
0670  * 0x8000 set, this is not a supported regulatory domain
0671  * but since we have more than one user with it we need
0672  * a solution for them. We default to 0x64, which is the
0673  * default Atheros world regulatory domain.
0674  */
0675 static void ath_regd_sanitize(struct ath_regulatory *reg)
0676 {
0677     if (reg->current_rd != COUNTRY_ERD_FLAG)
0678         return;
0679     printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
0680     reg->current_rd = 0x64;
0681 }
0682 
0683 static int __ath_regd_init(struct ath_regulatory *reg)
0684 {
0685     struct country_code_to_enum_rd *country = NULL;
0686     u16 regdmn;
0687 
0688     if (!reg)
0689         return -EINVAL;
0690 
0691     ath_regd_sanitize(reg);
0692 
0693     printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
0694 
0695     if (!ath_regd_is_eeprom_valid(reg)) {
0696         pr_err("Invalid EEPROM contents\n");
0697         return -EINVAL;
0698     }
0699 
0700     regdmn = ath_regd_get_eepromRD(reg);
0701     reg->country_code = ath_regd_get_default_country(regdmn);
0702 
0703     if (reg->country_code == CTRY_DEFAULT &&
0704         regdmn == CTRY_DEFAULT) {
0705         printk(KERN_DEBUG "ath: EEPROM indicates default "
0706                "country code should be used\n");
0707         reg->country_code = CTRY_UNITED_STATES;
0708     }
0709 
0710     if (reg->country_code == CTRY_DEFAULT) {
0711         country = NULL;
0712     } else {
0713         printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
0714                "map search\n");
0715         country = ath_regd_find_country(reg->country_code);
0716         if (country == NULL) {
0717             printk(KERN_DEBUG
0718                 "ath: no valid country maps found for "
0719                 "country code: 0x%0x\n",
0720                 reg->country_code);
0721             return -EINVAL;
0722         } else {
0723             regdmn = country->regDmnEnum;
0724             printk(KERN_DEBUG "ath: country maps to "
0725                    "regdmn code: 0x%0x\n",
0726                    regdmn);
0727         }
0728     }
0729 
0730     reg->regpair = ath_get_regpair(regdmn);
0731 
0732     if (!reg->regpair) {
0733         printk(KERN_DEBUG "ath: "
0734             "No regulatory domain pair found, cannot continue\n");
0735         return -EINVAL;
0736     }
0737 
0738     if (!country)
0739         country = ath_regd_find_country_by_rd(regdmn);
0740 
0741     if (country) {
0742         reg->alpha2[0] = country->isoName[0];
0743         reg->alpha2[1] = country->isoName[1];
0744     } else {
0745         reg->alpha2[0] = '0';
0746         reg->alpha2[1] = '0';
0747     }
0748 
0749     printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
0750         reg->alpha2[0], reg->alpha2[1]);
0751     printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
0752         reg->regpair->reg_domain);
0753 
0754     return 0;
0755 }
0756 
0757 int
0758 ath_regd_init(struct ath_regulatory *reg,
0759           struct wiphy *wiphy,
0760           void (*reg_notifier)(struct wiphy *wiphy,
0761                    struct regulatory_request *request))
0762 {
0763     struct ath_common *common = container_of(reg, struct ath_common,
0764                          regulatory);
0765     int r;
0766 
0767     r = __ath_regd_init(reg);
0768     if (r)
0769         return r;
0770 
0771     if (ath_is_world_regd(reg))
0772         memcpy(&common->reg_world_copy, reg,
0773                sizeof(struct ath_regulatory));
0774 
0775     ath_regd_init_wiphy(reg, wiphy, reg_notifier);
0776 
0777     return 0;
0778 }
0779 EXPORT_SYMBOL(ath_regd_init);
0780 
0781 u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
0782               enum nl80211_band band)
0783 {
0784     if (!reg->regpair ||
0785         (reg->country_code == CTRY_DEFAULT &&
0786          is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
0787         return SD_NO_CTL;
0788     }
0789 
0790     if (ath_regd_get_eepromRD(reg) == CTRY_DEFAULT) {
0791         switch (reg->region) {
0792         case NL80211_DFS_FCC:
0793             return CTL_FCC;
0794         case NL80211_DFS_ETSI:
0795             return CTL_ETSI;
0796         case NL80211_DFS_JP:
0797             return CTL_MKK;
0798         default:
0799             break;
0800         }
0801     }
0802 
0803     switch (band) {
0804     case NL80211_BAND_2GHZ:
0805         return reg->regpair->reg_2ghz_ctl;
0806     case NL80211_BAND_5GHZ:
0807         return reg->regpair->reg_5ghz_ctl;
0808     default:
0809         return NO_CTL;
0810     }
0811 }
0812 EXPORT_SYMBOL(ath_regd_get_band_ctl);