xref: /openbmc/linux/net/wireless/reg.c (revision f15cbe6f1a4b4d9df59142fc8e4abb973302cf44)
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 /*
12  * This regulatory domain control implementation is highly incomplete, it
13  * only exists for the purpose of not regressing mac80211.
14  *
15  * For now, drivers can restrict the set of allowed channels by either
16  * not registering those channels or setting the IEEE80211_CHAN_DISABLED
17  * flag; that flag will only be *set* by this code, never *cleared.
18  *
19  * The usual implementation is for a driver to read a device EEPROM to
20  * determine which regulatory domain it should be operating under, then
21  * looking up the allowable channels in a driver-local table and finally
22  * registering those channels in the wiphy structure.
23  *
24  * Alternatively, drivers that trust the regulatory domain control here
25  * will register a complete set of capabilities and the control code
26  * will restrict the set by setting the IEEE80211_CHAN_* flags.
27  */
28 #include <linux/kernel.h>
29 #include <net/wireless.h>
30 #include "core.h"
31 
32 static char *ieee80211_regdom = "US";
33 module_param(ieee80211_regdom, charp, 0444);
34 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
35 
36 struct ieee80211_channel_range {
37 	short start_freq;
38 	short end_freq;
39 	int max_power;
40 	int max_antenna_gain;
41 	u32 flags;
42 };
43 
44 struct ieee80211_regdomain {
45 	const char *code;
46 	const struct ieee80211_channel_range *ranges;
47 	int n_ranges;
48 };
49 
50 #define RANGE_PWR(_start, _end, _pwr, _ag, _flags)	\
51 	{ _start, _end, _pwr, _ag, _flags }
52 
53 
54 /*
55  * Ideally, in the future, these definitions will be loaded from a
56  * userspace table via some daemon.
57  */
58 static const struct ieee80211_channel_range ieee80211_US_channels[] = {
59 	/* IEEE 802.11b/g, channels 1..11 */
60 	RANGE_PWR(2412, 2462, 27, 6, 0),
61 	/* IEEE 802.11a, channel 36*/
62 	RANGE_PWR(5180, 5180, 23, 6, 0),
63 	/* IEEE 802.11a, channel 40*/
64 	RANGE_PWR(5200, 5200, 23, 6, 0),
65 	/* IEEE 802.11a, channel 44*/
66 	RANGE_PWR(5220, 5220, 23, 6, 0),
67 	/* IEEE 802.11a, channels 48..64 */
68 	RANGE_PWR(5240, 5320, 23, 6, 0),
69 	/* IEEE 802.11a, channels 149..165, outdoor */
70 	RANGE_PWR(5745, 5825, 30, 6, 0),
71 };
72 
73 static const struct ieee80211_channel_range ieee80211_JP_channels[] = {
74 	/* IEEE 802.11b/g, channels 1..14 */
75 	RANGE_PWR(2412, 2484, 20, 6, 0),
76 	/* IEEE 802.11a, channels 34..48 */
77 	RANGE_PWR(5170, 5240, 20, 6, IEEE80211_CHAN_PASSIVE_SCAN),
78 	/* IEEE 802.11a, channels 52..64 */
79 	RANGE_PWR(5260, 5320, 20, 6, IEEE80211_CHAN_NO_IBSS |
80 				     IEEE80211_CHAN_RADAR),
81 };
82 
83 static const struct ieee80211_channel_range ieee80211_EU_channels[] = {
84 	/* IEEE 802.11b/g, channels 1..13 */
85 	RANGE_PWR(2412, 2472, 20, 6, 0),
86 	/* IEEE 802.11a, channel 36*/
87 	RANGE_PWR(5180, 5180, 23, 6, IEEE80211_CHAN_PASSIVE_SCAN),
88 	/* IEEE 802.11a, channel 40*/
89 	RANGE_PWR(5200, 5200, 23, 6, IEEE80211_CHAN_PASSIVE_SCAN),
90 	/* IEEE 802.11a, channel 44*/
91 	RANGE_PWR(5220, 5220, 23, 6, IEEE80211_CHAN_PASSIVE_SCAN),
92 	/* IEEE 802.11a, channels 48..64 */
93 	RANGE_PWR(5240, 5320, 23, 6, IEEE80211_CHAN_NO_IBSS |
94 				     IEEE80211_CHAN_RADAR),
95 	/* IEEE 802.11a, channels 100..140 */
96 	RANGE_PWR(5500, 5700, 30, 6, IEEE80211_CHAN_NO_IBSS |
97 				     IEEE80211_CHAN_RADAR),
98 };
99 
100 #define REGDOM(_code)							\
101 	{								\
102 		.code = __stringify(_code),				\
103 		.ranges = ieee80211_ ##_code## _channels,		\
104 		.n_ranges = ARRAY_SIZE(ieee80211_ ##_code## _channels),	\
105 	}
106 
107 static const struct ieee80211_regdomain ieee80211_regdoms[] = {
108 	REGDOM(US),
109 	REGDOM(JP),
110 	REGDOM(EU),
111 };
112 
113 
114 static const struct ieee80211_regdomain *get_regdom(void)
115 {
116 	static const struct ieee80211_channel_range
117 	ieee80211_world_channels[] = {
118 		/* IEEE 802.11b/g, channels 1..11 */
119 		RANGE_PWR(2412, 2462, 27, 6, 0),
120 	};
121 	static const struct ieee80211_regdomain regdom_world = REGDOM(world);
122 	int i;
123 
124 	for (i = 0; i < ARRAY_SIZE(ieee80211_regdoms); i++)
125 		if (strcmp(ieee80211_regdom, ieee80211_regdoms[i].code) == 0)
126 			return &ieee80211_regdoms[i];
127 
128 	return &regdom_world;
129 }
130 
131 
132 static void handle_channel(struct ieee80211_channel *chan,
133 			   const struct ieee80211_regdomain *rd)
134 {
135 	int i;
136 	u32 flags = chan->orig_flags;
137 	const struct ieee80211_channel_range *rg = NULL;
138 
139 	for (i = 0; i < rd->n_ranges; i++) {
140 		if (rd->ranges[i].start_freq <= chan->center_freq &&
141 		    chan->center_freq <= rd->ranges[i].end_freq) {
142 			rg = &rd->ranges[i];
143 			break;
144 		}
145 	}
146 
147 	if (!rg) {
148 		/* not found */
149 		flags |= IEEE80211_CHAN_DISABLED;
150 		chan->flags = flags;
151 		return;
152 	}
153 
154 	chan->flags = flags;
155 	chan->max_antenna_gain = min(chan->orig_mag,
156 					 rg->max_antenna_gain);
157 	if (chan->orig_mpwr)
158 		chan->max_power = min(chan->orig_mpwr, rg->max_power);
159 	else
160 		chan->max_power = rg->max_power;
161 }
162 
163 static void handle_band(struct ieee80211_supported_band *sband,
164 			const struct ieee80211_regdomain *rd)
165 {
166 	int i;
167 
168 	for (i = 0; i < sband->n_channels; i++)
169 		handle_channel(&sband->channels[i], rd);
170 }
171 
172 void wiphy_update_regulatory(struct wiphy *wiphy)
173 {
174 	enum ieee80211_band band;
175 	const struct ieee80211_regdomain *rd = get_regdom();
176 
177 	for (band = 0; band < IEEE80211_NUM_BANDS; band++)
178 		if (wiphy->bands[band])
179 			handle_band(wiphy->bands[band], rd);
180 }
181