regd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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_wiphy *aphy = hw->priv;
  277. struct ath_softc *sc = aphy->sc;
  278. struct ath_hw *ah = sc->sc_ah;
  279. switch (ah->regulatory.regpair->regDmnEnum) {
  280. case 0x60:
  281. case 0x63:
  282. case 0x66:
  283. case 0x67:
  284. ath9k_reg_apply_beaconing_flags(wiphy, setby);
  285. break;
  286. case 0x68:
  287. ath9k_reg_apply_beaconing_flags(wiphy, setby);
  288. ath9k_reg_apply_active_scan_flags(wiphy, setby);
  289. break;
  290. }
  291. return;
  292. }
  293. int ath9k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  294. {
  295. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  296. struct ath_wiphy *aphy = hw->priv;
  297. struct ath_softc *sc = aphy->sc;
  298. /* We always apply this */
  299. ath9k_reg_apply_radar_flags(wiphy);
  300. switch (request->initiator) {
  301. case REGDOM_SET_BY_DRIVER:
  302. case REGDOM_SET_BY_INIT:
  303. case REGDOM_SET_BY_CORE:
  304. case REGDOM_SET_BY_USER:
  305. break;
  306. case REGDOM_SET_BY_COUNTRY_IE:
  307. if (ath9k_is_world_regd(sc->sc_ah))
  308. ath9k_reg_apply_world_flags(wiphy, request->initiator);
  309. break;
  310. }
  311. return 0;
  312. }
  313. bool ath9k_regd_is_eeprom_valid(struct ath_hw *ah)
  314. {
  315. u16 rd = ath9k_regd_get_eepromRD(ah);
  316. int i;
  317. if (rd & COUNTRY_ERD_FLAG) {
  318. /* EEPROM value is a country code */
  319. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  320. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  321. if (allCountries[i].countryCode == cc)
  322. return true;
  323. } else {
  324. /* EEPROM value is a regpair value */
  325. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  326. if (regDomainPairs[i].regDmnEnum == rd)
  327. return true;
  328. }
  329. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  330. "invalid regulatory domain/country code 0x%x\n", rd);
  331. return false;
  332. }
  333. /* EEPROM country code to regpair mapping */
  334. static struct country_code_to_enum_rd*
  335. ath9k_regd_find_country(u16 countryCode)
  336. {
  337. int i;
  338. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  339. if (allCountries[i].countryCode == countryCode)
  340. return &allCountries[i];
  341. }
  342. return NULL;
  343. }
  344. /* EEPROM rd code to regpair mapping */
  345. static struct country_code_to_enum_rd*
  346. ath9k_regd_find_country_by_rd(int regdmn)
  347. {
  348. int i;
  349. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  350. if (allCountries[i].regDmnEnum == regdmn)
  351. return &allCountries[i];
  352. }
  353. return NULL;
  354. }
  355. /* Returns the map of the EEPROM set RD to a country code */
  356. static u16 ath9k_regd_get_default_country(u16 rd)
  357. {
  358. if (rd & COUNTRY_ERD_FLAG) {
  359. struct country_code_to_enum_rd *country = NULL;
  360. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  361. country = ath9k_regd_find_country(cc);
  362. if (country != NULL)
  363. return cc;
  364. }
  365. return CTRY_DEFAULT;
  366. }
  367. static struct reg_dmn_pair_mapping*
  368. ath9k_get_regpair(int regdmn)
  369. {
  370. int i;
  371. if (regdmn == NO_ENUMRD)
  372. return NULL;
  373. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  374. if (regDomainPairs[i].regDmnEnum == regdmn)
  375. return &regDomainPairs[i];
  376. }
  377. return NULL;
  378. }
  379. int ath9k_regd_init(struct ath_hw *ah)
  380. {
  381. struct country_code_to_enum_rd *country = NULL;
  382. u16 regdmn;
  383. if (!ath9k_regd_is_eeprom_valid(ah)) {
  384. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  385. "Invalid EEPROM contents\n");
  386. return -EINVAL;
  387. }
  388. regdmn = ath9k_regd_get_eepromRD(ah);
  389. ah->regulatory.country_code = ath9k_regd_get_default_country(regdmn);
  390. if (ah->regulatory.country_code == CTRY_DEFAULT &&
  391. regdmn == CTRY_DEFAULT)
  392. ah->regulatory.country_code = CTRY_UNITED_STATES;
  393. if (ah->regulatory.country_code == CTRY_DEFAULT) {
  394. country = NULL;
  395. } else {
  396. country = ath9k_regd_find_country(ah->regulatory.country_code);
  397. if (country == NULL) {
  398. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  399. "Country is NULL!!!!, cc= %d\n",
  400. ah->regulatory.country_code);
  401. return -EINVAL;
  402. } else
  403. regdmn = country->regDmnEnum;
  404. }
  405. ah->regulatory.regpair = ath9k_get_regpair(regdmn);
  406. if (!ah->regulatory.regpair) {
  407. DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
  408. "No regulatory domain pair found, cannot continue\n");
  409. return -EINVAL;
  410. }
  411. if (!country)
  412. country = ath9k_regd_find_country_by_rd(regdmn);
  413. if (country) {
  414. ah->regulatory.alpha2[0] = country->isoName[0];
  415. ah->regulatory.alpha2[1] = country->isoName[1];
  416. } else {
  417. ah->regulatory.alpha2[0] = '0';
  418. ah->regulatory.alpha2[1] = '0';
  419. }
  420. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  421. "Country alpha2 being used: %c%c\n"
  422. "Regulatory.Regpair detected: 0x%0x\n",
  423. ah->regulatory.alpha2[0], ah->regulatory.alpha2[1],
  424. ah->regulatory.regpair->regDmnEnum);
  425. return 0;
  426. }
  427. u32 ath9k_regd_get_ctl(struct ath_hw *ah, struct ath9k_channel *chan)
  428. {
  429. u32 ctl = NO_CTL;
  430. if (!ah->regulatory.regpair ||
  431. (ah->regulatory.country_code == CTRY_DEFAULT &&
  432. is_wwr_sku(ath9k_regd_get_eepromRD(ah)))) {
  433. if (IS_CHAN_B(chan))
  434. ctl = SD_NO_CTL | CTL_11B;
  435. else if (IS_CHAN_G(chan))
  436. ctl = SD_NO_CTL | CTL_11G;
  437. else
  438. ctl = SD_NO_CTL | CTL_11A;
  439. return ctl;
  440. }
  441. if (IS_CHAN_B(chan))
  442. ctl = ah->regulatory.regpair->reg_2ghz_ctl | CTL_11B;
  443. else if (IS_CHAN_G(chan))
  444. ctl = ah->regulatory.regpair->reg_2ghz_ctl | CTL_11G;
  445. else
  446. ctl = ah->regulatory.regpair->reg_5ghz_ctl | CTL_11A;
  447. return ctl;
  448. }