]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/wireless/chan.c
c8d190d911e46984b5eb43ebf3f625e035a32bed
[net-next-2.6.git] / net / wireless / chan.c
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.
5  *
6  * Copyright 2009       Johannes Berg <johannes@sipsolutions.net>
7  */
8
9 #include <net/cfg80211.h>
10 #include "core.h"
11
12 struct ieee80211_channel *
13 rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
14                   int freq, enum nl80211_channel_type channel_type)
15 {
16         struct ieee80211_channel *chan;
17         struct ieee80211_sta_ht_cap *ht_cap;
18
19         chan = ieee80211_get_channel(&rdev->wiphy, freq);
20
21         /* Primary channel not allowed */
22         if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
23                 return NULL;
24
25         if (channel_type == NL80211_CHAN_HT40MINUS &&
26             chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
27                 return NULL;
28         else if (channel_type == NL80211_CHAN_HT40PLUS &&
29                  chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
30                 return NULL;
31
32         ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
33
34         if (channel_type != NL80211_CHAN_NO_HT) {
35                 if (!ht_cap->ht_supported)
36                         return NULL;
37
38                 if (channel_type != NL80211_CHAN_HT20 &&
39                     (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
40                     ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
41                         return NULL;
42         }
43
44         return chan;
45 }
46
47 static bool can_beacon_sec_chan(struct wiphy *wiphy,
48                                 struct ieee80211_channel *chan,
49                                 enum nl80211_channel_type channel_type)
50 {
51         struct ieee80211_channel *sec_chan;
52         int diff;
53
54         switch (channel_type) {
55         case NL80211_CHAN_HT40PLUS:
56                 diff = 20;
57         case NL80211_CHAN_HT40MINUS:
58                 diff = -20;
59         default:
60                 return false;
61         }
62
63         sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
64         if (!sec_chan)
65                 return false;
66
67         /* we'll need a DFS capability later */
68         if (sec_chan->flags & (IEEE80211_CHAN_DISABLED |
69                                IEEE80211_CHAN_PASSIVE_SCAN |
70                                IEEE80211_CHAN_NO_IBSS |
71                                IEEE80211_CHAN_RADAR))
72                 return false;
73
74         return true;
75 }
76
77 int cfg80211_set_freq(struct cfg80211_registered_device *rdev,
78                       struct wireless_dev *wdev, int freq,
79                       enum nl80211_channel_type channel_type)
80 {
81         struct ieee80211_channel *chan;
82         int result;
83
84         if (wdev && wdev->iftype == NL80211_IFTYPE_MONITOR)
85                 wdev = NULL;
86
87         if (wdev) {
88                 ASSERT_WDEV_LOCK(wdev);
89
90                 if (!netif_running(wdev->netdev))
91                         return -ENETDOWN;
92         }
93
94         if (!rdev->ops->set_channel)
95                 return -EOPNOTSUPP;
96
97         chan = rdev_freq_to_chan(rdev, freq, channel_type);
98         if (!chan)
99                 return -EINVAL;
100
101         /* Both channels should be able to initiate communication */
102         if (wdev && (wdev->iftype == NL80211_IFTYPE_ADHOC ||
103                      wdev->iftype == NL80211_IFTYPE_AP ||
104                      wdev->iftype == NL80211_IFTYPE_AP_VLAN ||
105                      wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
106                      wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
107                 switch (channel_type) {
108                 case NL80211_CHAN_HT40PLUS:
109                 case NL80211_CHAN_HT40MINUS:
110                         if (!can_beacon_sec_chan(&rdev->wiphy, chan,
111                                                  channel_type)) {
112                                 printk(KERN_DEBUG
113                                        "cfg80211: Secondary channel not "
114                                        "allowed to initiate communication\n");
115                                 return -EINVAL;
116                         }
117                         break;
118                 default:
119                         break;
120                 }
121         }
122
123         result = rdev->ops->set_channel(&rdev->wiphy,
124                                         wdev ? wdev->netdev : NULL,
125                                         chan, channel_type);
126         if (result)
127                 return result;
128
129         if (wdev)
130                 wdev->channel = chan;
131
132         return 0;
133 }