regd.c 13 KB

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