Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / net / wireless / chan.c
blob9af62b93e2a0a5e51571653a36852f34d7558b44
1 /*
2 * This file contains helper code to handle channel
3 * settings and keeping track of what is possible at
4 * any point in time.
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 */
9 #include <linux/export.h>
10 #include <net/cfg80211.h>
11 #include "core.h"
13 struct ieee80211_channel *
14 rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
15 int freq, enum nl80211_channel_type channel_type)
17 struct ieee80211_channel *chan;
18 struct ieee80211_sta_ht_cap *ht_cap;
20 chan = ieee80211_get_channel(&rdev->wiphy, freq);
22 /* Primary channel not allowed */
23 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
24 return NULL;
26 if (channel_type == NL80211_CHAN_HT40MINUS &&
27 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
28 return NULL;
29 else if (channel_type == NL80211_CHAN_HT40PLUS &&
30 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
31 return NULL;
33 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
35 if (channel_type != NL80211_CHAN_NO_HT) {
36 if (!ht_cap->ht_supported)
37 return NULL;
39 if (channel_type != NL80211_CHAN_HT20 &&
40 (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
41 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
42 return NULL;
45 return chan;
48 int cfg80211_can_beacon_sec_chan(struct wiphy *wiphy,
49 struct ieee80211_channel *chan,
50 enum nl80211_channel_type channel_type)
52 struct ieee80211_channel *sec_chan;
53 int diff;
55 switch (channel_type) {
56 case NL80211_CHAN_HT40PLUS:
57 diff = 20;
58 break;
59 case NL80211_CHAN_HT40MINUS:
60 diff = -20;
61 break;
62 default:
63 return false;
66 sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
67 if (!sec_chan)
68 return false;
70 /* we'll need a DFS capability later */
71 if (sec_chan->flags & (IEEE80211_CHAN_DISABLED |
72 IEEE80211_CHAN_PASSIVE_SCAN |
73 IEEE80211_CHAN_NO_IBSS |
74 IEEE80211_CHAN_RADAR))
75 return false;
77 return true;
79 EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan);
81 int cfg80211_set_freq(struct cfg80211_registered_device *rdev,
82 struct wireless_dev *wdev, int freq,
83 enum nl80211_channel_type channel_type)
85 struct ieee80211_channel *chan;
86 int result;
87 struct wireless_dev *mon_dev = NULL;
89 if (wdev && wdev->iftype == NL80211_IFTYPE_MONITOR) {
90 mon_dev = wdev;
91 wdev = NULL;
94 if (wdev) {
95 ASSERT_WDEV_LOCK(wdev);
97 if (!netif_running(wdev->netdev))
98 return -ENETDOWN;
101 if (!rdev->ops->set_channel)
102 return -EOPNOTSUPP;
104 chan = rdev_freq_to_chan(rdev, freq, channel_type);
105 if (!chan)
106 return -EINVAL;
108 /* Both channels should be able to initiate communication */
109 if (wdev && (wdev->iftype == NL80211_IFTYPE_ADHOC ||
110 wdev->iftype == NL80211_IFTYPE_AP ||
111 wdev->iftype == NL80211_IFTYPE_AP_VLAN ||
112 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
113 wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
114 switch (channel_type) {
115 case NL80211_CHAN_HT40PLUS:
116 case NL80211_CHAN_HT40MINUS:
117 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, chan,
118 channel_type)) {
119 printk(KERN_DEBUG
120 "cfg80211: Secondary channel not "
121 "allowed to initiate communication\n");
122 return -EINVAL;
124 break;
125 default:
126 break;
130 result = rdev->ops->set_channel(&rdev->wiphy,
131 wdev ? wdev->netdev : NULL,
132 chan, channel_type);
133 if (result)
134 return result;
136 if (wdev)
137 wdev->channel = chan;
139 if (mon_dev)
140 mon_dev->channel = chan;
142 return 0;