blob: dc10071deaaaad62963efb1c0f3d5651e45bda30 [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 Bergbe3d4812008-10-24 20:32:21 +020045/*
46 * wiphy is set if this request's initiator is
47 * REGDOM_SET_BY_COUNTRY_IE or _DRIVER
48 */
Johannes Berg734366d2008-09-15 10:56:48 +020049struct regulatory_request {
Johannes Berg734366d2008-09-15 10:56:48 +020050 struct wiphy *wiphy;
Johannes Berg734366d2008-09-15 10:56:48 +020051 enum reg_set_by initiator;
52 char alpha2[2];
53};
54
Johannes Bergf6037d02008-10-21 11:01:33 +020055static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020056
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070057/* To trigger userspace events */
58static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010059
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070060/* Keep the ordering from large to small */
61static u32 supported_bandwidths[] = {
62 MHZ_TO_KHZ(40),
63 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010064};
65
Johannes Berg734366d2008-09-15 10:56:48 +020066/* Central wireless core regulatory domains, we only need two,
67 * the current one and a world regulatory domain in case we have no
68 * information to give us an alpha2 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +020069static const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020070
71/* We keep a static world regulatory domain in case of the absence of CRDA */
72static const struct ieee80211_regdomain world_regdom = {
73 .n_reg_rules = 1,
74 .alpha2 = "00",
75 .reg_rules = {
76 REG_RULE(2412-10, 2462+10, 40, 6, 20,
77 NL80211_RRF_PASSIVE_SCAN |
78 NL80211_RRF_NO_IBSS),
79 }
80};
81
Johannes Berga3d2eaf2008-09-15 11:10:52 +020082static const struct ieee80211_regdomain *cfg80211_world_regdom =
83 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +020084
85#ifdef CONFIG_WIRELESS_OLD_REGULATORY
86static char *ieee80211_regdom = "US";
87module_param(ieee80211_regdom, charp, 0444);
88MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
89
90/* We assume 40 MHz bandwidth for the old regulatory work.
91 * We make emphasis we are using the exact same frequencies
92 * as before */
93
94static const struct ieee80211_regdomain us_regdom = {
95 .n_reg_rules = 6,
96 .alpha2 = "US",
97 .reg_rules = {
98 /* IEEE 802.11b/g, channels 1..11 */
99 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
100 /* IEEE 802.11a, channel 36 */
101 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
102 /* IEEE 802.11a, channel 40 */
103 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
104 /* IEEE 802.11a, channel 44 */
105 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
106 /* IEEE 802.11a, channels 48..64 */
107 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
108 /* IEEE 802.11a, channels 149..165, outdoor */
109 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
110 }
111};
112
113static const struct ieee80211_regdomain jp_regdom = {
114 .n_reg_rules = 3,
115 .alpha2 = "JP",
116 .reg_rules = {
117 /* IEEE 802.11b/g, channels 1..14 */
118 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
119 /* IEEE 802.11a, channels 34..48 */
120 REG_RULE(5170-10, 5240+10, 40, 6, 20,
121 NL80211_RRF_PASSIVE_SCAN),
122 /* IEEE 802.11a, channels 52..64 */
123 REG_RULE(5260-10, 5320+10, 40, 6, 20,
124 NL80211_RRF_NO_IBSS |
125 NL80211_RRF_DFS),
126 }
127};
128
129static const struct ieee80211_regdomain eu_regdom = {
130 .n_reg_rules = 6,
131 /* This alpha2 is bogus, we leave it here just for stupid
132 * backward compatibility */
133 .alpha2 = "EU",
134 .reg_rules = {
135 /* IEEE 802.11b/g, channels 1..13 */
136 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
137 /* IEEE 802.11a, channel 36 */
138 REG_RULE(5180-10, 5180+10, 40, 6, 23,
139 NL80211_RRF_PASSIVE_SCAN),
140 /* IEEE 802.11a, channel 40 */
141 REG_RULE(5200-10, 5200+10, 40, 6, 23,
142 NL80211_RRF_PASSIVE_SCAN),
143 /* IEEE 802.11a, channel 44 */
144 REG_RULE(5220-10, 5220+10, 40, 6, 23,
145 NL80211_RRF_PASSIVE_SCAN),
146 /* IEEE 802.11a, channels 48..64 */
147 REG_RULE(5240-10, 5320+10, 40, 6, 20,
148 NL80211_RRF_NO_IBSS |
149 NL80211_RRF_DFS),
150 /* IEEE 802.11a, channels 100..140 */
151 REG_RULE(5500-10, 5700+10, 40, 6, 30,
152 NL80211_RRF_NO_IBSS |
153 NL80211_RRF_DFS),
154 }
155};
156
157static const struct ieee80211_regdomain *static_regdom(char *alpha2)
158{
159 if (alpha2[0] == 'U' && alpha2[1] == 'S')
160 return &us_regdom;
161 if (alpha2[0] == 'J' && alpha2[1] == 'P')
162 return &jp_regdom;
163 if (alpha2[0] == 'E' && alpha2[1] == 'U')
164 return &eu_regdom;
165 /* Default, as per the old rules */
166 return &us_regdom;
167}
168
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200169static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200170{
171 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
172 return true;
173 return false;
174}
Johannes Berg734366d2008-09-15 10:56:48 +0200175#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200176static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
177{
178 return false;
179}
180#endif
181
Johannes Berg734366d2008-09-15 10:56:48 +0200182static void reset_regdomains(void)
183{
Johannes Berg942b25c2008-09-15 11:26:47 +0200184 /* avoid freeing static information or freeing something twice */
185 if (cfg80211_regdomain == cfg80211_world_regdom)
186 cfg80211_regdomain = NULL;
187 if (cfg80211_world_regdom == &world_regdom)
188 cfg80211_world_regdom = NULL;
189 if (cfg80211_regdomain == &world_regdom)
190 cfg80211_regdomain = NULL;
191 if (is_old_static_regdom(cfg80211_regdomain))
192 cfg80211_regdomain = NULL;
193
194 kfree(cfg80211_regdomain);
195 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200196
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200197 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200198 cfg80211_regdomain = NULL;
199}
200
201/* Dynamic world regulatory domain requested by the wireless
202 * core upon initialization */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200203static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200204{
Johannes Bergf6037d02008-10-21 11:01:33 +0200205 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200206
207 reset_regdomains();
208
209 cfg80211_world_regdom = rd;
210 cfg80211_regdomain = rd;
211}
Johannes Berg734366d2008-09-15 10:56:48 +0200212
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200213bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100214{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700215 if (!alpha2)
216 return false;
217 if (alpha2[0] == '0' && alpha2[1] == '0')
218 return true;
219 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100220}
221
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200222static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700223{
224 if (!alpha2)
225 return false;
226 if (alpha2[0] != 0 && alpha2[1] != 0)
227 return true;
228 return false;
229}
Johannes Berg8318d782008-01-24 19:38:38 +0100230
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700231static bool is_alpha_upper(char letter)
232{
233 /* ASCII A - Z */
234 if (letter >= 65 && letter <= 90)
235 return true;
236 return false;
237}
238
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200239static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700240{
241 if (!alpha2)
242 return false;
243 /* Special case where regulatory domain was built by driver
244 * but a specific alpha2 cannot be determined */
245 if (alpha2[0] == '9' && alpha2[1] == '9')
246 return true;
247 return false;
248}
249
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200250static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700251{
252 if (!alpha2)
253 return false;
254 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
255 return true;
256 return false;
257}
258
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200259static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700260{
261 if (!alpha2_x || !alpha2_y)
262 return false;
263 if (alpha2_x[0] == alpha2_y[0] &&
264 alpha2_x[1] == alpha2_y[1])
265 return true;
266 return false;
267}
268
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200269static bool regdom_changed(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700270{
271 if (!cfg80211_regdomain)
272 return true;
273 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
274 return false;
275 return true;
276}
277
278/* This lets us keep regulatory code which is updated on a regulatory
279 * basis in userspace. */
280static int call_crda(const char *alpha2)
281{
282 char country_env[9 + 2] = "COUNTRY=";
283 char *envp[] = {
284 country_env,
285 NULL
286 };
287
288 if (!is_world_regdom((char *) alpha2))
289 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
290 alpha2[0], alpha2[1]);
291 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700292 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
293 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700294
295 country_env[8] = alpha2[0];
296 country_env[9] = alpha2[1];
297
298 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
299}
300
301/* This has the logic which determines when a new request
302 * should be ignored. */
303static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
Johannes Bergbe3d4812008-10-24 20:32:21 +0200304 const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700305{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700306 /* All initial requests are respected */
Johannes Bergf6037d02008-10-21 11:01:33 +0200307 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700308 return 0;
309
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700310 switch (set_by) {
311 case REGDOM_SET_BY_INIT:
312 return -EINVAL;
313 case REGDOM_SET_BY_CORE:
314 /* Always respect new wireless core hints, should only
315 * come in for updating the world regulatory domain at init
316 * anyway */
317 return 0;
318 case REGDOM_SET_BY_COUNTRY_IE:
Johannes Bergf6037d02008-10-21 11:01:33 +0200319 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700320 if (last_request->wiphy != wiphy) {
321 /* Two cards with two APs claiming different
322 * different Country IE alpha2s!
323 * You're special!! */
324 if (!alpha2_equal(last_request->alpha2,
325 cfg80211_regdomain->alpha2)) {
326 /* XXX: Deal with conflict, consider
327 * building a new one out of the
328 * intersection */
329 WARN_ON(1);
330 return -EOPNOTSUPP;
331 }
332 return -EALREADY;
333 }
334 /* Two consecutive Country IE hints on the same wiphy */
335 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
336 return 0;
337 return -EALREADY;
338 }
339 if (WARN_ON(!is_alpha2_set(alpha2) || !is_an_alpha2(alpha2)),
340 "Invalid Country IE regulatory hint passed "
341 "to the wireless core\n")
342 return -EINVAL;
343 /* We ignore Country IE hints for now, as we haven't yet
344 * added the dot11MultiDomainCapabilityEnabled flag
345 * for wiphys */
346 return 1;
347 case REGDOM_SET_BY_DRIVER:
348 BUG_ON(!wiphy);
Johannes Bergbe3d4812008-10-24 20:32:21 +0200349 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700350 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700351 if (last_request->initiator == REGDOM_SET_BY_CORE)
352 return 0;
353 /* XXX: Handle intersection, and add the
354 * dot11MultiDomainCapabilityEnabled flag to wiphy. For now
355 * we assume the driver has this set to false, following the
356 * 802.11d dot11MultiDomainCapabilityEnabled documentation */
357 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
358 return 0;
359 return 0;
360 case REGDOM_SET_BY_USER:
Johannes Bergf6037d02008-10-21 11:01:33 +0200361 if (last_request->initiator == REGDOM_SET_BY_USER ||
362 last_request->initiator == REGDOM_SET_BY_CORE)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363 return 0;
364 /* Drivers can use their wiphy's reg_notifier()
365 * to override any information */
366 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
367 return 0;
368 /* XXX: Handle intersection */
369 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
370 return -EOPNOTSUPP;
371 return 0;
372 default:
373 return -EINVAL;
374 }
375}
376
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700377/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200378bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700379{
Johannes Bergf6037d02008-10-21 11:01:33 +0200380 if (!last_request)
381 return false;
382
383 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700384}
385
386/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200387static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200389 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390 u32 freq_diff;
391
392 if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
393 return false;
394
395 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
396 return false;
397
398 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
399
400 if (freq_range->max_bandwidth_khz > freq_diff)
401 return false;
402
403 return true;
404}
405
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200406static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700407{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200408 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700409 unsigned int i;
410
411 if (!rd->n_reg_rules)
412 return false;
413
414 for (i = 0; i < rd->n_reg_rules; i++) {
415 reg_rule = &rd->reg_rules[i];
416 if (!is_valid_reg_rule(reg_rule))
417 return false;
418 }
419
420 return true;
421}
422
423/* Returns value in KHz */
424static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
425 u32 freq)
426{
427 unsigned int i;
428 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
429 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
430 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
431 if (start_freq_khz >= freq_range->start_freq_khz &&
432 end_freq_khz <= freq_range->end_freq_khz)
433 return supported_bandwidths[i];
434 }
435 return 0;
436}
437
438/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
439 * want to just have the channel structure use these */
440static u32 map_regdom_flags(u32 rd_flags)
441{
442 u32 channel_flags = 0;
443 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
444 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
445 if (rd_flags & NL80211_RRF_NO_IBSS)
446 channel_flags |= IEEE80211_CHAN_NO_IBSS;
447 if (rd_flags & NL80211_RRF_DFS)
448 channel_flags |= IEEE80211_CHAN_RADAR;
449 return channel_flags;
450}
451
452/**
453 * freq_reg_info - get regulatory information for the given frequency
454 * @center_freq: Frequency in KHz for which we want regulatory information for
455 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
456 * you can set this to 0. If this frequency is allowed we then set
457 * this value to the maximum allowed bandwidth.
458 * @reg_rule: the regulatory rule which we have for this frequency
459 *
460 * Use this function to get the regulatory rule for a specific frequency.
461 */
462static int freq_reg_info(u32 center_freq, u32 *bandwidth,
463 const struct ieee80211_reg_rule **reg_rule)
Johannes Berg8318d782008-01-24 19:38:38 +0100464{
465 int i;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700466 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100467
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700468 if (!cfg80211_regdomain)
469 return -EINVAL;
470
471 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
472 const struct ieee80211_reg_rule *rr;
473 const struct ieee80211_freq_range *fr = NULL;
474 const struct ieee80211_power_rule *pr = NULL;
475
476 rr = &cfg80211_regdomain->reg_rules[i];
477 fr = &rr->freq_range;
478 pr = &rr->power_rule;
479 max_bandwidth = freq_max_bandwidth(fr, center_freq);
480 if (max_bandwidth && *bandwidth <= max_bandwidth) {
481 *reg_rule = rr;
482 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100483 break;
484 }
485 }
486
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700487 return !max_bandwidth;
488}
489
490static void handle_channel(struct ieee80211_channel *chan)
491{
492 int r;
493 u32 flags = chan->orig_flags;
494 u32 max_bandwidth = 0;
495 const struct ieee80211_reg_rule *reg_rule = NULL;
496 const struct ieee80211_power_rule *power_rule = NULL;
497
498 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
499 &max_bandwidth, &reg_rule);
500
501 if (r) {
Johannes Berg8318d782008-01-24 19:38:38 +0100502 flags |= IEEE80211_CHAN_DISABLED;
503 chan->flags = flags;
504 return;
505 }
506
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700507 power_rule = &reg_rule->power_rule;
508
509 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100510 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700511 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
512 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400513 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700514 chan->max_power = min(chan->orig_mpwr,
515 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400516 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700517 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100518}
519
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700520static void handle_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100521{
522 int i;
523
524 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700525 handle_channel(&sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +0100526}
527
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700528static void update_all_wiphy_regulatory(enum reg_set_by setby)
529{
530 struct cfg80211_registered_device *drv;
531
532 list_for_each_entry(drv, &cfg80211_drv_list, list)
533 wiphy_update_regulatory(&drv->wiphy, setby);
534}
535
536void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +0100537{
538 enum ieee80211_band band;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700539 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100540 if (wiphy->bands[band])
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700541 handle_band(wiphy->bands[band]);
542 if (wiphy->reg_notifier)
543 wiphy->reg_notifier(wiphy, setby);
544 }
545}
546
547/* Caller must hold &cfg80211_drv_mutex */
548int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
Johannes Bergbe3d4812008-10-24 20:32:21 +0200549 const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700550{
551 struct regulatory_request *request;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700552 int r = 0;
553
Johannes Bergbe3d4812008-10-24 20:32:21 +0200554 r = ignore_request(wiphy, set_by, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700555 if (r)
556 return r;
557
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700558 switch (set_by) {
559 case REGDOM_SET_BY_CORE:
560 case REGDOM_SET_BY_COUNTRY_IE:
561 case REGDOM_SET_BY_DRIVER:
562 case REGDOM_SET_BY_USER:
563 request = kzalloc(sizeof(struct regulatory_request),
Johannes Bergbe3d4812008-10-24 20:32:21 +0200564 GFP_KERNEL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700565 if (!request)
566 return -ENOMEM;
567
Johannes Bergbe3d4812008-10-24 20:32:21 +0200568 request->alpha2[0] = alpha2[0];
569 request->alpha2[1] = alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700570 request->initiator = set_by;
571 request->wiphy = wiphy;
572
Johannes Bergf6037d02008-10-21 11:01:33 +0200573 kfree(last_request);
574 last_request = request;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700575 r = call_crda(alpha2);
576#ifndef CONFIG_WIRELESS_OLD_REGULATORY
577 if (r)
578 printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
579#endif
580 break;
581 default:
582 r = -ENOTSUPP;
583 break;
584 }
585
586 return r;
587}
588
Johannes Bergbe3d4812008-10-24 20:32:21 +0200589void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700590{
Johannes Bergbe3d4812008-10-24 20:32:21 +0200591 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700592
593 mutex_lock(&cfg80211_drv_mutex);
Johannes Bergbe3d4812008-10-24 20:32:21 +0200594 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700595 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700596}
597EXPORT_SYMBOL(regulatory_hint);
598
599
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200600static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700601{
602 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200603 const struct ieee80211_reg_rule *reg_rule = NULL;
604 const struct ieee80211_freq_range *freq_range = NULL;
605 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700606
607 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
608 "(max_antenna_gain, max_eirp)\n");
609
610 for (i = 0; i < rd->n_reg_rules; i++) {
611 reg_rule = &rd->reg_rules[i];
612 freq_range = &reg_rule->freq_range;
613 power_rule = &reg_rule->power_rule;
614
615 /* There may not be documentation for max antenna gain
616 * in certain regions */
617 if (power_rule->max_antenna_gain)
618 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
619 "(%d mBi, %d mBm)\n",
620 freq_range->start_freq_khz,
621 freq_range->end_freq_khz,
622 freq_range->max_bandwidth_khz,
623 power_rule->max_antenna_gain,
624 power_rule->max_eirp);
625 else
626 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
627 "(N/A, %d mBm)\n",
628 freq_range->start_freq_khz,
629 freq_range->end_freq_khz,
630 freq_range->max_bandwidth_khz,
631 power_rule->max_eirp);
632 }
633}
634
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200635static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700636{
637
638 if (is_world_regdom(rd->alpha2))
639 printk(KERN_INFO "cfg80211: World regulatory "
640 "domain updated:\n");
641 else {
642 if (is_unknown_alpha2(rd->alpha2))
643 printk(KERN_INFO "cfg80211: Regulatory domain "
644 "changed to driver built-in settings "
645 "(unknown country)\n");
646 else
647 printk(KERN_INFO "cfg80211: Regulatory domain "
648 "changed to country: %c%c\n",
649 rd->alpha2[0], rd->alpha2[1]);
650 }
651 print_rd_rules(rd);
652}
653
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200654void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700655{
656 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
657 rd->alpha2[0], rd->alpha2[1]);
658 print_rd_rules(rd);
659}
660
Johannes Bergd2372b32008-10-24 20:32:20 +0200661/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200662static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700663{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700664 /* Some basic sanity checks first */
665
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700666 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +0200667 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700668 return -EINVAL;
669 update_world_regdomain(rd);
670 return 0;
671 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700672
673 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
674 !is_unknown_alpha2(rd->alpha2))
675 return -EINVAL;
676
Johannes Bergf6037d02008-10-21 11:01:33 +0200677 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700678 return -EINVAL;
679
Johannes Berg942b25c2008-09-15 11:26:47 +0200680 /* allow overriding the static definitions if CRDA is present */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700681 if (!is_old_static_regdom(cfg80211_regdomain) &&
Johannes Berg942b25c2008-09-15 11:26:47 +0200682 !regdom_changed(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700683 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700684
685 /* Now lets set the regulatory domain, update all driver channels
686 * and finally inform them of what we have done, in case they want
687 * to review or adjust their own settings based on their own
688 * internal EEPROM data */
689
Johannes Bergf6037d02008-10-21 11:01:33 +0200690 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700691 return -EINVAL;
692
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700693 reset_regdomains();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700694
695 /* Country IE parsing coming soon */
Johannes Bergf6037d02008-10-21 11:01:33 +0200696 switch (last_request->initiator) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700697 case REGDOM_SET_BY_CORE:
698 case REGDOM_SET_BY_DRIVER:
699 case REGDOM_SET_BY_USER:
700 if (!is_valid_rd(rd)) {
701 printk(KERN_ERR "cfg80211: Invalid "
702 "regulatory domain detected:\n");
703 print_regdomain_info(rd);
704 return -EINVAL;
705 }
706 break;
707 case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
708 WARN_ON(1);
709 default:
710 return -EOPNOTSUPP;
711 }
712
713 /* Tada! */
714 cfg80211_regdomain = rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700715
716 return 0;
717}
718
719
720/* Use this call to set the current regulatory domain. Conflicts with
721 * multiple drivers can be ironed out later. Caller must've already
Johannes Bergd2372b32008-10-24 20:32:20 +0200722 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200723int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700724{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700725 int r;
726
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700727 /* Note that this doesn't update the wiphys, this is done below */
728 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +0200729 if (r) {
730 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700731 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +0200732 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700733
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700734 /* This would make this whole thing pointless */
735 BUG_ON(rd != cfg80211_regdomain);
736
737 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +0200738 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700739
740 print_regdomain(rd);
741
742 return r;
743}
744
745int regulatory_init(void)
746{
Johannes Berg734366d2008-09-15 10:56:48 +0200747 int err;
748
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700749 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
750 if (IS_ERR(reg_pdev))
751 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +0200752
753#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200754 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200755
Johannes Berg942b25c2008-09-15 11:26:47 +0200756 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +0200757 print_regdomain_info(cfg80211_regdomain);
758 /* The old code still requests for a new regdomain and if
759 * you have CRDA you get it updated, otherwise you get
760 * stuck with the static values. We ignore "EU" code as
761 * that is not a valid ISO / IEC 3166 alpha2 */
Johannes Bergac9440a2008-10-21 11:08:27 +0200762 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Johannes Berg734366d2008-09-15 10:56:48 +0200763 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
Johannes Bergbe3d4812008-10-24 20:32:21 +0200764 ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200765#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200766 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200767
Johannes Bergbe3d4812008-10-24 20:32:21 +0200768 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00");
Johannes Berg734366d2008-09-15 10:56:48 +0200769 if (err)
770 printk(KERN_ERR "cfg80211: calling CRDA failed - "
771 "unable to update world regulatory domain, "
772 "using static definition\n");
773#endif
774
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700775 return 0;
776}
777
778void regulatory_exit(void)
779{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700780 mutex_lock(&cfg80211_drv_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +0200781
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700782 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +0200783
Johannes Bergf6037d02008-10-21 11:01:33 +0200784 kfree(last_request);
785
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700786 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +0200787
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700788 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +0100789}