regd.c 14 KB

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