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