| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 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 |  | 
| Alexander Simon | 54858ee | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 9 | #include <linux/export.h> | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 10 | #include <net/cfg80211.h> | 
|  | 11 | #include "core.h" | 
|  | 12 |  | 
|  | 13 | struct ieee80211_channel * | 
| Jouni Malinen | 9588bbd | 2009-12-23 13:15:41 +0100 | [diff] [blame] | 14 | rdev_freq_to_chan(struct cfg80211_registered_device *rdev, | 
|  | 15 | int freq, enum nl80211_channel_type channel_type) | 
|  | 16 | { | 
|  | 17 | struct ieee80211_channel *chan; | 
|  | 18 | struct ieee80211_sta_ht_cap *ht_cap; | 
|  | 19 |  | 
|  | 20 | chan = ieee80211_get_channel(&rdev->wiphy, freq); | 
|  | 21 |  | 
|  | 22 | /* Primary channel not allowed */ | 
|  | 23 | if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) | 
|  | 24 | return NULL; | 
|  | 25 |  | 
|  | 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; | 
|  | 32 |  | 
|  | 33 | ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap; | 
|  | 34 |  | 
|  | 35 | if (channel_type != NL80211_CHAN_NO_HT) { | 
|  | 36 | if (!ht_cap->ht_supported) | 
|  | 37 | return NULL; | 
|  | 38 |  | 
| Helmut Schaa | aed8e1f | 2010-05-17 17:30:59 +0200 | [diff] [blame] | 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)) | 
| Jouni Malinen | 9588bbd | 2009-12-23 13:15:41 +0100 | [diff] [blame] | 42 | return NULL; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | return chan; | 
|  | 46 | } | 
|  | 47 |  | 
| Alexander Simon | 54858ee | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 48 | int cfg80211_can_beacon_sec_chan(struct wiphy *wiphy, | 
|  | 49 | struct ieee80211_channel *chan, | 
|  | 50 | enum nl80211_channel_type channel_type) | 
| Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 51 | { | 
|  | 52 | struct ieee80211_channel *sec_chan; | 
|  | 53 | int diff; | 
|  | 54 |  | 
|  | 55 | switch (channel_type) { | 
|  | 56 | case NL80211_CHAN_HT40PLUS: | 
|  | 57 | diff = 20; | 
| Mark Mentovai | 09a02fd | 2010-11-17 16:34:37 -0500 | [diff] [blame] | 58 | break; | 
| Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 59 | case NL80211_CHAN_HT40MINUS: | 
|  | 60 | diff = -20; | 
| Mark Mentovai | 09a02fd | 2010-11-17 16:34:37 -0500 | [diff] [blame] | 61 | break; | 
| Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 62 | default: | 
|  | 63 | return false; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff); | 
|  | 67 | if (!sec_chan) | 
|  | 68 | return false; | 
|  | 69 |  | 
|  | 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; | 
|  | 76 |  | 
|  | 77 | return true; | 
|  | 78 | } | 
| Alexander Simon | 54858ee | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 79 | EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan); | 
| Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 80 |  | 
| Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 81 | int cfg80211_set_freq(struct cfg80211_registered_device *rdev, | 
|  | 82 | struct wireless_dev *wdev, int freq, | 
|  | 83 | enum nl80211_channel_type channel_type) | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 84 | { | 
|  | 85 | struct ieee80211_channel *chan; | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 86 | int result; | 
|  | 87 |  | 
| Felix Fietkau | 9fbc630 | 2010-05-15 15:46:17 +0200 | [diff] [blame] | 88 | if (wdev && wdev->iftype == NL80211_IFTYPE_MONITOR) | 
| Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 89 | wdev = NULL; | 
|  | 90 |  | 
|  | 91 | if (wdev) { | 
|  | 92 | ASSERT_WDEV_LOCK(wdev); | 
|  | 93 |  | 
|  | 94 | if (!netif_running(wdev->netdev)) | 
|  | 95 | return -ENETDOWN; | 
|  | 96 | } | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 97 |  | 
|  | 98 | if (!rdev->ops->set_channel) | 
|  | 99 | return -EOPNOTSUPP; | 
|  | 100 |  | 
| Jouni Malinen | 9588bbd | 2009-12-23 13:15:41 +0100 | [diff] [blame] | 101 | chan = rdev_freq_to_chan(rdev, freq, channel_type); | 
|  | 102 | if (!chan) | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 103 | return -EINVAL; | 
|  | 104 |  | 
| Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 105 | /* Both channels should be able to initiate communication */ | 
|  | 106 | if (wdev && (wdev->iftype == NL80211_IFTYPE_ADHOC || | 
|  | 107 | wdev->iftype == NL80211_IFTYPE_AP || | 
|  | 108 | wdev->iftype == NL80211_IFTYPE_AP_VLAN || | 
|  | 109 | wdev->iftype == NL80211_IFTYPE_MESH_POINT || | 
|  | 110 | wdev->iftype == NL80211_IFTYPE_P2P_GO)) { | 
|  | 111 | switch (channel_type) { | 
|  | 112 | case NL80211_CHAN_HT40PLUS: | 
|  | 113 | case NL80211_CHAN_HT40MINUS: | 
| Alexander Simon | 54858ee | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 114 | if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, chan, | 
|  | 115 | channel_type)) { | 
| Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 116 | printk(KERN_DEBUG | 
|  | 117 | "cfg80211: Secondary channel not " | 
|  | 118 | "allowed to initiate communication\n"); | 
|  | 119 | return -EINVAL; | 
|  | 120 | } | 
|  | 121 | break; | 
|  | 122 | default: | 
|  | 123 | break; | 
|  | 124 | } | 
|  | 125 | } | 
|  | 126 |  | 
| Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 127 | result = rdev->ops->set_channel(&rdev->wiphy, | 
|  | 128 | wdev ? wdev->netdev : NULL, | 
|  | 129 | chan, channel_type); | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 130 | if (result) | 
|  | 131 | return result; | 
|  | 132 |  | 
| Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 133 | if (wdev) | 
|  | 134 | wdev->channel = chan; | 
| Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 135 |  | 
|  | 136 | return 0; | 
|  | 137 | } |