blob: bf1737fc9a7e47fe448e71a058334fb5fdb1b068 [file] [log] [blame]
Johannes Berg59bbb6f2009-08-07 17:22:35 +02001/*
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
12struct ieee80211_channel *
13rdev_fixed_channel(struct cfg80211_registered_device *rdev,
14 struct wireless_dev *for_wdev)
15{
16 struct wireless_dev *wdev;
17 struct ieee80211_channel *result = NULL;
18
19 WARN_ON(!mutex_is_locked(&rdev->devlist_mtx));
20
21 list_for_each_entry(wdev, &rdev->netdev_list, list) {
22 if (wdev == for_wdev)
23 continue;
24
25 /*
26 * Lock manually to tell lockdep about allowed
27 * nesting here if for_wdev->mtx is held already.
28 * This is ok as it's all under the rdev devlist
29 * mutex and as such can only be done once at any
30 * given time.
31 */
32 mutex_lock_nested(&wdev->mtx, SINGLE_DEPTH_NESTING);
33 if (wdev->current_bss)
34 result = wdev->current_bss->pub.channel;
35 wdev_unlock(wdev);
36
37 if (result)
38 break;
39 }
40
41 return result;
42}
43
Jouni Malinen9588bbd2009-12-23 13:15:41 +010044struct ieee80211_channel *
45rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
46 int freq, enum nl80211_channel_type channel_type)
47{
48 struct ieee80211_channel *chan;
49 struct ieee80211_sta_ht_cap *ht_cap;
50
51 chan = ieee80211_get_channel(&rdev->wiphy, freq);
52
53 /* Primary channel not allowed */
54 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
55 return NULL;
56
57 if (channel_type == NL80211_CHAN_HT40MINUS &&
58 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
59 return NULL;
60 else if (channel_type == NL80211_CHAN_HT40PLUS &&
61 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
62 return NULL;
63
64 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
65
66 if (channel_type != NL80211_CHAN_NO_HT) {
67 if (!ht_cap->ht_supported)
68 return NULL;
69
70 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
71 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
72 return NULL;
73 }
74
75 return chan;
76}
77
Johannes Berg59bbb6f2009-08-07 17:22:35 +020078int rdev_set_freq(struct cfg80211_registered_device *rdev,
Johannes Berg4b181142009-08-08 11:03:58 +020079 struct wireless_dev *for_wdev,
Johannes Berg59bbb6f2009-08-07 17:22:35 +020080 int freq, enum nl80211_channel_type channel_type)
81{
82 struct ieee80211_channel *chan;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020083 int result;
84
Johannes Berg4b181142009-08-08 11:03:58 +020085 if (rdev_fixed_channel(rdev, for_wdev))
Johannes Berg59bbb6f2009-08-07 17:22:35 +020086 return -EBUSY;
87
88 if (!rdev->ops->set_channel)
89 return -EOPNOTSUPP;
90
Jouni Malinen9588bbd2009-12-23 13:15:41 +010091 chan = rdev_freq_to_chan(rdev, freq, channel_type);
92 if (!chan)
Johannes Berg59bbb6f2009-08-07 17:22:35 +020093 return -EINVAL;
94
Johannes Berg59bbb6f2009-08-07 17:22:35 +020095 result = rdev->ops->set_channel(&rdev->wiphy, chan, channel_type);
96 if (result)
97 return result;
98
99 rdev->channel = chan;
100
101 return 0;
102}