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