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