b43: LCN-PHY: load TX gain table on init
[linux-2.6-block.git] / net / mac80211 / util.c
CommitLineData
c2d1560a
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * utilities for mac80211
12 */
13
14#include <net/mac80211.h>
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/if_arp.h>
c2d1560a 21#include <linux/bitmap.h>
881d966b 22#include <net/net_namespace.h>
c2d1560a 23#include <net/cfg80211.h>
dabeb344 24#include <net/rtnetlink.h>
c2d1560a
JB
25
26#include "ieee80211_i.h"
24487981 27#include "driver-ops.h"
2c8dccc7 28#include "rate.h"
ee385855 29#include "mesh.h"
c2d1560a 30#include "wme.h"
f2753ddb 31#include "led.h"
fffd0934 32#include "wep.h"
c2d1560a
JB
33
34/* privid for wiphys to determine whether they belong to us or not */
35void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
36
9a95371a
LR
37struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
38{
39 struct ieee80211_local *local;
40 BUG_ON(!wiphy);
41
42 local = wiphy_priv(wiphy);
43 return &local->hw;
44}
45EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
c2d1560a 46
71364716 47u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
05c914fe 48 enum nl80211_iftype type)
c2d1560a 49{
a494bb1c 50 __le16 fc = hdr->frame_control;
c2d1560a 51
98f0b0a3
RR
52 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
53 if (len < 16)
c2d1560a
JB
54 return NULL;
55
a494bb1c 56 if (ieee80211_is_data(fc)) {
98f0b0a3
RR
57 if (len < 24) /* drop incorrect hdr len (data) */
58 return NULL;
a494bb1c
HH
59
60 if (ieee80211_has_a4(fc))
c2d1560a 61 return NULL;
a494bb1c
HH
62 if (ieee80211_has_tods(fc))
63 return hdr->addr1;
64 if (ieee80211_has_fromds(fc))
c2d1560a 65 return hdr->addr2;
a494bb1c
HH
66
67 return hdr->addr3;
68 }
69
70 if (ieee80211_is_mgmt(fc)) {
98f0b0a3
RR
71 if (len < 24) /* drop incorrect hdr len (mgmt) */
72 return NULL;
c2d1560a 73 return hdr->addr3;
a494bb1c
HH
74 }
75
76 if (ieee80211_is_ctl(fc)) {
77 if(ieee80211_is_pspoll(fc))
c2d1560a 78 return hdr->addr1;
a494bb1c
HH
79
80 if (ieee80211_is_back_req(fc)) {
71364716 81 switch (type) {
05c914fe 82 case NL80211_IFTYPE_STATION:
71364716 83 return hdr->addr2;
05c914fe
JB
84 case NL80211_IFTYPE_AP:
85 case NL80211_IFTYPE_AP_VLAN:
71364716
RR
86 return hdr->addr1;
87 default:
a494bb1c 88 break; /* fall through to the return */
71364716
RR
89 }
90 }
c2d1560a
JB
91 }
92
93 return NULL;
94}
95
5cf121c3 96void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
c2d1560a 97{
2de8e0d9
JB
98 struct sk_buff *skb = tx->skb;
99 struct ieee80211_hdr *hdr;
100
101 do {
102 hdr = (struct ieee80211_hdr *) skb->data;
103 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
104 } while ((skb = skb->next));
c2d1560a
JB
105}
106
107int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
108 int rate, int erp, int short_preamble)
109{
110 int dur;
111
112 /* calculate duration (in microseconds, rounded up to next higher
113 * integer if it includes a fractional microsecond) to send frame of
114 * len bytes (does not include FCS) at the given rate. Duration will
115 * also include SIFS.
116 *
117 * rate is in 100 kbps, so divident is multiplied by 10 in the
118 * DIV_ROUND_UP() operations.
119 */
120
8318d78a 121 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
c2d1560a
JB
122 /*
123 * OFDM:
124 *
125 * N_DBPS = DATARATE x 4
126 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
127 * (16 = SIGNAL time, 6 = tail bits)
128 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
129 *
130 * T_SYM = 4 usec
131 * 802.11a - 17.5.2: aSIFSTime = 16 usec
132 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
133 * signal ext = 6 usec
134 */
c2d1560a
JB
135 dur = 16; /* SIFS + signal ext */
136 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
137 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
138 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
139 4 * rate); /* T_SYM x N_SYM */
140 } else {
141 /*
142 * 802.11b or 802.11g with 802.11b compatibility:
143 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
144 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
145 *
146 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
147 * aSIFSTime = 10 usec
148 * aPreambleLength = 144 usec or 72 usec with short preamble
149 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
150 */
151 dur = 10; /* aSIFSTime = 10 usec */
152 dur += short_preamble ? (72 + 24) : (144 + 48);
153
154 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
155 }
156
157 return dur;
158}
159
160/* Exported duration function for driver use */
32bfd35d
JB
161__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
162 struct ieee80211_vif *vif,
8318d78a
JB
163 size_t frame_len,
164 struct ieee80211_rate *rate)
c2d1560a
JB
165{
166 struct ieee80211_local *local = hw_to_local(hw);
25d834e1 167 struct ieee80211_sub_if_data *sdata;
c2d1560a
JB
168 u16 dur;
169 int erp;
25d834e1 170 bool short_preamble = false;
c2d1560a 171
8318d78a 172 erp = 0;
25d834e1
JB
173 if (vif) {
174 sdata = vif_to_sdata(vif);
bda3933a 175 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
176 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
177 erp = rate->flags & IEEE80211_RATE_ERP_G;
178 }
8318d78a
JB
179
180 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
25d834e1 181 short_preamble);
c2d1560a
JB
182
183 return cpu_to_le16(dur);
184}
185EXPORT_SYMBOL(ieee80211_generic_frame_duration);
186
32bfd35d
JB
187__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
188 struct ieee80211_vif *vif, size_t frame_len,
e039fa4a 189 const struct ieee80211_tx_info *frame_txctl)
c2d1560a
JB
190{
191 struct ieee80211_local *local = hw_to_local(hw);
192 struct ieee80211_rate *rate;
25d834e1 193 struct ieee80211_sub_if_data *sdata;
471b3efd 194 bool short_preamble;
c2d1560a
JB
195 int erp;
196 u16 dur;
2e92e6f2
JB
197 struct ieee80211_supported_band *sband;
198
199 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
c2d1560a 200
25d834e1 201 short_preamble = false;
7e9ed188 202
e039fa4a 203 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
8318d78a
JB
204
205 erp = 0;
25d834e1
JB
206 if (vif) {
207 sdata = vif_to_sdata(vif);
bda3933a 208 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
209 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
210 erp = rate->flags & IEEE80211_RATE_ERP_G;
211 }
c2d1560a
JB
212
213 /* CTS duration */
8318d78a 214 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
c2d1560a
JB
215 erp, short_preamble);
216 /* Data frame duration */
8318d78a 217 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
c2d1560a
JB
218 erp, short_preamble);
219 /* ACK duration */
8318d78a 220 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
c2d1560a
JB
221 erp, short_preamble);
222
223 return cpu_to_le16(dur);
224}
225EXPORT_SYMBOL(ieee80211_rts_duration);
226
32bfd35d
JB
227__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
228 struct ieee80211_vif *vif,
c2d1560a 229 size_t frame_len,
e039fa4a 230 const struct ieee80211_tx_info *frame_txctl)
c2d1560a
JB
231{
232 struct ieee80211_local *local = hw_to_local(hw);
233 struct ieee80211_rate *rate;
25d834e1 234 struct ieee80211_sub_if_data *sdata;
471b3efd 235 bool short_preamble;
c2d1560a
JB
236 int erp;
237 u16 dur;
2e92e6f2
JB
238 struct ieee80211_supported_band *sband;
239
240 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
c2d1560a 241
25d834e1 242 short_preamble = false;
7e9ed188 243
e039fa4a 244 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
8318d78a 245 erp = 0;
25d834e1
JB
246 if (vif) {
247 sdata = vif_to_sdata(vif);
bda3933a 248 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
249 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
250 erp = rate->flags & IEEE80211_RATE_ERP_G;
251 }
c2d1560a
JB
252
253 /* Data frame duration */
8318d78a 254 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
c2d1560a 255 erp, short_preamble);
e039fa4a 256 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
c2d1560a 257 /* ACK duration */
8318d78a 258 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
c2d1560a
JB
259 erp, short_preamble);
260 }
261
262 return cpu_to_le16(dur);
263}
264EXPORT_SYMBOL(ieee80211_ctstoself_duration);
265
ce7c9111
KV
266static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
267 enum queue_stop_reason reason)
c2d1560a
JB
268{
269 struct ieee80211_local *local = hw_to_local(hw);
cf0277e7 270 struct ieee80211_sub_if_data *sdata;
c2d1560a 271
b5878a2d
JB
272 trace_wake_queue(local, queue, reason);
273
e4e72fb4
JB
274 if (WARN_ON(queue >= hw->queues))
275 return;
ce7c9111 276
96f5e66e
JB
277 __clear_bit(reason, &local->queue_stop_reasons[queue]);
278
279 if (local->queue_stop_reasons[queue] != 0)
280 /* someone still has this queue stopped */
281 return;
282
7236fe29
JB
283 if (skb_queue_empty(&local->pending[queue])) {
284 rcu_read_lock();
5b714c6a
JB
285 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
286 if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
287 continue;
2337db8d 288 netif_wake_subqueue(sdata->dev, queue);
5b714c6a 289 }
7236fe29
JB
290 rcu_read_unlock();
291 } else
3b8d81e0 292 tasklet_schedule(&local->tx_pending_tasklet);
c2d1560a 293}
ce7c9111 294
96f5e66e
JB
295void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
296 enum queue_stop_reason reason)
ce7c9111
KV
297{
298 struct ieee80211_local *local = hw_to_local(hw);
299 unsigned long flags;
300
301 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
302 __ieee80211_wake_queue(hw, queue, reason);
303 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
304}
305
306void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
307{
308 ieee80211_wake_queue_by_reason(hw, queue,
309 IEEE80211_QUEUE_STOP_REASON_DRIVER);
310}
c2d1560a
JB
311EXPORT_SYMBOL(ieee80211_wake_queue);
312
ce7c9111
KV
313static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
314 enum queue_stop_reason reason)
c2d1560a
JB
315{
316 struct ieee80211_local *local = hw_to_local(hw);
cf0277e7 317 struct ieee80211_sub_if_data *sdata;
c2d1560a 318
b5878a2d
JB
319 trace_stop_queue(local, queue, reason);
320
e4e72fb4
JB
321 if (WARN_ON(queue >= hw->queues))
322 return;
96f5e66e 323
2a577d98 324 __set_bit(reason, &local->queue_stop_reasons[queue]);
cf0277e7
JB
325
326 rcu_read_lock();
327 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2337db8d 328 netif_stop_subqueue(sdata->dev, queue);
cf0277e7 329 rcu_read_unlock();
c2d1560a 330}
ce7c9111 331
96f5e66e
JB
332void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
333 enum queue_stop_reason reason)
ce7c9111
KV
334{
335 struct ieee80211_local *local = hw_to_local(hw);
336 unsigned long flags;
337
338 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
339 __ieee80211_stop_queue(hw, queue, reason);
340 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
341}
342
343void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
344{
345 ieee80211_stop_queue_by_reason(hw, queue,
346 IEEE80211_QUEUE_STOP_REASON_DRIVER);
347}
c2d1560a
JB
348EXPORT_SYMBOL(ieee80211_stop_queue);
349
8f77f384
JB
350void ieee80211_add_pending_skb(struct ieee80211_local *local,
351 struct sk_buff *skb)
352{
353 struct ieee80211_hw *hw = &local->hw;
354 unsigned long flags;
355 int queue = skb_get_queue_mapping(skb);
a7bc376c
JB
356 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
357
358 if (WARN_ON(!info->control.vif)) {
0819663d 359 kfree_skb(skb);
a7bc376c
JB
360 return;
361 }
8f77f384
JB
362
363 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
364 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
3b8d81e0 365 __skb_queue_tail(&local->pending[queue], skb);
8f77f384
JB
366 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
367 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
368}
369
50a9432d
JB
370int ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
371 struct sk_buff_head *skbs,
372 void (*fn)(void *data), void *data)
8f77f384
JB
373{
374 struct ieee80211_hw *hw = &local->hw;
375 struct sk_buff *skb;
376 unsigned long flags;
377 int queue, ret = 0, i;
378
379 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
380 for (i = 0; i < hw->queues; i++)
381 __ieee80211_stop_queue(hw, i,
382 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
383
384 while ((skb = skb_dequeue(skbs))) {
a7bc376c
JB
385 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
386
387 if (WARN_ON(!info->control.vif)) {
0819663d 388 kfree_skb(skb);
a7bc376c
JB
389 continue;
390 }
391
8f77f384
JB
392 ret++;
393 queue = skb_get_queue_mapping(skb);
3b8d81e0 394 __skb_queue_tail(&local->pending[queue], skb);
8f77f384
JB
395 }
396
50a9432d
JB
397 if (fn)
398 fn(data);
399
3b8d81e0 400 for (i = 0; i < hw->queues; i++)
8f77f384
JB
401 __ieee80211_wake_queue(hw, i,
402 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
8f77f384
JB
403 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
404
405 return ret;
406}
407
50a9432d
JB
408int ieee80211_add_pending_skbs(struct ieee80211_local *local,
409 struct sk_buff_head *skbs)
410{
411 return ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
412}
413
ce7c9111
KV
414void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
415 enum queue_stop_reason reason)
c2d1560a 416{
ce7c9111
KV
417 struct ieee80211_local *local = hw_to_local(hw);
418 unsigned long flags;
c2d1560a
JB
419 int i;
420
ce7c9111
KV
421 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
422
96f5e66e 423 for (i = 0; i < hw->queues; i++)
ce7c9111
KV
424 __ieee80211_stop_queue(hw, i, reason);
425
426 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
427}
428
429void ieee80211_stop_queues(struct ieee80211_hw *hw)
430{
431 ieee80211_stop_queues_by_reason(hw,
432 IEEE80211_QUEUE_STOP_REASON_DRIVER);
c2d1560a
JB
433}
434EXPORT_SYMBOL(ieee80211_stop_queues);
435
92ab8535
TW
436int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
437{
438 struct ieee80211_local *local = hw_to_local(hw);
3b8d81e0
JB
439 unsigned long flags;
440 int ret;
96f5e66e 441
e4e72fb4
JB
442 if (WARN_ON(queue >= hw->queues))
443 return true;
96f5e66e 444
3b8d81e0
JB
445 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
446 ret = !!local->queue_stop_reasons[queue];
447 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
448 return ret;
92ab8535
TW
449}
450EXPORT_SYMBOL(ieee80211_queue_stopped);
451
ce7c9111
KV
452void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
453 enum queue_stop_reason reason)
c2d1560a 454{
ce7c9111
KV
455 struct ieee80211_local *local = hw_to_local(hw);
456 unsigned long flags;
c2d1560a
JB
457 int i;
458
ce7c9111
KV
459 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
460
e4e72fb4 461 for (i = 0; i < hw->queues; i++)
ce7c9111
KV
462 __ieee80211_wake_queue(hw, i, reason);
463
464 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
465}
466
467void ieee80211_wake_queues(struct ieee80211_hw *hw)
468{
469 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
c2d1560a
JB
470}
471EXPORT_SYMBOL(ieee80211_wake_queues);
dabeb344 472
32bfd35d
JB
473void ieee80211_iterate_active_interfaces(
474 struct ieee80211_hw *hw,
475 void (*iterator)(void *data, u8 *mac,
476 struct ieee80211_vif *vif),
477 void *data)
dabeb344
JB
478{
479 struct ieee80211_local *local = hw_to_local(hw);
480 struct ieee80211_sub_if_data *sdata;
481
c771c9d8 482 mutex_lock(&local->iflist_mtx);
2f561feb
ID
483
484 list_for_each_entry(sdata, &local->interfaces, list) {
485 switch (sdata->vif.type) {
05c914fe
JB
486 case NL80211_IFTYPE_MONITOR:
487 case NL80211_IFTYPE_AP_VLAN:
2f561feb 488 continue;
2ca27bcf 489 default:
2f561feb
ID
490 break;
491 }
9607e6b6 492 if (ieee80211_sdata_running(sdata))
47846c9b 493 iterator(data, sdata->vif.addr,
2f561feb
ID
494 &sdata->vif);
495 }
496
c771c9d8 497 mutex_unlock(&local->iflist_mtx);
2f561feb
ID
498}
499EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
500
501void ieee80211_iterate_active_interfaces_atomic(
502 struct ieee80211_hw *hw,
503 void (*iterator)(void *data, u8 *mac,
504 struct ieee80211_vif *vif),
505 void *data)
506{
507 struct ieee80211_local *local = hw_to_local(hw);
508 struct ieee80211_sub_if_data *sdata;
509
e38bad47 510 rcu_read_lock();
dabeb344 511
e38bad47 512 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
51fb61e7 513 switch (sdata->vif.type) {
05c914fe
JB
514 case NL80211_IFTYPE_MONITOR:
515 case NL80211_IFTYPE_AP_VLAN:
dabeb344 516 continue;
2ca27bcf 517 default:
dabeb344
JB
518 break;
519 }
9607e6b6 520 if (ieee80211_sdata_running(sdata))
47846c9b 521 iterator(data, sdata->vif.addr,
32bfd35d 522 &sdata->vif);
dabeb344 523 }
e38bad47
JB
524
525 rcu_read_unlock();
dabeb344 526}
2f561feb 527EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
37ffc8da 528
42935eca
LR
529/*
530 * Nothing should have been stuffed into the workqueue during
531 * the suspend->resume cycle. If this WARN is seen then there
532 * is a bug with either the driver suspend or something in
533 * mac80211 stuffing into the workqueue which we haven't yet
534 * cleared during mac80211's suspend cycle.
535 */
536static bool ieee80211_can_queue_work(struct ieee80211_local *local)
537{
ceb99fe0
JB
538 if (WARN(local->suspended && !local->resuming,
539 "queueing ieee80211 work while going to suspend\n"))
540 return false;
42935eca
LR
541
542 return true;
543}
544
545void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
546{
547 struct ieee80211_local *local = hw_to_local(hw);
548
549 if (!ieee80211_can_queue_work(local))
550 return;
551
552 queue_work(local->workqueue, work);
553}
554EXPORT_SYMBOL(ieee80211_queue_work);
555
556void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
557 struct delayed_work *dwork,
558 unsigned long delay)
559{
560 struct ieee80211_local *local = hw_to_local(hw);
561
562 if (!ieee80211_can_queue_work(local))
563 return;
564
565 queue_delayed_work(local->workqueue, dwork, delay);
566}
567EXPORT_SYMBOL(ieee80211_queue_delayed_work);
568
37ffc8da
JB
569void ieee802_11_parse_elems(u8 *start, size_t len,
570 struct ieee802_11_elems *elems)
d91f36db
JB
571{
572 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
573}
574
5825fe10
JB
575void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
576{
577 struct ieee80211_local *local = sdata->local;
578 struct ieee80211_tx_queue_params qparam;
aa837e1d
JB
579 int queue;
580 bool use_11b;
581 int aCWmin, aCWmax;
5825fe10
JB
582
583 if (!local->ops->conf_tx)
584 return;
585
586 memset(&qparam, 0, sizeof(qparam));
587
aa837e1d
JB
588 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
589 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
5825fe10 590
aa837e1d
JB
591 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
592 /* Set defaults according to 802.11-2007 Table 7-37 */
593 aCWmax = 1023;
594 if (use_11b)
595 aCWmin = 31;
596 else
597 aCWmin = 15;
598
599 switch (queue) {
600 case 3: /* AC_BK */
7ba10a8e
JB
601 qparam.cw_max = aCWmax;
602 qparam.cw_min = aCWmin;
aa837e1d
JB
603 qparam.txop = 0;
604 qparam.aifs = 7;
605 break;
606 default: /* never happens but let's not leave undefined */
607 case 2: /* AC_BE */
7ba10a8e
JB
608 qparam.cw_max = aCWmax;
609 qparam.cw_min = aCWmin;
aa837e1d
JB
610 qparam.txop = 0;
611 qparam.aifs = 3;
612 break;
613 case 1: /* AC_VI */
614 qparam.cw_max = aCWmin;
615 qparam.cw_min = (aCWmin + 1) / 2 - 1;
616 if (use_11b)
617 qparam.txop = 6016/32;
618 else
619 qparam.txop = 3008/32;
620 qparam.aifs = 2;
621 break;
622 case 0: /* AC_VO */
623 qparam.cw_max = (aCWmin + 1) / 2 - 1;
624 qparam.cw_min = (aCWmin + 1) / 4 - 1;
625 if (use_11b)
626 qparam.txop = 3264/32;
627 else
628 qparam.txop = 1504/32;
629 qparam.aifs = 2;
630 break;
631 }
5825fe10 632
ab13315a
KV
633 qparam.uapsd = false;
634
2683d65b 635 local->tx_conf[queue] = qparam;
aa837e1d
JB
636 drv_conf_tx(local, queue, &qparam);
637 }
e1b3ec1a
SG
638
639 /* after reinitialize QoS TX queues setting to default,
640 * disable QoS at all */
d9734979
S
641
642 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
643 sdata->vif.bss_conf.qos =
644 sdata->vif.type != NL80211_IFTYPE_STATION;
645 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
646 }
5825fe10 647}
e50db65c 648
46900298
JB
649void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
650 const size_t supp_rates_len,
651 const u8 *supp_rates)
652{
653 struct ieee80211_local *local = sdata->local;
654 int i, have_higher_than_11mbit = 0;
655
656 /* cf. IEEE 802.11 9.2.12 */
657 for (i = 0; i < supp_rates_len; i++)
658 if ((supp_rates[i] & 0x7f) * 5 > 110)
659 have_higher_than_11mbit = 1;
660
661 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
662 have_higher_than_11mbit)
663 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
664 else
665 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
666
667 ieee80211_set_wmm_default(sdata);
668}
669
881d948c 670u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
96dd22ac
JB
671 enum ieee80211_band band)
672{
673 struct ieee80211_supported_band *sband;
674 struct ieee80211_rate *bitrates;
881d948c 675 u32 mandatory_rates;
96dd22ac
JB
676 enum ieee80211_rate_flags mandatory_flag;
677 int i;
678
679 sband = local->hw.wiphy->bands[band];
680 if (!sband) {
681 WARN_ON(1);
682 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
683 }
684
685 if (band == IEEE80211_BAND_2GHZ)
686 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
687 else
688 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
689
690 bitrates = sband->bitrates;
691 mandatory_rates = 0;
692 for (i = 0; i < sband->n_bitrates; i++)
693 if (bitrates[i].flags & mandatory_flag)
694 mandatory_rates |= BIT(i);
695 return mandatory_rates;
696}
46900298
JB
697
698void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
699 u16 transaction, u16 auth_alg,
fffd0934
JB
700 u8 *extra, size_t extra_len, const u8 *bssid,
701 const u8 *key, u8 key_len, u8 key_idx)
46900298
JB
702{
703 struct ieee80211_local *local = sdata->local;
704 struct sk_buff *skb;
705 struct ieee80211_mgmt *mgmt;
fffd0934 706 int err;
46900298
JB
707
708 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
65fc73ac 709 sizeof(*mgmt) + 6 + extra_len);
d15b8459 710 if (!skb)
46900298 711 return;
d15b8459 712
46900298
JB
713 skb_reserve(skb, local->hw.extra_tx_headroom);
714
715 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
716 memset(mgmt, 0, 24 + 6);
717 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
718 IEEE80211_STYPE_AUTH);
46900298 719 memcpy(mgmt->da, bssid, ETH_ALEN);
47846c9b 720 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298
JB
721 memcpy(mgmt->bssid, bssid, ETH_ALEN);
722 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
723 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
724 mgmt->u.auth.status_code = cpu_to_le16(0);
725 if (extra)
726 memcpy(skb_put(skb, extra_len), extra, extra_len);
46900298 727
fffd0934
JB
728 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
729 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
730 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
731 WARN_ON(err);
732 }
733
62ae67be
JB
734 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
735 ieee80211_tx_skb(sdata, skb);
46900298
JB
736}
737
de95a54b 738int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
4d36ec58 739 const u8 *ie, size_t ie_len,
651b5225
JM
740 enum ieee80211_band band, u32 rate_mask,
741 u8 channel)
de95a54b
JB
742{
743 struct ieee80211_supported_band *sband;
8e664fb3
JB
744 u8 *pos;
745 size_t offset = 0, noffset;
746 int supp_rates_len, i;
8dcb2003
JM
747 u8 rates[32];
748 int num_rates;
749 int ext_rates_len;
de95a54b 750
4d36ec58 751 sband = local->hw.wiphy->bands[band];
de95a54b
JB
752
753 pos = buffer;
754
8dcb2003
JM
755 num_rates = 0;
756 for (i = 0; i < sband->n_bitrates; i++) {
757 if ((BIT(i) & rate_mask) == 0)
758 continue; /* skip rate */
759 rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
760 }
761
762 supp_rates_len = min_t(int, num_rates, 8);
8e664fb3 763
de95a54b 764 *pos++ = WLAN_EID_SUPP_RATES;
8e664fb3 765 *pos++ = supp_rates_len;
8dcb2003
JM
766 memcpy(pos, rates, supp_rates_len);
767 pos += supp_rates_len;
8e664fb3
JB
768
769 /* insert "request information" if in custom IEs */
770 if (ie && ie_len) {
771 static const u8 before_extrates[] = {
772 WLAN_EID_SSID,
773 WLAN_EID_SUPP_RATES,
774 WLAN_EID_REQUEST,
775 };
776 noffset = ieee80211_ie_split(ie, ie_len,
777 before_extrates,
778 ARRAY_SIZE(before_extrates),
779 offset);
780 memcpy(pos, ie + offset, noffset - offset);
781 pos += noffset - offset;
782 offset = noffset;
783 }
784
8dcb2003
JM
785 ext_rates_len = num_rates - supp_rates_len;
786 if (ext_rates_len > 0) {
8e664fb3 787 *pos++ = WLAN_EID_EXT_SUPP_RATES;
8dcb2003
JM
788 *pos++ = ext_rates_len;
789 memcpy(pos, rates + supp_rates_len, ext_rates_len);
790 pos += ext_rates_len;
8e664fb3
JB
791 }
792
651b5225
JM
793 if (channel && sband->band == IEEE80211_BAND_2GHZ) {
794 *pos++ = WLAN_EID_DS_PARAMS;
795 *pos++ = 1;
796 *pos++ = channel;
797 }
798
8e664fb3
JB
799 /* insert custom IEs that go before HT */
800 if (ie && ie_len) {
801 static const u8 before_ht[] = {
802 WLAN_EID_SSID,
803 WLAN_EID_SUPP_RATES,
804 WLAN_EID_REQUEST,
805 WLAN_EID_EXT_SUPP_RATES,
806 WLAN_EID_DS_PARAMS,
807 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
808 };
809 noffset = ieee80211_ie_split(ie, ie_len,
810 before_ht, ARRAY_SIZE(before_ht),
811 offset);
812 memcpy(pos, ie + offset, noffset - offset);
813 pos += noffset - offset;
814 offset = noffset;
de95a54b
JB
815 }
816
5ef2d41a 817 if (sband->ht_cap.ht_supported) {
f38fd12f
JB
818 u16 cap = sband->ht_cap.cap;
819 __le16 tmp;
820
5ef2d41a
JB
821 *pos++ = WLAN_EID_HT_CAPABILITY;
822 *pos++ = sizeof(struct ieee80211_ht_cap);
823 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
f38fd12f 824 tmp = cpu_to_le16(cap);
5ef2d41a
JB
825 memcpy(pos, &tmp, sizeof(u16));
826 pos += sizeof(u16);
5ef2d41a 827 *pos++ = sband->ht_cap.ampdu_factor |
f38fd12f
JB
828 (sband->ht_cap.ampdu_density <<
829 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
5ef2d41a
JB
830 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
831 pos += sizeof(sband->ht_cap.mcs);
832 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
833 }
834
de95a54b
JB
835 /*
836 * If adding more here, adjust code in main.c
837 * that calculates local->scan_ies_len.
838 */
839
8e664fb3
JB
840 /* add any remaining custom IEs */
841 if (ie && ie_len) {
842 noffset = ie_len;
843 memcpy(pos, ie + offset, noffset - offset);
844 pos += noffset - offset;
de95a54b
JB
845 }
846
847 return pos - buffer;
848}
849
a619a4c0 850struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
85a237fe 851 u8 *dst, u32 ratemask,
a619a4c0 852 const u8 *ssid, size_t ssid_len,
a806c558
PS
853 const u8 *ie, size_t ie_len,
854 bool directed)
46900298
JB
855{
856 struct ieee80211_local *local = sdata->local;
46900298
JB
857 struct sk_buff *skb;
858 struct ieee80211_mgmt *mgmt;
7c12ce8b
KV
859 size_t buf_len;
860 u8 *buf;
651b5225 861 u8 chan;
7c12ce8b
KV
862
863 /* FIXME: come up with a proper value */
864 buf = kmalloc(200 + ie_len, GFP_KERNEL);
d15b8459 865 if (!buf)
a619a4c0 866 return NULL;
46900298 867
a806c558
PS
868 /*
869 * Do not send DS Channel parameter for directed probe requests
870 * in order to maximize the chance that we get a response. Some
871 * badly-behaved APs don't respond when this parameter is included.
872 */
873 if (directed)
874 chan = 0;
875 else
876 chan = ieee80211_frequency_to_channel(
877 local->hw.conf.channel->center_freq);
651b5225 878
7c12ce8b 879 buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
8dcb2003 880 local->hw.conf.channel->band,
85a237fe 881 ratemask, chan);
7c12ce8b
KV
882
883 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
884 ssid, ssid_len,
885 buf, buf_len);
886
46900298 887 if (dst) {
7c12ce8b 888 mgmt = (struct ieee80211_mgmt *) skb->data;
46900298
JB
889 memcpy(mgmt->da, dst, ETH_ALEN);
890 memcpy(mgmt->bssid, dst, ETH_ALEN);
46900298 891 }
46900298 892
62ae67be 893 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
145b6d1a 894 kfree(buf);
a619a4c0
JO
895
896 return skb;
897}
898
899void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
900 const u8 *ssid, size_t ssid_len,
a806c558 901 const u8 *ie, size_t ie_len,
85a237fe 902 u32 ratemask, bool directed)
a619a4c0
JO
903{
904 struct sk_buff *skb;
905
85a237fe
JB
906 skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
907 ie, ie_len, directed);
a619a4c0
JO
908 if (skb)
909 ieee80211_tx_skb(sdata, skb);
46900298
JB
910}
911
912u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
913 struct ieee802_11_elems *elems,
914 enum ieee80211_band band)
915{
916 struct ieee80211_supported_band *sband;
917 struct ieee80211_rate *bitrates;
918 size_t num_rates;
919 u32 supp_rates;
920 int i, j;
921 sband = local->hw.wiphy->bands[band];
922
923 if (!sband) {
924 WARN_ON(1);
925 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
926 }
927
928 bitrates = sband->bitrates;
929 num_rates = sband->n_bitrates;
930 supp_rates = 0;
931 for (i = 0; i < elems->supp_rates_len +
932 elems->ext_supp_rates_len; i++) {
933 u8 rate = 0;
934 int own_rate;
935 if (i < elems->supp_rates_len)
936 rate = elems->supp_rates[i];
937 else if (elems->ext_supp_rates)
938 rate = elems->ext_supp_rates
939 [i - elems->supp_rates_len];
940 own_rate = 5 * (rate & 0x7f);
941 for (j = 0; j < num_rates; j++)
942 if (bitrates[j].bitrate == own_rate)
943 supp_rates |= BIT(j);
944 }
945 return supp_rates;
946}
f2753ddb 947
84f6a01c
JB
948void ieee80211_stop_device(struct ieee80211_local *local)
949{
950 ieee80211_led_radio(local, false);
67408c8c 951 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
84f6a01c
JB
952
953 cancel_work_sync(&local->reconfig_filter);
84f6a01c
JB
954
955 flush_workqueue(local->workqueue);
678f415f 956 drv_stop(local);
84f6a01c
JB
957}
958
f2753ddb
JB
959int ieee80211_reconfig(struct ieee80211_local *local)
960{
961 struct ieee80211_hw *hw = &local->hw;
962 struct ieee80211_sub_if_data *sdata;
f2753ddb 963 struct sta_info *sta;
2683d65b 964 int res, i;
5bb644a0 965
eecc4800 966#ifdef CONFIG_PM
ceb99fe0
JB
967 if (local->suspended)
968 local->resuming = true;
f2753ddb 969
eecc4800
JB
970 if (local->wowlan) {
971 local->wowlan = false;
972 res = drv_resume(local);
973 if (res < 0) {
974 local->resuming = false;
975 return res;
976 }
977 if (res == 0)
978 goto wake_up;
979 WARN_ON(res > 1);
980 /*
981 * res is 1, which means the driver requested
982 * to go through a regular reset on wakeup.
983 */
984 }
985#endif
986
94f9b97b
JB
987 /* setup fragmentation threshold */
988 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
989
990 /* setup RTS threshold */
991 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
992
993 /* reset coverage class */
994 drv_set_coverage_class(local, hw->wiphy->coverage_class);
995
996 /* everything else happens only if HW was up & running */
997 if (!local->open_count)
998 goto wake_up;
f2753ddb 999
94f9b97b
JB
1000 /*
1001 * Upon resume hardware can sometimes be goofy due to
1002 * various platform / driver / bus issues, so restarting
1003 * the device may at times not work immediately. Propagate
1004 * the error.
1005 */
1006 res = drv_start(local);
1007 if (res) {
1008 WARN(local->suspended, "Hardware became unavailable "
1009 "upon resume. This could be a software issue "
1010 "prior to suspend or a hardware issue.\n");
1011 return res;
f2753ddb
JB
1012 }
1013
94f9b97b
JB
1014 ieee80211_led_radio(local, true);
1015 ieee80211_mod_tpt_led_trig(local,
1016 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1017
f2753ddb
JB
1018 /* add interfaces */
1019 list_for_each_entry(sdata, &local->interfaces, list) {
1020 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1021 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1ed32e4f
JB
1022 ieee80211_sdata_running(sdata))
1023 res = drv_add_interface(local, &sdata->vif);
f2753ddb
JB
1024 }
1025
1026 /* add STAs back */
34e89507
JB
1027 mutex_lock(&local->sta_mtx);
1028 list_for_each_entry(sta, &local->sta_list, list) {
1029 if (sta->uploaded) {
5bb644a0 1030 sdata = sta->sdata;
f2753ddb
JB
1031 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1032 sdata = container_of(sdata->bss,
1033 struct ieee80211_sub_if_data,
1034 u.ap);
1035
f785d83a 1036 memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
34e89507 1037 WARN_ON(drv_sta_add(local, sdata, &sta->sta));
f2753ddb 1038 }
f2753ddb 1039 }
34e89507 1040 mutex_unlock(&local->sta_mtx);
f2753ddb 1041
2683d65b
EP
1042 /* reconfigure tx conf */
1043 for (i = 0; i < hw->queues; i++)
1044 drv_conf_tx(local, i, &local->tx_conf[i]);
1045
f2753ddb
JB
1046 /* reconfigure hardware */
1047 ieee80211_hw_config(local, ~0);
1048
f2753ddb 1049 ieee80211_configure_filter(local);
f2753ddb
JB
1050
1051 /* Finally also reconfigure all the BSS information */
1052 list_for_each_entry(sdata, &local->interfaces, list) {
ac8dd506
JB
1053 u32 changed;
1054
9607e6b6 1055 if (!ieee80211_sdata_running(sdata))
f2753ddb 1056 continue;
ac8dd506
JB
1057
1058 /* common change flags for all interface types */
1059 changed = BSS_CHANGED_ERP_CTS_PROT |
1060 BSS_CHANGED_ERP_PREAMBLE |
1061 BSS_CHANGED_ERP_SLOT |
1062 BSS_CHANGED_HT |
1063 BSS_CHANGED_BASIC_RATES |
1064 BSS_CHANGED_BEACON_INT |
1065 BSS_CHANGED_BSSID |
4ced3f74
JB
1066 BSS_CHANGED_CQM |
1067 BSS_CHANGED_QOS;
ac8dd506 1068
f2753ddb
JB
1069 switch (sdata->vif.type) {
1070 case NL80211_IFTYPE_STATION:
ac8dd506 1071 changed |= BSS_CHANGED_ASSOC;
a7b545f7 1072 mutex_lock(&sdata->u.mgd.mtx);
ac8dd506 1073 ieee80211_bss_info_change_notify(sdata, changed);
a7b545f7 1074 mutex_unlock(&sdata->u.mgd.mtx);
ac8dd506 1075 break;
f2753ddb 1076 case NL80211_IFTYPE_ADHOC:
ac8dd506
JB
1077 changed |= BSS_CHANGED_IBSS;
1078 /* fall through */
f2753ddb
JB
1079 case NL80211_IFTYPE_AP:
1080 case NL80211_IFTYPE_MESH_POINT:
ac8dd506
JB
1081 changed |= BSS_CHANGED_BEACON |
1082 BSS_CHANGED_BEACON_ENABLED;
2d0ddec5 1083 ieee80211_bss_info_change_notify(sdata, changed);
f2753ddb
JB
1084 break;
1085 case NL80211_IFTYPE_WDS:
1086 break;
1087 case NL80211_IFTYPE_AP_VLAN:
1088 case NL80211_IFTYPE_MONITOR:
1089 /* ignore virtual */
1090 break;
1091 case NL80211_IFTYPE_UNSPECIFIED:
2e161f78 1092 case NUM_NL80211_IFTYPES:
2ca27bcf
JB
1093 case NL80211_IFTYPE_P2P_CLIENT:
1094 case NL80211_IFTYPE_P2P_GO:
f2753ddb
JB
1095 WARN_ON(1);
1096 break;
1097 }
1098 }
1099
2a419056
JB
1100 /*
1101 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1102 * sessions can be established after a resume.
1103 *
1104 * Also tear down aggregation sessions since reconfiguring
1105 * them in a hardware restart scenario is not easily done
1106 * right now, and the hardware will have lost information
1107 * about the sessions, but we and the AP still think they
1108 * are active. This is really a workaround though.
1109 */
74e2bd1f 1110 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
2a419056
JB
1111 mutex_lock(&local->sta_mtx);
1112
1113 list_for_each_entry(sta, &local->sta_list, list) {
53f73c09 1114 ieee80211_sta_tear_down_BA_sessions(sta, true);
2a419056 1115 clear_sta_flags(sta, WLAN_STA_BLOCK_BA);
74e2bd1f 1116 }
2a419056
JB
1117
1118 mutex_unlock(&local->sta_mtx);
74e2bd1f 1119 }
74e2bd1f 1120
f2753ddb
JB
1121 /* add back keys */
1122 list_for_each_entry(sdata, &local->interfaces, list)
9607e6b6 1123 if (ieee80211_sdata_running(sdata))
f2753ddb
JB
1124 ieee80211_enable_keys(sdata);
1125
eecc4800 1126 wake_up:
f2753ddb
JB
1127 ieee80211_wake_queues_by_reason(hw,
1128 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1129
5bb644a0
JB
1130 /*
1131 * If this is for hw restart things are still running.
1132 * We may want to change that later, however.
1133 */
ceb99fe0 1134 if (!local->suspended)
5bb644a0
JB
1135 return 0;
1136
1137#ifdef CONFIG_PM
ceb99fe0 1138 /* first set suspended false, then resuming */
5bb644a0 1139 local->suspended = false;
ceb99fe0
JB
1140 mb();
1141 local->resuming = false;
5bb644a0
JB
1142
1143 list_for_each_entry(sdata, &local->interfaces, list) {
1144 switch(sdata->vif.type) {
1145 case NL80211_IFTYPE_STATION:
1146 ieee80211_sta_restart(sdata);
1147 break;
1148 case NL80211_IFTYPE_ADHOC:
1149 ieee80211_ibss_restart(sdata);
1150 break;
1151 case NL80211_IFTYPE_MESH_POINT:
1152 ieee80211_mesh_restart(sdata);
1153 break;
1154 default:
1155 break;
1156 }
1157 }
1158
26d59535 1159 mod_timer(&local->sta_cleanup, jiffies + 1);
5bb644a0 1160
34e89507 1161 mutex_lock(&local->sta_mtx);
5bb644a0
JB
1162 list_for_each_entry(sta, &local->sta_list, list)
1163 mesh_plink_restart(sta);
34e89507 1164 mutex_unlock(&local->sta_mtx);
5bb644a0
JB
1165#else
1166 WARN_ON(1);
1167#endif
f2753ddb
JB
1168 return 0;
1169}
42935eca 1170
95acac61
JB
1171void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1172{
1173 struct ieee80211_sub_if_data *sdata;
1174 struct ieee80211_local *local;
1175 struct ieee80211_key *key;
1176
1177 if (WARN_ON(!vif))
1178 return;
1179
1180 sdata = vif_to_sdata(vif);
1181 local = sdata->local;
1182
1183 if (WARN_ON(!local->resuming))
1184 return;
1185
1186 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1187 return;
1188
1189 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1190
1191 mutex_lock(&local->key_mtx);
1192 list_for_each_entry(key, &sdata->key_list, list)
1193 key->flags |= KEY_FLAG_TAINTED;
1194 mutex_unlock(&local->key_mtx);
1195}
1196EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1197
0f78231b
JB
1198static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1199 enum ieee80211_smps_mode *smps_mode)
1200{
1201 if (ifmgd->associated) {
1202 *smps_mode = ifmgd->ap_smps;
1203
1204 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1205 if (ifmgd->powersave)
1206 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1207 else
1208 *smps_mode = IEEE80211_SMPS_OFF;
1209 }
1210
1211 return 1;
1212 }
1213
1214 return 0;
1215}
1216
1217/* must hold iflist_mtx */
025e6be2 1218void ieee80211_recalc_smps(struct ieee80211_local *local)
0f78231b
JB
1219{
1220 struct ieee80211_sub_if_data *sdata;
1221 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1222 int count = 0;
1223
46a5ebaf 1224 lockdep_assert_held(&local->iflist_mtx);
0f78231b
JB
1225
1226 /*
1227 * This function could be improved to handle multiple
1228 * interfaces better, but right now it makes any
1229 * non-station interfaces force SM PS to be turned
1230 * off. If there are multiple station interfaces it
1231 * could also use the best possible mode, e.g. if
1232 * one is in static and the other in dynamic then
1233 * dynamic is ok.
1234 */
1235
1236 list_for_each_entry(sdata, &local->interfaces, list) {
26a58456 1237 if (!ieee80211_sdata_running(sdata))
0f78231b
JB
1238 continue;
1239 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1240 goto set;
025e6be2
JB
1241
1242 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
0f78231b
JB
1243
1244 if (count > 1) {
1245 smps_mode = IEEE80211_SMPS_OFF;
1246 break;
1247 }
1248 }
1249
1250 if (smps_mode == local->smps_mode)
1251 return;
1252
1253 set:
1254 local->smps_mode = smps_mode;
1255 /* changed flag is auto-detected for this */
1256 ieee80211_hw_config(local, 0);
1257}
8e664fb3
JB
1258
1259static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1260{
1261 int i;
1262
1263 for (i = 0; i < n_ids; i++)
1264 if (ids[i] == id)
1265 return true;
1266 return false;
1267}
1268
1269/**
1270 * ieee80211_ie_split - split an IE buffer according to ordering
1271 *
1272 * @ies: the IE buffer
1273 * @ielen: the length of the IE buffer
1274 * @ids: an array with element IDs that are allowed before
1275 * the split
1276 * @n_ids: the size of the element ID array
1277 * @offset: offset where to start splitting in the buffer
1278 *
1279 * This function splits an IE buffer by updating the @offset
1280 * variable to point to the location where the buffer should be
1281 * split.
1282 *
1283 * It assumes that the given IE buffer is well-formed, this
1284 * has to be guaranteed by the caller!
1285 *
1286 * It also assumes that the IEs in the buffer are ordered
1287 * correctly, if not the result of using this function will not
1288 * be ordered correctly either, i.e. it does no reordering.
1289 *
1290 * The function returns the offset where the next part of the
1291 * buffer starts, which may be @ielen if the entire (remainder)
1292 * of the buffer should be used.
1293 */
1294size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1295 const u8 *ids, int n_ids, size_t offset)
1296{
1297 size_t pos = offset;
1298
1299 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1300 pos += 2 + ies[pos + 1];
1301
1302 return pos;
1303}
1304
1305size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1306{
1307 size_t pos = offset;
1308
1309 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1310 pos += 2 + ies[pos + 1];
1311
1312 return pos;
1313}
615f7b9b
MV
1314
1315static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
1316 int rssi_min_thold,
1317 int rssi_max_thold)
1318{
1319 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
1320
1321 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1322 return;
1323
1324 /*
1325 * Scale up threshold values before storing it, as the RSSI averaging
1326 * algorithm uses a scaled up value as well. Change this scaling
1327 * factor if the RSSI averaging algorithm changes.
1328 */
1329 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
1330 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
1331}
1332
1333void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
1334 int rssi_min_thold,
1335 int rssi_max_thold)
1336{
1337 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1338
1339 WARN_ON(rssi_min_thold == rssi_max_thold ||
1340 rssi_min_thold > rssi_max_thold);
1341
1342 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
1343 rssi_max_thold);
1344}
1345EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
1346
1347void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
1348{
1349 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1350
1351 _ieee80211_enable_rssi_reports(sdata, 0, 0);
1352}
1353EXPORT_SYMBOL(ieee80211_disable_rssi_reports);