reg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /**
  12. * DOC: Wireless regulatory infrastructure
  13. *
  14. * The usual implementation is for a driver to read a device EEPROM to
  15. * determine which regulatory domain it should be operating under, then
  16. * looking up the allowable channels in a driver-local table and finally
  17. * registering those channels in the wiphy structure.
  18. *
  19. * Another set of compliance enforcement is for drivers to use their
  20. * own compliance limits which can be stored on the EEPROM. The host
  21. * driver or firmware may ensure these are used.
  22. *
  23. * In addition to all this we provide an extra layer of regulatory
  24. * conformance. For drivers which do not have any regulatory
  25. * information CRDA provides the complete regulatory solution.
  26. * For others it provides a community effort on further restrictions
  27. * to enhance compliance.
  28. *
  29. * Note: When number of rules --> infinity we will not be able to
  30. * index on alpha2 any more, instead we'll probably have to
  31. * rely on some SHA1 checksum of the regdomain for example.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/random.h>
  37. #include <linux/nl80211.h>
  38. #include <linux/platform_device.h>
  39. #include <net/wireless.h>
  40. #include <net/cfg80211.h>
  41. #include "core.h"
  42. #include "reg.h"
  43. /*
  44. * wiphy is set if this request's initiator is
  45. * REGDOM_SET_BY_COUNTRY_IE or _DRIVER
  46. */
  47. struct regulatory_request {
  48. struct wiphy *wiphy;
  49. enum reg_set_by initiator;
  50. char alpha2[2];
  51. };
  52. static struct regulatory_request *last_request;
  53. /* To trigger userspace events */
  54. static struct platform_device *reg_pdev;
  55. /* Keep the ordering from large to small */
  56. static u32 supported_bandwidths[] = {
  57. MHZ_TO_KHZ(40),
  58. MHZ_TO_KHZ(20),
  59. };
  60. /* Central wireless core regulatory domains, we only need two,
  61. * the current one and a world regulatory domain in case we have no
  62. * information to give us an alpha2 */
  63. static const struct ieee80211_regdomain *cfg80211_regdomain;
  64. /* We keep a static world regulatory domain in case of the absence of CRDA */
  65. static const struct ieee80211_regdomain world_regdom = {
  66. .n_reg_rules = 1,
  67. .alpha2 = "00",
  68. .reg_rules = {
  69. REG_RULE(2412-10, 2462+10, 40, 6, 20,
  70. NL80211_RRF_PASSIVE_SCAN |
  71. NL80211_RRF_NO_IBSS),
  72. }
  73. };
  74. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  75. &world_regdom;
  76. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  77. static char *ieee80211_regdom = "US";
  78. module_param(ieee80211_regdom, charp, 0444);
  79. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  80. /* We assume 40 MHz bandwidth for the old regulatory work.
  81. * We make emphasis we are using the exact same frequencies
  82. * as before */
  83. static const struct ieee80211_regdomain us_regdom = {
  84. .n_reg_rules = 6,
  85. .alpha2 = "US",
  86. .reg_rules = {
  87. /* IEEE 802.11b/g, channels 1..11 */
  88. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  89. /* IEEE 802.11a, channel 36 */
  90. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  91. /* IEEE 802.11a, channel 40 */
  92. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  93. /* IEEE 802.11a, channel 44 */
  94. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  95. /* IEEE 802.11a, channels 48..64 */
  96. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  97. /* IEEE 802.11a, channels 149..165, outdoor */
  98. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  99. }
  100. };
  101. static const struct ieee80211_regdomain jp_regdom = {
  102. .n_reg_rules = 3,
  103. .alpha2 = "JP",
  104. .reg_rules = {
  105. /* IEEE 802.11b/g, channels 1..14 */
  106. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  107. /* IEEE 802.11a, channels 34..48 */
  108. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  109. NL80211_RRF_PASSIVE_SCAN),
  110. /* IEEE 802.11a, channels 52..64 */
  111. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  112. NL80211_RRF_NO_IBSS |
  113. NL80211_RRF_DFS),
  114. }
  115. };
  116. static const struct ieee80211_regdomain eu_regdom = {
  117. .n_reg_rules = 6,
  118. /* This alpha2 is bogus, we leave it here just for stupid
  119. * backward compatibility */
  120. .alpha2 = "EU",
  121. .reg_rules = {
  122. /* IEEE 802.11b/g, channels 1..13 */
  123. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  124. /* IEEE 802.11a, channel 36 */
  125. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  126. NL80211_RRF_PASSIVE_SCAN),
  127. /* IEEE 802.11a, channel 40 */
  128. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  129. NL80211_RRF_PASSIVE_SCAN),
  130. /* IEEE 802.11a, channel 44 */
  131. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  132. NL80211_RRF_PASSIVE_SCAN),
  133. /* IEEE 802.11a, channels 48..64 */
  134. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  135. NL80211_RRF_NO_IBSS |
  136. NL80211_RRF_DFS),
  137. /* IEEE 802.11a, channels 100..140 */
  138. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  139. NL80211_RRF_NO_IBSS |
  140. NL80211_RRF_DFS),
  141. }
  142. };
  143. static const struct ieee80211_regdomain *static_regdom(char *alpha2)
  144. {
  145. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  146. return &us_regdom;
  147. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  148. return &jp_regdom;
  149. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  150. return &eu_regdom;
  151. /* Default, as per the old rules */
  152. return &us_regdom;
  153. }
  154. static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  155. {
  156. if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
  157. return true;
  158. return false;
  159. }
  160. #else
  161. static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  162. {
  163. return false;
  164. }
  165. #endif
  166. static void reset_regdomains(void)
  167. {
  168. /* avoid freeing static information or freeing something twice */
  169. if (cfg80211_regdomain == cfg80211_world_regdom)
  170. cfg80211_regdomain = NULL;
  171. if (cfg80211_world_regdom == &world_regdom)
  172. cfg80211_world_regdom = NULL;
  173. if (cfg80211_regdomain == &world_regdom)
  174. cfg80211_regdomain = NULL;
  175. if (is_old_static_regdom(cfg80211_regdomain))
  176. cfg80211_regdomain = NULL;
  177. kfree(cfg80211_regdomain);
  178. kfree(cfg80211_world_regdom);
  179. cfg80211_world_regdom = &world_regdom;
  180. cfg80211_regdomain = NULL;
  181. }
  182. /* Dynamic world regulatory domain requested by the wireless
  183. * core upon initialization */
  184. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  185. {
  186. BUG_ON(!last_request);
  187. reset_regdomains();
  188. cfg80211_world_regdom = rd;
  189. cfg80211_regdomain = rd;
  190. }
  191. bool is_world_regdom(const char *alpha2)
  192. {
  193. if (!alpha2)
  194. return false;
  195. if (alpha2[0] == '0' && alpha2[1] == '0')
  196. return true;
  197. return false;
  198. }
  199. static bool is_alpha2_set(const char *alpha2)
  200. {
  201. if (!alpha2)
  202. return false;
  203. if (alpha2[0] != 0 && alpha2[1] != 0)
  204. return true;
  205. return false;
  206. }
  207. static bool is_alpha_upper(char letter)
  208. {
  209. /* ASCII A - Z */
  210. if (letter >= 65 && letter <= 90)
  211. return true;
  212. return false;
  213. }
  214. static bool is_unknown_alpha2(const char *alpha2)
  215. {
  216. if (!alpha2)
  217. return false;
  218. /* Special case where regulatory domain was built by driver
  219. * but a specific alpha2 cannot be determined */
  220. if (alpha2[0] == '9' && alpha2[1] == '9')
  221. return true;
  222. return false;
  223. }
  224. static bool is_an_alpha2(const char *alpha2)
  225. {
  226. if (!alpha2)
  227. return false;
  228. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  229. return true;
  230. return false;
  231. }
  232. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  233. {
  234. if (!alpha2_x || !alpha2_y)
  235. return false;
  236. if (alpha2_x[0] == alpha2_y[0] &&
  237. alpha2_x[1] == alpha2_y[1])
  238. return true;
  239. return false;
  240. }
  241. static bool regdom_changed(const char *alpha2)
  242. {
  243. if (!cfg80211_regdomain)
  244. return true;
  245. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  246. return false;
  247. return true;
  248. }
  249. /* This lets us keep regulatory code which is updated on a regulatory
  250. * basis in userspace. */
  251. static int call_crda(const char *alpha2)
  252. {
  253. char country_env[9 + 2] = "COUNTRY=";
  254. char *envp[] = {
  255. country_env,
  256. NULL
  257. };
  258. if (!is_world_regdom((char *) alpha2))
  259. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  260. alpha2[0], alpha2[1]);
  261. else
  262. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  263. "regulatory domain\n");
  264. country_env[8] = alpha2[0];
  265. country_env[9] = alpha2[1];
  266. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  267. }
  268. /* This has the logic which determines when a new request
  269. * should be ignored. */
  270. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  271. const char *alpha2)
  272. {
  273. /* All initial requests are respected */
  274. if (!last_request)
  275. return 0;
  276. switch (set_by) {
  277. case REGDOM_SET_BY_INIT:
  278. return -EINVAL;
  279. case REGDOM_SET_BY_CORE:
  280. /*
  281. * Always respect new wireless core hints, should only happen
  282. * when updating the world regulatory domain at init.
  283. */
  284. return 0;
  285. case REGDOM_SET_BY_COUNTRY_IE:
  286. if (unlikely(!is_an_alpha2(alpha2)))
  287. return -EINVAL;
  288. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  289. if (last_request->wiphy != wiphy) {
  290. /*
  291. * Two cards with two APs claiming different
  292. * different Country IE alpha2s. We could
  293. * intersect them, but that seems unlikely
  294. * to be correct. Reject second one for now.
  295. */
  296. if (!alpha2_equal(alpha2,
  297. cfg80211_regdomain->alpha2))
  298. return -EOPNOTSUPP;
  299. return -EALREADY;
  300. }
  301. /* Two consecutive Country IE hints on the same wiphy */
  302. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  303. return 0;
  304. return -EALREADY;
  305. }
  306. /*
  307. * Ignore Country IE hints for now, need to think about
  308. * what we need to do to support multi-domain operation.
  309. */
  310. return -EOPNOTSUPP;
  311. case REGDOM_SET_BY_DRIVER:
  312. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  313. return -EALREADY;
  314. return 0;
  315. case REGDOM_SET_BY_USER:
  316. /*
  317. * If the user wants to override the AP's hint, we may
  318. * need to follow both and use the intersection. For now,
  319. * reject any such attempt (but we don't support country
  320. * IEs right now anyway.)
  321. */
  322. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  323. return -EOPNOTSUPP;
  324. return 0;
  325. }
  326. return -EINVAL;
  327. }
  328. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  329. bool reg_is_valid_request(const char *alpha2)
  330. {
  331. if (!last_request)
  332. return false;
  333. return alpha2_equal(last_request->alpha2, alpha2);
  334. }
  335. /* Sanity check on a regulatory rule */
  336. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  337. {
  338. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  339. u32 freq_diff;
  340. if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
  341. return false;
  342. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  343. return false;
  344. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  345. if (freq_range->max_bandwidth_khz > freq_diff)
  346. return false;
  347. return true;
  348. }
  349. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  350. {
  351. const struct ieee80211_reg_rule *reg_rule = NULL;
  352. unsigned int i;
  353. if (!rd->n_reg_rules)
  354. return false;
  355. for (i = 0; i < rd->n_reg_rules; i++) {
  356. reg_rule = &rd->reg_rules[i];
  357. if (!is_valid_reg_rule(reg_rule))
  358. return false;
  359. }
  360. return true;
  361. }
  362. /* Returns value in KHz */
  363. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  364. u32 freq)
  365. {
  366. unsigned int i;
  367. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  368. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  369. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  370. if (start_freq_khz >= freq_range->start_freq_khz &&
  371. end_freq_khz <= freq_range->end_freq_khz)
  372. return supported_bandwidths[i];
  373. }
  374. return 0;
  375. }
  376. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  377. * want to just have the channel structure use these */
  378. static u32 map_regdom_flags(u32 rd_flags)
  379. {
  380. u32 channel_flags = 0;
  381. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  382. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  383. if (rd_flags & NL80211_RRF_NO_IBSS)
  384. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  385. if (rd_flags & NL80211_RRF_DFS)
  386. channel_flags |= IEEE80211_CHAN_RADAR;
  387. return channel_flags;
  388. }
  389. /**
  390. * freq_reg_info - get regulatory information for the given frequency
  391. * @center_freq: Frequency in KHz for which we want regulatory information for
  392. * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
  393. * you can set this to 0. If this frequency is allowed we then set
  394. * this value to the maximum allowed bandwidth.
  395. * @reg_rule: the regulatory rule which we have for this frequency
  396. *
  397. * Use this function to get the regulatory rule for a specific frequency.
  398. */
  399. static int freq_reg_info(u32 center_freq, u32 *bandwidth,
  400. const struct ieee80211_reg_rule **reg_rule)
  401. {
  402. int i;
  403. u32 max_bandwidth = 0;
  404. if (!cfg80211_regdomain)
  405. return -EINVAL;
  406. for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
  407. const struct ieee80211_reg_rule *rr;
  408. const struct ieee80211_freq_range *fr = NULL;
  409. const struct ieee80211_power_rule *pr = NULL;
  410. rr = &cfg80211_regdomain->reg_rules[i];
  411. fr = &rr->freq_range;
  412. pr = &rr->power_rule;
  413. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  414. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  415. *reg_rule = rr;
  416. *bandwidth = max_bandwidth;
  417. break;
  418. }
  419. }
  420. return !max_bandwidth;
  421. }
  422. static void handle_channel(struct ieee80211_channel *chan)
  423. {
  424. int r;
  425. u32 flags = chan->orig_flags;
  426. u32 max_bandwidth = 0;
  427. const struct ieee80211_reg_rule *reg_rule = NULL;
  428. const struct ieee80211_power_rule *power_rule = NULL;
  429. r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
  430. &max_bandwidth, &reg_rule);
  431. if (r) {
  432. flags |= IEEE80211_CHAN_DISABLED;
  433. chan->flags = flags;
  434. return;
  435. }
  436. power_rule = &reg_rule->power_rule;
  437. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  438. chan->max_antenna_gain = min(chan->orig_mag,
  439. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  440. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  441. if (chan->orig_mpwr)
  442. chan->max_power = min(chan->orig_mpwr,
  443. (int) MBM_TO_DBM(power_rule->max_eirp));
  444. else
  445. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  446. }
  447. static void handle_band(struct ieee80211_supported_band *sband)
  448. {
  449. int i;
  450. for (i = 0; i < sband->n_channels; i++)
  451. handle_channel(&sband->channels[i]);
  452. }
  453. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  454. {
  455. struct cfg80211_registered_device *drv;
  456. list_for_each_entry(drv, &cfg80211_drv_list, list)
  457. wiphy_update_regulatory(&drv->wiphy, setby);
  458. }
  459. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  460. {
  461. enum ieee80211_band band;
  462. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  463. if (wiphy->bands[band])
  464. handle_band(wiphy->bands[band]);
  465. if (wiphy->reg_notifier)
  466. wiphy->reg_notifier(wiphy, setby);
  467. }
  468. }
  469. /* Caller must hold &cfg80211_drv_mutex */
  470. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  471. const char *alpha2)
  472. {
  473. struct regulatory_request *request;
  474. int r = 0;
  475. r = ignore_request(wiphy, set_by, alpha2);
  476. if (r)
  477. return r;
  478. switch (set_by) {
  479. case REGDOM_SET_BY_CORE:
  480. case REGDOM_SET_BY_COUNTRY_IE:
  481. case REGDOM_SET_BY_DRIVER:
  482. case REGDOM_SET_BY_USER:
  483. request = kzalloc(sizeof(struct regulatory_request),
  484. GFP_KERNEL);
  485. if (!request)
  486. return -ENOMEM;
  487. request->alpha2[0] = alpha2[0];
  488. request->alpha2[1] = alpha2[1];
  489. request->initiator = set_by;
  490. request->wiphy = wiphy;
  491. kfree(last_request);
  492. last_request = request;
  493. r = call_crda(alpha2);
  494. #ifndef CONFIG_WIRELESS_OLD_REGULATORY
  495. if (r)
  496. printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
  497. #endif
  498. break;
  499. default:
  500. r = -ENOTSUPP;
  501. break;
  502. }
  503. return r;
  504. }
  505. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  506. {
  507. BUG_ON(!alpha2);
  508. mutex_lock(&cfg80211_drv_mutex);
  509. __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2);
  510. mutex_unlock(&cfg80211_drv_mutex);
  511. }
  512. EXPORT_SYMBOL(regulatory_hint);
  513. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  514. {
  515. unsigned int i;
  516. const struct ieee80211_reg_rule *reg_rule = NULL;
  517. const struct ieee80211_freq_range *freq_range = NULL;
  518. const struct ieee80211_power_rule *power_rule = NULL;
  519. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  520. "(max_antenna_gain, max_eirp)\n");
  521. for (i = 0; i < rd->n_reg_rules; i++) {
  522. reg_rule = &rd->reg_rules[i];
  523. freq_range = &reg_rule->freq_range;
  524. power_rule = &reg_rule->power_rule;
  525. /* There may not be documentation for max antenna gain
  526. * in certain regions */
  527. if (power_rule->max_antenna_gain)
  528. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  529. "(%d mBi, %d mBm)\n",
  530. freq_range->start_freq_khz,
  531. freq_range->end_freq_khz,
  532. freq_range->max_bandwidth_khz,
  533. power_rule->max_antenna_gain,
  534. power_rule->max_eirp);
  535. else
  536. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  537. "(N/A, %d mBm)\n",
  538. freq_range->start_freq_khz,
  539. freq_range->end_freq_khz,
  540. freq_range->max_bandwidth_khz,
  541. power_rule->max_eirp);
  542. }
  543. }
  544. static void print_regdomain(const struct ieee80211_regdomain *rd)
  545. {
  546. if (is_world_regdom(rd->alpha2))
  547. printk(KERN_INFO "cfg80211: World regulatory "
  548. "domain updated:\n");
  549. else {
  550. if (is_unknown_alpha2(rd->alpha2))
  551. printk(KERN_INFO "cfg80211: Regulatory domain "
  552. "changed to driver built-in settings "
  553. "(unknown country)\n");
  554. else
  555. printk(KERN_INFO "cfg80211: Regulatory domain "
  556. "changed to country: %c%c\n",
  557. rd->alpha2[0], rd->alpha2[1]);
  558. }
  559. print_rd_rules(rd);
  560. }
  561. void print_regdomain_info(const struct ieee80211_regdomain *rd)
  562. {
  563. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  564. rd->alpha2[0], rd->alpha2[1]);
  565. print_rd_rules(rd);
  566. }
  567. /* Takes ownership of rd only if it doesn't fail */
  568. static int __set_regdom(const struct ieee80211_regdomain *rd)
  569. {
  570. /* Some basic sanity checks first */
  571. if (is_world_regdom(rd->alpha2)) {
  572. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  573. return -EINVAL;
  574. update_world_regdomain(rd);
  575. return 0;
  576. }
  577. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  578. !is_unknown_alpha2(rd->alpha2))
  579. return -EINVAL;
  580. if (!last_request)
  581. return -EINVAL;
  582. /* allow overriding the static definitions if CRDA is present */
  583. if (!is_old_static_regdom(cfg80211_regdomain) &&
  584. !regdom_changed(rd->alpha2))
  585. return -EINVAL;
  586. /* Now lets set the regulatory domain, update all driver channels
  587. * and finally inform them of what we have done, in case they want
  588. * to review or adjust their own settings based on their own
  589. * internal EEPROM data */
  590. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  591. return -EINVAL;
  592. reset_regdomains();
  593. /* Country IE parsing coming soon */
  594. switch (last_request->initiator) {
  595. case REGDOM_SET_BY_CORE:
  596. case REGDOM_SET_BY_DRIVER:
  597. case REGDOM_SET_BY_USER:
  598. if (!is_valid_rd(rd)) {
  599. printk(KERN_ERR "cfg80211: Invalid "
  600. "regulatory domain detected:\n");
  601. print_regdomain_info(rd);
  602. return -EINVAL;
  603. }
  604. break;
  605. case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
  606. WARN_ON(1);
  607. default:
  608. return -EOPNOTSUPP;
  609. }
  610. /* Tada! */
  611. cfg80211_regdomain = rd;
  612. return 0;
  613. }
  614. /* Use this call to set the current regulatory domain. Conflicts with
  615. * multiple drivers can be ironed out later. Caller must've already
  616. * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
  617. int set_regdom(const struct ieee80211_regdomain *rd)
  618. {
  619. int r;
  620. /* Note that this doesn't update the wiphys, this is done below */
  621. r = __set_regdom(rd);
  622. if (r) {
  623. kfree(rd);
  624. return r;
  625. }
  626. /* This would make this whole thing pointless */
  627. BUG_ON(rd != cfg80211_regdomain);
  628. /* update all wiphys now with the new established regulatory domain */
  629. update_all_wiphy_regulatory(last_request->initiator);
  630. print_regdomain(rd);
  631. return r;
  632. }
  633. int regulatory_init(void)
  634. {
  635. int err;
  636. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  637. if (IS_ERR(reg_pdev))
  638. return PTR_ERR(reg_pdev);
  639. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  640. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  641. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  642. print_regdomain_info(cfg80211_regdomain);
  643. /* The old code still requests for a new regdomain and if
  644. * you have CRDA you get it updated, otherwise you get
  645. * stuck with the static values. We ignore "EU" code as
  646. * that is not a valid ISO / IEC 3166 alpha2 */
  647. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  648. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  649. ieee80211_regdom);
  650. #else
  651. cfg80211_regdomain = cfg80211_world_regdom;
  652. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00");
  653. if (err)
  654. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  655. "unable to update world regulatory domain, "
  656. "using static definition\n");
  657. #endif
  658. return 0;
  659. }
  660. void regulatory_exit(void)
  661. {
  662. mutex_lock(&cfg80211_drv_mutex);
  663. reset_regdomains();
  664. kfree(last_request);
  665. platform_device_unregister(reg_pdev);
  666. mutex_unlock(&cfg80211_drv_mutex);
  667. }