]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/net/wireless/brcm80211/brcmfmac/wl_cfg80211.c
brcmfmac: call brcmf_cfg80211_detach() after removal of interfaces
[mirror_ubuntu-hirsute-kernel.git] / drivers / net / wireless / brcm80211 / brcmfmac / wl_cfg80211.c
CommitLineData
5b435de0
AS
1/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */
18
19#include <linux/kernel.h>
5b435de0 20#include <linux/etherdevice.h>
5b435de0 21#include <net/cfg80211.h>
cbaa177d 22#include <net/netlink.h>
5b435de0
AS
23
24#include <brcmu_utils.h>
25#include <defs.h>
26#include <brcmu_wifi.h>
27#include "dhd.h"
16886735 28#include "dhd_dbg.h"
40c1c249 29#include "tracepoint.h"
7a5c1f64 30#include "fwil_types.h"
9f440b7b 31#include "p2p.h"
61730d4d 32#include "btcoex.h"
5b435de0 33#include "wl_cfg80211.h"
81f5dcb8 34#include "fwil.h"
5b435de0 35
e5806072
AS
36#define BRCMF_SCAN_IE_LEN_MAX 2048
37#define BRCMF_PNO_VERSION 2
38#define BRCMF_PNO_TIME 30
39#define BRCMF_PNO_REPEAT 4
40#define BRCMF_PNO_FREQ_EXPO_MAX 3
41#define BRCMF_PNO_MAX_PFN_COUNT 16
42#define BRCMF_PNO_ENABLE_ADAPTSCAN_BIT 6
43#define BRCMF_PNO_HIDDEN_BIT 2
44#define BRCMF_PNO_WPA_AUTH_ANY 0xFFFFFFFF
45#define BRCMF_PNO_SCAN_COMPLETE 1
46#define BRCMF_PNO_SCAN_INCOMPLETE 0
47
9f440b7b 48#define BRCMF_IFACE_MAX_CNT 3
3eacf866 49
1a873342
HM
50#define WPA_OUI "\x00\x50\xF2" /* WPA OUI */
51#define WPA_OUI_TYPE 1
52#define RSN_OUI "\x00\x0F\xAC" /* RSN OUI */
53#define WME_OUI_TYPE 2
89286dc9 54#define WPS_OUI_TYPE 4
1a873342
HM
55
56#define VS_IE_FIXED_HDR_LEN 6
57#define WPA_IE_VERSION_LEN 2
58#define WPA_IE_MIN_OUI_LEN 4
59#define WPA_IE_SUITE_COUNT_LEN 2
60
61#define WPA_CIPHER_NONE 0 /* None */
62#define WPA_CIPHER_WEP_40 1 /* WEP (40-bit) */
63#define WPA_CIPHER_TKIP 2 /* TKIP: default for WPA */
64#define WPA_CIPHER_AES_CCM 4 /* AES (CCM) */
65#define WPA_CIPHER_WEP_104 5 /* WEP (104-bit) */
66
67#define RSN_AKM_NONE 0 /* None (IBSS) */
68#define RSN_AKM_UNSPECIFIED 1 /* Over 802.1x */
69#define RSN_AKM_PSK 2 /* Pre-shared Key */
70#define RSN_CAP_LEN 2 /* Length of RSN capabilities */
71#define RSN_CAP_PTK_REPLAY_CNTR_MASK 0x000C
72
73#define VNDR_IE_CMD_LEN 4 /* length of the set command
74 * string :"add", "del" (+ NUL)
75 */
76#define VNDR_IE_COUNT_OFFSET 4
77#define VNDR_IE_PKTFLAG_OFFSET 8
78#define VNDR_IE_VSIE_OFFSET 12
79#define VNDR_IE_HDR_SIZE 12
9f440b7b 80#define VNDR_IE_PARSE_LIMIT 5
1a873342
HM
81
82#define DOT11_MGMT_HDR_LEN 24 /* d11 management header len */
83#define DOT11_BCN_PRB_FIXED_LEN 12 /* beacon/probe fixed length */
04012895 84
89286dc9
HM
85#define BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS 320
86#define BRCMF_SCAN_JOIN_PASSIVE_DWELL_TIME_MS 400
87#define BRCMF_SCAN_JOIN_PROBE_INTERVAL_MS 20
88
5b435de0
AS
89#define BRCMF_ASSOC_PARAMS_FIXED_SIZE \
90 (sizeof(struct brcmf_assoc_params_le) - sizeof(u16))
91
ce81e317 92static bool check_vif_up(struct brcmf_cfg80211_vif *vif)
5b435de0 93{
c1179033 94 if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state)) {
647c9ae0
AS
95 brcmf_dbg(INFO, "device is not ready : status (%lu)\n",
96 vif->sme_state);
5b435de0
AS
97 return false;
98 }
99 return true;
100}
101
102#define CHAN2G(_channel, _freq, _flags) { \
103 .band = IEEE80211_BAND_2GHZ, \
104 .center_freq = (_freq), \
105 .hw_value = (_channel), \
106 .flags = (_flags), \
107 .max_antenna_gain = 0, \
108 .max_power = 30, \
109}
110
111#define CHAN5G(_channel, _flags) { \
112 .band = IEEE80211_BAND_5GHZ, \
113 .center_freq = 5000 + (5 * (_channel)), \
114 .hw_value = (_channel), \
115 .flags = (_flags), \
116 .max_antenna_gain = 0, \
117 .max_power = 30, \
118}
119
120#define RATE_TO_BASE100KBPS(rate) (((rate) * 10) / 2)
121#define RATETAB_ENT(_rateid, _flags) \
122 { \
123 .bitrate = RATE_TO_BASE100KBPS(_rateid), \
124 .hw_value = (_rateid), \
125 .flags = (_flags), \
126 }
127
128static struct ieee80211_rate __wl_rates[] = {
129 RATETAB_ENT(BRCM_RATE_1M, 0),
130 RATETAB_ENT(BRCM_RATE_2M, IEEE80211_RATE_SHORT_PREAMBLE),
131 RATETAB_ENT(BRCM_RATE_5M5, IEEE80211_RATE_SHORT_PREAMBLE),
132 RATETAB_ENT(BRCM_RATE_11M, IEEE80211_RATE_SHORT_PREAMBLE),
133 RATETAB_ENT(BRCM_RATE_6M, 0),
134 RATETAB_ENT(BRCM_RATE_9M, 0),
135 RATETAB_ENT(BRCM_RATE_12M, 0),
136 RATETAB_ENT(BRCM_RATE_18M, 0),
137 RATETAB_ENT(BRCM_RATE_24M, 0),
138 RATETAB_ENT(BRCM_RATE_36M, 0),
139 RATETAB_ENT(BRCM_RATE_48M, 0),
140 RATETAB_ENT(BRCM_RATE_54M, 0),
141};
142
143#define wl_a_rates (__wl_rates + 4)
144#define wl_a_rates_size 8
145#define wl_g_rates (__wl_rates + 0)
146#define wl_g_rates_size 12
147
148static struct ieee80211_channel __wl_2ghz_channels[] = {
149 CHAN2G(1, 2412, 0),
150 CHAN2G(2, 2417, 0),
151 CHAN2G(3, 2422, 0),
152 CHAN2G(4, 2427, 0),
153 CHAN2G(5, 2432, 0),
154 CHAN2G(6, 2437, 0),
155 CHAN2G(7, 2442, 0),
156 CHAN2G(8, 2447, 0),
157 CHAN2G(9, 2452, 0),
158 CHAN2G(10, 2457, 0),
159 CHAN2G(11, 2462, 0),
160 CHAN2G(12, 2467, 0),
161 CHAN2G(13, 2472, 0),
162 CHAN2G(14, 2484, 0),
163};
164
165static struct ieee80211_channel __wl_5ghz_a_channels[] = {
166 CHAN5G(34, 0), CHAN5G(36, 0),
167 CHAN5G(38, 0), CHAN5G(40, 0),
168 CHAN5G(42, 0), CHAN5G(44, 0),
169 CHAN5G(46, 0), CHAN5G(48, 0),
170 CHAN5G(52, 0), CHAN5G(56, 0),
171 CHAN5G(60, 0), CHAN5G(64, 0),
172 CHAN5G(100, 0), CHAN5G(104, 0),
173 CHAN5G(108, 0), CHAN5G(112, 0),
174 CHAN5G(116, 0), CHAN5G(120, 0),
175 CHAN5G(124, 0), CHAN5G(128, 0),
176 CHAN5G(132, 0), CHAN5G(136, 0),
177 CHAN5G(140, 0), CHAN5G(149, 0),
178 CHAN5G(153, 0), CHAN5G(157, 0),
179 CHAN5G(161, 0), CHAN5G(165, 0),
180 CHAN5G(184, 0), CHAN5G(188, 0),
181 CHAN5G(192, 0), CHAN5G(196, 0),
182 CHAN5G(200, 0), CHAN5G(204, 0),
183 CHAN5G(208, 0), CHAN5G(212, 0),
184 CHAN5G(216, 0),
185};
186
5b435de0
AS
187static struct ieee80211_supported_band __wl_band_2ghz = {
188 .band = IEEE80211_BAND_2GHZ,
189 .channels = __wl_2ghz_channels,
190 .n_channels = ARRAY_SIZE(__wl_2ghz_channels),
191 .bitrates = wl_g_rates,
192 .n_bitrates = wl_g_rates_size,
193};
194
195static struct ieee80211_supported_band __wl_band_5ghz_a = {
196 .band = IEEE80211_BAND_5GHZ,
197 .channels = __wl_5ghz_a_channels,
198 .n_channels = ARRAY_SIZE(__wl_5ghz_a_channels),
199 .bitrates = wl_a_rates,
200 .n_bitrates = wl_a_rates_size,
201};
202
d48200ba
HM
203/* This is to override regulatory domains defined in cfg80211 module (reg.c)
204 * By default world regulatory domain defined in reg.c puts the flags
8fe02e16
LR
205 * NL80211_RRF_NO_IR for 5GHz channels (for * 36..48 and 149..165).
206 * With respect to these flags, wpa_supplicant doesn't * start p2p
207 * operations on 5GHz channels. All the changes in world regulatory
d48200ba
HM
208 * domain are to be done here.
209 */
210static const struct ieee80211_regdomain brcmf_regdom = {
211 .n_reg_rules = 4,
212 .alpha2 = "99",
213 .reg_rules = {
214 /* IEEE 802.11b/g, channels 1..11 */
215 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
216 /* If any */
217 /* IEEE 802.11 channel 14 - Only JP enables
218 * this and for 802.11b only
219 */
220 REG_RULE(2484-10, 2484+10, 20, 6, 20, 0),
221 /* IEEE 802.11a, channel 36..64 */
222 REG_RULE(5150-10, 5350+10, 40, 6, 20, 0),
223 /* IEEE 802.11a, channel 100..165 */
224 REG_RULE(5470-10, 5850+10, 40, 6, 20, 0), }
5b435de0
AS
225};
226
227static const u32 __wl_cipher_suites[] = {
228 WLAN_CIPHER_SUITE_WEP40,
229 WLAN_CIPHER_SUITE_WEP104,
230 WLAN_CIPHER_SUITE_TKIP,
231 WLAN_CIPHER_SUITE_CCMP,
232 WLAN_CIPHER_SUITE_AES_CMAC,
233};
234
1a873342
HM
235/* Vendor specific ie. id = 221, oui and type defines exact ie */
236struct brcmf_vs_tlv {
237 u8 id;
238 u8 len;
239 u8 oui[3];
240 u8 oui_type;
241};
242
243struct parsed_vndr_ie_info {
244 u8 *ie_ptr;
245 u32 ie_len; /* total length including id & length field */
246 struct brcmf_vs_tlv vndrie;
247};
248
249struct parsed_vndr_ies {
250 u32 count;
9f440b7b 251 struct parsed_vndr_ie_info ie_info[VNDR_IE_PARSE_LIMIT];
1a873342
HM
252};
253
ef6ac17a
AB
254/* Quarter dBm units to mW
255 * Table starts at QDBM_OFFSET, so the first entry is mW for qdBm=153
256 * Table is offset so the last entry is largest mW value that fits in
257 * a u16.
258 */
259
260#define QDBM_OFFSET 153 /* Offset for first entry */
261#define QDBM_TABLE_LEN 40 /* Table size */
262
263/* Smallest mW value that will round up to the first table entry, QDBM_OFFSET.
264 * Value is ( mW(QDBM_OFFSET - 1) + mW(QDBM_OFFSET) ) / 2
265 */
266#define QDBM_TABLE_LOW_BOUND 6493 /* Low bound */
267
268/* Largest mW value that will round down to the last table entry,
269 * QDBM_OFFSET + QDBM_TABLE_LEN-1.
270 * Value is ( mW(QDBM_OFFSET + QDBM_TABLE_LEN - 1) +
271 * mW(QDBM_OFFSET + QDBM_TABLE_LEN) ) / 2.
272 */
273#define QDBM_TABLE_HIGH_BOUND 64938 /* High bound */
274
275static const u16 nqdBm_to_mW_map[QDBM_TABLE_LEN] = {
276/* qdBm: +0 +1 +2 +3 +4 +5 +6 +7 */
277/* 153: */ 6683, 7079, 7499, 7943, 8414, 8913, 9441, 10000,
278/* 161: */ 10593, 11220, 11885, 12589, 13335, 14125, 14962, 15849,
279/* 169: */ 16788, 17783, 18836, 19953, 21135, 22387, 23714, 25119,
280/* 177: */ 26607, 28184, 29854, 31623, 33497, 35481, 37584, 39811,
281/* 185: */ 42170, 44668, 47315, 50119, 53088, 56234, 59566, 63096
282};
283
284static u16 brcmf_qdbm_to_mw(u8 qdbm)
285{
286 uint factor = 1;
287 int idx = qdbm - QDBM_OFFSET;
288
289 if (idx >= QDBM_TABLE_LEN)
290 /* clamp to max u16 mW value */
291 return 0xFFFF;
292
293 /* scale the qdBm index up to the range of the table 0-40
294 * where an offset of 40 qdBm equals a factor of 10 mW.
295 */
296 while (idx < 0) {
297 idx += 40;
298 factor *= 10;
299 }
300
301 /* return the mW value scaled down to the correct factor of 10,
302 * adding in factor/2 to get proper rounding.
303 */
304 return (nqdBm_to_mW_map[idx] + factor / 2) / factor;
305}
306
307static u8 brcmf_mw_to_qdbm(u16 mw)
308{
309 u8 qdbm;
310 int offset;
311 uint mw_uint = mw;
312 uint boundary;
313
314 /* handle boundary case */
315 if (mw_uint <= 1)
316 return 0;
317
318 offset = QDBM_OFFSET;
319
320 /* move mw into the range of the table */
321 while (mw_uint < QDBM_TABLE_LOW_BOUND) {
322 mw_uint *= 10;
323 offset -= 40;
324 }
325
326 for (qdbm = 0; qdbm < QDBM_TABLE_LEN - 1; qdbm++) {
327 boundary = nqdBm_to_mW_map[qdbm] + (nqdBm_to_mW_map[qdbm + 1] -
328 nqdBm_to_mW_map[qdbm]) / 2;
329 if (mw_uint < boundary)
330 break;
331 }
332
333 qdbm += (u8) offset;
334
335 return qdbm;
336}
337
83cf17aa
FL
338u16 channel_to_chanspec(struct brcmu_d11inf *d11inf,
339 struct ieee80211_channel *ch)
6e186166 340{
83cf17aa 341 struct brcmu_chan ch_inf;
6e186166 342
83cf17aa
FL
343 ch_inf.chnum = ieee80211_frequency_to_channel(ch->center_freq);
344 ch_inf.bw = BRCMU_CHAN_BW_20;
345 d11inf->encchspec(&ch_inf);
6e186166 346
83cf17aa 347 return ch_inf.chspec;
6e186166
AS
348}
349
89286dc9
HM
350/* Traverse a string of 1-byte tag/1-byte length/variable-length value
351 * triples, returning a pointer to the substring whose first element
352 * matches tag
353 */
354struct brcmf_tlv *brcmf_parse_tlvs(void *buf, int buflen, uint key)
355{
356 struct brcmf_tlv *elt;
357 int totlen;
358
359 elt = (struct brcmf_tlv *)buf;
360 totlen = buflen;
361
362 /* find tagged parameter */
363 while (totlen >= TLV_HDR_LEN) {
364 int len = elt->len;
365
366 /* validate remaining totlen */
367 if ((elt->id == key) && (totlen >= (len + TLV_HDR_LEN)))
368 return elt;
369
370 elt = (struct brcmf_tlv *)((u8 *)elt + (len + TLV_HDR_LEN));
371 totlen -= (len + TLV_HDR_LEN);
372 }
373
374 return NULL;
375}
376
377/* Is any of the tlvs the expected entry? If
378 * not update the tlvs buffer pointer/length.
379 */
380static bool
381brcmf_tlv_has_ie(u8 *ie, u8 **tlvs, u32 *tlvs_len,
382 u8 *oui, u32 oui_len, u8 type)
383{
384 /* If the contents match the OUI and the type */
385 if (ie[TLV_LEN_OFF] >= oui_len + 1 &&
386 !memcmp(&ie[TLV_BODY_OFF], oui, oui_len) &&
387 type == ie[TLV_BODY_OFF + oui_len]) {
388 return true;
389 }
390
391 if (tlvs == NULL)
392 return false;
393 /* point to the next ie */
394 ie += ie[TLV_LEN_OFF] + TLV_HDR_LEN;
395 /* calculate the length of the rest of the buffer */
396 *tlvs_len -= (int)(ie - *tlvs);
397 /* update the pointer to the start of the buffer */
398 *tlvs = ie;
399
400 return false;
401}
402
403static struct brcmf_vs_tlv *
404brcmf_find_wpaie(u8 *parse, u32 len)
405{
406 struct brcmf_tlv *ie;
407
408 while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
409 if (brcmf_tlv_has_ie((u8 *)ie, &parse, &len,
410 WPA_OUI, TLV_OUI_LEN, WPA_OUI_TYPE))
411 return (struct brcmf_vs_tlv *)ie;
412 }
413 return NULL;
414}
415
416static struct brcmf_vs_tlv *
417brcmf_find_wpsie(u8 *parse, u32 len)
418{
419 struct brcmf_tlv *ie;
420
421 while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
422 if (brcmf_tlv_has_ie((u8 *)ie, &parse, &len,
423 WPA_OUI, TLV_OUI_LEN, WPS_OUI_TYPE))
424 return (struct brcmf_vs_tlv *)ie;
425 }
426 return NULL;
427}
428
429
5b435de0
AS
430static void convert_key_from_CPU(struct brcmf_wsec_key *key,
431 struct brcmf_wsec_key_le *key_le)
432{
433 key_le->index = cpu_to_le32(key->index);
434 key_le->len = cpu_to_le32(key->len);
435 key_le->algo = cpu_to_le32(key->algo);
436 key_le->flags = cpu_to_le32(key->flags);
437 key_le->rxiv.hi = cpu_to_le32(key->rxiv.hi);
438 key_le->rxiv.lo = cpu_to_le16(key->rxiv.lo);
439 key_le->iv_initialized = cpu_to_le32(key->iv_initialized);
440 memcpy(key_le->data, key->data, sizeof(key->data));
441 memcpy(key_le->ea, key->ea, sizeof(key->ea));
442}
443
f09d0c02 444static int
2eaba7e8 445send_key_to_dongle(struct net_device *ndev, struct brcmf_wsec_key *key)
5b435de0
AS
446{
447 int err;
448 struct brcmf_wsec_key_le key_le;
449
450 convert_key_from_CPU(key, &key_le);
f09d0c02 451
81f5dcb8
HM
452 brcmf_netdev_wait_pend8021x(ndev);
453
ac24be6f 454 err = brcmf_fil_bsscfg_data_set(netdev_priv(ndev), "wsec_key", &key_le,
81f5dcb8 455 sizeof(key_le));
f09d0c02 456
5b435de0 457 if (err)
57d6e91a 458 brcmf_err("wsec_key error (%d)\n", err);
5b435de0
AS
459 return err;
460}
461
b3657453
HM
462static s32
463brcmf_configure_arp_offload(struct brcmf_if *ifp, bool enable)
464{
465 s32 err;
466 u32 mode;
467
468 if (enable)
469 mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
470 else
471 mode = 0;
472
473 /* Try to set and enable ARP offload feature, this may fail, then it */
474 /* is simply not supported and err 0 will be returned */
475 err = brcmf_fil_iovar_int_set(ifp, "arp_ol", mode);
476 if (err) {
477 brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
478 mode, err);
479 err = 0;
480 } else {
481 err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
482 if (err) {
483 brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
484 enable, err);
485 err = 0;
486 } else
487 brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
488 enable, mode);
489 }
490
491 return err;
492}
493
9f440b7b
AS
494static struct wireless_dev *brcmf_cfg80211_add_iface(struct wiphy *wiphy,
495 const char *name,
496 enum nl80211_iftype type,
497 u32 *flags,
498 struct vif_params *params)
499{
500 brcmf_dbg(TRACE, "enter: %s type %d\n", name, type);
501 switch (type) {
502 case NL80211_IFTYPE_ADHOC:
503 case NL80211_IFTYPE_STATION:
504 case NL80211_IFTYPE_AP:
505 case NL80211_IFTYPE_AP_VLAN:
506 case NL80211_IFTYPE_WDS:
507 case NL80211_IFTYPE_MONITOR:
508 case NL80211_IFTYPE_MESH_POINT:
509 return ERR_PTR(-EOPNOTSUPP);
510 case NL80211_IFTYPE_P2P_CLIENT:
511 case NL80211_IFTYPE_P2P_GO:
27f10e38 512 case NL80211_IFTYPE_P2P_DEVICE:
9f440b7b
AS
513 return brcmf_p2p_add_vif(wiphy, name, type, flags, params);
514 case NL80211_IFTYPE_UNSPECIFIED:
9f440b7b
AS
515 default:
516 return ERR_PTR(-EINVAL);
517 }
518}
519
f96aa07e 520void brcmf_set_mpc(struct brcmf_if *ifp, int mpc)
5f4f9f11 521{
5f4f9f11
AS
522 s32 err = 0;
523
524 if (check_vif_up(ifp->vif)) {
525 err = brcmf_fil_iovar_int_set(ifp, "mpc", mpc);
526 if (err) {
527 brcmf_err("fail to set mpc\n");
528 return;
529 }
530 brcmf_dbg(INFO, "MPC : %d\n", mpc);
531 }
532}
533
a0f472ac
AS
534s32 brcmf_notify_escan_complete(struct brcmf_cfg80211_info *cfg,
535 struct brcmf_if *ifp, bool aborted,
536 bool fw_abort)
5f4f9f11
AS
537{
538 struct brcmf_scan_params_le params_le;
539 struct cfg80211_scan_request *scan_request;
540 s32 err = 0;
541
542 brcmf_dbg(SCAN, "Enter\n");
543
544 /* clear scan request, because the FW abort can cause a second call */
545 /* to this functon and might cause a double cfg80211_scan_done */
546 scan_request = cfg->scan_request;
547 cfg->scan_request = NULL;
548
549 if (timer_pending(&cfg->escan_timeout))
550 del_timer_sync(&cfg->escan_timeout);
551
552 if (fw_abort) {
553 /* Do a scan abort to stop the driver's scan engine */
554 brcmf_dbg(SCAN, "ABORT scan in firmware\n");
555 memset(&params_le, 0, sizeof(params_le));
556 memset(params_le.bssid, 0xFF, ETH_ALEN);
557 params_le.bss_type = DOT11_BSSTYPE_ANY;
558 params_le.scan_type = 0;
559 params_le.channel_num = cpu_to_le32(1);
560 params_le.nprobes = cpu_to_le32(1);
561 params_le.active_time = cpu_to_le32(-1);
562 params_le.passive_time = cpu_to_le32(-1);
563 params_le.home_time = cpu_to_le32(-1);
564 /* Scan is aborted by setting channel_list[0] to -1 */
565 params_le.channel_list[0] = cpu_to_le16(-1);
566 /* E-Scan (or anyother type) can be aborted by SCAN */
f96aa07e 567 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
5f4f9f11
AS
568 &params_le, sizeof(params_le));
569 if (err)
570 brcmf_err("Scan abort failed\n");
571 }
572 /*
573 * e-scan can be initiated by scheduled scan
574 * which takes precedence.
575 */
576 if (cfg->sched_escan) {
577 brcmf_dbg(SCAN, "scheduled scan completed\n");
578 cfg->sched_escan = false;
579 if (!aborted)
580 cfg80211_sched_scan_results(cfg_to_wiphy(cfg));
f96aa07e 581 brcmf_set_mpc(ifp, 1);
5f4f9f11
AS
582 } else if (scan_request) {
583 brcmf_dbg(SCAN, "ESCAN Completed scan: %s\n",
584 aborted ? "Aborted" : "Done");
585 cfg80211_scan_done(scan_request, aborted);
f96aa07e 586 brcmf_set_mpc(ifp, 1);
5f4f9f11 587 }
6eda4e2c
HM
588 if (!test_and_clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
589 brcmf_dbg(SCAN, "Scan complete, probably P2P scan\n");
5f4f9f11
AS
590
591 return err;
592}
593
9f440b7b
AS
594static
595int brcmf_cfg80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
596{
5f4f9f11
AS
597 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
598 struct net_device *ndev = wdev->netdev;
599
600 /* vif event pending in firmware */
601 if (brcmf_cfg80211_vif_event_armed(cfg))
602 return -EBUSY;
603
604 if (ndev) {
605 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status) &&
a0f472ac
AS
606 cfg->escan_info.ifp == netdev_priv(ndev))
607 brcmf_notify_escan_complete(cfg, netdev_priv(ndev),
608 true, true);
5f4f9f11
AS
609
610 brcmf_fil_iovar_int_set(netdev_priv(ndev), "mpc", 1);
611 }
612
9f440b7b
AS
613 switch (wdev->iftype) {
614 case NL80211_IFTYPE_ADHOC:
615 case NL80211_IFTYPE_STATION:
616 case NL80211_IFTYPE_AP:
617 case NL80211_IFTYPE_AP_VLAN:
618 case NL80211_IFTYPE_WDS:
619 case NL80211_IFTYPE_MONITOR:
620 case NL80211_IFTYPE_MESH_POINT:
621 return -EOPNOTSUPP;
622 case NL80211_IFTYPE_P2P_CLIENT:
623 case NL80211_IFTYPE_P2P_GO:
27f10e38 624 case NL80211_IFTYPE_P2P_DEVICE:
9f440b7b
AS
625 return brcmf_p2p_del_vif(wiphy, wdev);
626 case NL80211_IFTYPE_UNSPECIFIED:
9f440b7b
AS
627 default:
628 return -EINVAL;
629 }
630 return -EOPNOTSUPP;
631}
632
5b435de0
AS
633static s32
634brcmf_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
635 enum nl80211_iftype type, u32 *flags,
636 struct vif_params *params)
637{
7a5c1f64 638 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
c1179033 639 struct brcmf_if *ifp = netdev_priv(ndev);
128ce3b6 640 struct brcmf_cfg80211_vif *vif = ifp->vif;
5b435de0 641 s32 infra = 0;
1a873342 642 s32 ap = 0;
5b435de0
AS
643 s32 err = 0;
644
d96b801f 645 brcmf_dbg(TRACE, "Enter, ndev=%p, type=%d\n", ndev, type);
5b435de0
AS
646
647 switch (type) {
648 case NL80211_IFTYPE_MONITOR:
649 case NL80211_IFTYPE_WDS:
57d6e91a
AS
650 brcmf_err("type (%d) : currently we do not support this type\n",
651 type);
5b435de0
AS
652 return -EOPNOTSUPP;
653 case NL80211_IFTYPE_ADHOC:
128ce3b6 654 vif->mode = WL_MODE_IBSS;
5b435de0
AS
655 infra = 0;
656 break;
657 case NL80211_IFTYPE_STATION:
1bc7c654
HM
658 /* Ignore change for p2p IF. Unclear why supplicant does this */
659 if ((vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT) ||
660 (vif->wdev.iftype == NL80211_IFTYPE_P2P_GO)) {
661 brcmf_dbg(TRACE, "Ignoring cmd for p2p if\n");
662 /* WAR: It is unexpected to get a change of VIF for P2P
663 * IF, but it happens. The request can not be handled
664 * but returning EPERM causes a crash. Returning 0
665 * without setting ieee80211_ptr->iftype causes trace
666 * (WARN_ON) but it works with wpa_supplicant
667 */
668 return 0;
669 }
128ce3b6 670 vif->mode = WL_MODE_BSS;
5b435de0
AS
671 infra = 1;
672 break;
1a873342 673 case NL80211_IFTYPE_AP:
7a5c1f64 674 case NL80211_IFTYPE_P2P_GO:
128ce3b6 675 vif->mode = WL_MODE_AP;
1a873342
HM
676 ap = 1;
677 break;
5b435de0
AS
678 default:
679 err = -EINVAL;
680 goto done;
681 }
682
1a873342 683 if (ap) {
7a5c1f64
HM
684 if (type == NL80211_IFTYPE_P2P_GO) {
685 brcmf_dbg(INFO, "IF Type = P2P GO\n");
686 err = brcmf_p2p_ifchange(cfg, BRCMF_FIL_P2P_IF_GO);
687 }
688 if (!err) {
689 set_bit(BRCMF_VIF_STATUS_AP_CREATING, &vif->sme_state);
690 brcmf_dbg(INFO, "IF Type = AP\n");
691 }
5b435de0 692 } else {
128ce3b6 693 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, infra);
1a873342 694 if (err) {
57d6e91a 695 brcmf_err("WLC_SET_INFRA error (%d)\n", err);
1a873342
HM
696 err = -EAGAIN;
697 goto done;
698 }
647c9ae0
AS
699 brcmf_dbg(INFO, "IF Type = %s\n", (vif->mode == WL_MODE_IBSS) ?
700 "Adhoc" : "Infra");
5b435de0 701 }
1a873342 702 ndev->ieee80211_ptr->iftype = type;
5b435de0
AS
703
704done:
d96b801f 705 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
706
707 return err;
708}
709
83cf17aa
FL
710static void brcmf_escan_prep(struct brcmf_cfg80211_info *cfg,
711 struct brcmf_scan_params_le *params_le,
e756af5b
HM
712 struct cfg80211_scan_request *request)
713{
714 u32 n_ssids;
715 u32 n_channels;
716 s32 i;
717 s32 offset;
029591f3 718 u16 chanspec;
e756af5b 719 char *ptr;
029591f3 720 struct brcmf_ssid_le ssid_le;
e756af5b 721
ba40d166 722 memset(params_le->bssid, 0xFF, ETH_ALEN);
e756af5b
HM
723 params_le->bss_type = DOT11_BSSTYPE_ANY;
724 params_le->scan_type = 0;
725 params_le->channel_num = 0;
726 params_le->nprobes = cpu_to_le32(-1);
727 params_le->active_time = cpu_to_le32(-1);
728 params_le->passive_time = cpu_to_le32(-1);
729 params_le->home_time = cpu_to_le32(-1);
730 memset(&params_le->ssid_le, 0, sizeof(params_le->ssid_le));
731
732 /* if request is null exit so it will be all channel broadcast scan */
733 if (!request)
734 return;
735
736 n_ssids = request->n_ssids;
737 n_channels = request->n_channels;
738 /* Copy channel array if applicable */
4e8a008e
AS
739 brcmf_dbg(SCAN, "### List of channelspecs to scan ### %d\n",
740 n_channels);
e756af5b
HM
741 if (n_channels > 0) {
742 for (i = 0; i < n_channels; i++) {
83cf17aa
FL
743 chanspec = channel_to_chanspec(&cfg->d11inf,
744 request->channels[i]);
4e8a008e
AS
745 brcmf_dbg(SCAN, "Chan : %d, Channel spec: %x\n",
746 request->channels[i]->hw_value, chanspec);
029591f3 747 params_le->channel_list[i] = cpu_to_le16(chanspec);
e756af5b
HM
748 }
749 } else {
4e8a008e 750 brcmf_dbg(SCAN, "Scanning all channels\n");
e756af5b
HM
751 }
752 /* Copy ssid array if applicable */
4e8a008e 753 brcmf_dbg(SCAN, "### List of SSIDs to scan ### %d\n", n_ssids);
e756af5b
HM
754 if (n_ssids > 0) {
755 offset = offsetof(struct brcmf_scan_params_le, channel_list) +
756 n_channels * sizeof(u16);
757 offset = roundup(offset, sizeof(u32));
758 ptr = (char *)params_le + offset;
759 for (i = 0; i < n_ssids; i++) {
029591f3
AS
760 memset(&ssid_le, 0, sizeof(ssid_le));
761 ssid_le.SSID_len =
762 cpu_to_le32(request->ssids[i].ssid_len);
763 memcpy(ssid_le.SSID, request->ssids[i].ssid,
764 request->ssids[i].ssid_len);
765 if (!ssid_le.SSID_len)
4e8a008e 766 brcmf_dbg(SCAN, "%d: Broadcast scan\n", i);
e756af5b 767 else
4e8a008e
AS
768 brcmf_dbg(SCAN, "%d: scan for %s size =%d\n",
769 i, ssid_le.SSID, ssid_le.SSID_len);
029591f3
AS
770 memcpy(ptr, &ssid_le, sizeof(ssid_le));
771 ptr += sizeof(ssid_le);
e756af5b
HM
772 }
773 } else {
4e8a008e 774 brcmf_dbg(SCAN, "Broadcast scan %p\n", request->ssids);
e756af5b 775 if ((request->ssids) && request->ssids->ssid_len) {
4e8a008e
AS
776 brcmf_dbg(SCAN, "SSID %s len=%d\n",
777 params_le->ssid_le.SSID,
778 request->ssids->ssid_len);
e756af5b
HM
779 params_le->ssid_le.SSID_len =
780 cpu_to_le32(request->ssids->ssid_len);
781 memcpy(&params_le->ssid_le.SSID, request->ssids->ssid,
782 request->ssids->ssid_len);
783 }
784 }
785 /* Adding mask to channel numbers */
786 params_le->channel_num =
787 cpu_to_le32((n_ssids << BRCMF_SCAN_PARAMS_NSSID_SHIFT) |
788 (n_channels & BRCMF_SCAN_PARAMS_COUNT_MASK));
789}
790
e756af5b 791static s32
a0f472ac 792brcmf_run_escan(struct brcmf_cfg80211_info *cfg, struct brcmf_if *ifp,
e756af5b
HM
793 struct cfg80211_scan_request *request, u16 action)
794{
795 s32 params_size = BRCMF_SCAN_PARAMS_FIXED_SIZE +
796 offsetof(struct brcmf_escan_params_le, params_le);
797 struct brcmf_escan_params_le *params;
798 s32 err = 0;
799
4e8a008e 800 brcmf_dbg(SCAN, "E-SCAN START\n");
e756af5b
HM
801
802 if (request != NULL) {
803 /* Allocate space for populating ssids in struct */
804 params_size += sizeof(u32) * ((request->n_channels + 1) / 2);
805
806 /* Allocate space for populating ssids in struct */
807 params_size += sizeof(struct brcmf_ssid) * request->n_ssids;
808 }
809
810 params = kzalloc(params_size, GFP_KERNEL);
811 if (!params) {
812 err = -ENOMEM;
813 goto exit;
814 }
815 BUG_ON(params_size + sizeof("escan") >= BRCMF_DCMD_MEDLEN);
83cf17aa 816 brcmf_escan_prep(cfg, &params->params_le, request);
e756af5b
HM
817 params->version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
818 params->action = cpu_to_le16(action);
819 params->sync_id = cpu_to_le16(0x1234);
820
a0f472ac 821 err = brcmf_fil_iovar_data_set(ifp, "escan", params, params_size);
e756af5b
HM
822 if (err) {
823 if (err == -EBUSY)
647c9ae0 824 brcmf_dbg(INFO, "system busy : escan canceled\n");
e756af5b 825 else
57d6e91a 826 brcmf_err("error (%d)\n", err);
e756af5b
HM
827 }
828
829 kfree(params);
830exit:
831 return err;
832}
833
834static s32
27a68fe3 835brcmf_do_escan(struct brcmf_cfg80211_info *cfg, struct wiphy *wiphy,
a0f472ac 836 struct brcmf_if *ifp, struct cfg80211_scan_request *request)
e756af5b
HM
837{
838 s32 err;
81f5dcb8 839 u32 passive_scan;
e756af5b 840 struct brcmf_scan_results *results;
9f440b7b 841 struct escan_info *escan = &cfg->escan_info;
e756af5b 842
4e8a008e 843 brcmf_dbg(SCAN, "Enter\n");
a0f472ac 844 escan->ifp = ifp;
9f440b7b
AS
845 escan->wiphy = wiphy;
846 escan->escan_state = WL_ESCAN_STATE_SCANNING;
81f5dcb8 847 passive_scan = cfg->active_scan ? 0 : 1;
f96aa07e 848 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PASSIVE_SCAN,
81f5dcb8 849 passive_scan);
e756af5b 850 if (err) {
57d6e91a 851 brcmf_err("error (%d)\n", err);
e756af5b
HM
852 return err;
853 }
f96aa07e 854 brcmf_set_mpc(ifp, 0);
27a68fe3 855 results = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
e756af5b
HM
856 results->version = 0;
857 results->count = 0;
858 results->buflen = WL_ESCAN_RESULTS_FIXED_SIZE;
859
a0f472ac 860 err = escan->run(cfg, ifp, request, WL_ESCAN_ACTION_START);
e756af5b 861 if (err)
f96aa07e 862 brcmf_set_mpc(ifp, 1);
e756af5b
HM
863 return err;
864}
865
866static s32
a0f472ac 867brcmf_cfg80211_escan(struct wiphy *wiphy, struct brcmf_cfg80211_vif *vif,
e756af5b
HM
868 struct cfg80211_scan_request *request,
869 struct cfg80211_ssid *this_ssid)
870{
a0f472ac
AS
871 struct brcmf_if *ifp = vif->ifp;
872 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
e756af5b 873 struct cfg80211_ssid *ssids;
f0799895 874 struct brcmf_cfg80211_scan_req *sr = &cfg->scan_req_int;
81f5dcb8 875 u32 passive_scan;
e756af5b
HM
876 bool escan_req;
877 bool spec_scan;
878 s32 err;
879 u32 SSID_len;
880
4e8a008e 881 brcmf_dbg(SCAN, "START ESCAN\n");
e756af5b 882
c1179033 883 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
57d6e91a 884 brcmf_err("Scanning already: status (%lu)\n", cfg->scan_status);
e756af5b
HM
885 return -EAGAIN;
886 }
c1179033 887 if (test_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status)) {
57d6e91a
AS
888 brcmf_err("Scanning being aborted: status (%lu)\n",
889 cfg->scan_status);
e756af5b
HM
890 return -EAGAIN;
891 }
1687eee2
AS
892 if (test_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
893 brcmf_err("Scanning suppressed: status (%lu)\n",
894 cfg->scan_status);
895 return -EAGAIN;
896 }
c1179033 897 if (test_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state)) {
57d6e91a 898 brcmf_err("Connecting: status (%lu)\n", ifp->vif->sme_state);
e756af5b
HM
899 return -EAGAIN;
900 }
901
0f8ffe17 902 /* If scan req comes for p2p0, send it over primary I/F */
a0f472ac
AS
903 if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
904 vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
0f8ffe17 905
e756af5b 906 /* Arm scan timeout timer */
27a68fe3 907 mod_timer(&cfg->escan_timeout, jiffies +
e756af5b
HM
908 WL_ESCAN_TIMER_INTERVAL_MS * HZ / 1000);
909
910 escan_req = false;
911 if (request) {
912 /* scan bss */
913 ssids = request->ssids;
914 escan_req = true;
915 } else {
916 /* scan in ibss */
917 /* we don't do escan in ibss */
918 ssids = this_ssid;
919 }
920
27a68fe3 921 cfg->scan_request = request;
c1179033 922 set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
e756af5b 923 if (escan_req) {
9f440b7b 924 cfg->escan_info.run = brcmf_run_escan;
a0f472ac 925 err = brcmf_p2p_scan_prep(wiphy, request, vif);
9f440b7b
AS
926 if (err)
927 goto scan_out;
928
a0f472ac 929 err = brcmf_do_escan(cfg, wiphy, vif->ifp, request);
2cb941c0 930 if (err)
e756af5b
HM
931 goto scan_out;
932 } else {
4e8a008e
AS
933 brcmf_dbg(SCAN, "ssid \"%s\", ssid_len (%d)\n",
934 ssids->ssid, ssids->ssid_len);
e756af5b
HM
935 memset(&sr->ssid_le, 0, sizeof(sr->ssid_le));
936 SSID_len = min_t(u8, sizeof(sr->ssid_le.SSID), ssids->ssid_len);
937 sr->ssid_le.SSID_len = cpu_to_le32(0);
938 spec_scan = false;
939 if (SSID_len) {
940 memcpy(sr->ssid_le.SSID, ssids->ssid, SSID_len);
941 sr->ssid_le.SSID_len = cpu_to_le32(SSID_len);
942 spec_scan = true;
943 } else
4e8a008e 944 brcmf_dbg(SCAN, "Broadcast scan\n");
e756af5b 945
81f5dcb8 946 passive_scan = cfg->active_scan ? 0 : 1;
c1179033 947 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PASSIVE_SCAN,
81f5dcb8 948 passive_scan);
e756af5b 949 if (err) {
57d6e91a 950 brcmf_err("WLC_SET_PASSIVE_SCAN error (%d)\n", err);
e756af5b
HM
951 goto scan_out;
952 }
f96aa07e 953 brcmf_set_mpc(ifp, 0);
c1179033 954 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
ac24be6f 955 &sr->ssid_le, sizeof(sr->ssid_le));
e756af5b
HM
956 if (err) {
957 if (err == -EBUSY)
647c9ae0
AS
958 brcmf_dbg(INFO, "BUSY: scan for \"%s\" canceled\n",
959 sr->ssid_le.SSID);
e756af5b 960 else
57d6e91a 961 brcmf_err("WLC_SCAN error (%d)\n", err);
e756af5b 962
f96aa07e 963 brcmf_set_mpc(ifp, 1);
e756af5b
HM
964 goto scan_out;
965 }
966 }
967
968 return 0;
969
970scan_out:
c1179033 971 clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
27a68fe3
AS
972 if (timer_pending(&cfg->escan_timeout))
973 del_timer_sync(&cfg->escan_timeout);
974 cfg->scan_request = NULL;
e756af5b
HM
975 return err;
976}
977
5b435de0 978static s32
0abb5f21 979brcmf_cfg80211_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
5b435de0 980{
a0f472ac 981 struct brcmf_cfg80211_vif *vif;
5b435de0
AS
982 s32 err = 0;
983
d96b801f 984 brcmf_dbg(TRACE, "Enter\n");
a0f472ac
AS
985 vif = container_of(request->wdev, struct brcmf_cfg80211_vif, wdev);
986 if (!check_vif_up(vif))
5b435de0
AS
987 return -EIO;
988
a0f472ac 989 err = brcmf_cfg80211_escan(wiphy, vif, request, NULL);
e756af5b 990
5b435de0 991 if (err)
57d6e91a 992 brcmf_err("scan error (%d)\n", err);
5b435de0 993
d96b801f 994 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
995 return err;
996}
997
998static s32 brcmf_set_rts(struct net_device *ndev, u32 rts_threshold)
999{
1000 s32 err = 0;
1001
ac24be6f
AS
1002 err = brcmf_fil_iovar_int_set(netdev_priv(ndev), "rtsthresh",
1003 rts_threshold);
5b435de0 1004 if (err)
57d6e91a 1005 brcmf_err("Error (%d)\n", err);
5b435de0
AS
1006
1007 return err;
1008}
1009
1010static s32 brcmf_set_frag(struct net_device *ndev, u32 frag_threshold)
1011{
1012 s32 err = 0;
1013
ac24be6f
AS
1014 err = brcmf_fil_iovar_int_set(netdev_priv(ndev), "fragthresh",
1015 frag_threshold);
5b435de0 1016 if (err)
57d6e91a 1017 brcmf_err("Error (%d)\n", err);
5b435de0
AS
1018
1019 return err;
1020}
1021
1022static s32 brcmf_set_retry(struct net_device *ndev, u32 retry, bool l)
1023{
1024 s32 err = 0;
b87e2c48 1025 u32 cmd = (l ? BRCMF_C_SET_LRL : BRCMF_C_SET_SRL);
5b435de0 1026
ac24be6f 1027 err = brcmf_fil_cmd_int_set(netdev_priv(ndev), cmd, retry);
5b435de0 1028 if (err) {
57d6e91a 1029 brcmf_err("cmd (%d) , error (%d)\n", cmd, err);
5b435de0
AS
1030 return err;
1031 }
1032 return err;
1033}
1034
1035static s32 brcmf_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1036{
27a68fe3
AS
1037 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1038 struct net_device *ndev = cfg_to_ndev(cfg);
0abb5f21 1039 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
1040 s32 err = 0;
1041
d96b801f 1042 brcmf_dbg(TRACE, "Enter\n");
ce81e317 1043 if (!check_vif_up(ifp->vif))
5b435de0
AS
1044 return -EIO;
1045
1046 if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
27a68fe3
AS
1047 (cfg->conf->rts_threshold != wiphy->rts_threshold)) {
1048 cfg->conf->rts_threshold = wiphy->rts_threshold;
1049 err = brcmf_set_rts(ndev, cfg->conf->rts_threshold);
5b435de0
AS
1050 if (!err)
1051 goto done;
1052 }
1053 if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
27a68fe3
AS
1054 (cfg->conf->frag_threshold != wiphy->frag_threshold)) {
1055 cfg->conf->frag_threshold = wiphy->frag_threshold;
1056 err = brcmf_set_frag(ndev, cfg->conf->frag_threshold);
5b435de0
AS
1057 if (!err)
1058 goto done;
1059 }
1060 if (changed & WIPHY_PARAM_RETRY_LONG
27a68fe3
AS
1061 && (cfg->conf->retry_long != wiphy->retry_long)) {
1062 cfg->conf->retry_long = wiphy->retry_long;
1063 err = brcmf_set_retry(ndev, cfg->conf->retry_long, true);
5b435de0
AS
1064 if (!err)
1065 goto done;
1066 }
1067 if (changed & WIPHY_PARAM_RETRY_SHORT
27a68fe3
AS
1068 && (cfg->conf->retry_short != wiphy->retry_short)) {
1069 cfg->conf->retry_short = wiphy->retry_short;
1070 err = brcmf_set_retry(ndev, cfg->conf->retry_short, false);
5b435de0
AS
1071 if (!err)
1072 goto done;
1073 }
1074
1075done:
d96b801f 1076 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1077 return err;
1078}
1079
5b435de0
AS
1080static void brcmf_init_prof(struct brcmf_cfg80211_profile *prof)
1081{
1082 memset(prof, 0, sizeof(*prof));
1083}
1084
903e0eee 1085static void brcmf_link_down(struct brcmf_cfg80211_vif *vif)
5b435de0 1086{
61730d4d 1087 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(vif->wdev.wiphy);
5b435de0
AS
1088 s32 err = 0;
1089
d96b801f 1090 brcmf_dbg(TRACE, "Enter\n");
5b435de0 1091
903e0eee 1092 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state)) {
647c9ae0 1093 brcmf_dbg(INFO, "Call WLC_DISASSOC to stop excess roaming\n ");
903e0eee 1094 err = brcmf_fil_cmd_data_set(vif->ifp,
ac24be6f 1095 BRCMF_C_DISASSOC, NULL, 0);
a538ae31 1096 if (err) {
57d6e91a 1097 brcmf_err("WLC_DISASSOC failed (%d)\n", err);
a538ae31 1098 }
903e0eee 1099 clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state);
43dffbc6
AS
1100 cfg80211_disconnected(vif->wdev.netdev, 0, NULL, 0, GFP_KERNEL);
1101
5b435de0 1102 }
903e0eee 1103 clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state);
61730d4d
PH
1104 clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
1105 brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
d96b801f 1106 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1107}
1108
1109static s32
1110brcmf_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
1111 struct cfg80211_ibss_params *params)
1112{
27a68fe3 1113 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21
AS
1114 struct brcmf_if *ifp = netdev_priv(ndev);
1115 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5b435de0
AS
1116 struct brcmf_join_params join_params;
1117 size_t join_params_size = 0;
1118 s32 err = 0;
1119 s32 wsec = 0;
1120 s32 bcnprd;
1701261d 1121 u16 chanspec;
5b435de0 1122
d96b801f 1123 brcmf_dbg(TRACE, "Enter\n");
ce81e317 1124 if (!check_vif_up(ifp->vif))
5b435de0
AS
1125 return -EIO;
1126
1127 if (params->ssid)
16886735 1128 brcmf_dbg(CONN, "SSID: %s\n", params->ssid);
5b435de0 1129 else {
16886735 1130 brcmf_dbg(CONN, "SSID: NULL, Not supported\n");
5b435de0
AS
1131 return -EOPNOTSUPP;
1132 }
1133
c1179033 1134 set_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
5b435de0
AS
1135
1136 if (params->bssid)
16886735 1137 brcmf_dbg(CONN, "BSSID: %pM\n", params->bssid);
5b435de0 1138 else
16886735 1139 brcmf_dbg(CONN, "No BSSID specified\n");
5b435de0 1140
683b6d3b 1141 if (params->chandef.chan)
16886735
AS
1142 brcmf_dbg(CONN, "channel: %d\n",
1143 params->chandef.chan->center_freq);
5b435de0 1144 else
16886735 1145 brcmf_dbg(CONN, "no channel specified\n");
5b435de0
AS
1146
1147 if (params->channel_fixed)
16886735 1148 brcmf_dbg(CONN, "fixed channel required\n");
5b435de0 1149 else
16886735 1150 brcmf_dbg(CONN, "no fixed channel required\n");
5b435de0
AS
1151
1152 if (params->ie && params->ie_len)
16886735 1153 brcmf_dbg(CONN, "ie len: %d\n", params->ie_len);
5b435de0 1154 else
16886735 1155 brcmf_dbg(CONN, "no ie specified\n");
5b435de0
AS
1156
1157 if (params->beacon_interval)
16886735
AS
1158 brcmf_dbg(CONN, "beacon interval: %d\n",
1159 params->beacon_interval);
5b435de0 1160 else
16886735 1161 brcmf_dbg(CONN, "no beacon interval specified\n");
5b435de0
AS
1162
1163 if (params->basic_rates)
16886735 1164 brcmf_dbg(CONN, "basic rates: %08X\n", params->basic_rates);
5b435de0 1165 else
16886735 1166 brcmf_dbg(CONN, "no basic rates specified\n");
5b435de0
AS
1167
1168 if (params->privacy)
16886735 1169 brcmf_dbg(CONN, "privacy required\n");
5b435de0 1170 else
16886735 1171 brcmf_dbg(CONN, "no privacy required\n");
5b435de0
AS
1172
1173 /* Configure Privacy for starter */
1174 if (params->privacy)
1175 wsec |= WEP_ENABLED;
1176
c1179033 1177 err = brcmf_fil_iovar_int_set(ifp, "wsec", wsec);
5b435de0 1178 if (err) {
57d6e91a 1179 brcmf_err("wsec failed (%d)\n", err);
5b435de0
AS
1180 goto done;
1181 }
1182
1183 /* Configure Beacon Interval for starter */
1184 if (params->beacon_interval)
1185 bcnprd = params->beacon_interval;
1186 else
1187 bcnprd = 100;
1188
b87e2c48 1189 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD, bcnprd);
5b435de0 1190 if (err) {
57d6e91a 1191 brcmf_err("WLC_SET_BCNPRD failed (%d)\n", err);
5b435de0
AS
1192 goto done;
1193 }
1194
1195 /* Configure required join parameter */
1196 memset(&join_params, 0, sizeof(struct brcmf_join_params));
1197
1198 /* SSID */
6c8c4f72
AS
1199 profile->ssid.SSID_len = min_t(u32, params->ssid_len, 32);
1200 memcpy(profile->ssid.SSID, params->ssid, profile->ssid.SSID_len);
1201 memcpy(join_params.ssid_le.SSID, params->ssid, profile->ssid.SSID_len);
1202 join_params.ssid_le.SSID_len = cpu_to_le32(profile->ssid.SSID_len);
5b435de0 1203 join_params_size = sizeof(join_params.ssid_le);
5b435de0
AS
1204
1205 /* BSSID */
1206 if (params->bssid) {
1207 memcpy(join_params.params_le.bssid, params->bssid, ETH_ALEN);
1208 join_params_size = sizeof(join_params.ssid_le) +
1209 BRCMF_ASSOC_PARAMS_FIXED_SIZE;
6c8c4f72 1210 memcpy(profile->bssid, params->bssid, ETH_ALEN);
5b435de0 1211 } else {
ba40d166 1212 memset(join_params.params_le.bssid, 0xFF, ETH_ALEN);
6c8c4f72 1213 memset(profile->bssid, 0, ETH_ALEN);
5b435de0
AS
1214 }
1215
5b435de0 1216 /* Channel */
683b6d3b 1217 if (params->chandef.chan) {
5b435de0
AS
1218 u32 target_channel;
1219
27a68fe3 1220 cfg->channel =
5b435de0 1221 ieee80211_frequency_to_channel(
683b6d3b 1222 params->chandef.chan->center_freq);
5b435de0
AS
1223 if (params->channel_fixed) {
1224 /* adding chanspec */
83cf17aa
FL
1225 chanspec = channel_to_chanspec(&cfg->d11inf,
1226 params->chandef.chan);
1701261d
HM
1227 join_params.params_le.chanspec_list[0] =
1228 cpu_to_le16(chanspec);
1229 join_params.params_le.chanspec_num = cpu_to_le32(1);
1230 join_params_size += sizeof(join_params.params_le);
5b435de0
AS
1231 }
1232
1233 /* set channel for starter */
27a68fe3 1234 target_channel = cfg->channel;
b87e2c48 1235 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_CHANNEL,
81f5dcb8 1236 target_channel);
5b435de0 1237 if (err) {
57d6e91a 1238 brcmf_err("WLC_SET_CHANNEL failed (%d)\n", err);
5b435de0
AS
1239 goto done;
1240 }
1241 } else
27a68fe3 1242 cfg->channel = 0;
5b435de0 1243
27a68fe3 1244 cfg->ibss_starter = false;
5b435de0
AS
1245
1246
c1179033 1247 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
81f5dcb8 1248 &join_params, join_params_size);
5b435de0 1249 if (err) {
57d6e91a 1250 brcmf_err("WLC_SET_SSID failed (%d)\n", err);
5b435de0
AS
1251 goto done;
1252 }
1253
1254done:
1255 if (err)
c1179033 1256 clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
d96b801f 1257 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1258 return err;
1259}
1260
1261static s32
1262brcmf_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *ndev)
1263{
0abb5f21 1264 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
1265 s32 err = 0;
1266
d96b801f 1267 brcmf_dbg(TRACE, "Enter\n");
ce81e317 1268 if (!check_vif_up(ifp->vif))
5b435de0
AS
1269 return -EIO;
1270
903e0eee 1271 brcmf_link_down(ifp->vif);
5b435de0 1272
d96b801f 1273 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1274
1275 return err;
1276}
1277
1278static s32 brcmf_set_wpa_version(struct net_device *ndev,
1279 struct cfg80211_connect_params *sme)
1280{
6ac4f4ed 1281 struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
5b435de0
AS
1282 struct brcmf_cfg80211_security *sec;
1283 s32 val = 0;
1284 s32 err = 0;
1285
1286 if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_1)
1287 val = WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED;
1288 else if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2)
1289 val = WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED;
1290 else
1291 val = WPA_AUTH_DISABLED;
16886735 1292 brcmf_dbg(CONN, "setting wpa_auth to 0x%0x\n", val);
89286dc9 1293 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "wpa_auth", val);
5b435de0 1294 if (err) {
57d6e91a 1295 brcmf_err("set wpa_auth failed (%d)\n", err);
5b435de0
AS
1296 return err;
1297 }
06bb123e 1298 sec = &profile->sec;
5b435de0
AS
1299 sec->wpa_versions = sme->crypto.wpa_versions;
1300 return err;
1301}
1302
1303static s32 brcmf_set_auth_type(struct net_device *ndev,
1304 struct cfg80211_connect_params *sme)
1305{
6ac4f4ed 1306 struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
5b435de0
AS
1307 struct brcmf_cfg80211_security *sec;
1308 s32 val = 0;
1309 s32 err = 0;
1310
1311 switch (sme->auth_type) {
1312 case NL80211_AUTHTYPE_OPEN_SYSTEM:
1313 val = 0;
16886735 1314 brcmf_dbg(CONN, "open system\n");
5b435de0
AS
1315 break;
1316 case NL80211_AUTHTYPE_SHARED_KEY:
1317 val = 1;
16886735 1318 brcmf_dbg(CONN, "shared key\n");
5b435de0
AS
1319 break;
1320 case NL80211_AUTHTYPE_AUTOMATIC:
1321 val = 2;
16886735 1322 brcmf_dbg(CONN, "automatic\n");
5b435de0
AS
1323 break;
1324 case NL80211_AUTHTYPE_NETWORK_EAP:
16886735 1325 brcmf_dbg(CONN, "network eap\n");
5b435de0
AS
1326 default:
1327 val = 2;
57d6e91a 1328 brcmf_err("invalid auth type (%d)\n", sme->auth_type);
5b435de0
AS
1329 break;
1330 }
1331
89286dc9 1332 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "auth", val);
5b435de0 1333 if (err) {
57d6e91a 1334 brcmf_err("set auth failed (%d)\n", err);
5b435de0
AS
1335 return err;
1336 }
06bb123e 1337 sec = &profile->sec;
5b435de0
AS
1338 sec->auth_type = sme->auth_type;
1339 return err;
1340}
1341
1342static s32
1343brcmf_set_set_cipher(struct net_device *ndev,
1344 struct cfg80211_connect_params *sme)
1345{
6ac4f4ed 1346 struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
5b435de0
AS
1347 struct brcmf_cfg80211_security *sec;
1348 s32 pval = 0;
1349 s32 gval = 0;
1350 s32 err = 0;
1351
1352 if (sme->crypto.n_ciphers_pairwise) {
1353 switch (sme->crypto.ciphers_pairwise[0]) {
1354 case WLAN_CIPHER_SUITE_WEP40:
1355 case WLAN_CIPHER_SUITE_WEP104:
1356 pval = WEP_ENABLED;
1357 break;
1358 case WLAN_CIPHER_SUITE_TKIP:
1359 pval = TKIP_ENABLED;
1360 break;
1361 case WLAN_CIPHER_SUITE_CCMP:
1362 pval = AES_ENABLED;
1363 break;
1364 case WLAN_CIPHER_SUITE_AES_CMAC:
1365 pval = AES_ENABLED;
1366 break;
1367 default:
57d6e91a
AS
1368 brcmf_err("invalid cipher pairwise (%d)\n",
1369 sme->crypto.ciphers_pairwise[0]);
5b435de0
AS
1370 return -EINVAL;
1371 }
1372 }
1373 if (sme->crypto.cipher_group) {
1374 switch (sme->crypto.cipher_group) {
1375 case WLAN_CIPHER_SUITE_WEP40:
1376 case WLAN_CIPHER_SUITE_WEP104:
1377 gval = WEP_ENABLED;
1378 break;
1379 case WLAN_CIPHER_SUITE_TKIP:
1380 gval = TKIP_ENABLED;
1381 break;
1382 case WLAN_CIPHER_SUITE_CCMP:
1383 gval = AES_ENABLED;
1384 break;
1385 case WLAN_CIPHER_SUITE_AES_CMAC:
1386 gval = AES_ENABLED;
1387 break;
1388 default:
57d6e91a
AS
1389 brcmf_err("invalid cipher group (%d)\n",
1390 sme->crypto.cipher_group);
5b435de0
AS
1391 return -EINVAL;
1392 }
1393 }
1394
16886735 1395 brcmf_dbg(CONN, "pval (%d) gval (%d)\n", pval, gval);
89286dc9
HM
1396 /* In case of privacy, but no security and WPS then simulate */
1397 /* setting AES. WPS-2.0 allows no security */
1398 if (brcmf_find_wpsie(sme->ie, sme->ie_len) && !pval && !gval &&
1399 sme->privacy)
1400 pval = AES_ENABLED;
1401 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "wsec", pval | gval);
5b435de0 1402 if (err) {
57d6e91a 1403 brcmf_err("error (%d)\n", err);
5b435de0
AS
1404 return err;
1405 }
1406
06bb123e 1407 sec = &profile->sec;
5b435de0
AS
1408 sec->cipher_pairwise = sme->crypto.ciphers_pairwise[0];
1409 sec->cipher_group = sme->crypto.cipher_group;
1410
1411 return err;
1412}
1413
1414static s32
1415brcmf_set_key_mgmt(struct net_device *ndev, struct cfg80211_connect_params *sme)
1416{
6ac4f4ed 1417 struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
5b435de0
AS
1418 struct brcmf_cfg80211_security *sec;
1419 s32 val = 0;
1420 s32 err = 0;
1421
1422 if (sme->crypto.n_akm_suites) {
89286dc9
HM
1423 err = brcmf_fil_bsscfg_int_get(netdev_priv(ndev),
1424 "wpa_auth", &val);
5b435de0 1425 if (err) {
57d6e91a 1426 brcmf_err("could not get wpa_auth (%d)\n", err);
5b435de0
AS
1427 return err;
1428 }
1429 if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED)) {
1430 switch (sme->crypto.akm_suites[0]) {
1431 case WLAN_AKM_SUITE_8021X:
1432 val = WPA_AUTH_UNSPECIFIED;
1433 break;
1434 case WLAN_AKM_SUITE_PSK:
1435 val = WPA_AUTH_PSK;
1436 break;
1437 default:
57d6e91a
AS
1438 brcmf_err("invalid cipher group (%d)\n",
1439 sme->crypto.cipher_group);
5b435de0
AS
1440 return -EINVAL;
1441 }
1442 } else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED)) {
1443 switch (sme->crypto.akm_suites[0]) {
1444 case WLAN_AKM_SUITE_8021X:
1445 val = WPA2_AUTH_UNSPECIFIED;
1446 break;
1447 case WLAN_AKM_SUITE_PSK:
1448 val = WPA2_AUTH_PSK;
1449 break;
1450 default:
57d6e91a
AS
1451 brcmf_err("invalid cipher group (%d)\n",
1452 sme->crypto.cipher_group);
5b435de0
AS
1453 return -EINVAL;
1454 }
1455 }
1456
16886735 1457 brcmf_dbg(CONN, "setting wpa_auth to %d\n", val);
89286dc9
HM
1458 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev),
1459 "wpa_auth", val);
5b435de0 1460 if (err) {
57d6e91a 1461 brcmf_err("could not set wpa_auth (%d)\n", err);
5b435de0
AS
1462 return err;
1463 }
1464 }
06bb123e 1465 sec = &profile->sec;
5b435de0
AS
1466 sec->wpa_auth = sme->crypto.akm_suites[0];
1467
1468 return err;
1469}
1470
1471static s32
f09d0c02
HM
1472brcmf_set_sharedkey(struct net_device *ndev,
1473 struct cfg80211_connect_params *sme)
5b435de0 1474{
6ac4f4ed 1475 struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
5b435de0
AS
1476 struct brcmf_cfg80211_security *sec;
1477 struct brcmf_wsec_key key;
1478 s32 val;
1479 s32 err = 0;
1480
16886735 1481 brcmf_dbg(CONN, "key len (%d)\n", sme->key_len);
5b435de0 1482
a718e2fe
RV
1483 if (sme->key_len == 0)
1484 return 0;
1485
06bb123e 1486 sec = &profile->sec;
16886735
AS
1487 brcmf_dbg(CONN, "wpa_versions 0x%x cipher_pairwise 0x%x\n",
1488 sec->wpa_versions, sec->cipher_pairwise);
a718e2fe
RV
1489
1490 if (sec->wpa_versions & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2))
1491 return 0;
1492
f09d0c02
HM
1493 if (!(sec->cipher_pairwise &
1494 (WLAN_CIPHER_SUITE_WEP40 | WLAN_CIPHER_SUITE_WEP104)))
1495 return 0;
a718e2fe 1496
f09d0c02
HM
1497 memset(&key, 0, sizeof(key));
1498 key.len = (u32) sme->key_len;
1499 key.index = (u32) sme->key_idx;
1500 if (key.len > sizeof(key.data)) {
57d6e91a 1501 brcmf_err("Too long key length (%u)\n", key.len);
f09d0c02
HM
1502 return -EINVAL;
1503 }
1504 memcpy(key.data, sme->key, key.len);
1505 key.flags = BRCMF_PRIMARY_KEY;
1506 switch (sec->cipher_pairwise) {
1507 case WLAN_CIPHER_SUITE_WEP40:
1508 key.algo = CRYPTO_ALGO_WEP1;
1509 break;
1510 case WLAN_CIPHER_SUITE_WEP104:
1511 key.algo = CRYPTO_ALGO_WEP128;
1512 break;
1513 default:
57d6e91a
AS
1514 brcmf_err("Invalid algorithm (%d)\n",
1515 sme->crypto.ciphers_pairwise[0]);
f09d0c02
HM
1516 return -EINVAL;
1517 }
1518 /* Set the new key/index */
16886735
AS
1519 brcmf_dbg(CONN, "key length (%d) key index (%d) algo (%d)\n",
1520 key.len, key.index, key.algo);
1521 brcmf_dbg(CONN, "key \"%s\"\n", key.data);
2eaba7e8 1522 err = send_key_to_dongle(ndev, &key);
f09d0c02
HM
1523 if (err)
1524 return err;
1525
1526 if (sec->auth_type == NL80211_AUTHTYPE_SHARED_KEY) {
16886735 1527 brcmf_dbg(CONN, "set auth_type to shared key\n");
f09d0c02 1528 val = WL_AUTH_SHARED_KEY; /* shared key */
ac24be6f 1529 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "auth", val);
f09d0c02 1530 if (err)
57d6e91a 1531 brcmf_err("set auth failed (%d)\n", err);
5b435de0
AS
1532 }
1533 return err;
1534}
1535
cbb1ec94
AS
1536static
1537enum nl80211_auth_type brcmf_war_auth_type(struct brcmf_if *ifp,
1538 enum nl80211_auth_type type)
1539{
1540 u32 ci;
1541 if (type == NL80211_AUTHTYPE_AUTOMATIC) {
1542 /* shift to ignore chip revision */
1543 ci = brcmf_get_chip_info(ifp) >> 4;
1544 switch (ci) {
1545 case 43236:
1546 brcmf_dbg(CONN, "43236 WAR: use OPEN instead of AUTO\n");
1547 return NL80211_AUTHTYPE_OPEN_SYSTEM;
1548 default:
1549 break;
1550 }
1551 }
1552 return type;
1553}
1554
5b435de0
AS
1555static s32
1556brcmf_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev,
cbb1ec94 1557 struct cfg80211_connect_params *sme)
5b435de0 1558{
27a68fe3 1559 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21
AS
1560 struct brcmf_if *ifp = netdev_priv(ndev);
1561 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5b435de0
AS
1562 struct ieee80211_channel *chan = sme->channel;
1563 struct brcmf_join_params join_params;
1564 size_t join_params_size;
89286dc9
HM
1565 struct brcmf_tlv *rsn_ie;
1566 struct brcmf_vs_tlv *wpa_ie;
1567 void *ie;
1568 u32 ie_len;
1569 struct brcmf_ext_join_params_le *ext_join_params;
1701261d 1570 u16 chanspec;
5b435de0
AS
1571
1572 s32 err = 0;
1573
d96b801f 1574 brcmf_dbg(TRACE, "Enter\n");
ce81e317 1575 if (!check_vif_up(ifp->vif))
5b435de0
AS
1576 return -EIO;
1577
1578 if (!sme->ssid) {
57d6e91a 1579 brcmf_err("Invalid ssid\n");
5b435de0
AS
1580 return -EOPNOTSUPP;
1581 }
1582
89286dc9
HM
1583 if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif) {
1584 /* A normal (non P2P) connection request setup. */
1585 ie = NULL;
1586 ie_len = 0;
1587 /* find the WPA_IE */
1588 wpa_ie = brcmf_find_wpaie((u8 *)sme->ie, sme->ie_len);
1589 if (wpa_ie) {
1590 ie = wpa_ie;
1591 ie_len = wpa_ie->len + TLV_HDR_LEN;
1592 } else {
1593 /* find the RSN_IE */
1594 rsn_ie = brcmf_parse_tlvs((u8 *)sme->ie, sme->ie_len,
1595 WLAN_EID_RSN);
1596 if (rsn_ie) {
1597 ie = rsn_ie;
1598 ie_len = rsn_ie->len + TLV_HDR_LEN;
1599 }
1600 }
1601 brcmf_fil_iovar_data_set(ifp, "wpaie", ie, ie_len);
1602 }
1603
1604 err = brcmf_vif_set_mgmt_ie(ifp->vif, BRCMF_VNDR_IE_ASSOCREQ_FLAG,
1605 sme->ie, sme->ie_len);
1606 if (err)
1607 brcmf_err("Set Assoc REQ IE Failed\n");
1608 else
1609 brcmf_dbg(TRACE, "Applied Vndr IEs for Assoc request\n");
1610
c1179033 1611 set_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
5b435de0
AS
1612
1613 if (chan) {
27a68fe3 1614 cfg->channel =
5b435de0 1615 ieee80211_frequency_to_channel(chan->center_freq);
83cf17aa 1616 chanspec = channel_to_chanspec(&cfg->d11inf, chan);
1701261d
HM
1617 brcmf_dbg(CONN, "channel=%d, center_req=%d, chanspec=0x%04x\n",
1618 cfg->channel, chan->center_freq, chanspec);
1619 } else {
27a68fe3 1620 cfg->channel = 0;
1701261d
HM
1621 chanspec = 0;
1622 }
5b435de0 1623
647c9ae0 1624 brcmf_dbg(INFO, "ie (%p), ie_len (%zd)\n", sme->ie, sme->ie_len);
5b435de0
AS
1625
1626 err = brcmf_set_wpa_version(ndev, sme);
1627 if (err) {
57d6e91a 1628 brcmf_err("wl_set_wpa_version failed (%d)\n", err);
5b435de0
AS
1629 goto done;
1630 }
1631
cbb1ec94 1632 sme->auth_type = brcmf_war_auth_type(ifp, sme->auth_type);
5b435de0
AS
1633 err = brcmf_set_auth_type(ndev, sme);
1634 if (err) {
57d6e91a 1635 brcmf_err("wl_set_auth_type failed (%d)\n", err);
5b435de0
AS
1636 goto done;
1637 }
1638
1639 err = brcmf_set_set_cipher(ndev, sme);
1640 if (err) {
57d6e91a 1641 brcmf_err("wl_set_set_cipher failed (%d)\n", err);
5b435de0
AS
1642 goto done;
1643 }
1644
1645 err = brcmf_set_key_mgmt(ndev, sme);
1646 if (err) {
57d6e91a 1647 brcmf_err("wl_set_key_mgmt failed (%d)\n", err);
5b435de0
AS
1648 goto done;
1649 }
1650
f09d0c02 1651 err = brcmf_set_sharedkey(ndev, sme);
5b435de0 1652 if (err) {
57d6e91a 1653 brcmf_err("brcmf_set_sharedkey failed (%d)\n", err);
5b435de0
AS
1654 goto done;
1655 }
1656
89286dc9
HM
1657 profile->ssid.SSID_len = min_t(u32, (u32)sizeof(profile->ssid.SSID),
1658 (u32)sme->ssid_len);
1659 memcpy(&profile->ssid.SSID, sme->ssid, profile->ssid.SSID_len);
1660 if (profile->ssid.SSID_len < IEEE80211_MAX_SSID_LEN) {
1661 profile->ssid.SSID[profile->ssid.SSID_len] = 0;
1662 brcmf_dbg(CONN, "SSID \"%s\", len (%d)\n", profile->ssid.SSID,
1663 profile->ssid.SSID_len);
1664 }
1665
1666 /* Join with specific BSSID and cached SSID
1667 * If SSID is zero join based on BSSID only
1668 */
1669 join_params_size = offsetof(struct brcmf_ext_join_params_le, assoc_le) +
1670 offsetof(struct brcmf_assoc_params_le, chanspec_list);
1671 if (cfg->channel)
1672 join_params_size += sizeof(u16);
1673 ext_join_params = kzalloc(join_params_size, GFP_KERNEL);
1674 if (ext_join_params == NULL) {
1675 err = -ENOMEM;
1676 goto done;
1677 }
1678 ext_join_params->ssid_le.SSID_len = cpu_to_le32(profile->ssid.SSID_len);
1679 memcpy(&ext_join_params->ssid_le.SSID, sme->ssid,
1680 profile->ssid.SSID_len);
1681 /*increase dwell time to receive probe response or detect Beacon
1682 * from target AP at a noisy air only during connect command
1683 */
1684 ext_join_params->scan_le.active_time =
1685 cpu_to_le32(BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS);
1686 ext_join_params->scan_le.passive_time =
1687 cpu_to_le32(BRCMF_SCAN_JOIN_PASSIVE_DWELL_TIME_MS);
1688 /* Set up join scan parameters */
1689 ext_join_params->scan_le.scan_type = -1;
1690 /* to sync with presence period of VSDB GO.
1691 * Send probe request more frequently. Probe request will be stopped
1692 * when it gets probe response from target AP/GO.
1693 */
1694 ext_join_params->scan_le.nprobes =
1695 cpu_to_le32(BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS /
1696 BRCMF_SCAN_JOIN_PROBE_INTERVAL_MS);
1697 ext_join_params->scan_le.home_time = cpu_to_le32(-1);
1698
1699 if (sme->bssid)
1700 memcpy(&ext_join_params->assoc_le.bssid, sme->bssid, ETH_ALEN);
1701 else
1702 memset(&ext_join_params->assoc_le.bssid, 0xFF, ETH_ALEN);
1703
1704 if (cfg->channel) {
1705 ext_join_params->assoc_le.chanspec_num = cpu_to_le32(1);
1706
1707 ext_join_params->assoc_le.chanspec_list[0] =
1708 cpu_to_le16(chanspec);
1709 }
1710
1711 err = brcmf_fil_bsscfg_data_set(ifp, "join", ext_join_params,
1712 join_params_size);
1713 kfree(ext_join_params);
1714 if (!err)
1715 /* This is it. join command worked, we are done */
1716 goto done;
1717
1718 /* join command failed, fallback to set ssid */
5b435de0
AS
1719 memset(&join_params, 0, sizeof(join_params));
1720 join_params_size = sizeof(join_params.ssid_le);
1721
6c8c4f72 1722 memcpy(&join_params.ssid_le.SSID, sme->ssid, profile->ssid.SSID_len);
6c8c4f72 1723 join_params.ssid_le.SSID_len = cpu_to_le32(profile->ssid.SSID_len);
5b435de0 1724
89286dc9
HM
1725 if (sme->bssid)
1726 memcpy(join_params.params_le.bssid, sme->bssid, ETH_ALEN);
1727 else
1728 memset(join_params.params_le.bssid, 0xFF, ETH_ALEN);
5b435de0 1729
1701261d
HM
1730 if (cfg->channel) {
1731 join_params.params_le.chanspec_list[0] = cpu_to_le16(chanspec);
1732 join_params.params_le.chanspec_num = cpu_to_le32(1);
1733 join_params_size += sizeof(join_params.params_le);
1734 }
c1179033 1735 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
81f5dcb8 1736 &join_params, join_params_size);
5b435de0 1737 if (err)
89286dc9 1738 brcmf_err("BRCMF_C_SET_SSID failed (%d)\n", err);
5b435de0
AS
1739
1740done:
1741 if (err)
c1179033 1742 clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
d96b801f 1743 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1744 return err;
1745}
1746
1747static s32
1748brcmf_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *ndev,
1749 u16 reason_code)
1750{
0abb5f21
AS
1751 struct brcmf_if *ifp = netdev_priv(ndev);
1752 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5b435de0
AS
1753 struct brcmf_scb_val_le scbval;
1754 s32 err = 0;
1755
d96b801f 1756 brcmf_dbg(TRACE, "Enter. Reason code = %d\n", reason_code);
ce81e317 1757 if (!check_vif_up(ifp->vif))
5b435de0
AS
1758 return -EIO;
1759
c1179033 1760 clear_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
43dffbc6 1761 cfg80211_disconnected(ndev, reason_code, NULL, 0, GFP_KERNEL);
5b435de0 1762
06bb123e 1763 memcpy(&scbval.ea, &profile->bssid, ETH_ALEN);
5b435de0 1764 scbval.val = cpu_to_le32(reason_code);
c1179033 1765 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC,
ac24be6f 1766 &scbval, sizeof(scbval));
5b435de0 1767 if (err)
57d6e91a 1768 brcmf_err("error (%d)\n", err);
5b435de0 1769
d96b801f 1770 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1771 return err;
1772}
1773
1774static s32
c8442118 1775brcmf_cfg80211_set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
d3f31134 1776 enum nl80211_tx_power_setting type, s32 mbm)
5b435de0
AS
1777{
1778
27a68fe3 1779 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21
AS
1780 struct net_device *ndev = cfg_to_ndev(cfg);
1781 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
1782 u16 txpwrmw;
1783 s32 err = 0;
1784 s32 disable = 0;
d3f31134 1785 s32 dbm = MBM_TO_DBM(mbm);
5b435de0 1786
d96b801f 1787 brcmf_dbg(TRACE, "Enter\n");
ce81e317 1788 if (!check_vif_up(ifp->vif))
5b435de0
AS
1789 return -EIO;
1790
1791 switch (type) {
1792 case NL80211_TX_POWER_AUTOMATIC:
1793 break;
1794 case NL80211_TX_POWER_LIMITED:
5b435de0
AS
1795 case NL80211_TX_POWER_FIXED:
1796 if (dbm < 0) {
57d6e91a 1797 brcmf_err("TX_POWER_FIXED - dbm is negative\n");
5b435de0
AS
1798 err = -EINVAL;
1799 goto done;
1800 }
1801 break;
1802 }
1803 /* Make sure radio is off or on as far as software is concerned */
1804 disable = WL_RADIO_SW_DISABLE << 16;
ac24be6f 1805 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_RADIO, disable);
5b435de0 1806 if (err)
57d6e91a 1807 brcmf_err("WLC_SET_RADIO error (%d)\n", err);
5b435de0
AS
1808
1809 if (dbm > 0xffff)
1810 txpwrmw = 0xffff;
1811 else
1812 txpwrmw = (u16) dbm;
ac24be6f
AS
1813 err = brcmf_fil_iovar_int_set(ifp, "qtxpower",
1814 (s32)brcmf_mw_to_qdbm(txpwrmw));
5b435de0 1815 if (err)
57d6e91a 1816 brcmf_err("qtxpower error (%d)\n", err);
27a68fe3 1817 cfg->conf->tx_power = dbm;
5b435de0
AS
1818
1819done:
d96b801f 1820 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1821 return err;
1822}
1823
c8442118
JB
1824static s32 brcmf_cfg80211_get_tx_power(struct wiphy *wiphy,
1825 struct wireless_dev *wdev,
1826 s32 *dbm)
5b435de0 1827{
27a68fe3 1828 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 1829 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5b435de0
AS
1830 s32 txpwrdbm;
1831 u8 result;
1832 s32 err = 0;
1833
d96b801f 1834 brcmf_dbg(TRACE, "Enter\n");
ce81e317 1835 if (!check_vif_up(ifp->vif))
5b435de0
AS
1836 return -EIO;
1837
0abb5f21 1838 err = brcmf_fil_iovar_int_get(ifp, "qtxpower", &txpwrdbm);
5b435de0 1839 if (err) {
57d6e91a 1840 brcmf_err("error (%d)\n", err);
5b435de0
AS
1841 goto done;
1842 }
1843
1844 result = (u8) (txpwrdbm & ~WL_TXPWR_OVERRIDE);
ef6ac17a 1845 *dbm = (s32) brcmf_qdbm_to_mw(result);
5b435de0
AS
1846
1847done:
d96b801f 1848 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1849 return err;
1850}
1851
1852static s32
1853brcmf_cfg80211_config_default_key(struct wiphy *wiphy, struct net_device *ndev,
1854 u8 key_idx, bool unicast, bool multicast)
1855{
0abb5f21 1856 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
1857 u32 index;
1858 u32 wsec;
1859 s32 err = 0;
1860
d96b801f 1861 brcmf_dbg(TRACE, "Enter\n");
16886735 1862 brcmf_dbg(CONN, "key index (%d)\n", key_idx);
ce81e317 1863 if (!check_vif_up(ifp->vif))
5b435de0
AS
1864 return -EIO;
1865
0abb5f21 1866 err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
5b435de0 1867 if (err) {
57d6e91a 1868 brcmf_err("WLC_GET_WSEC error (%d)\n", err);
5b435de0
AS
1869 goto done;
1870 }
1871
1872 if (wsec & WEP_ENABLED) {
1873 /* Just select a new current key */
1874 index = key_idx;
0abb5f21 1875 err = brcmf_fil_cmd_int_set(ifp,
ac24be6f 1876 BRCMF_C_SET_KEY_PRIMARY, index);
5b435de0 1877 if (err)
57d6e91a 1878 brcmf_err("error (%d)\n", err);
5b435de0
AS
1879 }
1880done:
d96b801f 1881 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
1882 return err;
1883}
1884
1885static s32
1886brcmf_add_keyext(struct wiphy *wiphy, struct net_device *ndev,
1887 u8 key_idx, const u8 *mac_addr, struct key_params *params)
1888{
992f6068 1889 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0 1890 struct brcmf_wsec_key key;
5b435de0 1891 s32 err = 0;
992f6068 1892 u8 keybuf[8];
5b435de0
AS
1893
1894 memset(&key, 0, sizeof(key));
1895 key.index = (u32) key_idx;
1896 /* Instead of bcast for ea address for default wep keys,
1897 driver needs it to be Null */
1898 if (!is_multicast_ether_addr(mac_addr))
1899 memcpy((char *)&key.ea, (void *)mac_addr, ETH_ALEN);
1900 key.len = (u32) params->key_len;
1901 /* check for key index change */
1902 if (key.len == 0) {
1903 /* key delete */
2eaba7e8 1904 err = send_key_to_dongle(ndev, &key);
5b435de0 1905 if (err)
57d6e91a 1906 brcmf_err("key delete error (%d)\n", err);
5b435de0
AS
1907 } else {
1908 if (key.len > sizeof(key.data)) {
57d6e91a 1909 brcmf_err("Invalid key length (%d)\n", key.len);
5b435de0
AS
1910 return -EINVAL;
1911 }
1912
16886735 1913 brcmf_dbg(CONN, "Setting the key index %d\n", key.index);
5b435de0
AS
1914 memcpy(key.data, params->key, key.len);
1915
992f6068
HM
1916 if ((ifp->vif->mode != WL_MODE_AP) &&
1917 (params->cipher == WLAN_CIPHER_SUITE_TKIP)) {
1918 brcmf_dbg(CONN, "Swapping RX/TX MIC key\n");
5b435de0
AS
1919 memcpy(keybuf, &key.data[24], sizeof(keybuf));
1920 memcpy(&key.data[24], &key.data[16], sizeof(keybuf));
1921 memcpy(&key.data[16], keybuf, sizeof(keybuf));
1922 }
1923
1924 /* if IW_ENCODE_EXT_RX_SEQ_VALID set */
1925 if (params->seq && params->seq_len == 6) {
1926 /* rx iv */
1927 u8 *ivptr;
1928 ivptr = (u8 *) params->seq;
1929 key.rxiv.hi = (ivptr[5] << 24) | (ivptr[4] << 16) |
1930 (ivptr[3] << 8) | ivptr[2];
1931 key.rxiv.lo = (ivptr[1] << 8) | ivptr[0];
1932 key.iv_initialized = true;
1933 }
1934
1935 switch (params->cipher) {
1936 case WLAN_CIPHER_SUITE_WEP40:
1937 key.algo = CRYPTO_ALGO_WEP1;
16886735 1938 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
5b435de0
AS
1939 break;
1940 case WLAN_CIPHER_SUITE_WEP104:
1941 key.algo = CRYPTO_ALGO_WEP128;
16886735 1942 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
5b435de0
AS
1943 break;
1944 case WLAN_CIPHER_SUITE_TKIP:
1945 key.algo = CRYPTO_ALGO_TKIP;
16886735 1946 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
5b435de0
AS
1947 break;
1948 case WLAN_CIPHER_SUITE_AES_CMAC:
1949 key.algo = CRYPTO_ALGO_AES_CCM;
16886735 1950 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
5b435de0
AS
1951 break;
1952 case WLAN_CIPHER_SUITE_CCMP:
1953 key.algo = CRYPTO_ALGO_AES_CCM;
16886735 1954 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_CCMP\n");
5b435de0
AS
1955 break;
1956 default:
57d6e91a 1957 brcmf_err("Invalid cipher (0x%x)\n", params->cipher);
5b435de0
AS
1958 return -EINVAL;
1959 }
2eaba7e8 1960 err = send_key_to_dongle(ndev, &key);
f09d0c02 1961 if (err)
57d6e91a 1962 brcmf_err("wsec_key error (%d)\n", err);
5b435de0
AS
1963 }
1964 return err;
1965}
1966
1967static s32
1968brcmf_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
1969 u8 key_idx, bool pairwise, const u8 *mac_addr,
1970 struct key_params *params)
1971{
0abb5f21 1972 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
1973 struct brcmf_wsec_key key;
1974 s32 val;
1975 s32 wsec;
1976 s32 err = 0;
1977 u8 keybuf[8];
1978
d96b801f 1979 brcmf_dbg(TRACE, "Enter\n");
16886735 1980 brcmf_dbg(CONN, "key index (%d)\n", key_idx);
ce81e317 1981 if (!check_vif_up(ifp->vif))
5b435de0
AS
1982 return -EIO;
1983
1984 if (mac_addr) {
d96b801f 1985 brcmf_dbg(TRACE, "Exit");
5b435de0
AS
1986 return brcmf_add_keyext(wiphy, ndev, key_idx, mac_addr, params);
1987 }
1988 memset(&key, 0, sizeof(key));
1989
1990 key.len = (u32) params->key_len;
1991 key.index = (u32) key_idx;
1992
1993 if (key.len > sizeof(key.data)) {
57d6e91a 1994 brcmf_err("Too long key length (%u)\n", key.len);
5b435de0
AS
1995 err = -EINVAL;
1996 goto done;
1997 }
1998 memcpy(key.data, params->key, key.len);
1999
2000 key.flags = BRCMF_PRIMARY_KEY;
2001 switch (params->cipher) {
2002 case WLAN_CIPHER_SUITE_WEP40:
2003 key.algo = CRYPTO_ALGO_WEP1;
f09d0c02 2004 val = WEP_ENABLED;
16886735 2005 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
5b435de0
AS
2006 break;
2007 case WLAN_CIPHER_SUITE_WEP104:
2008 key.algo = CRYPTO_ALGO_WEP128;
f09d0c02 2009 val = WEP_ENABLED;
16886735 2010 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
5b435de0
AS
2011 break;
2012 case WLAN_CIPHER_SUITE_TKIP:
128ce3b6 2013 if (ifp->vif->mode != WL_MODE_AP) {
992f6068 2014 brcmf_dbg(CONN, "Swapping RX/TX MIC key\n");
1a873342
HM
2015 memcpy(keybuf, &key.data[24], sizeof(keybuf));
2016 memcpy(&key.data[24], &key.data[16], sizeof(keybuf));
2017 memcpy(&key.data[16], keybuf, sizeof(keybuf));
2018 }
5b435de0 2019 key.algo = CRYPTO_ALGO_TKIP;
f09d0c02 2020 val = TKIP_ENABLED;
16886735 2021 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
5b435de0
AS
2022 break;
2023 case WLAN_CIPHER_SUITE_AES_CMAC:
2024 key.algo = CRYPTO_ALGO_AES_CCM;
f09d0c02 2025 val = AES_ENABLED;
16886735 2026 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
5b435de0
AS
2027 break;
2028 case WLAN_CIPHER_SUITE_CCMP:
2029 key.algo = CRYPTO_ALGO_AES_CCM;
f09d0c02 2030 val = AES_ENABLED;
16886735 2031 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_CCMP\n");
5b435de0
AS
2032 break;
2033 default:
57d6e91a 2034 brcmf_err("Invalid cipher (0x%x)\n", params->cipher);
5b435de0
AS
2035 err = -EINVAL;
2036 goto done;
2037 }
2038
2eaba7e8 2039 err = send_key_to_dongle(ndev, &key);
5b435de0
AS
2040 if (err)
2041 goto done;
2042
0abb5f21 2043 err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
5b435de0 2044 if (err) {
57d6e91a 2045 brcmf_err("get wsec error (%d)\n", err);
5b435de0
AS
2046 goto done;
2047 }
5b435de0 2048 wsec |= val;
0abb5f21 2049 err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
5b435de0 2050 if (err) {
57d6e91a 2051 brcmf_err("set wsec error (%d)\n", err);
5b435de0
AS
2052 goto done;
2053 }
2054
5b435de0 2055done:
d96b801f 2056 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2057 return err;
2058}
2059
2060static s32
2061brcmf_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
2062 u8 key_idx, bool pairwise, const u8 *mac_addr)
2063{
0abb5f21 2064 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
2065 struct brcmf_wsec_key key;
2066 s32 err = 0;
5b435de0 2067
d96b801f 2068 brcmf_dbg(TRACE, "Enter\n");
ce81e317 2069 if (!check_vif_up(ifp->vif))
5b435de0
AS
2070 return -EIO;
2071
256c374f
HM
2072 if (key_idx >= DOT11_MAX_DEFAULT_KEYS) {
2073 /* we ignore this key index in this case */
57d6e91a 2074 brcmf_err("invalid key index (%d)\n", key_idx);
256c374f
HM
2075 return -EINVAL;
2076 }
2077
5b435de0
AS
2078 memset(&key, 0, sizeof(key));
2079
2080 key.index = (u32) key_idx;
2081 key.flags = BRCMF_PRIMARY_KEY;
2082 key.algo = CRYPTO_ALGO_OFF;
2083
16886735 2084 brcmf_dbg(CONN, "key index (%d)\n", key_idx);
5b435de0
AS
2085
2086 /* Set the new key/index */
2eaba7e8 2087 err = send_key_to_dongle(ndev, &key);
5b435de0 2088
d96b801f 2089 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2090 return err;
2091}
2092
2093static s32
2094brcmf_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
2095 u8 key_idx, bool pairwise, const u8 *mac_addr, void *cookie,
2096 void (*callback) (void *cookie, struct key_params * params))
2097{
2098 struct key_params params;
0abb5f21
AS
2099 struct brcmf_if *ifp = netdev_priv(ndev);
2100 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5b435de0
AS
2101 struct brcmf_cfg80211_security *sec;
2102 s32 wsec;
2103 s32 err = 0;
2104
d96b801f 2105 brcmf_dbg(TRACE, "Enter\n");
16886735 2106 brcmf_dbg(CONN, "key index (%d)\n", key_idx);
ce81e317 2107 if (!check_vif_up(ifp->vif))
5b435de0
AS
2108 return -EIO;
2109
2110 memset(&params, 0, sizeof(params));
2111
0abb5f21 2112 err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
5b435de0 2113 if (err) {
57d6e91a 2114 brcmf_err("WLC_GET_WSEC error (%d)\n", err);
5b435de0
AS
2115 /* Ignore this error, may happen during DISASSOC */
2116 err = -EAGAIN;
2117 goto done;
2118 }
c5bf53a8 2119 if (wsec & WEP_ENABLED) {
06bb123e 2120 sec = &profile->sec;
5b435de0
AS
2121 if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP40) {
2122 params.cipher = WLAN_CIPHER_SUITE_WEP40;
16886735 2123 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
5b435de0
AS
2124 } else if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP104) {
2125 params.cipher = WLAN_CIPHER_SUITE_WEP104;
16886735 2126 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
5b435de0 2127 }
c5bf53a8 2128 } else if (wsec & TKIP_ENABLED) {
5b435de0 2129 params.cipher = WLAN_CIPHER_SUITE_TKIP;
16886735 2130 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
c5bf53a8 2131 } else if (wsec & AES_ENABLED) {
5b435de0 2132 params.cipher = WLAN_CIPHER_SUITE_AES_CMAC;
16886735 2133 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
c5bf53a8 2134 } else {
57d6e91a 2135 brcmf_err("Invalid algo (0x%x)\n", wsec);
5b435de0
AS
2136 err = -EINVAL;
2137 goto done;
2138 }
2139 callback(cookie, &params);
2140
2141done:
d96b801f 2142 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2143 return err;
2144}
2145
2146static s32
2147brcmf_cfg80211_config_default_mgmt_key(struct wiphy *wiphy,
2148 struct net_device *ndev, u8 key_idx)
2149{
647c9ae0 2150 brcmf_dbg(INFO, "Not supported\n");
5b435de0
AS
2151
2152 return -EOPNOTSUPP;
2153}
2154
2155static s32
2156brcmf_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev,
1a873342 2157 u8 *mac, struct station_info *sinfo)
5b435de0 2158{
0abb5f21
AS
2159 struct brcmf_if *ifp = netdev_priv(ndev);
2160 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5b435de0
AS
2161 struct brcmf_scb_val_le scb_val;
2162 int rssi;
2163 s32 rate;
2164 s32 err = 0;
06bb123e 2165 u8 *bssid = profile->bssid;
81f5dcb8 2166 struct brcmf_sta_info_le sta_info_le;
5b435de0 2167
d96b801f 2168 brcmf_dbg(TRACE, "Enter, MAC %pM\n", mac);
ce81e317 2169 if (!check_vif_up(ifp->vif))
5b435de0
AS
2170 return -EIO;
2171
128ce3b6 2172 if (ifp->vif->mode == WL_MODE_AP) {
81f5dcb8 2173 memcpy(&sta_info_le, mac, ETH_ALEN);
0abb5f21 2174 err = brcmf_fil_iovar_data_get(ifp, "sta_info",
ac24be6f 2175 &sta_info_le,
81f5dcb8 2176 sizeof(sta_info_le));
1a873342 2177 if (err < 0) {
57d6e91a 2178 brcmf_err("GET STA INFO failed, %d\n", err);
1a873342
HM
2179 goto done;
2180 }
1a873342 2181 sinfo->filled = STATION_INFO_INACTIVE_TIME;
81f5dcb8
HM
2182 sinfo->inactive_time = le32_to_cpu(sta_info_le.idle) * 1000;
2183 if (le32_to_cpu(sta_info_le.flags) & BRCMF_STA_ASSOC) {
1a873342 2184 sinfo->filled |= STATION_INFO_CONNECTED_TIME;
81f5dcb8 2185 sinfo->connected_time = le32_to_cpu(sta_info_le.in);
1a873342 2186 }
d96b801f
AS
2187 brcmf_dbg(TRACE, "STA idle time : %d ms, connected time :%d sec\n",
2188 sinfo->inactive_time, sinfo->connected_time);
128ce3b6 2189 } else if (ifp->vif->mode == WL_MODE_BSS) {
1a873342 2190 if (memcmp(mac, bssid, ETH_ALEN)) {
57d6e91a
AS
2191 brcmf_err("Wrong Mac address cfg_mac-%pM wl_bssid-%pM\n",
2192 mac, bssid);
1a873342
HM
2193 err = -ENOENT;
2194 goto done;
2195 }
2196 /* Report the current tx rate */
89286dc9 2197 err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_RATE, &rate);
7f6c562d 2198 if (err) {
57d6e91a 2199 brcmf_err("Could not get rate (%d)\n", err);
1a873342 2200 goto done;
7f6c562d 2201 } else {
1a873342
HM
2202 sinfo->filled |= STATION_INFO_TX_BITRATE;
2203 sinfo->txrate.legacy = rate * 5;
16886735 2204 brcmf_dbg(CONN, "Rate %d Mbps\n", rate / 2);
7f6c562d 2205 }
5b435de0 2206
c1179033
AS
2207 if (test_bit(BRCMF_VIF_STATUS_CONNECTED,
2208 &ifp->vif->sme_state)) {
1a873342 2209 memset(&scb_val, 0, sizeof(scb_val));
c1179033
AS
2210 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_RSSI,
2211 &scb_val, sizeof(scb_val));
1a873342 2212 if (err) {
57d6e91a 2213 brcmf_err("Could not get rssi (%d)\n", err);
1a873342
HM
2214 goto done;
2215 } else {
2216 rssi = le32_to_cpu(scb_val.val);
2217 sinfo->filled |= STATION_INFO_SIGNAL;
2218 sinfo->signal = rssi;
16886735 2219 brcmf_dbg(CONN, "RSSI %d dBm\n", rssi);
1a873342
HM
2220 }
2221 }
2222 } else
2223 err = -EPERM;
5b435de0 2224done:
d96b801f 2225 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2226 return err;
2227}
2228
2229static s32
2230brcmf_cfg80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *ndev,
2231 bool enabled, s32 timeout)
2232{
2233 s32 pm;
2234 s32 err = 0;
27a68fe3 2235 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
c1179033 2236 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0 2237
d96b801f 2238 brcmf_dbg(TRACE, "Enter\n");
5b435de0
AS
2239
2240 /*
2241 * Powersave enable/disable request is coming from the
2242 * cfg80211 even before the interface is up. In that
2243 * scenario, driver will be storing the power save
27a68fe3 2244 * preference in cfg struct to apply this to
5b435de0
AS
2245 * FW later while initializing the dongle
2246 */
27a68fe3 2247 cfg->pwr_save = enabled;
ce81e317 2248 if (!check_vif_up(ifp->vif)) {
5b435de0 2249
647c9ae0 2250 brcmf_dbg(INFO, "Device is not ready, storing the value in cfg_info struct\n");
5b435de0
AS
2251 goto done;
2252 }
2253
2254 pm = enabled ? PM_FAST : PM_OFF;
102fd0d6
HM
2255 /* Do not enable the power save after assoc if it is a p2p interface */
2256 if (ifp->vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT) {
2257 brcmf_dbg(INFO, "Do not enable power save for P2P clients\n");
2258 pm = PM_OFF;
2259 }
647c9ae0 2260 brcmf_dbg(INFO, "power save %s\n", (pm ? "enabled" : "disabled"));
5b435de0 2261
c1179033 2262 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, pm);
5b435de0
AS
2263 if (err) {
2264 if (err == -ENODEV)
57d6e91a 2265 brcmf_err("net_device is not ready yet\n");
5b435de0 2266 else
57d6e91a 2267 brcmf_err("error (%d)\n", err);
5b435de0
AS
2268 }
2269done:
d96b801f 2270 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2271 return err;
2272}
2273
27a68fe3 2274static s32 brcmf_inform_single_bss(struct brcmf_cfg80211_info *cfg,
d34bf64f 2275 struct brcmf_bss_info_le *bi)
5b435de0 2276{
27a68fe3 2277 struct wiphy *wiphy = cfg_to_wiphy(cfg);
5b435de0
AS
2278 struct ieee80211_channel *notify_channel;
2279 struct cfg80211_bss *bss;
2280 struct ieee80211_supported_band *band;
83cf17aa 2281 struct brcmu_chan ch;
5b435de0
AS
2282 s32 err = 0;
2283 u16 channel;
2284 u32 freq;
5b435de0
AS
2285 u16 notify_capability;
2286 u16 notify_interval;
2287 u8 *notify_ie;
2288 size_t notify_ielen;
2289 s32 notify_signal;
2290
2291 if (le32_to_cpu(bi->length) > WL_BSS_INFO_MAX) {
57d6e91a 2292 brcmf_err("Bss info is larger than buffer. Discarding\n");
5b435de0
AS
2293 return 0;
2294 }
2295
83cf17aa
FL
2296 if (!bi->ctl_ch) {
2297 ch.chspec = le16_to_cpu(bi->chanspec);
2298 cfg->d11inf.decchspec(&ch);
2299 bi->ctl_ch = ch.chnum;
2300 }
2301 channel = bi->ctl_ch;
5b435de0
AS
2302
2303 if (channel <= CH_MAX_2G_CHANNEL)
2304 band = wiphy->bands[IEEE80211_BAND_2GHZ];
2305 else
2306 band = wiphy->bands[IEEE80211_BAND_5GHZ];
2307
2308 freq = ieee80211_channel_to_frequency(channel, band->band);
2309 notify_channel = ieee80211_get_channel(wiphy, freq);
2310
5b435de0
AS
2311 notify_capability = le16_to_cpu(bi->capability);
2312 notify_interval = le16_to_cpu(bi->beacon_period);
2313 notify_ie = (u8 *)bi + le16_to_cpu(bi->ie_offset);
2314 notify_ielen = le32_to_cpu(bi->ie_length);
2315 notify_signal = (s16)le16_to_cpu(bi->RSSI) * 100;
2316
16886735
AS
2317 brcmf_dbg(CONN, "bssid: %pM\n", bi->BSSID);
2318 brcmf_dbg(CONN, "Channel: %d(%d)\n", channel, freq);
2319 brcmf_dbg(CONN, "Capability: %X\n", notify_capability);
2320 brcmf_dbg(CONN, "Beacon interval: %d\n", notify_interval);
2321 brcmf_dbg(CONN, "Signal: %d\n", notify_signal);
5b435de0
AS
2322
2323 bss = cfg80211_inform_bss(wiphy, notify_channel, (const u8 *)bi->BSSID,
8e6cffb3 2324 0, notify_capability, notify_interval, notify_ie,
5b435de0
AS
2325 notify_ielen, notify_signal, GFP_KERNEL);
2326
e78946e1
FL
2327 if (!bss)
2328 return -ENOMEM;
2329
5b112d3d 2330 cfg80211_put_bss(wiphy, bss);
5b435de0
AS
2331
2332 return err;
2333}
2334
6f09be0a
RV
2335static struct brcmf_bss_info_le *
2336next_bss_le(struct brcmf_scan_results *list, struct brcmf_bss_info_le *bss)
2337{
2338 if (bss == NULL)
2339 return list->bss_info_le;
2340 return (struct brcmf_bss_info_le *)((unsigned long)bss +
2341 le32_to_cpu(bss->length));
2342}
2343
27a68fe3 2344static s32 brcmf_inform_bss(struct brcmf_cfg80211_info *cfg)
5b435de0
AS
2345{
2346 struct brcmf_scan_results *bss_list;
d34bf64f 2347 struct brcmf_bss_info_le *bi = NULL; /* must be initialized */
5b435de0
AS
2348 s32 err = 0;
2349 int i;
2350
27a68fe3 2351 bss_list = cfg->bss_list;
0ecd8164
AS
2352 if (bss_list->count != 0 &&
2353 bss_list->version != BRCMF_BSS_INFO_VERSION) {
57d6e91a
AS
2354 brcmf_err("Version %d != WL_BSS_INFO_VERSION\n",
2355 bss_list->version);
5b435de0
AS
2356 return -EOPNOTSUPP;
2357 }
4e8a008e 2358 brcmf_dbg(SCAN, "scanned AP count (%d)\n", bss_list->count);
f0799895 2359 for (i = 0; i < bss_list->count; i++) {
6f09be0a 2360 bi = next_bss_le(bss_list, bi);
27a68fe3 2361 err = brcmf_inform_single_bss(cfg, bi);
5b435de0
AS
2362 if (err)
2363 break;
2364 }
2365 return err;
2366}
2367
27a68fe3 2368static s32 wl_inform_ibss(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
2369 struct net_device *ndev, const u8 *bssid)
2370{
27a68fe3 2371 struct wiphy *wiphy = cfg_to_wiphy(cfg);
5b435de0 2372 struct ieee80211_channel *notify_channel;
d34bf64f 2373 struct brcmf_bss_info_le *bi = NULL;
5b435de0 2374 struct ieee80211_supported_band *band;
e78946e1 2375 struct cfg80211_bss *bss;
83cf17aa 2376 struct brcmu_chan ch;
5b435de0
AS
2377 u8 *buf = NULL;
2378 s32 err = 0;
5b435de0 2379 u32 freq;
5b435de0
AS
2380 u16 notify_capability;
2381 u16 notify_interval;
2382 u8 *notify_ie;
2383 size_t notify_ielen;
2384 s32 notify_signal;
2385
d96b801f 2386 brcmf_dbg(TRACE, "Enter\n");
5b435de0
AS
2387
2388 buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
2389 if (buf == NULL) {
2390 err = -ENOMEM;
2391 goto CleanUp;
2392 }
2393
2394 *(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
2395
ac24be6f
AS
2396 err = brcmf_fil_cmd_data_get(netdev_priv(ndev), BRCMF_C_GET_BSS_INFO,
2397 buf, WL_BSS_INFO_MAX);
5b435de0 2398 if (err) {
57d6e91a 2399 brcmf_err("WLC_GET_BSS_INFO failed: %d\n", err);
5b435de0
AS
2400 goto CleanUp;
2401 }
2402
d34bf64f 2403 bi = (struct brcmf_bss_info_le *)(buf + 4);
5b435de0 2404
83cf17aa
FL
2405 ch.chspec = le16_to_cpu(bi->chanspec);
2406 cfg->d11inf.decchspec(&ch);
5b435de0 2407
83cf17aa 2408 if (ch.band == BRCMU_CHAN_BAND_2G)
5b435de0
AS
2409 band = wiphy->bands[IEEE80211_BAND_2GHZ];
2410 else
2411 band = wiphy->bands[IEEE80211_BAND_5GHZ];
2412
83cf17aa 2413 freq = ieee80211_channel_to_frequency(ch.chnum, band->band);
5b435de0
AS
2414 notify_channel = ieee80211_get_channel(wiphy, freq);
2415
5b435de0
AS
2416 notify_capability = le16_to_cpu(bi->capability);
2417 notify_interval = le16_to_cpu(bi->beacon_period);
2418 notify_ie = (u8 *)bi + le16_to_cpu(bi->ie_offset);
2419 notify_ielen = le32_to_cpu(bi->ie_length);
2420 notify_signal = (s16)le16_to_cpu(bi->RSSI) * 100;
2421
83cf17aa 2422 brcmf_dbg(CONN, "channel: %d(%d)\n", ch.chnum, freq);
16886735
AS
2423 brcmf_dbg(CONN, "capability: %X\n", notify_capability);
2424 brcmf_dbg(CONN, "beacon interval: %d\n", notify_interval);
2425 brcmf_dbg(CONN, "signal: %d\n", notify_signal);
5b435de0 2426
e78946e1 2427 bss = cfg80211_inform_bss(wiphy, notify_channel, bssid,
8e6cffb3 2428 0, notify_capability, notify_interval,
5b435de0
AS
2429 notify_ie, notify_ielen, notify_signal, GFP_KERNEL);
2430
e78946e1
FL
2431 if (!bss) {
2432 err = -ENOMEM;
2433 goto CleanUp;
2434 }
2435
5b112d3d 2436 cfg80211_put_bss(wiphy, bss);
e78946e1 2437
5b435de0
AS
2438CleanUp:
2439
2440 kfree(buf);
2441
d96b801f 2442 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2443
2444 return err;
2445}
2446
128ce3b6 2447static bool brcmf_is_ibssmode(struct brcmf_cfg80211_vif *vif)
5b435de0 2448{
128ce3b6 2449 return vif->mode == WL_MODE_IBSS;
5b435de0
AS
2450}
2451
89286dc9
HM
2452static s32 brcmf_update_bss_info(struct brcmf_cfg80211_info *cfg,
2453 struct brcmf_if *ifp)
1a873342 2454{
89286dc9 2455 struct brcmf_cfg80211_profile *profile = ndev_to_prof(ifp->ndev);
d34bf64f 2456 struct brcmf_bss_info_le *bi;
5b435de0 2457 struct brcmf_ssid *ssid;
f8e4b412 2458 struct brcmf_tlv *tim;
5b435de0
AS
2459 u16 beacon_interval;
2460 u8 dtim_period;
2461 size_t ie_len;
2462 u8 *ie;
2463 s32 err = 0;
2464
d96b801f 2465 brcmf_dbg(TRACE, "Enter\n");
128ce3b6 2466 if (brcmf_is_ibssmode(ifp->vif))
5b435de0
AS
2467 return err;
2468
06bb123e 2469 ssid = &profile->ssid;
5b435de0 2470
27a68fe3 2471 *(__le32 *)cfg->extra_buf = cpu_to_le32(WL_EXTRA_BUF_MAX);
ac24be6f 2472 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
81f5dcb8 2473 cfg->extra_buf, WL_EXTRA_BUF_MAX);
5b435de0 2474 if (err) {
57d6e91a 2475 brcmf_err("Could not get bss info %d\n", err);
5b435de0
AS
2476 goto update_bss_info_out;
2477 }
2478
27a68fe3
AS
2479 bi = (struct brcmf_bss_info_le *)(cfg->extra_buf + 4);
2480 err = brcmf_inform_single_bss(cfg, bi);
5b435de0
AS
2481 if (err)
2482 goto update_bss_info_out;
2483
2484 ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
2485 ie_len = le32_to_cpu(bi->ie_length);
2486 beacon_interval = le16_to_cpu(bi->beacon_period);
2487
f8e4b412 2488 tim = brcmf_parse_tlvs(ie, ie_len, WLAN_EID_TIM);
5b435de0
AS
2489 if (tim)
2490 dtim_period = tim->data[1];
2491 else {
2492 /*
2493 * active scan was done so we could not get dtim
2494 * information out of probe response.
2495 * so we speficially query dtim information to dongle.
2496 */
2497 u32 var;
ac24be6f 2498 err = brcmf_fil_iovar_int_get(ifp, "dtim_assoc", &var);
5b435de0 2499 if (err) {
57d6e91a 2500 brcmf_err("wl dtim_assoc failed (%d)\n", err);
5b435de0
AS
2501 goto update_bss_info_out;
2502 }
2503 dtim_period = (u8)var;
2504 }
2505
5b435de0 2506update_bss_info_out:
d96b801f 2507 brcmf_dbg(TRACE, "Exit");
5b435de0
AS
2508 return err;
2509}
2510
18e2f61d 2511void brcmf_abort_scanning(struct brcmf_cfg80211_info *cfg)
5b435de0 2512{
27a68fe3 2513 struct escan_info *escan = &cfg->escan_info;
5b435de0 2514
c1179033 2515 set_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
f0799895 2516 if (cfg->scan_request) {
108a4bee 2517 escan->escan_state = WL_ESCAN_STATE_IDLE;
a0f472ac 2518 brcmf_notify_escan_complete(cfg, escan->ifp, true, true);
5b435de0 2519 }
c1179033
AS
2520 clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
2521 clear_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
5b435de0
AS
2522}
2523
e756af5b
HM
2524static void brcmf_cfg80211_escan_timeout_worker(struct work_struct *work)
2525{
27a68fe3
AS
2526 struct brcmf_cfg80211_info *cfg =
2527 container_of(work, struct brcmf_cfg80211_info,
e756af5b
HM
2528 escan_timeout_work);
2529
a0f472ac 2530 brcmf_notify_escan_complete(cfg, cfg->escan_info.ifp, true, true);
e756af5b
HM
2531}
2532
2533static void brcmf_escan_timeout(unsigned long data)
2534{
27a68fe3
AS
2535 struct brcmf_cfg80211_info *cfg =
2536 (struct brcmf_cfg80211_info *)data;
e756af5b 2537
27a68fe3 2538 if (cfg->scan_request) {
57d6e91a 2539 brcmf_err("timer expired\n");
f0799895 2540 schedule_work(&cfg->escan_timeout_work);
e756af5b
HM
2541 }
2542}
2543
2544static s32
83cf17aa
FL
2545brcmf_compare_update_same_bss(struct brcmf_cfg80211_info *cfg,
2546 struct brcmf_bss_info_le *bss,
e756af5b
HM
2547 struct brcmf_bss_info_le *bss_info_le)
2548{
83cf17aa
FL
2549 struct brcmu_chan ch_bss, ch_bss_info_le;
2550
2551 ch_bss.chspec = le16_to_cpu(bss->chanspec);
2552 cfg->d11inf.decchspec(&ch_bss);
2553 ch_bss_info_le.chspec = le16_to_cpu(bss_info_le->chanspec);
2554 cfg->d11inf.decchspec(&ch_bss_info_le);
2555
e756af5b 2556 if (!memcmp(&bss_info_le->BSSID, &bss->BSSID, ETH_ALEN) &&
83cf17aa 2557 ch_bss.band == ch_bss_info_le.band &&
e756af5b
HM
2558 bss_info_le->SSID_len == bss->SSID_len &&
2559 !memcmp(bss_info_le->SSID, bss->SSID, bss_info_le->SSID_len)) {
6f5838a4
AS
2560 if ((bss->flags & BRCMF_BSS_RSSI_ON_CHANNEL) ==
2561 (bss_info_le->flags & BRCMF_BSS_RSSI_ON_CHANNEL)) {
029591f3
AS
2562 s16 bss_rssi = le16_to_cpu(bss->RSSI);
2563 s16 bss_info_rssi = le16_to_cpu(bss_info_le->RSSI);
2564
e756af5b
HM
2565 /* preserve max RSSI if the measurements are
2566 * both on-channel or both off-channel
2567 */
029591f3 2568 if (bss_info_rssi > bss_rssi)
e756af5b 2569 bss->RSSI = bss_info_le->RSSI;
6f5838a4
AS
2570 } else if ((bss->flags & BRCMF_BSS_RSSI_ON_CHANNEL) &&
2571 (bss_info_le->flags & BRCMF_BSS_RSSI_ON_CHANNEL) == 0) {
e756af5b
HM
2572 /* preserve the on-channel rssi measurement
2573 * if the new measurement is off channel
2574 */
2575 bss->RSSI = bss_info_le->RSSI;
6f5838a4 2576 bss->flags |= BRCMF_BSS_RSSI_ON_CHANNEL;
e756af5b
HM
2577 }
2578 return 1;
2579 }
2580 return 0;
2581}
2582
2583static s32
1993732e 2584brcmf_cfg80211_escan_handler(struct brcmf_if *ifp,
e756af5b
HM
2585 const struct brcmf_event_msg *e, void *data)
2586{
1993732e 2587 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
e756af5b
HM
2588 s32 status;
2589 s32 err = 0;
2590 struct brcmf_escan_result_le *escan_result_le;
2591 struct brcmf_bss_info_le *bss_info_le;
2592 struct brcmf_bss_info_le *bss = NULL;
2593 u32 bi_length;
2594 struct brcmf_scan_results *list;
2595 u32 i;
97ed15c7 2596 bool aborted;
e756af5b 2597
5c36b99a 2598 status = e->status;
e756af5b 2599
a0f472ac
AS
2600 if (!test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
2601 brcmf_err("scan not ready, bssidx=%d\n", ifp->bssidx);
e756af5b
HM
2602 return -EPERM;
2603 }
2604
2605 if (status == BRCMF_E_STATUS_PARTIAL) {
4e8a008e 2606 brcmf_dbg(SCAN, "ESCAN Partial result\n");
e756af5b
HM
2607 escan_result_le = (struct brcmf_escan_result_le *) data;
2608 if (!escan_result_le) {
57d6e91a 2609 brcmf_err("Invalid escan result (NULL pointer)\n");
e756af5b
HM
2610 goto exit;
2611 }
e756af5b 2612 if (le16_to_cpu(escan_result_le->bss_count) != 1) {
57d6e91a
AS
2613 brcmf_err("Invalid bss_count %d: ignoring\n",
2614 escan_result_le->bss_count);
e756af5b
HM
2615 goto exit;
2616 }
2617 bss_info_le = &escan_result_le->bss_info_le;
2618
6eda4e2c
HM
2619 if (brcmf_p2p_scan_finding_common_channel(cfg, bss_info_le))
2620 goto exit;
2621
2622 if (!cfg->scan_request) {
2623 brcmf_dbg(SCAN, "result without cfg80211 request\n");
2624 goto exit;
2625 }
2626
e756af5b
HM
2627 bi_length = le32_to_cpu(bss_info_le->length);
2628 if (bi_length != (le32_to_cpu(escan_result_le->buflen) -
2629 WL_ESCAN_RESULTS_FIXED_SIZE)) {
57d6e91a
AS
2630 brcmf_err("Invalid bss_info length %d: ignoring\n",
2631 bi_length);
e756af5b
HM
2632 goto exit;
2633 }
2634
27a68fe3 2635 if (!(cfg_to_wiphy(cfg)->interface_modes &
e756af5b
HM
2636 BIT(NL80211_IFTYPE_ADHOC))) {
2637 if (le16_to_cpu(bss_info_le->capability) &
2638 WLAN_CAPABILITY_IBSS) {
57d6e91a 2639 brcmf_err("Ignoring IBSS result\n");
e756af5b
HM
2640 goto exit;
2641 }
2642 }
2643
2644 list = (struct brcmf_scan_results *)
27a68fe3 2645 cfg->escan_info.escan_buf;
e756af5b 2646 if (bi_length > WL_ESCAN_BUF_SIZE - list->buflen) {
57d6e91a 2647 brcmf_err("Buffer is too small: ignoring\n");
e756af5b
HM
2648 goto exit;
2649 }
2650
2651 for (i = 0; i < list->count; i++) {
2652 bss = bss ? (struct brcmf_bss_info_le *)
2653 ((unsigned char *)bss +
2654 le32_to_cpu(bss->length)) : list->bss_info_le;
83cf17aa
FL
2655 if (brcmf_compare_update_same_bss(cfg, bss,
2656 bss_info_le))
e756af5b
HM
2657 goto exit;
2658 }
27a68fe3 2659 memcpy(&(cfg->escan_info.escan_buf[list->buflen]),
e756af5b
HM
2660 bss_info_le, bi_length);
2661 list->version = le32_to_cpu(bss_info_le->version);
2662 list->buflen += bi_length;
2663 list->count++;
2664 } else {
27a68fe3 2665 cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
6eda4e2c
HM
2666 if (brcmf_p2p_scan_finding_common_channel(cfg, NULL))
2667 goto exit;
27a68fe3
AS
2668 if (cfg->scan_request) {
2669 cfg->bss_list = (struct brcmf_scan_results *)
2670 cfg->escan_info.escan_buf;
2671 brcmf_inform_bss(cfg);
97ed15c7 2672 aborted = status != BRCMF_E_STATUS_SUCCESS;
a0f472ac 2673 brcmf_notify_escan_complete(cfg, ifp, aborted,
97ed15c7 2674 false);
e756af5b 2675 } else
6eda4e2c
HM
2676 brcmf_dbg(SCAN, "Ignored scan complete result 0x%x\n",
2677 status);
e756af5b
HM
2678 }
2679exit:
2680 return err;
2681}
2682
27a68fe3 2683static void brcmf_init_escan(struct brcmf_cfg80211_info *cfg)
e756af5b 2684{
5c36b99a
AS
2685 brcmf_fweh_register(cfg->pub, BRCMF_E_ESCAN_RESULT,
2686 brcmf_cfg80211_escan_handler);
f0799895
HM
2687 cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
2688 /* Init scan_timeout timer */
2689 init_timer(&cfg->escan_timeout);
2690 cfg->escan_timeout.data = (unsigned long) cfg;
2691 cfg->escan_timeout.function = brcmf_escan_timeout;
2692 INIT_WORK(&cfg->escan_timeout_work,
2693 brcmf_cfg80211_escan_timeout_worker);
e756af5b
HM
2694}
2695
5addc0de 2696static __always_inline void brcmf_delay(u32 ms)
5b435de0
AS
2697{
2698 if (ms < 1000 / HZ) {
2699 cond_resched();
2700 mdelay(ms);
2701 } else {
2702 msleep(ms);
2703 }
2704}
2705
2706static s32 brcmf_cfg80211_resume(struct wiphy *wiphy)
2707{
d96b801f 2708 brcmf_dbg(TRACE, "Enter\n");
5b435de0 2709
5b435de0
AS
2710 return 0;
2711}
2712
2713static s32 brcmf_cfg80211_suspend(struct wiphy *wiphy,
2714 struct cfg80211_wowlan *wow)
2715{
27a68fe3
AS
2716 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2717 struct net_device *ndev = cfg_to_ndev(cfg);
7d641072 2718 struct brcmf_cfg80211_vif *vif;
5b435de0 2719
d96b801f 2720 brcmf_dbg(TRACE, "Enter\n");
5b435de0
AS
2721
2722 /*
7d641072
AS
2723 * if the primary net_device is not READY there is nothing
2724 * we can do but pray resume goes smoothly.
5b435de0 2725 */
7d641072
AS
2726 vif = ((struct brcmf_if *)netdev_priv(ndev))->vif;
2727 if (!check_vif_up(vif))
2728 goto exit;
5b435de0 2729
7d641072
AS
2730 list_for_each_entry(vif, &cfg->vif_list, list) {
2731 if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state))
2732 continue;
5b435de0 2733 /*
7d641072
AS
2734 * While going to suspend if associated with AP disassociate
2735 * from AP to save power while system is in suspended state
5b435de0 2736 */
903e0eee
AS
2737 brcmf_link_down(vif);
2738
2739 /* Make sure WPA_Supplicant receives all the event
2740 * generated due to DISASSOC call to the fw to keep
2741 * the state fw and WPA_Supplicant state consistent
2742 */
2743 brcmf_delay(500);
5b435de0
AS
2744 }
2745
7d641072
AS
2746 /* end any scanning */
2747 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
27a68fe3 2748 brcmf_abort_scanning(cfg);
5b435de0
AS
2749
2750 /* Turn off watchdog timer */
f96aa07e 2751 brcmf_set_mpc(netdev_priv(ndev), 1);
5b435de0 2752
7d641072 2753exit:
d96b801f 2754 brcmf_dbg(TRACE, "Exit\n");
7d641072
AS
2755 /* clear any scanning activity */
2756 cfg->scan_status = 0;
5b435de0
AS
2757 return 0;
2758}
2759
5b435de0
AS
2760static __used s32
2761brcmf_update_pmklist(struct net_device *ndev,
2762 struct brcmf_cfg80211_pmk_list *pmk_list, s32 err)
2763{
2764 int i, j;
40c8e95a 2765 int pmkid_len;
5b435de0 2766
40c8e95a
AS
2767 pmkid_len = le32_to_cpu(pmk_list->pmkids.npmkid);
2768
16886735 2769 brcmf_dbg(CONN, "No of elements %d\n", pmkid_len);
40c8e95a 2770 for (i = 0; i < pmkid_len; i++) {
16886735
AS
2771 brcmf_dbg(CONN, "PMKID[%d]: %pM =\n", i,
2772 &pmk_list->pmkids.pmkid[i].BSSID);
5b435de0 2773 for (j = 0; j < WLAN_PMKID_LEN; j++)
16886735
AS
2774 brcmf_dbg(CONN, "%02x\n",
2775 pmk_list->pmkids.pmkid[i].PMKID[j]);
5b435de0
AS
2776 }
2777
2778 if (!err)
ac24be6f
AS
2779 brcmf_fil_iovar_data_set(netdev_priv(ndev), "pmkid_info",
2780 (char *)pmk_list, sizeof(*pmk_list));
5b435de0
AS
2781
2782 return err;
2783}
2784
2785static s32
2786brcmf_cfg80211_set_pmksa(struct wiphy *wiphy, struct net_device *ndev,
2787 struct cfg80211_pmksa *pmksa)
2788{
27a68fe3 2789 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 2790 struct brcmf_if *ifp = netdev_priv(ndev);
27a68fe3 2791 struct pmkid_list *pmkids = &cfg->pmk_list->pmkids;
5b435de0
AS
2792 s32 err = 0;
2793 int i;
40c8e95a 2794 int pmkid_len;
5b435de0 2795
d96b801f 2796 brcmf_dbg(TRACE, "Enter\n");
ce81e317 2797 if (!check_vif_up(ifp->vif))
5b435de0
AS
2798 return -EIO;
2799
40c8e95a
AS
2800 pmkid_len = le32_to_cpu(pmkids->npmkid);
2801 for (i = 0; i < pmkid_len; i++)
5b435de0
AS
2802 if (!memcmp(pmksa->bssid, pmkids->pmkid[i].BSSID, ETH_ALEN))
2803 break;
2804 if (i < WL_NUM_PMKIDS_MAX) {
2805 memcpy(pmkids->pmkid[i].BSSID, pmksa->bssid, ETH_ALEN);
2806 memcpy(pmkids->pmkid[i].PMKID, pmksa->pmkid, WLAN_PMKID_LEN);
40c8e95a
AS
2807 if (i == pmkid_len) {
2808 pmkid_len++;
2809 pmkids->npmkid = cpu_to_le32(pmkid_len);
2810 }
5b435de0
AS
2811 } else
2812 err = -EINVAL;
2813
16886735
AS
2814 brcmf_dbg(CONN, "set_pmksa,IW_PMKSA_ADD - PMKID: %pM =\n",
2815 pmkids->pmkid[pmkid_len].BSSID);
5b435de0 2816 for (i = 0; i < WLAN_PMKID_LEN; i++)
16886735 2817 brcmf_dbg(CONN, "%02x\n", pmkids->pmkid[pmkid_len].PMKID[i]);
5b435de0 2818
27a68fe3 2819 err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
5b435de0 2820
d96b801f 2821 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2822 return err;
2823}
2824
2825static s32
2826brcmf_cfg80211_del_pmksa(struct wiphy *wiphy, struct net_device *ndev,
2827 struct cfg80211_pmksa *pmksa)
2828{
27a68fe3 2829 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 2830 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
2831 struct pmkid_list pmkid;
2832 s32 err = 0;
40c8e95a 2833 int i, pmkid_len;
5b435de0 2834
d96b801f 2835 brcmf_dbg(TRACE, "Enter\n");
ce81e317 2836 if (!check_vif_up(ifp->vif))
5b435de0
AS
2837 return -EIO;
2838
2839 memcpy(&pmkid.pmkid[0].BSSID, pmksa->bssid, ETH_ALEN);
2840 memcpy(&pmkid.pmkid[0].PMKID, pmksa->pmkid, WLAN_PMKID_LEN);
2841
16886735
AS
2842 brcmf_dbg(CONN, "del_pmksa,IW_PMKSA_REMOVE - PMKID: %pM =\n",
2843 &pmkid.pmkid[0].BSSID);
5b435de0 2844 for (i = 0; i < WLAN_PMKID_LEN; i++)
16886735 2845 brcmf_dbg(CONN, "%02x\n", pmkid.pmkid[0].PMKID[i]);
5b435de0 2846
27a68fe3 2847 pmkid_len = le32_to_cpu(cfg->pmk_list->pmkids.npmkid);
40c8e95a 2848 for (i = 0; i < pmkid_len; i++)
5b435de0 2849 if (!memcmp
27a68fe3 2850 (pmksa->bssid, &cfg->pmk_list->pmkids.pmkid[i].BSSID,
5b435de0
AS
2851 ETH_ALEN))
2852 break;
2853
40c8e95a
AS
2854 if ((pmkid_len > 0)
2855 && (i < pmkid_len)) {
27a68fe3 2856 memset(&cfg->pmk_list->pmkids.pmkid[i], 0,
5b435de0 2857 sizeof(struct pmkid));
40c8e95a 2858 for (; i < (pmkid_len - 1); i++) {
27a68fe3
AS
2859 memcpy(&cfg->pmk_list->pmkids.pmkid[i].BSSID,
2860 &cfg->pmk_list->pmkids.pmkid[i + 1].BSSID,
5b435de0 2861 ETH_ALEN);
27a68fe3
AS
2862 memcpy(&cfg->pmk_list->pmkids.pmkid[i].PMKID,
2863 &cfg->pmk_list->pmkids.pmkid[i + 1].PMKID,
5b435de0
AS
2864 WLAN_PMKID_LEN);
2865 }
27a68fe3 2866 cfg->pmk_list->pmkids.npmkid = cpu_to_le32(pmkid_len - 1);
5b435de0
AS
2867 } else
2868 err = -EINVAL;
2869
27a68fe3 2870 err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
5b435de0 2871
d96b801f 2872 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2873 return err;
2874
2875}
2876
2877static s32
2878brcmf_cfg80211_flush_pmksa(struct wiphy *wiphy, struct net_device *ndev)
2879{
27a68fe3 2880 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 2881 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
2882 s32 err = 0;
2883
d96b801f 2884 brcmf_dbg(TRACE, "Enter\n");
ce81e317 2885 if (!check_vif_up(ifp->vif))
5b435de0
AS
2886 return -EIO;
2887
27a68fe3
AS
2888 memset(cfg->pmk_list, 0, sizeof(*cfg->pmk_list));
2889 err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
5b435de0 2890
d96b801f 2891 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2892 return err;
2893
2894}
2895
e5806072
AS
2896/*
2897 * PFN result doesn't have all the info which are
2898 * required by the supplicant
2899 * (For e.g IEs) Do a target Escan so that sched scan results are reported
2900 * via wl_inform_single_bss in the required format. Escan does require the
2901 * scan request in the form of cfg80211_scan_request. For timebeing, create
2902 * cfg80211_scan_request one out of the received PNO event.
2903 */
2904static s32
1993732e 2905brcmf_notify_sched_scan_results(struct brcmf_if *ifp,
e5806072
AS
2906 const struct brcmf_event_msg *e, void *data)
2907{
1993732e 2908 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
e5806072
AS
2909 struct brcmf_pno_net_info_le *netinfo, *netinfo_start;
2910 struct cfg80211_scan_request *request = NULL;
2911 struct cfg80211_ssid *ssid = NULL;
2912 struct ieee80211_channel *channel = NULL;
27a68fe3 2913 struct wiphy *wiphy = cfg_to_wiphy(cfg);
e5806072
AS
2914 int err = 0;
2915 int channel_req = 0;
2916 int band = 0;
2917 struct brcmf_pno_scanresults_le *pfn_result;
2918 u32 result_count;
2919 u32 status;
2920
4e8a008e 2921 brcmf_dbg(SCAN, "Enter\n");
e5806072 2922
5c36b99a 2923 if (e->event_code == BRCMF_E_PFN_NET_LOST) {
4e8a008e 2924 brcmf_dbg(SCAN, "PFN NET LOST event. Do Nothing\n");
e5806072
AS
2925 return 0;
2926 }
2927
2928 pfn_result = (struct brcmf_pno_scanresults_le *)data;
2929 result_count = le32_to_cpu(pfn_result->count);
2930 status = le32_to_cpu(pfn_result->status);
2931
2932 /*
2933 * PFN event is limited to fit 512 bytes so we may get
2934 * multiple NET_FOUND events. For now place a warning here.
2935 */
2936 WARN_ON(status != BRCMF_PNO_SCAN_COMPLETE);
4e8a008e 2937 brcmf_dbg(SCAN, "PFN NET FOUND event. count: %d\n", result_count);
e5806072
AS
2938 if (result_count > 0) {
2939 int i;
2940
2941 request = kzalloc(sizeof(*request), GFP_KERNEL);
58901d18
DC
2942 ssid = kcalloc(result_count, sizeof(*ssid), GFP_KERNEL);
2943 channel = kcalloc(result_count, sizeof(*channel), GFP_KERNEL);
e5806072
AS
2944 if (!request || !ssid || !channel) {
2945 err = -ENOMEM;
2946 goto out_err;
2947 }
2948
2949 request->wiphy = wiphy;
2950 data += sizeof(struct brcmf_pno_scanresults_le);
2951 netinfo_start = (struct brcmf_pno_net_info_le *)data;
2952
2953 for (i = 0; i < result_count; i++) {
2954 netinfo = &netinfo_start[i];
2955 if (!netinfo) {
57d6e91a
AS
2956 brcmf_err("Invalid netinfo ptr. index: %d\n",
2957 i);
e5806072
AS
2958 err = -EINVAL;
2959 goto out_err;
2960 }
2961
4e8a008e
AS
2962 brcmf_dbg(SCAN, "SSID:%s Channel:%d\n",
2963 netinfo->SSID, netinfo->channel);
e5806072
AS
2964 memcpy(ssid[i].ssid, netinfo->SSID, netinfo->SSID_len);
2965 ssid[i].ssid_len = netinfo->SSID_len;
2966 request->n_ssids++;
2967
2968 channel_req = netinfo->channel;
2969 if (channel_req <= CH_MAX_2G_CHANNEL)
2970 band = NL80211_BAND_2GHZ;
2971 else
2972 band = NL80211_BAND_5GHZ;
2973 channel[i].center_freq =
2974 ieee80211_channel_to_frequency(channel_req,
2975 band);
2976 channel[i].band = band;
2977 channel[i].flags |= IEEE80211_CHAN_NO_HT40;
2978 request->channels[i] = &channel[i];
2979 request->n_channels++;
2980 }
2981
2982 /* assign parsed ssid array */
2983 if (request->n_ssids)
2984 request->ssids = &ssid[0];
2985
c1179033 2986 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
e5806072 2987 /* Abort any on-going scan */
27a68fe3 2988 brcmf_abort_scanning(cfg);
e5806072
AS
2989 }
2990
c1179033 2991 set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
a0f472ac 2992 err = brcmf_do_escan(cfg, wiphy, ifp, request);
e5806072 2993 if (err) {
c1179033 2994 clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
e5806072
AS
2995 goto out_err;
2996 }
27a68fe3
AS
2997 cfg->sched_escan = true;
2998 cfg->scan_request = request;
e5806072 2999 } else {
57d6e91a 3000 brcmf_err("FALSE PNO Event. (pfn_count == 0)\n");
e5806072
AS
3001 goto out_err;
3002 }
3003
3004 kfree(ssid);
3005 kfree(channel);
3006 kfree(request);
3007 return 0;
3008
3009out_err:
3010 kfree(ssid);
3011 kfree(channel);
3012 kfree(request);
3013 cfg80211_sched_scan_stopped(wiphy);
3014 return err;
3015}
3016
e5806072
AS
3017static int brcmf_dev_pno_clean(struct net_device *ndev)
3018{
e5806072
AS
3019 int ret;
3020
3021 /* Disable pfn */
ac24be6f 3022 ret = brcmf_fil_iovar_int_set(netdev_priv(ndev), "pfn", 0);
e5806072
AS
3023 if (ret == 0) {
3024 /* clear pfn */
ac24be6f
AS
3025 ret = brcmf_fil_iovar_data_set(netdev_priv(ndev), "pfnclear",
3026 NULL, 0);
e5806072
AS
3027 }
3028 if (ret < 0)
57d6e91a 3029 brcmf_err("failed code %d\n", ret);
e5806072
AS
3030
3031 return ret;
3032}
3033
3034static int brcmf_dev_pno_config(struct net_device *ndev)
3035{
3036 struct brcmf_pno_param_le pfn_param;
e5806072
AS
3037
3038 memset(&pfn_param, 0, sizeof(pfn_param));
3039 pfn_param.version = cpu_to_le32(BRCMF_PNO_VERSION);
3040
3041 /* set extra pno params */
3042 pfn_param.flags = cpu_to_le16(1 << BRCMF_PNO_ENABLE_ADAPTSCAN_BIT);
3043 pfn_param.repeat = BRCMF_PNO_REPEAT;
3044 pfn_param.exp = BRCMF_PNO_FREQ_EXPO_MAX;
3045
3046 /* set up pno scan fr */
3047 pfn_param.scan_freq = cpu_to_le32(BRCMF_PNO_TIME);
3048
ac24be6f
AS
3049 return brcmf_fil_iovar_data_set(netdev_priv(ndev), "pfn_set",
3050 &pfn_param, sizeof(pfn_param));
e5806072
AS
3051}
3052
3053static int
3054brcmf_cfg80211_sched_scan_start(struct wiphy *wiphy,
3055 struct net_device *ndev,
3056 struct cfg80211_sched_scan_request *request)
3057{
c1179033 3058 struct brcmf_if *ifp = netdev_priv(ndev);
27a68fe3 3059 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
e5806072
AS
3060 struct brcmf_pno_net_param_le pfn;
3061 int i;
3062 int ret = 0;
3063
dc7bdbf1 3064 brcmf_dbg(SCAN, "Enter n_match_sets:%d n_ssids:%d\n",
4e8a008e 3065 request->n_match_sets, request->n_ssids);
c1179033 3066 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
57d6e91a 3067 brcmf_err("Scanning already: status (%lu)\n", cfg->scan_status);
e5806072
AS
3068 return -EAGAIN;
3069 }
1687eee2
AS
3070 if (test_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
3071 brcmf_err("Scanning suppressed: status (%lu)\n",
3072 cfg->scan_status);
3073 return -EAGAIN;
3074 }
e5806072 3075
dc7bdbf1 3076 if (!request->n_ssids || !request->n_match_sets) {
57d6e91a 3077 brcmf_err("Invalid sched scan req!! n_ssids:%d\n",
dc7bdbf1 3078 request->n_ssids);
e5806072
AS
3079 return -EINVAL;
3080 }
3081
3082 if (request->n_ssids > 0) {
3083 for (i = 0; i < request->n_ssids; i++) {
3084 /* Active scan req for ssids */
4e8a008e
AS
3085 brcmf_dbg(SCAN, ">>> Active scan req for ssid (%s)\n",
3086 request->ssids[i].ssid);
e5806072
AS
3087
3088 /*
3089 * match_set ssids is a supert set of n_ssid list,
3090 * so we need not add these set seperately.
3091 */
3092 }
3093 }
3094
3095 if (request->n_match_sets > 0) {
3096 /* clean up everything */
3097 ret = brcmf_dev_pno_clean(ndev);
3098 if (ret < 0) {
57d6e91a 3099 brcmf_err("failed error=%d\n", ret);
e5806072
AS
3100 return ret;
3101 }
3102
3103 /* configure pno */
3104 ret = brcmf_dev_pno_config(ndev);
3105 if (ret < 0) {
57d6e91a 3106 brcmf_err("PNO setup failed!! ret=%d\n", ret);
e5806072
AS
3107 return -EINVAL;
3108 }
3109
3110 /* configure each match set */
3111 for (i = 0; i < request->n_match_sets; i++) {
3112 struct cfg80211_ssid *ssid;
3113 u32 ssid_len;
3114
3115 ssid = &request->match_sets[i].ssid;
3116 ssid_len = ssid->ssid_len;
3117
3118 if (!ssid_len) {
57d6e91a 3119 brcmf_err("skip broadcast ssid\n");
e5806072
AS
3120 continue;
3121 }
3122 pfn.auth = cpu_to_le32(WLAN_AUTH_OPEN);
3123 pfn.wpa_auth = cpu_to_le32(BRCMF_PNO_WPA_AUTH_ANY);
3124 pfn.wsec = cpu_to_le32(0);
3125 pfn.infra = cpu_to_le32(1);
3126 pfn.flags = cpu_to_le32(1 << BRCMF_PNO_HIDDEN_BIT);
3127 pfn.ssid.SSID_len = cpu_to_le32(ssid_len);
3128 memcpy(pfn.ssid.SSID, ssid->ssid, ssid_len);
c1179033 3129 ret = brcmf_fil_iovar_data_set(ifp, "pfn_add", &pfn,
ac24be6f 3130 sizeof(pfn));
4e8a008e
AS
3131 brcmf_dbg(SCAN, ">>> PNO filter %s for ssid (%s)\n",
3132 ret == 0 ? "set" : "failed", ssid->ssid);
e5806072
AS
3133 }
3134 /* Enable the PNO */
c1179033 3135 if (brcmf_fil_iovar_int_set(ifp, "pfn", 1) < 0) {
57d6e91a 3136 brcmf_err("PNO enable failed!! ret=%d\n", ret);
e5806072
AS
3137 return -EINVAL;
3138 }
3139 } else {
3140 return -EINVAL;
3141 }
3142
3143 return 0;
3144}
3145
3146static int brcmf_cfg80211_sched_scan_stop(struct wiphy *wiphy,
3147 struct net_device *ndev)
3148{
27a68fe3 3149 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
e5806072 3150
4e8a008e 3151 brcmf_dbg(SCAN, "enter\n");
e5806072 3152 brcmf_dev_pno_clean(ndev);
27a68fe3 3153 if (cfg->sched_escan)
a0f472ac 3154 brcmf_notify_escan_complete(cfg, netdev_priv(ndev), true, true);
e5806072
AS
3155 return 0;
3156}
e5806072 3157
cbaa177d 3158#ifdef CONFIG_NL80211_TESTMODE
fc73f11f
DS
3159static int brcmf_cfg80211_testmode(struct wiphy *wiphy,
3160 struct wireless_dev *wdev,
3161 void *data, int len)
cbaa177d 3162{
27a68fe3 3163 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3eacf866 3164 struct net_device *ndev = cfg_to_ndev(cfg);
cbaa177d
AS
3165 struct brcmf_dcmd *dcmd = data;
3166 struct sk_buff *reply;
3167 int ret;
3168
d96b801f
AS
3169 brcmf_dbg(TRACE, "cmd %x set %d buf %p len %d\n", dcmd->cmd, dcmd->set,
3170 dcmd->buf, dcmd->len);
f368a5b6
HM
3171
3172 if (dcmd->set)
ac24be6f
AS
3173 ret = brcmf_fil_cmd_data_set(netdev_priv(ndev), dcmd->cmd,
3174 dcmd->buf, dcmd->len);
f368a5b6 3175 else
ac24be6f
AS
3176 ret = brcmf_fil_cmd_data_get(netdev_priv(ndev), dcmd->cmd,
3177 dcmd->buf, dcmd->len);
cbaa177d
AS
3178 if (ret == 0) {
3179 reply = cfg80211_testmode_alloc_reply_skb(wiphy, sizeof(*dcmd));
3180 nla_put(reply, NL80211_ATTR_TESTDATA, sizeof(*dcmd), dcmd);
3181 ret = cfg80211_testmode_reply(reply);
3182 }
3183 return ret;
3184}
3185#endif
3186
1f170110 3187static s32 brcmf_configure_opensecurity(struct brcmf_if *ifp)
1a873342
HM
3188{
3189 s32 err;
3190
3191 /* set auth */
ac24be6f 3192 err = brcmf_fil_bsscfg_int_set(ifp, "auth", 0);
1a873342 3193 if (err < 0) {
57d6e91a 3194 brcmf_err("auth error %d\n", err);
1a873342
HM
3195 return err;
3196 }
3197 /* set wsec */
ac24be6f 3198 err = brcmf_fil_bsscfg_int_set(ifp, "wsec", 0);
1a873342 3199 if (err < 0) {
57d6e91a 3200 brcmf_err("wsec error %d\n", err);
1a873342
HM
3201 return err;
3202 }
3203 /* set upper-layer auth */
ac24be6f 3204 err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", WPA_AUTH_NONE);
1a873342 3205 if (err < 0) {
57d6e91a 3206 brcmf_err("wpa_auth error %d\n", err);
1a873342
HM
3207 return err;
3208 }
3209
3210 return 0;
3211}
3212
3213static bool brcmf_valid_wpa_oui(u8 *oui, bool is_rsn_ie)
3214{
3215 if (is_rsn_ie)
3216 return (memcmp(oui, RSN_OUI, TLV_OUI_LEN) == 0);
3217
3218 return (memcmp(oui, WPA_OUI, TLV_OUI_LEN) == 0);
3219}
3220
3221static s32
3222brcmf_configure_wpaie(struct net_device *ndev, struct brcmf_vs_tlv *wpa_ie,
34778529 3223 bool is_rsn_ie)
1a873342 3224{
ac24be6f 3225 struct brcmf_if *ifp = netdev_priv(ndev);
1a873342
HM
3226 u32 auth = 0; /* d11 open authentication */
3227 u16 count;
3228 s32 err = 0;
3229 s32 len = 0;
3230 u32 i;
3231 u32 wsec;
3232 u32 pval = 0;
3233 u32 gval = 0;
3234 u32 wpa_auth = 0;
3235 u32 offset;
3236 u8 *data;
3237 u16 rsn_cap;
3238 u32 wme_bss_disable;
3239
d96b801f 3240 brcmf_dbg(TRACE, "Enter\n");
1a873342
HM
3241 if (wpa_ie == NULL)
3242 goto exit;
3243
3244 len = wpa_ie->len + TLV_HDR_LEN;
3245 data = (u8 *)wpa_ie;
619c5a9a 3246 offset = TLV_HDR_LEN;
1a873342
HM
3247 if (!is_rsn_ie)
3248 offset += VS_IE_FIXED_HDR_LEN;
619c5a9a
HM
3249 else
3250 offset += WPA_IE_VERSION_LEN;
1a873342
HM
3251
3252 /* check for multicast cipher suite */
3253 if (offset + WPA_IE_MIN_OUI_LEN > len) {
3254 err = -EINVAL;
57d6e91a 3255 brcmf_err("no multicast cipher suite\n");
1a873342
HM
3256 goto exit;
3257 }
3258
3259 if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3260 err = -EINVAL;
57d6e91a 3261 brcmf_err("ivalid OUI\n");
1a873342
HM
3262 goto exit;
3263 }
3264 offset += TLV_OUI_LEN;
3265
3266 /* pick up multicast cipher */
3267 switch (data[offset]) {
3268 case WPA_CIPHER_NONE:
3269 gval = 0;
3270 break;
3271 case WPA_CIPHER_WEP_40:
3272 case WPA_CIPHER_WEP_104:
3273 gval = WEP_ENABLED;
3274 break;
3275 case WPA_CIPHER_TKIP:
3276 gval = TKIP_ENABLED;
3277 break;
3278 case WPA_CIPHER_AES_CCM:
3279 gval = AES_ENABLED;
3280 break;
3281 default:
3282 err = -EINVAL;
57d6e91a 3283 brcmf_err("Invalid multi cast cipher info\n");
1a873342
HM
3284 goto exit;
3285 }
3286
3287 offset++;
3288 /* walk thru unicast cipher list and pick up what we recognize */
3289 count = data[offset] + (data[offset + 1] << 8);
3290 offset += WPA_IE_SUITE_COUNT_LEN;
3291 /* Check for unicast suite(s) */
3292 if (offset + (WPA_IE_MIN_OUI_LEN * count) > len) {
3293 err = -EINVAL;
57d6e91a 3294 brcmf_err("no unicast cipher suite\n");
1a873342
HM
3295 goto exit;
3296 }
3297 for (i = 0; i < count; i++) {
3298 if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3299 err = -EINVAL;
57d6e91a 3300 brcmf_err("ivalid OUI\n");
1a873342
HM
3301 goto exit;
3302 }
3303 offset += TLV_OUI_LEN;
3304 switch (data[offset]) {
3305 case WPA_CIPHER_NONE:
3306 break;
3307 case WPA_CIPHER_WEP_40:
3308 case WPA_CIPHER_WEP_104:
3309 pval |= WEP_ENABLED;
3310 break;
3311 case WPA_CIPHER_TKIP:
3312 pval |= TKIP_ENABLED;
3313 break;
3314 case WPA_CIPHER_AES_CCM:
3315 pval |= AES_ENABLED;
3316 break;
3317 default:
57d6e91a 3318 brcmf_err("Ivalid unicast security info\n");
1a873342
HM
3319 }
3320 offset++;
3321 }
3322 /* walk thru auth management suite list and pick up what we recognize */
3323 count = data[offset] + (data[offset + 1] << 8);
3324 offset += WPA_IE_SUITE_COUNT_LEN;
3325 /* Check for auth key management suite(s) */
3326 if (offset + (WPA_IE_MIN_OUI_LEN * count) > len) {
3327 err = -EINVAL;
57d6e91a 3328 brcmf_err("no auth key mgmt suite\n");
1a873342
HM
3329 goto exit;
3330 }
3331 for (i = 0; i < count; i++) {
3332 if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3333 err = -EINVAL;
57d6e91a 3334 brcmf_err("ivalid OUI\n");
1a873342
HM
3335 goto exit;
3336 }
3337 offset += TLV_OUI_LEN;
3338 switch (data[offset]) {
3339 case RSN_AKM_NONE:
d96b801f 3340 brcmf_dbg(TRACE, "RSN_AKM_NONE\n");
1a873342
HM
3341 wpa_auth |= WPA_AUTH_NONE;
3342 break;
3343 case RSN_AKM_UNSPECIFIED:
d96b801f 3344 brcmf_dbg(TRACE, "RSN_AKM_UNSPECIFIED\n");
1a873342
HM
3345 is_rsn_ie ? (wpa_auth |= WPA2_AUTH_UNSPECIFIED) :
3346 (wpa_auth |= WPA_AUTH_UNSPECIFIED);
3347 break;
3348 case RSN_AKM_PSK:
d96b801f 3349 brcmf_dbg(TRACE, "RSN_AKM_PSK\n");
1a873342
HM
3350 is_rsn_ie ? (wpa_auth |= WPA2_AUTH_PSK) :
3351 (wpa_auth |= WPA_AUTH_PSK);
3352 break;
3353 default:
57d6e91a 3354 brcmf_err("Ivalid key mgmt info\n");
1a873342
HM
3355 }
3356 offset++;
3357 }
3358
3359 if (is_rsn_ie) {
3360 wme_bss_disable = 1;
3361 if ((offset + RSN_CAP_LEN) <= len) {
3362 rsn_cap = data[offset] + (data[offset + 1] << 8);
3363 if (rsn_cap & RSN_CAP_PTK_REPLAY_CNTR_MASK)
3364 wme_bss_disable = 0;
3365 }
3366 /* set wme_bss_disable to sync RSN Capabilities */
ac24be6f 3367 err = brcmf_fil_bsscfg_int_set(ifp, "wme_bss_disable",
81f5dcb8 3368 wme_bss_disable);
1a873342 3369 if (err < 0) {
57d6e91a 3370 brcmf_err("wme_bss_disable error %d\n", err);
1a873342
HM
3371 goto exit;
3372 }
3373 }
3374 /* FOR WPS , set SES_OW_ENABLED */
3375 wsec = (pval | gval | SES_OW_ENABLED);
3376
3377 /* set auth */
ac24be6f 3378 err = brcmf_fil_bsscfg_int_set(ifp, "auth", auth);
1a873342 3379 if (err < 0) {
57d6e91a 3380 brcmf_err("auth error %d\n", err);
1a873342
HM
3381 goto exit;
3382 }
3383 /* set wsec */
ac24be6f 3384 err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
1a873342 3385 if (err < 0) {
57d6e91a 3386 brcmf_err("wsec error %d\n", err);
1a873342
HM
3387 goto exit;
3388 }
3389 /* set upper-layer auth */
ac24be6f 3390 err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_auth);
1a873342 3391 if (err < 0) {
57d6e91a 3392 brcmf_err("wpa_auth error %d\n", err);
1a873342
HM
3393 goto exit;
3394 }
3395
3396exit:
3397 return err;
3398}
3399
3400static s32
3082b9be 3401brcmf_parse_vndr_ies(const u8 *vndr_ie_buf, u32 vndr_ie_len,
1a873342
HM
3402 struct parsed_vndr_ies *vndr_ies)
3403{
3404 s32 err = 0;
3405 struct brcmf_vs_tlv *vndrie;
3406 struct brcmf_tlv *ie;
3407 struct parsed_vndr_ie_info *parsed_info;
3408 s32 remaining_len;
3409
3410 remaining_len = (s32)vndr_ie_len;
3411 memset(vndr_ies, 0, sizeof(*vndr_ies));
3412
3413 ie = (struct brcmf_tlv *)vndr_ie_buf;
3414 while (ie) {
3415 if (ie->id != WLAN_EID_VENDOR_SPECIFIC)
3416 goto next;
3417 vndrie = (struct brcmf_vs_tlv *)ie;
3418 /* len should be bigger than OUI length + one */
3419 if (vndrie->len < (VS_IE_FIXED_HDR_LEN - TLV_HDR_LEN + 1)) {
57d6e91a
AS
3420 brcmf_err("invalid vndr ie. length is too small %d\n",
3421 vndrie->len);
1a873342
HM
3422 goto next;
3423 }
3424 /* if wpa or wme ie, do not add ie */
3425 if (!memcmp(vndrie->oui, (u8 *)WPA_OUI, TLV_OUI_LEN) &&
3426 ((vndrie->oui_type == WPA_OUI_TYPE) ||
3427 (vndrie->oui_type == WME_OUI_TYPE))) {
d96b801f 3428 brcmf_dbg(TRACE, "Found WPA/WME oui. Do not add it\n");
1a873342
HM
3429 goto next;
3430 }
3431
3432 parsed_info = &vndr_ies->ie_info[vndr_ies->count];
3433
3434 /* save vndr ie information */
3435 parsed_info->ie_ptr = (char *)vndrie;
3436 parsed_info->ie_len = vndrie->len + TLV_HDR_LEN;
3437 memcpy(&parsed_info->vndrie, vndrie, sizeof(*vndrie));
3438
3439 vndr_ies->count++;
3440
d96b801f
AS
3441 brcmf_dbg(TRACE, "** OUI %02x %02x %02x, type 0x%02x\n",
3442 parsed_info->vndrie.oui[0],
3443 parsed_info->vndrie.oui[1],
3444 parsed_info->vndrie.oui[2],
3445 parsed_info->vndrie.oui_type);
1a873342 3446
9f440b7b 3447 if (vndr_ies->count >= VNDR_IE_PARSE_LIMIT)
1a873342
HM
3448 break;
3449next:
b41fc3d7
HM
3450 remaining_len -= (ie->len + TLV_HDR_LEN);
3451 if (remaining_len <= TLV_HDR_LEN)
1a873342
HM
3452 ie = NULL;
3453 else
b41fc3d7
HM
3454 ie = (struct brcmf_tlv *)(((u8 *)ie) + ie->len +
3455 TLV_HDR_LEN);
1a873342
HM
3456 }
3457 return err;
3458}
3459
3460static u32
3461brcmf_vndr_ie(u8 *iebuf, s32 pktflag, u8 *ie_ptr, u32 ie_len, s8 *add_del_cmd)
3462{
3463
3464 __le32 iecount_le;
3465 __le32 pktflag_le;
3466
3467 strncpy(iebuf, add_del_cmd, VNDR_IE_CMD_LEN - 1);
3468 iebuf[VNDR_IE_CMD_LEN - 1] = '\0';
3469
3470 iecount_le = cpu_to_le32(1);
3471 memcpy(&iebuf[VNDR_IE_COUNT_OFFSET], &iecount_le, sizeof(iecount_le));
3472
3473 pktflag_le = cpu_to_le32(pktflag);
3474 memcpy(&iebuf[VNDR_IE_PKTFLAG_OFFSET], &pktflag_le, sizeof(pktflag_le));
3475
3476 memcpy(&iebuf[VNDR_IE_VSIE_OFFSET], ie_ptr, ie_len);
3477
3478 return ie_len + VNDR_IE_HDR_SIZE;
3479}
3480
1332e26e
AS
3481s32 brcmf_vif_set_mgmt_ie(struct brcmf_cfg80211_vif *vif, s32 pktflag,
3482 const u8 *vndr_ie_buf, u32 vndr_ie_len)
1a873342 3483{
1332e26e
AS
3484 struct brcmf_if *ifp;
3485 struct vif_saved_ie *saved_ie;
1a873342
HM
3486 s32 err = 0;
3487 u8 *iovar_ie_buf;
3488 u8 *curr_ie_buf;
3489 u8 *mgmt_ie_buf = NULL;
3e4f319d 3490 int mgmt_ie_buf_len;
81118d16 3491 u32 *mgmt_ie_len;
1a873342
HM
3492 u32 del_add_ie_buf_len = 0;
3493 u32 total_ie_buf_len = 0;
3494 u32 parsed_ie_buf_len = 0;
3495 struct parsed_vndr_ies old_vndr_ies;
3496 struct parsed_vndr_ies new_vndr_ies;
3497 struct parsed_vndr_ie_info *vndrie_info;
3498 s32 i;
3499 u8 *ptr;
3e4f319d 3500 int remained_buf_len;
1a873342 3501
1332e26e
AS
3502 if (!vif)
3503 return -ENODEV;
3504 ifp = vif->ifp;
3505 saved_ie = &vif->saved_ie;
3506
d96b801f 3507 brcmf_dbg(TRACE, "bssidx %d, pktflag : 0x%02X\n", ifp->bssidx, pktflag);
1a873342
HM
3508 iovar_ie_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
3509 if (!iovar_ie_buf)
3510 return -ENOMEM;
3511 curr_ie_buf = iovar_ie_buf;
89286dc9
HM
3512 switch (pktflag) {
3513 case BRCMF_VNDR_IE_PRBREQ_FLAG:
3514 mgmt_ie_buf = saved_ie->probe_req_ie;
3515 mgmt_ie_len = &saved_ie->probe_req_ie_len;
3516 mgmt_ie_buf_len = sizeof(saved_ie->probe_req_ie);
3517 break;
3518 case BRCMF_VNDR_IE_PRBRSP_FLAG:
3519 mgmt_ie_buf = saved_ie->probe_res_ie;
3520 mgmt_ie_len = &saved_ie->probe_res_ie_len;
3521 mgmt_ie_buf_len = sizeof(saved_ie->probe_res_ie);
3522 break;
3523 case BRCMF_VNDR_IE_BEACON_FLAG:
3524 mgmt_ie_buf = saved_ie->beacon_ie;
3525 mgmt_ie_len = &saved_ie->beacon_ie_len;
3526 mgmt_ie_buf_len = sizeof(saved_ie->beacon_ie);
3527 break;
3528 case BRCMF_VNDR_IE_ASSOCREQ_FLAG:
3529 mgmt_ie_buf = saved_ie->assoc_req_ie;
3530 mgmt_ie_len = &saved_ie->assoc_req_ie_len;
3531 mgmt_ie_buf_len = sizeof(saved_ie->assoc_req_ie);
3532 break;
3533 default:
3534 err = -EPERM;
3535 brcmf_err("not suitable type\n");
3536 goto exit;
1a873342
HM
3537 }
3538
3539 if (vndr_ie_len > mgmt_ie_buf_len) {
3540 err = -ENOMEM;
57d6e91a 3541 brcmf_err("extra IE size too big\n");
1a873342
HM
3542 goto exit;
3543 }
3544
3545 /* parse and save new vndr_ie in curr_ie_buff before comparing it */
3546 if (vndr_ie_buf && vndr_ie_len && curr_ie_buf) {
3547 ptr = curr_ie_buf;
3548 brcmf_parse_vndr_ies(vndr_ie_buf, vndr_ie_len, &new_vndr_ies);
3549 for (i = 0; i < new_vndr_ies.count; i++) {
3550 vndrie_info = &new_vndr_ies.ie_info[i];
3551 memcpy(ptr + parsed_ie_buf_len, vndrie_info->ie_ptr,
3552 vndrie_info->ie_len);
3553 parsed_ie_buf_len += vndrie_info->ie_len;
3554 }
3555 }
3556
b41fc3d7 3557 if (mgmt_ie_buf && *mgmt_ie_len) {
1a873342
HM
3558 if (parsed_ie_buf_len && (parsed_ie_buf_len == *mgmt_ie_len) &&
3559 (memcmp(mgmt_ie_buf, curr_ie_buf,
3560 parsed_ie_buf_len) == 0)) {
d96b801f 3561 brcmf_dbg(TRACE, "Previous mgmt IE equals to current IE\n");
1a873342
HM
3562 goto exit;
3563 }
3564
3565 /* parse old vndr_ie */
3566 brcmf_parse_vndr_ies(mgmt_ie_buf, *mgmt_ie_len, &old_vndr_ies);
3567
3568 /* make a command to delete old ie */
3569 for (i = 0; i < old_vndr_ies.count; i++) {
3570 vndrie_info = &old_vndr_ies.ie_info[i];
3571
d96b801f
AS
3572 brcmf_dbg(TRACE, "DEL ID : %d, Len: %d , OUI:%02x:%02x:%02x\n",
3573 vndrie_info->vndrie.id,
3574 vndrie_info->vndrie.len,
3575 vndrie_info->vndrie.oui[0],
3576 vndrie_info->vndrie.oui[1],
3577 vndrie_info->vndrie.oui[2]);
1a873342
HM
3578
3579 del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
3580 vndrie_info->ie_ptr,
3581 vndrie_info->ie_len,
3582 "del");
3583 curr_ie_buf += del_add_ie_buf_len;
3584 total_ie_buf_len += del_add_ie_buf_len;
3585 }
3586 }
3587
3588 *mgmt_ie_len = 0;
3589 /* Add if there is any extra IE */
3590 if (mgmt_ie_buf && parsed_ie_buf_len) {
3591 ptr = mgmt_ie_buf;
3592
3593 remained_buf_len = mgmt_ie_buf_len;
3594
3595 /* make a command to add new ie */
3596 for (i = 0; i < new_vndr_ies.count; i++) {
3597 vndrie_info = &new_vndr_ies.ie_info[i];
3598
b41fc3d7
HM
3599 /* verify remained buf size before copy data */
3600 if (remained_buf_len < (vndrie_info->vndrie.len +
3601 VNDR_IE_VSIE_OFFSET)) {
57d6e91a
AS
3602 brcmf_err("no space in mgmt_ie_buf: len left %d",
3603 remained_buf_len);
b41fc3d7
HM
3604 break;
3605 }
3606 remained_buf_len -= (vndrie_info->ie_len +
3607 VNDR_IE_VSIE_OFFSET);
3608
d96b801f
AS
3609 brcmf_dbg(TRACE, "ADDED ID : %d, Len: %d, OUI:%02x:%02x:%02x\n",
3610 vndrie_info->vndrie.id,
3611 vndrie_info->vndrie.len,
3612 vndrie_info->vndrie.oui[0],
3613 vndrie_info->vndrie.oui[1],
3614 vndrie_info->vndrie.oui[2]);
1a873342
HM
3615
3616 del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
3617 vndrie_info->ie_ptr,
3618 vndrie_info->ie_len,
3619 "add");
1a873342
HM
3620
3621 /* save the parsed IE in wl struct */
3622 memcpy(ptr + (*mgmt_ie_len), vndrie_info->ie_ptr,
3623 vndrie_info->ie_len);
3624 *mgmt_ie_len += vndrie_info->ie_len;
3625
3626 curr_ie_buf += del_add_ie_buf_len;
3627 total_ie_buf_len += del_add_ie_buf_len;
3628 }
3629 }
3630 if (total_ie_buf_len) {
c1179033 3631 err = brcmf_fil_bsscfg_data_set(ifp, "vndr_ie", iovar_ie_buf,
81f5dcb8 3632 total_ie_buf_len);
1a873342 3633 if (err)
57d6e91a 3634 brcmf_err("vndr ie set error : %d\n", err);
1a873342
HM
3635 }
3636
3637exit:
3638 kfree(iovar_ie_buf);
3639 return err;
3640}
3641
5f4f9f11
AS
3642s32 brcmf_vif_clear_mgmt_ies(struct brcmf_cfg80211_vif *vif)
3643{
3644 s32 pktflags[] = {
3645 BRCMF_VNDR_IE_PRBREQ_FLAG,
3646 BRCMF_VNDR_IE_PRBRSP_FLAG,
3647 BRCMF_VNDR_IE_BEACON_FLAG
3648 };
3649 int i;
3650
3651 for (i = 0; i < ARRAY_SIZE(pktflags); i++)
3652 brcmf_vif_set_mgmt_ie(vif, pktflags[i], NULL, 0);
3653
3654 memset(&vif->saved_ie, 0, sizeof(vif->saved_ie));
3655 return 0;
3656}
3657
a0f07959
HM
3658static s32
3659brcmf_config_ap_mgmt_ie(struct brcmf_cfg80211_vif *vif,
3660 struct cfg80211_beacon_data *beacon)
3661{
3662 s32 err;
3663
3664 /* Set Beacon IEs to FW */
3665 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_BEACON_FLAG,
3666 beacon->tail, beacon->tail_len);
3667 if (err) {
3668 brcmf_err("Set Beacon IE Failed\n");
3669 return err;
3670 }
3671 brcmf_dbg(TRACE, "Applied Vndr IEs for Beacon\n");
3672
3673 /* Set Probe Response IEs to FW */
3674 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBRSP_FLAG,
3675 beacon->proberesp_ies,
3676 beacon->proberesp_ies_len);
3677 if (err)
3678 brcmf_err("Set Probe Resp IE Failed\n");
3679 else
3680 brcmf_dbg(TRACE, "Applied Vndr IEs for Probe Resp\n");
3681
3682 return err;
3683}
3684
1c9d30cf
HM
3685static s32
3686brcmf_cfg80211_set_channel(struct brcmf_cfg80211_info *cfg,
3687 struct brcmf_if *ifp,
3688 struct ieee80211_channel *channel)
3689{
3690 u16 chanspec;
3691 s32 err;
3692
3693 brcmf_dbg(TRACE, "band=%d, center_freq=%d\n", channel->band,
3694 channel->center_freq);
3695
3696 chanspec = channel_to_chanspec(&cfg->d11inf, channel);
3697 err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
3698
3699 return err;
3700}
3701
1a873342
HM
3702static s32
3703brcmf_cfg80211_start_ap(struct wiphy *wiphy, struct net_device *ndev,
3704 struct cfg80211_ap_settings *settings)
3705{
3706 s32 ie_offset;
1c9d30cf 3707 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
ac24be6f 3708 struct brcmf_if *ifp = netdev_priv(ndev);
1a873342
HM
3709 struct brcmf_tlv *ssid_ie;
3710 struct brcmf_ssid_le ssid_le;
1a873342
HM
3711 s32 err = -EPERM;
3712 struct brcmf_tlv *rsn_ie;
3713 struct brcmf_vs_tlv *wpa_ie;
3714 struct brcmf_join_params join_params;
a0f07959
HM
3715 enum nl80211_iftype dev_role;
3716 struct brcmf_fil_bss_enable_le bss_enable;
1a873342 3717
d96b801f
AS
3718 brcmf_dbg(TRACE, "channel_type=%d, beacon_interval=%d, dtim_period=%d,\n",
3719 cfg80211_get_chandef_type(&settings->chandef),
3720 settings->beacon_interval,
3721 settings->dtim_period);
3722 brcmf_dbg(TRACE, "ssid=%s(%zu), auth_type=%d, inactivity_timeout=%d\n",
3723 settings->ssid, settings->ssid_len, settings->auth_type,
3724 settings->inactivity_timeout);
1a873342 3725
426d0a56 3726 dev_role = ifp->vif->wdev.iftype;
1a873342
HM
3727
3728 memset(&ssid_le, 0, sizeof(ssid_le));
3729 if (settings->ssid == NULL || settings->ssid_len == 0) {
3730 ie_offset = DOT11_MGMT_HDR_LEN + DOT11_BCN_PRB_FIXED_LEN;
3731 ssid_ie = brcmf_parse_tlvs(
3732 (u8 *)&settings->beacon.head[ie_offset],
3733 settings->beacon.head_len - ie_offset,
3734 WLAN_EID_SSID);
3735 if (!ssid_ie)
3736 return -EINVAL;
3737
3738 memcpy(ssid_le.SSID, ssid_ie->data, ssid_ie->len);
3739 ssid_le.SSID_len = cpu_to_le32(ssid_ie->len);
d96b801f 3740 brcmf_dbg(TRACE, "SSID is (%s) in Head\n", ssid_le.SSID);
1a873342
HM
3741 } else {
3742 memcpy(ssid_le.SSID, settings->ssid, settings->ssid_len);
3743 ssid_le.SSID_len = cpu_to_le32((u32)settings->ssid_len);
3744 }
3745
f96aa07e 3746 brcmf_set_mpc(ifp, 0);
b3657453 3747 brcmf_configure_arp_offload(ifp, false);
1a873342
HM
3748
3749 /* find the RSN_IE */
3750 rsn_ie = brcmf_parse_tlvs((u8 *)settings->beacon.tail,
3751 settings->beacon.tail_len, WLAN_EID_RSN);
3752
3753 /* find the WPA_IE */
3754 wpa_ie = brcmf_find_wpaie((u8 *)settings->beacon.tail,
3755 settings->beacon.tail_len);
3756
1a873342 3757 if ((wpa_ie != NULL || rsn_ie != NULL)) {
d96b801f 3758 brcmf_dbg(TRACE, "WPA(2) IE is found\n");
1a873342
HM
3759 if (wpa_ie != NULL) {
3760 /* WPA IE */
34778529 3761 err = brcmf_configure_wpaie(ndev, wpa_ie, false);
1a873342
HM
3762 if (err < 0)
3763 goto exit;
1a873342
HM
3764 } else {
3765 /* RSN IE */
3766 err = brcmf_configure_wpaie(ndev,
34778529 3767 (struct brcmf_vs_tlv *)rsn_ie, true);
1a873342
HM
3768 if (err < 0)
3769 goto exit;
1a873342 3770 }
1a873342 3771 } else {
d96b801f 3772 brcmf_dbg(TRACE, "No WPA(2) IEs found\n");
1f170110 3773 brcmf_configure_opensecurity(ifp);
1a873342 3774 }
1a873342 3775
a0f07959 3776 brcmf_config_ap_mgmt_ie(ifp->vif, &settings->beacon);
1a873342 3777
1c9d30cf
HM
3778 err = brcmf_cfg80211_set_channel(cfg, ifp, settings->chandef.chan);
3779 if (err < 0) {
3780 brcmf_err("Set Channel failed, %d\n", err);
3781 goto exit;
3782 }
3783
1a873342 3784 if (settings->beacon_interval) {
ac24be6f 3785 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD,
81f5dcb8 3786 settings->beacon_interval);
1a873342 3787 if (err < 0) {
57d6e91a 3788 brcmf_err("Beacon Interval Set Error, %d\n", err);
1a873342
HM
3789 goto exit;
3790 }
3791 }
3792 if (settings->dtim_period) {
ac24be6f 3793 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_DTIMPRD,
81f5dcb8 3794 settings->dtim_period);
1a873342 3795 if (err < 0) {
57d6e91a 3796 brcmf_err("DTIM Interval Set Error, %d\n", err);
1a873342
HM
3797 goto exit;
3798 }
3799 }
a0f07959
HM
3800
3801 if (dev_role == NL80211_IFTYPE_AP) {
3802 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
3803 if (err < 0) {
3804 brcmf_err("BRCMF_C_DOWN error %d\n", err);
3805 goto exit;
3806 }
2880b868 3807 brcmf_fil_iovar_int_set(ifp, "apsta", 0);
1a873342
HM
3808 }
3809
a0f07959 3810 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 1);
1a873342 3811 if (err < 0) {
a0f07959 3812 brcmf_err("SET INFRA error %d\n", err);
1a873342
HM
3813 goto exit;
3814 }
a0f07959
HM
3815 if (dev_role == NL80211_IFTYPE_AP) {
3816 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 1);
3817 if (err < 0) {
3818 brcmf_err("setting AP mode failed %d\n", err);
3819 goto exit;
3820 }
3821 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
3822 if (err < 0) {
3823 brcmf_err("BRCMF_C_UP error (%d)\n", err);
3824 goto exit;
3825 }
3826
3827 memset(&join_params, 0, sizeof(join_params));
3828 /* join parameters starts with ssid */
3829 memcpy(&join_params.ssid_le, &ssid_le, sizeof(ssid_le));
3830 /* create softap */
3831 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
3832 &join_params, sizeof(join_params));
3833 if (err < 0) {
3834 brcmf_err("SET SSID error (%d)\n", err);
3835 goto exit;
3836 }
3837 brcmf_dbg(TRACE, "AP mode configuration complete\n");
3838 } else {
3839 err = brcmf_fil_bsscfg_data_set(ifp, "ssid", &ssid_le,
3840 sizeof(ssid_le));
3841 if (err < 0) {
3842 brcmf_err("setting ssid failed %d\n", err);
3843 goto exit;
3844 }
3845 bss_enable.bsscfg_idx = cpu_to_le32(ifp->bssidx);
3846 bss_enable.enable = cpu_to_le32(1);
3847 err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
3848 sizeof(bss_enable));
3849 if (err < 0) {
3850 brcmf_err("bss_enable config failed %d\n", err);
3851 goto exit;
3852 }
3853
3854 brcmf_dbg(TRACE, "GO mode configuration complete\n");
3855 }
c1179033
AS
3856 clear_bit(BRCMF_VIF_STATUS_AP_CREATING, &ifp->vif->sme_state);
3857 set_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
1a873342
HM
3858
3859exit:
b3657453 3860 if (err) {
f96aa07e 3861 brcmf_set_mpc(ifp, 1);
b3657453
HM
3862 brcmf_configure_arp_offload(ifp, true);
3863 }
1a873342
HM
3864 return err;
3865}
3866
3867static int brcmf_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *ndev)
3868{
c1179033 3869 struct brcmf_if *ifp = netdev_priv(ndev);
5c33a942 3870 s32 err;
426d0a56 3871 struct brcmf_fil_bss_enable_le bss_enable;
5c33a942 3872 struct brcmf_join_params join_params;
1a873342 3873
d96b801f 3874 brcmf_dbg(TRACE, "Enter\n");
1a873342 3875
426d0a56 3876 if (ifp->vif->wdev.iftype == NL80211_IFTYPE_AP) {
1a873342
HM
3877 /* Due to most likely deauths outstanding we sleep */
3878 /* first to make sure they get processed by fw. */
3879 msleep(400);
5c33a942
HM
3880
3881 memset(&join_params, 0, sizeof(join_params));
3882 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
3883 &join_params, sizeof(join_params));
3884 if (err < 0)
3885 brcmf_err("SET SSID error (%d)\n", err);
128ce3b6 3886 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
5c33a942 3887 if (err < 0)
57d6e91a 3888 brcmf_err("BRCMF_C_UP error %d\n", err);
5c33a942
HM
3889 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 0);
3890 if (err < 0)
3891 brcmf_err("setting AP mode failed %d\n", err);
3892 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 0);
3893 if (err < 0)
3894 brcmf_err("setting INFRA mode failed %d\n", err);
426d0a56
HM
3895 } else {
3896 bss_enable.bsscfg_idx = cpu_to_le32(ifp->bssidx);
3897 bss_enable.enable = cpu_to_le32(0);
3898 err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
3899 sizeof(bss_enable));
3900 if (err < 0)
3901 brcmf_err("bss_enable config failed %d\n", err);
1a873342 3902 }
f96aa07e 3903 brcmf_set_mpc(ifp, 1);
b3657453 3904 brcmf_configure_arp_offload(ifp, true);
426d0a56
HM
3905 set_bit(BRCMF_VIF_STATUS_AP_CREATING, &ifp->vif->sme_state);
3906 clear_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
3907
1a873342
HM
3908 return err;
3909}
3910
a0f07959
HM
3911static s32
3912brcmf_cfg80211_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
3913 struct cfg80211_beacon_data *info)
3914{
a0f07959
HM
3915 struct brcmf_if *ifp = netdev_priv(ndev);
3916 s32 err;
3917
3918 brcmf_dbg(TRACE, "Enter\n");
3919
a0f07959
HM
3920 err = brcmf_config_ap_mgmt_ie(ifp->vif, info);
3921
3922 return err;
3923}
3924
1a873342
HM
3925static int
3926brcmf_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev,
3927 u8 *mac)
3928{
a0f07959 3929 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1a873342 3930 struct brcmf_scb_val_le scbval;
0abb5f21 3931 struct brcmf_if *ifp = netdev_priv(ndev);
1a873342
HM
3932 s32 err;
3933
3934 if (!mac)
3935 return -EFAULT;
3936
d96b801f 3937 brcmf_dbg(TRACE, "Enter %pM\n", mac);
1a873342 3938
a0f07959
HM
3939 if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
3940 ifp = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
ce81e317 3941 if (!check_vif_up(ifp->vif))
1a873342
HM
3942 return -EIO;
3943
3944 memcpy(&scbval.ea, mac, ETH_ALEN);
3945 scbval.val = cpu_to_le32(WLAN_REASON_DEAUTH_LEAVING);
0abb5f21 3946 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCB_DEAUTHENTICATE_FOR_REASON,
81f5dcb8 3947 &scbval, sizeof(scbval));
1a873342 3948 if (err)
57d6e91a 3949 brcmf_err("SCB_DEAUTHENTICATE_FOR_REASON failed %d\n", err);
7ab6acd0 3950
d96b801f 3951 brcmf_dbg(TRACE, "Exit\n");
1a873342
HM
3952 return err;
3953}
3954
0de8aace
HM
3955
3956static void
3957brcmf_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
3958 struct wireless_dev *wdev,
3959 u16 frame_type, bool reg)
3960{
7fa2e352 3961 struct brcmf_cfg80211_vif *vif;
0de8aace
HM
3962 u16 mgmt_type;
3963
3964 brcmf_dbg(TRACE, "Enter, frame_type %04x, reg=%d\n", frame_type, reg);
3965
3966 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
7fa2e352 3967 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
0de8aace
HM
3968 if (reg)
3969 vif->mgmt_rx_reg |= BIT(mgmt_type);
3970 else
318a64ce 3971 vif->mgmt_rx_reg &= ~BIT(mgmt_type);
0de8aace
HM
3972}
3973
3974
3975static int
3976brcmf_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
b176e629 3977 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
0de8aace
HM
3978{
3979 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
b176e629
AO
3980 struct ieee80211_channel *chan = params->chan;
3981 const u8 *buf = params->buf;
3982 size_t len = params->len;
0de8aace
HM
3983 const struct ieee80211_mgmt *mgmt;
3984 struct brcmf_cfg80211_vif *vif;
3985 s32 err = 0;
3986 s32 ie_offset;
3987 s32 ie_len;
18e2f61d
HM
3988 struct brcmf_fil_action_frame_le *action_frame;
3989 struct brcmf_fil_af_params_le *af_params;
3990 bool ack;
3991 s32 chan_nr;
c2ff8cad 3992 u32 freq;
0de8aace
HM
3993
3994 brcmf_dbg(TRACE, "Enter\n");
3995
3996 *cookie = 0;
3997
3998 mgmt = (const struct ieee80211_mgmt *)buf;
3999
a0f07959
HM
4000 if (!ieee80211_is_mgmt(mgmt->frame_control)) {
4001 brcmf_err("Driver only allows MGMT packet type\n");
4002 return -EPERM;
4003 }
0de8aace 4004
c2ff8cad
AQ
4005 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4006
a0f07959
HM
4007 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
4008 /* Right now the only reason to get a probe response */
4009 /* is for p2p listen response or for p2p GO from */
4010 /* wpa_supplicant. Unfortunately the probe is send */
4011 /* on primary ndev, while dongle wants it on the p2p */
4012 /* vif. Since this is only reason for a probe */
4013 /* response to be sent, the vif is taken from cfg. */
4014 /* If ever desired to send proberesp for non p2p */
4015 /* response then data should be checked for */
4016 /* "DIRECT-". Note in future supplicant will take */
4017 /* dedicated p2p wdev to do this and then this 'hack'*/
4018 /* is not needed anymore. */
4019 ie_offset = DOT11_MGMT_HDR_LEN +
4020 DOT11_BCN_PRB_FIXED_LEN;
4021 ie_len = len - ie_offset;
a0f07959 4022 if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif)
0de8aace 4023 vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
a0f07959
HM
4024 err = brcmf_vif_set_mgmt_ie(vif,
4025 BRCMF_VNDR_IE_PRBRSP_FLAG,
4026 &buf[ie_offset],
4027 ie_len);
4028 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
4029 GFP_KERNEL);
18e2f61d
HM
4030 } else if (ieee80211_is_action(mgmt->frame_control)) {
4031 af_params = kzalloc(sizeof(*af_params), GFP_KERNEL);
4032 if (af_params == NULL) {
4033 brcmf_err("unable to allocate frame\n");
4034 err = -ENOMEM;
4035 goto exit;
4036 }
4037 action_frame = &af_params->action_frame;
4038 /* Add the packet Id */
4039 action_frame->packet_id = cpu_to_le32(*cookie);
4040 /* Add BSSID */
4041 memcpy(&action_frame->da[0], &mgmt->da[0], ETH_ALEN);
4042 memcpy(&af_params->bssid[0], &mgmt->bssid[0], ETH_ALEN);
4043 /* Add the length exepted for 802.11 header */
4044 action_frame->len = cpu_to_le16(len - DOT11_MGMT_HDR_LEN);
c2ff8cad
AQ
4045 /* Add the channel. Use the one specified as parameter if any or
4046 * the current one (got from the firmware) otherwise
4047 */
4048 if (chan)
4049 freq = chan->center_freq;
4050 else
4051 brcmf_fil_cmd_int_get(vif->ifp, BRCMF_C_GET_CHANNEL,
4052 &freq);
4053 chan_nr = ieee80211_frequency_to_channel(freq);
18e2f61d
HM
4054 af_params->channel = cpu_to_le32(chan_nr);
4055
4056 memcpy(action_frame->data, &buf[DOT11_MGMT_HDR_LEN],
4057 le16_to_cpu(action_frame->len));
4058
4059 brcmf_dbg(TRACE, "Action frame, cookie=%lld, len=%d, freq=%d\n",
86a9c4a2 4060 *cookie, le16_to_cpu(action_frame->len), freq);
18e2f61d 4061
7fa2e352 4062 ack = brcmf_p2p_send_action_frame(cfg, cfg_to_ndev(cfg),
18e2f61d
HM
4063 af_params);
4064
4065 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, ack,
4066 GFP_KERNEL);
4067 kfree(af_params);
a0f07959
HM
4068 } else {
4069 brcmf_dbg(TRACE, "Unhandled, fc=%04x!!\n", mgmt->frame_control);
4070 brcmf_dbg_hex_dump(true, buf, len, "payload, len=%Zu\n", len);
0de8aace 4071 }
a0f07959 4072
18e2f61d 4073exit:
0de8aace
HM
4074 return err;
4075}
4076
4077
4078static int
4079brcmf_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
4080 struct wireless_dev *wdev,
4081 u64 cookie)
4082{
4083 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4084 struct brcmf_cfg80211_vif *vif;
4085 int err = 0;
4086
4087 brcmf_dbg(TRACE, "Enter p2p listen cancel\n");
4088
4089 vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
4090 if (vif == NULL) {
4091 brcmf_err("No p2p device available for probe response\n");
4092 err = -ENODEV;
4093 goto exit;
4094 }
4095 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
4096exit:
4097 return err;
4098}
4099
61730d4d
PH
4100static int brcmf_cfg80211_crit_proto_start(struct wiphy *wiphy,
4101 struct wireless_dev *wdev,
4102 enum nl80211_crit_proto_id proto,
4103 u16 duration)
4104{
4105 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4106 struct brcmf_cfg80211_vif *vif;
4107
4108 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4109
4110 /* only DHCP support for now */
4111 if (proto != NL80211_CRIT_PROTO_DHCP)
4112 return -EINVAL;
4113
4114 /* suppress and abort scanning */
4115 set_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
4116 brcmf_abort_scanning(cfg);
4117
4118 return brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_DISABLED, duration);
4119}
4120
4121static void brcmf_cfg80211_crit_proto_stop(struct wiphy *wiphy,
4122 struct wireless_dev *wdev)
4123{
4124 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4125 struct brcmf_cfg80211_vif *vif;
4126
4127 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4128
4129 brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
4130 clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
4131}
4132
89c2f382
AS
4133static int brcmf_convert_nl80211_tdls_oper(enum nl80211_tdls_operation oper)
4134{
4135 int ret;
4136
4137 switch (oper) {
4138 case NL80211_TDLS_DISCOVERY_REQ:
4139 ret = BRCMF_TDLS_MANUAL_EP_DISCOVERY;
4140 break;
4141 case NL80211_TDLS_SETUP:
4142 ret = BRCMF_TDLS_MANUAL_EP_CREATE;
4143 break;
4144 case NL80211_TDLS_TEARDOWN:
4145 ret = BRCMF_TDLS_MANUAL_EP_DELETE;
4146 break;
4147 default:
4148 brcmf_err("unsupported operation: %d\n", oper);
4149 ret = -EOPNOTSUPP;
4150 }
4151 return ret;
4152}
4153
4154static int brcmf_cfg80211_tdls_oper(struct wiphy *wiphy,
4155 struct net_device *ndev, u8 *peer,
4156 enum nl80211_tdls_operation oper)
4157{
4158 struct brcmf_if *ifp;
4159 struct brcmf_tdls_iovar_le info;
4160 int ret = 0;
4161
4162 ret = brcmf_convert_nl80211_tdls_oper(oper);
4163 if (ret < 0)
4164 return ret;
4165
4166 ifp = netdev_priv(ndev);
4167 memset(&info, 0, sizeof(info));
4168 info.mode = (u8)ret;
4169 if (peer)
4170 memcpy(info.ea, peer, ETH_ALEN);
4171
4172 ret = brcmf_fil_iovar_data_set(ifp, "tdls_endpoint",
4173 &info, sizeof(info));
4174 if (ret < 0)
4175 brcmf_err("tdls_endpoint iovar failed: ret=%d\n", ret);
4176
4177 return ret;
4178}
4179
5b435de0 4180static struct cfg80211_ops wl_cfg80211_ops = {
9f440b7b
AS
4181 .add_virtual_intf = brcmf_cfg80211_add_iface,
4182 .del_virtual_intf = brcmf_cfg80211_del_iface,
5b435de0
AS
4183 .change_virtual_intf = brcmf_cfg80211_change_iface,
4184 .scan = brcmf_cfg80211_scan,
4185 .set_wiphy_params = brcmf_cfg80211_set_wiphy_params,
4186 .join_ibss = brcmf_cfg80211_join_ibss,
4187 .leave_ibss = brcmf_cfg80211_leave_ibss,
4188 .get_station = brcmf_cfg80211_get_station,
4189 .set_tx_power = brcmf_cfg80211_set_tx_power,
4190 .get_tx_power = brcmf_cfg80211_get_tx_power,
4191 .add_key = brcmf_cfg80211_add_key,
4192 .del_key = brcmf_cfg80211_del_key,
4193 .get_key = brcmf_cfg80211_get_key,
4194 .set_default_key = brcmf_cfg80211_config_default_key,
4195 .set_default_mgmt_key = brcmf_cfg80211_config_default_mgmt_key,
4196 .set_power_mgmt = brcmf_cfg80211_set_power_mgmt,
5b435de0
AS
4197 .connect = brcmf_cfg80211_connect,
4198 .disconnect = brcmf_cfg80211_disconnect,
4199 .suspend = brcmf_cfg80211_suspend,
4200 .resume = brcmf_cfg80211_resume,
4201 .set_pmksa = brcmf_cfg80211_set_pmksa,
4202 .del_pmksa = brcmf_cfg80211_del_pmksa,
cbaa177d 4203 .flush_pmksa = brcmf_cfg80211_flush_pmksa,
1a873342
HM
4204 .start_ap = brcmf_cfg80211_start_ap,
4205 .stop_ap = brcmf_cfg80211_stop_ap,
a0f07959 4206 .change_beacon = brcmf_cfg80211_change_beacon,
1a873342 4207 .del_station = brcmf_cfg80211_del_station,
e5806072
AS
4208 .sched_scan_start = brcmf_cfg80211_sched_scan_start,
4209 .sched_scan_stop = brcmf_cfg80211_sched_scan_stop,
0de8aace
HM
4210 .mgmt_frame_register = brcmf_cfg80211_mgmt_frame_register,
4211 .mgmt_tx = brcmf_cfg80211_mgmt_tx,
4212 .remain_on_channel = brcmf_p2p_remain_on_channel,
4213 .cancel_remain_on_channel = brcmf_cfg80211_cancel_remain_on_channel,
27f10e38
AS
4214 .start_p2p_device = brcmf_p2p_start_device,
4215 .stop_p2p_device = brcmf_p2p_stop_device,
61730d4d
PH
4216 .crit_proto_start = brcmf_cfg80211_crit_proto_start,
4217 .crit_proto_stop = brcmf_cfg80211_crit_proto_stop,
89c2f382 4218 .tdls_oper = brcmf_cfg80211_tdls_oper,
e3335476 4219 CFG80211_TESTMODE_CMD(brcmf_cfg80211_testmode)
5b435de0
AS
4220};
4221
9f440b7b 4222static s32 brcmf_nl80211_iftype_to_mode(enum nl80211_iftype type)
5b435de0 4223{
9f440b7b
AS
4224 switch (type) {
4225 case NL80211_IFTYPE_AP_VLAN:
4226 case NL80211_IFTYPE_WDS:
4227 case NL80211_IFTYPE_MONITOR:
4228 case NL80211_IFTYPE_MESH_POINT:
4229 return -ENOTSUPP;
4230 case NL80211_IFTYPE_ADHOC:
4231 return WL_MODE_IBSS;
4232 case NL80211_IFTYPE_STATION:
4233 case NL80211_IFTYPE_P2P_CLIENT:
4234 return WL_MODE_BSS;
4235 case NL80211_IFTYPE_AP:
4236 case NL80211_IFTYPE_P2P_GO:
4237 return WL_MODE_AP;
4238 case NL80211_IFTYPE_P2P_DEVICE:
4239 return WL_MODE_P2P;
4240 case NL80211_IFTYPE_UNSPECIFIED:
5b435de0 4241 default:
9f440b7b 4242 break;
5b435de0
AS
4243 }
4244
9f440b7b 4245 return -EINVAL;
5b435de0
AS
4246}
4247
e5806072
AS
4248static void brcmf_wiphy_pno_params(struct wiphy *wiphy)
4249{
e5806072
AS
4250 /* scheduled scan settings */
4251 wiphy->max_sched_scan_ssids = BRCMF_PNO_MAX_PFN_COUNT;
4252 wiphy->max_match_sets = BRCMF_PNO_MAX_PFN_COUNT;
4253 wiphy->max_sched_scan_ie_len = BRCMF_SCAN_IE_LEN_MAX;
4254 wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
e5806072
AS
4255}
4256
9f440b7b
AS
4257static const struct ieee80211_iface_limit brcmf_iface_limits[] = {
4258 {
dded3d53 4259 .max = 2,
9f440b7b
AS
4260 .types = BIT(NL80211_IFTYPE_STATION) |
4261 BIT(NL80211_IFTYPE_ADHOC) |
4262 BIT(NL80211_IFTYPE_AP)
4263 },
4264 {
4265 .max = 1,
4266 .types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
4267 BIT(NL80211_IFTYPE_P2P_GO)
4268 },
9af221b3
AS
4269 {
4270 .max = 1,
4271 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
4272 }
9f440b7b
AS
4273};
4274static const struct ieee80211_iface_combination brcmf_iface_combos[] = {
4275 {
dded3d53 4276 .max_interfaces = BRCMF_IFACE_MAX_CNT,
1c9d30cf 4277 .num_different_channels = 2,
9f440b7b
AS
4278 .n_limits = ARRAY_SIZE(brcmf_iface_limits),
4279 .limits = brcmf_iface_limits
4280 }
4281};
4282
0de8aace
HM
4283static const struct ieee80211_txrx_stypes
4284brcmf_txrx_stypes[NUM_NL80211_IFTYPES] = {
4285 [NL80211_IFTYPE_STATION] = {
4286 .tx = 0xffff,
4287 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
4288 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
4289 },
4290 [NL80211_IFTYPE_P2P_CLIENT] = {
4291 .tx = 0xffff,
4292 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
4293 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
4294 },
4295 [NL80211_IFTYPE_P2P_GO] = {
4296 .tx = 0xffff,
4297 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
4298 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
4299 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
4300 BIT(IEEE80211_STYPE_DISASSOC >> 4) |
4301 BIT(IEEE80211_STYPE_AUTH >> 4) |
4302 BIT(IEEE80211_STYPE_DEAUTH >> 4) |
4303 BIT(IEEE80211_STYPE_ACTION >> 4)
bffc61c9
AS
4304 },
4305 [NL80211_IFTYPE_P2P_DEVICE] = {
4306 .tx = 0xffff,
4307 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
4308 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
0de8aace
HM
4309 }
4310};
4311
3eacf866 4312static struct wiphy *brcmf_setup_wiphy(struct device *phydev)
5b435de0 4313{
3eacf866 4314 struct wiphy *wiphy;
5b435de0
AS
4315 s32 err = 0;
4316
3eacf866
AS
4317 wiphy = wiphy_new(&wl_cfg80211_ops, sizeof(struct brcmf_cfg80211_info));
4318 if (!wiphy) {
57d6e91a 4319 brcmf_err("Could not allocate wiphy device\n");
3eacf866
AS
4320 return ERR_PTR(-ENOMEM);
4321 }
4322 set_wiphy_dev(wiphy, phydev);
4323 wiphy->max_scan_ssids = WL_NUM_SCAN_MAX;
9f440b7b 4324 wiphy->max_scan_ie_len = BRCMF_SCAN_IE_LEN_MAX;
3eacf866
AS
4325 wiphy->max_num_pmkids = WL_NUM_PMKIDS_MAX;
4326 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4327 BIT(NL80211_IFTYPE_ADHOC) |
9f440b7b
AS
4328 BIT(NL80211_IFTYPE_AP) |
4329 BIT(NL80211_IFTYPE_P2P_CLIENT) |
9af221b3
AS
4330 BIT(NL80211_IFTYPE_P2P_GO) |
4331 BIT(NL80211_IFTYPE_P2P_DEVICE);
9f440b7b
AS
4332 wiphy->iface_combinations = brcmf_iface_combos;
4333 wiphy->n_iface_combinations = ARRAY_SIZE(brcmf_iface_combos);
3eacf866 4334 wiphy->bands[IEEE80211_BAND_2GHZ] = &__wl_band_2ghz;
3eacf866
AS
4335 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
4336 wiphy->cipher_suites = __wl_cipher_suites;
4337 wiphy->n_cipher_suites = ARRAY_SIZE(__wl_cipher_suites);
0de8aace 4338 wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT |
6eda4e2c 4339 WIPHY_FLAG_OFFCHAN_TX |
89c2f382
AS
4340 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
4341 WIPHY_FLAG_SUPPORTS_TDLS;
0de8aace
HM
4342 wiphy->mgmt_stypes = brcmf_txrx_stypes;
4343 wiphy->max_remain_on_channel_duration = 5000;
3eacf866 4344 brcmf_wiphy_pno_params(wiphy);
d48200ba 4345 brcmf_dbg(INFO, "Registering custom regulatory\n");
a2f73b6c 4346 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
d48200ba 4347 wiphy_apply_custom_regulatory(wiphy, &brcmf_regdom);
3eacf866 4348 err = wiphy_register(wiphy);
5b435de0 4349 if (err < 0) {
57d6e91a 4350 brcmf_err("Could not register wiphy device (%d)\n", err);
3eacf866
AS
4351 wiphy_free(wiphy);
4352 return ERR_PTR(err);
5b435de0 4353 }
3eacf866
AS
4354 return wiphy;
4355}
4356
3eacf866 4357struct brcmf_cfg80211_vif *brcmf_alloc_vif(struct brcmf_cfg80211_info *cfg,
9f440b7b
AS
4358 enum nl80211_iftype type,
4359 bool pm_block)
3eacf866
AS
4360{
4361 struct brcmf_cfg80211_vif *vif;
5b435de0 4362
33a6b157 4363 brcmf_dbg(TRACE, "allocating virtual interface (size=%zu)\n",
9f440b7b 4364 sizeof(*vif));
3eacf866
AS
4365 vif = kzalloc(sizeof(*vif), GFP_KERNEL);
4366 if (!vif)
4367 return ERR_PTR(-ENOMEM);
4368
4369 vif->wdev.wiphy = cfg->wiphy;
9f440b7b 4370 vif->wdev.iftype = type;
5b435de0 4371
9f440b7b 4372 vif->mode = brcmf_nl80211_iftype_to_mode(type);
3eacf866
AS
4373 vif->pm_block = pm_block;
4374 vif->roam_off = -1;
4375
6ac4f4ed
AS
4376 brcmf_init_prof(&vif->profile);
4377
3eacf866 4378 list_add_tail(&vif->list, &cfg->vif_list);
3eacf866 4379 return vif;
5b435de0
AS
4380}
4381
427dec5f 4382void brcmf_free_vif(struct brcmf_cfg80211_vif *vif)
5b435de0 4383{
3eacf866 4384 list_del(&vif->list);
3eacf866 4385 kfree(vif);
5b435de0
AS
4386}
4387
903e0eee 4388static bool brcmf_is_linkup(const struct brcmf_event_msg *e)
5b435de0 4389{
5c36b99a
AS
4390 u32 event = e->event_code;
4391 u32 status = e->status;
5b435de0
AS
4392
4393 if (event == BRCMF_E_SET_SSID && status == BRCMF_E_STATUS_SUCCESS) {
16886735 4394 brcmf_dbg(CONN, "Processing set ssid\n");
5b435de0
AS
4395 return true;
4396 }
4397
4398 return false;
4399}
4400
903e0eee 4401static bool brcmf_is_linkdown(const struct brcmf_event_msg *e)
5b435de0 4402{
5c36b99a
AS
4403 u32 event = e->event_code;
4404 u16 flags = e->flags;
5b435de0
AS
4405
4406 if (event == BRCMF_E_LINK && (!(flags & BRCMF_EVENT_MSG_LINK))) {
16886735 4407 brcmf_dbg(CONN, "Processing link down\n");
5b435de0
AS
4408 return true;
4409 }
4410 return false;
4411}
4412
27a68fe3 4413static bool brcmf_is_nonetwork(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
4414 const struct brcmf_event_msg *e)
4415{
5c36b99a
AS
4416 u32 event = e->event_code;
4417 u32 status = e->status;
5b435de0
AS
4418
4419 if (event == BRCMF_E_LINK && status == BRCMF_E_STATUS_NO_NETWORKS) {
16886735
AS
4420 brcmf_dbg(CONN, "Processing Link %s & no network found\n",
4421 e->flags & BRCMF_EVENT_MSG_LINK ? "up" : "down");
5b435de0
AS
4422 return true;
4423 }
4424
4425 if (event == BRCMF_E_SET_SSID && status != BRCMF_E_STATUS_SUCCESS) {
16886735 4426 brcmf_dbg(CONN, "Processing connecting & no network found\n");
5b435de0
AS
4427 return true;
4428 }
4429
4430 return false;
4431}
4432
27a68fe3 4433static void brcmf_clear_assoc_ies(struct brcmf_cfg80211_info *cfg)
5b435de0 4434{
27a68fe3 4435 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5b435de0
AS
4436
4437 kfree(conn_info->req_ie);
4438 conn_info->req_ie = NULL;
4439 conn_info->req_ie_len = 0;
4440 kfree(conn_info->resp_ie);
4441 conn_info->resp_ie = NULL;
4442 conn_info->resp_ie_len = 0;
4443}
4444
89286dc9
HM
4445static s32 brcmf_get_assoc_ies(struct brcmf_cfg80211_info *cfg,
4446 struct brcmf_if *ifp)
5b435de0 4447{
c4e382d2 4448 struct brcmf_cfg80211_assoc_ielen_le *assoc_info;
27a68fe3 4449 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5b435de0
AS
4450 u32 req_len;
4451 u32 resp_len;
4452 s32 err = 0;
4453
27a68fe3 4454 brcmf_clear_assoc_ies(cfg);
5b435de0 4455
ac24be6f
AS
4456 err = brcmf_fil_iovar_data_get(ifp, "assoc_info",
4457 cfg->extra_buf, WL_ASSOC_INFO_MAX);
5b435de0 4458 if (err) {
57d6e91a 4459 brcmf_err("could not get assoc info (%d)\n", err);
5b435de0
AS
4460 return err;
4461 }
c4e382d2 4462 assoc_info =
27a68fe3 4463 (struct brcmf_cfg80211_assoc_ielen_le *)cfg->extra_buf;
c4e382d2
AS
4464 req_len = le32_to_cpu(assoc_info->req_len);
4465 resp_len = le32_to_cpu(assoc_info->resp_len);
5b435de0 4466 if (req_len) {
ac24be6f 4467 err = brcmf_fil_iovar_data_get(ifp, "assoc_req_ies",
81f5dcb8
HM
4468 cfg->extra_buf,
4469 WL_ASSOC_INFO_MAX);
5b435de0 4470 if (err) {
57d6e91a 4471 brcmf_err("could not get assoc req (%d)\n", err);
5b435de0
AS
4472 return err;
4473 }
4474 conn_info->req_ie_len = req_len;
4475 conn_info->req_ie =
27a68fe3 4476 kmemdup(cfg->extra_buf, conn_info->req_ie_len,
5b435de0
AS
4477 GFP_KERNEL);
4478 } else {
4479 conn_info->req_ie_len = 0;
4480 conn_info->req_ie = NULL;
4481 }
4482 if (resp_len) {
ac24be6f 4483 err = brcmf_fil_iovar_data_get(ifp, "assoc_resp_ies",
81f5dcb8
HM
4484 cfg->extra_buf,
4485 WL_ASSOC_INFO_MAX);
5b435de0 4486 if (err) {
57d6e91a 4487 brcmf_err("could not get assoc resp (%d)\n", err);
5b435de0
AS
4488 return err;
4489 }
4490 conn_info->resp_ie_len = resp_len;
4491 conn_info->resp_ie =
27a68fe3 4492 kmemdup(cfg->extra_buf, conn_info->resp_ie_len,
5b435de0
AS
4493 GFP_KERNEL);
4494 } else {
4495 conn_info->resp_ie_len = 0;
4496 conn_info->resp_ie = NULL;
4497 }
16886735
AS
4498 brcmf_dbg(CONN, "req len (%d) resp len (%d)\n",
4499 conn_info->req_ie_len, conn_info->resp_ie_len);
5b435de0
AS
4500
4501 return err;
4502}
4503
4504static s32
27a68fe3 4505brcmf_bss_roaming_done(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
4506 struct net_device *ndev,
4507 const struct brcmf_event_msg *e)
4508{
c1179033
AS
4509 struct brcmf_if *ifp = netdev_priv(ndev);
4510 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
27a68fe3
AS
4511 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
4512 struct wiphy *wiphy = cfg_to_wiphy(cfg);
a180b83b 4513 struct ieee80211_channel *notify_channel = NULL;
5b435de0 4514 struct ieee80211_supported_band *band;
a180b83b 4515 struct brcmf_bss_info_le *bi;
83cf17aa 4516 struct brcmu_chan ch;
5b435de0
AS
4517 u32 freq;
4518 s32 err = 0;
a180b83b 4519 u8 *buf;
5b435de0 4520
d96b801f 4521 brcmf_dbg(TRACE, "Enter\n");
5b435de0 4522
89286dc9 4523 brcmf_get_assoc_ies(cfg, ifp);
6c8c4f72 4524 memcpy(profile->bssid, e->addr, ETH_ALEN);
89286dc9 4525 brcmf_update_bss_info(cfg, ifp);
5b435de0 4526
a180b83b
FL
4527 buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
4528 if (buf == NULL) {
4529 err = -ENOMEM;
4530 goto done;
4531 }
4532
4533 /* data sent to dongle has to be little endian */
4534 *(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
c1179033 4535 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
ac24be6f 4536 buf, WL_BSS_INFO_MAX);
a180b83b
FL
4537
4538 if (err)
4539 goto done;
5b435de0 4540
a180b83b 4541 bi = (struct brcmf_bss_info_le *)(buf + 4);
83cf17aa
FL
4542 ch.chspec = le16_to_cpu(bi->chanspec);
4543 cfg->d11inf.decchspec(&ch);
5b435de0 4544
83cf17aa 4545 if (ch.band == BRCMU_CHAN_BAND_2G)
5b435de0
AS
4546 band = wiphy->bands[IEEE80211_BAND_2GHZ];
4547 else
4548 band = wiphy->bands[IEEE80211_BAND_5GHZ];
4549
83cf17aa 4550 freq = ieee80211_channel_to_frequency(ch.chnum, band->band);
5b435de0
AS
4551 notify_channel = ieee80211_get_channel(wiphy, freq);
4552
a180b83b
FL
4553done:
4554 kfree(buf);
06bb123e 4555 cfg80211_roamed(ndev, notify_channel, (u8 *)profile->bssid,
5b435de0
AS
4556 conn_info->req_ie, conn_info->req_ie_len,
4557 conn_info->resp_ie, conn_info->resp_ie_len, GFP_KERNEL);
16886735 4558 brcmf_dbg(CONN, "Report roaming result\n");
5b435de0 4559
c1179033 4560 set_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
d96b801f 4561 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
4562 return err;
4563}
4564
4565static s32
27a68fe3 4566brcmf_bss_connect_done(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
4567 struct net_device *ndev, const struct brcmf_event_msg *e,
4568 bool completed)
4569{
c1179033
AS
4570 struct brcmf_if *ifp = netdev_priv(ndev);
4571 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
27a68fe3 4572 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5b435de0
AS
4573 s32 err = 0;
4574
d96b801f 4575 brcmf_dbg(TRACE, "Enter\n");
5b435de0 4576
c1179033
AS
4577 if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4578 &ifp->vif->sme_state)) {
5b435de0 4579 if (completed) {
89286dc9 4580 brcmf_get_assoc_ies(cfg, ifp);
6c8c4f72 4581 memcpy(profile->bssid, e->addr, ETH_ALEN);
89286dc9
HM
4582 brcmf_update_bss_info(cfg, ifp);
4583 set_bit(BRCMF_VIF_STATUS_CONNECTED,
4584 &ifp->vif->sme_state);
5b435de0
AS
4585 }
4586 cfg80211_connect_result(ndev,
06bb123e 4587 (u8 *)profile->bssid,
5b435de0
AS
4588 conn_info->req_ie,
4589 conn_info->req_ie_len,
4590 conn_info->resp_ie,
4591 conn_info->resp_ie_len,
4592 completed ? WLAN_STATUS_SUCCESS :
4593 WLAN_STATUS_AUTH_TIMEOUT,
4594 GFP_KERNEL);
16886735
AS
4595 brcmf_dbg(CONN, "Report connect result - connection %s\n",
4596 completed ? "succeeded" : "failed");
5b435de0 4597 }
d96b801f 4598 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
4599 return err;
4600}
4601
4602static s32
27a68fe3 4603brcmf_notify_connect_status_ap(struct brcmf_cfg80211_info *cfg,
1a873342
HM
4604 struct net_device *ndev,
4605 const struct brcmf_event_msg *e, void *data)
4606{
7ee29602 4607 static int generation;
5c36b99a
AS
4608 u32 event = e->event_code;
4609 u32 reason = e->reason;
1a873342
HM
4610 struct station_info sinfo;
4611
16886735 4612 brcmf_dbg(CONN, "event %d, reason %d\n", event, reason);
5f4f9f11
AS
4613 if (event == BRCMF_E_LINK && reason == BRCMF_E_REASON_LINK_BSSCFG_DIS &&
4614 ndev != cfg_to_ndev(cfg)) {
4615 brcmf_dbg(CONN, "AP mode link down\n");
4616 complete(&cfg->vif_disabled);
4617 return 0;
4618 }
1a873342 4619
1a873342 4620 if (((event == BRCMF_E_ASSOC_IND) || (event == BRCMF_E_REASSOC_IND)) &&
7ee29602
HM
4621 (reason == BRCMF_E_STATUS_SUCCESS)) {
4622 memset(&sinfo, 0, sizeof(sinfo));
1a873342
HM
4623 sinfo.filled = STATION_INFO_ASSOC_REQ_IES;
4624 if (!data) {
57d6e91a 4625 brcmf_err("No IEs present in ASSOC/REASSOC_IND");
1a873342
HM
4626 return -EINVAL;
4627 }
4628 sinfo.assoc_req_ies = data;
7ee29602 4629 sinfo.assoc_req_ies_len = e->datalen;
1a873342
HM
4630 generation++;
4631 sinfo.generation = generation;
7ee29602 4632 cfg80211_new_sta(ndev, e->addr, &sinfo, GFP_KERNEL);
1a873342
HM
4633 } else if ((event == BRCMF_E_DISASSOC_IND) ||
4634 (event == BRCMF_E_DEAUTH_IND) ||
4635 (event == BRCMF_E_DEAUTH)) {
7ee29602 4636 cfg80211_del_sta(ndev, e->addr, GFP_KERNEL);
1a873342 4637 }
7ee29602 4638 return 0;
1a873342
HM
4639}
4640
5b435de0 4641static s32
1993732e 4642brcmf_notify_connect_status(struct brcmf_if *ifp,
5b435de0
AS
4643 const struct brcmf_event_msg *e, void *data)
4644{
1993732e
AS
4645 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
4646 struct net_device *ndev = ifp->ndev;
c1179033 4647 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5b435de0
AS
4648 s32 err = 0;
4649
128ce3b6 4650 if (ifp->vif->mode == WL_MODE_AP) {
27a68fe3 4651 err = brcmf_notify_connect_status_ap(cfg, ndev, e, data);
903e0eee 4652 } else if (brcmf_is_linkup(e)) {
16886735 4653 brcmf_dbg(CONN, "Linkup\n");
128ce3b6 4654 if (brcmf_is_ibssmode(ifp->vif)) {
6c8c4f72 4655 memcpy(profile->bssid, e->addr, ETH_ALEN);
27a68fe3 4656 wl_inform_ibss(cfg, ndev, e->addr);
5b435de0 4657 cfg80211_ibss_joined(ndev, e->addr, GFP_KERNEL);
c1179033
AS
4658 clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4659 &ifp->vif->sme_state);
4660 set_bit(BRCMF_VIF_STATUS_CONNECTED,
4661 &ifp->vif->sme_state);
5b435de0 4662 } else
27a68fe3 4663 brcmf_bss_connect_done(cfg, ndev, e, true);
903e0eee 4664 } else if (brcmf_is_linkdown(e)) {
16886735 4665 brcmf_dbg(CONN, "Linkdown\n");
128ce3b6 4666 if (!brcmf_is_ibssmode(ifp->vif)) {
27a68fe3 4667 brcmf_bss_connect_done(cfg, ndev, e, false);
c1179033 4668 if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTED,
903e0eee 4669 &ifp->vif->sme_state))
5b435de0 4670 cfg80211_disconnected(ndev, 0, NULL, 0,
c1179033 4671 GFP_KERNEL);
5b435de0 4672 }
903e0eee 4673 brcmf_link_down(ifp->vif);
6ac4f4ed 4674 brcmf_init_prof(ndev_to_prof(ndev));
5f4f9f11
AS
4675 if (ndev != cfg_to_ndev(cfg))
4676 complete(&cfg->vif_disabled);
27a68fe3 4677 } else if (brcmf_is_nonetwork(cfg, e)) {
128ce3b6 4678 if (brcmf_is_ibssmode(ifp->vif))
c1179033
AS
4679 clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4680 &ifp->vif->sme_state);
5b435de0 4681 else
27a68fe3 4682 brcmf_bss_connect_done(cfg, ndev, e, false);
5b435de0
AS
4683 }
4684
4685 return err;
4686}
4687
4688static s32
1993732e 4689brcmf_notify_roaming_status(struct brcmf_if *ifp,
5b435de0
AS
4690 const struct brcmf_event_msg *e, void *data)
4691{
1993732e 4692 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5b435de0 4693 s32 err = 0;
5c36b99a
AS
4694 u32 event = e->event_code;
4695 u32 status = e->status;
5b435de0
AS
4696
4697 if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) {
c1179033 4698 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state))
1993732e 4699 brcmf_bss_roaming_done(cfg, ifp->ndev, e);
5b435de0 4700 else
1993732e 4701 brcmf_bss_connect_done(cfg, ifp->ndev, e, true);
5b435de0
AS
4702 }
4703
4704 return err;
4705}
4706
4707static s32
1993732e 4708brcmf_notify_mic_status(struct brcmf_if *ifp,
5b435de0
AS
4709 const struct brcmf_event_msg *e, void *data)
4710{
5c36b99a 4711 u16 flags = e->flags;
5b435de0
AS
4712 enum nl80211_key_type key_type;
4713
4714 if (flags & BRCMF_EVENT_MSG_GROUP)
4715 key_type = NL80211_KEYTYPE_GROUP;
4716 else
4717 key_type = NL80211_KEYTYPE_PAIRWISE;
4718
1993732e 4719 cfg80211_michael_mic_failure(ifp->ndev, (u8 *)&e->addr, key_type, -1,
5b435de0
AS
4720 NULL, GFP_KERNEL);
4721
4722 return 0;
4723}
4724
d3c0b633
AS
4725static s32 brcmf_notify_vif_event(struct brcmf_if *ifp,
4726 const struct brcmf_event_msg *e, void *data)
4727{
4728 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
4729 struct brcmf_if_event *ifevent = (struct brcmf_if_event *)data;
4730 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
4731 struct brcmf_cfg80211_vif *vif;
4732
4733 brcmf_dbg(TRACE, "Enter: action %u flags %u ifidx %u bsscfg %u\n",
4734 ifevent->action, ifevent->flags, ifevent->ifidx,
4735 ifevent->bssidx);
4736
d3c0b633
AS
4737 mutex_lock(&event->vif_event_lock);
4738 event->action = ifevent->action;
4739 vif = event->vif;
4740
4741 switch (ifevent->action) {
4742 case BRCMF_E_IF_ADD:
4743 /* waiting process may have timed out */
dc4a787c
WY
4744 if (!cfg->vif_event.vif) {
4745 mutex_unlock(&event->vif_event_lock);
d3c0b633 4746 return -EBADF;
dc4a787c 4747 }
d3c0b633
AS
4748
4749 ifp->vif = vif;
4750 vif->ifp = ifp;
01b8e7db
AS
4751 if (ifp->ndev) {
4752 vif->wdev.netdev = ifp->ndev;
4753 ifp->ndev->ieee80211_ptr = &vif->wdev;
4754 SET_NETDEV_DEV(ifp->ndev, wiphy_dev(cfg->wiphy));
4755 }
d3c0b633
AS
4756 mutex_unlock(&event->vif_event_lock);
4757 wake_up(&event->vif_wq);
4b3a89de 4758 return 0;
d3c0b633
AS
4759
4760 case BRCMF_E_IF_DEL:
d3c0b633
AS
4761 mutex_unlock(&event->vif_event_lock);
4762 /* event may not be upon user request */
4763 if (brcmf_cfg80211_vif_event_armed(cfg))
4764 wake_up(&event->vif_wq);
4765 return 0;
4766
7a5c1f64
HM
4767 case BRCMF_E_IF_CHANGE:
4768 mutex_unlock(&event->vif_event_lock);
4769 wake_up(&event->vif_wq);
4770 return 0;
4771
d3c0b633
AS
4772 default:
4773 mutex_unlock(&event->vif_event_lock);
4774 break;
4775 }
4776 return -EINVAL;
4777}
4778
5b435de0
AS
4779static void brcmf_init_conf(struct brcmf_cfg80211_conf *conf)
4780{
5b435de0
AS
4781 conf->frag_threshold = (u32)-1;
4782 conf->rts_threshold = (u32)-1;
4783 conf->retry_short = (u32)-1;
4784 conf->retry_long = (u32)-1;
4785 conf->tx_power = -1;
4786}
4787
5c36b99a 4788static void brcmf_register_event_handlers(struct brcmf_cfg80211_info *cfg)
5b435de0 4789{
5c36b99a
AS
4790 brcmf_fweh_register(cfg->pub, BRCMF_E_LINK,
4791 brcmf_notify_connect_status);
4792 brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH_IND,
4793 brcmf_notify_connect_status);
4794 brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH,
4795 brcmf_notify_connect_status);
4796 brcmf_fweh_register(cfg->pub, BRCMF_E_DISASSOC_IND,
4797 brcmf_notify_connect_status);
4798 brcmf_fweh_register(cfg->pub, BRCMF_E_ASSOC_IND,
4799 brcmf_notify_connect_status);
4800 brcmf_fweh_register(cfg->pub, BRCMF_E_REASSOC_IND,
4801 brcmf_notify_connect_status);
4802 brcmf_fweh_register(cfg->pub, BRCMF_E_ROAM,
4803 brcmf_notify_roaming_status);
4804 brcmf_fweh_register(cfg->pub, BRCMF_E_MIC_ERROR,
4805 brcmf_notify_mic_status);
4806 brcmf_fweh_register(cfg->pub, BRCMF_E_SET_SSID,
4807 brcmf_notify_connect_status);
4808 brcmf_fweh_register(cfg->pub, BRCMF_E_PFN_NET_FOUND,
4809 brcmf_notify_sched_scan_results);
d3c0b633
AS
4810 brcmf_fweh_register(cfg->pub, BRCMF_E_IF,
4811 brcmf_notify_vif_event);
0de8aace 4812 brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_PROBEREQ_MSG,
6eda4e2c 4813 brcmf_p2p_notify_rx_mgmt_p2p_probereq);
0de8aace
HM
4814 brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_DISC_LISTEN_COMPLETE,
4815 brcmf_p2p_notify_listen_complete);
e6da3400
HM
4816 brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_RX,
4817 brcmf_p2p_notify_action_frame_rx);
18e2f61d
HM
4818 brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_COMPLETE,
4819 brcmf_p2p_notify_action_tx_complete);
6eda4e2c
HM
4820 brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE,
4821 brcmf_p2p_notify_action_tx_complete);
5b435de0
AS
4822}
4823
27a68fe3
AS
4824static void brcmf_deinit_priv_mem(struct brcmf_cfg80211_info *cfg)
4825{
27a68fe3
AS
4826 kfree(cfg->conf);
4827 cfg->conf = NULL;
27a68fe3
AS
4828 kfree(cfg->escan_ioctl_buf);
4829 cfg->escan_ioctl_buf = NULL;
27a68fe3
AS
4830 kfree(cfg->extra_buf);
4831 cfg->extra_buf = NULL;
27a68fe3
AS
4832 kfree(cfg->pmk_list);
4833 cfg->pmk_list = NULL;
27a68fe3
AS
4834}
4835
4836static s32 brcmf_init_priv_mem(struct brcmf_cfg80211_info *cfg)
4837{
27a68fe3
AS
4838 cfg->conf = kzalloc(sizeof(*cfg->conf), GFP_KERNEL);
4839 if (!cfg->conf)
5b435de0 4840 goto init_priv_mem_out;
27a68fe3
AS
4841 cfg->escan_ioctl_buf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
4842 if (!cfg->escan_ioctl_buf)
e756af5b 4843 goto init_priv_mem_out;
27a68fe3
AS
4844 cfg->extra_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
4845 if (!cfg->extra_buf)
5b435de0 4846 goto init_priv_mem_out;
27a68fe3
AS
4847 cfg->pmk_list = kzalloc(sizeof(*cfg->pmk_list), GFP_KERNEL);
4848 if (!cfg->pmk_list)
5b435de0
AS
4849 goto init_priv_mem_out;
4850
4851 return 0;
4852
4853init_priv_mem_out:
27a68fe3 4854 brcmf_deinit_priv_mem(cfg);
5b435de0
AS
4855
4856 return -ENOMEM;
4857}
4858
27a68fe3 4859static s32 wl_init_priv(struct brcmf_cfg80211_info *cfg)
5b435de0
AS
4860{
4861 s32 err = 0;
4862
27a68fe3
AS
4863 cfg->scan_request = NULL;
4864 cfg->pwr_save = true;
27a68fe3 4865 cfg->roam_on = true; /* roam on & off switch.
5b435de0 4866 we enable roam per default */
27a68fe3 4867 cfg->active_scan = true; /* we do active scan for
5b435de0 4868 specific scan per default */
27a68fe3 4869 cfg->dongle_up = false; /* dongle is not up yet */
27a68fe3 4870 err = brcmf_init_priv_mem(cfg);
5b435de0
AS
4871 if (err)
4872 return err;
5c36b99a 4873 brcmf_register_event_handlers(cfg);
27a68fe3 4874 mutex_init(&cfg->usr_sync);
27a68fe3
AS
4875 brcmf_init_escan(cfg);
4876 brcmf_init_conf(cfg->conf);
5f4f9f11 4877 init_completion(&cfg->vif_disabled);
5b435de0
AS
4878 return err;
4879}
4880
27a68fe3 4881static void wl_deinit_priv(struct brcmf_cfg80211_info *cfg)
5b435de0 4882{
27a68fe3 4883 cfg->dongle_up = false; /* dongle down */
27a68fe3
AS
4884 brcmf_abort_scanning(cfg);
4885 brcmf_deinit_priv_mem(cfg);
5b435de0
AS
4886}
4887
d3c0b633
AS
4888static void init_vif_event(struct brcmf_cfg80211_vif_event *event)
4889{
4890 init_waitqueue_head(&event->vif_wq);
d3c0b633
AS
4891 mutex_init(&event->vif_event_lock);
4892}
4893
d9cb2596
AS
4894struct brcmf_cfg80211_info *brcmf_cfg80211_attach(struct brcmf_pub *drvr,
4895 struct device *busdev)
5b435de0 4896{
1ed9baf0 4897 struct net_device *ndev = drvr->iflist[0]->ndev;
27a68fe3 4898 struct brcmf_cfg80211_info *cfg;
3eacf866
AS
4899 struct wiphy *wiphy;
4900 struct brcmf_cfg80211_vif *vif;
4901 struct brcmf_if *ifp;
5b435de0 4902 s32 err = 0;
83cf17aa 4903 s32 io_type;
5b435de0
AS
4904
4905 if (!ndev) {
57d6e91a 4906 brcmf_err("ndev is invalid\n");
5b435de0
AS
4907 return NULL;
4908 }
5b435de0 4909
3eacf866
AS
4910 ifp = netdev_priv(ndev);
4911 wiphy = brcmf_setup_wiphy(busdev);
4912 if (IS_ERR(wiphy))
5b435de0 4913 return NULL;
5b435de0 4914
3eacf866
AS
4915 cfg = wiphy_priv(wiphy);
4916 cfg->wiphy = wiphy;
27a68fe3 4917 cfg->pub = drvr;
d3c0b633 4918 init_vif_event(&cfg->vif_event);
3eacf866
AS
4919 INIT_LIST_HEAD(&cfg->vif_list);
4920
d3c0b633 4921 vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_STATION, false);
3eacf866
AS
4922 if (IS_ERR(vif)) {
4923 wiphy_free(wiphy);
4924 return NULL;
4925 }
4926
d3c0b633
AS
4927 vif->ifp = ifp;
4928 vif->wdev.netdev = ndev;
4929 ndev->ieee80211_ptr = &vif->wdev;
4930 SET_NETDEV_DEV(ndev, wiphy_dev(cfg->wiphy));
4931
27a68fe3 4932 err = wl_init_priv(cfg);
5b435de0 4933 if (err) {
57d6e91a 4934 brcmf_err("Failed to init iwm_priv (%d)\n", err);
5b435de0
AS
4935 goto cfg80211_attach_out;
4936 }
3eacf866 4937 ifp->vif = vif;
2fde59d9
HM
4938
4939 err = brcmf_p2p_attach(cfg);
4940 if (err) {
4941 brcmf_err("P2P initilisation failed (%d)\n", err);
4942 goto cfg80211_p2p_attach_out;
4943 }
61730d4d
PH
4944 err = brcmf_btcoex_attach(cfg);
4945 if (err) {
4946 brcmf_err("BT-coex initialisation failed (%d)\n", err);
4947 brcmf_p2p_detach(&cfg->p2p);
4948 goto cfg80211_p2p_attach_out;
4949 }
2fde59d9 4950
89c2f382
AS
4951 err = brcmf_fil_iovar_int_set(ifp, "tdls_enable", 1);
4952 if (err) {
4953 brcmf_dbg(INFO, "TDLS not enabled (%d)\n", err);
4954 wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_TDLS;
4955 }
4956
83cf17aa
FL
4957 err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_VERSION,
4958 &io_type);
4959 if (err) {
4960 brcmf_err("Failed to get D11 version (%d)\n", err);
4961 goto cfg80211_p2p_attach_out;
4962 }
4963 cfg->d11inf.io_type = (u8)io_type;
4964 brcmu_d11_attach(&cfg->d11inf);
4965
27a68fe3 4966 return cfg;
5b435de0 4967
2fde59d9
HM
4968cfg80211_p2p_attach_out:
4969 wl_deinit_priv(cfg);
4970
5b435de0 4971cfg80211_attach_out:
427dec5f 4972 brcmf_free_vif(vif);
5b435de0
AS
4973 return NULL;
4974}
4975
27a68fe3 4976void brcmf_cfg80211_detach(struct brcmf_cfg80211_info *cfg)
5b435de0 4977{
e1479951
AS
4978 if (!cfg)
4979 return;
4980
427dec5f
AS
4981 WARN_ON(!list_empty(&cfg->vif_list));
4982 wiphy_unregister(cfg->wiphy);
61730d4d 4983 brcmf_btcoex_detach(cfg);
427dec5f
AS
4984 wl_deinit_priv(cfg);
4985 wiphy_free(cfg->wiphy);
5b435de0
AS
4986}
4987
5b435de0 4988static s32
40a23296 4989brcmf_dongle_roam(struct brcmf_if *ifp, u32 roamvar, u32 bcn_timeout)
5b435de0 4990{
5b435de0 4991 s32 err = 0;
f588bc0c
AS
4992 __le32 roamtrigger[2];
4993 __le32 roam_delta[2];
5b435de0
AS
4994
4995 /*
4996 * Setup timeout if Beacons are lost and roam is
4997 * off to report link down
4998 */
4999 if (roamvar) {
ac24be6f 5000 err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout", bcn_timeout);
5b435de0 5001 if (err) {
57d6e91a 5002 brcmf_err("bcn_timeout error (%d)\n", err);
5b435de0
AS
5003 goto dongle_rom_out;
5004 }
5005 }
5006
5007 /*
5008 * Enable/Disable built-in roaming to allow supplicant
5009 * to take care of roaming
5010 */
647c9ae0 5011 brcmf_dbg(INFO, "Internal Roaming = %s\n", roamvar ? "Off" : "On");
ac24be6f 5012 err = brcmf_fil_iovar_int_set(ifp, "roam_off", roamvar);
5b435de0 5013 if (err) {
57d6e91a 5014 brcmf_err("roam_off error (%d)\n", err);
5b435de0
AS
5015 goto dongle_rom_out;
5016 }
5017
f588bc0c
AS
5018 roamtrigger[0] = cpu_to_le32(WL_ROAM_TRIGGER_LEVEL);
5019 roamtrigger[1] = cpu_to_le32(BRCM_BAND_ALL);
ac24be6f 5020 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_TRIGGER,
81f5dcb8 5021 (void *)roamtrigger, sizeof(roamtrigger));
5b435de0 5022 if (err) {
57d6e91a 5023 brcmf_err("WLC_SET_ROAM_TRIGGER error (%d)\n", err);
5b435de0
AS
5024 goto dongle_rom_out;
5025 }
5026
f588bc0c
AS
5027 roam_delta[0] = cpu_to_le32(WL_ROAM_DELTA);
5028 roam_delta[1] = cpu_to_le32(BRCM_BAND_ALL);
ac24be6f 5029 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_DELTA,
81f5dcb8 5030 (void *)roam_delta, sizeof(roam_delta));
5b435de0 5031 if (err) {
57d6e91a 5032 brcmf_err("WLC_SET_ROAM_DELTA error (%d)\n", err);
5b435de0
AS
5033 goto dongle_rom_out;
5034 }
5035
5036dongle_rom_out:
5037 return err;
5038}
5039
5040static s32
40a23296 5041brcmf_dongle_scantime(struct brcmf_if *ifp, s32 scan_assoc_time,
c68cdc0f 5042 s32 scan_unassoc_time, s32 scan_passive_time)
5b435de0
AS
5043{
5044 s32 err = 0;
5045
ac24be6f 5046 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
81f5dcb8 5047 scan_assoc_time);
5b435de0
AS
5048 if (err) {
5049 if (err == -EOPNOTSUPP)
647c9ae0 5050 brcmf_dbg(INFO, "Scan assoc time is not supported\n");
5b435de0 5051 else
57d6e91a 5052 brcmf_err("Scan assoc time error (%d)\n", err);
5b435de0
AS
5053 goto dongle_scantime_out;
5054 }
ac24be6f 5055 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
81f5dcb8 5056 scan_unassoc_time);
5b435de0
AS
5057 if (err) {
5058 if (err == -EOPNOTSUPP)
647c9ae0 5059 brcmf_dbg(INFO, "Scan unassoc time is not supported\n");
5b435de0 5060 else
57d6e91a 5061 brcmf_err("Scan unassoc time error (%d)\n", err);
5b435de0
AS
5062 goto dongle_scantime_out;
5063 }
5064
ac24be6f 5065 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_PASSIVE_TIME,
81f5dcb8 5066 scan_passive_time);
5b435de0
AS
5067 if (err) {
5068 if (err == -EOPNOTSUPP)
647c9ae0 5069 brcmf_dbg(INFO, "Scan passive time is not supported\n");
5b435de0 5070 else
57d6e91a 5071 brcmf_err("Scan passive time error (%d)\n", err);
5b435de0
AS
5072 goto dongle_scantime_out;
5073 }
5074
5075dongle_scantime_out:
5076 return err;
5077}
5078
d48200ba 5079
2375d970
AS
5080static s32 brcmf_construct_reginfo(struct brcmf_cfg80211_info *cfg,
5081 u32 bw_cap[])
d48200ba
HM
5082{
5083 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5084 struct ieee80211_channel *band_chan_arr;
5085 struct brcmf_chanspec_list *list;
83cf17aa 5086 struct brcmu_chan ch;
d48200ba
HM
5087 s32 err;
5088 u8 *pbuf;
5089 u32 i, j;
5090 u32 total;
d48200ba
HM
5091 enum ieee80211_band band;
5092 u32 channel;
5093 u32 *n_cnt;
d48200ba
HM
5094 u32 index;
5095 u32 ht40_flag;
5096 bool update;
5097 u32 array_size;
5098
5099 pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
5100
5101 if (pbuf == NULL)
5102 return -ENOMEM;
5103
5104 list = (struct brcmf_chanspec_list *)pbuf;
5105
5106 err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
5107 BRCMF_DCMD_MEDLEN);
5108 if (err) {
5109 brcmf_err("get chanspecs error (%d)\n", err);
5110 goto exit;
5111 }
5112
5113 __wl_band_2ghz.n_channels = 0;
5114 __wl_band_5ghz_a.n_channels = 0;
5115
5116 total = le32_to_cpu(list->count);
5117 for (i = 0; i < total; i++) {
83cf17aa
FL
5118 ch.chspec = (u16)le32_to_cpu(list->element[i]);
5119 cfg->d11inf.decchspec(&ch);
d48200ba 5120
83cf17aa 5121 if (ch.band == BRCMU_CHAN_BAND_2G) {
d48200ba
HM
5122 band_chan_arr = __wl_2ghz_channels;
5123 array_size = ARRAY_SIZE(__wl_2ghz_channels);
5124 n_cnt = &__wl_band_2ghz.n_channels;
5125 band = IEEE80211_BAND_2GHZ;
83cf17aa 5126 } else if (ch.band == BRCMU_CHAN_BAND_5G) {
d48200ba
HM
5127 band_chan_arr = __wl_5ghz_a_channels;
5128 array_size = ARRAY_SIZE(__wl_5ghz_a_channels);
5129 n_cnt = &__wl_band_5ghz_a.n_channels;
5130 band = IEEE80211_BAND_5GHZ;
d48200ba 5131 } else {
2375d970 5132 brcmf_err("Invalid channel Spec. 0x%x.\n", ch.chspec);
d48200ba
HM
5133 continue;
5134 }
2375d970
AS
5135 if (!(bw_cap[band] & WLC_BW_40MHZ_BIT) &&
5136 ch.bw == BRCMU_CHAN_BW_40)
d48200ba
HM
5137 continue;
5138 update = false;
5139 for (j = 0; (j < *n_cnt && (*n_cnt < array_size)); j++) {
83cf17aa 5140 if (band_chan_arr[j].hw_value == ch.chnum) {
d48200ba
HM
5141 update = true;
5142 break;
5143 }
5144 }
5145 if (update)
5146 index = j;
5147 else
5148 index = *n_cnt;
5149 if (index < array_size) {
5150 band_chan_arr[index].center_freq =
83cf17aa
FL
5151 ieee80211_channel_to_frequency(ch.chnum, band);
5152 band_chan_arr[index].hw_value = ch.chnum;
d48200ba 5153
2375d970
AS
5154 brcmf_err("channel %d: f=%d bw=%d sb=%d\n",
5155 ch.chnum, band_chan_arr[index].center_freq,
5156 ch.bw, ch.sb);
5157 if (ch.bw == BRCMU_CHAN_BW_40) {
d48200ba
HM
5158 /* assuming the order is HT20, HT40 Upper,
5159 * HT40 lower from chanspecs
5160 */
5161 ht40_flag = band_chan_arr[index].flags &
5162 IEEE80211_CHAN_NO_HT40;
83cf17aa 5163 if (ch.sb == BRCMU_CHAN_SB_U) {
d48200ba
HM
5164 if (ht40_flag == IEEE80211_CHAN_NO_HT40)
5165 band_chan_arr[index].flags &=
5166 ~IEEE80211_CHAN_NO_HT40;
5167 band_chan_arr[index].flags |=
5168 IEEE80211_CHAN_NO_HT40PLUS;
5169 } else {
5170 /* It should be one of
5171 * IEEE80211_CHAN_NO_HT40 or
5172 * IEEE80211_CHAN_NO_HT40PLUS
5173 */
5174 band_chan_arr[index].flags &=
5175 ~IEEE80211_CHAN_NO_HT40;
5176 if (ht40_flag == IEEE80211_CHAN_NO_HT40)
5177 band_chan_arr[index].flags |=
5178 IEEE80211_CHAN_NO_HT40MINUS;
5179 }
5180 } else {
5181 band_chan_arr[index].flags =
5182 IEEE80211_CHAN_NO_HT40;
83cf17aa
FL
5183 ch.bw = BRCMU_CHAN_BW_20;
5184 cfg->d11inf.encchspec(&ch);
5185 channel = ch.chspec;
d48200ba
HM
5186 err = brcmf_fil_bsscfg_int_get(ifp,
5187 "per_chan_info",
5188 &channel);
5189 if (!err) {
5190 if (channel & WL_CHAN_RADAR)
5191 band_chan_arr[index].flags |=
5192 (IEEE80211_CHAN_RADAR |
8fe02e16 5193 IEEE80211_CHAN_NO_IR);
d48200ba
HM
5194 if (channel & WL_CHAN_PASSIVE)
5195 band_chan_arr[index].flags |=
8fe02e16 5196 IEEE80211_CHAN_NO_IR;
d48200ba
HM
5197 }
5198 }
5199 if (!update)
5200 (*n_cnt)++;
5201 }
5202 }
5203exit:
5204 kfree(pbuf);
5205 return err;
5206}
5207
2375d970
AS
5208static void brcmf_get_bwcap(struct brcmf_if *ifp, u32 bw_cap[])
5209{
5210 u32 band, mimo_bwcap;
5211 int err;
5212
5213 band = WLC_BAND_2G;
5214 err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
5215 if (!err) {
5216 bw_cap[IEEE80211_BAND_2GHZ] = band;
5217 band = WLC_BAND_5G;
5218 err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
5219 if (!err) {
5220 bw_cap[IEEE80211_BAND_5GHZ] = band;
5221 return;
5222 }
5223 WARN_ON(1);
5224 return;
5225 }
5226 brcmf_dbg(INFO, "fallback to mimo_bw_cap info\n");
5227 mimo_bwcap = 0;
5228 err = brcmf_fil_iovar_int_get(ifp, "mimo_bw_cap", &mimo_bwcap);
5229 if (err)
5230 /* assume 20MHz if firmware does not give a clue */
5231 mimo_bwcap = WLC_N_BW_20ALL;
5232
5233 switch (mimo_bwcap) {
5234 case WLC_N_BW_40ALL:
5235 bw_cap[IEEE80211_BAND_2GHZ] |= WLC_BW_40MHZ_BIT;
5236 /* fall-thru */
5237 case WLC_N_BW_20IN2G_40IN5G:
5238 bw_cap[IEEE80211_BAND_5GHZ] |= WLC_BW_40MHZ_BIT;
5239 /* fall-thru */
5240 case WLC_N_BW_20ALL:
5241 bw_cap[IEEE80211_BAND_2GHZ] |= WLC_BW_20MHZ_BIT;
5242 bw_cap[IEEE80211_BAND_5GHZ] |= WLC_BW_20MHZ_BIT;
5243 break;
5244 default:
5245 brcmf_err("invalid mimo_bw_cap value\n");
5246 }
5247}
d48200ba
HM
5248
5249static s32 brcmf_update_wiphybands(struct brcmf_cfg80211_info *cfg)
5b435de0 5250{
ac24be6f 5251 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5b435de0
AS
5252 struct wiphy *wiphy;
5253 s32 phy_list;
d48200ba
HM
5254 u32 band_list[3];
5255 u32 nmode;
2375d970 5256 u32 bw_cap[2] = { 0, 0 };
5b435de0 5257 s8 phy;
d48200ba
HM
5258 s32 err;
5259 u32 nband;
5260 s32 i;
2375d970
AS
5261 struct ieee80211_supported_band *bands[2] = { NULL, NULL };
5262 struct ieee80211_supported_band *band;
5b435de0 5263
b87e2c48 5264 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_PHYLIST,
81f5dcb8 5265 &phy_list, sizeof(phy_list));
5b435de0 5266 if (err) {
d48200ba 5267 brcmf_err("BRCMF_C_GET_PHYLIST error (%d)\n", err);
5b435de0
AS
5268 return err;
5269 }
5270
3ba81376 5271 phy = ((char *)&phy_list)[0];
d48200ba
HM
5272 brcmf_dbg(INFO, "BRCMF_C_GET_PHYLIST reported: %c phy\n", phy);
5273
5274
5275 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BANDLIST,
5276 &band_list, sizeof(band_list));
5277 if (err) {
5278 brcmf_err("BRCMF_C_GET_BANDLIST error (%d)\n", err);
5279 return err;
5b435de0 5280 }
d48200ba
HM
5281 brcmf_dbg(INFO, "BRCMF_C_GET_BANDLIST reported: 0x%08x 0x%08x 0x%08x phy\n",
5282 band_list[0], band_list[1], band_list[2]);
5283
5284 err = brcmf_fil_iovar_int_get(ifp, "nmode", &nmode);
5285 if (err) {
5286 brcmf_err("nmode error (%d)\n", err);
5287 } else {
2375d970 5288 brcmf_get_bwcap(ifp, bw_cap);
d48200ba 5289 }
2375d970
AS
5290 brcmf_dbg(INFO, "nmode=%d, bw_cap=(%d, %d)\n", nmode,
5291 bw_cap[IEEE80211_BAND_2GHZ], bw_cap[IEEE80211_BAND_5GHZ]);
d48200ba
HM
5292
5293 err = brcmf_construct_reginfo(cfg, bw_cap);
5294 if (err) {
5295 brcmf_err("brcmf_construct_reginfo failed (%d)\n", err);
5296 return err;
5297 }
5298
5299 nband = band_list[0];
d48200ba
HM
5300
5301 for (i = 1; i <= nband && i < ARRAY_SIZE(band_list); i++) {
2375d970 5302 band = NULL;
d48200ba 5303 if ((band_list[i] == WLC_BAND_5G) &&
2375d970
AS
5304 (__wl_band_5ghz_a.n_channels > 0))
5305 band = &__wl_band_5ghz_a;
5306 else if ((band_list[i] == WLC_BAND_2G) &&
5307 (__wl_band_2ghz.n_channels > 0))
5308 band = &__wl_band_2ghz;
5309 else
5310 continue;
d48200ba 5311
2375d970
AS
5312 if (bw_cap[band->band] & WLC_BW_40MHZ_BIT) {
5313 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5314 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
d48200ba 5315 }
2375d970
AS
5316 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5317 band->ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5318 band->ht_cap.ht_supported = true;
5319 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5320 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
5321 /* An HT shall support all EQM rates for one spatial
5322 * stream
5323 */
5324 band->ht_cap.mcs.rx_mask[0] = 0xff;
5325 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
5326 bands[band->band] = band;
d48200ba
HM
5327 }
5328
5329 wiphy = cfg_to_wiphy(cfg);
5330 wiphy->bands[IEEE80211_BAND_2GHZ] = bands[IEEE80211_BAND_2GHZ];
5331 wiphy->bands[IEEE80211_BAND_5GHZ] = bands[IEEE80211_BAND_5GHZ];
5332 wiphy_apply_custom_regulatory(wiphy, &brcmf_regdom);
5b435de0
AS
5333
5334 return err;
5335}
5336
d48200ba 5337
27a68fe3 5338static s32 brcmf_dongle_probecap(struct brcmf_cfg80211_info *cfg)
5b435de0 5339{
d48200ba 5340 return brcmf_update_wiphybands(cfg);
5b435de0
AS
5341}
5342
27a68fe3 5343static s32 brcmf_config_dongle(struct brcmf_cfg80211_info *cfg)
5b435de0
AS
5344{
5345 struct net_device *ndev;
5346 struct wireless_dev *wdev;
40a23296 5347 struct brcmf_if *ifp;
5b435de0
AS
5348 s32 power_mode;
5349 s32 err = 0;
5350
27a68fe3 5351 if (cfg->dongle_up)
5b435de0
AS
5352 return err;
5353
27a68fe3 5354 ndev = cfg_to_ndev(cfg);
5b435de0 5355 wdev = ndev->ieee80211_ptr;
40a23296
HM
5356 ifp = netdev_priv(ndev);
5357
5358 /* make sure RF is ready for work */
5359 brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
5b435de0 5360
40a23296
HM
5361 brcmf_dongle_scantime(ifp, WL_SCAN_CHANNEL_TIME,
5362 WL_SCAN_UNASSOC_TIME, WL_SCAN_PASSIVE_TIME);
5b435de0 5363
27a68fe3 5364 power_mode = cfg->pwr_save ? PM_FAST : PM_OFF;
40a23296 5365 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, power_mode);
5b435de0
AS
5366 if (err)
5367 goto default_conf_out;
647c9ae0
AS
5368 brcmf_dbg(INFO, "power save set to %s\n",
5369 (power_mode ? "enabled" : "disabled"));
5b435de0 5370
40a23296 5371 err = brcmf_dongle_roam(ifp, (cfg->roam_on ? 0 : 1), WL_BEACON_TIMEOUT);
5b435de0
AS
5372 if (err)
5373 goto default_conf_out;
5dd161ff
FL
5374 err = brcmf_cfg80211_change_iface(wdev->wiphy, ndev, wdev->iftype,
5375 NULL, NULL);
40a23296 5376 if (err)
5b435de0 5377 goto default_conf_out;
27a68fe3 5378 err = brcmf_dongle_probecap(cfg);
5b435de0
AS
5379 if (err)
5380 goto default_conf_out;
5381
b3657453
HM
5382 brcmf_configure_arp_offload(ifp, true);
5383
27a68fe3 5384 cfg->dongle_up = true;
40a23296 5385default_conf_out:
5b435de0
AS
5386
5387 return err;
5388
5389}
5390
bdf5ff51 5391static s32 __brcmf_cfg80211_up(struct brcmf_if *ifp)
5b435de0 5392{
c1179033 5393 set_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
5b435de0 5394
bdf5ff51 5395 return brcmf_config_dongle(ifp->drvr->config);
5b435de0
AS
5396}
5397
bdf5ff51 5398static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp)
5b435de0 5399{
bdf5ff51 5400 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
c1179033 5401
5b435de0
AS
5402 /*
5403 * While going down, if associated with AP disassociate
5404 * from AP to save power
5405 */
903e0eee
AS
5406 if (check_vif_up(ifp->vif)) {
5407 brcmf_link_down(ifp->vif);
5b435de0
AS
5408
5409 /* Make sure WPA_Supplicant receives all the event
5410 generated due to DISASSOC call to the fw to keep
5411 the state fw and WPA_Supplicant state consistent
5412 */
5413 brcmf_delay(500);
5414 }
5415
27a68fe3 5416 brcmf_abort_scanning(cfg);
c1179033 5417 clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
5b435de0 5418
5b435de0
AS
5419 return 0;
5420}
5421
bdf5ff51 5422s32 brcmf_cfg80211_up(struct net_device *ndev)
5b435de0 5423{
bdf5ff51
AS
5424 struct brcmf_if *ifp = netdev_priv(ndev);
5425 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5b435de0
AS
5426 s32 err = 0;
5427
27a68fe3 5428 mutex_lock(&cfg->usr_sync);
bdf5ff51 5429 err = __brcmf_cfg80211_up(ifp);
27a68fe3 5430 mutex_unlock(&cfg->usr_sync);
5b435de0
AS
5431
5432 return err;
5433}
5434
bdf5ff51 5435s32 brcmf_cfg80211_down(struct net_device *ndev)
5b435de0 5436{
bdf5ff51
AS
5437 struct brcmf_if *ifp = netdev_priv(ndev);
5438 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5b435de0
AS
5439 s32 err = 0;
5440
27a68fe3 5441 mutex_lock(&cfg->usr_sync);
bdf5ff51 5442 err = __brcmf_cfg80211_down(ifp);
27a68fe3 5443 mutex_unlock(&cfg->usr_sync);
5b435de0
AS
5444
5445 return err;
5446}
5447
a7965fbb
AS
5448enum nl80211_iftype brcmf_cfg80211_get_iftype(struct brcmf_if *ifp)
5449{
5450 struct wireless_dev *wdev = &ifp->vif->wdev;
5451
5452 return wdev->iftype;
5453}
5454
9f440b7b
AS
5455u32 wl_get_vif_state_all(struct brcmf_cfg80211_info *cfg, unsigned long state)
5456{
5457 struct brcmf_cfg80211_vif *vif;
5458 bool result = 0;
5459
5460 list_for_each_entry(vif, &cfg->vif_list, list) {
5461 if (test_bit(state, &vif->sme_state))
5462 result++;
5463 }
5464 return result;
5465}
d3c0b633
AS
5466
5467static inline bool vif_event_equals(struct brcmf_cfg80211_vif_event *event,
5468 u8 action)
5469{
5470 u8 evt_action;
5471
5472 mutex_lock(&event->vif_event_lock);
5473 evt_action = event->action;
5474 mutex_unlock(&event->vif_event_lock);
5475 return evt_action == action;
5476}
5477
5478void brcmf_cfg80211_arm_vif_event(struct brcmf_cfg80211_info *cfg,
5479 struct brcmf_cfg80211_vif *vif)
5480{
5481 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5482
5483 mutex_lock(&event->vif_event_lock);
5484 event->vif = vif;
5485 event->action = 0;
5486 mutex_unlock(&event->vif_event_lock);
5487}
5488
5489bool brcmf_cfg80211_vif_event_armed(struct brcmf_cfg80211_info *cfg)
5490{
5491 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5492 bool armed;
5493
5494 mutex_lock(&event->vif_event_lock);
5495 armed = event->vif != NULL;
5496 mutex_unlock(&event->vif_event_lock);
5497
5498 return armed;
5499}
5500int brcmf_cfg80211_wait_vif_event_timeout(struct brcmf_cfg80211_info *cfg,
5501 u8 action, ulong timeout)
5502{
5503 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5504
5505 return wait_event_timeout(event->vif_wq,
5506 vif_event_equals(event, action), timeout);
5507}
5508