regd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (c) 2008 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include "core.h"
  19. #include "hw.h"
  20. #include "regd.h"
  21. #include "regd_common.h"
  22. /*
  23. * This is a set of common rules used by our world regulatory domains.
  24. * We have 12 world regulatory domains. To save space we consolidate
  25. * the regulatory domains in 5 structures by frequency and change
  26. * the flags on our reg_notifier() on a case by case basis.
  27. */
  28. /* Only these channels all allow active scan on all world regulatory domains */
  29. #define ATH9K_2GHZ_CH01_11 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
  30. /* We enable active scan on these a case by case basis by regulatory domain */
  31. #define ATH9K_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
  32. NL80211_RRF_PASSIVE_SCAN)
  33. #define ATH9K_2GHZ_CH14 REG_RULE(2484-10, 2484+10, 40, 0, 20,\
  34. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_OFDM)
  35. /* We allow IBSS on these on a case by case basis by regulatory domain */
  36. #define ATH9K_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 40, 0, 30,\
  37. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
  38. #define ATH9K_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 40, 0, 30,\
  39. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
  40. #define ATH9K_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 40, 0, 30,\
  41. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
  42. #define ATH9K_2GHZ_ALL ATH9K_2GHZ_CH01_11, \
  43. ATH9K_2GHZ_CH12_13, \
  44. ATH9K_2GHZ_CH14
  45. #define ATH9K_5GHZ_ALL ATH9K_5GHZ_5150_5350, \
  46. ATH9K_5GHZ_5470_5850
  47. /* This one skips what we call "mid band" */
  48. #define ATH9K_5GHZ_NO_MIDBAND ATH9K_5GHZ_5150_5350, \
  49. ATH9K_5GHZ_5725_5850
  50. /* Can be used for:
  51. * 0x60, 0x61, 0x62 */
  52. static const struct ieee80211_regdomain ath9k_world_regdom_60_61_62 = {
  53. .n_reg_rules = 5,
  54. .alpha2 = "99",
  55. .reg_rules = {
  56. ATH9K_2GHZ_ALL,
  57. ATH9K_5GHZ_ALL,
  58. }
  59. };
  60. /* Can be used by 0x63 and 0x65 */
  61. static const struct ieee80211_regdomain ath9k_world_regdom_63_65 = {
  62. .n_reg_rules = 4,
  63. .alpha2 = "99",
  64. .reg_rules = {
  65. ATH9K_2GHZ_CH01_11,
  66. ATH9K_2GHZ_CH12_13,
  67. ATH9K_5GHZ_NO_MIDBAND,
  68. }
  69. };
  70. /* Can be used by 0x64 only */
  71. static const struct ieee80211_regdomain ath9k_world_regdom_64 = {
  72. .n_reg_rules = 3,
  73. .alpha2 = "99",
  74. .reg_rules = {
  75. ATH9K_2GHZ_CH01_11,
  76. ATH9K_5GHZ_NO_MIDBAND,
  77. }
  78. };
  79. /* Can be used by 0x66 and 0x69 */
  80. static const struct ieee80211_regdomain ath9k_world_regdom_66_69 = {
  81. .n_reg_rules = 3,
  82. .alpha2 = "99",
  83. .reg_rules = {
  84. ATH9K_2GHZ_CH01_11,
  85. ATH9K_5GHZ_ALL,
  86. }
  87. };
  88. /* Can be used by 0x67, 0x6A and 0x68 */
  89. static const struct ieee80211_regdomain ath9k_world_regdom_67_68_6A = {
  90. .n_reg_rules = 4,
  91. .alpha2 = "99",
  92. .reg_rules = {
  93. ATH9K_2GHZ_CH01_11,
  94. ATH9K_2GHZ_CH12_13,
  95. ATH9K_5GHZ_ALL,
  96. }
  97. };
  98. static u16 ath9k_regd_get_eepromRD(struct ath_hal *ah)
  99. {
  100. return ah->ah_currentRD & ~WORLDWIDE_ROAMING_FLAG;
  101. }
  102. u16 ath9k_regd_get_rd(struct ath_hal *ah)
  103. {
  104. return ath9k_regd_get_eepromRD(ah);
  105. }
  106. bool ath9k_is_world_regd(struct ath_hal *ah)
  107. {
  108. return isWwrSKU(ah);
  109. }
  110. const struct ieee80211_regdomain *ath9k_default_world_regdomain(void)
  111. {
  112. /* this is the most restrictive */
  113. return &ath9k_world_regdom_64;
  114. }
  115. const struct ieee80211_regdomain *ath9k_world_regdomain(struct ath_hal *ah)
  116. {
  117. switch (ah->regpair->regDmnEnum) {
  118. case 0x60:
  119. case 0x61:
  120. case 0x62:
  121. return &ath9k_world_regdom_60_61_62;
  122. case 0x63:
  123. case 0x65:
  124. return &ath9k_world_regdom_63_65;
  125. case 0x64:
  126. return &ath9k_world_regdom_64;
  127. case 0x66:
  128. case 0x69:
  129. return &ath9k_world_regdom_66_69;
  130. case 0x67:
  131. case 0x68:
  132. case 0x6A:
  133. return &ath9k_world_regdom_67_68_6A;
  134. default:
  135. WARN_ON(1);
  136. return ath9k_default_world_regdomain();
  137. }
  138. }
  139. /* Enable adhoc on 5 GHz if allowed by 11d */
  140. static void ath9k_reg_apply_5ghz_adhoc_flags(struct wiphy *wiphy,
  141. enum reg_set_by setby)
  142. {
  143. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  144. struct ath_softc *sc = hw->priv;
  145. struct ieee80211_supported_band *sband;
  146. const struct ieee80211_reg_rule *reg_rule;
  147. struct ieee80211_channel *ch;
  148. unsigned int i;
  149. u32 bandwidth = 0;
  150. int r;
  151. if (setby != REGDOM_SET_BY_COUNTRY_IE)
  152. return;
  153. if (!test_bit(ATH9K_MODE_11A,
  154. sc->sc_ah->ah_caps.wireless_modes))
  155. return;
  156. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  157. for (i = 0; i < sband->n_channels; i++) {
  158. ch = &sband->channels[i];
  159. r = freq_reg_info(wiphy, ch->center_freq,
  160. &bandwidth, &reg_rule);
  161. if (r)
  162. continue;
  163. /* If 11d had a rule for this channel ensure we enable adhoc
  164. * if it allows us to use it. Note that we would have disabled
  165. * it by applying our static world regdomain by default during
  166. * probe */
  167. if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
  168. ch->flags &= ~NL80211_RRF_NO_IBSS;
  169. }
  170. }
  171. /* Allows active scan scan on Ch 12 and 13 */
  172. static void ath9k_reg_apply_active_scan_flags(struct wiphy *wiphy,
  173. enum reg_set_by setby)
  174. {
  175. struct ieee80211_supported_band *sband;
  176. struct ieee80211_channel *ch;
  177. const struct ieee80211_reg_rule *reg_rule;
  178. u32 bandwidth = 0;
  179. int r;
  180. /* Force passive scan on Channels 12-13 */
  181. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  182. /* If no country IE has been received always enable active scan
  183. * on these channels */
  184. if (setby != REGDOM_SET_BY_COUNTRY_IE) {
  185. ch = &sband->channels[11]; /* CH 12 */
  186. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  187. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  188. ch = &sband->channels[12]; /* CH 13 */
  189. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  190. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  191. return;
  192. }
  193. /* If a country IE has been recieved check its rule for this
  194. * channel first before enabling active scan. The passive scan
  195. * would have been enforced by the initial probe processing on
  196. * our custom regulatory domain. */
  197. ch = &sband->channels[11]; /* CH 12 */
  198. r = freq_reg_info(wiphy, ch->center_freq, &bandwidth, &reg_rule);
  199. if (!r) {
  200. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  201. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  202. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  203. }
  204. ch = &sband->channels[12]; /* CH 13 */
  205. r = freq_reg_info(wiphy, ch->center_freq, &bandwidth, &reg_rule);
  206. if (!r) {
  207. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  208. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  209. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  210. }
  211. }
  212. /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
  213. void ath9k_reg_apply_radar_flags(struct wiphy *wiphy)
  214. {
  215. struct ieee80211_supported_band *sband;
  216. struct ieee80211_channel *ch;
  217. unsigned int i;
  218. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  219. return;
  220. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  221. for (i = 0; i < sband->n_channels; i++) {
  222. ch = &sband->channels[i];
  223. if (ch->center_freq < 5260)
  224. continue;
  225. if (ch->center_freq > 5700)
  226. continue;
  227. /* We always enable radar detection/DFS on this
  228. * frequency range. Additionally we also apply on
  229. * this frequency range:
  230. * - If STA mode does not yet have DFS supports disable
  231. * active scanning
  232. * - If adhoc mode does not support DFS yet then
  233. * disable adhoc in the frequency.
  234. * - If AP mode does not yet support radar detection/DFS
  235. * do not allow AP mode
  236. */
  237. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  238. ch->flags |= IEEE80211_CHAN_RADAR |
  239. IEEE80211_CHAN_NO_IBSS |
  240. IEEE80211_CHAN_PASSIVE_SCAN;
  241. }
  242. }
  243. void ath9k_reg_apply_world_flags(struct wiphy *wiphy, enum reg_set_by setby)
  244. {
  245. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  246. struct ath_softc *sc = hw->priv;
  247. struct ath_hal *ah = sc->sc_ah;
  248. switch (ah->regpair->regDmnEnum) {
  249. case 0x60:
  250. case 0x63:
  251. case 0x66:
  252. case 0x67:
  253. ath9k_reg_apply_5ghz_adhoc_flags(wiphy, setby);
  254. break;
  255. case 0x68:
  256. ath9k_reg_apply_5ghz_adhoc_flags(wiphy, setby);
  257. ath9k_reg_apply_active_scan_flags(wiphy, setby);
  258. break;
  259. }
  260. return;
  261. }
  262. int ath9k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  263. {
  264. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  265. struct ath_softc *sc = hw->priv;
  266. /* We always apply this */
  267. ath9k_reg_apply_radar_flags(wiphy);
  268. switch (request->initiator) {
  269. case REGDOM_SET_BY_DRIVER:
  270. case REGDOM_SET_BY_INIT:
  271. case REGDOM_SET_BY_CORE:
  272. case REGDOM_SET_BY_USER:
  273. break;
  274. case REGDOM_SET_BY_COUNTRY_IE:
  275. if (ath9k_is_world_regd(sc->sc_ah))
  276. ath9k_reg_apply_world_flags(wiphy, request->initiator);
  277. break;
  278. }
  279. return 0;
  280. }
  281. bool ath9k_regd_is_eeprom_valid(struct ath_hal *ah)
  282. {
  283. u16 rd = ath9k_regd_get_eepromRD(ah);
  284. int i;
  285. if (rd & COUNTRY_ERD_FLAG) {
  286. /* EEPROM value is a country code */
  287. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  288. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  289. if (allCountries[i].countryCode == cc)
  290. return true;
  291. } else {
  292. /* EEPROM value is a regpair value */
  293. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  294. if (regDomainPairs[i].regDmnEnum == rd)
  295. return true;
  296. }
  297. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  298. "invalid regulatory domain/country code 0x%x\n", rd);
  299. return false;
  300. }
  301. /* EEPROM country code to regpair mapping */
  302. static struct country_code_to_enum_rd*
  303. ath9k_regd_find_country(u16 countryCode)
  304. {
  305. int i;
  306. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  307. if (allCountries[i].countryCode == countryCode)
  308. return &allCountries[i];
  309. }
  310. return NULL;
  311. }
  312. /* EEPROM rd code to regpair mapping */
  313. static struct country_code_to_enum_rd*
  314. ath9k_regd_find_country_by_rd(int regdmn)
  315. {
  316. int i;
  317. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  318. if (allCountries[i].regDmnEnum == regdmn)
  319. return &allCountries[i];
  320. }
  321. return NULL;
  322. }
  323. /* Returns the map of the EEPROM set RD to a country code */
  324. static u16 ath9k_regd_get_default_country(struct ath_hal *ah)
  325. {
  326. u16 rd;
  327. rd = ath9k_regd_get_eepromRD(ah);
  328. if (rd & COUNTRY_ERD_FLAG) {
  329. struct country_code_to_enum_rd *country = NULL;
  330. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  331. country = ath9k_regd_find_country(cc);
  332. if (country != NULL)
  333. return cc;
  334. }
  335. return CTRY_DEFAULT;
  336. }
  337. static struct reg_dmn_pair_mapping*
  338. ath9k_get_regpair(int regdmn)
  339. {
  340. int i;
  341. if (regdmn == NO_ENUMRD)
  342. return NULL;
  343. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  344. if (regDomainPairs[i].regDmnEnum == regdmn)
  345. return &regDomainPairs[i];
  346. }
  347. return NULL;
  348. }
  349. int ath9k_regd_init(struct ath_hal *ah)
  350. {
  351. struct country_code_to_enum_rd *country = NULL;
  352. int regdmn;
  353. if (!ath9k_regd_is_eeprom_valid(ah)) {
  354. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  355. "Invalid EEPROM contents\n");
  356. return -EINVAL;
  357. }
  358. ah->ah_countryCode = ath9k_regd_get_default_country(ah);
  359. if (ah->ah_countryCode == CTRY_DEFAULT &&
  360. ath9k_regd_get_eepromRD(ah) == CTRY_DEFAULT)
  361. ah->ah_countryCode = CTRY_UNITED_STATES;
  362. if (ah->ah_countryCode == CTRY_DEFAULT) {
  363. regdmn = ath9k_regd_get_eepromRD(ah);
  364. country = NULL;
  365. } else {
  366. country = ath9k_regd_find_country(ah->ah_countryCode);
  367. if (country == NULL) {
  368. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  369. "Country is NULL!!!!, cc= %d\n",
  370. ah->ah_countryCode);
  371. return -EINVAL;
  372. } else
  373. regdmn = country->regDmnEnum;
  374. }
  375. ah->ah_currentRDInUse = regdmn;
  376. ah->regpair = ath9k_get_regpair(regdmn);
  377. if (!ah->regpair) {
  378. DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
  379. "No regulatory domain pair found, cannot continue\n");
  380. return -EINVAL;
  381. }
  382. if (!country)
  383. country = ath9k_regd_find_country_by_rd(regdmn);
  384. if (country) {
  385. ah->alpha2[0] = country->isoName[0];
  386. ah->alpha2[1] = country->isoName[1];
  387. } else {
  388. ah->alpha2[0] = '0';
  389. ah->alpha2[1] = '0';
  390. }
  391. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  392. "Country alpha2 being used: %c%c\n"
  393. "Regpair detected: 0x%0x\n",
  394. ah->alpha2[0], ah->alpha2[1],
  395. ah->regpair->regDmnEnum);
  396. return 0;
  397. }
  398. u32 ath9k_regd_get_ctl(struct ath_hal *ah, struct ath9k_channel *chan)
  399. {
  400. u32 ctl = NO_CTL;
  401. if (!ah->regpair ||
  402. (ah->ah_countryCode == CTRY_DEFAULT && isWwrSKU(ah))) {
  403. if (IS_CHAN_B(chan))
  404. ctl = SD_NO_CTL | CTL_11B;
  405. else if (IS_CHAN_G(chan))
  406. ctl = SD_NO_CTL | CTL_11G;
  407. else
  408. ctl = SD_NO_CTL | CTL_11A;
  409. return ctl;
  410. }
  411. if (IS_CHAN_B(chan))
  412. ctl = ah->regpair->reg_2ghz_ctl | CTL_11B;
  413. else if (IS_CHAN_G(chan))
  414. ctl = ah->regpair->reg_5ghz_ctl | CTL_11G;
  415. else
  416. ctl = ah->regpair->reg_5ghz_ctl | CTL_11A;
  417. return ctl;
  418. }