regd.c 14 KB

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