brcmfmac: fix conversion of channel width 20MHZ_NOHT
[deliverable/linux.git] / drivers / net / wireless / brcm80211 / brcmfmac / wl_cfg80211.c
CommitLineData
5b435de0
AS
1/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */
18
19#include <linux/kernel.h>
5b435de0 20#include <linux/etherdevice.h>
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>
29#include "dhd.h"
16886735 30#include "dhd_dbg.h"
40c1c249 31#include "tracepoint.h"
7a5c1f64 32#include "fwil_types.h"
9f440b7b 33#include "p2p.h"
61730d4d 34#include "btcoex.h"
5b435de0 35#include "wl_cfg80211.h"
c08437b4 36#include "feature.h"
81f5dcb8 37#include "fwil.h"
8851cce0 38#include "proto.h"
1bacb048 39#include "vendor.h"
4eb3af7c 40#include "dhd_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
2788static s32 brcmf_cfg80211_resume(struct wiphy *wiphy)
2789{
4eb3af7c
HM
2790 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2791 struct net_device *ndev = cfg_to_ndev(cfg);
2792 struct brcmf_if *ifp = netdev_priv(ndev);
2793
d96b801f 2794 brcmf_dbg(TRACE, "Enter\n");
5b435de0 2795
4eb3af7c
HM
2796 if (cfg->wowl_enabled) {
2797 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM,
2798 cfg->pre_wowl_pmmode);
2799 brcmf_fil_iovar_data_set(ifp, "wowl_pattern", "clr", 4);
2800 brcmf_fil_iovar_int_set(ifp, "wowl_clear", 0);
2801 cfg->wowl_enabled = false;
2802 }
5b435de0
AS
2803 return 0;
2804}
2805
4eb3af7c
HM
2806static void brcmf_configure_wowl(struct brcmf_cfg80211_info *cfg,
2807 struct brcmf_if *ifp,
2808 struct cfg80211_wowlan *wowl)
2809{
2810 u32 wowl_config;
2811
2812 brcmf_dbg(TRACE, "Suspend, wowl config.\n");
2813
2814 brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_PM, &cfg->pre_wowl_pmmode);
2815 brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, PM_MAX);
2816
2817 wowl_config = 0;
2818 if (wowl->disconnect)
2819 wowl_config |= WL_WOWL_DIS | WL_WOWL_BCN | WL_WOWL_RETR;
2820 /* Note: if "wowl" target and not "wowlpf" then wowl_bcn_loss
2821 * should be configured. This paramater is not supported by
2822 * wowlpf.
2823 */
2824 if (wowl->magic_pkt)
2825 wowl_config |= WL_WOWL_MAGIC;
2826 brcmf_fil_iovar_int_set(ifp, "wowl", wowl_config);
2827 brcmf_fil_iovar_int_set(ifp, "wowl_activate", 1);
2828 brcmf_bus_wowl_config(cfg->pub->bus_if, true);
2829 cfg->wowl_enabled = true;
2830}
2831
5b435de0 2832static s32 brcmf_cfg80211_suspend(struct wiphy *wiphy,
4eb3af7c 2833 struct cfg80211_wowlan *wowl)
5b435de0 2834{
27a68fe3
AS
2835 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2836 struct net_device *ndev = cfg_to_ndev(cfg);
4eb3af7c 2837 struct brcmf_if *ifp = netdev_priv(ndev);
7d641072 2838 struct brcmf_cfg80211_vif *vif;
5b435de0 2839
d96b801f 2840 brcmf_dbg(TRACE, "Enter\n");
5b435de0 2841
4eb3af7c 2842 /* if the primary net_device is not READY there is nothing
7d641072 2843 * we can do but pray resume goes smoothly.
5b435de0 2844 */
4eb3af7c 2845 if (!check_vif_up(ifp->vif))
7d641072 2846 goto exit;
5b435de0 2847
7d641072
AS
2848 /* end any scanning */
2849 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
27a68fe3 2850 brcmf_abort_scanning(cfg);
5b435de0 2851
4eb3af7c
HM
2852 if (wowl == NULL) {
2853 brcmf_bus_wowl_config(cfg->pub->bus_if, false);
2854 list_for_each_entry(vif, &cfg->vif_list, list) {
2855 if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state))
2856 continue;
2857 /* While going to suspend if associated with AP
2858 * disassociate from AP to save power while system is
2859 * in suspended state
2860 */
2861 brcmf_link_down(vif);
2862 /* Make sure WPA_Supplicant receives all the event
2863 * generated due to DISASSOC call to the fw to keep
2864 * the state fw and WPA_Supplicant state consistent
2865 */
2866 brcmf_delay(500);
2867 }
2868 /* Configure MPC */
2869 brcmf_set_mpc(ifp, 1);
2870
2871 } else {
2872 /* Configure WOWL paramaters */
2873 brcmf_configure_wowl(cfg, ifp, wowl);
2874 }
5b435de0 2875
7d641072 2876exit:
d96b801f 2877 brcmf_dbg(TRACE, "Exit\n");
7d641072
AS
2878 /* clear any scanning activity */
2879 cfg->scan_status = 0;
5b435de0
AS
2880 return 0;
2881}
2882
5b435de0
AS
2883static __used s32
2884brcmf_update_pmklist(struct net_device *ndev,
2885 struct brcmf_cfg80211_pmk_list *pmk_list, s32 err)
2886{
2887 int i, j;
40c8e95a 2888 int pmkid_len;
5b435de0 2889
40c8e95a
AS
2890 pmkid_len = le32_to_cpu(pmk_list->pmkids.npmkid);
2891
16886735 2892 brcmf_dbg(CONN, "No of elements %d\n", pmkid_len);
40c8e95a 2893 for (i = 0; i < pmkid_len; i++) {
16886735
AS
2894 brcmf_dbg(CONN, "PMKID[%d]: %pM =\n", i,
2895 &pmk_list->pmkids.pmkid[i].BSSID);
5b435de0 2896 for (j = 0; j < WLAN_PMKID_LEN; j++)
16886735
AS
2897 brcmf_dbg(CONN, "%02x\n",
2898 pmk_list->pmkids.pmkid[i].PMKID[j]);
5b435de0
AS
2899 }
2900
2901 if (!err)
ac24be6f
AS
2902 brcmf_fil_iovar_data_set(netdev_priv(ndev), "pmkid_info",
2903 (char *)pmk_list, sizeof(*pmk_list));
5b435de0
AS
2904
2905 return err;
2906}
2907
2908static s32
2909brcmf_cfg80211_set_pmksa(struct wiphy *wiphy, struct net_device *ndev,
2910 struct cfg80211_pmksa *pmksa)
2911{
27a68fe3 2912 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 2913 struct brcmf_if *ifp = netdev_priv(ndev);
27a68fe3 2914 struct pmkid_list *pmkids = &cfg->pmk_list->pmkids;
5b435de0
AS
2915 s32 err = 0;
2916 int i;
40c8e95a 2917 int pmkid_len;
5b435de0 2918
d96b801f 2919 brcmf_dbg(TRACE, "Enter\n");
ce81e317 2920 if (!check_vif_up(ifp->vif))
5b435de0
AS
2921 return -EIO;
2922
40c8e95a
AS
2923 pmkid_len = le32_to_cpu(pmkids->npmkid);
2924 for (i = 0; i < pmkid_len; i++)
5b435de0
AS
2925 if (!memcmp(pmksa->bssid, pmkids->pmkid[i].BSSID, ETH_ALEN))
2926 break;
2927 if (i < WL_NUM_PMKIDS_MAX) {
2928 memcpy(pmkids->pmkid[i].BSSID, pmksa->bssid, ETH_ALEN);
2929 memcpy(pmkids->pmkid[i].PMKID, pmksa->pmkid, WLAN_PMKID_LEN);
40c8e95a
AS
2930 if (i == pmkid_len) {
2931 pmkid_len++;
2932 pmkids->npmkid = cpu_to_le32(pmkid_len);
2933 }
5b435de0
AS
2934 } else
2935 err = -EINVAL;
2936
16886735
AS
2937 brcmf_dbg(CONN, "set_pmksa,IW_PMKSA_ADD - PMKID: %pM =\n",
2938 pmkids->pmkid[pmkid_len].BSSID);
5b435de0 2939 for (i = 0; i < WLAN_PMKID_LEN; i++)
16886735 2940 brcmf_dbg(CONN, "%02x\n", pmkids->pmkid[pmkid_len].PMKID[i]);
5b435de0 2941
27a68fe3 2942 err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
5b435de0 2943
d96b801f 2944 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2945 return err;
2946}
2947
2948static s32
2949brcmf_cfg80211_del_pmksa(struct wiphy *wiphy, struct net_device *ndev,
2950 struct cfg80211_pmksa *pmksa)
2951{
27a68fe3 2952 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 2953 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
2954 struct pmkid_list pmkid;
2955 s32 err = 0;
40c8e95a 2956 int i, pmkid_len;
5b435de0 2957
d96b801f 2958 brcmf_dbg(TRACE, "Enter\n");
ce81e317 2959 if (!check_vif_up(ifp->vif))
5b435de0
AS
2960 return -EIO;
2961
2962 memcpy(&pmkid.pmkid[0].BSSID, pmksa->bssid, ETH_ALEN);
2963 memcpy(&pmkid.pmkid[0].PMKID, pmksa->pmkid, WLAN_PMKID_LEN);
2964
16886735
AS
2965 brcmf_dbg(CONN, "del_pmksa,IW_PMKSA_REMOVE - PMKID: %pM =\n",
2966 &pmkid.pmkid[0].BSSID);
5b435de0 2967 for (i = 0; i < WLAN_PMKID_LEN; i++)
16886735 2968 brcmf_dbg(CONN, "%02x\n", pmkid.pmkid[0].PMKID[i]);
5b435de0 2969
27a68fe3 2970 pmkid_len = le32_to_cpu(cfg->pmk_list->pmkids.npmkid);
40c8e95a 2971 for (i = 0; i < pmkid_len; i++)
5b435de0 2972 if (!memcmp
27a68fe3 2973 (pmksa->bssid, &cfg->pmk_list->pmkids.pmkid[i].BSSID,
5b435de0
AS
2974 ETH_ALEN))
2975 break;
2976
40c8e95a
AS
2977 if ((pmkid_len > 0)
2978 && (i < pmkid_len)) {
27a68fe3 2979 memset(&cfg->pmk_list->pmkids.pmkid[i], 0,
5b435de0 2980 sizeof(struct pmkid));
40c8e95a 2981 for (; i < (pmkid_len - 1); i++) {
27a68fe3
AS
2982 memcpy(&cfg->pmk_list->pmkids.pmkid[i].BSSID,
2983 &cfg->pmk_list->pmkids.pmkid[i + 1].BSSID,
5b435de0 2984 ETH_ALEN);
27a68fe3
AS
2985 memcpy(&cfg->pmk_list->pmkids.pmkid[i].PMKID,
2986 &cfg->pmk_list->pmkids.pmkid[i + 1].PMKID,
5b435de0
AS
2987 WLAN_PMKID_LEN);
2988 }
27a68fe3 2989 cfg->pmk_list->pmkids.npmkid = cpu_to_le32(pmkid_len - 1);
5b435de0
AS
2990 } else
2991 err = -EINVAL;
2992
27a68fe3 2993 err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
5b435de0 2994
d96b801f 2995 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
2996 return err;
2997
2998}
2999
3000static s32
3001brcmf_cfg80211_flush_pmksa(struct wiphy *wiphy, struct net_device *ndev)
3002{
27a68fe3 3003 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
0abb5f21 3004 struct brcmf_if *ifp = netdev_priv(ndev);
5b435de0
AS
3005 s32 err = 0;
3006
d96b801f 3007 brcmf_dbg(TRACE, "Enter\n");
ce81e317 3008 if (!check_vif_up(ifp->vif))
5b435de0
AS
3009 return -EIO;
3010
27a68fe3
AS
3011 memset(cfg->pmk_list, 0, sizeof(*cfg->pmk_list));
3012 err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
5b435de0 3013
d96b801f 3014 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
3015 return err;
3016
3017}
3018
e5806072
AS
3019/*
3020 * PFN result doesn't have all the info which are
3021 * required by the supplicant
3022 * (For e.g IEs) Do a target Escan so that sched scan results are reported
3023 * via wl_inform_single_bss in the required format. Escan does require the
3024 * scan request in the form of cfg80211_scan_request. For timebeing, create
3025 * cfg80211_scan_request one out of the received PNO event.
3026 */
3027static s32
1993732e 3028brcmf_notify_sched_scan_results(struct brcmf_if *ifp,
e5806072
AS
3029 const struct brcmf_event_msg *e, void *data)
3030{
1993732e 3031 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
e5806072
AS
3032 struct brcmf_pno_net_info_le *netinfo, *netinfo_start;
3033 struct cfg80211_scan_request *request = NULL;
3034 struct cfg80211_ssid *ssid = NULL;
3035 struct ieee80211_channel *channel = NULL;
27a68fe3 3036 struct wiphy *wiphy = cfg_to_wiphy(cfg);
e5806072
AS
3037 int err = 0;
3038 int channel_req = 0;
3039 int band = 0;
3040 struct brcmf_pno_scanresults_le *pfn_result;
3041 u32 result_count;
3042 u32 status;
3043
4e8a008e 3044 brcmf_dbg(SCAN, "Enter\n");
e5806072 3045
5c36b99a 3046 if (e->event_code == BRCMF_E_PFN_NET_LOST) {
4e8a008e 3047 brcmf_dbg(SCAN, "PFN NET LOST event. Do Nothing\n");
e5806072
AS
3048 return 0;
3049 }
3050
3051 pfn_result = (struct brcmf_pno_scanresults_le *)data;
3052 result_count = le32_to_cpu(pfn_result->count);
3053 status = le32_to_cpu(pfn_result->status);
3054
3055 /*
3056 * PFN event is limited to fit 512 bytes so we may get
3057 * multiple NET_FOUND events. For now place a warning here.
3058 */
3059 WARN_ON(status != BRCMF_PNO_SCAN_COMPLETE);
4e8a008e 3060 brcmf_dbg(SCAN, "PFN NET FOUND event. count: %d\n", result_count);
e5806072
AS
3061 if (result_count > 0) {
3062 int i;
3063
3064 request = kzalloc(sizeof(*request), GFP_KERNEL);
58901d18
DC
3065 ssid = kcalloc(result_count, sizeof(*ssid), GFP_KERNEL);
3066 channel = kcalloc(result_count, sizeof(*channel), GFP_KERNEL);
e5806072
AS
3067 if (!request || !ssid || !channel) {
3068 err = -ENOMEM;
3069 goto out_err;
3070 }
3071
3072 request->wiphy = wiphy;
3073 data += sizeof(struct brcmf_pno_scanresults_le);
3074 netinfo_start = (struct brcmf_pno_net_info_le *)data;
3075
3076 for (i = 0; i < result_count; i++) {
3077 netinfo = &netinfo_start[i];
3078 if (!netinfo) {
57d6e91a
AS
3079 brcmf_err("Invalid netinfo ptr. index: %d\n",
3080 i);
e5806072
AS
3081 err = -EINVAL;
3082 goto out_err;
3083 }
3084
4e8a008e
AS
3085 brcmf_dbg(SCAN, "SSID:%s Channel:%d\n",
3086 netinfo->SSID, netinfo->channel);
e5806072
AS
3087 memcpy(ssid[i].ssid, netinfo->SSID, netinfo->SSID_len);
3088 ssid[i].ssid_len = netinfo->SSID_len;
3089 request->n_ssids++;
3090
3091 channel_req = netinfo->channel;
3092 if (channel_req <= CH_MAX_2G_CHANNEL)
3093 band = NL80211_BAND_2GHZ;
3094 else
3095 band = NL80211_BAND_5GHZ;
3096 channel[i].center_freq =
3097 ieee80211_channel_to_frequency(channel_req,
3098 band);
3099 channel[i].band = band;
3100 channel[i].flags |= IEEE80211_CHAN_NO_HT40;
3101 request->channels[i] = &channel[i];
3102 request->n_channels++;
3103 }
3104
3105 /* assign parsed ssid array */
3106 if (request->n_ssids)
3107 request->ssids = &ssid[0];
3108
c1179033 3109 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
e5806072 3110 /* Abort any on-going scan */
27a68fe3 3111 brcmf_abort_scanning(cfg);
e5806072
AS
3112 }
3113
c1179033 3114 set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
2668b0b1 3115 cfg->escan_info.run = brcmf_run_escan;
a0f472ac 3116 err = brcmf_do_escan(cfg, wiphy, ifp, request);
e5806072 3117 if (err) {
c1179033 3118 clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
e5806072
AS
3119 goto out_err;
3120 }
27a68fe3
AS
3121 cfg->sched_escan = true;
3122 cfg->scan_request = request;
e5806072 3123 } else {
57d6e91a 3124 brcmf_err("FALSE PNO Event. (pfn_count == 0)\n");
e5806072
AS
3125 goto out_err;
3126 }
3127
3128 kfree(ssid);
3129 kfree(channel);
3130 kfree(request);
3131 return 0;
3132
3133out_err:
3134 kfree(ssid);
3135 kfree(channel);
3136 kfree(request);
3137 cfg80211_sched_scan_stopped(wiphy);
3138 return err;
3139}
3140
e5806072
AS
3141static int brcmf_dev_pno_clean(struct net_device *ndev)
3142{
e5806072
AS
3143 int ret;
3144
3145 /* Disable pfn */
ac24be6f 3146 ret = brcmf_fil_iovar_int_set(netdev_priv(ndev), "pfn", 0);
e5806072
AS
3147 if (ret == 0) {
3148 /* clear pfn */
ac24be6f
AS
3149 ret = brcmf_fil_iovar_data_set(netdev_priv(ndev), "pfnclear",
3150 NULL, 0);
e5806072
AS
3151 }
3152 if (ret < 0)
57d6e91a 3153 brcmf_err("failed code %d\n", ret);
e5806072
AS
3154
3155 return ret;
3156}
3157
3158static int brcmf_dev_pno_config(struct net_device *ndev)
3159{
3160 struct brcmf_pno_param_le pfn_param;
e5806072
AS
3161
3162 memset(&pfn_param, 0, sizeof(pfn_param));
3163 pfn_param.version = cpu_to_le32(BRCMF_PNO_VERSION);
3164
3165 /* set extra pno params */
3166 pfn_param.flags = cpu_to_le16(1 << BRCMF_PNO_ENABLE_ADAPTSCAN_BIT);
3167 pfn_param.repeat = BRCMF_PNO_REPEAT;
3168 pfn_param.exp = BRCMF_PNO_FREQ_EXPO_MAX;
3169
3170 /* set up pno scan fr */
3171 pfn_param.scan_freq = cpu_to_le32(BRCMF_PNO_TIME);
3172
ac24be6f
AS
3173 return brcmf_fil_iovar_data_set(netdev_priv(ndev), "pfn_set",
3174 &pfn_param, sizeof(pfn_param));
e5806072
AS
3175}
3176
3177static int
3178brcmf_cfg80211_sched_scan_start(struct wiphy *wiphy,
3179 struct net_device *ndev,
3180 struct cfg80211_sched_scan_request *request)
3181{
c1179033 3182 struct brcmf_if *ifp = netdev_priv(ndev);
27a68fe3 3183 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
e5806072
AS
3184 struct brcmf_pno_net_param_le pfn;
3185 int i;
3186 int ret = 0;
3187
dc7bdbf1 3188 brcmf_dbg(SCAN, "Enter n_match_sets:%d n_ssids:%d\n",
4e8a008e 3189 request->n_match_sets, request->n_ssids);
c1179033 3190 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
57d6e91a 3191 brcmf_err("Scanning already: status (%lu)\n", cfg->scan_status);
e5806072
AS
3192 return -EAGAIN;
3193 }
1687eee2
AS
3194 if (test_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
3195 brcmf_err("Scanning suppressed: status (%lu)\n",
3196 cfg->scan_status);
3197 return -EAGAIN;
3198 }
e5806072 3199
dc7bdbf1 3200 if (!request->n_ssids || !request->n_match_sets) {
181f2d17 3201 brcmf_dbg(SCAN, "Invalid sched scan req!! n_ssids:%d\n",
dc7bdbf1 3202 request->n_ssids);
e5806072
AS
3203 return -EINVAL;
3204 }
3205
3206 if (request->n_ssids > 0) {
3207 for (i = 0; i < request->n_ssids; i++) {
3208 /* Active scan req for ssids */
4e8a008e
AS
3209 brcmf_dbg(SCAN, ">>> Active scan req for ssid (%s)\n",
3210 request->ssids[i].ssid);
e5806072
AS
3211
3212 /*
3213 * match_set ssids is a supert set of n_ssid list,
3214 * so we need not add these set seperately.
3215 */
3216 }
3217 }
3218
3219 if (request->n_match_sets > 0) {
3220 /* clean up everything */
3221 ret = brcmf_dev_pno_clean(ndev);
3222 if (ret < 0) {
57d6e91a 3223 brcmf_err("failed error=%d\n", ret);
e5806072
AS
3224 return ret;
3225 }
3226
3227 /* configure pno */
3228 ret = brcmf_dev_pno_config(ndev);
3229 if (ret < 0) {
57d6e91a 3230 brcmf_err("PNO setup failed!! ret=%d\n", ret);
e5806072
AS
3231 return -EINVAL;
3232 }
3233
3234 /* configure each match set */
3235 for (i = 0; i < request->n_match_sets; i++) {
3236 struct cfg80211_ssid *ssid;
3237 u32 ssid_len;
3238
3239 ssid = &request->match_sets[i].ssid;
3240 ssid_len = ssid->ssid_len;
3241
3242 if (!ssid_len) {
57d6e91a 3243 brcmf_err("skip broadcast ssid\n");
e5806072
AS
3244 continue;
3245 }
3246 pfn.auth = cpu_to_le32(WLAN_AUTH_OPEN);
3247 pfn.wpa_auth = cpu_to_le32(BRCMF_PNO_WPA_AUTH_ANY);
3248 pfn.wsec = cpu_to_le32(0);
3249 pfn.infra = cpu_to_le32(1);
3250 pfn.flags = cpu_to_le32(1 << BRCMF_PNO_HIDDEN_BIT);
3251 pfn.ssid.SSID_len = cpu_to_le32(ssid_len);
3252 memcpy(pfn.ssid.SSID, ssid->ssid, ssid_len);
c1179033 3253 ret = brcmf_fil_iovar_data_set(ifp, "pfn_add", &pfn,
ac24be6f 3254 sizeof(pfn));
4e8a008e
AS
3255 brcmf_dbg(SCAN, ">>> PNO filter %s for ssid (%s)\n",
3256 ret == 0 ? "set" : "failed", ssid->ssid);
e5806072
AS
3257 }
3258 /* Enable the PNO */
c1179033 3259 if (brcmf_fil_iovar_int_set(ifp, "pfn", 1) < 0) {
57d6e91a 3260 brcmf_err("PNO enable failed!! ret=%d\n", ret);
e5806072
AS
3261 return -EINVAL;
3262 }
3263 } else {
3264 return -EINVAL;
3265 }
3266
3267 return 0;
3268}
3269
3270static int brcmf_cfg80211_sched_scan_stop(struct wiphy *wiphy,
3271 struct net_device *ndev)
3272{
27a68fe3 3273 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
e5806072 3274
4e8a008e 3275 brcmf_dbg(SCAN, "enter\n");
e5806072 3276 brcmf_dev_pno_clean(ndev);
27a68fe3 3277 if (cfg->sched_escan)
a0f472ac 3278 brcmf_notify_escan_complete(cfg, netdev_priv(ndev), true, true);
e5806072
AS
3279 return 0;
3280}
e5806072 3281
1f170110 3282static s32 brcmf_configure_opensecurity(struct brcmf_if *ifp)
1a873342
HM
3283{
3284 s32 err;
3285
3286 /* set auth */
ac24be6f 3287 err = brcmf_fil_bsscfg_int_set(ifp, "auth", 0);
1a873342 3288 if (err < 0) {
57d6e91a 3289 brcmf_err("auth error %d\n", err);
1a873342
HM
3290 return err;
3291 }
3292 /* set wsec */
ac24be6f 3293 err = brcmf_fil_bsscfg_int_set(ifp, "wsec", 0);
1a873342 3294 if (err < 0) {
57d6e91a 3295 brcmf_err("wsec error %d\n", err);
1a873342
HM
3296 return err;
3297 }
3298 /* set upper-layer auth */
ac24be6f 3299 err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", WPA_AUTH_NONE);
1a873342 3300 if (err < 0) {
57d6e91a 3301 brcmf_err("wpa_auth error %d\n", err);
1a873342
HM
3302 return err;
3303 }
3304
3305 return 0;
3306}
3307
3308static bool brcmf_valid_wpa_oui(u8 *oui, bool is_rsn_ie)
3309{
3310 if (is_rsn_ie)
3311 return (memcmp(oui, RSN_OUI, TLV_OUI_LEN) == 0);
3312
3313 return (memcmp(oui, WPA_OUI, TLV_OUI_LEN) == 0);
3314}
3315
3316static s32
4b5800fe
JB
3317brcmf_configure_wpaie(struct net_device *ndev,
3318 const struct brcmf_vs_tlv *wpa_ie,
3319 bool is_rsn_ie)
1a873342 3320{
ac24be6f 3321 struct brcmf_if *ifp = netdev_priv(ndev);
1a873342
HM
3322 u32 auth = 0; /* d11 open authentication */
3323 u16 count;
3324 s32 err = 0;
3325 s32 len = 0;
3326 u32 i;
3327 u32 wsec;
3328 u32 pval = 0;
3329 u32 gval = 0;
3330 u32 wpa_auth = 0;
3331 u32 offset;
3332 u8 *data;
3333 u16 rsn_cap;
3334 u32 wme_bss_disable;
3335
d96b801f 3336 brcmf_dbg(TRACE, "Enter\n");
1a873342
HM
3337 if (wpa_ie == NULL)
3338 goto exit;
3339
3340 len = wpa_ie->len + TLV_HDR_LEN;
3341 data = (u8 *)wpa_ie;
619c5a9a 3342 offset = TLV_HDR_LEN;
1a873342
HM
3343 if (!is_rsn_ie)
3344 offset += VS_IE_FIXED_HDR_LEN;
619c5a9a
HM
3345 else
3346 offset += WPA_IE_VERSION_LEN;
1a873342
HM
3347
3348 /* check for multicast cipher suite */
3349 if (offset + WPA_IE_MIN_OUI_LEN > len) {
3350 err = -EINVAL;
57d6e91a 3351 brcmf_err("no multicast cipher suite\n");
1a873342
HM
3352 goto exit;
3353 }
3354
3355 if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3356 err = -EINVAL;
57d6e91a 3357 brcmf_err("ivalid OUI\n");
1a873342
HM
3358 goto exit;
3359 }
3360 offset += TLV_OUI_LEN;
3361
3362 /* pick up multicast cipher */
3363 switch (data[offset]) {
3364 case WPA_CIPHER_NONE:
3365 gval = 0;
3366 break;
3367 case WPA_CIPHER_WEP_40:
3368 case WPA_CIPHER_WEP_104:
3369 gval = WEP_ENABLED;
3370 break;
3371 case WPA_CIPHER_TKIP:
3372 gval = TKIP_ENABLED;
3373 break;
3374 case WPA_CIPHER_AES_CCM:
3375 gval = AES_ENABLED;
3376 break;
3377 default:
3378 err = -EINVAL;
57d6e91a 3379 brcmf_err("Invalid multi cast cipher info\n");
1a873342
HM
3380 goto exit;
3381 }
3382
3383 offset++;
3384 /* walk thru unicast cipher list and pick up what we recognize */
3385 count = data[offset] + (data[offset + 1] << 8);
3386 offset += WPA_IE_SUITE_COUNT_LEN;
3387 /* Check for unicast suite(s) */
3388 if (offset + (WPA_IE_MIN_OUI_LEN * count) > len) {
3389 err = -EINVAL;
57d6e91a 3390 brcmf_err("no unicast cipher suite\n");
1a873342
HM
3391 goto exit;
3392 }
3393 for (i = 0; i < count; i++) {
3394 if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3395 err = -EINVAL;
57d6e91a 3396 brcmf_err("ivalid OUI\n");
1a873342
HM
3397 goto exit;
3398 }
3399 offset += TLV_OUI_LEN;
3400 switch (data[offset]) {
3401 case WPA_CIPHER_NONE:
3402 break;
3403 case WPA_CIPHER_WEP_40:
3404 case WPA_CIPHER_WEP_104:
3405 pval |= WEP_ENABLED;
3406 break;
3407 case WPA_CIPHER_TKIP:
3408 pval |= TKIP_ENABLED;
3409 break;
3410 case WPA_CIPHER_AES_CCM:
3411 pval |= AES_ENABLED;
3412 break;
3413 default:
57d6e91a 3414 brcmf_err("Ivalid unicast security info\n");
1a873342
HM
3415 }
3416 offset++;
3417 }
3418 /* walk thru auth management suite list and pick up what we recognize */
3419 count = data[offset] + (data[offset + 1] << 8);
3420 offset += WPA_IE_SUITE_COUNT_LEN;
3421 /* Check for auth key management suite(s) */
3422 if (offset + (WPA_IE_MIN_OUI_LEN * count) > len) {
3423 err = -EINVAL;
57d6e91a 3424 brcmf_err("no auth key mgmt suite\n");
1a873342
HM
3425 goto exit;
3426 }
3427 for (i = 0; i < count; i++) {
3428 if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3429 err = -EINVAL;
57d6e91a 3430 brcmf_err("ivalid OUI\n");
1a873342
HM
3431 goto exit;
3432 }
3433 offset += TLV_OUI_LEN;
3434 switch (data[offset]) {
3435 case RSN_AKM_NONE:
d96b801f 3436 brcmf_dbg(TRACE, "RSN_AKM_NONE\n");
1a873342
HM
3437 wpa_auth |= WPA_AUTH_NONE;
3438 break;
3439 case RSN_AKM_UNSPECIFIED:
d96b801f 3440 brcmf_dbg(TRACE, "RSN_AKM_UNSPECIFIED\n");
1a873342
HM
3441 is_rsn_ie ? (wpa_auth |= WPA2_AUTH_UNSPECIFIED) :
3442 (wpa_auth |= WPA_AUTH_UNSPECIFIED);
3443 break;
3444 case RSN_AKM_PSK:
d96b801f 3445 brcmf_dbg(TRACE, "RSN_AKM_PSK\n");
1a873342
HM
3446 is_rsn_ie ? (wpa_auth |= WPA2_AUTH_PSK) :
3447 (wpa_auth |= WPA_AUTH_PSK);
3448 break;
3449 default:
57d6e91a 3450 brcmf_err("Ivalid key mgmt info\n");
1a873342
HM
3451 }
3452 offset++;
3453 }
3454
3455 if (is_rsn_ie) {
3456 wme_bss_disable = 1;
3457 if ((offset + RSN_CAP_LEN) <= len) {
3458 rsn_cap = data[offset] + (data[offset + 1] << 8);
3459 if (rsn_cap & RSN_CAP_PTK_REPLAY_CNTR_MASK)
3460 wme_bss_disable = 0;
3461 }
3462 /* set wme_bss_disable to sync RSN Capabilities */
ac24be6f 3463 err = brcmf_fil_bsscfg_int_set(ifp, "wme_bss_disable",
81f5dcb8 3464 wme_bss_disable);
1a873342 3465 if (err < 0) {
57d6e91a 3466 brcmf_err("wme_bss_disable error %d\n", err);
1a873342
HM
3467 goto exit;
3468 }
3469 }
3470 /* FOR WPS , set SES_OW_ENABLED */
3471 wsec = (pval | gval | SES_OW_ENABLED);
3472
3473 /* set auth */
ac24be6f 3474 err = brcmf_fil_bsscfg_int_set(ifp, "auth", auth);
1a873342 3475 if (err < 0) {
57d6e91a 3476 brcmf_err("auth error %d\n", err);
1a873342
HM
3477 goto exit;
3478 }
3479 /* set wsec */
ac24be6f 3480 err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
1a873342 3481 if (err < 0) {
57d6e91a 3482 brcmf_err("wsec error %d\n", err);
1a873342
HM
3483 goto exit;
3484 }
3485 /* set upper-layer auth */
ac24be6f 3486 err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_auth);
1a873342 3487 if (err < 0) {
57d6e91a 3488 brcmf_err("wpa_auth error %d\n", err);
1a873342
HM
3489 goto exit;
3490 }
3491
3492exit:
3493 return err;
3494}
3495
3496static s32
3082b9be 3497brcmf_parse_vndr_ies(const u8 *vndr_ie_buf, u32 vndr_ie_len,
1a873342
HM
3498 struct parsed_vndr_ies *vndr_ies)
3499{
1a873342
HM
3500 struct brcmf_vs_tlv *vndrie;
3501 struct brcmf_tlv *ie;
3502 struct parsed_vndr_ie_info *parsed_info;
3503 s32 remaining_len;
3504
3505 remaining_len = (s32)vndr_ie_len;
3506 memset(vndr_ies, 0, sizeof(*vndr_ies));
3507
3508 ie = (struct brcmf_tlv *)vndr_ie_buf;
3509 while (ie) {
3510 if (ie->id != WLAN_EID_VENDOR_SPECIFIC)
3511 goto next;
3512 vndrie = (struct brcmf_vs_tlv *)ie;
3513 /* len should be bigger than OUI length + one */
3514 if (vndrie->len < (VS_IE_FIXED_HDR_LEN - TLV_HDR_LEN + 1)) {
57d6e91a
AS
3515 brcmf_err("invalid vndr ie. length is too small %d\n",
3516 vndrie->len);
1a873342
HM
3517 goto next;
3518 }
3519 /* if wpa or wme ie, do not add ie */
3520 if (!memcmp(vndrie->oui, (u8 *)WPA_OUI, TLV_OUI_LEN) &&
3521 ((vndrie->oui_type == WPA_OUI_TYPE) ||
3522 (vndrie->oui_type == WME_OUI_TYPE))) {
d96b801f 3523 brcmf_dbg(TRACE, "Found WPA/WME oui. Do not add it\n");
1a873342
HM
3524 goto next;
3525 }
3526
3527 parsed_info = &vndr_ies->ie_info[vndr_ies->count];
3528
3529 /* save vndr ie information */
3530 parsed_info->ie_ptr = (char *)vndrie;
3531 parsed_info->ie_len = vndrie->len + TLV_HDR_LEN;
3532 memcpy(&parsed_info->vndrie, vndrie, sizeof(*vndrie));
3533
3534 vndr_ies->count++;
3535
d96b801f
AS
3536 brcmf_dbg(TRACE, "** OUI %02x %02x %02x, type 0x%02x\n",
3537 parsed_info->vndrie.oui[0],
3538 parsed_info->vndrie.oui[1],
3539 parsed_info->vndrie.oui[2],
3540 parsed_info->vndrie.oui_type);
1a873342 3541
9f440b7b 3542 if (vndr_ies->count >= VNDR_IE_PARSE_LIMIT)
1a873342
HM
3543 break;
3544next:
b41fc3d7
HM
3545 remaining_len -= (ie->len + TLV_HDR_LEN);
3546 if (remaining_len <= TLV_HDR_LEN)
1a873342
HM
3547 ie = NULL;
3548 else
b41fc3d7
HM
3549 ie = (struct brcmf_tlv *)(((u8 *)ie) + ie->len +
3550 TLV_HDR_LEN);
1a873342 3551 }
12f32370 3552 return 0;
1a873342
HM
3553}
3554
3555static u32
3556brcmf_vndr_ie(u8 *iebuf, s32 pktflag, u8 *ie_ptr, u32 ie_len, s8 *add_del_cmd)
3557{
3558
3559 __le32 iecount_le;
3560 __le32 pktflag_le;
3561
3562 strncpy(iebuf, add_del_cmd, VNDR_IE_CMD_LEN - 1);
3563 iebuf[VNDR_IE_CMD_LEN - 1] = '\0';
3564
3565 iecount_le = cpu_to_le32(1);
3566 memcpy(&iebuf[VNDR_IE_COUNT_OFFSET], &iecount_le, sizeof(iecount_le));
3567
3568 pktflag_le = cpu_to_le32(pktflag);
3569 memcpy(&iebuf[VNDR_IE_PKTFLAG_OFFSET], &pktflag_le, sizeof(pktflag_le));
3570
3571 memcpy(&iebuf[VNDR_IE_VSIE_OFFSET], ie_ptr, ie_len);
3572
3573 return ie_len + VNDR_IE_HDR_SIZE;
3574}
3575
1332e26e
AS
3576s32 brcmf_vif_set_mgmt_ie(struct brcmf_cfg80211_vif *vif, s32 pktflag,
3577 const u8 *vndr_ie_buf, u32 vndr_ie_len)
1a873342 3578{
1332e26e
AS
3579 struct brcmf_if *ifp;
3580 struct vif_saved_ie *saved_ie;
1a873342
HM
3581 s32 err = 0;
3582 u8 *iovar_ie_buf;
3583 u8 *curr_ie_buf;
3584 u8 *mgmt_ie_buf = NULL;
3e4f319d 3585 int mgmt_ie_buf_len;
81118d16 3586 u32 *mgmt_ie_len;
1a873342
HM
3587 u32 del_add_ie_buf_len = 0;
3588 u32 total_ie_buf_len = 0;
3589 u32 parsed_ie_buf_len = 0;
3590 struct parsed_vndr_ies old_vndr_ies;
3591 struct parsed_vndr_ies new_vndr_ies;
3592 struct parsed_vndr_ie_info *vndrie_info;
3593 s32 i;
3594 u8 *ptr;
3e4f319d 3595 int remained_buf_len;
1a873342 3596
1332e26e
AS
3597 if (!vif)
3598 return -ENODEV;
3599 ifp = vif->ifp;
3600 saved_ie = &vif->saved_ie;
3601
d96b801f 3602 brcmf_dbg(TRACE, "bssidx %d, pktflag : 0x%02X\n", ifp->bssidx, pktflag);
1a873342
HM
3603 iovar_ie_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
3604 if (!iovar_ie_buf)
3605 return -ENOMEM;
3606 curr_ie_buf = iovar_ie_buf;
89286dc9
HM
3607 switch (pktflag) {
3608 case BRCMF_VNDR_IE_PRBREQ_FLAG:
3609 mgmt_ie_buf = saved_ie->probe_req_ie;
3610 mgmt_ie_len = &saved_ie->probe_req_ie_len;
3611 mgmt_ie_buf_len = sizeof(saved_ie->probe_req_ie);
3612 break;
3613 case BRCMF_VNDR_IE_PRBRSP_FLAG:
3614 mgmt_ie_buf = saved_ie->probe_res_ie;
3615 mgmt_ie_len = &saved_ie->probe_res_ie_len;
3616 mgmt_ie_buf_len = sizeof(saved_ie->probe_res_ie);
3617 break;
3618 case BRCMF_VNDR_IE_BEACON_FLAG:
3619 mgmt_ie_buf = saved_ie->beacon_ie;
3620 mgmt_ie_len = &saved_ie->beacon_ie_len;
3621 mgmt_ie_buf_len = sizeof(saved_ie->beacon_ie);
3622 break;
3623 case BRCMF_VNDR_IE_ASSOCREQ_FLAG:
3624 mgmt_ie_buf = saved_ie->assoc_req_ie;
3625 mgmt_ie_len = &saved_ie->assoc_req_ie_len;
3626 mgmt_ie_buf_len = sizeof(saved_ie->assoc_req_ie);
3627 break;
3628 default:
3629 err = -EPERM;
3630 brcmf_err("not suitable type\n");
3631 goto exit;
1a873342
HM
3632 }
3633
3634 if (vndr_ie_len > mgmt_ie_buf_len) {
3635 err = -ENOMEM;
57d6e91a 3636 brcmf_err("extra IE size too big\n");
1a873342
HM
3637 goto exit;
3638 }
3639
3640 /* parse and save new vndr_ie in curr_ie_buff before comparing it */
3641 if (vndr_ie_buf && vndr_ie_len && curr_ie_buf) {
3642 ptr = curr_ie_buf;
3643 brcmf_parse_vndr_ies(vndr_ie_buf, vndr_ie_len, &new_vndr_ies);
3644 for (i = 0; i < new_vndr_ies.count; i++) {
3645 vndrie_info = &new_vndr_ies.ie_info[i];
3646 memcpy(ptr + parsed_ie_buf_len, vndrie_info->ie_ptr,
3647 vndrie_info->ie_len);
3648 parsed_ie_buf_len += vndrie_info->ie_len;
3649 }
3650 }
3651
b41fc3d7 3652 if (mgmt_ie_buf && *mgmt_ie_len) {
1a873342
HM
3653 if (parsed_ie_buf_len && (parsed_ie_buf_len == *mgmt_ie_len) &&
3654 (memcmp(mgmt_ie_buf, curr_ie_buf,
3655 parsed_ie_buf_len) == 0)) {
d96b801f 3656 brcmf_dbg(TRACE, "Previous mgmt IE equals to current IE\n");
1a873342
HM
3657 goto exit;
3658 }
3659
3660 /* parse old vndr_ie */
3661 brcmf_parse_vndr_ies(mgmt_ie_buf, *mgmt_ie_len, &old_vndr_ies);
3662
3663 /* make a command to delete old ie */
3664 for (i = 0; i < old_vndr_ies.count; i++) {
3665 vndrie_info = &old_vndr_ies.ie_info[i];
3666
d96b801f
AS
3667 brcmf_dbg(TRACE, "DEL ID : %d, Len: %d , OUI:%02x:%02x:%02x\n",
3668 vndrie_info->vndrie.id,
3669 vndrie_info->vndrie.len,
3670 vndrie_info->vndrie.oui[0],
3671 vndrie_info->vndrie.oui[1],
3672 vndrie_info->vndrie.oui[2]);
1a873342
HM
3673
3674 del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
3675 vndrie_info->ie_ptr,
3676 vndrie_info->ie_len,
3677 "del");
3678 curr_ie_buf += del_add_ie_buf_len;
3679 total_ie_buf_len += del_add_ie_buf_len;
3680 }
3681 }
3682
3683 *mgmt_ie_len = 0;
3684 /* Add if there is any extra IE */
3685 if (mgmt_ie_buf && parsed_ie_buf_len) {
3686 ptr = mgmt_ie_buf;
3687
3688 remained_buf_len = mgmt_ie_buf_len;
3689
3690 /* make a command to add new ie */
3691 for (i = 0; i < new_vndr_ies.count; i++) {
3692 vndrie_info = &new_vndr_ies.ie_info[i];
3693
b41fc3d7
HM
3694 /* verify remained buf size before copy data */
3695 if (remained_buf_len < (vndrie_info->vndrie.len +
3696 VNDR_IE_VSIE_OFFSET)) {
57d6e91a
AS
3697 brcmf_err("no space in mgmt_ie_buf: len left %d",
3698 remained_buf_len);
b41fc3d7
HM
3699 break;
3700 }
3701 remained_buf_len -= (vndrie_info->ie_len +
3702 VNDR_IE_VSIE_OFFSET);
3703
d96b801f
AS
3704 brcmf_dbg(TRACE, "ADDED ID : %d, Len: %d, OUI:%02x:%02x:%02x\n",
3705 vndrie_info->vndrie.id,
3706 vndrie_info->vndrie.len,
3707 vndrie_info->vndrie.oui[0],
3708 vndrie_info->vndrie.oui[1],
3709 vndrie_info->vndrie.oui[2]);
1a873342
HM
3710
3711 del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
3712 vndrie_info->ie_ptr,
3713 vndrie_info->ie_len,
3714 "add");
1a873342
HM
3715
3716 /* save the parsed IE in wl struct */
3717 memcpy(ptr + (*mgmt_ie_len), vndrie_info->ie_ptr,
3718 vndrie_info->ie_len);
3719 *mgmt_ie_len += vndrie_info->ie_len;
3720
3721 curr_ie_buf += del_add_ie_buf_len;
3722 total_ie_buf_len += del_add_ie_buf_len;
3723 }
3724 }
3725 if (total_ie_buf_len) {
c1179033 3726 err = brcmf_fil_bsscfg_data_set(ifp, "vndr_ie", iovar_ie_buf,
81f5dcb8 3727 total_ie_buf_len);
1a873342 3728 if (err)
57d6e91a 3729 brcmf_err("vndr ie set error : %d\n", err);
1a873342
HM
3730 }
3731
3732exit:
3733 kfree(iovar_ie_buf);
3734 return err;
3735}
3736
5f4f9f11
AS
3737s32 brcmf_vif_clear_mgmt_ies(struct brcmf_cfg80211_vif *vif)
3738{
3739 s32 pktflags[] = {
3740 BRCMF_VNDR_IE_PRBREQ_FLAG,
3741 BRCMF_VNDR_IE_PRBRSP_FLAG,
3742 BRCMF_VNDR_IE_BEACON_FLAG
3743 };
3744 int i;
3745
3746 for (i = 0; i < ARRAY_SIZE(pktflags); i++)
3747 brcmf_vif_set_mgmt_ie(vif, pktflags[i], NULL, 0);
3748
3749 memset(&vif->saved_ie, 0, sizeof(vif->saved_ie));
3750 return 0;
3751}
3752
a0f07959
HM
3753static s32
3754brcmf_config_ap_mgmt_ie(struct brcmf_cfg80211_vif *vif,
3755 struct cfg80211_beacon_data *beacon)
3756{
3757 s32 err;
3758
3759 /* Set Beacon IEs to FW */
3760 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_BEACON_FLAG,
3761 beacon->tail, beacon->tail_len);
3762 if (err) {
3763 brcmf_err("Set Beacon IE Failed\n");
3764 return err;
3765 }
3766 brcmf_dbg(TRACE, "Applied Vndr IEs for Beacon\n");
3767
3768 /* Set Probe Response IEs to FW */
3769 err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBRSP_FLAG,
3770 beacon->proberesp_ies,
3771 beacon->proberesp_ies_len);
3772 if (err)
3773 brcmf_err("Set Probe Resp IE Failed\n");
3774 else
3775 brcmf_dbg(TRACE, "Applied Vndr IEs for Probe Resp\n");
3776
3777 return err;
3778}
3779
1a873342
HM
3780static s32
3781brcmf_cfg80211_start_ap(struct wiphy *wiphy, struct net_device *ndev,
3782 struct cfg80211_ap_settings *settings)
3783{
3784 s32 ie_offset;
1c9d30cf 3785 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
ac24be6f 3786 struct brcmf_if *ifp = netdev_priv(ndev);
4b5800fe 3787 const struct brcmf_tlv *ssid_ie;
1a873342 3788 struct brcmf_ssid_le ssid_le;
1a873342 3789 s32 err = -EPERM;
4b5800fe
JB
3790 const struct brcmf_tlv *rsn_ie;
3791 const struct brcmf_vs_tlv *wpa_ie;
1a873342 3792 struct brcmf_join_params join_params;
a0f07959
HM
3793 enum nl80211_iftype dev_role;
3794 struct brcmf_fil_bss_enable_le bss_enable;
06c01585 3795 u16 chanspec;
1a873342 3796
06c01585
AS
3797 brcmf_dbg(TRACE, "ctrlchn=%d, center=%d, bw=%d, beacon_interval=%d, dtim_period=%d,\n",
3798 settings->chandef.chan->hw_value,
3799 settings->chandef.center_freq1, settings->chandef.width,
a9a56878 3800 settings->beacon_interval, settings->dtim_period);
d96b801f
AS
3801 brcmf_dbg(TRACE, "ssid=%s(%zu), auth_type=%d, inactivity_timeout=%d\n",
3802 settings->ssid, settings->ssid_len, settings->auth_type,
3803 settings->inactivity_timeout);
1a873342 3804
426d0a56 3805 dev_role = ifp->vif->wdev.iftype;
1a873342
HM
3806
3807 memset(&ssid_le, 0, sizeof(ssid_le));
3808 if (settings->ssid == NULL || settings->ssid_len == 0) {
3809 ie_offset = DOT11_MGMT_HDR_LEN + DOT11_BCN_PRB_FIXED_LEN;
3810 ssid_ie = brcmf_parse_tlvs(
3811 (u8 *)&settings->beacon.head[ie_offset],
3812 settings->beacon.head_len - ie_offset,
3813 WLAN_EID_SSID);
3814 if (!ssid_ie)
3815 return -EINVAL;
3816
3817 memcpy(ssid_le.SSID, ssid_ie->data, ssid_ie->len);
3818 ssid_le.SSID_len = cpu_to_le32(ssid_ie->len);
d96b801f 3819 brcmf_dbg(TRACE, "SSID is (%s) in Head\n", ssid_le.SSID);
1a873342
HM
3820 } else {
3821 memcpy(ssid_le.SSID, settings->ssid, settings->ssid_len);
3822 ssid_le.SSID_len = cpu_to_le32((u32)settings->ssid_len);
3823 }
3824
f96aa07e 3825 brcmf_set_mpc(ifp, 0);
b3657453 3826 brcmf_configure_arp_offload(ifp, false);
1a873342
HM
3827
3828 /* find the RSN_IE */
3829 rsn_ie = brcmf_parse_tlvs((u8 *)settings->beacon.tail,
3830 settings->beacon.tail_len, WLAN_EID_RSN);
3831
3832 /* find the WPA_IE */
3833 wpa_ie = brcmf_find_wpaie((u8 *)settings->beacon.tail,
3834 settings->beacon.tail_len);
3835
1a873342 3836 if ((wpa_ie != NULL || rsn_ie != NULL)) {
d96b801f 3837 brcmf_dbg(TRACE, "WPA(2) IE is found\n");
1a873342
HM
3838 if (wpa_ie != NULL) {
3839 /* WPA IE */
34778529 3840 err = brcmf_configure_wpaie(ndev, wpa_ie, false);
1a873342
HM
3841 if (err < 0)
3842 goto exit;
1a873342
HM
3843 } else {
3844 /* RSN IE */
3845 err = brcmf_configure_wpaie(ndev,
34778529 3846 (struct brcmf_vs_tlv *)rsn_ie, true);
1a873342
HM
3847 if (err < 0)
3848 goto exit;
1a873342 3849 }
1a873342 3850 } else {
d96b801f 3851 brcmf_dbg(TRACE, "No WPA(2) IEs found\n");
1f170110 3852 brcmf_configure_opensecurity(ifp);
1a873342 3853 }
1a873342 3854
a0f07959 3855 brcmf_config_ap_mgmt_ie(ifp->vif, &settings->beacon);
1a873342 3856
600a897d 3857 chanspec = chandef_to_chanspec(&cfg->d11inf, &settings->chandef);
06c01585 3858 err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
1c9d30cf 3859 if (err < 0) {
06c01585 3860 brcmf_err("Set Channel failed: chspec=%d, %d\n", chanspec, err);
1c9d30cf
HM
3861 goto exit;
3862 }
3863
1a873342 3864 if (settings->beacon_interval) {
ac24be6f 3865 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD,
81f5dcb8 3866 settings->beacon_interval);
1a873342 3867 if (err < 0) {
57d6e91a 3868 brcmf_err("Beacon Interval Set Error, %d\n", err);
1a873342
HM
3869 goto exit;
3870 }
3871 }
3872 if (settings->dtim_period) {
ac24be6f 3873 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_DTIMPRD,
81f5dcb8 3874 settings->dtim_period);
1a873342 3875 if (err < 0) {
57d6e91a 3876 brcmf_err("DTIM Interval Set Error, %d\n", err);
1a873342
HM
3877 goto exit;
3878 }
3879 }
a0f07959
HM
3880
3881 if (dev_role == NL80211_IFTYPE_AP) {
3882 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
3883 if (err < 0) {
3884 brcmf_err("BRCMF_C_DOWN error %d\n", err);
3885 goto exit;
3886 }
2880b868 3887 brcmf_fil_iovar_int_set(ifp, "apsta", 0);
1a873342
HM
3888 }
3889
a0f07959 3890 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 1);
1a873342 3891 if (err < 0) {
a0f07959 3892 brcmf_err("SET INFRA error %d\n", err);
1a873342
HM
3893 goto exit;
3894 }
a0f07959
HM
3895 if (dev_role == NL80211_IFTYPE_AP) {
3896 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 1);
3897 if (err < 0) {
3898 brcmf_err("setting AP mode failed %d\n", err);
3899 goto exit;
3900 }
3901 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
3902 if (err < 0) {
3903 brcmf_err("BRCMF_C_UP error (%d)\n", err);
3904 goto exit;
3905 }
3906
3907 memset(&join_params, 0, sizeof(join_params));
3908 /* join parameters starts with ssid */
3909 memcpy(&join_params.ssid_le, &ssid_le, sizeof(ssid_le));
3910 /* create softap */
3911 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
3912 &join_params, sizeof(join_params));
3913 if (err < 0) {
3914 brcmf_err("SET SSID error (%d)\n", err);
3915 goto exit;
3916 }
3917 brcmf_dbg(TRACE, "AP mode configuration complete\n");
3918 } else {
3919 err = brcmf_fil_bsscfg_data_set(ifp, "ssid", &ssid_le,
3920 sizeof(ssid_le));
3921 if (err < 0) {
3922 brcmf_err("setting ssid failed %d\n", err);
3923 goto exit;
3924 }
3925 bss_enable.bsscfg_idx = cpu_to_le32(ifp->bssidx);
3926 bss_enable.enable = cpu_to_le32(1);
3927 err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
3928 sizeof(bss_enable));
3929 if (err < 0) {
3930 brcmf_err("bss_enable config failed %d\n", err);
3931 goto exit;
3932 }
3933
3934 brcmf_dbg(TRACE, "GO mode configuration complete\n");
3935 }
c1179033
AS
3936 clear_bit(BRCMF_VIF_STATUS_AP_CREATING, &ifp->vif->sme_state);
3937 set_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
1a873342
HM
3938
3939exit:
b3657453 3940 if (err) {
f96aa07e 3941 brcmf_set_mpc(ifp, 1);
b3657453
HM
3942 brcmf_configure_arp_offload(ifp, true);
3943 }
1a873342
HM
3944 return err;
3945}
3946
3947static int brcmf_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *ndev)
3948{
c1179033 3949 struct brcmf_if *ifp = netdev_priv(ndev);
5c33a942 3950 s32 err;
426d0a56 3951 struct brcmf_fil_bss_enable_le bss_enable;
5c33a942 3952 struct brcmf_join_params join_params;
1a873342 3953
d96b801f 3954 brcmf_dbg(TRACE, "Enter\n");
1a873342 3955
426d0a56 3956 if (ifp->vif->wdev.iftype == NL80211_IFTYPE_AP) {
1a873342
HM
3957 /* Due to most likely deauths outstanding we sleep */
3958 /* first to make sure they get processed by fw. */
3959 msleep(400);
5c33a942
HM
3960
3961 memset(&join_params, 0, sizeof(join_params));
3962 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
3963 &join_params, sizeof(join_params));
3964 if (err < 0)
3965 brcmf_err("SET SSID error (%d)\n", err);
128ce3b6 3966 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
5c33a942 3967 if (err < 0)
57d6e91a 3968 brcmf_err("BRCMF_C_UP error %d\n", err);
5c33a942
HM
3969 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 0);
3970 if (err < 0)
3971 brcmf_err("setting AP mode failed %d\n", err);
3972 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 0);
3973 if (err < 0)
3974 brcmf_err("setting INFRA mode failed %d\n", err);
426d0a56
HM
3975 } else {
3976 bss_enable.bsscfg_idx = cpu_to_le32(ifp->bssidx);
3977 bss_enable.enable = cpu_to_le32(0);
3978 err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
3979 sizeof(bss_enable));
3980 if (err < 0)
3981 brcmf_err("bss_enable config failed %d\n", err);
1a873342 3982 }
f96aa07e 3983 brcmf_set_mpc(ifp, 1);
b3657453 3984 brcmf_configure_arp_offload(ifp, true);
426d0a56
HM
3985 set_bit(BRCMF_VIF_STATUS_AP_CREATING, &ifp->vif->sme_state);
3986 clear_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
3987
1a873342
HM
3988 return err;
3989}
3990
a0f07959
HM
3991static s32
3992brcmf_cfg80211_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
3993 struct cfg80211_beacon_data *info)
3994{
a0f07959
HM
3995 struct brcmf_if *ifp = netdev_priv(ndev);
3996 s32 err;
3997
3998 brcmf_dbg(TRACE, "Enter\n");
3999
a0f07959
HM
4000 err = brcmf_config_ap_mgmt_ie(ifp->vif, info);
4001
4002 return err;
4003}
4004
1a873342
HM
4005static int
4006brcmf_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev,
3b3a0162 4007 const u8 *mac)
1a873342 4008{
a0f07959 4009 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1a873342 4010 struct brcmf_scb_val_le scbval;
0abb5f21 4011 struct brcmf_if *ifp = netdev_priv(ndev);
1a873342
HM
4012 s32 err;
4013
4014 if (!mac)
4015 return -EFAULT;
4016
d96b801f 4017 brcmf_dbg(TRACE, "Enter %pM\n", mac);
1a873342 4018
a0f07959
HM
4019 if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
4020 ifp = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
ce81e317 4021 if (!check_vif_up(ifp->vif))
1a873342
HM
4022 return -EIO;
4023
4024 memcpy(&scbval.ea, mac, ETH_ALEN);
4025 scbval.val = cpu_to_le32(WLAN_REASON_DEAUTH_LEAVING);
0abb5f21 4026 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCB_DEAUTHENTICATE_FOR_REASON,
81f5dcb8 4027 &scbval, sizeof(scbval));
1a873342 4028 if (err)
57d6e91a 4029 brcmf_err("SCB_DEAUTHENTICATE_FOR_REASON failed %d\n", err);
7ab6acd0 4030
d96b801f 4031 brcmf_dbg(TRACE, "Exit\n");
1a873342
HM
4032 return err;
4033}
4034
0de8aace
HM
4035
4036static void
4037brcmf_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
4038 struct wireless_dev *wdev,
4039 u16 frame_type, bool reg)
4040{
7fa2e352 4041 struct brcmf_cfg80211_vif *vif;
0de8aace
HM
4042 u16 mgmt_type;
4043
4044 brcmf_dbg(TRACE, "Enter, frame_type %04x, reg=%d\n", frame_type, reg);
4045
4046 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
7fa2e352 4047 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
0de8aace
HM
4048 if (reg)
4049 vif->mgmt_rx_reg |= BIT(mgmt_type);
4050 else
318a64ce 4051 vif->mgmt_rx_reg &= ~BIT(mgmt_type);
0de8aace
HM
4052}
4053
4054
4055static int
4056brcmf_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
b176e629 4057 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
0de8aace
HM
4058{
4059 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
b176e629
AO
4060 struct ieee80211_channel *chan = params->chan;
4061 const u8 *buf = params->buf;
4062 size_t len = params->len;
0de8aace
HM
4063 const struct ieee80211_mgmt *mgmt;
4064 struct brcmf_cfg80211_vif *vif;
4065 s32 err = 0;
4066 s32 ie_offset;
4067 s32 ie_len;
18e2f61d
HM
4068 struct brcmf_fil_action_frame_le *action_frame;
4069 struct brcmf_fil_af_params_le *af_params;
4070 bool ack;
4071 s32 chan_nr;
c2ff8cad 4072 u32 freq;
0de8aace
HM
4073
4074 brcmf_dbg(TRACE, "Enter\n");
4075
4076 *cookie = 0;
4077
4078 mgmt = (const struct ieee80211_mgmt *)buf;
4079
a0f07959
HM
4080 if (!ieee80211_is_mgmt(mgmt->frame_control)) {
4081 brcmf_err("Driver only allows MGMT packet type\n");
4082 return -EPERM;
4083 }
0de8aace 4084
c2ff8cad
AQ
4085 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4086
a0f07959
HM
4087 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
4088 /* Right now the only reason to get a probe response */
4089 /* is for p2p listen response or for p2p GO from */
4090 /* wpa_supplicant. Unfortunately the probe is send */
4091 /* on primary ndev, while dongle wants it on the p2p */
4092 /* vif. Since this is only reason for a probe */
4093 /* response to be sent, the vif is taken from cfg. */
4094 /* If ever desired to send proberesp for non p2p */
4095 /* response then data should be checked for */
4096 /* "DIRECT-". Note in future supplicant will take */
4097 /* dedicated p2p wdev to do this and then this 'hack'*/
4098 /* is not needed anymore. */
4099 ie_offset = DOT11_MGMT_HDR_LEN +
4100 DOT11_BCN_PRB_FIXED_LEN;
4101 ie_len = len - ie_offset;
a0f07959 4102 if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif)
0de8aace 4103 vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
a0f07959
HM
4104 err = brcmf_vif_set_mgmt_ie(vif,
4105 BRCMF_VNDR_IE_PRBRSP_FLAG,
4106 &buf[ie_offset],
4107 ie_len);
4108 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
4109 GFP_KERNEL);
18e2f61d
HM
4110 } else if (ieee80211_is_action(mgmt->frame_control)) {
4111 af_params = kzalloc(sizeof(*af_params), GFP_KERNEL);
4112 if (af_params == NULL) {
4113 brcmf_err("unable to allocate frame\n");
4114 err = -ENOMEM;
4115 goto exit;
4116 }
4117 action_frame = &af_params->action_frame;
4118 /* Add the packet Id */
4119 action_frame->packet_id = cpu_to_le32(*cookie);
4120 /* Add BSSID */
4121 memcpy(&action_frame->da[0], &mgmt->da[0], ETH_ALEN);
4122 memcpy(&af_params->bssid[0], &mgmt->bssid[0], ETH_ALEN);
4123 /* Add the length exepted for 802.11 header */
4124 action_frame->len = cpu_to_le16(len - DOT11_MGMT_HDR_LEN);
c2ff8cad
AQ
4125 /* Add the channel. Use the one specified as parameter if any or
4126 * the current one (got from the firmware) otherwise
4127 */
4128 if (chan)
4129 freq = chan->center_freq;
4130 else
4131 brcmf_fil_cmd_int_get(vif->ifp, BRCMF_C_GET_CHANNEL,
4132 &freq);
4133 chan_nr = ieee80211_frequency_to_channel(freq);
18e2f61d
HM
4134 af_params->channel = cpu_to_le32(chan_nr);
4135
4136 memcpy(action_frame->data, &buf[DOT11_MGMT_HDR_LEN],
4137 le16_to_cpu(action_frame->len));
4138
4139 brcmf_dbg(TRACE, "Action frame, cookie=%lld, len=%d, freq=%d\n",
86a9c4a2 4140 *cookie, le16_to_cpu(action_frame->len), freq);
18e2f61d 4141
7fa2e352 4142 ack = brcmf_p2p_send_action_frame(cfg, cfg_to_ndev(cfg),
18e2f61d
HM
4143 af_params);
4144
4145 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, ack,
4146 GFP_KERNEL);
4147 kfree(af_params);
a0f07959
HM
4148 } else {
4149 brcmf_dbg(TRACE, "Unhandled, fc=%04x!!\n", mgmt->frame_control);
4150 brcmf_dbg_hex_dump(true, buf, len, "payload, len=%Zu\n", len);
0de8aace 4151 }
a0f07959 4152
18e2f61d 4153exit:
0de8aace
HM
4154 return err;
4155}
4156
4157
4158static int
4159brcmf_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
4160 struct wireless_dev *wdev,
4161 u64 cookie)
4162{
4163 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4164 struct brcmf_cfg80211_vif *vif;
4165 int err = 0;
4166
4167 brcmf_dbg(TRACE, "Enter p2p listen cancel\n");
4168
4169 vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
4170 if (vif == NULL) {
4171 brcmf_err("No p2p device available for probe response\n");
4172 err = -ENODEV;
4173 goto exit;
4174 }
4175 brcmf_p2p_cancel_remain_on_channel(vif->ifp);
4176exit:
4177 return err;
4178}
4179
61730d4d
PH
4180static int brcmf_cfg80211_crit_proto_start(struct wiphy *wiphy,
4181 struct wireless_dev *wdev,
4182 enum nl80211_crit_proto_id proto,
4183 u16 duration)
4184{
4185 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4186 struct brcmf_cfg80211_vif *vif;
4187
4188 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4189
4190 /* only DHCP support for now */
4191 if (proto != NL80211_CRIT_PROTO_DHCP)
4192 return -EINVAL;
4193
4194 /* suppress and abort scanning */
4195 set_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
4196 brcmf_abort_scanning(cfg);
4197
4198 return brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_DISABLED, duration);
4199}
4200
4201static void brcmf_cfg80211_crit_proto_stop(struct wiphy *wiphy,
4202 struct wireless_dev *wdev)
4203{
4204 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4205 struct brcmf_cfg80211_vif *vif;
4206
4207 vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4208
4209 brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
4210 clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
4211}
4212
70b7d94b
HM
4213static s32
4214brcmf_notify_tdls_peer_event(struct brcmf_if *ifp,
4215 const struct brcmf_event_msg *e, void *data)
4216{
4217 switch (e->reason) {
4218 case BRCMF_E_REASON_TDLS_PEER_DISCOVERED:
4219 brcmf_dbg(TRACE, "TDLS Peer Discovered\n");
4220 break;
4221 case BRCMF_E_REASON_TDLS_PEER_CONNECTED:
4222 brcmf_dbg(TRACE, "TDLS Peer Connected\n");
4223 brcmf_proto_add_tdls_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
4224 break;
4225 case BRCMF_E_REASON_TDLS_PEER_DISCONNECTED:
4226 brcmf_dbg(TRACE, "TDLS Peer Disconnected\n");
4227 brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
4228 break;
4229 }
4230
4231 return 0;
4232}
4233
89c2f382
AS
4234static int brcmf_convert_nl80211_tdls_oper(enum nl80211_tdls_operation oper)
4235{
4236 int ret;
4237
4238 switch (oper) {
4239 case NL80211_TDLS_DISCOVERY_REQ:
4240 ret = BRCMF_TDLS_MANUAL_EP_DISCOVERY;
4241 break;
4242 case NL80211_TDLS_SETUP:
4243 ret = BRCMF_TDLS_MANUAL_EP_CREATE;
4244 break;
4245 case NL80211_TDLS_TEARDOWN:
4246 ret = BRCMF_TDLS_MANUAL_EP_DELETE;
4247 break;
4248 default:
4249 brcmf_err("unsupported operation: %d\n", oper);
4250 ret = -EOPNOTSUPP;
4251 }
4252 return ret;
4253}
4254
4255static int brcmf_cfg80211_tdls_oper(struct wiphy *wiphy,
3b3a0162 4256 struct net_device *ndev, const u8 *peer,
89c2f382
AS
4257 enum nl80211_tdls_operation oper)
4258{
4259 struct brcmf_if *ifp;
4260 struct brcmf_tdls_iovar_le info;
4261 int ret = 0;
4262
4263 ret = brcmf_convert_nl80211_tdls_oper(oper);
4264 if (ret < 0)
4265 return ret;
4266
4267 ifp = netdev_priv(ndev);
4268 memset(&info, 0, sizeof(info));
4269 info.mode = (u8)ret;
4270 if (peer)
4271 memcpy(info.ea, peer, ETH_ALEN);
4272
4273 ret = brcmf_fil_iovar_data_set(ifp, "tdls_endpoint",
4274 &info, sizeof(info));
4275 if (ret < 0)
4276 brcmf_err("tdls_endpoint iovar failed: ret=%d\n", ret);
4277
4278 return ret;
4279}
4280
5b435de0 4281static struct cfg80211_ops wl_cfg80211_ops = {
9f440b7b
AS
4282 .add_virtual_intf = brcmf_cfg80211_add_iface,
4283 .del_virtual_intf = brcmf_cfg80211_del_iface,
5b435de0
AS
4284 .change_virtual_intf = brcmf_cfg80211_change_iface,
4285 .scan = brcmf_cfg80211_scan,
4286 .set_wiphy_params = brcmf_cfg80211_set_wiphy_params,
4287 .join_ibss = brcmf_cfg80211_join_ibss,
4288 .leave_ibss = brcmf_cfg80211_leave_ibss,
4289 .get_station = brcmf_cfg80211_get_station,
4290 .set_tx_power = brcmf_cfg80211_set_tx_power,
4291 .get_tx_power = brcmf_cfg80211_get_tx_power,
4292 .add_key = brcmf_cfg80211_add_key,
4293 .del_key = brcmf_cfg80211_del_key,
4294 .get_key = brcmf_cfg80211_get_key,
4295 .set_default_key = brcmf_cfg80211_config_default_key,
4296 .set_default_mgmt_key = brcmf_cfg80211_config_default_mgmt_key,
4297 .set_power_mgmt = brcmf_cfg80211_set_power_mgmt,
5b435de0
AS
4298 .connect = brcmf_cfg80211_connect,
4299 .disconnect = brcmf_cfg80211_disconnect,
4300 .suspend = brcmf_cfg80211_suspend,
4301 .resume = brcmf_cfg80211_resume,
4302 .set_pmksa = brcmf_cfg80211_set_pmksa,
4303 .del_pmksa = brcmf_cfg80211_del_pmksa,
cbaa177d 4304 .flush_pmksa = brcmf_cfg80211_flush_pmksa,
1a873342
HM
4305 .start_ap = brcmf_cfg80211_start_ap,
4306 .stop_ap = brcmf_cfg80211_stop_ap,
a0f07959 4307 .change_beacon = brcmf_cfg80211_change_beacon,
1a873342 4308 .del_station = brcmf_cfg80211_del_station,
e5806072
AS
4309 .sched_scan_start = brcmf_cfg80211_sched_scan_start,
4310 .sched_scan_stop = brcmf_cfg80211_sched_scan_stop,
0de8aace
HM
4311 .mgmt_frame_register = brcmf_cfg80211_mgmt_frame_register,
4312 .mgmt_tx = brcmf_cfg80211_mgmt_tx,
4313 .remain_on_channel = brcmf_p2p_remain_on_channel,
4314 .cancel_remain_on_channel = brcmf_cfg80211_cancel_remain_on_channel,
27f10e38
AS
4315 .start_p2p_device = brcmf_p2p_start_device,
4316 .stop_p2p_device = brcmf_p2p_stop_device,
61730d4d
PH
4317 .crit_proto_start = brcmf_cfg80211_crit_proto_start,
4318 .crit_proto_stop = brcmf_cfg80211_crit_proto_stop,
89c2f382 4319 .tdls_oper = brcmf_cfg80211_tdls_oper,
5b435de0
AS
4320};
4321
3eacf866 4322struct brcmf_cfg80211_vif *brcmf_alloc_vif(struct brcmf_cfg80211_info *cfg,
9f440b7b
AS
4323 enum nl80211_iftype type,
4324 bool pm_block)
3eacf866
AS
4325{
4326 struct brcmf_cfg80211_vif *vif;
5b435de0 4327
33a6b157 4328 brcmf_dbg(TRACE, "allocating virtual interface (size=%zu)\n",
9f440b7b 4329 sizeof(*vif));
3eacf866
AS
4330 vif = kzalloc(sizeof(*vif), GFP_KERNEL);
4331 if (!vif)
4332 return ERR_PTR(-ENOMEM);
4333
4334 vif->wdev.wiphy = cfg->wiphy;
9f440b7b 4335 vif->wdev.iftype = type;
5b435de0 4336
3eacf866
AS
4337 vif->pm_block = pm_block;
4338 vif->roam_off = -1;
4339
6ac4f4ed
AS
4340 brcmf_init_prof(&vif->profile);
4341
3eacf866 4342 list_add_tail(&vif->list, &cfg->vif_list);
3eacf866 4343 return vif;
5b435de0
AS
4344}
4345
427dec5f 4346void brcmf_free_vif(struct brcmf_cfg80211_vif *vif)
5b435de0 4347{
3eacf866 4348 list_del(&vif->list);
3eacf866 4349 kfree(vif);
5b435de0
AS
4350}
4351
9df4d542
AS
4352void brcmf_cfg80211_free_netdev(struct net_device *ndev)
4353{
4354 struct brcmf_cfg80211_vif *vif;
4355 struct brcmf_if *ifp;
4356
4357 ifp = netdev_priv(ndev);
4358 vif = ifp->vif;
4359
4360 brcmf_free_vif(vif);
4361 free_netdev(ndev);
4362}
4363
903e0eee 4364static bool brcmf_is_linkup(const struct brcmf_event_msg *e)
5b435de0 4365{
5c36b99a
AS
4366 u32 event = e->event_code;
4367 u32 status = e->status;
5b435de0
AS
4368
4369 if (event == BRCMF_E_SET_SSID && status == BRCMF_E_STATUS_SUCCESS) {
16886735 4370 brcmf_dbg(CONN, "Processing set ssid\n");
5b435de0
AS
4371 return true;
4372 }
4373
4374 return false;
4375}
4376
903e0eee 4377static bool brcmf_is_linkdown(const struct brcmf_event_msg *e)
5b435de0 4378{
5c36b99a
AS
4379 u32 event = e->event_code;
4380 u16 flags = e->flags;
5b435de0 4381
68ca395f
HM
4382 if ((event == BRCMF_E_DEAUTH) || (event == BRCMF_E_DEAUTH_IND) ||
4383 (event == BRCMF_E_DISASSOC_IND) ||
4384 ((event == BRCMF_E_LINK) && (!(flags & BRCMF_EVENT_MSG_LINK)))) {
16886735 4385 brcmf_dbg(CONN, "Processing link down\n");
5b435de0
AS
4386 return true;
4387 }
4388 return false;
4389}
4390
27a68fe3 4391static bool brcmf_is_nonetwork(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
4392 const struct brcmf_event_msg *e)
4393{
5c36b99a
AS
4394 u32 event = e->event_code;
4395 u32 status = e->status;
5b435de0
AS
4396
4397 if (event == BRCMF_E_LINK && status == BRCMF_E_STATUS_NO_NETWORKS) {
16886735
AS
4398 brcmf_dbg(CONN, "Processing Link %s & no network found\n",
4399 e->flags & BRCMF_EVENT_MSG_LINK ? "up" : "down");
5b435de0
AS
4400 return true;
4401 }
4402
4403 if (event == BRCMF_E_SET_SSID && status != BRCMF_E_STATUS_SUCCESS) {
16886735 4404 brcmf_dbg(CONN, "Processing connecting & no network found\n");
5b435de0
AS
4405 return true;
4406 }
4407
4408 return false;
4409}
4410
27a68fe3 4411static void brcmf_clear_assoc_ies(struct brcmf_cfg80211_info *cfg)
5b435de0 4412{
27a68fe3 4413 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5b435de0
AS
4414
4415 kfree(conn_info->req_ie);
4416 conn_info->req_ie = NULL;
4417 conn_info->req_ie_len = 0;
4418 kfree(conn_info->resp_ie);
4419 conn_info->resp_ie = NULL;
4420 conn_info->resp_ie_len = 0;
4421}
4422
89286dc9
HM
4423static s32 brcmf_get_assoc_ies(struct brcmf_cfg80211_info *cfg,
4424 struct brcmf_if *ifp)
5b435de0 4425{
c4e382d2 4426 struct brcmf_cfg80211_assoc_ielen_le *assoc_info;
27a68fe3 4427 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5b435de0
AS
4428 u32 req_len;
4429 u32 resp_len;
4430 s32 err = 0;
4431
27a68fe3 4432 brcmf_clear_assoc_ies(cfg);
5b435de0 4433
ac24be6f
AS
4434 err = brcmf_fil_iovar_data_get(ifp, "assoc_info",
4435 cfg->extra_buf, WL_ASSOC_INFO_MAX);
5b435de0 4436 if (err) {
57d6e91a 4437 brcmf_err("could not get assoc info (%d)\n", err);
5b435de0
AS
4438 return err;
4439 }
c4e382d2 4440 assoc_info =
27a68fe3 4441 (struct brcmf_cfg80211_assoc_ielen_le *)cfg->extra_buf;
c4e382d2
AS
4442 req_len = le32_to_cpu(assoc_info->req_len);
4443 resp_len = le32_to_cpu(assoc_info->resp_len);
5b435de0 4444 if (req_len) {
ac24be6f 4445 err = brcmf_fil_iovar_data_get(ifp, "assoc_req_ies",
81f5dcb8
HM
4446 cfg->extra_buf,
4447 WL_ASSOC_INFO_MAX);
5b435de0 4448 if (err) {
57d6e91a 4449 brcmf_err("could not get assoc req (%d)\n", err);
5b435de0
AS
4450 return err;
4451 }
4452 conn_info->req_ie_len = req_len;
4453 conn_info->req_ie =
27a68fe3 4454 kmemdup(cfg->extra_buf, conn_info->req_ie_len,
5b435de0
AS
4455 GFP_KERNEL);
4456 } else {
4457 conn_info->req_ie_len = 0;
4458 conn_info->req_ie = NULL;
4459 }
4460 if (resp_len) {
ac24be6f 4461 err = brcmf_fil_iovar_data_get(ifp, "assoc_resp_ies",
81f5dcb8
HM
4462 cfg->extra_buf,
4463 WL_ASSOC_INFO_MAX);
5b435de0 4464 if (err) {
57d6e91a 4465 brcmf_err("could not get assoc resp (%d)\n", err);
5b435de0
AS
4466 return err;
4467 }
4468 conn_info->resp_ie_len = resp_len;
4469 conn_info->resp_ie =
27a68fe3 4470 kmemdup(cfg->extra_buf, conn_info->resp_ie_len,
5b435de0
AS
4471 GFP_KERNEL);
4472 } else {
4473 conn_info->resp_ie_len = 0;
4474 conn_info->resp_ie = NULL;
4475 }
16886735
AS
4476 brcmf_dbg(CONN, "req len (%d) resp len (%d)\n",
4477 conn_info->req_ie_len, conn_info->resp_ie_len);
5b435de0
AS
4478
4479 return err;
4480}
4481
4482static s32
27a68fe3 4483brcmf_bss_roaming_done(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
4484 struct net_device *ndev,
4485 const struct brcmf_event_msg *e)
4486{
c1179033
AS
4487 struct brcmf_if *ifp = netdev_priv(ndev);
4488 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
27a68fe3
AS
4489 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
4490 struct wiphy *wiphy = cfg_to_wiphy(cfg);
a180b83b 4491 struct ieee80211_channel *notify_channel = NULL;
5b435de0 4492 struct ieee80211_supported_band *band;
a180b83b 4493 struct brcmf_bss_info_le *bi;
83cf17aa 4494 struct brcmu_chan ch;
5b435de0
AS
4495 u32 freq;
4496 s32 err = 0;
a180b83b 4497 u8 *buf;
5b435de0 4498
d96b801f 4499 brcmf_dbg(TRACE, "Enter\n");
5b435de0 4500
89286dc9 4501 brcmf_get_assoc_ies(cfg, ifp);
6c8c4f72 4502 memcpy(profile->bssid, e->addr, ETH_ALEN);
89286dc9 4503 brcmf_update_bss_info(cfg, ifp);
5b435de0 4504
a180b83b
FL
4505 buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
4506 if (buf == NULL) {
4507 err = -ENOMEM;
4508 goto done;
4509 }
4510
4511 /* data sent to dongle has to be little endian */
4512 *(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
c1179033 4513 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
ac24be6f 4514 buf, WL_BSS_INFO_MAX);
a180b83b
FL
4515
4516 if (err)
4517 goto done;
5b435de0 4518
a180b83b 4519 bi = (struct brcmf_bss_info_le *)(buf + 4);
83cf17aa
FL
4520 ch.chspec = le16_to_cpu(bi->chanspec);
4521 cfg->d11inf.decchspec(&ch);
5b435de0 4522
83cf17aa 4523 if (ch.band == BRCMU_CHAN_BAND_2G)
5b435de0
AS
4524 band = wiphy->bands[IEEE80211_BAND_2GHZ];
4525 else
4526 band = wiphy->bands[IEEE80211_BAND_5GHZ];
4527
83cf17aa 4528 freq = ieee80211_channel_to_frequency(ch.chnum, band->band);
5b435de0
AS
4529 notify_channel = ieee80211_get_channel(wiphy, freq);
4530
a180b83b
FL
4531done:
4532 kfree(buf);
06bb123e 4533 cfg80211_roamed(ndev, notify_channel, (u8 *)profile->bssid,
5b435de0
AS
4534 conn_info->req_ie, conn_info->req_ie_len,
4535 conn_info->resp_ie, conn_info->resp_ie_len, GFP_KERNEL);
16886735 4536 brcmf_dbg(CONN, "Report roaming result\n");
5b435de0 4537
c1179033 4538 set_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
d96b801f 4539 brcmf_dbg(TRACE, "Exit\n");
5b435de0
AS
4540 return err;
4541}
4542
4543static s32
27a68fe3 4544brcmf_bss_connect_done(struct brcmf_cfg80211_info *cfg,
5b435de0
AS
4545 struct net_device *ndev, const struct brcmf_event_msg *e,
4546 bool completed)
4547{
c1179033
AS
4548 struct brcmf_if *ifp = netdev_priv(ndev);
4549 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
27a68fe3 4550 struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5b435de0 4551
d96b801f 4552 brcmf_dbg(TRACE, "Enter\n");
5b435de0 4553
c1179033
AS
4554 if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4555 &ifp->vif->sme_state)) {
5b435de0 4556 if (completed) {
89286dc9 4557 brcmf_get_assoc_ies(cfg, ifp);
6c8c4f72 4558 memcpy(profile->bssid, e->addr, ETH_ALEN);
89286dc9
HM
4559 brcmf_update_bss_info(cfg, ifp);
4560 set_bit(BRCMF_VIF_STATUS_CONNECTED,
4561 &ifp->vif->sme_state);
5b435de0
AS
4562 }
4563 cfg80211_connect_result(ndev,
06bb123e 4564 (u8 *)profile->bssid,
5b435de0
AS
4565 conn_info->req_ie,
4566 conn_info->req_ie_len,
4567 conn_info->resp_ie,
4568 conn_info->resp_ie_len,
4569 completed ? WLAN_STATUS_SUCCESS :
4570 WLAN_STATUS_AUTH_TIMEOUT,
4571 GFP_KERNEL);
16886735
AS
4572 brcmf_dbg(CONN, "Report connect result - connection %s\n",
4573 completed ? "succeeded" : "failed");
5b435de0 4574 }
d96b801f 4575 brcmf_dbg(TRACE, "Exit\n");
12f32370 4576 return 0;
5b435de0
AS
4577}
4578
4579static s32
27a68fe3 4580brcmf_notify_connect_status_ap(struct brcmf_cfg80211_info *cfg,
1a873342
HM
4581 struct net_device *ndev,
4582 const struct brcmf_event_msg *e, void *data)
4583{
7ee29602 4584 static int generation;
5c36b99a
AS
4585 u32 event = e->event_code;
4586 u32 reason = e->reason;
1a873342
HM
4587 struct station_info sinfo;
4588
16886735 4589 brcmf_dbg(CONN, "event %d, reason %d\n", event, reason);
5f4f9f11
AS
4590 if (event == BRCMF_E_LINK && reason == BRCMF_E_REASON_LINK_BSSCFG_DIS &&
4591 ndev != cfg_to_ndev(cfg)) {
4592 brcmf_dbg(CONN, "AP mode link down\n");
4593 complete(&cfg->vif_disabled);
4594 return 0;
4595 }
1a873342 4596
1a873342 4597 if (((event == BRCMF_E_ASSOC_IND) || (event == BRCMF_E_REASSOC_IND)) &&
7ee29602
HM
4598 (reason == BRCMF_E_STATUS_SUCCESS)) {
4599 memset(&sinfo, 0, sizeof(sinfo));
1a873342
HM
4600 sinfo.filled = STATION_INFO_ASSOC_REQ_IES;
4601 if (!data) {
57d6e91a 4602 brcmf_err("No IEs present in ASSOC/REASSOC_IND");
1a873342
HM
4603 return -EINVAL;
4604 }
4605 sinfo.assoc_req_ies = data;
7ee29602 4606 sinfo.assoc_req_ies_len = e->datalen;
1a873342
HM
4607 generation++;
4608 sinfo.generation = generation;
7ee29602 4609 cfg80211_new_sta(ndev, e->addr, &sinfo, GFP_KERNEL);
1a873342
HM
4610 } else if ((event == BRCMF_E_DISASSOC_IND) ||
4611 (event == BRCMF_E_DEAUTH_IND) ||
4612 (event == BRCMF_E_DEAUTH)) {
7ee29602 4613 cfg80211_del_sta(ndev, e->addr, GFP_KERNEL);
1a873342 4614 }
7ee29602 4615 return 0;
1a873342
HM
4616}
4617
5b435de0 4618static s32
1993732e 4619brcmf_notify_connect_status(struct brcmf_if *ifp,
5b435de0
AS
4620 const struct brcmf_event_msg *e, void *data)
4621{
1993732e
AS
4622 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
4623 struct net_device *ndev = ifp->ndev;
c1179033 4624 struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
fe94f3a4 4625 struct ieee80211_channel *chan;
5b435de0
AS
4626 s32 err = 0;
4627
8851cce0
HM
4628 if ((e->event_code == BRCMF_E_DEAUTH) ||
4629 (e->event_code == BRCMF_E_DEAUTH_IND) ||
4630 (e->event_code == BRCMF_E_DISASSOC_IND) ||
4631 ((e->event_code == BRCMF_E_LINK) && (!e->flags))) {
4632 brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
4633 }
4634
967fe2c8 4635 if (brcmf_is_apmode(ifp->vif)) {
27a68fe3 4636 err = brcmf_notify_connect_status_ap(cfg, ndev, e, data);
903e0eee 4637 } else if (brcmf_is_linkup(e)) {
16886735 4638 brcmf_dbg(CONN, "Linkup\n");
128ce3b6 4639 if (brcmf_is_ibssmode(ifp->vif)) {
fe94f3a4 4640 chan = ieee80211_get_channel(cfg->wiphy, cfg->channel);
6c8c4f72 4641 memcpy(profile->bssid, e->addr, ETH_ALEN);
27a68fe3 4642 wl_inform_ibss(cfg, ndev, e->addr);
fe94f3a4 4643 cfg80211_ibss_joined(ndev, e->addr, chan, GFP_KERNEL);
c1179033
AS
4644 clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4645 &ifp->vif->sme_state);
4646 set_bit(BRCMF_VIF_STATUS_CONNECTED,
4647 &ifp->vif->sme_state);
5b435de0 4648 } else
27a68fe3 4649 brcmf_bss_connect_done(cfg, ndev, e, true);
903e0eee 4650 } else if (brcmf_is_linkdown(e)) {
16886735 4651 brcmf_dbg(CONN, "Linkdown\n");
128ce3b6 4652 if (!brcmf_is_ibssmode(ifp->vif)) {
27a68fe3 4653 brcmf_bss_connect_done(cfg, ndev, e, false);
5b435de0 4654 }
903e0eee 4655 brcmf_link_down(ifp->vif);
6ac4f4ed 4656 brcmf_init_prof(ndev_to_prof(ndev));
5f4f9f11
AS
4657 if (ndev != cfg_to_ndev(cfg))
4658 complete(&cfg->vif_disabled);
27a68fe3 4659 } else if (brcmf_is_nonetwork(cfg, e)) {
128ce3b6 4660 if (brcmf_is_ibssmode(ifp->vif))
c1179033
AS
4661 clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4662 &ifp->vif->sme_state);
5b435de0 4663 else
27a68fe3 4664 brcmf_bss_connect_done(cfg, ndev, e, false);
5b435de0
AS
4665 }
4666
4667 return err;
4668}
4669
4670static s32
1993732e 4671brcmf_notify_roaming_status(struct brcmf_if *ifp,
5b435de0
AS
4672 const struct brcmf_event_msg *e, void *data)
4673{
1993732e 4674 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5c36b99a
AS
4675 u32 event = e->event_code;
4676 u32 status = e->status;
5b435de0
AS
4677
4678 if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) {
c1179033 4679 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state))
1993732e 4680 brcmf_bss_roaming_done(cfg, ifp->ndev, e);
5b435de0 4681 else
1993732e 4682 brcmf_bss_connect_done(cfg, ifp->ndev, e, true);
5b435de0
AS
4683 }
4684
12f32370 4685 return 0;
5b435de0
AS
4686}
4687
4688static s32
1993732e 4689brcmf_notify_mic_status(struct brcmf_if *ifp,
5b435de0
AS
4690 const struct brcmf_event_msg *e, void *data)
4691{
5c36b99a 4692 u16 flags = e->flags;
5b435de0
AS
4693 enum nl80211_key_type key_type;
4694
4695 if (flags & BRCMF_EVENT_MSG_GROUP)
4696 key_type = NL80211_KEYTYPE_GROUP;
4697 else
4698 key_type = NL80211_KEYTYPE_PAIRWISE;
4699
1993732e 4700 cfg80211_michael_mic_failure(ifp->ndev, (u8 *)&e->addr, key_type, -1,
5b435de0
AS
4701 NULL, GFP_KERNEL);
4702
4703 return 0;
4704}
4705
d3c0b633
AS
4706static s32 brcmf_notify_vif_event(struct brcmf_if *ifp,
4707 const struct brcmf_event_msg *e, void *data)
4708{
4709 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
4710 struct brcmf_if_event *ifevent = (struct brcmf_if_event *)data;
4711 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
4712 struct brcmf_cfg80211_vif *vif;
4713
4714 brcmf_dbg(TRACE, "Enter: action %u flags %u ifidx %u bsscfg %u\n",
4715 ifevent->action, ifevent->flags, ifevent->ifidx,
4716 ifevent->bssidx);
4717
d3c0b633
AS
4718 mutex_lock(&event->vif_event_lock);
4719 event->action = ifevent->action;
4720 vif = event->vif;
4721
4722 switch (ifevent->action) {
4723 case BRCMF_E_IF_ADD:
4724 /* waiting process may have timed out */
dc4a787c
WY
4725 if (!cfg->vif_event.vif) {
4726 mutex_unlock(&event->vif_event_lock);
d3c0b633 4727 return -EBADF;
dc4a787c 4728 }
d3c0b633
AS
4729
4730 ifp->vif = vif;
4731 vif->ifp = ifp;
01b8e7db
AS
4732 if (ifp->ndev) {
4733 vif->wdev.netdev = ifp->ndev;
4734 ifp->ndev->ieee80211_ptr = &vif->wdev;
4735 SET_NETDEV_DEV(ifp->ndev, wiphy_dev(cfg->wiphy));
4736 }
d3c0b633
AS
4737 mutex_unlock(&event->vif_event_lock);
4738 wake_up(&event->vif_wq);
4b3a89de 4739 return 0;
d3c0b633
AS
4740
4741 case BRCMF_E_IF_DEL:
d3c0b633
AS
4742 mutex_unlock(&event->vif_event_lock);
4743 /* event may not be upon user request */
4744 if (brcmf_cfg80211_vif_event_armed(cfg))
4745 wake_up(&event->vif_wq);
4746 return 0;
4747
7a5c1f64
HM
4748 case BRCMF_E_IF_CHANGE:
4749 mutex_unlock(&event->vif_event_lock);
4750 wake_up(&event->vif_wq);
4751 return 0;
4752
d3c0b633
AS
4753 default:
4754 mutex_unlock(&event->vif_event_lock);
4755 break;
4756 }
4757 return -EINVAL;
4758}
4759
5b435de0
AS
4760static void brcmf_init_conf(struct brcmf_cfg80211_conf *conf)
4761{
5b435de0
AS
4762 conf->frag_threshold = (u32)-1;
4763 conf->rts_threshold = (u32)-1;
4764 conf->retry_short = (u32)-1;
4765 conf->retry_long = (u32)-1;
4766 conf->tx_power = -1;
4767}
4768
5c36b99a 4769static void brcmf_register_event_handlers(struct brcmf_cfg80211_info *cfg)
5b435de0 4770{
5c36b99a
AS
4771 brcmf_fweh_register(cfg->pub, BRCMF_E_LINK,
4772 brcmf_notify_connect_status);
4773 brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH_IND,
4774 brcmf_notify_connect_status);
4775 brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH,
4776 brcmf_notify_connect_status);
4777 brcmf_fweh_register(cfg->pub, BRCMF_E_DISASSOC_IND,
4778 brcmf_notify_connect_status);
4779 brcmf_fweh_register(cfg->pub, BRCMF_E_ASSOC_IND,
4780 brcmf_notify_connect_status);
4781 brcmf_fweh_register(cfg->pub, BRCMF_E_REASSOC_IND,
4782 brcmf_notify_connect_status);
4783 brcmf_fweh_register(cfg->pub, BRCMF_E_ROAM,
4784 brcmf_notify_roaming_status);
4785 brcmf_fweh_register(cfg->pub, BRCMF_E_MIC_ERROR,
4786 brcmf_notify_mic_status);
4787 brcmf_fweh_register(cfg->pub, BRCMF_E_SET_SSID,
4788 brcmf_notify_connect_status);
4789 brcmf_fweh_register(cfg->pub, BRCMF_E_PFN_NET_FOUND,
4790 brcmf_notify_sched_scan_results);
d3c0b633
AS
4791 brcmf_fweh_register(cfg->pub, BRCMF_E_IF,
4792 brcmf_notify_vif_event);
0de8aace 4793 brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_PROBEREQ_MSG,
6eda4e2c 4794 brcmf_p2p_notify_rx_mgmt_p2p_probereq);
0de8aace
HM
4795 brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_DISC_LISTEN_COMPLETE,
4796 brcmf_p2p_notify_listen_complete);
e6da3400
HM
4797 brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_RX,
4798 brcmf_p2p_notify_action_frame_rx);
18e2f61d
HM
4799 brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_COMPLETE,
4800 brcmf_p2p_notify_action_tx_complete);
6eda4e2c
HM
4801 brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE,
4802 brcmf_p2p_notify_action_tx_complete);
5b435de0
AS
4803}
4804
27a68fe3
AS
4805static void brcmf_deinit_priv_mem(struct brcmf_cfg80211_info *cfg)
4806{
27a68fe3
AS
4807 kfree(cfg->conf);
4808 cfg->conf = NULL;
27a68fe3
AS
4809 kfree(cfg->escan_ioctl_buf);
4810 cfg->escan_ioctl_buf = NULL;
27a68fe3
AS
4811 kfree(cfg->extra_buf);
4812 cfg->extra_buf = NULL;
27a68fe3
AS
4813 kfree(cfg->pmk_list);
4814 cfg->pmk_list = NULL;
27a68fe3
AS
4815}
4816
4817static s32 brcmf_init_priv_mem(struct brcmf_cfg80211_info *cfg)
4818{
27a68fe3
AS
4819 cfg->conf = kzalloc(sizeof(*cfg->conf), GFP_KERNEL);
4820 if (!cfg->conf)
5b435de0 4821 goto init_priv_mem_out;
27a68fe3
AS
4822 cfg->escan_ioctl_buf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
4823 if (!cfg->escan_ioctl_buf)
e756af5b 4824 goto init_priv_mem_out;
27a68fe3
AS
4825 cfg->extra_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
4826 if (!cfg->extra_buf)
5b435de0 4827 goto init_priv_mem_out;
27a68fe3
AS
4828 cfg->pmk_list = kzalloc(sizeof(*cfg->pmk_list), GFP_KERNEL);
4829 if (!cfg->pmk_list)
5b435de0
AS
4830 goto init_priv_mem_out;
4831
4832 return 0;
4833
4834init_priv_mem_out:
27a68fe3 4835 brcmf_deinit_priv_mem(cfg);
5b435de0
AS
4836
4837 return -ENOMEM;
4838}
4839
27a68fe3 4840static s32 wl_init_priv(struct brcmf_cfg80211_info *cfg)
5b435de0
AS
4841{
4842 s32 err = 0;
4843
27a68fe3
AS
4844 cfg->scan_request = NULL;
4845 cfg->pwr_save = true;
68ca395f
HM
4846 cfg->active_scan = true; /* we do active scan per default */
4847 cfg->dongle_up = false; /* dongle is not up yet */
27a68fe3 4848 err = brcmf_init_priv_mem(cfg);
5b435de0
AS
4849 if (err)
4850 return err;
5c36b99a 4851 brcmf_register_event_handlers(cfg);
27a68fe3 4852 mutex_init(&cfg->usr_sync);
27a68fe3
AS
4853 brcmf_init_escan(cfg);
4854 brcmf_init_conf(cfg->conf);
5f4f9f11 4855 init_completion(&cfg->vif_disabled);
5b435de0
AS
4856 return err;
4857}
4858
27a68fe3 4859static void wl_deinit_priv(struct brcmf_cfg80211_info *cfg)
5b435de0 4860{
27a68fe3 4861 cfg->dongle_up = false; /* dongle down */
27a68fe3
AS
4862 brcmf_abort_scanning(cfg);
4863 brcmf_deinit_priv_mem(cfg);
5b435de0
AS
4864}
4865
d3c0b633
AS
4866static void init_vif_event(struct brcmf_cfg80211_vif_event *event)
4867{
4868 init_waitqueue_head(&event->vif_wq);
d3c0b633
AS
4869 mutex_init(&event->vif_event_lock);
4870}
4871
5b435de0 4872static s32
68ca395f 4873brcmf_dongle_roam(struct brcmf_if *ifp, u32 bcn_timeout)
5b435de0 4874{
5b435de0 4875 s32 err = 0;
f588bc0c
AS
4876 __le32 roamtrigger[2];
4877 __le32 roam_delta[2];
5b435de0
AS
4878
4879 /*
4880 * Setup timeout if Beacons are lost and roam is
4881 * off to report link down
4882 */
68ca395f 4883 if (brcmf_roamoff) {
ac24be6f 4884 err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout", bcn_timeout);
5b435de0 4885 if (err) {
57d6e91a 4886 brcmf_err("bcn_timeout error (%d)\n", err);
5b435de0
AS
4887 goto dongle_rom_out;
4888 }
4889 }
4890
4891 /*
4892 * Enable/Disable built-in roaming to allow supplicant
4893 * to take care of roaming
4894 */
68ca395f
HM
4895 brcmf_dbg(INFO, "Internal Roaming = %s\n",
4896 brcmf_roamoff ? "Off" : "On");
4897 err = brcmf_fil_iovar_int_set(ifp, "roam_off", !!(brcmf_roamoff));
5b435de0 4898 if (err) {
57d6e91a 4899 brcmf_err("roam_off error (%d)\n", err);
5b435de0
AS
4900 goto dongle_rom_out;
4901 }
4902
f588bc0c
AS
4903 roamtrigger[0] = cpu_to_le32(WL_ROAM_TRIGGER_LEVEL);
4904 roamtrigger[1] = cpu_to_le32(BRCM_BAND_ALL);
ac24be6f 4905 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_TRIGGER,
81f5dcb8 4906 (void *)roamtrigger, sizeof(roamtrigger));
5b435de0 4907 if (err) {
57d6e91a 4908 brcmf_err("WLC_SET_ROAM_TRIGGER error (%d)\n", err);
5b435de0
AS
4909 goto dongle_rom_out;
4910 }
4911
f588bc0c
AS
4912 roam_delta[0] = cpu_to_le32(WL_ROAM_DELTA);
4913 roam_delta[1] = cpu_to_le32(BRCM_BAND_ALL);
ac24be6f 4914 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_DELTA,
81f5dcb8 4915 (void *)roam_delta, sizeof(roam_delta));
5b435de0 4916 if (err) {
57d6e91a 4917 brcmf_err("WLC_SET_ROAM_DELTA error (%d)\n", err);
5b435de0
AS
4918 goto dongle_rom_out;
4919 }
4920
4921dongle_rom_out:
4922 return err;
4923}
4924
4925static s32
40a23296 4926brcmf_dongle_scantime(struct brcmf_if *ifp, s32 scan_assoc_time,
c68cdc0f 4927 s32 scan_unassoc_time, s32 scan_passive_time)
5b435de0
AS
4928{
4929 s32 err = 0;
4930
ac24be6f 4931 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
81f5dcb8 4932 scan_assoc_time);
5b435de0
AS
4933 if (err) {
4934 if (err == -EOPNOTSUPP)
647c9ae0 4935 brcmf_dbg(INFO, "Scan assoc time is not supported\n");
5b435de0 4936 else
57d6e91a 4937 brcmf_err("Scan assoc time error (%d)\n", err);
5b435de0
AS
4938 goto dongle_scantime_out;
4939 }
ac24be6f 4940 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
81f5dcb8 4941 scan_unassoc_time);
5b435de0
AS
4942 if (err) {
4943 if (err == -EOPNOTSUPP)
647c9ae0 4944 brcmf_dbg(INFO, "Scan unassoc time is not supported\n");
5b435de0 4945 else
57d6e91a 4946 brcmf_err("Scan unassoc time error (%d)\n", err);
5b435de0
AS
4947 goto dongle_scantime_out;
4948 }
4949
ac24be6f 4950 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_PASSIVE_TIME,
81f5dcb8 4951 scan_passive_time);
5b435de0
AS
4952 if (err) {
4953 if (err == -EOPNOTSUPP)
647c9ae0 4954 brcmf_dbg(INFO, "Scan passive time is not supported\n");
5b435de0 4955 else
57d6e91a 4956 brcmf_err("Scan passive time error (%d)\n", err);
5b435de0
AS
4957 goto dongle_scantime_out;
4958 }
4959
4960dongle_scantime_out:
4961 return err;
4962}
4963
b48d8916
AS
4964/* Filter the list of channels received from firmware counting only
4965 * the 20MHz channels. The wiphy band data only needs those which get
4966 * flagged to indicate if they can take part in higher bandwidth.
4967 */
4968static void brcmf_count_20mhz_channels(struct brcmf_cfg80211_info *cfg,
4969 struct brcmf_chanspec_list *chlist,
4970 u32 chcnt[])
4971{
4972 u32 total = le32_to_cpu(chlist->count);
4973 struct brcmu_chan ch;
4974 int i;
4975
f8adaf0a 4976 for (i = 0; i < total; i++) {
b48d8916
AS
4977 ch.chspec = (u16)le32_to_cpu(chlist->element[i]);
4978 cfg->d11inf.decchspec(&ch);
4979
4980 /* Firmware gives a ordered list. We skip non-20MHz
4981 * channels is 2G. For 5G we can abort upon reaching
4982 * a non-20MHz channel in the list.
4983 */
4984 if (ch.bw != BRCMU_CHAN_BW_20) {
4985 if (ch.band == BRCMU_CHAN_BAND_5G)
4986 break;
4987 else
4988 continue;
4989 }
4990
4991 if (ch.band == BRCMU_CHAN_BAND_2G)
4992 chcnt[0] += 1;
4993 else if (ch.band == BRCMU_CHAN_BAND_5G)
4994 chcnt[1] += 1;
4995 }
4996}
4997
4998static void brcmf_update_bw40_channel_flag(struct ieee80211_channel *channel,
4999 struct brcmu_chan *ch)
5000{
5001 u32 ht40_flag;
d48200ba 5002
b48d8916
AS
5003 ht40_flag = channel->flags & IEEE80211_CHAN_NO_HT40;
5004 if (ch->sb == BRCMU_CHAN_SB_U) {
5005 if (ht40_flag == IEEE80211_CHAN_NO_HT40)
5006 channel->flags &= ~IEEE80211_CHAN_NO_HT40;
5007 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
5008 } else {
5009 /* It should be one of
5010 * IEEE80211_CHAN_NO_HT40 or
5011 * IEEE80211_CHAN_NO_HT40PLUS
5012 */
5013 channel->flags &= ~IEEE80211_CHAN_NO_HT40;
5014 if (ht40_flag == IEEE80211_CHAN_NO_HT40)
5015 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
5016 }
5017}
5018
5019static int brcmf_construct_chaninfo(struct brcmf_cfg80211_info *cfg,
5020 u32 bw_cap[])
d48200ba
HM
5021{
5022 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
b48d8916
AS
5023 struct ieee80211_supported_band *band;
5024 struct ieee80211_channel *channel;
5025 struct wiphy *wiphy;
d48200ba 5026 struct brcmf_chanspec_list *list;
83cf17aa 5027 struct brcmu_chan ch;
b48d8916 5028 int err;
d48200ba
HM
5029 u8 *pbuf;
5030 u32 i, j;
5031 u32 total;
b48d8916
AS
5032 u32 chaninfo;
5033 u32 chcnt[2] = { 0, 0 };
d48200ba 5034 u32 index;
d48200ba
HM
5035
5036 pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
5037
5038 if (pbuf == NULL)
5039 return -ENOMEM;
5040
5041 list = (struct brcmf_chanspec_list *)pbuf;
5042
5043 err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
5044 BRCMF_DCMD_MEDLEN);
5045 if (err) {
5046 brcmf_err("get chanspecs error (%d)\n", err);
b48d8916 5047 goto fail_pbuf;
d48200ba
HM
5048 }
5049
b48d8916
AS
5050 brcmf_count_20mhz_channels(cfg, list, chcnt);
5051 wiphy = cfg_to_wiphy(cfg);
5052 if (chcnt[0]) {
5053 band = kmemdup(&__wl_band_2ghz, sizeof(__wl_band_2ghz),
5054 GFP_KERNEL);
5055 if (band == NULL) {
5056 err = -ENOMEM;
5057 goto fail_pbuf;
5058 }
5059 band->channels = kcalloc(chcnt[0], sizeof(*channel),
5060 GFP_KERNEL);
5061 if (band->channels == NULL) {
5062 kfree(band);
5063 err = -ENOMEM;
5064 goto fail_pbuf;
5065 }
5066 band->n_channels = 0;
5067 wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5068 }
5069 if (chcnt[1]) {
5070 band = kmemdup(&__wl_band_5ghz_a, sizeof(__wl_band_5ghz_a),
5071 GFP_KERNEL);
5072 if (band == NULL) {
5073 err = -ENOMEM;
5074 goto fail_band2g;
5075 }
5076 band->channels = kcalloc(chcnt[1], sizeof(*channel),
5077 GFP_KERNEL);
5078 if (band->channels == NULL) {
5079 kfree(band);
5080 err = -ENOMEM;
5081 goto fail_band2g;
5082 }
5083 band->n_channels = 0;
5084 wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5085 }
d48200ba
HM
5086
5087 total = le32_to_cpu(list->count);
5088 for (i = 0; i < total; i++) {
83cf17aa
FL
5089 ch.chspec = (u16)le32_to_cpu(list->element[i]);
5090 cfg->d11inf.decchspec(&ch);
d48200ba 5091
83cf17aa 5092 if (ch.band == BRCMU_CHAN_BAND_2G) {
b48d8916 5093 band = wiphy->bands[IEEE80211_BAND_2GHZ];
83cf17aa 5094 } else if (ch.band == BRCMU_CHAN_BAND_5G) {
b48d8916 5095 band = wiphy->bands[IEEE80211_BAND_5GHZ];
d48200ba 5096 } else {
2375d970 5097 brcmf_err("Invalid channel Spec. 0x%x.\n", ch.chspec);
d48200ba
HM
5098 continue;
5099 }
b48d8916 5100 if (!(bw_cap[band->band] & WLC_BW_40MHZ_BIT) &&
2375d970 5101 ch.bw == BRCMU_CHAN_BW_40)
d48200ba 5102 continue;
b48d8916 5103 if (!(bw_cap[band->band] & WLC_BW_80MHZ_BIT) &&
ee942ecc
AS
5104 ch.bw == BRCMU_CHAN_BW_80)
5105 continue;
b48d8916
AS
5106
5107 channel = band->channels;
5108 index = band->n_channels;
5109 for (j = 0; j < band->n_channels; j++) {
5110 if (channel[j].hw_value == ch.chnum) {
5111 index = j;
d48200ba
HM
5112 break;
5113 }
5114 }
b48d8916
AS
5115 channel[index].center_freq =
5116 ieee80211_channel_to_frequency(ch.chnum, band->band);
5117 channel[index].hw_value = ch.chnum;
5118
5119 /* assuming the chanspecs order is HT20,
5120 * HT40 upper, HT40 lower, and VHT80.
5121 */
5122 if (ch.bw == BRCMU_CHAN_BW_80) {
5123 channel[index].flags &= ~IEEE80211_CHAN_NO_80MHZ;
5124 } else if (ch.bw == BRCMU_CHAN_BW_40) {
5125 brcmf_update_bw40_channel_flag(&channel[index], &ch);
5126 } else {
5127 /* disable other bandwidths for now as mentioned
5128 * order assure they are enabled for subsequent
5129 * chanspecs.
ee942ecc 5130 */
b48d8916
AS
5131 channel[index].flags = IEEE80211_CHAN_NO_HT40 |
5132 IEEE80211_CHAN_NO_80MHZ;
5133 ch.bw = BRCMU_CHAN_BW_20;
5134 cfg->d11inf.encchspec(&ch);
5135 chaninfo = ch.chspec;
5136 err = brcmf_fil_bsscfg_int_get(ifp, "per_chan_info",
5137 &chaninfo);
5138 if (!err) {
5139 if (chaninfo & WL_CHAN_RADAR)
5140 channel[index].flags |=
5141 (IEEE80211_CHAN_RADAR |
5142 IEEE80211_CHAN_NO_IR);
5143 if (chaninfo & WL_CHAN_PASSIVE)
5144 channel[index].flags |=
5145 IEEE80211_CHAN_NO_IR;
d48200ba 5146 }
d48200ba 5147 }
b48d8916
AS
5148 if (index == band->n_channels)
5149 band->n_channels++;
d48200ba 5150 }
b48d8916
AS
5151 kfree(pbuf);
5152 return 0;
5153
5154fail_band2g:
5155 kfree(wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
5156 kfree(wiphy->bands[IEEE80211_BAND_2GHZ]);
5157 wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
5158fail_pbuf:
d48200ba
HM
5159 kfree(pbuf);
5160 return err;
5161}
5162
b48d8916 5163static int brcmf_enable_bw40_2g(struct brcmf_cfg80211_info *cfg)
aa70b4fa 5164{
b48d8916
AS
5165 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5166 struct ieee80211_supported_band *band;
aa70b4fa 5167 struct brcmf_fil_bwcap_le band_bwcap;
b48d8916
AS
5168 struct brcmf_chanspec_list *list;
5169 u8 *pbuf;
aa70b4fa
AS
5170 u32 val;
5171 int err;
b48d8916
AS
5172 struct brcmu_chan ch;
5173 u32 num_chan;
5174 int i, j;
aa70b4fa
AS
5175
5176 /* verify support for bw_cap command */
5177 val = WLC_BAND_5G;
5178 err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &val);
5179
5180 if (!err) {
5181 /* only set 2G bandwidth using bw_cap command */
5182 band_bwcap.band = cpu_to_le32(WLC_BAND_2G);
5183 band_bwcap.bw_cap = cpu_to_le32(WLC_BW_CAP_40MHZ);
5184 err = brcmf_fil_iovar_data_set(ifp, "bw_cap", &band_bwcap,
5185 sizeof(band_bwcap));
5186 } else {
5187 brcmf_dbg(INFO, "fallback to mimo_bw_cap\n");
5188 val = WLC_N_BW_40ALL;
5189 err = brcmf_fil_iovar_int_set(ifp, "mimo_bw_cap", val);
5190 }
b48d8916
AS
5191
5192 if (!err) {
5193 /* update channel info in 2G band */
5194 pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
5195
5196 if (pbuf == NULL)
5197 return -ENOMEM;
5198
5199 ch.band = BRCMU_CHAN_BAND_2G;
5200 ch.bw = BRCMU_CHAN_BW_40;
fac7d2a3 5201 ch.sb = BRCMU_CHAN_SB_NONE;
b48d8916
AS
5202 ch.chnum = 0;
5203 cfg->d11inf.encchspec(&ch);
5204
5205 /* pass encoded chanspec in query */
5206 *(__le16 *)pbuf = cpu_to_le16(ch.chspec);
5207
5208 err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
5209 BRCMF_DCMD_MEDLEN);
5210 if (err) {
5211 brcmf_err("get chanspecs error (%d)\n", err);
5212 kfree(pbuf);
5213 return err;
5214 }
5215
5216 band = cfg_to_wiphy(cfg)->bands[IEEE80211_BAND_2GHZ];
5217 list = (struct brcmf_chanspec_list *)pbuf;
5218 num_chan = le32_to_cpu(list->count);
5219 for (i = 0; i < num_chan; i++) {
5220 ch.chspec = (u16)le32_to_cpu(list->element[i]);
5221 cfg->d11inf.decchspec(&ch);
5222 if (WARN_ON(ch.band != BRCMU_CHAN_BAND_2G))
5223 continue;
5224 if (WARN_ON(ch.bw != BRCMU_CHAN_BW_40))
5225 continue;
5226 for (j = 0; j < band->n_channels; j++) {
5227 if (band->channels[j].hw_value == ch.chnum)
5228 break;
5229 }
5230 if (WARN_ON(j == band->n_channels))
5231 continue;
5232
5233 brcmf_update_bw40_channel_flag(&band->channels[j], &ch);
5234 }
fac7d2a3 5235 kfree(pbuf);
b48d8916 5236 }
aa70b4fa
AS
5237 return err;
5238}
5239
2375d970
AS
5240static void brcmf_get_bwcap(struct brcmf_if *ifp, u32 bw_cap[])
5241{
5242 u32 band, mimo_bwcap;
5243 int err;
5244
5245 band = WLC_BAND_2G;
5246 err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
5247 if (!err) {
5248 bw_cap[IEEE80211_BAND_2GHZ] = band;
5249 band = WLC_BAND_5G;
5250 err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
5251 if (!err) {
5252 bw_cap[IEEE80211_BAND_5GHZ] = band;
5253 return;
5254 }
5255 WARN_ON(1);
5256 return;
5257 }
5258 brcmf_dbg(INFO, "fallback to mimo_bw_cap info\n");
5259 mimo_bwcap = 0;
5260 err = brcmf_fil_iovar_int_get(ifp, "mimo_bw_cap", &mimo_bwcap);
5261 if (err)
5262 /* assume 20MHz if firmware does not give a clue */
5263 mimo_bwcap = WLC_N_BW_20ALL;
5264
5265 switch (mimo_bwcap) {
5266 case WLC_N_BW_40ALL:
5267 bw_cap[IEEE80211_BAND_2GHZ] |= WLC_BW_40MHZ_BIT;
5268 /* fall-thru */
5269 case WLC_N_BW_20IN2G_40IN5G:
5270 bw_cap[IEEE80211_BAND_5GHZ] |= WLC_BW_40MHZ_BIT;
5271 /* fall-thru */
5272 case WLC_N_BW_20ALL:
5273 bw_cap[IEEE80211_BAND_2GHZ] |= WLC_BW_20MHZ_BIT;
5274 bw_cap[IEEE80211_BAND_5GHZ] |= WLC_BW_20MHZ_BIT;
5275 break;
5276 default:
5277 brcmf_err("invalid mimo_bw_cap value\n");
5278 }
5279}
d48200ba 5280
18d6c535
AS
5281static void brcmf_update_ht_cap(struct ieee80211_supported_band *band,
5282 u32 bw_cap[2], u32 nchain)
5283{
5284 band->ht_cap.ht_supported = true;
5285 if (bw_cap[band->band] & WLC_BW_40MHZ_BIT) {
5286 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5287 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5288 }
5289 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5290 band->ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5291 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5292 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
5293 memset(band->ht_cap.mcs.rx_mask, 0xff, nchain);
5294 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
5295}
5296
5297static __le16 brcmf_get_mcs_map(u32 nchain, enum ieee80211_vht_mcs_support supp)
5298{
5299 u16 mcs_map;
5300 int i;
5301
5302 for (i = 0, mcs_map = 0xFFFF; i < nchain; i++)
5303 mcs_map = (mcs_map << 2) | supp;
5304
5305 return cpu_to_le16(mcs_map);
5306}
5307
5308static void brcmf_update_vht_cap(struct ieee80211_supported_band *band,
5309 u32 bw_cap[2], u32 nchain)
5310{
5311 __le16 mcs_map;
5312
5313 /* not allowed in 2.4G band */
5314 if (band->band == IEEE80211_BAND_2GHZ)
5315 return;
5316
5317 band->vht_cap.vht_supported = true;
5318 /* 80MHz is mandatory */
5319 band->vht_cap.cap |= IEEE80211_VHT_CAP_SHORT_GI_80;
5320 if (bw_cap[band->band] & WLC_BW_160MHZ_BIT) {
5321 band->vht_cap.cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
5322 band->vht_cap.cap |= IEEE80211_VHT_CAP_SHORT_GI_160;
5323 }
5324 /* all support 256-QAM */
5325 mcs_map = brcmf_get_mcs_map(nchain, IEEE80211_VHT_MCS_SUPPORT_0_9);
5326 band->vht_cap.vht_mcs.rx_mcs_map = mcs_map;
5327 band->vht_cap.vht_mcs.tx_mcs_map = mcs_map;
5328}
5329
b48d8916 5330static int brcmf_setup_wiphybands(struct wiphy *wiphy)
5b435de0 5331{
b48d8916 5332 struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
ac24be6f 5333 struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
18d6c535
AS
5334 u32 nmode = 0;
5335 u32 vhtmode = 0;
b48d8916 5336 u32 bw_cap[2] = { WLC_BW_20MHZ_BIT, WLC_BW_20MHZ_BIT };
4aca7a18
DK
5337 u32 rxchain;
5338 u32 nchain;
b48d8916 5339 int err;
d48200ba 5340 s32 i;
2375d970 5341 struct ieee80211_supported_band *band;
5b435de0 5342
18d6c535 5343 (void)brcmf_fil_iovar_int_get(ifp, "vhtmode", &vhtmode);
d48200ba
HM
5344 err = brcmf_fil_iovar_int_get(ifp, "nmode", &nmode);
5345 if (err) {
5346 brcmf_err("nmode error (%d)\n", err);
5347 } else {
2375d970 5348 brcmf_get_bwcap(ifp, bw_cap);
d48200ba 5349 }
18d6c535
AS
5350 brcmf_dbg(INFO, "nmode=%d, vhtmode=%d, bw_cap=(%d, %d)\n",
5351 nmode, vhtmode, bw_cap[IEEE80211_BAND_2GHZ],
5352 bw_cap[IEEE80211_BAND_5GHZ]);
d48200ba 5353
4aca7a18
DK
5354 err = brcmf_fil_iovar_int_get(ifp, "rxchain", &rxchain);
5355 if (err) {
5356 brcmf_err("rxchain error (%d)\n", err);
5357 nchain = 1;
5358 } else {
5359 for (nchain = 0; rxchain; nchain++)
5360 rxchain = rxchain & (rxchain - 1);
5361 }
5362 brcmf_dbg(INFO, "nchain=%d\n", nchain);
5363
b48d8916 5364 err = brcmf_construct_chaninfo(cfg, bw_cap);
d48200ba 5365 if (err) {
b48d8916 5366 brcmf_err("brcmf_construct_chaninfo failed (%d)\n", err);
d48200ba
HM
5367 return err;
5368 }
5369
b48d8916
AS
5370 wiphy = cfg_to_wiphy(cfg);
5371 for (i = 0; i < ARRAY_SIZE(wiphy->bands); i++) {
5372 band = wiphy->bands[i];
5373 if (band == NULL)
2375d970 5374 continue;
d48200ba 5375
18d6c535
AS
5376 if (nmode)
5377 brcmf_update_ht_cap(band, bw_cap, nchain);
5378 if (vhtmode)
5379 brcmf_update_vht_cap(band, bw_cap, nchain);
d48200ba
HM
5380 }
5381
b48d8916 5382 return 0;
5b435de0
AS
5383}
5384
aa70b4fa
AS
5385static const struct ieee80211_iface_limit brcmf_iface_limits[] = {
5386 {
5387 .max = 2,
5388 .types = BIT(NL80211_IFTYPE_STATION) |
5389 BIT(NL80211_IFTYPE_ADHOC) |
5390 BIT(NL80211_IFTYPE_AP)
5391 },
5392 {
5393 .max = 1,
5394 .types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
5395 BIT(NL80211_IFTYPE_P2P_GO)
5396 },
5397 {
5398 .max = 1,
5399 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5400 }
5401};
5402static struct ieee80211_iface_combination brcmf_iface_combos[] = {
5403 {
5404 .max_interfaces = BRCMF_IFACE_MAX_CNT,
5405 .num_different_channels = 1,
5406 .n_limits = ARRAY_SIZE(brcmf_iface_limits),
5407 .limits = brcmf_iface_limits
5408 }
5409};
5410
5411static const struct ieee80211_txrx_stypes
5412brcmf_txrx_stypes[NUM_NL80211_IFTYPES] = {
5413 [NL80211_IFTYPE_STATION] = {
5414 .tx = 0xffff,
5415 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
5416 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
5417 },
5418 [NL80211_IFTYPE_P2P_CLIENT] = {
5419 .tx = 0xffff,
5420 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
5421 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
5422 },
5423 [NL80211_IFTYPE_P2P_GO] = {
5424 .tx = 0xffff,
5425 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
5426 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
5427 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
5428 BIT(IEEE80211_STYPE_DISASSOC >> 4) |
5429 BIT(IEEE80211_STYPE_AUTH >> 4) |
5430 BIT(IEEE80211_STYPE_DEAUTH >> 4) |
5431 BIT(IEEE80211_STYPE_ACTION >> 4)
5432 },
5433 [NL80211_IFTYPE_P2P_DEVICE] = {
5434 .tx = 0xffff,
5435 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
5436 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
5437 }
5438};
5439
5440static void brcmf_wiphy_pno_params(struct wiphy *wiphy)
5441{
5442 /* scheduled scan settings */
5443 wiphy->max_sched_scan_ssids = BRCMF_PNO_MAX_PFN_COUNT;
5444 wiphy->max_match_sets = BRCMF_PNO_MAX_PFN_COUNT;
5445 wiphy->max_sched_scan_ie_len = BRCMF_SCAN_IE_LEN_MAX;
5446 wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
5447}
5448
4eb3af7c
HM
5449
5450#ifdef CONFIG_PM
5451static const struct wiphy_wowlan_support brcmf_wowlan_support = {
5452 .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT,
5453};
5454#endif
5455
5456static void brcmf_wiphy_wowl_params(struct wiphy *wiphy)
5457{
5458#ifdef CONFIG_PM
5459 /* wowl settings */
5460 wiphy->wowlan = &brcmf_wowlan_support;
5461#endif
5462}
5463
b48d8916 5464static int brcmf_setup_wiphy(struct wiphy *wiphy, struct brcmf_if *ifp)
aa70b4fa 5465{
b48d8916 5466 struct ieee80211_iface_combination ifc_combo;
aa70b4fa
AS
5467 wiphy->max_scan_ssids = WL_NUM_SCAN_MAX;
5468 wiphy->max_scan_ie_len = BRCMF_SCAN_IE_LEN_MAX;
5469 wiphy->max_num_pmkids = WL_NUM_PMKIDS_MAX;
5470 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5471 BIT(NL80211_IFTYPE_ADHOC) |
5472 BIT(NL80211_IFTYPE_AP) |
5473 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5474 BIT(NL80211_IFTYPE_P2P_GO) |
5475 BIT(NL80211_IFTYPE_P2P_DEVICE);
5476 /* need VSDB firmware feature for concurrent channels */
b48d8916 5477 ifc_combo = brcmf_iface_combos[0];
aa70b4fa 5478 if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MCHAN))
b48d8916
AS
5479 ifc_combo.num_different_channels = 2;
5480 wiphy->iface_combinations = kmemdup(&ifc_combo,
5481 sizeof(ifc_combo),
5482 GFP_KERNEL);
aa70b4fa 5483 wiphy->n_iface_combinations = ARRAY_SIZE(brcmf_iface_combos);
aa70b4fa
AS
5484 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
5485 wiphy->cipher_suites = __wl_cipher_suites;
5486 wiphy->n_cipher_suites = ARRAY_SIZE(__wl_cipher_suites);
5487 wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT |
5488 WIPHY_FLAG_OFFCHAN_TX |
5489 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
5490 WIPHY_FLAG_SUPPORTS_TDLS;
5491 if (!brcmf_roamoff)
5492 wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM;
5493 wiphy->mgmt_stypes = brcmf_txrx_stypes;
5494 wiphy->max_remain_on_channel_duration = 5000;
5495 brcmf_wiphy_pno_params(wiphy);
aa70b4fa
AS
5496
5497 /* vendor commands/events support */
5498 wiphy->vendor_commands = brcmf_vendor_cmds;
5499 wiphy->n_vendor_commands = BRCMF_VNDR_CMDS_LAST - 1;
5500
4eb3af7c
HM
5501 if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL))
5502 brcmf_wiphy_wowl_params(wiphy);
5503
b48d8916 5504 return brcmf_setup_wiphybands(wiphy);
5b435de0
AS
5505}
5506
27a68fe3 5507static s32 brcmf_config_dongle(struct brcmf_cfg80211_info *cfg)
5b435de0
AS
5508{
5509 struct net_device *ndev;
5510 struct wireless_dev *wdev;
40a23296 5511 struct brcmf_if *ifp;
5b435de0
AS
5512 s32 power_mode;
5513 s32 err = 0;
5514
27a68fe3 5515 if (cfg->dongle_up)
5b435de0
AS
5516 return err;
5517
27a68fe3 5518 ndev = cfg_to_ndev(cfg);
5b435de0 5519 wdev = ndev->ieee80211_ptr;
40a23296
HM
5520 ifp = netdev_priv(ndev);
5521
5522 /* make sure RF is ready for work */
5523 brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
5b435de0 5524
40a23296
HM
5525 brcmf_dongle_scantime(ifp, WL_SCAN_CHANNEL_TIME,
5526 WL_SCAN_UNASSOC_TIME, WL_SCAN_PASSIVE_TIME);
5b435de0 5527
27a68fe3 5528 power_mode = cfg->pwr_save ? PM_FAST : PM_OFF;
40a23296 5529 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, power_mode);
5b435de0
AS
5530 if (err)
5531 goto default_conf_out;
647c9ae0
AS
5532 brcmf_dbg(INFO, "power save set to %s\n",
5533 (power_mode ? "enabled" : "disabled"));
5b435de0 5534
68ca395f 5535 err = brcmf_dongle_roam(ifp, WL_BEACON_TIMEOUT);
5b435de0
AS
5536 if (err)
5537 goto default_conf_out;
5dd161ff
FL
5538 err = brcmf_cfg80211_change_iface(wdev->wiphy, ndev, wdev->iftype,
5539 NULL, NULL);
40a23296 5540 if (err)
5b435de0 5541 goto default_conf_out;
5b435de0 5542
b3657453
HM
5543 brcmf_configure_arp_offload(ifp, true);
5544
27a68fe3 5545 cfg->dongle_up = true;
40a23296 5546default_conf_out:
5b435de0
AS
5547
5548 return err;
5549
5550}
5551
bdf5ff51 5552static s32 __brcmf_cfg80211_up(struct brcmf_if *ifp)
5b435de0 5553{
c1179033 5554 set_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
5b435de0 5555
bdf5ff51 5556 return brcmf_config_dongle(ifp->drvr->config);
5b435de0
AS
5557}
5558
bdf5ff51 5559static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp)
5b435de0 5560{
bdf5ff51 5561 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
c1179033 5562
5b435de0
AS
5563 /*
5564 * While going down, if associated with AP disassociate
5565 * from AP to save power
5566 */
903e0eee
AS
5567 if (check_vif_up(ifp->vif)) {
5568 brcmf_link_down(ifp->vif);
5b435de0
AS
5569
5570 /* Make sure WPA_Supplicant receives all the event
5571 generated due to DISASSOC call to the fw to keep
5572 the state fw and WPA_Supplicant state consistent
5573 */
5574 brcmf_delay(500);
5575 }
5576
27a68fe3 5577 brcmf_abort_scanning(cfg);
c1179033 5578 clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
5b435de0 5579
5b435de0
AS
5580 return 0;
5581}
5582
bdf5ff51 5583s32 brcmf_cfg80211_up(struct net_device *ndev)
5b435de0 5584{
bdf5ff51
AS
5585 struct brcmf_if *ifp = netdev_priv(ndev);
5586 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5b435de0
AS
5587 s32 err = 0;
5588
27a68fe3 5589 mutex_lock(&cfg->usr_sync);
bdf5ff51 5590 err = __brcmf_cfg80211_up(ifp);
27a68fe3 5591 mutex_unlock(&cfg->usr_sync);
5b435de0
AS
5592
5593 return err;
5594}
5595
bdf5ff51 5596s32 brcmf_cfg80211_down(struct net_device *ndev)
5b435de0 5597{
bdf5ff51
AS
5598 struct brcmf_if *ifp = netdev_priv(ndev);
5599 struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5b435de0
AS
5600 s32 err = 0;
5601
27a68fe3 5602 mutex_lock(&cfg->usr_sync);
bdf5ff51 5603 err = __brcmf_cfg80211_down(ifp);
27a68fe3 5604 mutex_unlock(&cfg->usr_sync);
5b435de0
AS
5605
5606 return err;
5607}
5608
a7965fbb
AS
5609enum nl80211_iftype brcmf_cfg80211_get_iftype(struct brcmf_if *ifp)
5610{
5611 struct wireless_dev *wdev = &ifp->vif->wdev;
5612
5613 return wdev->iftype;
5614}
5615
e843bb19 5616bool brcmf_get_vif_state_any(struct brcmf_cfg80211_info *cfg, unsigned long state)
9f440b7b
AS
5617{
5618 struct brcmf_cfg80211_vif *vif;
9f440b7b
AS
5619
5620 list_for_each_entry(vif, &cfg->vif_list, list) {
5621 if (test_bit(state, &vif->sme_state))
e843bb19 5622 return true;
9f440b7b 5623 }
e843bb19 5624 return false;
9f440b7b 5625}
d3c0b633
AS
5626
5627static inline bool vif_event_equals(struct brcmf_cfg80211_vif_event *event,
5628 u8 action)
5629{
5630 u8 evt_action;
5631
5632 mutex_lock(&event->vif_event_lock);
5633 evt_action = event->action;
5634 mutex_unlock(&event->vif_event_lock);
5635 return evt_action == action;
5636}
5637
5638void brcmf_cfg80211_arm_vif_event(struct brcmf_cfg80211_info *cfg,
5639 struct brcmf_cfg80211_vif *vif)
5640{
5641 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5642
5643 mutex_lock(&event->vif_event_lock);
5644 event->vif = vif;
5645 event->action = 0;
5646 mutex_unlock(&event->vif_event_lock);
5647}
5648
5649bool brcmf_cfg80211_vif_event_armed(struct brcmf_cfg80211_info *cfg)
5650{
5651 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5652 bool armed;
5653
5654 mutex_lock(&event->vif_event_lock);
5655 armed = event->vif != NULL;
5656 mutex_unlock(&event->vif_event_lock);
5657
5658 return armed;
5659}
5660int brcmf_cfg80211_wait_vif_event_timeout(struct brcmf_cfg80211_info *cfg,
5661 u8 action, ulong timeout)
5662{
5663 struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5664
5665 return wait_event_timeout(event->vif_wq,
5666 vif_event_equals(event, action), timeout);
5667}
5668
b48d8916
AS
5669static void brcmf_free_wiphy(struct wiphy *wiphy)
5670{
5671 kfree(wiphy->iface_combinations);
5672 if (wiphy->bands[IEEE80211_BAND_2GHZ]) {
5673 kfree(wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
5674 kfree(wiphy->bands[IEEE80211_BAND_2GHZ]);
5675 }
5676 if (wiphy->bands[IEEE80211_BAND_5GHZ]) {
5677 kfree(wiphy->bands[IEEE80211_BAND_5GHZ]->channels);
5678 kfree(wiphy->bands[IEEE80211_BAND_5GHZ]);
5679 }
5680 wiphy_free(wiphy);
5681}
5682
ccfd1e81
AS
5683struct brcmf_cfg80211_info *brcmf_cfg80211_attach(struct brcmf_pub *drvr,
5684 struct device *busdev)
5685{
5686 struct net_device *ndev = drvr->iflist[0]->ndev;
5687 struct brcmf_cfg80211_info *cfg;
5688 struct wiphy *wiphy;
5689 struct brcmf_cfg80211_vif *vif;
5690 struct brcmf_if *ifp;
5691 s32 err = 0;
5692 s32 io_type;
b48d8916 5693 u16 *cap = NULL;
ccfd1e81
AS
5694
5695 if (!ndev) {
5696 brcmf_err("ndev is invalid\n");
5697 return NULL;
5698 }
5699
5700 ifp = netdev_priv(ndev);
b48d8916
AS
5701 wiphy = wiphy_new(&wl_cfg80211_ops, sizeof(struct brcmf_cfg80211_info));
5702 if (!wiphy) {
5703 brcmf_err("Could not allocate wiphy device\n");
ccfd1e81 5704 return NULL;
b48d8916
AS
5705 }
5706 set_wiphy_dev(wiphy, busdev);
ccfd1e81
AS
5707
5708 cfg = wiphy_priv(wiphy);
5709 cfg->wiphy = wiphy;
5710 cfg->pub = drvr;
5711 init_vif_event(&cfg->vif_event);
5712 INIT_LIST_HEAD(&cfg->vif_list);
5713
5714 vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_STATION, false);
b48d8916
AS
5715 if (IS_ERR(vif))
5716 goto wiphy_out;
ccfd1e81
AS
5717
5718 vif->ifp = ifp;
5719 vif->wdev.netdev = ndev;
5720 ndev->ieee80211_ptr = &vif->wdev;
5721 SET_NETDEV_DEV(ndev, wiphy_dev(cfg->wiphy));
5722
5723 err = wl_init_priv(cfg);
5724 if (err) {
5725 brcmf_err("Failed to init iwm_priv (%d)\n", err);
b48d8916
AS
5726 brcmf_free_vif(vif);
5727 goto wiphy_out;
ccfd1e81
AS
5728 }
5729 ifp->vif = vif;
5730
b48d8916
AS
5731 /* determine d11 io type before wiphy setup */
5732 err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_VERSION, &io_type);
ccfd1e81 5733 if (err) {
b48d8916
AS
5734 brcmf_err("Failed to get D11 version (%d)\n", err);
5735 goto priv_out;
ccfd1e81 5736 }
b48d8916
AS
5737 cfg->d11inf.io_type = (u8)io_type;
5738 brcmu_d11_attach(&cfg->d11inf);
5739
5740 err = brcmf_setup_wiphy(wiphy, ifp);
5741 if (err < 0)
5742 goto priv_out;
5743
5744 brcmf_dbg(INFO, "Registering custom regulatory\n");
5745 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
5746 wiphy_apply_custom_regulatory(wiphy, &brcmf_regdom);
5747
5748 /* firmware defaults to 40MHz disabled in 2G band. We signal
5749 * cfg80211 here that we do and have it decide we can enable
5750 * it. But first check if device does support 2G operation.
5751 */
5752 if (wiphy->bands[IEEE80211_BAND_2GHZ]) {
5753 cap = &wiphy->bands[IEEE80211_BAND_2GHZ]->ht_cap.cap;
5754 *cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5755 }
5756 err = wiphy_register(wiphy);
5757 if (err < 0) {
5758 brcmf_err("Could not register wiphy device (%d)\n", err);
5759 goto priv_out;
ccfd1e81
AS
5760 }
5761
5762 /* If cfg80211 didn't disable 40MHz HT CAP in wiphy_register(),
5763 * setup 40MHz in 2GHz band and enable OBSS scanning.
5764 */
b48d8916
AS
5765 if (cap && (*cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)) {
5766 err = brcmf_enable_bw40_2g(cfg);
ccfd1e81
AS
5767 if (!err)
5768 err = brcmf_fil_iovar_int_set(ifp, "obss_coex",
5769 BRCMF_OBSS_COEX_AUTO);
b48d8916
AS
5770 else
5771 *cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
ccfd1e81 5772 }
ccfd1e81 5773
b48d8916 5774 err = brcmf_p2p_attach(cfg);
ccfd1e81 5775 if (err) {
b48d8916
AS
5776 brcmf_err("P2P initilisation failed (%d)\n", err);
5777 goto wiphy_unreg_out;
5778 }
5779 err = brcmf_btcoex_attach(cfg);
5780 if (err) {
5781 brcmf_err("BT-coex initialisation failed (%d)\n", err);
5782 brcmf_p2p_detach(&cfg->p2p);
5783 goto wiphy_unreg_out;
ccfd1e81
AS
5784 }
5785
b48d8916 5786 err = brcmf_fil_iovar_int_set(ifp, "tdls_enable", 1);
ccfd1e81 5787 if (err) {
b48d8916
AS
5788 brcmf_dbg(INFO, "TDLS not enabled (%d)\n", err);
5789 wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_TDLS;
70b7d94b
HM
5790 } else {
5791 brcmf_fweh_register(cfg->pub, BRCMF_E_TDLS_PEER_EVENT,
5792 brcmf_notify_tdls_peer_event);
ccfd1e81 5793 }
ccfd1e81
AS
5794
5795 return cfg;
5796
b48d8916
AS
5797wiphy_unreg_out:
5798 wiphy_unregister(cfg->wiphy);
5799priv_out:
ccfd1e81 5800 wl_deinit_priv(cfg);
ccfd1e81 5801 brcmf_free_vif(vif);
b48d8916
AS
5802wiphy_out:
5803 brcmf_free_wiphy(wiphy);
ccfd1e81
AS
5804 return NULL;
5805}
5806
5807void brcmf_cfg80211_detach(struct brcmf_cfg80211_info *cfg)
5808{
5809 if (!cfg)
5810 return;
5811
5812 WARN_ON(!list_empty(&cfg->vif_list));
5813 wiphy_unregister(cfg->wiphy);
5814 brcmf_btcoex_detach(cfg);
c3da74bb 5815 brcmf_p2p_detach(&cfg->p2p);
ccfd1e81 5816 wl_deinit_priv(cfg);
b48d8916 5817 brcmf_free_wiphy(cfg->wiphy);
ccfd1e81 5818}
This page took 0.818593 seconds and 5 git commands to generate.