regd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/export.h>
  19. #include <net/cfg80211.h>
  20. #include <net/mac80211.h>
  21. #include "regd.h"
  22. #include "regd_common.h"
  23. static int __ath_regd_init(struct ath_regulatory *reg);
  24. /*
  25. * This is a set of common rules used by our world regulatory domains.
  26. * We have 12 world regulatory domains. To save space we consolidate
  27. * the regulatory domains in 5 structures by frequency and change
  28. * the flags on our reg_notifier() on a case by case basis.
  29. */
  30. /* Only these channels all allow active scan on all world regulatory domains */
  31. #define ATH9K_2GHZ_CH01_11 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
  32. /* We enable active scan on these a case by case basis by regulatory domain */
  33. #define ATH9K_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
  34. NL80211_RRF_NO_IR)
  35. #define ATH9K_2GHZ_CH14 REG_RULE(2484-10, 2484+10, 40, 0, 20,\
  36. NL80211_RRF_NO_IR | \
  37. NL80211_RRF_NO_OFDM)
  38. /* We allow IBSS on these on a case by case basis by regulatory domain */
  39. #define ATH9K_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 80, 0, 30,\
  40. NL80211_RRF_NO_IR)
  41. #define ATH9K_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 80, 0, 30,\
  42. NL80211_RRF_NO_IR)
  43. #define ATH9K_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 80, 0, 30,\
  44. NL80211_RRF_NO_IR)
  45. #define ATH9K_2GHZ_ALL ATH9K_2GHZ_CH01_11, \
  46. ATH9K_2GHZ_CH12_13, \
  47. ATH9K_2GHZ_CH14
  48. #define ATH9K_5GHZ_ALL ATH9K_5GHZ_5150_5350, \
  49. ATH9K_5GHZ_5470_5850
  50. /* This one skips what we call "mid band" */
  51. #define ATH9K_5GHZ_NO_MIDBAND ATH9K_5GHZ_5150_5350, \
  52. ATH9K_5GHZ_5725_5850
  53. /* Can be used for:
  54. * 0x60, 0x61, 0x62 */
  55. static const struct ieee80211_regdomain ath_world_regdom_60_61_62 = {
  56. .n_reg_rules = 5,
  57. .alpha2 = "99",
  58. .reg_rules = {
  59. ATH9K_2GHZ_ALL,
  60. ATH9K_5GHZ_ALL,
  61. }
  62. };
  63. /* Can be used by 0x63 and 0x65 */
  64. static const struct ieee80211_regdomain ath_world_regdom_63_65 = {
  65. .n_reg_rules = 4,
  66. .alpha2 = "99",
  67. .reg_rules = {
  68. ATH9K_2GHZ_CH01_11,
  69. ATH9K_2GHZ_CH12_13,
  70. ATH9K_5GHZ_NO_MIDBAND,
  71. }
  72. };
  73. /* Can be used by 0x64 only */
  74. static const struct ieee80211_regdomain ath_world_regdom_64 = {
  75. .n_reg_rules = 3,
  76. .alpha2 = "99",
  77. .reg_rules = {
  78. ATH9K_2GHZ_CH01_11,
  79. ATH9K_5GHZ_NO_MIDBAND,
  80. }
  81. };
  82. /* Can be used by 0x66 and 0x69 */
  83. static const struct ieee80211_regdomain ath_world_regdom_66_69 = {
  84. .n_reg_rules = 3,
  85. .alpha2 = "99",
  86. .reg_rules = {
  87. ATH9K_2GHZ_CH01_11,
  88. ATH9K_5GHZ_ALL,
  89. }
  90. };
  91. /* Can be used by 0x67, 0x68, 0x6A and 0x6C */
  92. static const struct ieee80211_regdomain ath_world_regdom_67_68_6A_6C = {
  93. .n_reg_rules = 4,
  94. .alpha2 = "99",
  95. .reg_rules = {
  96. ATH9K_2GHZ_CH01_11,
  97. ATH9K_2GHZ_CH12_13,
  98. ATH9K_5GHZ_ALL,
  99. }
  100. };
  101. static bool dynamic_country_user_possible(struct ath_regulatory *reg)
  102. {
  103. if (config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_CERT_TESTING))
  104. return true;
  105. switch (reg->country_code) {
  106. case CTRY_UNITED_STATES:
  107. case CTRY_JAPAN1:
  108. case CTRY_JAPAN2:
  109. case CTRY_JAPAN3:
  110. case CTRY_JAPAN4:
  111. case CTRY_JAPAN5:
  112. case CTRY_JAPAN6:
  113. case CTRY_JAPAN7:
  114. case CTRY_JAPAN8:
  115. case CTRY_JAPAN9:
  116. case CTRY_JAPAN10:
  117. case CTRY_JAPAN11:
  118. case CTRY_JAPAN12:
  119. case CTRY_JAPAN13:
  120. case CTRY_JAPAN14:
  121. case CTRY_JAPAN15:
  122. case CTRY_JAPAN16:
  123. case CTRY_JAPAN17:
  124. case CTRY_JAPAN18:
  125. case CTRY_JAPAN19:
  126. case CTRY_JAPAN20:
  127. case CTRY_JAPAN21:
  128. case CTRY_JAPAN22:
  129. case CTRY_JAPAN23:
  130. case CTRY_JAPAN24:
  131. case CTRY_JAPAN25:
  132. case CTRY_JAPAN26:
  133. case CTRY_JAPAN27:
  134. case CTRY_JAPAN28:
  135. case CTRY_JAPAN29:
  136. case CTRY_JAPAN30:
  137. case CTRY_JAPAN31:
  138. case CTRY_JAPAN32:
  139. case CTRY_JAPAN33:
  140. case CTRY_JAPAN34:
  141. case CTRY_JAPAN35:
  142. case CTRY_JAPAN36:
  143. case CTRY_JAPAN37:
  144. case CTRY_JAPAN38:
  145. case CTRY_JAPAN39:
  146. case CTRY_JAPAN40:
  147. case CTRY_JAPAN41:
  148. case CTRY_JAPAN42:
  149. case CTRY_JAPAN43:
  150. case CTRY_JAPAN44:
  151. case CTRY_JAPAN45:
  152. case CTRY_JAPAN46:
  153. case CTRY_JAPAN47:
  154. case CTRY_JAPAN48:
  155. case CTRY_JAPAN49:
  156. case CTRY_JAPAN50:
  157. case CTRY_JAPAN51:
  158. case CTRY_JAPAN52:
  159. case CTRY_JAPAN53:
  160. case CTRY_JAPAN54:
  161. case CTRY_JAPAN55:
  162. case CTRY_JAPAN56:
  163. case CTRY_JAPAN57:
  164. case CTRY_JAPAN58:
  165. case CTRY_JAPAN59:
  166. return false;
  167. }
  168. return true;
  169. }
  170. static bool ath_reg_dyn_country_user_allow(struct ath_regulatory *reg)
  171. {
  172. if (!config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS))
  173. return false;
  174. if (!dynamic_country_user_possible(reg))
  175. return false;
  176. return true;
  177. }
  178. static inline bool is_wwr_sku(u16 regd)
  179. {
  180. return ((regd & COUNTRY_ERD_FLAG) != COUNTRY_ERD_FLAG) &&
  181. (((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
  182. (regd == WORLD));
  183. }
  184. static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
  185. {
  186. return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
  187. }
  188. bool ath_is_world_regd(struct ath_regulatory *reg)
  189. {
  190. return is_wwr_sku(ath_regd_get_eepromRD(reg));
  191. }
  192. EXPORT_SYMBOL(ath_is_world_regd);
  193. static const struct ieee80211_regdomain *ath_default_world_regdomain(void)
  194. {
  195. /* this is the most restrictive */
  196. return &ath_world_regdom_64;
  197. }
  198. static const struct
  199. ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
  200. {
  201. switch (reg->regpair->reg_domain) {
  202. case 0x60:
  203. case 0x61:
  204. case 0x62:
  205. return &ath_world_regdom_60_61_62;
  206. case 0x63:
  207. case 0x65:
  208. return &ath_world_regdom_63_65;
  209. case 0x64:
  210. return &ath_world_regdom_64;
  211. case 0x66:
  212. case 0x69:
  213. return &ath_world_regdom_66_69;
  214. case 0x67:
  215. case 0x68:
  216. case 0x6A:
  217. case 0x6C:
  218. return &ath_world_regdom_67_68_6A_6C;
  219. default:
  220. WARN_ON(1);
  221. return ath_default_world_regdomain();
  222. }
  223. }
  224. bool ath_is_49ghz_allowed(u16 regdomain)
  225. {
  226. /* possibly more */
  227. return regdomain == MKK9_MKKC;
  228. }
  229. EXPORT_SYMBOL(ath_is_49ghz_allowed);
  230. /* Frequency is one where radar detection is required */
  231. static bool ath_is_radar_freq(u16 center_freq,
  232. struct ath_regulatory *reg)
  233. {
  234. if (reg->country_code == CTRY_INDIA)
  235. return (center_freq >= 5500 && center_freq <= 5700);
  236. return (center_freq >= 5260 && center_freq <= 5700);
  237. }
  238. static void ath_force_clear_no_ir_chan(struct wiphy *wiphy,
  239. struct ieee80211_channel *ch)
  240. {
  241. const struct ieee80211_reg_rule *reg_rule;
  242. reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(ch->center_freq));
  243. if (IS_ERR(reg_rule))
  244. return;
  245. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  246. if (ch->flags & IEEE80211_CHAN_NO_IR)
  247. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  248. }
  249. static void ath_force_clear_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
  250. {
  251. struct ieee80211_channel *ch;
  252. ch = ieee80211_get_channel(wiphy, center_freq);
  253. if (!ch)
  254. return;
  255. ath_force_clear_no_ir_chan(wiphy, ch);
  256. }
  257. static void ath_force_no_ir_chan(struct ieee80211_channel *ch)
  258. {
  259. ch->flags |= IEEE80211_CHAN_NO_IR;
  260. }
  261. static void ath_force_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
  262. {
  263. struct ieee80211_channel *ch;
  264. ch = ieee80211_get_channel(wiphy, center_freq);
  265. if (!ch)
  266. return;
  267. ath_force_no_ir_chan(ch);
  268. }
  269. static void
  270. __ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  271. struct ath_regulatory *reg,
  272. enum nl80211_reg_initiator initiator,
  273. struct ieee80211_channel *ch)
  274. {
  275. if (ath_is_radar_freq(ch->center_freq, reg) ||
  276. (ch->flags & IEEE80211_CHAN_RADAR))
  277. return;
  278. switch (initiator) {
  279. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  280. ath_force_clear_no_ir_chan(wiphy, ch);
  281. break;
  282. case NL80211_REGDOM_SET_BY_USER:
  283. if (ath_reg_dyn_country_user_allow(reg))
  284. ath_force_clear_no_ir_chan(wiphy, ch);
  285. break;
  286. default:
  287. if (ch->beacon_found)
  288. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  289. }
  290. }
  291. /*
  292. * These exception rules do not apply radar frequencies.
  293. *
  294. * - We enable initiating radiation if the country IE says its fine:
  295. * - If no country IE has been processed and a we determine we have
  296. * received a beacon on a channel we can enable initiating radiation.
  297. */
  298. static void
  299. ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  300. struct ath_regulatory *reg,
  301. enum nl80211_reg_initiator initiator)
  302. {
  303. enum ieee80211_band band;
  304. struct ieee80211_supported_band *sband;
  305. struct ieee80211_channel *ch;
  306. unsigned int i;
  307. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  308. if (!wiphy->bands[band])
  309. continue;
  310. sband = wiphy->bands[band];
  311. for (i = 0; i < sband->n_channels; i++) {
  312. ch = &sband->channels[i];
  313. __ath_reg_apply_beaconing_flags(wiphy, reg,
  314. initiator, ch);
  315. }
  316. }
  317. }
  318. /**
  319. * ath_reg_apply_ir_flags()
  320. * @wiphy: the wiphy to use
  321. * @initiator: the regulatory hint initiator
  322. *
  323. * If no country IE has been received always enable passive scan
  324. * and no-ibss on these channels. This is only done for specific
  325. * regulatory SKUs.
  326. *
  327. * If a country IE has been received check its rule for this
  328. * channel first before enabling active scan. The passive scan
  329. * would have been enforced by the initial processing of our
  330. * custom regulatory domain.
  331. */
  332. static void
  333. ath_reg_apply_ir_flags(struct wiphy *wiphy,
  334. struct ath_regulatory *reg,
  335. enum nl80211_reg_initiator initiator)
  336. {
  337. struct ieee80211_supported_band *sband;
  338. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  339. if (!sband)
  340. return;
  341. switch(initiator) {
  342. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  343. ath_force_clear_no_ir_freq(wiphy, 2467);
  344. ath_force_clear_no_ir_freq(wiphy, 2472);
  345. break;
  346. case NL80211_REGDOM_SET_BY_USER:
  347. if (!ath_reg_dyn_country_user_allow(reg))
  348. break;
  349. ath_force_clear_no_ir_freq(wiphy, 2467);
  350. ath_force_clear_no_ir_freq(wiphy, 2472);
  351. break;
  352. default:
  353. ath_force_no_ir_freq(wiphy, 2467);
  354. ath_force_no_ir_freq(wiphy, 2472);
  355. }
  356. }
  357. /* Always apply Radar/DFS rules on freq range 5500 MHz - 5700 MHz */
  358. static void ath_reg_apply_radar_flags(struct wiphy *wiphy,
  359. struct ath_regulatory *reg)
  360. {
  361. struct ieee80211_supported_band *sband;
  362. struct ieee80211_channel *ch;
  363. unsigned int i;
  364. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  365. return;
  366. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  367. for (i = 0; i < sband->n_channels; i++) {
  368. ch = &sband->channels[i];
  369. if (!ath_is_radar_freq(ch->center_freq, reg))
  370. continue;
  371. /* We always enable radar detection/DFS on this
  372. * frequency range. Additionally we also apply on
  373. * this frequency range:
  374. * - If STA mode does not yet have DFS supports disable
  375. * active scanning
  376. * - If adhoc mode does not support DFS yet then
  377. * disable adhoc in the frequency.
  378. * - If AP mode does not yet support radar detection/DFS
  379. * do not allow AP mode
  380. */
  381. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  382. ch->flags |= IEEE80211_CHAN_RADAR |
  383. IEEE80211_CHAN_NO_IR;
  384. }
  385. }
  386. static void ath_reg_apply_world_flags(struct wiphy *wiphy,
  387. enum nl80211_reg_initiator initiator,
  388. struct ath_regulatory *reg)
  389. {
  390. switch (reg->regpair->reg_domain) {
  391. case 0x60:
  392. case 0x63:
  393. case 0x66:
  394. case 0x67:
  395. case 0x6C:
  396. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  397. break;
  398. case 0x68:
  399. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  400. ath_reg_apply_ir_flags(wiphy, reg, initiator);
  401. break;
  402. default:
  403. if (ath_reg_dyn_country_user_allow(reg))
  404. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  405. }
  406. }
  407. static u16 ath_regd_find_country_by_name(char *alpha2)
  408. {
  409. unsigned int i;
  410. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  411. if (!memcmp(allCountries[i].isoName, alpha2, 2))
  412. return allCountries[i].countryCode;
  413. }
  414. return -1;
  415. }
  416. static int __ath_reg_dyn_country(struct wiphy *wiphy,
  417. struct ath_regulatory *reg,
  418. struct regulatory_request *request)
  419. {
  420. u16 country_code;
  421. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  422. !ath_is_world_regd(reg))
  423. return -EINVAL;
  424. country_code = ath_regd_find_country_by_name(request->alpha2);
  425. if (country_code == (u16) -1)
  426. return -EINVAL;
  427. reg->current_rd = COUNTRY_ERD_FLAG;
  428. reg->current_rd |= country_code;
  429. __ath_regd_init(reg);
  430. ath_reg_apply_world_flags(wiphy, request->initiator, reg);
  431. return 0;
  432. }
  433. static void ath_reg_dyn_country(struct wiphy *wiphy,
  434. struct ath_regulatory *reg,
  435. struct regulatory_request *request)
  436. {
  437. if (__ath_reg_dyn_country(wiphy, reg, request))
  438. return;
  439. printk(KERN_DEBUG "ath: regdomain 0x%0x "
  440. "dynamically updated by %s\n",
  441. reg->current_rd,
  442. reg_initiator_name(request->initiator));
  443. }
  444. void ath_reg_notifier_apply(struct wiphy *wiphy,
  445. struct regulatory_request *request,
  446. struct ath_regulatory *reg)
  447. {
  448. struct ath_common *common = container_of(reg, struct ath_common,
  449. regulatory);
  450. /* We always apply this */
  451. ath_reg_apply_radar_flags(wiphy, reg);
  452. /*
  453. * This would happen when we have sent a custom regulatory request
  454. * a world regulatory domain and the scheduler hasn't yet processed
  455. * any pending requests in the queue.
  456. */
  457. if (!request)
  458. return;
  459. reg->region = request->dfs_region;
  460. switch (request->initiator) {
  461. case NL80211_REGDOM_SET_BY_CORE:
  462. /*
  463. * If common->reg_world_copy is world roaming it means we *were*
  464. * world roaming... so we now have to restore that data.
  465. */
  466. if (!ath_is_world_regd(&common->reg_world_copy))
  467. break;
  468. memcpy(reg, &common->reg_world_copy,
  469. sizeof(struct ath_regulatory));
  470. break;
  471. case NL80211_REGDOM_SET_BY_DRIVER:
  472. break;
  473. case NL80211_REGDOM_SET_BY_USER:
  474. if (ath_reg_dyn_country_user_allow(reg))
  475. ath_reg_dyn_country(wiphy, reg, request);
  476. break;
  477. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  478. ath_reg_dyn_country(wiphy, reg, request);
  479. break;
  480. }
  481. }
  482. EXPORT_SYMBOL(ath_reg_notifier_apply);
  483. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  484. {
  485. u16 rd = ath_regd_get_eepromRD(reg);
  486. int i;
  487. if (rd & COUNTRY_ERD_FLAG) {
  488. /* EEPROM value is a country code */
  489. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  490. printk(KERN_DEBUG
  491. "ath: EEPROM indicates we should expect "
  492. "a country code\n");
  493. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  494. if (allCountries[i].countryCode == cc)
  495. return true;
  496. } else {
  497. /* EEPROM value is a regpair value */
  498. if (rd != CTRY_DEFAULT)
  499. printk(KERN_DEBUG "ath: EEPROM indicates we "
  500. "should expect a direct regpair map\n");
  501. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  502. if (regDomainPairs[i].reg_domain == rd)
  503. return true;
  504. }
  505. printk(KERN_DEBUG
  506. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  507. return false;
  508. }
  509. /* EEPROM country code to regpair mapping */
  510. static struct country_code_to_enum_rd*
  511. ath_regd_find_country(u16 countryCode)
  512. {
  513. int i;
  514. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  515. if (allCountries[i].countryCode == countryCode)
  516. return &allCountries[i];
  517. }
  518. return NULL;
  519. }
  520. /* EEPROM rd code to regpair mapping */
  521. static struct country_code_to_enum_rd*
  522. ath_regd_find_country_by_rd(int regdmn)
  523. {
  524. int i;
  525. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  526. if (allCountries[i].regDmnEnum == regdmn)
  527. return &allCountries[i];
  528. }
  529. return NULL;
  530. }
  531. /* Returns the map of the EEPROM set RD to a country code */
  532. static u16 ath_regd_get_default_country(u16 rd)
  533. {
  534. if (rd & COUNTRY_ERD_FLAG) {
  535. struct country_code_to_enum_rd *country = NULL;
  536. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  537. country = ath_regd_find_country(cc);
  538. if (country != NULL)
  539. return cc;
  540. }
  541. return CTRY_DEFAULT;
  542. }
  543. static struct reg_dmn_pair_mapping*
  544. ath_get_regpair(int regdmn)
  545. {
  546. int i;
  547. if (regdmn == NO_ENUMRD)
  548. return NULL;
  549. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  550. if (regDomainPairs[i].reg_domain == regdmn)
  551. return &regDomainPairs[i];
  552. }
  553. return NULL;
  554. }
  555. static int
  556. ath_regd_init_wiphy(struct ath_regulatory *reg,
  557. struct wiphy *wiphy,
  558. void (*reg_notifier)(struct wiphy *wiphy,
  559. struct regulatory_request *request))
  560. {
  561. const struct ieee80211_regdomain *regd;
  562. wiphy->reg_notifier = reg_notifier;
  563. wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
  564. REGULATORY_CUSTOM_REG;
  565. if (ath_is_world_regd(reg)) {
  566. /*
  567. * Anything applied here (prior to wiphy registration) gets
  568. * saved on the wiphy orig_* parameters
  569. */
  570. regd = ath_world_regdomain(reg);
  571. wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_FOLLOW_POWER;
  572. } else {
  573. /*
  574. * This gets applied in the case of the absence of CRDA,
  575. * it's our own custom world regulatory domain, similar to
  576. * cfg80211's but we enable passive scanning.
  577. */
  578. regd = ath_default_world_regdomain();
  579. }
  580. wiphy_apply_custom_regulatory(wiphy, regd);
  581. ath_reg_apply_radar_flags(wiphy, reg);
  582. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  583. return 0;
  584. }
  585. /*
  586. * Some users have reported their EEPROM programmed with
  587. * 0x8000 set, this is not a supported regulatory domain
  588. * but since we have more than one user with it we need
  589. * a solution for them. We default to 0x64, which is the
  590. * default Atheros world regulatory domain.
  591. */
  592. static void ath_regd_sanitize(struct ath_regulatory *reg)
  593. {
  594. if (reg->current_rd != COUNTRY_ERD_FLAG)
  595. return;
  596. printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
  597. reg->current_rd = 0x64;
  598. }
  599. static int __ath_regd_init(struct ath_regulatory *reg)
  600. {
  601. struct country_code_to_enum_rd *country = NULL;
  602. u16 regdmn;
  603. if (!reg)
  604. return -EINVAL;
  605. ath_regd_sanitize(reg);
  606. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  607. if (!ath_regd_is_eeprom_valid(reg)) {
  608. pr_err("Invalid EEPROM contents\n");
  609. return -EINVAL;
  610. }
  611. regdmn = ath_regd_get_eepromRD(reg);
  612. reg->country_code = ath_regd_get_default_country(regdmn);
  613. if (reg->country_code == CTRY_DEFAULT &&
  614. regdmn == CTRY_DEFAULT) {
  615. printk(KERN_DEBUG "ath: EEPROM indicates default "
  616. "country code should be used\n");
  617. reg->country_code = CTRY_UNITED_STATES;
  618. }
  619. if (reg->country_code == CTRY_DEFAULT) {
  620. country = NULL;
  621. } else {
  622. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  623. "map search\n");
  624. country = ath_regd_find_country(reg->country_code);
  625. if (country == NULL) {
  626. printk(KERN_DEBUG
  627. "ath: no valid country maps found for "
  628. "country code: 0x%0x\n",
  629. reg->country_code);
  630. return -EINVAL;
  631. } else {
  632. regdmn = country->regDmnEnum;
  633. printk(KERN_DEBUG "ath: country maps to "
  634. "regdmn code: 0x%0x\n",
  635. regdmn);
  636. }
  637. }
  638. reg->regpair = ath_get_regpair(regdmn);
  639. if (!reg->regpair) {
  640. printk(KERN_DEBUG "ath: "
  641. "No regulatory domain pair found, cannot continue\n");
  642. return -EINVAL;
  643. }
  644. if (!country)
  645. country = ath_regd_find_country_by_rd(regdmn);
  646. if (country) {
  647. reg->alpha2[0] = country->isoName[0];
  648. reg->alpha2[1] = country->isoName[1];
  649. } else {
  650. reg->alpha2[0] = '0';
  651. reg->alpha2[1] = '0';
  652. }
  653. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  654. reg->alpha2[0], reg->alpha2[1]);
  655. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  656. reg->regpair->reg_domain);
  657. return 0;
  658. }
  659. int
  660. ath_regd_init(struct ath_regulatory *reg,
  661. struct wiphy *wiphy,
  662. void (*reg_notifier)(struct wiphy *wiphy,
  663. struct regulatory_request *request))
  664. {
  665. struct ath_common *common = container_of(reg, struct ath_common,
  666. regulatory);
  667. int r;
  668. r = __ath_regd_init(reg);
  669. if (r)
  670. return r;
  671. if (ath_is_world_regd(reg))
  672. memcpy(&common->reg_world_copy, reg,
  673. sizeof(struct ath_regulatory));
  674. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(ath_regd_init);
  678. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  679. enum ieee80211_band band)
  680. {
  681. if (!reg->regpair ||
  682. (reg->country_code == CTRY_DEFAULT &&
  683. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  684. return SD_NO_CTL;
  685. }
  686. if (ath_regd_get_eepromRD(reg) == CTRY_DEFAULT) {
  687. switch (reg->region) {
  688. case NL80211_DFS_FCC:
  689. return CTL_FCC;
  690. case NL80211_DFS_ETSI:
  691. return CTL_ETSI;
  692. case NL80211_DFS_JP:
  693. return CTL_MKK;
  694. default:
  695. break;
  696. }
  697. }
  698. switch (band) {
  699. case IEEE80211_BAND_2GHZ:
  700. return reg->regpair->reg_2ghz_ctl;
  701. case IEEE80211_BAND_5GHZ:
  702. return reg->regpair->reg_5ghz_ctl;
  703. default:
  704. return NO_CTL;
  705. }
  706. }
  707. EXPORT_SYMBOL(ath_regd_get_band_ctl);