regd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. /* Frequency is one where radar detection is required */
  140. static bool ath9k_is_radar_freq(u16 center_freq)
  141. {
  142. return (center_freq >= 5260 && center_freq <= 5700);
  143. }
  144. /*
  145. * Enable adhoc on 5 GHz if allowed by 11d.
  146. * Remove passive scan if channel is allowed by 11d,
  147. * except when on radar frequencies.
  148. */
  149. static void ath9k_reg_apply_5ghz_beaconing_flags(struct wiphy *wiphy,
  150. enum reg_set_by setby)
  151. {
  152. struct ieee80211_supported_band *sband;
  153. const struct ieee80211_reg_rule *reg_rule;
  154. struct ieee80211_channel *ch;
  155. unsigned int i;
  156. u32 bandwidth = 0;
  157. int r;
  158. if (setby != REGDOM_SET_BY_COUNTRY_IE)
  159. return;
  160. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  161. return;
  162. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  163. for (i = 0; i < sband->n_channels; i++) {
  164. ch = &sband->channels[i];
  165. r = freq_reg_info(wiphy, ch->center_freq,
  166. &bandwidth, &reg_rule);
  167. if (r)
  168. continue;
  169. /* If 11d had a rule for this channel ensure we enable adhoc
  170. * if it allows us to use it. Note that we would have disabled
  171. * it by applying our static world regdomain by default during
  172. * probe */
  173. if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
  174. ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
  175. if (!ath9k_is_radar_freq(ch->center_freq))
  176. continue;
  177. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  178. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  179. }
  180. }
  181. /* Allows active scan scan on Ch 12 and 13 */
  182. static void ath9k_reg_apply_active_scan_flags(struct wiphy *wiphy,
  183. enum reg_set_by setby)
  184. {
  185. struct ieee80211_supported_band *sband;
  186. struct ieee80211_channel *ch;
  187. const struct ieee80211_reg_rule *reg_rule;
  188. u32 bandwidth = 0;
  189. int r;
  190. /* Force passive scan on Channels 12-13 */
  191. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  192. /* If no country IE has been received always enable active scan
  193. * on these channels */
  194. if (setby != REGDOM_SET_BY_COUNTRY_IE) {
  195. ch = &sband->channels[11]; /* CH 12 */
  196. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  197. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  198. ch = &sband->channels[12]; /* CH 13 */
  199. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  200. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  201. return;
  202. }
  203. /* If a country IE has been recieved check its rule for this
  204. * channel first before enabling active scan. The passive scan
  205. * would have been enforced by the initial probe processing on
  206. * our custom regulatory domain. */
  207. ch = &sband->channels[11]; /* CH 12 */
  208. r = freq_reg_info(wiphy, ch->center_freq, &bandwidth, &reg_rule);
  209. if (!r) {
  210. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  211. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  212. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  213. }
  214. ch = &sband->channels[12]; /* CH 13 */
  215. r = freq_reg_info(wiphy, ch->center_freq, &bandwidth, &reg_rule);
  216. if (!r) {
  217. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  218. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  219. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  220. }
  221. }
  222. /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
  223. void ath9k_reg_apply_radar_flags(struct wiphy *wiphy)
  224. {
  225. struct ieee80211_supported_band *sband;
  226. struct ieee80211_channel *ch;
  227. unsigned int i;
  228. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  229. return;
  230. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  231. for (i = 0; i < sband->n_channels; i++) {
  232. ch = &sband->channels[i];
  233. if (!ath9k_is_radar_freq(ch->center_freq))
  234. continue;
  235. /* We always enable radar detection/DFS on this
  236. * frequency range. Additionally we also apply on
  237. * this frequency range:
  238. * - If STA mode does not yet have DFS supports disable
  239. * active scanning
  240. * - If adhoc mode does not support DFS yet then
  241. * disable adhoc in the frequency.
  242. * - If AP mode does not yet support radar detection/DFS
  243. * do not allow AP mode
  244. */
  245. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  246. ch->flags |= IEEE80211_CHAN_RADAR |
  247. IEEE80211_CHAN_NO_IBSS |
  248. IEEE80211_CHAN_PASSIVE_SCAN;
  249. }
  250. }
  251. void ath9k_reg_apply_world_flags(struct wiphy *wiphy, enum reg_set_by setby)
  252. {
  253. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  254. struct ath_softc *sc = hw->priv;
  255. struct ath_hal *ah = sc->sc_ah;
  256. switch (ah->regpair->regDmnEnum) {
  257. case 0x60:
  258. case 0x63:
  259. case 0x66:
  260. case 0x67:
  261. ath9k_reg_apply_5ghz_beaconing_flags(wiphy, setby);
  262. break;
  263. case 0x68:
  264. ath9k_reg_apply_5ghz_beaconing_flags(wiphy, setby);
  265. ath9k_reg_apply_active_scan_flags(wiphy, setby);
  266. break;
  267. }
  268. return;
  269. }
  270. int ath9k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  271. {
  272. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  273. struct ath_softc *sc = hw->priv;
  274. /* We always apply this */
  275. ath9k_reg_apply_radar_flags(wiphy);
  276. switch (request->initiator) {
  277. case REGDOM_SET_BY_DRIVER:
  278. case REGDOM_SET_BY_INIT:
  279. case REGDOM_SET_BY_CORE:
  280. case REGDOM_SET_BY_USER:
  281. break;
  282. case REGDOM_SET_BY_COUNTRY_IE:
  283. if (ath9k_is_world_regd(sc->sc_ah))
  284. ath9k_reg_apply_world_flags(wiphy, request->initiator);
  285. break;
  286. }
  287. return 0;
  288. }
  289. bool ath9k_regd_is_eeprom_valid(struct ath_hal *ah)
  290. {
  291. u16 rd = ath9k_regd_get_eepromRD(ah);
  292. int i;
  293. if (rd & COUNTRY_ERD_FLAG) {
  294. /* EEPROM value is a country code */
  295. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  296. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  297. if (allCountries[i].countryCode == cc)
  298. return true;
  299. } else {
  300. /* EEPROM value is a regpair value */
  301. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  302. if (regDomainPairs[i].regDmnEnum == rd)
  303. return true;
  304. }
  305. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  306. "invalid regulatory domain/country code 0x%x\n", rd);
  307. return false;
  308. }
  309. /* EEPROM country code to regpair mapping */
  310. static struct country_code_to_enum_rd*
  311. ath9k_regd_find_country(u16 countryCode)
  312. {
  313. int i;
  314. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  315. if (allCountries[i].countryCode == countryCode)
  316. return &allCountries[i];
  317. }
  318. return NULL;
  319. }
  320. /* EEPROM rd code to regpair mapping */
  321. static struct country_code_to_enum_rd*
  322. ath9k_regd_find_country_by_rd(int regdmn)
  323. {
  324. int i;
  325. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  326. if (allCountries[i].regDmnEnum == regdmn)
  327. return &allCountries[i];
  328. }
  329. return NULL;
  330. }
  331. /* Returns the map of the EEPROM set RD to a country code */
  332. static u16 ath9k_regd_get_default_country(struct ath_hal *ah)
  333. {
  334. u16 rd;
  335. rd = ath9k_regd_get_eepromRD(ah);
  336. if (rd & COUNTRY_ERD_FLAG) {
  337. struct country_code_to_enum_rd *country = NULL;
  338. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  339. country = ath9k_regd_find_country(cc);
  340. if (country != NULL)
  341. return cc;
  342. }
  343. return CTRY_DEFAULT;
  344. }
  345. static struct reg_dmn_pair_mapping*
  346. ath9k_get_regpair(int regdmn)
  347. {
  348. int i;
  349. if (regdmn == NO_ENUMRD)
  350. return NULL;
  351. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  352. if (regDomainPairs[i].regDmnEnum == regdmn)
  353. return &regDomainPairs[i];
  354. }
  355. return NULL;
  356. }
  357. int ath9k_regd_init(struct ath_hal *ah)
  358. {
  359. struct country_code_to_enum_rd *country = NULL;
  360. int regdmn;
  361. if (!ath9k_regd_is_eeprom_valid(ah)) {
  362. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  363. "Invalid EEPROM contents\n");
  364. return -EINVAL;
  365. }
  366. ah->ah_countryCode = ath9k_regd_get_default_country(ah);
  367. if (ah->ah_countryCode == CTRY_DEFAULT &&
  368. ath9k_regd_get_eepromRD(ah) == CTRY_DEFAULT)
  369. ah->ah_countryCode = CTRY_UNITED_STATES;
  370. if (ah->ah_countryCode == CTRY_DEFAULT) {
  371. regdmn = ath9k_regd_get_eepromRD(ah);
  372. country = NULL;
  373. } else {
  374. country = ath9k_regd_find_country(ah->ah_countryCode);
  375. if (country == NULL) {
  376. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  377. "Country is NULL!!!!, cc= %d\n",
  378. ah->ah_countryCode);
  379. return -EINVAL;
  380. } else
  381. regdmn = country->regDmnEnum;
  382. }
  383. ah->ah_currentRDInUse = regdmn;
  384. ah->regpair = ath9k_get_regpair(regdmn);
  385. if (!ah->regpair) {
  386. DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
  387. "No regulatory domain pair found, cannot continue\n");
  388. return -EINVAL;
  389. }
  390. if (!country)
  391. country = ath9k_regd_find_country_by_rd(regdmn);
  392. if (country) {
  393. ah->alpha2[0] = country->isoName[0];
  394. ah->alpha2[1] = country->isoName[1];
  395. } else {
  396. ah->alpha2[0] = '0';
  397. ah->alpha2[1] = '0';
  398. }
  399. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  400. "Country alpha2 being used: %c%c\n"
  401. "Regpair detected: 0x%0x\n",
  402. ah->alpha2[0], ah->alpha2[1],
  403. ah->regpair->regDmnEnum);
  404. return 0;
  405. }
  406. u32 ath9k_regd_get_ctl(struct ath_hal *ah, struct ath9k_channel *chan)
  407. {
  408. u32 ctl = NO_CTL;
  409. if (!ah->regpair ||
  410. (ah->ah_countryCode == CTRY_DEFAULT && isWwrSKU(ah))) {
  411. if (IS_CHAN_B(chan))
  412. ctl = SD_NO_CTL | CTL_11B;
  413. else if (IS_CHAN_G(chan))
  414. ctl = SD_NO_CTL | CTL_11G;
  415. else
  416. ctl = SD_NO_CTL | CTL_11A;
  417. return ctl;
  418. }
  419. if (IS_CHAN_B(chan))
  420. ctl = ah->regpair->reg_2ghz_ctl | CTL_11B;
  421. else if (IS_CHAN_G(chan))
  422. ctl = ah->regpair->reg_5ghz_ctl | CTL_11G;
  423. else
  424. ctl = ah->regpair->reg_5ghz_ctl | CTL_11A;
  425. return ctl;
  426. }