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_chandef_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)
108 EXPORT_SYMBOL(cfg80211_chandef_valid);
110 static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
111 int *pri40, int *pri80)
116 case NL80211_CHAN_WIDTH_40:
117 *pri40 = c->center_freq1;
120 case NL80211_CHAN_WIDTH_80:
121 case NL80211_CHAN_WIDTH_80P80:
122 *pri80 = c->center_freq1;
124 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
128 *pri40 = c->center_freq1 - 20 + 40 * tmp;
130 case NL80211_CHAN_WIDTH_160:
132 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
136 *pri40 = c->center_freq1 - 60 + 40 * tmp;
139 *pri80 = c->center_freq1 - 40 + 80 * tmp;
146 const struct cfg80211_chan_def *
147 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
148 const struct cfg80211_chan_def *c2)
150 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
152 /* If they are identical, return */
153 if (cfg80211_chandef_identical(c1, c2))
156 /* otherwise, must have same control channel */
157 if (c1->chan != c2->chan)
161 * If they have the same width, but aren't identical,
162 * then they can't be compatible.
164 if (c1->width == c2->width)
167 if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
168 c1->width == NL80211_CHAN_WIDTH_20)
171 if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
172 c2->width == NL80211_CHAN_WIDTH_20)
175 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
176 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
178 if (c1_pri40 != c2_pri40)
181 WARN_ON(!c1_pri80 && !c2_pri80);
182 if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
185 if (c1->width > c2->width)
189 EXPORT_SYMBOL(cfg80211_chandef_compatible);
191 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
192 u32 center_freq, u32 bandwidth,
193 u32 prohibited_flags)
195 struct ieee80211_channel *c;
198 for (freq = center_freq - bandwidth/2 + 10;
199 freq <= center_freq + bandwidth/2 - 10;
201 c = ieee80211_get_channel(wiphy, freq);
202 if (!c || c->flags & prohibited_flags)
209 bool cfg80211_chandef_usable(struct wiphy *wiphy,
210 const struct cfg80211_chan_def *chandef,
211 u32 prohibited_flags)
213 struct ieee80211_sta_ht_cap *ht_cap;
214 struct ieee80211_sta_vht_cap *vht_cap;
215 u32 width, control_freq;
217 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
220 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
221 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
223 control_freq = chandef->chan->center_freq;
225 switch (chandef->width) {
226 case NL80211_CHAN_WIDTH_20:
227 if (!ht_cap->ht_supported)
229 case NL80211_CHAN_WIDTH_20_NOHT:
232 case NL80211_CHAN_WIDTH_40:
234 if (!ht_cap->ht_supported)
236 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
237 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
239 if (chandef->center_freq1 < control_freq &&
240 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
242 if (chandef->center_freq1 > control_freq &&
243 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
246 case NL80211_CHAN_WIDTH_80P80:
247 if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
249 case NL80211_CHAN_WIDTH_80:
250 if (!vht_cap->vht_supported)
254 case NL80211_CHAN_WIDTH_160:
255 if (!vht_cap->vht_supported)
257 if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
266 /* TODO: missing regulatory check on 80/160 bandwidth */
268 if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
269 width, prohibited_flags))
272 if (!chandef->center_freq2)
274 return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
275 width, prohibited_flags);
277 EXPORT_SYMBOL(cfg80211_chandef_usable);
279 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
280 struct cfg80211_chan_def *chandef)
284 trace_cfg80211_reg_can_beacon(wiphy, chandef);
286 res = cfg80211_chandef_usable(wiphy, chandef,
287 IEEE80211_CHAN_DISABLED |
288 IEEE80211_CHAN_PASSIVE_SCAN |
289 IEEE80211_CHAN_NO_IBSS |
290 IEEE80211_CHAN_RADAR);
292 trace_cfg80211_return_bool(res);
295 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
297 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
298 struct cfg80211_chan_def *chandef)
300 if (!rdev->ops->set_monitor_channel)
302 if (!cfg80211_has_monitors_only(rdev))
305 return rdev_set_monitor_channel(rdev, chandef);
309 cfg80211_get_chan_state(struct wireless_dev *wdev,
310 struct ieee80211_channel **chan,
311 enum cfg80211_chan_mode *chanmode)
314 *chanmode = CHAN_MODE_UNDEFINED;
316 ASSERT_WDEV_LOCK(wdev);
318 if (wdev->netdev && !netif_running(wdev->netdev))
321 switch (wdev->iftype) {
322 case NL80211_IFTYPE_ADHOC:
323 if (wdev->current_bss) {
324 *chan = wdev->current_bss->pub.channel;
325 *chanmode = wdev->ibss_fixed
327 : CHAN_MODE_EXCLUSIVE;
330 case NL80211_IFTYPE_STATION:
331 case NL80211_IFTYPE_P2P_CLIENT:
332 if (wdev->current_bss) {
333 *chan = wdev->current_bss->pub.channel;
334 *chanmode = CHAN_MODE_SHARED;
338 case NL80211_IFTYPE_AP:
339 case NL80211_IFTYPE_P2P_GO:
340 if (wdev->beacon_interval) {
341 *chan = wdev->channel;
342 *chanmode = CHAN_MODE_SHARED;
345 case NL80211_IFTYPE_MESH_POINT:
346 if (wdev->mesh_id_len) {
347 *chan = wdev->channel;
348 *chanmode = CHAN_MODE_SHARED;
351 case NL80211_IFTYPE_MONITOR:
352 case NL80211_IFTYPE_AP_VLAN:
353 case NL80211_IFTYPE_WDS:
354 /* these interface types don't really have a channel */
356 case NL80211_IFTYPE_P2P_DEVICE:
357 if (wdev->wiphy->features &
358 NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
359 *chanmode = CHAN_MODE_EXCLUSIVE;
361 case NL80211_IFTYPE_UNSPECIFIED:
362 case NUM_NL80211_IFTYPES: