2 * This file contains helper code to handle channel
3 * settings and keeping track of what is possible at
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
9 #include <linux/export.h>
10 #include <net/cfg80211.h>
14 void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
15 struct ieee80211_channel *chan,
16 enum nl80211_channel_type chan_type)
22 chandef->center_freq2 = 0;
25 case NL80211_CHAN_NO_HT:
26 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
27 chandef->center_freq1 = chan->center_freq;
29 case NL80211_CHAN_HT20:
30 chandef->width = NL80211_CHAN_WIDTH_20;
31 chandef->center_freq1 = chan->center_freq;
33 case NL80211_CHAN_HT40PLUS:
34 chandef->width = NL80211_CHAN_WIDTH_40;
35 chandef->center_freq1 = chan->center_freq + 10;
37 case NL80211_CHAN_HT40MINUS:
38 chandef->width = NL80211_CHAN_WIDTH_40;
39 chandef->center_freq1 = chan->center_freq - 10;
45 EXPORT_SYMBOL(cfg80211_chandef_create);
47 bool cfg80211_chan_def_valid(const struct cfg80211_chan_def *chandef)
54 control_freq = chandef->chan->center_freq;
56 switch (chandef->width) {
57 case NL80211_CHAN_WIDTH_20:
58 case NL80211_CHAN_WIDTH_20_NOHT:
59 if (chandef->center_freq1 != control_freq)
61 if (chandef->center_freq2)
64 case NL80211_CHAN_WIDTH_40:
65 if (chandef->center_freq1 != control_freq + 10 &&
66 chandef->center_freq1 != control_freq - 10)
68 if (chandef->center_freq2)
71 case NL80211_CHAN_WIDTH_80P80:
72 if (chandef->center_freq1 != control_freq + 30 &&
73 chandef->center_freq1 != control_freq + 10 &&
74 chandef->center_freq1 != control_freq - 10 &&
75 chandef->center_freq1 != control_freq - 30)
77 if (!chandef->center_freq2)
80 case NL80211_CHAN_WIDTH_80:
81 if (chandef->center_freq1 != control_freq + 30 &&
82 chandef->center_freq1 != control_freq + 10 &&
83 chandef->center_freq1 != control_freq - 10 &&
84 chandef->center_freq1 != control_freq - 30)
86 if (chandef->center_freq2)
89 case NL80211_CHAN_WIDTH_160:
90 if (chandef->center_freq1 != control_freq + 70 &&
91 chandef->center_freq1 != control_freq + 50 &&
92 chandef->center_freq1 != control_freq + 30 &&
93 chandef->center_freq1 != control_freq + 10 &&
94 chandef->center_freq1 != control_freq - 10 &&
95 chandef->center_freq1 != control_freq - 30 &&
96 chandef->center_freq1 != control_freq - 50 &&
97 chandef->center_freq1 != control_freq - 70)
99 if (chandef->center_freq2)
109 static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
110 int *pri40, int *pri80)
115 case NL80211_CHAN_WIDTH_40:
116 *pri40 = c->center_freq1;
119 case NL80211_CHAN_WIDTH_80:
120 case NL80211_CHAN_WIDTH_80P80:
121 *pri80 = c->center_freq1;
123 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
127 *pri40 = c->center_freq1 - 20 + 40 * tmp;
129 case NL80211_CHAN_WIDTH_160:
131 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
135 *pri40 = c->center_freq1 - 60 + 40 * tmp;
138 *pri80 = c->center_freq1 - 40 + 80 * tmp;
145 const struct cfg80211_chan_def *
146 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
147 const struct cfg80211_chan_def *c2)
149 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
151 /* If they are identical, return */
152 if (cfg80211_chandef_identical(c1, c2))
155 /* otherwise, must have same control channel */
156 if (c1->chan != c2->chan)
160 * If they have the same width, but aren't identical,
161 * then they can't be compatible.
163 if (c1->width == c2->width)
166 if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
167 c1->width == NL80211_CHAN_WIDTH_20)
170 if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
171 c2->width == NL80211_CHAN_WIDTH_20)
174 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
175 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
177 if (c1_pri40 != c2_pri40)
180 WARN_ON(!c1_pri80 && !c2_pri80);
181 if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
184 if (c1->width > c2->width)
188 EXPORT_SYMBOL(cfg80211_chandef_compatible);
190 bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
191 u32 center_freq, u32 bandwidth,
192 u32 prohibited_flags)
194 struct ieee80211_channel *c;
197 for (freq = center_freq - bandwidth/2 + 10;
198 freq <= center_freq + bandwidth/2 - 10;
200 c = ieee80211_get_channel(wiphy, freq);
201 if (!c || c->flags & prohibited_flags)
208 static bool cfg80211_check_beacon_chans(struct wiphy *wiphy,
209 u32 center_freq, u32 bw)
211 return cfg80211_secondary_chans_ok(wiphy, center_freq, bw,
212 IEEE80211_CHAN_DISABLED |
213 IEEE80211_CHAN_PASSIVE_SCAN |
214 IEEE80211_CHAN_NO_IBSS |
215 IEEE80211_CHAN_RADAR);
218 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
219 struct cfg80211_chan_def *chandef)
224 trace_cfg80211_reg_can_beacon(wiphy, chandef);
226 if (WARN_ON(!cfg80211_chan_def_valid(chandef))) {
227 trace_cfg80211_return_bool(false);
231 switch (chandef->width) {
232 case NL80211_CHAN_WIDTH_20_NOHT:
233 case NL80211_CHAN_WIDTH_20:
236 case NL80211_CHAN_WIDTH_40:
239 case NL80211_CHAN_WIDTH_80:
240 case NL80211_CHAN_WIDTH_80P80:
243 case NL80211_CHAN_WIDTH_160:
248 trace_cfg80211_return_bool(false);
252 res = cfg80211_check_beacon_chans(wiphy, chandef->center_freq1, width);
254 if (res && chandef->center_freq2)
255 res = cfg80211_check_beacon_chans(wiphy, chandef->center_freq2,
258 trace_cfg80211_return_bool(res);
261 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
263 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
264 struct cfg80211_chan_def *chandef)
266 if (!rdev->ops->set_monitor_channel)
268 if (!cfg80211_has_monitors_only(rdev))
271 return rdev_set_monitor_channel(rdev, chandef);
275 cfg80211_get_chan_state(struct wireless_dev *wdev,
276 struct ieee80211_channel **chan,
277 enum cfg80211_chan_mode *chanmode)
280 *chanmode = CHAN_MODE_UNDEFINED;
282 ASSERT_WDEV_LOCK(wdev);
284 if (wdev->netdev && !netif_running(wdev->netdev))
287 switch (wdev->iftype) {
288 case NL80211_IFTYPE_ADHOC:
289 if (wdev->current_bss) {
290 *chan = wdev->current_bss->pub.channel;
291 *chanmode = wdev->ibss_fixed
293 : CHAN_MODE_EXCLUSIVE;
296 case NL80211_IFTYPE_STATION:
297 case NL80211_IFTYPE_P2P_CLIENT:
298 if (wdev->current_bss) {
299 *chan = wdev->current_bss->pub.channel;
300 *chanmode = CHAN_MODE_SHARED;
304 case NL80211_IFTYPE_AP:
305 case NL80211_IFTYPE_P2P_GO:
306 if (wdev->beacon_interval) {
307 *chan = wdev->channel;
308 *chanmode = CHAN_MODE_SHARED;
311 case NL80211_IFTYPE_MESH_POINT:
312 if (wdev->mesh_id_len) {
313 *chan = wdev->channel;
314 *chanmode = CHAN_MODE_SHARED;
317 case NL80211_IFTYPE_MONITOR:
318 case NL80211_IFTYPE_AP_VLAN:
319 case NL80211_IFTYPE_WDS:
320 /* these interface types don't really have a channel */
322 case NL80211_IFTYPE_P2P_DEVICE:
323 if (wdev->wiphy->features &
324 NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
325 *chanmode = CHAN_MODE_EXCLUSIVE;
327 case NL80211_IFTYPE_UNSPECIFIED:
328 case NUM_NL80211_IFTYPES: