blob: 038f8f133c54107b8285d178a662b8a3b3110843 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
Johannes Berg8318d782008-01-24 19:38:38 +01006 *
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
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012/**
13 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010014 *
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 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070020 * 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 *
Johannes Berg8318d782008-01-24 19:38:38 +010034 */
35#include <linux/kernel.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070036#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
Johannes Berg8318d782008-01-24 19:38:38 +010040#include <net/wireless.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070041#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010042#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070043#include "reg.h"
Johannes Berg8318d782008-01-24 19:38:38 +010044
Johannes Berg734366d2008-09-15 10:56:48 +020045/* wiphy is set if this request's initiator is REGDOM_SET_BY_DRIVER */
46struct regulatory_request {
Johannes Berg734366d2008-09-15 10:56:48 +020047 struct wiphy *wiphy;
Johannes Berg734366d2008-09-15 10:56:48 +020048 enum reg_set_by initiator;
49 char alpha2[2];
50};
51
Johannes Bergf6037d02008-10-21 11:01:33 +020052static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020053
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070054/* To trigger userspace events */
55static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010056
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070057/* Keep the ordering from large to small */
58static u32 supported_bandwidths[] = {
59 MHZ_TO_KHZ(40),
60 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010061};
62
Johannes Berg734366d2008-09-15 10:56:48 +020063/* 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 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +020066static const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020067
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
Johannes Berga3d2eaf2008-09-15 11:10:52 +020079static const struct ieee80211_regdomain *cfg80211_world_regdom =
80 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +020081
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
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200166static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200167{
168 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
169 return true;
170 return false;
171}
Johannes Berg734366d2008-09-15 10:56:48 +0200172#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200173static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
174{
175 return false;
176}
177#endif
178
Johannes Berg734366d2008-09-15 10:56:48 +0200179static void reset_regdomains(void)
180{
Johannes Berg942b25c2008-09-15 11:26:47 +0200181 /* 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);
Johannes Berg734366d2008-09-15 10:56:48 +0200193
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200194 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200195 cfg80211_regdomain = NULL;
196}
197
198/* Dynamic world regulatory domain requested by the wireless
199 * core upon initialization */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200200static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200201{
Johannes Bergf6037d02008-10-21 11:01:33 +0200202 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200203
204 reset_regdomains();
205
206 cfg80211_world_regdom = rd;
207 cfg80211_regdomain = rd;
208}
Johannes Berg734366d2008-09-15 10:56:48 +0200209
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200210bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100211{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700212 if (!alpha2)
213 return false;
214 if (alpha2[0] == '0' && alpha2[1] == '0')
215 return true;
216 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100217}
218
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200219static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700220{
221 if (!alpha2)
222 return false;
223 if (alpha2[0] != 0 && alpha2[1] != 0)
224 return true;
225 return false;
226}
Johannes Berg8318d782008-01-24 19:38:38 +0100227
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700228static bool is_alpha_upper(char letter)
229{
230 /* ASCII A - Z */
231 if (letter >= 65 && letter <= 90)
232 return true;
233 return false;
234}
235
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200236static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700237{
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}
246
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200247static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700248{
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}
255
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200256static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700257{
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
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200266static bool regdom_changed(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700267{
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
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700289 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
290 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700291
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{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700303 /* All initial requests are respected */
Johannes Bergf6037d02008-10-21 11:01:33 +0200304 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700305 return 0;
306
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700307 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:
Johannes Bergf6037d02008-10-21 11:01:33 +0200316 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700317 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);
Johannes Bergf6037d02008-10-21 11:01:33 +0200346 if (last_request->initiator == REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700347 /* 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:
Johannes Bergf6037d02008-10-21 11:01:33 +0200372 if (last_request->initiator == REGDOM_SET_BY_USER ||
373 last_request->initiator == REGDOM_SET_BY_CORE)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700374 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;
385 }
386}
387
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200389bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390{
Johannes Bergf6037d02008-10-21 11:01:33 +0200391 if (!last_request)
392 return false;
393
394 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395}
396
397/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200398static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700399{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200400 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700401 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
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200417static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200419 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700420 unsigned int i;
421
422 if (!rd->n_reg_rules)
423 return false;
424
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;
432}
433
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}
448
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)
Johannes Berg8318d782008-01-24 19:38:38 +0100475{
476 int i;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700477 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100478
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700479 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;
Johannes Berg8318d782008-01-24 19:38:38 +0100494 break;
495 }
496 }
497
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700498 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) {
Johannes Berg8318d782008-01-24 19:38:38 +0100513 flags |= IEEE80211_CHAN_DISABLED;
514 chan->flags = flags;
515 return;
516 }
517
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700518 power_rule = &reg_rule->power_rule;
519
520 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100521 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700522 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
523 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400524 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700525 chan->max_power = min(chan->orig_mpwr,
526 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400527 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700528 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100529}
530
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700531static void handle_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100532{
533 int i;
534
535 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700536 handle_channel(&sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +0100537}
538
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700539static void update_all_wiphy_regulatory(enum reg_set_by setby)
540{
541 struct cfg80211_registered_device *drv;
542
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)
Johannes Berg8318d782008-01-24 19:38:38 +0100548{
549 enum ieee80211_band band;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700550 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100551 if (wiphy->bands[band])
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700552 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
Johannes Bergf6037d02008-10-21 11:01:33 +0200590 kfree(last_request);
591 last_request = request;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700592 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
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700608int 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
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200631static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700632{
633 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200634 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;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700637
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
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200666static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700667{
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
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200685void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700686{
687 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
688 rd->alpha2[0], rd->alpha2[1]);
689 print_rd_rules(rd);
690}
691
Johannes Bergd2372b32008-10-24 20:32:20 +0200692/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200693static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700694{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700695 /* Some basic sanity checks first */
696
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700697 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +0200698 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700699 return -EINVAL;
700 update_world_regdomain(rd);
701 return 0;
702 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700703
704 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
705 !is_unknown_alpha2(rd->alpha2))
706 return -EINVAL;
707
Johannes Bergf6037d02008-10-21 11:01:33 +0200708 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700709 return -EINVAL;
710
Johannes Berg942b25c2008-09-15 11:26:47 +0200711 /* allow overriding the static definitions if CRDA is present */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700712 if (!is_old_static_regdom(cfg80211_regdomain) &&
Johannes Berg942b25c2008-09-15 11:26:47 +0200713 !regdom_changed(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700714 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700715
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
Johannes Bergf6037d02008-10-21 11:01:33 +0200721 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700722 return -EINVAL;
723
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700724 reset_regdomains();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700725
726 /* Country IE parsing coming soon */
Johannes Bergf6037d02008-10-21 11:01:33 +0200727 switch (last_request->initiator) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700728 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;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700746
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
Johannes Bergd2372b32008-10-24 20:32:20 +0200753 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200754int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700755{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700756 int r;
757
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700758 /* Note that this doesn't update the wiphys, this is done below */
759 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +0200760 if (r) {
761 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700762 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +0200763 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700764
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700765 /* 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 */
Johannes Bergf6037d02008-10-21 11:01:33 +0200769 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700770
771 print_regdomain(rd);
772
773 return r;
774}
775
776int regulatory_init(void)
777{
Johannes Berg734366d2008-09-15 10:56:48 +0200778 int err;
779
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700780 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
781 if (IS_ERR(reg_pdev))
782 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +0200783
784#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200785 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200786
Johannes Berg942b25c2008-09-15 11:26:47 +0200787 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +0200788 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 */
Johannes Bergac9440a2008-10-21 11:08:27 +0200793 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Johannes Berg734366d2008-09-15 10:56:48 +0200794 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
795 ieee80211_regdom, NULL);
796#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200797 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200798
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
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700806 return 0;
807}
808
809void regulatory_exit(void)
810{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700811 mutex_lock(&cfg80211_drv_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +0200812
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700813 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +0200814
Johannes Bergf6037d02008-10-21 11:01:33 +0200815 kfree(last_request);
816
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700817 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +0200818
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700819 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +0100820}