PageRenderTime 1345ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/ath/regd.c

https://github.com/Mengqi/linux-2.6
C | 606 lines | 431 code | 75 blank | 100 comment | 81 complexity | 26827ab637194467c2e8e0c62866d723 MD5 | raw file
  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 <net/cfg80211.h>
  18. #include <net/mac80211.h>
  19. #include "regd.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 ath_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 ath_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 ath_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 ath_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, 0x68, 0x6A and 0x6C */
  88. static const struct ieee80211_regdomain ath_world_regdom_67_68_6A_6C = {
  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 & COUNTRY_ERD_FLAG) != COUNTRY_ERD_FLAG) &&
  100. (((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. case 0x6C:
  137. return &ath_world_regdom_67_68_6A_6C;
  138. default:
  139. WARN_ON(1);
  140. return ath_default_world_regdomain();
  141. }
  142. }
  143. bool ath_is_49ghz_allowed(u16 regdomain)
  144. {
  145. /* possibly more */
  146. return regdomain == MKK9_MKKC;
  147. }
  148. EXPORT_SYMBOL(ath_is_49ghz_allowed);
  149. /* Frequency is one where radar detection is required */
  150. static bool ath_is_radar_freq(u16 center_freq)
  151. {
  152. return (center_freq >= 5260 && center_freq <= 5700);
  153. }
  154. /*
  155. * N.B: These exception rules do not apply radar freqs.
  156. *
  157. * - We enable adhoc (or beaconing) if allowed by 11d
  158. * - We enable active scan if the channel is allowed by 11d
  159. * - If no country IE has been processed and a we determine we have
  160. * received a beacon on a channel we can enable active scan and
  161. * adhoc (or beaconing).
  162. */
  163. static void
  164. ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  165. enum nl80211_reg_initiator initiator)
  166. {
  167. enum ieee80211_band band;
  168. struct ieee80211_supported_band *sband;
  169. const struct ieee80211_reg_rule *reg_rule;
  170. struct ieee80211_channel *ch;
  171. unsigned int i;
  172. u32 bandwidth = 0;
  173. int r;
  174. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  175. if (!wiphy->bands[band])
  176. continue;
  177. sband = wiphy->bands[band];
  178. for (i = 0; i < sband->n_channels; i++) {
  179. ch = &sband->channels[i];
  180. if (ath_is_radar_freq(ch->center_freq) ||
  181. (ch->flags & IEEE80211_CHAN_RADAR))
  182. continue;
  183. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  184. r = freq_reg_info(wiphy,
  185. ch->center_freq,
  186. bandwidth,
  187. &reg_rule);
  188. if (r)
  189. continue;
  190. /*
  191. * If 11d had a rule for this channel ensure
  192. * we enable adhoc/beaconing if it allows us to
  193. * use it. Note that we would have disabled it
  194. * by applying our static world regdomain by
  195. * default during init, prior to calling our
  196. * regulatory_hint().
  197. */
  198. if (!(reg_rule->flags &
  199. NL80211_RRF_NO_IBSS))
  200. ch->flags &=
  201. ~IEEE80211_CHAN_NO_IBSS;
  202. if (!(reg_rule->flags &
  203. NL80211_RRF_PASSIVE_SCAN))
  204. ch->flags &=
  205. ~IEEE80211_CHAN_PASSIVE_SCAN;
  206. } else {
  207. if (ch->beacon_found)
  208. ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
  209. IEEE80211_CHAN_PASSIVE_SCAN);
  210. }
  211. }
  212. }
  213. }
  214. /* Allows active scan scan on Ch 12 and 13 */
  215. static void
  216. ath_reg_apply_active_scan_flags(struct wiphy *wiphy,
  217. enum nl80211_reg_initiator initiator)
  218. {
  219. struct ieee80211_supported_band *sband;
  220. struct ieee80211_channel *ch;
  221. const struct ieee80211_reg_rule *reg_rule;
  222. u32 bandwidth = 0;
  223. int r;
  224. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  225. /*
  226. * If no country IE has been received always enable active scan
  227. * on these channels. This is only done for specific regulatory SKUs
  228. */
  229. if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  230. ch = &sband->channels[11]; /* CH 12 */
  231. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  232. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  233. ch = &sband->channels[12]; /* CH 13 */
  234. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  235. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  236. return;
  237. }
  238. /*
  239. * If a country IE has been received check its rule for this
  240. * channel first before enabling active scan. The passive scan
  241. * would have been enforced by the initial processing of our
  242. * custom regulatory domain.
  243. */
  244. ch = &sband->channels[11]; /* CH 12 */
  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. ch = &sband->channels[12]; /* CH 13 */
  252. r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
  253. if (!r) {
  254. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  255. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  256. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  257. }
  258. }
  259. /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
  260. static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
  261. {
  262. struct ieee80211_supported_band *sband;
  263. struct ieee80211_channel *ch;
  264. unsigned int i;
  265. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  266. return;
  267. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  268. for (i = 0; i < sband->n_channels; i++) {
  269. ch = &sband->channels[i];
  270. if (!ath_is_radar_freq(ch->center_freq))
  271. continue;
  272. /* We always enable radar detection/DFS on this
  273. * frequency range. Additionally we also apply on
  274. * this frequency range:
  275. * - If STA mode does not yet have DFS supports disable
  276. * active scanning
  277. * - If adhoc mode does not support DFS yet then
  278. * disable adhoc in the frequency.
  279. * - If AP mode does not yet support radar detection/DFS
  280. * do not allow AP mode
  281. */
  282. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  283. ch->flags |= IEEE80211_CHAN_RADAR |
  284. IEEE80211_CHAN_NO_IBSS |
  285. IEEE80211_CHAN_PASSIVE_SCAN;
  286. }
  287. }
  288. static void ath_reg_apply_world_flags(struct wiphy *wiphy,
  289. enum nl80211_reg_initiator initiator,
  290. struct ath_regulatory *reg)
  291. {
  292. switch (reg->regpair->regDmnEnum) {
  293. case 0x60:
  294. case 0x63:
  295. case 0x66:
  296. case 0x67:
  297. case 0x6C:
  298. ath_reg_apply_beaconing_flags(wiphy, initiator);
  299. break;
  300. case 0x68:
  301. ath_reg_apply_beaconing_flags(wiphy, initiator);
  302. ath_reg_apply_active_scan_flags(wiphy, initiator);
  303. break;
  304. }
  305. }
  306. int ath_reg_notifier_apply(struct wiphy *wiphy,
  307. struct regulatory_request *request,
  308. struct ath_regulatory *reg)
  309. {
  310. /* We always apply this */
  311. ath_reg_apply_radar_flags(wiphy);
  312. /*
  313. * This would happen when we have sent a custom regulatory request
  314. * a world regulatory domain and the scheduler hasn't yet processed
  315. * any pending requests in the queue.
  316. */
  317. if (!request)
  318. return 0;
  319. switch (request->initiator) {
  320. case NL80211_REGDOM_SET_BY_DRIVER:
  321. case NL80211_REGDOM_SET_BY_CORE:
  322. case NL80211_REGDOM_SET_BY_USER:
  323. break;
  324. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  325. if (ath_is_world_regd(reg))
  326. ath_reg_apply_world_flags(wiphy, request->initiator,
  327. reg);
  328. break;
  329. }
  330. return 0;
  331. }
  332. EXPORT_SYMBOL(ath_reg_notifier_apply);
  333. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  334. {
  335. u16 rd = ath_regd_get_eepromRD(reg);
  336. int i;
  337. if (rd & COUNTRY_ERD_FLAG) {
  338. /* EEPROM value is a country code */
  339. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  340. printk(KERN_DEBUG
  341. "ath: EEPROM indicates we should expect "
  342. "a country code\n");
  343. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  344. if (allCountries[i].countryCode == cc)
  345. return true;
  346. } else {
  347. /* EEPROM value is a regpair value */
  348. if (rd != CTRY_DEFAULT)
  349. printk(KERN_DEBUG "ath: EEPROM indicates we "
  350. "should expect a direct regpair map\n");
  351. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  352. if (regDomainPairs[i].regDmnEnum == rd)
  353. return true;
  354. }
  355. printk(KERN_DEBUG
  356. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  357. return false;
  358. }
  359. /* EEPROM country code to regpair mapping */
  360. static struct country_code_to_enum_rd*
  361. ath_regd_find_country(u16 countryCode)
  362. {
  363. int i;
  364. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  365. if (allCountries[i].countryCode == countryCode)
  366. return &allCountries[i];
  367. }
  368. return NULL;
  369. }
  370. /* EEPROM rd code to regpair mapping */
  371. static struct country_code_to_enum_rd*
  372. ath_regd_find_country_by_rd(int regdmn)
  373. {
  374. int i;
  375. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  376. if (allCountries[i].regDmnEnum == regdmn)
  377. return &allCountries[i];
  378. }
  379. return NULL;
  380. }
  381. /* Returns the map of the EEPROM set RD to a country code */
  382. static u16 ath_regd_get_default_country(u16 rd)
  383. {
  384. if (rd & COUNTRY_ERD_FLAG) {
  385. struct country_code_to_enum_rd *country = NULL;
  386. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  387. country = ath_regd_find_country(cc);
  388. if (country != NULL)
  389. return cc;
  390. }
  391. return CTRY_DEFAULT;
  392. }
  393. static struct reg_dmn_pair_mapping*
  394. ath_get_regpair(int regdmn)
  395. {
  396. int i;
  397. if (regdmn == NO_ENUMRD)
  398. return NULL;
  399. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  400. if (regDomainPairs[i].regDmnEnum == regdmn)
  401. return &regDomainPairs[i];
  402. }
  403. return NULL;
  404. }
  405. static int
  406. ath_regd_init_wiphy(struct ath_regulatory *reg,
  407. struct wiphy *wiphy,
  408. int (*reg_notifier)(struct wiphy *wiphy,
  409. struct regulatory_request *request))
  410. {
  411. const struct ieee80211_regdomain *regd;
  412. wiphy->reg_notifier = reg_notifier;
  413. wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  414. if (ath_is_world_regd(reg)) {
  415. /*
  416. * Anything applied here (prior to wiphy registration) gets
  417. * saved on the wiphy orig_* parameters
  418. */
  419. regd = ath_world_regdomain(reg);
  420. wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  421. } else {
  422. /*
  423. * This gets applied in the case of the absence of CRDA,
  424. * it's our own custom world regulatory domain, similar to
  425. * cfg80211's but we enable passive scanning.
  426. */
  427. regd = ath_default_world_regdomain();
  428. }
  429. wiphy_apply_custom_regulatory(wiphy, regd);
  430. ath_reg_apply_radar_flags(wiphy);
  431. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  432. return 0;
  433. }
  434. /*
  435. * Some users have reported their EEPROM programmed with
  436. * 0x8000 set, this is not a supported regulatory domain
  437. * but since we have more than one user with it we need
  438. * a solution for them. We default to 0x64, which is the
  439. * default Atheros world regulatory domain.
  440. */
  441. static void ath_regd_sanitize(struct ath_regulatory *reg)
  442. {
  443. if (reg->current_rd != COUNTRY_ERD_FLAG)
  444. return;
  445. printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
  446. reg->current_rd = 0x64;
  447. }
  448. int
  449. ath_regd_init(struct ath_regulatory *reg,
  450. struct wiphy *wiphy,
  451. int (*reg_notifier)(struct wiphy *wiphy,
  452. struct regulatory_request *request))
  453. {
  454. struct country_code_to_enum_rd *country = NULL;
  455. u16 regdmn;
  456. if (!reg)
  457. return -EINVAL;
  458. ath_regd_sanitize(reg);
  459. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  460. if (!ath_regd_is_eeprom_valid(reg)) {
  461. printk(KERN_ERR "ath: Invalid EEPROM contents\n");
  462. return -EINVAL;
  463. }
  464. regdmn = ath_regd_get_eepromRD(reg);
  465. reg->country_code = ath_regd_get_default_country(regdmn);
  466. if (reg->country_code == CTRY_DEFAULT &&
  467. regdmn == CTRY_DEFAULT) {
  468. printk(KERN_DEBUG "ath: EEPROM indicates default "
  469. "country code should be used\n");
  470. reg->country_code = CTRY_UNITED_STATES;
  471. }
  472. if (reg->country_code == CTRY_DEFAULT) {
  473. country = NULL;
  474. } else {
  475. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  476. "map search\n");
  477. country = ath_regd_find_country(reg->country_code);
  478. if (country == NULL) {
  479. printk(KERN_DEBUG
  480. "ath: no valid country maps found for "
  481. "country code: 0x%0x\n",
  482. reg->country_code);
  483. return -EINVAL;
  484. } else {
  485. regdmn = country->regDmnEnum;
  486. printk(KERN_DEBUG "ath: country maps to "
  487. "regdmn code: 0x%0x\n",
  488. regdmn);
  489. }
  490. }
  491. reg->regpair = ath_get_regpair(regdmn);
  492. if (!reg->regpair) {
  493. printk(KERN_DEBUG "ath: "
  494. "No regulatory domain pair found, cannot continue\n");
  495. return -EINVAL;
  496. }
  497. if (!country)
  498. country = ath_regd_find_country_by_rd(regdmn);
  499. if (country) {
  500. reg->alpha2[0] = country->isoName[0];
  501. reg->alpha2[1] = country->isoName[1];
  502. } else {
  503. reg->alpha2[0] = '0';
  504. reg->alpha2[1] = '0';
  505. }
  506. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  507. reg->alpha2[0], reg->alpha2[1]);
  508. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  509. reg->regpair->regDmnEnum);
  510. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  511. return 0;
  512. }
  513. EXPORT_SYMBOL(ath_regd_init);
  514. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  515. enum ieee80211_band band)
  516. {
  517. if (!reg->regpair ||
  518. (reg->country_code == CTRY_DEFAULT &&
  519. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  520. return SD_NO_CTL;
  521. }
  522. switch (band) {
  523. case IEEE80211_BAND_2GHZ:
  524. return reg->regpair->reg_2ghz_ctl;
  525. case IEEE80211_BAND_5GHZ:
  526. return reg->regpair->reg_5ghz_ctl;
  527. default:
  528. return NO_CTL;
  529. }
  530. }
  531. EXPORT_SYMBOL(ath_regd_get_band_ctl);