regd.c 14 KB

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