]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/mac80211/chan.c
samsung-laptop: Add quirk for broken acpi_video backlight on N250P
[mirror_ubuntu-zesty-kernel.git] / net / mac80211 / chan.c
CommitLineData
f444de05
JB
1/*
2 * mac80211 - channel management
3 */
4
0aaffa9b 5#include <linux/nl80211.h>
3448c005 6#include <linux/export.h>
3117bbdb 7#include <net/cfg80211.h>
f444de05 8#include "ieee80211_i.h"
35f2fce9 9#include "driver-ops.h"
f444de05 10
4bf88530
JB
11static void ieee80211_change_chandef(struct ieee80211_local *local,
12 struct ieee80211_chanctx *ctx,
13 const struct cfg80211_chan_def *chandef)
23a85b45 14{
4bf88530 15 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
e89a96f5 16 return;
0aaffa9b 17
4bf88530
JB
18 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
19
20 ctx->conf.def = *chandef;
21 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
55de908a
JB
22
23 if (!local->use_chanctx) {
4bf88530 24 local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
55de908a
JB
25 ieee80211_hw_config(local, 0);
26 }
0aaffa9b 27}
d01a1e65
MK
28
29static struct ieee80211_chanctx *
30ieee80211_find_chanctx(struct ieee80211_local *local,
4bf88530 31 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
32 enum ieee80211_chanctx_mode mode)
33{
34 struct ieee80211_chanctx *ctx;
35
36 lockdep_assert_held(&local->chanctx_mtx);
37
38 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
39 return NULL;
d01a1e65
MK
40
41 list_for_each_entry(ctx, &local->chanctx_list, list) {
4bf88530 42 const struct cfg80211_chan_def *compat;
e89a96f5 43
d01a1e65
MK
44 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
45 continue;
4bf88530
JB
46
47 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
48 if (!compat)
d01a1e65
MK
49 continue;
50
4bf88530 51 ieee80211_change_chandef(local, ctx, compat);
e89a96f5 52
d01a1e65
MK
53 return ctx;
54 }
55
56 return NULL;
57}
58
59static struct ieee80211_chanctx *
60ieee80211_new_chanctx(struct ieee80211_local *local,
4bf88530 61 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
62 enum ieee80211_chanctx_mode mode)
63{
64 struct ieee80211_chanctx *ctx;
35f2fce9 65 int err;
d01a1e65
MK
66
67 lockdep_assert_held(&local->chanctx_mtx);
68
69 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
70 if (!ctx)
71 return ERR_PTR(-ENOMEM);
72
4bf88530 73 ctx->conf.def = *chandef;
04ecd257
JB
74 ctx->conf.rx_chains_static = 1;
75 ctx->conf.rx_chains_dynamic = 1;
d01a1e65
MK
76 ctx->mode = mode;
77
55de908a 78 if (!local->use_chanctx) {
4bf88530
JB
79 local->_oper_channel_type =
80 cfg80211_get_chandef_type(chandef);
81 local->_oper_channel = chandef->chan;
55de908a
JB
82 ieee80211_hw_config(local, 0);
83 } else {
84 err = drv_add_chanctx(local, ctx);
85 if (err) {
86 kfree(ctx);
87 return ERR_PTR(err);
88 }
35f2fce9
MK
89 }
90
3448c005 91 list_add_rcu(&ctx->list, &local->chanctx_list);
d01a1e65
MK
92
93 return ctx;
94}
95
96static void ieee80211_free_chanctx(struct ieee80211_local *local,
97 struct ieee80211_chanctx *ctx)
98{
99 lockdep_assert_held(&local->chanctx_mtx);
100
101 WARN_ON_ONCE(ctx->refcount != 0);
102
55de908a
JB
103 if (!local->use_chanctx) {
104 local->_oper_channel_type = NL80211_CHAN_NO_HT;
105 ieee80211_hw_config(local, 0);
106 } else {
107 drv_remove_chanctx(local, ctx);
108 }
35f2fce9 109
3448c005 110 list_del_rcu(&ctx->list);
d01a1e65
MK
111 kfree_rcu(ctx, rcu_head);
112}
113
114static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
115 struct ieee80211_chanctx *ctx)
116{
35f2fce9
MK
117 struct ieee80211_local *local = sdata->local;
118 int ret;
d01a1e65
MK
119
120 lockdep_assert_held(&local->chanctx_mtx);
121
35f2fce9
MK
122 ret = drv_assign_vif_chanctx(local, sdata, ctx);
123 if (ret)
124 return ret;
125
d01a1e65
MK
126 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
127 ctx->refcount++;
128
1ea6f9c0
JB
129 ieee80211_recalc_txpower(sdata);
130
d01a1e65
MK
131 return 0;
132}
133
4bf88530
JB
134static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
135 struct ieee80211_chanctx *ctx)
e89a96f5
MK
136{
137 struct ieee80211_chanctx_conf *conf = &ctx->conf;
138 struct ieee80211_sub_if_data *sdata;
4bf88530 139 const struct cfg80211_chan_def *compat = NULL;
e89a96f5
MK
140
141 lockdep_assert_held(&local->chanctx_mtx);
142
143 rcu_read_lock();
144 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4bf88530 145
e89a96f5
MK
146 if (!ieee80211_sdata_running(sdata))
147 continue;
148 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
149 continue;
150
4bf88530
JB
151 if (!compat)
152 compat = &sdata->vif.bss_conf.chandef;
153
154 compat = cfg80211_chandef_compatible(
155 &sdata->vif.bss_conf.chandef, compat);
156 if (!compat)
157 break;
e89a96f5
MK
158 }
159 rcu_read_unlock();
160
4bf88530
JB
161 if (WARN_ON_ONCE(!compat))
162 return;
e89a96f5 163
4bf88530 164 ieee80211_change_chandef(local, ctx, compat);
e89a96f5
MK
165}
166
d01a1e65
MK
167static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
168 struct ieee80211_chanctx *ctx)
169{
35f2fce9 170 struct ieee80211_local *local = sdata->local;
d01a1e65
MK
171
172 lockdep_assert_held(&local->chanctx_mtx);
173
174 ctx->refcount--;
175 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
35f2fce9
MK
176
177 drv_unassign_vif_chanctx(local, sdata, ctx);
e89a96f5 178
04ecd257 179 if (ctx->refcount > 0) {
e89a96f5 180 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
04ecd257
JB
181 ieee80211_recalc_smps_chanctx(local, ctx);
182 }
d01a1e65
MK
183}
184
185static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
186{
187 struct ieee80211_local *local = sdata->local;
188 struct ieee80211_chanctx_conf *conf;
189 struct ieee80211_chanctx *ctx;
190
191 lockdep_assert_held(&local->chanctx_mtx);
192
193 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
194 lockdep_is_held(&local->chanctx_mtx));
195 if (!conf)
196 return;
197
198 ctx = container_of(conf, struct ieee80211_chanctx, conf);
199
200 ieee80211_unassign_vif_chanctx(sdata, ctx);
201 if (ctx->refcount == 0)
202 ieee80211_free_chanctx(local, ctx);
203}
204
04ecd257
JB
205void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
206 struct ieee80211_chanctx *chanctx)
207{
208 struct ieee80211_sub_if_data *sdata;
209 u8 rx_chains_static, rx_chains_dynamic;
210
211 lockdep_assert_held(&local->chanctx_mtx);
212
213 rx_chains_static = 1;
214 rx_chains_dynamic = 1;
215
216 rcu_read_lock();
217 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
218 u8 needed_static, needed_dynamic;
219
220 if (!ieee80211_sdata_running(sdata))
221 continue;
222
223 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
224 &chanctx->conf)
225 continue;
226
227 switch (sdata->vif.type) {
228 case NL80211_IFTYPE_P2P_DEVICE:
229 continue;
230 case NL80211_IFTYPE_STATION:
231 if (!sdata->u.mgd.associated)
232 continue;
233 break;
234 case NL80211_IFTYPE_AP_VLAN:
235 continue;
236 case NL80211_IFTYPE_AP:
237 case NL80211_IFTYPE_ADHOC:
238 case NL80211_IFTYPE_WDS:
239 case NL80211_IFTYPE_MESH_POINT:
240 break;
241 default:
242 WARN_ON_ONCE(1);
243 }
244
245 switch (sdata->smps_mode) {
246 default:
247 WARN_ONCE(1, "Invalid SMPS mode %d\n",
248 sdata->smps_mode);
249 /* fall through */
250 case IEEE80211_SMPS_OFF:
251 needed_static = sdata->needed_rx_chains;
252 needed_dynamic = sdata->needed_rx_chains;
253 break;
254 case IEEE80211_SMPS_DYNAMIC:
255 needed_static = 1;
256 needed_dynamic = sdata->needed_rx_chains;
257 break;
258 case IEEE80211_SMPS_STATIC:
259 needed_static = 1;
260 needed_dynamic = 1;
261 break;
262 }
263
264 rx_chains_static = max(rx_chains_static, needed_static);
265 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
266 }
267 rcu_read_unlock();
268
269 if (!local->use_chanctx) {
270 if (rx_chains_static > 1)
271 local->smps_mode = IEEE80211_SMPS_OFF;
272 else if (rx_chains_dynamic > 1)
273 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
274 else
275 local->smps_mode = IEEE80211_SMPS_STATIC;
276 ieee80211_hw_config(local, 0);
277 }
278
279 if (rx_chains_static == chanctx->conf.rx_chains_static &&
280 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
281 return;
282
283 chanctx->conf.rx_chains_static = rx_chains_static;
284 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
285 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
286}
287
d01a1e65 288int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
4bf88530 289 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
290 enum ieee80211_chanctx_mode mode)
291{
292 struct ieee80211_local *local = sdata->local;
293 struct ieee80211_chanctx *ctx;
294 int ret;
295
55de908a
JB
296 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
297
d01a1e65
MK
298 mutex_lock(&local->chanctx_mtx);
299 __ieee80211_vif_release_channel(sdata);
300
4bf88530 301 ctx = ieee80211_find_chanctx(local, chandef, mode);
d01a1e65 302 if (!ctx)
4bf88530 303 ctx = ieee80211_new_chanctx(local, chandef, mode);
d01a1e65
MK
304 if (IS_ERR(ctx)) {
305 ret = PTR_ERR(ctx);
306 goto out;
307 }
308
4bf88530 309 sdata->vif.bss_conf.chandef = *chandef;
55de908a 310
d01a1e65
MK
311 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
312 if (ret) {
313 /* if assign fails refcount stays the same */
314 if (ctx->refcount == 0)
315 ieee80211_free_chanctx(local, ctx);
316 goto out;
317 }
318
04ecd257 319 ieee80211_recalc_smps_chanctx(local, ctx);
d01a1e65
MK
320 out:
321 mutex_unlock(&local->chanctx_mtx);
322 return ret;
323}
324
325void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
326{
55de908a
JB
327 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
328
d01a1e65
MK
329 mutex_lock(&sdata->local->chanctx_mtx);
330 __ieee80211_vif_release_channel(sdata);
331 mutex_unlock(&sdata->local->chanctx_mtx);
332}
3448c005
JB
333
334void ieee80211_iter_chan_contexts_atomic(
335 struct ieee80211_hw *hw,
336 void (*iter)(struct ieee80211_hw *hw,
337 struct ieee80211_chanctx_conf *chanctx_conf,
338 void *data),
339 void *iter_data)
340{
341 struct ieee80211_local *local = hw_to_local(hw);
342 struct ieee80211_chanctx *ctx;
343
344 rcu_read_lock();
345 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
346 iter(hw, &ctx->conf, iter_data);
347 rcu_read_unlock();
348}
349EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);