regd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/cfg80211.h>
  19. #include <net/mac80211.h>
  20. #include "regd.h"
  21. #include "regd_common.h"
  22. /*
  23. * This is a set of common rules used by our world regulatory domains.
  24. * We have 12 world regulatory domains. To save space we consolidate
  25. * the regulatory domains in 5 structures by frequency and change
  26. * the flags on our reg_notifier() on a case by case basis.
  27. */
  28. /* Only these channels all allow active scan on all world regulatory domains */
  29. #define ATH9K_2GHZ_CH01_11 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
  30. /* We enable active scan on these a case by case basis by regulatory domain */
  31. #define ATH9K_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
  32. NL80211_RRF_PASSIVE_SCAN)
  33. #define ATH9K_2GHZ_CH14 REG_RULE(2484-10, 2484+10, 40, 0, 20,\
  34. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_OFDM)
  35. /* We allow IBSS on these on a case by case basis by regulatory domain */
  36. #define ATH9K_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 40, 0, 30,\
  37. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
  38. #define ATH9K_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 40, 0, 30,\
  39. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
  40. #define ATH9K_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 40, 0, 30,\
  41. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
  42. #define ATH9K_2GHZ_ALL ATH9K_2GHZ_CH01_11, \
  43. ATH9K_2GHZ_CH12_13, \
  44. ATH9K_2GHZ_CH14
  45. #define ATH9K_5GHZ_ALL ATH9K_5GHZ_5150_5350, \
  46. ATH9K_5GHZ_5470_5850
  47. /* This one skips what we call "mid band" */
  48. #define ATH9K_5GHZ_NO_MIDBAND ATH9K_5GHZ_5150_5350, \
  49. ATH9K_5GHZ_5725_5850
  50. /* Can be used for:
  51. * 0x60, 0x61, 0x62 */
  52. static const struct ieee80211_regdomain ath_world_regdom_60_61_62 = {
  53. .n_reg_rules = 5,
  54. .alpha2 = "99",
  55. .reg_rules = {
  56. ATH9K_2GHZ_ALL,
  57. ATH9K_5GHZ_ALL,
  58. }
  59. };
  60. /* Can be used by 0x63 and 0x65 */
  61. static const struct ieee80211_regdomain ath_world_regdom_63_65 = {
  62. .n_reg_rules = 4,
  63. .alpha2 = "99",
  64. .reg_rules = {
  65. ATH9K_2GHZ_CH01_11,
  66. ATH9K_2GHZ_CH12_13,
  67. ATH9K_5GHZ_NO_MIDBAND,
  68. }
  69. };
  70. /* Can be used by 0x64 only */
  71. static const struct ieee80211_regdomain ath_world_regdom_64 = {
  72. .n_reg_rules = 3,
  73. .alpha2 = "99",
  74. .reg_rules = {
  75. ATH9K_2GHZ_CH01_11,
  76. ATH9K_5GHZ_NO_MIDBAND,
  77. }
  78. };
  79. /* Can be used by 0x66 and 0x69 */
  80. static const struct ieee80211_regdomain ath_world_regdom_66_69 = {
  81. .n_reg_rules = 3,
  82. .alpha2 = "99",
  83. .reg_rules = {
  84. ATH9K_2GHZ_CH01_11,
  85. ATH9K_5GHZ_ALL,
  86. }
  87. };
  88. /* Can be used by 0x67, 0x6A and 0x68 */
  89. static const struct ieee80211_regdomain ath_world_regdom_67_68_6A = {
  90. .n_reg_rules = 4,
  91. .alpha2 = "99",
  92. .reg_rules = {
  93. ATH9K_2GHZ_CH01_11,
  94. ATH9K_2GHZ_CH12_13,
  95. ATH9K_5GHZ_ALL,
  96. }
  97. };
  98. static inline bool is_wwr_sku(u16 regd)
  99. {
  100. return ((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
  101. (regd == WORLD);
  102. }
  103. static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
  104. {
  105. return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
  106. }
  107. bool ath_is_world_regd(struct ath_regulatory *reg)
  108. {
  109. return is_wwr_sku(ath_regd_get_eepromRD(reg));
  110. }
  111. EXPORT_SYMBOL(ath_is_world_regd);
  112. static const struct ieee80211_regdomain *ath_default_world_regdomain(void)
  113. {
  114. /* this is the most restrictive */
  115. return &ath_world_regdom_64;
  116. }
  117. static const struct
  118. ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
  119. {
  120. switch (reg->regpair->regDmnEnum) {
  121. case 0x60:
  122. case 0x61:
  123. case 0x62:
  124. return &ath_world_regdom_60_61_62;
  125. case 0x63:
  126. case 0x65:
  127. return &ath_world_regdom_63_65;
  128. case 0x64:
  129. return &ath_world_regdom_64;
  130. case 0x66:
  131. case 0x69:
  132. return &ath_world_regdom_66_69;
  133. case 0x67:
  134. case 0x68:
  135. case 0x6A:
  136. return &ath_world_regdom_67_68_6A;
  137. default:
  138. WARN_ON(1);
  139. return ath_default_world_regdomain();
  140. }
  141. }
  142. /* Frequency is one where radar detection is required */
  143. static bool ath_is_radar_freq(u16 center_freq)
  144. {
  145. return (center_freq >= 5260 && center_freq <= 5700);
  146. }
  147. /*
  148. * N.B: These exception rules do not apply radar freqs.
  149. *
  150. * - We enable adhoc (or beaconing) if allowed by 11d
  151. * - We enable active scan if the channel is allowed by 11d
  152. * - If no country IE has been processed and a we determine we have
  153. * received a beacon on a channel we can enable active scan and
  154. * adhoc (or beaconing).
  155. */
  156. static void
  157. ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  158. enum nl80211_reg_initiator initiator)
  159. {
  160. enum ieee80211_band band;
  161. struct ieee80211_supported_band *sband;
  162. const struct ieee80211_reg_rule *reg_rule;
  163. struct ieee80211_channel *ch;
  164. unsigned int i;
  165. u32 bandwidth = 0;
  166. int r;
  167. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  168. if (!wiphy->bands[band])
  169. continue;
  170. sband = wiphy->bands[band];
  171. for (i = 0; i < sband->n_channels; i++) {
  172. ch = &sband->channels[i];
  173. if (ath_is_radar_freq(ch->center_freq) ||
  174. (ch->flags & IEEE80211_CHAN_RADAR))
  175. continue;
  176. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  177. r = freq_reg_info(wiphy,
  178. ch->center_freq,
  179. bandwidth,
  180. &reg_rule);
  181. if (r)
  182. continue;
  183. /*
  184. * If 11d had a rule for this channel ensure
  185. * we enable adhoc/beaconing if it allows us to
  186. * use it. Note that we would have disabled it
  187. * by applying our static world regdomain by
  188. * default during init, prior to calling our
  189. * regulatory_hint().
  190. */
  191. if (!(reg_rule->flags &
  192. NL80211_RRF_NO_IBSS))
  193. ch->flags &=
  194. ~IEEE80211_CHAN_NO_IBSS;
  195. if (!(reg_rule->flags &
  196. NL80211_RRF_PASSIVE_SCAN))
  197. ch->flags &=
  198. ~IEEE80211_CHAN_PASSIVE_SCAN;
  199. } else {
  200. if (ch->beacon_found)
  201. ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
  202. IEEE80211_CHAN_PASSIVE_SCAN);
  203. }
  204. }
  205. }
  206. }
  207. /* Allows active scan scan on Ch 12 and 13 */
  208. static void
  209. ath_reg_apply_active_scan_flags(struct wiphy *wiphy,
  210. enum nl80211_reg_initiator initiator)
  211. {
  212. struct ieee80211_supported_band *sband;
  213. struct ieee80211_channel *ch;
  214. const struct ieee80211_reg_rule *reg_rule;
  215. u32 bandwidth = 0;
  216. int r;
  217. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  218. /*
  219. * If no country IE has been received always enable active scan
  220. * on these channels. This is only done for specific regulatory SKUs
  221. */
  222. if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  223. ch = &sband->channels[11]; /* CH 12 */
  224. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  225. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  226. ch = &sband->channels[12]; /* CH 13 */
  227. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  228. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  229. return;
  230. }
  231. /*
  232. * If a country IE has been recieved check its rule for this
  233. * channel first before enabling active scan. The passive scan
  234. * would have been enforced by the initial processing of our
  235. * custom regulatory domain.
  236. */
  237. ch = &sband->channels[11]; /* CH 12 */
  238. r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
  239. if (!r) {
  240. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  241. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  242. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  243. }
  244. ch = &sband->channels[12]; /* CH 13 */
  245. r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
  246. if (!r) {
  247. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  248. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  249. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  250. }
  251. }
  252. /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
  253. static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
  254. {
  255. struct ieee80211_supported_band *sband;
  256. struct ieee80211_channel *ch;
  257. unsigned int i;
  258. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  259. return;
  260. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  261. for (i = 0; i < sband->n_channels; i++) {
  262. ch = &sband->channels[i];
  263. if (!ath_is_radar_freq(ch->center_freq))
  264. continue;
  265. /* We always enable radar detection/DFS on this
  266. * frequency range. Additionally we also apply on
  267. * this frequency range:
  268. * - If STA mode does not yet have DFS supports disable
  269. * active scanning
  270. * - If adhoc mode does not support DFS yet then
  271. * disable adhoc in the frequency.
  272. * - If AP mode does not yet support radar detection/DFS
  273. * do not allow AP mode
  274. */
  275. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  276. ch->flags |= IEEE80211_CHAN_RADAR |
  277. IEEE80211_CHAN_NO_IBSS |
  278. IEEE80211_CHAN_PASSIVE_SCAN;
  279. }
  280. }
  281. static void ath_reg_apply_world_flags(struct wiphy *wiphy,
  282. enum nl80211_reg_initiator initiator,
  283. struct ath_regulatory *reg)
  284. {
  285. switch (reg->regpair->regDmnEnum) {
  286. case 0x60:
  287. case 0x63:
  288. case 0x66:
  289. case 0x67:
  290. ath_reg_apply_beaconing_flags(wiphy, initiator);
  291. break;
  292. case 0x68:
  293. ath_reg_apply_beaconing_flags(wiphy, initiator);
  294. ath_reg_apply_active_scan_flags(wiphy, initiator);
  295. break;
  296. }
  297. return;
  298. }
  299. int ath_reg_notifier_apply(struct wiphy *wiphy,
  300. struct regulatory_request *request,
  301. struct ath_regulatory *reg)
  302. {
  303. /* We always apply this */
  304. ath_reg_apply_radar_flags(wiphy);
  305. switch (request->initiator) {
  306. case NL80211_REGDOM_SET_BY_DRIVER:
  307. case NL80211_REGDOM_SET_BY_CORE:
  308. case NL80211_REGDOM_SET_BY_USER:
  309. break;
  310. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  311. if (ath_is_world_regd(reg))
  312. ath_reg_apply_world_flags(wiphy, request->initiator,
  313. reg);
  314. break;
  315. }
  316. return 0;
  317. }
  318. EXPORT_SYMBOL(ath_reg_notifier_apply);
  319. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  320. {
  321. u16 rd = ath_regd_get_eepromRD(reg);
  322. int i;
  323. if (rd & COUNTRY_ERD_FLAG) {
  324. /* EEPROM value is a country code */
  325. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  326. printk(KERN_DEBUG
  327. "ath: EEPROM indicates we should expect "
  328. "a country code\n");
  329. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  330. if (allCountries[i].countryCode == cc)
  331. return true;
  332. } else {
  333. /* EEPROM value is a regpair value */
  334. if (rd != CTRY_DEFAULT)
  335. printk(KERN_DEBUG "ath: EEPROM indicates we "
  336. "should expect a direct regpair map\n");
  337. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  338. if (regDomainPairs[i].regDmnEnum == rd)
  339. return true;
  340. }
  341. printk(KERN_DEBUG
  342. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  343. return false;
  344. }
  345. /* EEPROM country code to regpair mapping */
  346. static struct country_code_to_enum_rd*
  347. ath_regd_find_country(u16 countryCode)
  348. {
  349. int i;
  350. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  351. if (allCountries[i].countryCode == countryCode)
  352. return &allCountries[i];
  353. }
  354. return NULL;
  355. }
  356. /* EEPROM rd code to regpair mapping */
  357. static struct country_code_to_enum_rd*
  358. ath_regd_find_country_by_rd(int regdmn)
  359. {
  360. int i;
  361. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  362. if (allCountries[i].regDmnEnum == regdmn)
  363. return &allCountries[i];
  364. }
  365. return NULL;
  366. }
  367. /* Returns the map of the EEPROM set RD to a country code */
  368. static u16 ath_regd_get_default_country(u16 rd)
  369. {
  370. if (rd & COUNTRY_ERD_FLAG) {
  371. struct country_code_to_enum_rd *country = NULL;
  372. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  373. country = ath_regd_find_country(cc);
  374. if (country != NULL)
  375. return cc;
  376. }
  377. return CTRY_DEFAULT;
  378. }
  379. static struct reg_dmn_pair_mapping*
  380. ath_get_regpair(int regdmn)
  381. {
  382. int i;
  383. if (regdmn == NO_ENUMRD)
  384. return NULL;
  385. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  386. if (regDomainPairs[i].regDmnEnum == regdmn)
  387. return &regDomainPairs[i];
  388. }
  389. return NULL;
  390. }
  391. static int
  392. ath_regd_init_wiphy(struct ath_regulatory *reg,
  393. struct wiphy *wiphy,
  394. int (*reg_notifier)(struct wiphy *wiphy,
  395. struct regulatory_request *request))
  396. {
  397. const struct ieee80211_regdomain *regd;
  398. wiphy->reg_notifier = reg_notifier;
  399. wiphy->strict_regulatory = true;
  400. if (ath_is_world_regd(reg)) {
  401. /*
  402. * Anything applied here (prior to wiphy registration) gets
  403. * saved on the wiphy orig_* parameters
  404. */
  405. regd = ath_world_regdomain(reg);
  406. wiphy->custom_regulatory = true;
  407. wiphy->strict_regulatory = false;
  408. } else {
  409. /*
  410. * This gets applied in the case of the absense of CRDA,
  411. * it's our own custom world regulatory domain, similar to
  412. * cfg80211's but we enable passive scanning.
  413. */
  414. regd = ath_default_world_regdomain();
  415. }
  416. wiphy_apply_custom_regulatory(wiphy, regd);
  417. ath_reg_apply_radar_flags(wiphy);
  418. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  419. return 0;
  420. }
  421. int
  422. ath_regd_init(struct ath_regulatory *reg,
  423. struct wiphy *wiphy,
  424. int (*reg_notifier)(struct wiphy *wiphy,
  425. struct regulatory_request *request))
  426. {
  427. struct country_code_to_enum_rd *country = NULL;
  428. u16 regdmn;
  429. if (!reg)
  430. return -EINVAL;
  431. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  432. if (!ath_regd_is_eeprom_valid(reg)) {
  433. printk(KERN_ERR "ath: Invalid EEPROM contents\n");
  434. return -EINVAL;
  435. }
  436. regdmn = ath_regd_get_eepromRD(reg);
  437. reg->country_code = ath_regd_get_default_country(regdmn);
  438. if (reg->country_code == CTRY_DEFAULT &&
  439. regdmn == CTRY_DEFAULT) {
  440. printk(KERN_DEBUG "ath: EEPROM indicates default "
  441. "country code should be used\n");
  442. reg->country_code = CTRY_UNITED_STATES;
  443. }
  444. if (reg->country_code == CTRY_DEFAULT) {
  445. country = NULL;
  446. } else {
  447. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  448. "map search\n");
  449. country = ath_regd_find_country(reg->country_code);
  450. if (country == NULL) {
  451. printk(KERN_DEBUG
  452. "ath: no valid country maps found for "
  453. "country code: 0x%0x\n",
  454. reg->country_code);
  455. return -EINVAL;
  456. } else {
  457. regdmn = country->regDmnEnum;
  458. printk(KERN_DEBUG "ath: country maps to "
  459. "regdmn code: 0x%0x\n",
  460. regdmn);
  461. }
  462. }
  463. reg->regpair = ath_get_regpair(regdmn);
  464. if (!reg->regpair) {
  465. printk(KERN_DEBUG "ath: "
  466. "No regulatory domain pair found, cannot continue\n");
  467. return -EINVAL;
  468. }
  469. if (!country)
  470. country = ath_regd_find_country_by_rd(regdmn);
  471. if (country) {
  472. reg->alpha2[0] = country->isoName[0];
  473. reg->alpha2[1] = country->isoName[1];
  474. } else {
  475. reg->alpha2[0] = '0';
  476. reg->alpha2[1] = '0';
  477. }
  478. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  479. reg->alpha2[0], reg->alpha2[1]);
  480. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  481. reg->regpair->regDmnEnum);
  482. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  483. return 0;
  484. }
  485. EXPORT_SYMBOL(ath_regd_init);
  486. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  487. enum ieee80211_band band)
  488. {
  489. if (!reg->regpair ||
  490. (reg->country_code == CTRY_DEFAULT &&
  491. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  492. return SD_NO_CTL;
  493. }
  494. switch (band) {
  495. case IEEE80211_BAND_2GHZ:
  496. return reg->regpair->reg_2ghz_ctl;
  497. case IEEE80211_BAND_5GHZ:
  498. return reg->regpair->reg_5ghz_ctl;
  499. default:
  500. return NO_CTL;
  501. }
  502. return NO_CTL;
  503. }
  504. EXPORT_SYMBOL(ath_regd_get_band_ctl);