regd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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 ATH_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 ATH_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
  34. NL80211_RRF_NO_IR)
  35. #define ATH_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 ATH_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 80, 0, 30,\
  40. NL80211_RRF_NO_IR)
  41. #define ATH_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 80, 0, 30,\
  42. NL80211_RRF_NO_IR)
  43. #define ATH_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 80, 0, 30,\
  44. NL80211_RRF_NO_IR)
  45. #define ATH_2GHZ_ALL ATH_2GHZ_CH01_11, \
  46. ATH_2GHZ_CH12_13, \
  47. ATH_2GHZ_CH14
  48. #define ATH_5GHZ_ALL ATH_5GHZ_5150_5350, \
  49. ATH_5GHZ_5470_5850
  50. /* This one skips what we call "mid band" */
  51. #define ATH_5GHZ_NO_MIDBAND ATH_5GHZ_5150_5350, \
  52. ATH_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. ATH_2GHZ_ALL,
  60. ATH_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. ATH_2GHZ_CH01_11,
  69. ATH_2GHZ_CH12_13,
  70. ATH_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. ATH_2GHZ_CH01_11,
  79. ATH_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. ATH_2GHZ_CH01_11,
  88. ATH_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. ATH_2GHZ_CH01_11,
  97. ATH_2GHZ_CH12_13,
  98. ATH_5GHZ_ALL,
  99. }
  100. };
  101. static bool dynamic_country_user_possible(struct ath_regulatory *reg)
  102. {
  103. if (IS_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 (!IS_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 nl80211_band band;
  304. struct ieee80211_supported_band *sband;
  305. struct ieee80211_channel *ch;
  306. unsigned int i;
  307. for (band = 0; band < NUM_NL80211_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. * @reg: regulatory structure - used for country selection
  322. * @initiator: the regulatory hint initiator
  323. *
  324. * If no country IE has been received always enable passive scan
  325. * and no-ibss on these channels. This is only done for specific
  326. * regulatory SKUs.
  327. *
  328. * If a country IE has been received check its rule for this
  329. * channel first before enabling active scan. The passive scan
  330. * would have been enforced by the initial processing of our
  331. * custom regulatory domain.
  332. */
  333. static void
  334. ath_reg_apply_ir_flags(struct wiphy *wiphy,
  335. struct ath_regulatory *reg,
  336. enum nl80211_reg_initiator initiator)
  337. {
  338. struct ieee80211_supported_band *sband;
  339. sband = wiphy->bands[NL80211_BAND_2GHZ];
  340. if (!sband)
  341. return;
  342. switch(initiator) {
  343. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  344. ath_force_clear_no_ir_freq(wiphy, 2467);
  345. ath_force_clear_no_ir_freq(wiphy, 2472);
  346. break;
  347. case NL80211_REGDOM_SET_BY_USER:
  348. if (!ath_reg_dyn_country_user_allow(reg))
  349. break;
  350. ath_force_clear_no_ir_freq(wiphy, 2467);
  351. ath_force_clear_no_ir_freq(wiphy, 2472);
  352. break;
  353. default:
  354. ath_force_no_ir_freq(wiphy, 2467);
  355. ath_force_no_ir_freq(wiphy, 2472);
  356. }
  357. }
  358. /* Always apply Radar/DFS rules on freq range 5500 MHz - 5700 MHz */
  359. static void ath_reg_apply_radar_flags(struct wiphy *wiphy,
  360. struct ath_regulatory *reg)
  361. {
  362. struct ieee80211_supported_band *sband;
  363. struct ieee80211_channel *ch;
  364. unsigned int i;
  365. if (!wiphy->bands[NL80211_BAND_5GHZ])
  366. return;
  367. sband = wiphy->bands[NL80211_BAND_5GHZ];
  368. for (i = 0; i < sband->n_channels; i++) {
  369. ch = &sband->channels[i];
  370. if (!ath_is_radar_freq(ch->center_freq, reg))
  371. continue;
  372. /* We always enable radar detection/DFS on this
  373. * frequency range. Additionally we also apply on
  374. * this frequency range:
  375. * - If STA mode does not yet have DFS supports disable
  376. * active scanning
  377. * - If adhoc mode does not support DFS yet then
  378. * disable adhoc in the frequency.
  379. * - If AP mode does not yet support radar detection/DFS
  380. * do not allow AP mode
  381. */
  382. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  383. ch->flags |= IEEE80211_CHAN_RADAR |
  384. IEEE80211_CHAN_NO_IR;
  385. }
  386. }
  387. static void ath_reg_apply_world_flags(struct wiphy *wiphy,
  388. enum nl80211_reg_initiator initiator,
  389. struct ath_regulatory *reg)
  390. {
  391. switch (reg->regpair->reg_domain) {
  392. case 0x60:
  393. case 0x63:
  394. case 0x66:
  395. case 0x67:
  396. case 0x6C:
  397. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  398. break;
  399. case 0x68:
  400. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  401. ath_reg_apply_ir_flags(wiphy, reg, initiator);
  402. break;
  403. default:
  404. if (ath_reg_dyn_country_user_allow(reg))
  405. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  406. }
  407. }
  408. u16 ath_regd_find_country_by_name(char *alpha2)
  409. {
  410. unsigned int i;
  411. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  412. if (!memcmp(allCountries[i].isoName, alpha2, 2))
  413. return allCountries[i].countryCode;
  414. }
  415. return -1;
  416. }
  417. EXPORT_SYMBOL(ath_regd_find_country_by_name);
  418. static int __ath_reg_dyn_country(struct wiphy *wiphy,
  419. struct ath_regulatory *reg,
  420. struct regulatory_request *request)
  421. {
  422. u16 country_code;
  423. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  424. !ath_is_world_regd(reg))
  425. return -EINVAL;
  426. country_code = ath_regd_find_country_by_name(request->alpha2);
  427. if (country_code == (u16) -1)
  428. return -EINVAL;
  429. reg->current_rd = COUNTRY_ERD_FLAG;
  430. reg->current_rd |= country_code;
  431. __ath_regd_init(reg);
  432. ath_reg_apply_world_flags(wiphy, request->initiator, reg);
  433. return 0;
  434. }
  435. static void ath_reg_dyn_country(struct wiphy *wiphy,
  436. struct ath_regulatory *reg,
  437. struct regulatory_request *request)
  438. {
  439. if (__ath_reg_dyn_country(wiphy, reg, request))
  440. return;
  441. printk(KERN_DEBUG "ath: regdomain 0x%0x "
  442. "dynamically updated by %s\n",
  443. reg->current_rd,
  444. reg_initiator_name(request->initiator));
  445. }
  446. void ath_reg_notifier_apply(struct wiphy *wiphy,
  447. struct regulatory_request *request,
  448. struct ath_regulatory *reg)
  449. {
  450. struct ath_common *common = container_of(reg, struct ath_common,
  451. regulatory);
  452. /* We always apply this */
  453. ath_reg_apply_radar_flags(wiphy, reg);
  454. /*
  455. * This would happen when we have sent a custom regulatory request
  456. * a world regulatory domain and the scheduler hasn't yet processed
  457. * any pending requests in the queue.
  458. */
  459. if (!request)
  460. return;
  461. reg->region = request->dfs_region;
  462. switch (request->initiator) {
  463. case NL80211_REGDOM_SET_BY_CORE:
  464. /*
  465. * If common->reg_world_copy is world roaming it means we *were*
  466. * world roaming... so we now have to restore that data.
  467. */
  468. if (!ath_is_world_regd(&common->reg_world_copy))
  469. break;
  470. memcpy(reg, &common->reg_world_copy,
  471. sizeof(struct ath_regulatory));
  472. break;
  473. case NL80211_REGDOM_SET_BY_DRIVER:
  474. break;
  475. case NL80211_REGDOM_SET_BY_USER:
  476. if (ath_reg_dyn_country_user_allow(reg))
  477. ath_reg_dyn_country(wiphy, reg, request);
  478. break;
  479. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  480. ath_reg_dyn_country(wiphy, reg, request);
  481. break;
  482. }
  483. }
  484. EXPORT_SYMBOL(ath_reg_notifier_apply);
  485. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  486. {
  487. u16 rd = ath_regd_get_eepromRD(reg);
  488. int i;
  489. if (rd & COUNTRY_ERD_FLAG) {
  490. /* EEPROM value is a country code */
  491. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  492. printk(KERN_DEBUG
  493. "ath: EEPROM indicates we should expect "
  494. "a country code\n");
  495. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  496. if (allCountries[i].countryCode == cc)
  497. return true;
  498. } else {
  499. /* EEPROM value is a regpair value */
  500. if (rd != CTRY_DEFAULT)
  501. printk(KERN_DEBUG "ath: EEPROM indicates we "
  502. "should expect a direct regpair map\n");
  503. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  504. if (regDomainPairs[i].reg_domain == rd)
  505. return true;
  506. }
  507. printk(KERN_DEBUG
  508. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  509. return false;
  510. }
  511. /* EEPROM country code to regpair mapping */
  512. static struct country_code_to_enum_rd*
  513. ath_regd_find_country(u16 countryCode)
  514. {
  515. int i;
  516. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  517. if (allCountries[i].countryCode == countryCode)
  518. return &allCountries[i];
  519. }
  520. return NULL;
  521. }
  522. /* EEPROM rd code to regpair mapping */
  523. static struct country_code_to_enum_rd*
  524. ath_regd_find_country_by_rd(int regdmn)
  525. {
  526. int i;
  527. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  528. if (allCountries[i].regDmnEnum == regdmn)
  529. return &allCountries[i];
  530. }
  531. return NULL;
  532. }
  533. /* Returns the map of the EEPROM set RD to a country code */
  534. static u16 ath_regd_get_default_country(u16 rd)
  535. {
  536. if (rd & COUNTRY_ERD_FLAG) {
  537. struct country_code_to_enum_rd *country = NULL;
  538. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  539. country = ath_regd_find_country(cc);
  540. if (country != NULL)
  541. return cc;
  542. }
  543. return CTRY_DEFAULT;
  544. }
  545. static struct reg_dmn_pair_mapping*
  546. ath_get_regpair(int regdmn)
  547. {
  548. int i;
  549. if (regdmn == NO_ENUMRD)
  550. return NULL;
  551. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  552. if (regDomainPairs[i].reg_domain == regdmn)
  553. return &regDomainPairs[i];
  554. }
  555. return NULL;
  556. }
  557. static int
  558. ath_regd_init_wiphy(struct ath_regulatory *reg,
  559. struct wiphy *wiphy,
  560. void (*reg_notifier)(struct wiphy *wiphy,
  561. struct regulatory_request *request))
  562. {
  563. const struct ieee80211_regdomain *regd;
  564. wiphy->reg_notifier = reg_notifier;
  565. wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
  566. REGULATORY_CUSTOM_REG;
  567. if (ath_is_world_regd(reg)) {
  568. /*
  569. * Anything applied here (prior to wiphy registration) gets
  570. * saved on the wiphy orig_* parameters
  571. */
  572. regd = ath_world_regdomain(reg);
  573. wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_FOLLOW_POWER;
  574. } else {
  575. /*
  576. * This gets applied in the case of the absence of CRDA,
  577. * it's our own custom world regulatory domain, similar to
  578. * cfg80211's but we enable passive scanning.
  579. */
  580. regd = ath_default_world_regdomain();
  581. }
  582. wiphy_apply_custom_regulatory(wiphy, regd);
  583. ath_reg_apply_radar_flags(wiphy, reg);
  584. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  585. return 0;
  586. }
  587. /*
  588. * Some users have reported their EEPROM programmed with
  589. * 0x8000 set, this is not a supported regulatory domain
  590. * but since we have more than one user with it we need
  591. * a solution for them. We default to 0x64, which is the
  592. * default Atheros world regulatory domain.
  593. */
  594. static void ath_regd_sanitize(struct ath_regulatory *reg)
  595. {
  596. if (reg->current_rd != COUNTRY_ERD_FLAG)
  597. return;
  598. printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
  599. reg->current_rd = 0x64;
  600. }
  601. static int __ath_regd_init(struct ath_regulatory *reg)
  602. {
  603. struct country_code_to_enum_rd *country = NULL;
  604. u16 regdmn;
  605. if (!reg)
  606. return -EINVAL;
  607. ath_regd_sanitize(reg);
  608. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  609. if (!ath_regd_is_eeprom_valid(reg)) {
  610. pr_err("Invalid EEPROM contents\n");
  611. return -EINVAL;
  612. }
  613. regdmn = ath_regd_get_eepromRD(reg);
  614. reg->country_code = ath_regd_get_default_country(regdmn);
  615. if (reg->country_code == CTRY_DEFAULT &&
  616. regdmn == CTRY_DEFAULT) {
  617. printk(KERN_DEBUG "ath: EEPROM indicates default "
  618. "country code should be used\n");
  619. reg->country_code = CTRY_UNITED_STATES;
  620. }
  621. if (reg->country_code == CTRY_DEFAULT) {
  622. country = NULL;
  623. } else {
  624. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  625. "map search\n");
  626. country = ath_regd_find_country(reg->country_code);
  627. if (country == NULL) {
  628. printk(KERN_DEBUG
  629. "ath: no valid country maps found for "
  630. "country code: 0x%0x\n",
  631. reg->country_code);
  632. return -EINVAL;
  633. } else {
  634. regdmn = country->regDmnEnum;
  635. printk(KERN_DEBUG "ath: country maps to "
  636. "regdmn code: 0x%0x\n",
  637. regdmn);
  638. }
  639. }
  640. reg->regpair = ath_get_regpair(regdmn);
  641. if (!reg->regpair) {
  642. printk(KERN_DEBUG "ath: "
  643. "No regulatory domain pair found, cannot continue\n");
  644. return -EINVAL;
  645. }
  646. if (!country)
  647. country = ath_regd_find_country_by_rd(regdmn);
  648. if (country) {
  649. reg->alpha2[0] = country->isoName[0];
  650. reg->alpha2[1] = country->isoName[1];
  651. } else {
  652. reg->alpha2[0] = '0';
  653. reg->alpha2[1] = '0';
  654. }
  655. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  656. reg->alpha2[0], reg->alpha2[1]);
  657. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  658. reg->regpair->reg_domain);
  659. return 0;
  660. }
  661. int
  662. ath_regd_init(struct ath_regulatory *reg,
  663. struct wiphy *wiphy,
  664. void (*reg_notifier)(struct wiphy *wiphy,
  665. struct regulatory_request *request))
  666. {
  667. struct ath_common *common = container_of(reg, struct ath_common,
  668. regulatory);
  669. int r;
  670. r = __ath_regd_init(reg);
  671. if (r)
  672. return r;
  673. if (ath_is_world_regd(reg))
  674. memcpy(&common->reg_world_copy, reg,
  675. sizeof(struct ath_regulatory));
  676. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL(ath_regd_init);
  680. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  681. enum nl80211_band band)
  682. {
  683. if (!reg->regpair ||
  684. (reg->country_code == CTRY_DEFAULT &&
  685. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  686. return SD_NO_CTL;
  687. }
  688. if (ath_regd_get_eepromRD(reg) == CTRY_DEFAULT) {
  689. switch (reg->region) {
  690. case NL80211_DFS_FCC:
  691. return CTL_FCC;
  692. case NL80211_DFS_ETSI:
  693. return CTL_ETSI;
  694. case NL80211_DFS_JP:
  695. return CTL_MKK;
  696. default:
  697. break;
  698. }
  699. }
  700. switch (band) {
  701. case NL80211_BAND_2GHZ:
  702. return reg->regpair->reg_2ghz_ctl;
  703. case NL80211_BAND_5GHZ:
  704. return reg->regpair->reg_5ghz_ctl;
  705. default:
  706. return NO_CTL;
  707. }
  708. }
  709. EXPORT_SYMBOL(ath_regd_get_band_ctl);