reg.c 22 KB

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