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