regd.c 13 KB

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