Merge git://git.kvack.org/~bcrl/aio-fixes
[cascardo/linux.git] / drivers / net / wireless / brcm80211 / brcmfmac / wl_cfg80211.c
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>
20 #include <linux/etherdevice.h>
21 #include <linux/module.h>
22 #include <linux/vmalloc.h>
23 #include <net/cfg80211.h>
24 #include <net/netlink.h>
25
26 #include <brcmu_utils.h>
27 #include <defs.h>
28 #include <brcmu_wifi.h>
29 #include "dhd.h"
30 #include "dhd_dbg.h"
31 #include "tracepoint.h"
32 #include "fwil_types.h"
33 #include "p2p.h"
34 #include "btcoex.h"
35 #include "wl_cfg80211.h"
36 #include "feature.h"
37 #include "fwil.h"
38 #include "proto.h"
39 #include "vendor.h"
40 #include "dhd_bus.h"
41
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
54 #define BRCMF_IFACE_MAX_CNT             3
55
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
60 #define WPS_OUI_TYPE                    4
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
86 #define VNDR_IE_PARSE_LIMIT             5
87
88 #define DOT11_MGMT_HDR_LEN              24      /* d11 management header len */
89 #define DOT11_BCN_PRB_FIXED_LEN         12      /* beacon/probe fixed length */
90
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
95 #define BRCMF_ASSOC_PARAMS_FIXED_SIZE \
96         (sizeof(struct brcmf_assoc_params_le) - sizeof(u16))
97
98 static bool check_vif_up(struct brcmf_cfg80211_vif *vif)
99 {
100         if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state)) {
101                 brcmf_dbg(INFO, "device is not ready : status (%lu)\n",
102                           vif->sme_state);
103                 return false;
104         }
105         return true;
106 }
107
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
116 static 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
136 /* Band templates duplicated per wiphy. The channel info
137  * is filled in after querying the device.
138  */
139 static const struct ieee80211_supported_band __wl_band_2ghz = {
140         .band = IEEE80211_BAND_2GHZ,
141         .bitrates = wl_g_rates,
142         .n_bitrates = wl_g_rates_size,
143 };
144
145 static const struct ieee80211_supported_band __wl_band_5ghz_a = {
146         .band = IEEE80211_BAND_5GHZ,
147         .bitrates = wl_a_rates,
148         .n_bitrates = wl_a_rates_size,
149 };
150
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
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
156  * domain are to be done here.
157  */
158 static 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 */
170                 REG_RULE(5150-10, 5350+10, 80, 6, 20, 0),
171                 /* IEEE 802.11a, channel 100..165 */
172                 REG_RULE(5470-10, 5850+10, 80, 6, 20, 0), }
173 };
174
175 static 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
183 /* Vendor specific ie. id = 221, oui and type defines exact ie */
184 struct brcmf_vs_tlv {
185         u8 id;
186         u8 len;
187         u8 oui[3];
188         u8 oui_type;
189 };
190
191 struct 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
197 struct parsed_vndr_ies {
198         u32 count;
199         struct parsed_vndr_ie_info ie_info[VNDR_IE_PARSE_LIMIT];
200 };
201
202 static int brcmf_roamoff;
203 module_param_named(roamoff, brcmf_roamoff, int, S_IRUSR);
204 MODULE_PARM_DESC(roamoff, "do not use internal roaming engine");
205
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
227 static 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
236 static 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
259 static 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
290 static u16 chandef_to_chanspec(struct brcmu_d11inf *d11inf,
291                                struct cfg80211_chan_def *ch)
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:
302         case NL80211_CHAN_WIDTH_20_NOHT:
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;
327         case NL80211_CHAN_WIDTH_80P80:
328         case NL80211_CHAN_WIDTH_160:
329         case NL80211_CHAN_WIDTH_5:
330         case NL80211_CHAN_WIDTH_10:
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;
341         case IEEE80211_BAND_60GHZ:
342         default:
343                 WARN_ON_ONCE(1);
344         }
345         d11inf->encchspec(&ch_inf);
346
347         return ch_inf.chspec;
348 }
349
350 u16 channel_to_chanspec(struct brcmu_d11inf *d11inf,
351                         struct ieee80211_channel *ch)
352 {
353         struct brcmu_chan ch_inf;
354
355         ch_inf.chnum = ieee80211_frequency_to_channel(ch->center_freq);
356         ch_inf.bw = BRCMU_CHAN_BW_20;
357         d11inf->encchspec(&ch_inf);
358
359         return ch_inf.chspec;
360 }
361
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  */
366 const struct brcmf_tlv *
367 brcmf_parse_tlvs(const void *buf, int buflen, uint key)
368 {
369         const struct brcmf_tlv *elt = buf;
370         int totlen = buflen;
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  */
390 static bool
391 brcmf_tlv_has_ie(const u8 *ie, const u8 **tlvs, u32 *tlvs_len,
392                  const u8 *oui, u32 oui_len, u8 type)
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
413 static struct brcmf_vs_tlv *
414 brcmf_find_wpaie(const u8 *parse, u32 len)
415 {
416         const struct brcmf_tlv *ie;
417
418         while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
419                 if (brcmf_tlv_has_ie((const u8 *)ie, &parse, &len,
420                                      WPA_OUI, TLV_OUI_LEN, WPA_OUI_TYPE))
421                         return (struct brcmf_vs_tlv *)ie;
422         }
423         return NULL;
424 }
425
426 static struct brcmf_vs_tlv *
427 brcmf_find_wpsie(const u8 *parse, u32 len)
428 {
429         const struct brcmf_tlv *ie;
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
440 static 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
454 static int
455 send_key_to_dongle(struct net_device *ndev, struct brcmf_wsec_key *key)
456 {
457         int err;
458         struct brcmf_wsec_key_le key_le;
459
460         convert_key_from_CPU(key, &key_le);
461
462         brcmf_netdev_wait_pend8021x(ndev);
463
464         err = brcmf_fil_bsscfg_data_set(netdev_priv(ndev), "wsec_key", &key_le,
465                                         sizeof(key_le));
466
467         if (err)
468                 brcmf_err("wsec_key error (%d)\n", err);
469         return err;
470 }
471
472 static s32
473 brcmf_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
504 static void
505 brcmf_cfg80211_update_proto_addr_mode(struct wireless_dev *wdev)
506 {
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;
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
523 static 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
531 static bool brcmf_is_ibssmode(struct brcmf_cfg80211_vif *vif)
532 {
533         return vif->wdev.iftype == NL80211_IFTYPE_ADHOC;
534 }
535
536 static 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 {
542         struct wireless_dev *wdev;
543
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:
556         case NL80211_IFTYPE_P2P_DEVICE:
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;
561         case NL80211_IFTYPE_UNSPECIFIED:
562         default:
563                 return ERR_PTR(-EINVAL);
564         }
565 }
566
567 static void brcmf_scan_config_mpc(struct brcmf_if *ifp, int mpc)
568 {
569         if (brcmf_feat_is_quirk_enabled(ifp, BRCMF_FEAT_QUIRK_NEED_MPC))
570                 brcmf_set_mpc(ifp, mpc);
571 }
572
573 void brcmf_set_mpc(struct brcmf_if *ifp, int mpc)
574 {
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
587 s32 brcmf_notify_escan_complete(struct brcmf_cfg80211_info *cfg,
588                                 struct brcmf_if *ifp, bool aborted,
589                                 bool fw_abort)
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 */
620                 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
621                                              &params_le, sizeof(params_le));
622                 if (err)
623                         brcmf_err("Scan abort  failed\n");
624         }
625
626         brcmf_scan_config_mpc(ifp, 1);
627
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));
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);
641         }
642         if (!test_and_clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
643                 brcmf_dbg(SCAN, "Scan complete, probably P2P scan\n");
644
645         return err;
646 }
647
648 static
649 int brcmf_cfg80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
650 {
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) &&
660                     cfg->escan_info.ifp == netdev_priv(ndev))
661                         brcmf_notify_escan_complete(cfg, netdev_priv(ndev),
662                                                     true, true);
663
664                 brcmf_fil_iovar_int_set(netdev_priv(ndev), "mpc", 1);
665         }
666
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:
678         case NL80211_IFTYPE_P2P_DEVICE:
679                 return brcmf_p2p_del_vif(wiphy, wdev);
680         case NL80211_IFTYPE_UNSPECIFIED:
681         default:
682                 return -EINVAL;
683         }
684         return -EOPNOTSUPP;
685 }
686
687 static s32
688 brcmf_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
689                          enum nl80211_iftype type, u32 *flags,
690                          struct vif_params *params)
691 {
692         struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
693         struct brcmf_if *ifp = netdev_priv(ndev);
694         struct brcmf_cfg80211_vif *vif = ifp->vif;
695         s32 infra = 0;
696         s32 ap = 0;
697         s32 err = 0;
698
699         brcmf_dbg(TRACE, "Enter, ndev=%p, type=%d\n", ndev, type);
700
701         switch (type) {
702         case NL80211_IFTYPE_MONITOR:
703         case NL80211_IFTYPE_WDS:
704                 brcmf_err("type (%d) : currently we do not support this type\n",
705                           type);
706                 return -EOPNOTSUPP;
707         case NL80211_IFTYPE_ADHOC:
708                 infra = 0;
709                 break;
710         case NL80211_IFTYPE_STATION:
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                 }
723                 infra = 1;
724                 break;
725         case NL80211_IFTYPE_AP:
726         case NL80211_IFTYPE_P2P_GO:
727                 ap = 1;
728                 break;
729         default:
730                 err = -EINVAL;
731                 goto done;
732         }
733
734         if (ap) {
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                 }
743         } else {
744                 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, infra);
745                 if (err) {
746                         brcmf_err("WLC_SET_INFRA error (%d)\n", err);
747                         err = -EAGAIN;
748                         goto done;
749                 }
750                 brcmf_dbg(INFO, "IF Type = %s\n", brcmf_is_ibssmode(vif) ?
751                           "Adhoc" : "Infra");
752         }
753         ndev->ieee80211_ptr->iftype = type;
754
755         brcmf_cfg80211_update_proto_addr_mode(&vif->wdev);
756
757 done:
758         brcmf_dbg(TRACE, "Exit\n");
759
760         return err;
761 }
762
763 static void brcmf_escan_prep(struct brcmf_cfg80211_info *cfg,
764                              struct brcmf_scan_params_le *params_le,
765                              struct cfg80211_scan_request *request)
766 {
767         u32 n_ssids;
768         u32 n_channels;
769         s32 i;
770         s32 offset;
771         u16 chanspec;
772         char *ptr;
773         struct brcmf_ssid_le ssid_le;
774
775         memset(params_le->bssid, 0xFF, ETH_ALEN);
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 */
792         brcmf_dbg(SCAN, "### List of channelspecs to scan ### %d\n",
793                   n_channels);
794         if (n_channels > 0) {
795                 for (i = 0; i < n_channels; i++) {
796                         chanspec = channel_to_chanspec(&cfg->d11inf,
797                                                        request->channels[i]);
798                         brcmf_dbg(SCAN, "Chan : %d, Channel spec: %x\n",
799                                   request->channels[i]->hw_value, chanspec);
800                         params_le->channel_list[i] = cpu_to_le16(chanspec);
801                 }
802         } else {
803                 brcmf_dbg(SCAN, "Scanning all channels\n");
804         }
805         /* Copy ssid array if applicable */
806         brcmf_dbg(SCAN, "### List of SSIDs to scan ### %d\n", n_ssids);
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++) {
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)
819                                 brcmf_dbg(SCAN, "%d: Broadcast scan\n", i);
820                         else
821                                 brcmf_dbg(SCAN, "%d: scan for  %s size =%d\n",
822                                           i, ssid_le.SSID, ssid_le.SSID_len);
823                         memcpy(ptr, &ssid_le, sizeof(ssid_le));
824                         ptr += sizeof(ssid_le);
825                 }
826         } else {
827                 brcmf_dbg(SCAN, "Broadcast scan %p\n", request->ssids);
828                 if ((request->ssids) && request->ssids->ssid_len) {
829                         brcmf_dbg(SCAN, "SSID %s len=%d\n",
830                                   params_le->ssid_le.SSID,
831                                   request->ssids->ssid_len);
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
844 static s32
845 brcmf_run_escan(struct brcmf_cfg80211_info *cfg, struct brcmf_if *ifp,
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
853         brcmf_dbg(SCAN, "E-SCAN START\n");
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);
869         brcmf_escan_prep(cfg, &params->params_le, request);
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
874         err = brcmf_fil_iovar_data_set(ifp, "escan", params, params_size);
875         if (err) {
876                 if (err == -EBUSY)
877                         brcmf_dbg(INFO, "system busy : escan canceled\n");
878                 else
879                         brcmf_err("error (%d)\n", err);
880         }
881
882         kfree(params);
883 exit:
884         return err;
885 }
886
887 static s32
888 brcmf_do_escan(struct brcmf_cfg80211_info *cfg, struct wiphy *wiphy,
889                struct brcmf_if *ifp, struct cfg80211_scan_request *request)
890 {
891         s32 err;
892         u32 passive_scan;
893         struct brcmf_scan_results *results;
894         struct escan_info *escan = &cfg->escan_info;
895
896         brcmf_dbg(SCAN, "Enter\n");
897         escan->ifp = ifp;
898         escan->wiphy = wiphy;
899         escan->escan_state = WL_ESCAN_STATE_SCANNING;
900         passive_scan = cfg->active_scan ? 0 : 1;
901         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PASSIVE_SCAN,
902                                     passive_scan);
903         if (err) {
904                 brcmf_err("error (%d)\n", err);
905                 return err;
906         }
907         brcmf_scan_config_mpc(ifp, 0);
908         results = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
909         results->version = 0;
910         results->count = 0;
911         results->buflen = WL_ESCAN_RESULTS_FIXED_SIZE;
912
913         err = escan->run(cfg, ifp, request, WL_ESCAN_ACTION_START);
914         if (err)
915                 brcmf_scan_config_mpc(ifp, 1);
916         return err;
917 }
918
919 static s32
920 brcmf_cfg80211_escan(struct wiphy *wiphy, struct brcmf_cfg80211_vif *vif,
921                      struct cfg80211_scan_request *request,
922                      struct cfg80211_ssid *this_ssid)
923 {
924         struct brcmf_if *ifp = vif->ifp;
925         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
926         struct cfg80211_ssid *ssids;
927         struct brcmf_cfg80211_scan_req *sr = &cfg->scan_req_int;
928         u32 passive_scan;
929         bool escan_req;
930         bool spec_scan;
931         s32 err;
932         u32 SSID_len;
933
934         brcmf_dbg(SCAN, "START ESCAN\n");
935
936         if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
937                 brcmf_err("Scanning already: status (%lu)\n", cfg->scan_status);
938                 return -EAGAIN;
939         }
940         if (test_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status)) {
941                 brcmf_err("Scanning being aborted: status (%lu)\n",
942                           cfg->scan_status);
943                 return -EAGAIN;
944         }
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         }
950         if (test_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state)) {
951                 brcmf_err("Connecting: status (%lu)\n", ifp->vif->sme_state);
952                 return -EAGAIN;
953         }
954
955         /* If scan req comes for p2p0, send it over primary I/F */
956         if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
957                 vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
958
959         /* Arm scan timeout timer */
960         mod_timer(&cfg->escan_timeout, jiffies +
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
974         cfg->scan_request = request;
975         set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
976         if (escan_req) {
977                 cfg->escan_info.run = brcmf_run_escan;
978                 err = brcmf_p2p_scan_prep(wiphy, request, vif);
979                 if (err)
980                         goto scan_out;
981
982                 err = brcmf_do_escan(cfg, wiphy, vif->ifp, request);
983                 if (err)
984                         goto scan_out;
985         } else {
986                 brcmf_dbg(SCAN, "ssid \"%s\", ssid_len (%d)\n",
987                           ssids->ssid, ssids->ssid_len);
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
997                         brcmf_dbg(SCAN, "Broadcast scan\n");
998
999                 passive_scan = cfg->active_scan ? 0 : 1;
1000                 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PASSIVE_SCAN,
1001                                             passive_scan);
1002                 if (err) {
1003                         brcmf_err("WLC_SET_PASSIVE_SCAN error (%d)\n", err);
1004                         goto scan_out;
1005                 }
1006                 brcmf_scan_config_mpc(ifp, 0);
1007                 err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
1008                                              &sr->ssid_le, sizeof(sr->ssid_le));
1009                 if (err) {
1010                         if (err == -EBUSY)
1011                                 brcmf_dbg(INFO, "BUSY: scan for \"%s\" canceled\n",
1012                                           sr->ssid_le.SSID);
1013                         else
1014                                 brcmf_err("WLC_SCAN error (%d)\n", err);
1015
1016                         brcmf_scan_config_mpc(ifp, 1);
1017                         goto scan_out;
1018                 }
1019         }
1020
1021         return 0;
1022
1023 scan_out:
1024         clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
1025         if (timer_pending(&cfg->escan_timeout))
1026                 del_timer_sync(&cfg->escan_timeout);
1027         cfg->scan_request = NULL;
1028         return err;
1029 }
1030
1031 static s32
1032 brcmf_cfg80211_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
1033 {
1034         struct brcmf_cfg80211_vif *vif;
1035         s32 err = 0;
1036
1037         brcmf_dbg(TRACE, "Enter\n");
1038         vif = container_of(request->wdev, struct brcmf_cfg80211_vif, wdev);
1039         if (!check_vif_up(vif))
1040                 return -EIO;
1041
1042         err = brcmf_cfg80211_escan(wiphy, vif, request, NULL);
1043
1044         if (err)
1045                 brcmf_err("scan error (%d)\n", err);
1046
1047         brcmf_dbg(TRACE, "Exit\n");
1048         return err;
1049 }
1050
1051 static s32 brcmf_set_rts(struct net_device *ndev, u32 rts_threshold)
1052 {
1053         s32 err = 0;
1054
1055         err = brcmf_fil_iovar_int_set(netdev_priv(ndev), "rtsthresh",
1056                                       rts_threshold);
1057         if (err)
1058                 brcmf_err("Error (%d)\n", err);
1059
1060         return err;
1061 }
1062
1063 static s32 brcmf_set_frag(struct net_device *ndev, u32 frag_threshold)
1064 {
1065         s32 err = 0;
1066
1067         err = brcmf_fil_iovar_int_set(netdev_priv(ndev), "fragthresh",
1068                                       frag_threshold);
1069         if (err)
1070                 brcmf_err("Error (%d)\n", err);
1071
1072         return err;
1073 }
1074
1075 static s32 brcmf_set_retry(struct net_device *ndev, u32 retry, bool l)
1076 {
1077         s32 err = 0;
1078         u32 cmd = (l ? BRCMF_C_SET_LRL : BRCMF_C_SET_SRL);
1079
1080         err = brcmf_fil_cmd_int_set(netdev_priv(ndev), cmd, retry);
1081         if (err) {
1082                 brcmf_err("cmd (%d) , error (%d)\n", cmd, err);
1083                 return err;
1084         }
1085         return err;
1086 }
1087
1088 static s32 brcmf_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1089 {
1090         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1091         struct net_device *ndev = cfg_to_ndev(cfg);
1092         struct brcmf_if *ifp = netdev_priv(ndev);
1093         s32 err = 0;
1094
1095         brcmf_dbg(TRACE, "Enter\n");
1096         if (!check_vif_up(ifp->vif))
1097                 return -EIO;
1098
1099         if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
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);
1103                 if (!err)
1104                         goto done;
1105         }
1106         if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
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);
1110                 if (!err)
1111                         goto done;
1112         }
1113         if (changed & WIPHY_PARAM_RETRY_LONG
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);
1117                 if (!err)
1118                         goto done;
1119         }
1120         if (changed & WIPHY_PARAM_RETRY_SHORT
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);
1124                 if (!err)
1125                         goto done;
1126         }
1127
1128 done:
1129         brcmf_dbg(TRACE, "Exit\n");
1130         return err;
1131 }
1132
1133 static void brcmf_init_prof(struct brcmf_cfg80211_profile *prof)
1134 {
1135         memset(prof, 0, sizeof(*prof));
1136 }
1137
1138 static void brcmf_link_down(struct brcmf_cfg80211_vif *vif)
1139 {
1140         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(vif->wdev.wiphy);
1141         s32 err = 0;
1142
1143         brcmf_dbg(TRACE, "Enter\n");
1144
1145         if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state)) {
1146                 brcmf_dbg(INFO, "Call WLC_DISASSOC to stop excess roaming\n ");
1147                 err = brcmf_fil_cmd_data_set(vif->ifp,
1148                                              BRCMF_C_DISASSOC, NULL, 0);
1149                 if (err) {
1150                         brcmf_err("WLC_DISASSOC failed (%d)\n", err);
1151                 }
1152                 clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state);
1153                 cfg80211_disconnected(vif->wdev.netdev, 0, NULL, 0, GFP_KERNEL);
1154
1155         }
1156         clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state);
1157         clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
1158         brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
1159         brcmf_dbg(TRACE, "Exit\n");
1160 }
1161
1162 static s32
1163 brcmf_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
1164                       struct cfg80211_ibss_params *params)
1165 {
1166         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1167         struct brcmf_if *ifp = netdev_priv(ndev);
1168         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
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;
1174         u16 chanspec;
1175
1176         brcmf_dbg(TRACE, "Enter\n");
1177         if (!check_vif_up(ifp->vif))
1178                 return -EIO;
1179
1180         if (params->ssid)
1181                 brcmf_dbg(CONN, "SSID: %s\n", params->ssid);
1182         else {
1183                 brcmf_dbg(CONN, "SSID: NULL, Not supported\n");
1184                 return -EOPNOTSUPP;
1185         }
1186
1187         set_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
1188
1189         if (params->bssid)
1190                 brcmf_dbg(CONN, "BSSID: %pM\n", params->bssid);
1191         else
1192                 brcmf_dbg(CONN, "No BSSID specified\n");
1193
1194         if (params->chandef.chan)
1195                 brcmf_dbg(CONN, "channel: %d\n",
1196                           params->chandef.chan->center_freq);
1197         else
1198                 brcmf_dbg(CONN, "no channel specified\n");
1199
1200         if (params->channel_fixed)
1201                 brcmf_dbg(CONN, "fixed channel required\n");
1202         else
1203                 brcmf_dbg(CONN, "no fixed channel required\n");
1204
1205         if (params->ie && params->ie_len)
1206                 brcmf_dbg(CONN, "ie len: %d\n", params->ie_len);
1207         else
1208                 brcmf_dbg(CONN, "no ie specified\n");
1209
1210         if (params->beacon_interval)
1211                 brcmf_dbg(CONN, "beacon interval: %d\n",
1212                           params->beacon_interval);
1213         else
1214                 brcmf_dbg(CONN, "no beacon interval specified\n");
1215
1216         if (params->basic_rates)
1217                 brcmf_dbg(CONN, "basic rates: %08X\n", params->basic_rates);
1218         else
1219                 brcmf_dbg(CONN, "no basic rates specified\n");
1220
1221         if (params->privacy)
1222                 brcmf_dbg(CONN, "privacy required\n");
1223         else
1224                 brcmf_dbg(CONN, "no privacy required\n");
1225
1226         /* Configure Privacy for starter */
1227         if (params->privacy)
1228                 wsec |= WEP_ENABLED;
1229
1230         err = brcmf_fil_iovar_int_set(ifp, "wsec", wsec);
1231         if (err) {
1232                 brcmf_err("wsec failed (%d)\n", err);
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
1242         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD, bcnprd);
1243         if (err) {
1244                 brcmf_err("WLC_SET_BCNPRD failed (%d)\n", err);
1245                 goto done;
1246         }
1247
1248         /* Configure required join parameter */
1249         memset(&join_params, 0, sizeof(struct brcmf_join_params));
1250
1251         /* SSID */
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);
1256         join_params_size = sizeof(join_params.ssid_le);
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;
1263                 memcpy(profile->bssid, params->bssid, ETH_ALEN);
1264         } else {
1265                 memset(join_params.params_le.bssid, 0xFF, ETH_ALEN);
1266                 memset(profile->bssid, 0, ETH_ALEN);
1267         }
1268
1269         /* Channel */
1270         if (params->chandef.chan) {
1271                 u32 target_channel;
1272
1273                 cfg->channel =
1274                         ieee80211_frequency_to_channel(
1275                                 params->chandef.chan->center_freq);
1276                 if (params->channel_fixed) {
1277                         /* adding chanspec */
1278                         chanspec = chandef_to_chanspec(&cfg->d11inf,
1279                                                        &params->chandef);
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);
1284                 }
1285
1286                 /* set channel for starter */
1287                 target_channel = cfg->channel;
1288                 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_CHANNEL,
1289                                             target_channel);
1290                 if (err) {
1291                         brcmf_err("WLC_SET_CHANNEL failed (%d)\n", err);
1292                         goto done;
1293                 }
1294         } else
1295                 cfg->channel = 0;
1296
1297         cfg->ibss_starter = false;
1298
1299
1300         err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
1301                                      &join_params, join_params_size);
1302         if (err) {
1303                 brcmf_err("WLC_SET_SSID failed (%d)\n", err);
1304                 goto done;
1305         }
1306
1307 done:
1308         if (err)
1309                 clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
1310         brcmf_dbg(TRACE, "Exit\n");
1311         return err;
1312 }
1313
1314 static s32
1315 brcmf_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *ndev)
1316 {
1317         struct brcmf_if *ifp = netdev_priv(ndev);
1318
1319         brcmf_dbg(TRACE, "Enter\n");
1320         if (!check_vif_up(ifp->vif))
1321                 return -EIO;
1322
1323         brcmf_link_down(ifp->vif);
1324
1325         brcmf_dbg(TRACE, "Exit\n");
1326
1327         return 0;
1328 }
1329
1330 static s32 brcmf_set_wpa_version(struct net_device *ndev,
1331                                  struct cfg80211_connect_params *sme)
1332 {
1333         struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
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;
1344         brcmf_dbg(CONN, "setting wpa_auth to 0x%0x\n", val);
1345         err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "wpa_auth", val);
1346         if (err) {
1347                 brcmf_err("set wpa_auth failed (%d)\n", err);
1348                 return err;
1349         }
1350         sec = &profile->sec;
1351         sec->wpa_versions = sme->crypto.wpa_versions;
1352         return err;
1353 }
1354
1355 static s32 brcmf_set_auth_type(struct net_device *ndev,
1356                                struct cfg80211_connect_params *sme)
1357 {
1358         struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
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;
1366                 brcmf_dbg(CONN, "open system\n");
1367                 break;
1368         case NL80211_AUTHTYPE_SHARED_KEY:
1369                 val = 1;
1370                 brcmf_dbg(CONN, "shared key\n");
1371                 break;
1372         case NL80211_AUTHTYPE_AUTOMATIC:
1373                 val = 2;
1374                 brcmf_dbg(CONN, "automatic\n");
1375                 break;
1376         case NL80211_AUTHTYPE_NETWORK_EAP:
1377                 brcmf_dbg(CONN, "network eap\n");
1378         default:
1379                 val = 2;
1380                 brcmf_err("invalid auth type (%d)\n", sme->auth_type);
1381                 break;
1382         }
1383
1384         err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "auth", val);
1385         if (err) {
1386                 brcmf_err("set auth failed (%d)\n", err);
1387                 return err;
1388         }
1389         sec = &profile->sec;
1390         sec->auth_type = sme->auth_type;
1391         return err;
1392 }
1393
1394 static s32
1395 brcmf_set_wsec_mode(struct net_device *ndev,
1396                      struct cfg80211_connect_params *sme, bool mfp)
1397 {
1398         struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1399         struct brcmf_cfg80211_security *sec;
1400         s32 pval = 0;
1401         s32 gval = 0;
1402         s32 wsec;
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:
1421                         brcmf_err("invalid cipher pairwise (%d)\n",
1422                                   sme->crypto.ciphers_pairwise[0]);
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:
1442                         brcmf_err("invalid cipher group (%d)\n",
1443                                   sme->crypto.cipher_group);
1444                         return -EINVAL;
1445                 }
1446         }
1447
1448         brcmf_dbg(CONN, "pval (%d) gval (%d)\n", pval, gval);
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;
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);
1460         if (err) {
1461                 brcmf_err("error (%d)\n", err);
1462                 return err;
1463         }
1464
1465         sec = &profile->sec;
1466         sec->cipher_pairwise = sme->crypto.ciphers_pairwise[0];
1467         sec->cipher_group = sme->crypto.cipher_group;
1468
1469         return err;
1470 }
1471
1472 static s32
1473 brcmf_set_key_mgmt(struct net_device *ndev, struct cfg80211_connect_params *sme)
1474 {
1475         struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1476         struct brcmf_cfg80211_security *sec;
1477         s32 val = 0;
1478         s32 err = 0;
1479
1480         if (sme->crypto.n_akm_suites) {
1481                 err = brcmf_fil_bsscfg_int_get(netdev_priv(ndev),
1482                                                "wpa_auth", &val);
1483                 if (err) {
1484                         brcmf_err("could not get wpa_auth (%d)\n", err);
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:
1496                                 brcmf_err("invalid cipher group (%d)\n",
1497                                           sme->crypto.cipher_group);
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:
1509                                 brcmf_err("invalid cipher group (%d)\n",
1510                                           sme->crypto.cipher_group);
1511                                 return -EINVAL;
1512                         }
1513                 }
1514
1515                 brcmf_dbg(CONN, "setting wpa_auth to %d\n", val);
1516                 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev),
1517                                                "wpa_auth", val);
1518                 if (err) {
1519                         brcmf_err("could not set wpa_auth (%d)\n", err);
1520                         return err;
1521                 }
1522         }
1523         sec = &profile->sec;
1524         sec->wpa_auth = sme->crypto.akm_suites[0];
1525
1526         return err;
1527 }
1528
1529 static s32
1530 brcmf_set_sharedkey(struct net_device *ndev,
1531                     struct cfg80211_connect_params *sme)
1532 {
1533         struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1534         struct brcmf_cfg80211_security *sec;
1535         struct brcmf_wsec_key key;
1536         s32 val;
1537         s32 err = 0;
1538
1539         brcmf_dbg(CONN, "key len (%d)\n", sme->key_len);
1540
1541         if (sme->key_len == 0)
1542                 return 0;
1543
1544         sec = &profile->sec;
1545         brcmf_dbg(CONN, "wpa_versions 0x%x cipher_pairwise 0x%x\n",
1546                   sec->wpa_versions, sec->cipher_pairwise);
1547
1548         if (sec->wpa_versions & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2))
1549                 return 0;
1550
1551         if (!(sec->cipher_pairwise &
1552             (WLAN_CIPHER_SUITE_WEP40 | WLAN_CIPHER_SUITE_WEP104)))
1553                 return 0;
1554
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)) {
1559                 brcmf_err("Too long key length (%u)\n", key.len);
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:
1572                 brcmf_err("Invalid algorithm (%d)\n",
1573                           sme->crypto.ciphers_pairwise[0]);
1574                 return -EINVAL;
1575         }
1576         /* Set the new key/index */
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);
1580         err = send_key_to_dongle(ndev, &key);
1581         if (err)
1582                 return err;
1583
1584         if (sec->auth_type == NL80211_AUTHTYPE_SHARED_KEY) {
1585                 brcmf_dbg(CONN, "set auth_type to shared key\n");
1586                 val = WL_AUTH_SHARED_KEY;       /* shared key */
1587                 err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "auth", val);
1588                 if (err)
1589                         brcmf_err("set auth failed (%d)\n", err);
1590         }
1591         return err;
1592 }
1593
1594 static
1595 enum nl80211_auth_type brcmf_war_auth_type(struct brcmf_if *ifp,
1596                                            enum nl80211_auth_type type)
1597 {
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;
1602         }
1603         return type;
1604 }
1605
1606 static s32
1607 brcmf_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev,
1608                        struct cfg80211_connect_params *sme)
1609 {
1610         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1611         struct brcmf_if *ifp = netdev_priv(ndev);
1612         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
1613         struct ieee80211_channel *chan = sme->channel;
1614         struct brcmf_join_params join_params;
1615         size_t join_params_size;
1616         const struct brcmf_tlv *rsn_ie;
1617         const struct brcmf_vs_tlv *wpa_ie;
1618         const void *ie;
1619         u32 ie_len;
1620         struct brcmf_ext_join_params_le *ext_join_params;
1621         u16 chanspec;
1622         s32 err = 0;
1623
1624         brcmf_dbg(TRACE, "Enter\n");
1625         if (!check_vif_up(ifp->vif))
1626                 return -EIO;
1627
1628         if (!sme->ssid) {
1629                 brcmf_err("Invalid ssid\n");
1630                 return -EOPNOTSUPP;
1631         }
1632
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 */
1644                         rsn_ie = brcmf_parse_tlvs((const u8 *)sme->ie,
1645                                                   sme->ie_len,
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
1662         set_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
1663
1664         if (chan) {
1665                 cfg->channel =
1666                         ieee80211_frequency_to_channel(chan->center_freq);
1667                 chanspec = channel_to_chanspec(&cfg->d11inf, chan);
1668                 brcmf_dbg(CONN, "channel=%d, center_req=%d, chanspec=0x%04x\n",
1669                           cfg->channel, chan->center_freq, chanspec);
1670         } else {
1671                 cfg->channel = 0;
1672                 chanspec = 0;
1673         }
1674
1675         brcmf_dbg(INFO, "ie (%p), ie_len (%zd)\n", sme->ie, sme->ie_len);
1676
1677         err = brcmf_set_wpa_version(ndev, sme);
1678         if (err) {
1679                 brcmf_err("wl_set_wpa_version failed (%d)\n", err);
1680                 goto done;
1681         }
1682
1683         sme->auth_type = brcmf_war_auth_type(ifp, sme->auth_type);
1684         err = brcmf_set_auth_type(ndev, sme);
1685         if (err) {
1686                 brcmf_err("wl_set_auth_type failed (%d)\n", err);
1687                 goto done;
1688         }
1689
1690         err = brcmf_set_wsec_mode(ndev, sme, sme->mfp == NL80211_MFP_REQUIRED);
1691         if (err) {
1692                 brcmf_err("wl_set_set_cipher failed (%d)\n", err);
1693                 goto done;
1694         }
1695
1696         err = brcmf_set_key_mgmt(ndev, sme);
1697         if (err) {
1698                 brcmf_err("wl_set_key_mgmt failed (%d)\n", err);
1699                 goto done;
1700         }
1701
1702         err = brcmf_set_sharedkey(ndev, sme);
1703         if (err) {
1704                 brcmf_err("brcmf_set_sharedkey failed (%d)\n", err);
1705                 goto done;
1706         }
1707
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);
1732
1733         /* Set up join scan parameters */
1734         ext_join_params->scan_le.scan_type = -1;
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);
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);
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 */
1776         memset(&join_params, 0, sizeof(join_params));
1777         join_params_size = sizeof(join_params.ssid_le);
1778
1779         memcpy(&join_params.ssid_le.SSID, sme->ssid, profile->ssid.SSID_len);
1780         join_params.ssid_le.SSID_len = cpu_to_le32(profile->ssid.SSID_len);
1781
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);
1786
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         }
1792         err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
1793                                      &join_params, join_params_size);
1794         if (err)
1795                 brcmf_err("BRCMF_C_SET_SSID failed (%d)\n", err);
1796
1797 done:
1798         if (err)
1799                 clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
1800         brcmf_dbg(TRACE, "Exit\n");
1801         return err;
1802 }
1803
1804 static s32
1805 brcmf_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *ndev,
1806                        u16 reason_code)
1807 {
1808         struct brcmf_if *ifp = netdev_priv(ndev);
1809         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
1810         struct brcmf_scb_val_le scbval;
1811         s32 err = 0;
1812
1813         brcmf_dbg(TRACE, "Enter. Reason code = %d\n", reason_code);
1814         if (!check_vif_up(ifp->vif))
1815                 return -EIO;
1816
1817         clear_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
1818         cfg80211_disconnected(ndev, reason_code, NULL, 0, GFP_KERNEL);
1819
1820         memcpy(&scbval.ea, &profile->bssid, ETH_ALEN);
1821         scbval.val = cpu_to_le32(reason_code);
1822         err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC,
1823                                      &scbval, sizeof(scbval));
1824         if (err)
1825                 brcmf_err("error (%d)\n", err);
1826
1827         brcmf_dbg(TRACE, "Exit\n");
1828         return err;
1829 }
1830
1831 static s32
1832 brcmf_cfg80211_set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1833                             enum nl80211_tx_power_setting type, s32 mbm)
1834 {
1835
1836         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1837         struct net_device *ndev = cfg_to_ndev(cfg);
1838         struct brcmf_if *ifp = netdev_priv(ndev);
1839         u16 txpwrmw;
1840         s32 err = 0;
1841         s32 disable = 0;
1842         s32 dbm = MBM_TO_DBM(mbm);
1843
1844         brcmf_dbg(TRACE, "Enter\n");
1845         if (!check_vif_up(ifp->vif))
1846                 return -EIO;
1847
1848         switch (type) {
1849         case NL80211_TX_POWER_AUTOMATIC:
1850                 break;
1851         case NL80211_TX_POWER_LIMITED:
1852         case NL80211_TX_POWER_FIXED:
1853                 if (dbm < 0) {
1854                         brcmf_err("TX_POWER_FIXED - dbm is negative\n");
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;
1862         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_RADIO, disable);
1863         if (err)
1864                 brcmf_err("WLC_SET_RADIO error (%d)\n", err);
1865
1866         if (dbm > 0xffff)
1867                 txpwrmw = 0xffff;
1868         else
1869                 txpwrmw = (u16) dbm;
1870         err = brcmf_fil_iovar_int_set(ifp, "qtxpower",
1871                                       (s32)brcmf_mw_to_qdbm(txpwrmw));
1872         if (err)
1873                 brcmf_err("qtxpower error (%d)\n", err);
1874         cfg->conf->tx_power = dbm;
1875
1876 done:
1877         brcmf_dbg(TRACE, "Exit\n");
1878         return err;
1879 }
1880
1881 static s32 brcmf_cfg80211_get_tx_power(struct wiphy *wiphy,
1882                                        struct wireless_dev *wdev,
1883                                        s32 *dbm)
1884 {
1885         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1886         struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
1887         s32 txpwrdbm;
1888         u8 result;
1889         s32 err = 0;
1890
1891         brcmf_dbg(TRACE, "Enter\n");
1892         if (!check_vif_up(ifp->vif))
1893                 return -EIO;
1894
1895         err = brcmf_fil_iovar_int_get(ifp, "qtxpower", &txpwrdbm);
1896         if (err) {
1897                 brcmf_err("error (%d)\n", err);
1898                 goto done;
1899         }
1900
1901         result = (u8) (txpwrdbm & ~WL_TXPWR_OVERRIDE);
1902         *dbm = (s32) brcmf_qdbm_to_mw(result);
1903
1904 done:
1905         brcmf_dbg(TRACE, "Exit\n");
1906         return err;
1907 }
1908
1909 static s32
1910 brcmf_cfg80211_config_default_key(struct wiphy *wiphy, struct net_device *ndev,
1911                                u8 key_idx, bool unicast, bool multicast)
1912 {
1913         struct brcmf_if *ifp = netdev_priv(ndev);
1914         u32 index;
1915         u32 wsec;
1916         s32 err = 0;
1917
1918         brcmf_dbg(TRACE, "Enter\n");
1919         brcmf_dbg(CONN, "key index (%d)\n", key_idx);
1920         if (!check_vif_up(ifp->vif))
1921                 return -EIO;
1922
1923         err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
1924         if (err) {
1925                 brcmf_err("WLC_GET_WSEC error (%d)\n", err);
1926                 goto done;
1927         }
1928
1929         if (wsec & WEP_ENABLED) {
1930                 /* Just select a new current key */
1931                 index = key_idx;
1932                 err = brcmf_fil_cmd_int_set(ifp,
1933                                             BRCMF_C_SET_KEY_PRIMARY, index);
1934                 if (err)
1935                         brcmf_err("error (%d)\n", err);
1936         }
1937 done:
1938         brcmf_dbg(TRACE, "Exit\n");
1939         return err;
1940 }
1941
1942 static s32
1943 brcmf_add_keyext(struct wiphy *wiphy, struct net_device *ndev,
1944               u8 key_idx, const u8 *mac_addr, struct key_params *params)
1945 {
1946         struct brcmf_if *ifp = netdev_priv(ndev);
1947         struct brcmf_wsec_key key;
1948         s32 err = 0;
1949         u8 keybuf[8];
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 */
1961                 err = send_key_to_dongle(ndev, &key);
1962                 if (err)
1963                         brcmf_err("key delete error (%d)\n", err);
1964         } else {
1965                 if (key.len > sizeof(key.data)) {
1966                         brcmf_err("Invalid key length (%d)\n", key.len);
1967                         return -EINVAL;
1968                 }
1969
1970                 brcmf_dbg(CONN, "Setting the key index %d\n", key.index);
1971                 memcpy(key.data, params->key, key.len);
1972
1973                 if (!brcmf_is_apmode(ifp->vif) &&
1974                     (params->cipher == WLAN_CIPHER_SUITE_TKIP)) {
1975                         brcmf_dbg(CONN, "Swapping RX/TX MIC key\n");
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;
1995                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
1996                         break;
1997                 case WLAN_CIPHER_SUITE_WEP104:
1998                         key.algo = CRYPTO_ALGO_WEP128;
1999                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
2000                         break;
2001                 case WLAN_CIPHER_SUITE_TKIP:
2002                         key.algo = CRYPTO_ALGO_TKIP;
2003                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
2004                         break;
2005                 case WLAN_CIPHER_SUITE_AES_CMAC:
2006                         key.algo = CRYPTO_ALGO_AES_CCM;
2007                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
2008                         break;
2009                 case WLAN_CIPHER_SUITE_CCMP:
2010                         key.algo = CRYPTO_ALGO_AES_CCM;
2011                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_CCMP\n");
2012                         break;
2013                 default:
2014                         brcmf_err("Invalid cipher (0x%x)\n", params->cipher);
2015                         return -EINVAL;
2016                 }
2017                 err = send_key_to_dongle(ndev, &key);
2018                 if (err)
2019                         brcmf_err("wsec_key error (%d)\n", err);
2020         }
2021         return err;
2022 }
2023
2024 static s32
2025 brcmf_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 {
2029         struct brcmf_if *ifp = netdev_priv(ndev);
2030         struct brcmf_wsec_key key;
2031         s32 val;
2032         s32 wsec;
2033         s32 err = 0;
2034         u8 keybuf[8];
2035
2036         brcmf_dbg(TRACE, "Enter\n");
2037         brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2038         if (!check_vif_up(ifp->vif))
2039                 return -EIO;
2040
2041         if (mac_addr &&
2042                 (params->cipher != WLAN_CIPHER_SUITE_WEP40) &&
2043                 (params->cipher != WLAN_CIPHER_SUITE_WEP104)) {
2044                 brcmf_dbg(TRACE, "Exit");
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)) {
2053                 brcmf_err("Too long key length (%u)\n", key.len);
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;
2063                 val = WEP_ENABLED;
2064                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
2065                 break;
2066         case WLAN_CIPHER_SUITE_WEP104:
2067                 key.algo = CRYPTO_ALGO_WEP128;
2068                 val = WEP_ENABLED;
2069                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
2070                 break;
2071         case WLAN_CIPHER_SUITE_TKIP:
2072                 if (!brcmf_is_apmode(ifp->vif)) {
2073                         brcmf_dbg(CONN, "Swapping RX/TX MIC key\n");
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                 }
2078                 key.algo = CRYPTO_ALGO_TKIP;
2079                 val = TKIP_ENABLED;
2080                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
2081                 break;
2082         case WLAN_CIPHER_SUITE_AES_CMAC:
2083                 key.algo = CRYPTO_ALGO_AES_CCM;
2084                 val = AES_ENABLED;
2085                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
2086                 break;
2087         case WLAN_CIPHER_SUITE_CCMP:
2088                 key.algo = CRYPTO_ALGO_AES_CCM;
2089                 val = AES_ENABLED;
2090                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_CCMP\n");
2091                 break;
2092         default:
2093                 brcmf_err("Invalid cipher (0x%x)\n", params->cipher);
2094                 err = -EINVAL;
2095                 goto done;
2096         }
2097
2098         err = send_key_to_dongle(ndev, &key);
2099         if (err)
2100                 goto done;
2101
2102         err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
2103         if (err) {
2104                 brcmf_err("get wsec error (%d)\n", err);
2105                 goto done;
2106         }
2107         wsec |= val;
2108         err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
2109         if (err) {
2110                 brcmf_err("set wsec error (%d)\n", err);
2111                 goto done;
2112         }
2113
2114 done:
2115         brcmf_dbg(TRACE, "Exit\n");
2116         return err;
2117 }
2118
2119 static s32
2120 brcmf_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
2121                     u8 key_idx, bool pairwise, const u8 *mac_addr)
2122 {
2123         struct brcmf_if *ifp = netdev_priv(ndev);
2124         struct brcmf_wsec_key key;
2125         s32 err = 0;
2126
2127         brcmf_dbg(TRACE, "Enter\n");
2128         if (!check_vif_up(ifp->vif))
2129                 return -EIO;
2130
2131         if (key_idx >= DOT11_MAX_DEFAULT_KEYS) {
2132                 /* we ignore this key index in this case */
2133                 brcmf_err("invalid key index (%d)\n", key_idx);
2134                 return -EINVAL;
2135         }
2136
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
2143         brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2144
2145         /* Set the new key/index */
2146         err = send_key_to_dongle(ndev, &key);
2147
2148         brcmf_dbg(TRACE, "Exit\n");
2149         return err;
2150 }
2151
2152 static s32
2153 brcmf_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;
2158         struct brcmf_if *ifp = netdev_priv(ndev);
2159         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
2160         struct brcmf_cfg80211_security *sec;
2161         s32 wsec;
2162         s32 err = 0;
2163
2164         brcmf_dbg(TRACE, "Enter\n");
2165         brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2166         if (!check_vif_up(ifp->vif))
2167                 return -EIO;
2168
2169         memset(&params, 0, sizeof(params));
2170
2171         err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
2172         if (err) {
2173                 brcmf_err("WLC_GET_WSEC error (%d)\n", err);
2174                 /* Ignore this error, may happen during DISASSOC */
2175                 err = -EAGAIN;
2176                 goto done;
2177         }
2178         if (wsec & WEP_ENABLED) {
2179                 sec = &profile->sec;
2180                 if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP40) {
2181                         params.cipher = WLAN_CIPHER_SUITE_WEP40;
2182                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
2183                 } else if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP104) {
2184                         params.cipher = WLAN_CIPHER_SUITE_WEP104;
2185                         brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
2186                 }
2187         } else if (wsec & TKIP_ENABLED) {
2188                 params.cipher = WLAN_CIPHER_SUITE_TKIP;
2189                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
2190         } else if (wsec & AES_ENABLED) {
2191                 params.cipher = WLAN_CIPHER_SUITE_AES_CMAC;
2192                 brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
2193         } else  {
2194                 brcmf_err("Invalid algo (0x%x)\n", wsec);
2195                 err = -EINVAL;
2196                 goto done;
2197         }
2198         callback(cookie, &params);
2199
2200 done:
2201         brcmf_dbg(TRACE, "Exit\n");
2202         return err;
2203 }
2204
2205 static s32
2206 brcmf_cfg80211_config_default_mgmt_key(struct wiphy *wiphy,
2207                                     struct net_device *ndev, u8 key_idx)
2208 {
2209         brcmf_dbg(INFO, "Not supported\n");
2210
2211         return -EOPNOTSUPP;
2212 }
2213
2214 static s32
2215 brcmf_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev,
2216                            const u8 *mac, struct station_info *sinfo)
2217 {
2218         struct brcmf_if *ifp = netdev_priv(ndev);
2219         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
2220         struct brcmf_scb_val_le scb_val;
2221         int rssi;
2222         s32 rate;
2223         s32 err = 0;
2224         u8 *bssid = profile->bssid;
2225         struct brcmf_sta_info_le sta_info_le;
2226         u32 beacon_period;
2227         u32 dtim_period;
2228
2229         brcmf_dbg(TRACE, "Enter, MAC %pM\n", mac);
2230         if (!check_vif_up(ifp->vif))
2231                 return -EIO;
2232
2233         if (brcmf_is_apmode(ifp->vif)) {
2234                 memcpy(&sta_info_le, mac, ETH_ALEN);
2235                 err = brcmf_fil_iovar_data_get(ifp, "sta_info",
2236                                                &sta_info_le,
2237                                                sizeof(sta_info_le));
2238                 if (err < 0) {
2239                         brcmf_err("GET STA INFO failed, %d\n", err);
2240                         goto done;
2241                 }
2242                 sinfo->filled = STATION_INFO_INACTIVE_TIME;
2243                 sinfo->inactive_time = le32_to_cpu(sta_info_le.idle) * 1000;
2244                 if (le32_to_cpu(sta_info_le.flags) & BRCMF_STA_ASSOC) {
2245                         sinfo->filled |= STATION_INFO_CONNECTED_TIME;
2246                         sinfo->connected_time = le32_to_cpu(sta_info_le.in);
2247                 }
2248                 brcmf_dbg(TRACE, "STA idle time : %d ms, connected time :%d sec\n",
2249                           sinfo->inactive_time, sinfo->connected_time);
2250         } else if (ifp->vif->wdev.iftype == NL80211_IFTYPE_STATION) {
2251                 if (memcmp(mac, bssid, ETH_ALEN)) {
2252                         brcmf_err("Wrong Mac address cfg_mac-%pM wl_bssid-%pM\n",
2253                                   mac, bssid);
2254                         err = -ENOENT;
2255                         goto done;
2256                 }
2257                 /* Report the current tx rate */
2258                 err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_RATE, &rate);
2259                 if (err) {
2260                         brcmf_err("Could not get rate (%d)\n", err);
2261                         goto done;
2262                 } else {
2263                         sinfo->filled |= STATION_INFO_TX_BITRATE;
2264                         sinfo->txrate.legacy = rate * 5;
2265                         brcmf_dbg(CONN, "Rate %d Mbps\n", rate / 2);
2266                 }
2267
2268                 if (test_bit(BRCMF_VIF_STATUS_CONNECTED,
2269                              &ifp->vif->sme_state)) {
2270                         memset(&scb_val, 0, sizeof(scb_val));
2271                         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_RSSI,
2272                                                      &scb_val, sizeof(scb_val));
2273                         if (err) {
2274                                 brcmf_err("Could not get rssi (%d)\n", err);
2275                                 goto done;
2276                         } else {
2277                                 rssi = le32_to_cpu(scb_val.val);
2278                                 sinfo->filled |= STATION_INFO_SIGNAL;
2279                                 sinfo->signal = rssi;
2280                                 brcmf_dbg(CONN, "RSSI %d dBm\n", rssi);
2281                         }
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;
2306                 }
2307         } else
2308                 err = -EPERM;
2309 done:
2310         brcmf_dbg(TRACE, "Exit\n");
2311         return err;
2312 }
2313
2314 static s32
2315 brcmf_cfg80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *ndev,
2316                            bool enabled, s32 timeout)
2317 {
2318         s32 pm;
2319         s32 err = 0;
2320         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2321         struct brcmf_if *ifp = netdev_priv(ndev);
2322
2323         brcmf_dbg(TRACE, "Enter\n");
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
2329          * preference in cfg struct to apply this to
2330          * FW later while initializing the dongle
2331          */
2332         cfg->pwr_save = enabled;
2333         if (!check_vif_up(ifp->vif)) {
2334
2335                 brcmf_dbg(INFO, "Device is not ready, storing the value in cfg_info struct\n");
2336                 goto done;
2337         }
2338
2339         pm = enabled ? PM_FAST : PM_OFF;
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         }
2345         brcmf_dbg(INFO, "power save %s\n", (pm ? "enabled" : "disabled"));
2346
2347         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, pm);
2348         if (err) {
2349                 if (err == -ENODEV)
2350                         brcmf_err("net_device is not ready yet\n");
2351                 else
2352                         brcmf_err("error (%d)\n", err);
2353         }
2354 done:
2355         brcmf_dbg(TRACE, "Exit\n");
2356         return err;
2357 }
2358
2359 static s32 brcmf_inform_single_bss(struct brcmf_cfg80211_info *cfg,
2360                                    struct brcmf_bss_info_le *bi)
2361 {
2362         struct wiphy *wiphy = cfg_to_wiphy(cfg);
2363         struct ieee80211_channel *notify_channel;
2364         struct cfg80211_bss *bss;
2365         struct ieee80211_supported_band *band;
2366         struct brcmu_chan ch;
2367         u16 channel;
2368         u32 freq;
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) {
2376                 brcmf_err("Bss info is larger than buffer. Discarding\n");
2377                 return 0;
2378         }
2379
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;
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
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
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);
2406
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);
2414
2415         if (!bss)
2416                 return -ENOMEM;
2417
2418         cfg80211_put_bss(wiphy, bss);
2419
2420         return 0;
2421 }
2422
2423 static struct brcmf_bss_info_le *
2424 next_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
2432 static s32 brcmf_inform_bss(struct brcmf_cfg80211_info *cfg)
2433 {
2434         struct brcmf_scan_results *bss_list;
2435         struct brcmf_bss_info_le *bi = NULL;    /* must be initialized */
2436         s32 err = 0;
2437         int i;
2438
2439         bss_list = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
2440         if (bss_list->count != 0 &&
2441             bss_list->version != BRCMF_BSS_INFO_VERSION) {
2442                 brcmf_err("Version %d != WL_BSS_INFO_VERSION\n",
2443                           bss_list->version);
2444                 return -EOPNOTSUPP;
2445         }
2446         brcmf_dbg(SCAN, "scanned AP count (%d)\n", bss_list->count);
2447         for (i = 0; i < bss_list->count; i++) {
2448                 bi = next_bss_le(bss_list, bi);
2449                 err = brcmf_inform_single_bss(cfg, bi);
2450                 if (err)
2451                         break;
2452         }
2453         return err;
2454 }
2455
2456 static s32 wl_inform_ibss(struct brcmf_cfg80211_info *cfg,
2457                           struct net_device *ndev, const u8 *bssid)
2458 {
2459         struct wiphy *wiphy = cfg_to_wiphy(cfg);
2460         struct ieee80211_channel *notify_channel;
2461         struct brcmf_bss_info_le *bi = NULL;
2462         struct ieee80211_supported_band *band;
2463         struct cfg80211_bss *bss;
2464         struct brcmu_chan ch;
2465         u8 *buf = NULL;
2466         s32 err = 0;
2467         u32 freq;
2468         u16 notify_capability;
2469         u16 notify_interval;
2470         u8 *notify_ie;
2471         size_t notify_ielen;
2472         s32 notify_signal;
2473
2474         brcmf_dbg(TRACE, "Enter\n");
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
2484         err = brcmf_fil_cmd_data_get(netdev_priv(ndev), BRCMF_C_GET_BSS_INFO,
2485                                      buf, WL_BSS_INFO_MAX);
2486         if (err) {
2487                 brcmf_err("WLC_GET_BSS_INFO failed: %d\n", err);
2488                 goto CleanUp;
2489         }
2490
2491         bi = (struct brcmf_bss_info_le *)(buf + 4);
2492
2493         ch.chspec = le16_to_cpu(bi->chanspec);
2494         cfg->d11inf.decchspec(&ch);
2495
2496         if (ch.band == BRCMU_CHAN_BAND_2G)
2497                 band = wiphy->bands[IEEE80211_BAND_2GHZ];
2498         else
2499                 band = wiphy->bands[IEEE80211_BAND_5GHZ];
2500
2501         freq = ieee80211_channel_to_frequency(ch.chnum, band->band);
2502         notify_channel = ieee80211_get_channel(wiphy, freq);
2503
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
2510         brcmf_dbg(CONN, "channel: %d(%d)\n", ch.chnum, freq);
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);
2514
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);
2520
2521         if (!bss) {
2522                 err = -ENOMEM;
2523                 goto CleanUp;
2524         }
2525
2526         cfg80211_put_bss(wiphy, bss);
2527
2528 CleanUp:
2529
2530         kfree(buf);
2531
2532         brcmf_dbg(TRACE, "Exit\n");
2533
2534         return err;
2535 }
2536
2537 static s32 brcmf_update_bss_info(struct brcmf_cfg80211_info *cfg,
2538                                  struct brcmf_if *ifp)
2539 {
2540         struct brcmf_cfg80211_profile *profile = ndev_to_prof(ifp->ndev);
2541         struct brcmf_bss_info_le *bi;
2542         struct brcmf_ssid *ssid;
2543         const struct brcmf_tlv *tim;
2544         u16 beacon_interval;
2545         u8 dtim_period;
2546         size_t ie_len;
2547         u8 *ie;
2548         s32 err = 0;
2549
2550         brcmf_dbg(TRACE, "Enter\n");
2551         if (brcmf_is_ibssmode(ifp->vif))
2552                 return err;
2553
2554         ssid = &profile->ssid;
2555
2556         *(__le32 *)cfg->extra_buf = cpu_to_le32(WL_EXTRA_BUF_MAX);
2557         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
2558                                      cfg->extra_buf, WL_EXTRA_BUF_MAX);
2559         if (err) {
2560                 brcmf_err("Could not get bss info %d\n", err);
2561                 goto update_bss_info_out;
2562         }
2563
2564         bi = (struct brcmf_bss_info_le *)(cfg->extra_buf + 4);
2565         err = brcmf_inform_single_bss(cfg, bi);
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
2573         tim = brcmf_parse_tlvs(ie, ie_len, WLAN_EID_TIM);
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;
2583                 err = brcmf_fil_iovar_int_get(ifp, "dtim_assoc", &var);
2584                 if (err) {
2585                         brcmf_err("wl dtim_assoc failed (%d)\n", err);
2586                         goto update_bss_info_out;
2587                 }
2588                 dtim_period = (u8)var;
2589         }
2590
2591 update_bss_info_out:
2592         brcmf_dbg(TRACE, "Exit");
2593         return err;
2594 }
2595
2596 void brcmf_abort_scanning(struct brcmf_cfg80211_info *cfg)
2597 {
2598         struct escan_info *escan = &cfg->escan_info;
2599
2600         set_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
2601         if (cfg->scan_request) {
2602                 escan->escan_state = WL_ESCAN_STATE_IDLE;
2603                 brcmf_notify_escan_complete(cfg, escan->ifp, true, true);
2604         }
2605         clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
2606         clear_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
2607 }
2608
2609 static void brcmf_cfg80211_escan_timeout_worker(struct work_struct *work)
2610 {
2611         struct brcmf_cfg80211_info *cfg =
2612                         container_of(work, struct brcmf_cfg80211_info,
2613                                      escan_timeout_work);
2614
2615         brcmf_inform_bss(cfg);
2616         brcmf_notify_escan_complete(cfg, cfg->escan_info.ifp, true, true);
2617 }
2618
2619 static void brcmf_escan_timeout(unsigned long data)
2620 {
2621         struct brcmf_cfg80211_info *cfg =
2622                         (struct brcmf_cfg80211_info *)data;
2623
2624         if (cfg->scan_request) {
2625                 brcmf_err("timer expired\n");
2626                 schedule_work(&cfg->escan_timeout_work);
2627         }
2628 }
2629
2630 static s32
2631 brcmf_compare_update_same_bss(struct brcmf_cfg80211_info *cfg,
2632                               struct brcmf_bss_info_le *bss,
2633                               struct brcmf_bss_info_le *bss_info_le)
2634 {
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
2642         if (!memcmp(&bss_info_le->BSSID, &bss->BSSID, ETH_ALEN) &&
2643                 ch_bss.band == ch_bss_info_le.band &&
2644                 bss_info_le->SSID_len == bss->SSID_len &&
2645                 !memcmp(bss_info_le->SSID, bss->SSID, bss_info_le->SSID_len)) {
2646                 if ((bss->flags & BRCMF_BSS_RSSI_ON_CHANNEL) ==
2647                         (bss_info_le->flags & BRCMF_BSS_RSSI_ON_CHANNEL)) {
2648                         s16 bss_rssi = le16_to_cpu(bss->RSSI);
2649                         s16 bss_info_rssi = le16_to_cpu(bss_info_le->RSSI);
2650
2651                         /* preserve max RSSI if the measurements are
2652                         * both on-channel or both off-channel
2653                         */
2654                         if (bss_info_rssi > bss_rssi)
2655                                 bss->RSSI = bss_info_le->RSSI;
2656                 } else if ((bss->flags & BRCMF_BSS_RSSI_ON_CHANNEL) &&
2657                         (bss_info_le->flags & BRCMF_BSS_RSSI_ON_CHANNEL) == 0) {
2658                         /* preserve the on-channel rssi measurement
2659                         * if the new measurement is off channel
2660                         */
2661                         bss->RSSI = bss_info_le->RSSI;
2662                         bss->flags |= BRCMF_BSS_RSSI_ON_CHANNEL;
2663                 }
2664                 return 1;
2665         }
2666         return 0;
2667 }
2668
2669 static s32
2670 brcmf_cfg80211_escan_handler(struct brcmf_if *ifp,
2671                              const struct brcmf_event_msg *e, void *data)
2672 {
2673         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
2674         s32 status;
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;
2681         bool aborted;
2682
2683         status = e->status;
2684
2685         if (!test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
2686                 brcmf_err("scan not ready, bssidx=%d\n", ifp->bssidx);
2687                 return -EPERM;
2688         }
2689
2690         if (status == BRCMF_E_STATUS_PARTIAL) {
2691                 brcmf_dbg(SCAN, "ESCAN Partial result\n");
2692                 escan_result_le = (struct brcmf_escan_result_le *) data;
2693                 if (!escan_result_le) {
2694                         brcmf_err("Invalid escan result (NULL pointer)\n");
2695                         goto exit;
2696                 }
2697                 if (le16_to_cpu(escan_result_le->bss_count) != 1) {
2698                         brcmf_err("Invalid bss_count %d: ignoring\n",
2699                                   escan_result_le->bss_count);
2700                         goto exit;
2701                 }
2702                 bss_info_le = &escan_result_le->bss_info_le;
2703
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
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)) {
2715                         brcmf_err("Invalid bss_info length %d: ignoring\n",
2716                                   bi_length);
2717                         goto exit;
2718                 }
2719
2720                 if (!(cfg_to_wiphy(cfg)->interface_modes &
2721                                         BIT(NL80211_IFTYPE_ADHOC))) {
2722                         if (le16_to_cpu(bss_info_le->capability) &
2723                                                 WLAN_CAPABILITY_IBSS) {
2724                                 brcmf_err("Ignoring IBSS result\n");
2725                                 goto exit;
2726                         }
2727                 }
2728
2729                 list = (struct brcmf_scan_results *)
2730                                 cfg->escan_info.escan_buf;
2731                 if (bi_length > WL_ESCAN_BUF_SIZE - list->buflen) {
2732                         brcmf_err("Buffer is too small: ignoring\n");
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;
2740                         if (brcmf_compare_update_same_bss(cfg, bss,
2741                                                           bss_info_le))
2742                                 goto exit;
2743                 }
2744                 memcpy(&(cfg->escan_info.escan_buf[list->buflen]),
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 {
2750                 cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
2751                 if (brcmf_p2p_scan_finding_common_channel(cfg, NULL))
2752                         goto exit;
2753                 if (cfg->scan_request) {
2754                         brcmf_inform_bss(cfg);
2755                         aborted = status != BRCMF_E_STATUS_SUCCESS;
2756                         brcmf_notify_escan_complete(cfg, ifp, aborted, false);
2757                 } else
2758                         brcmf_dbg(SCAN, "Ignored scan complete result 0x%x\n",
2759                                   status);
2760         }
2761 exit:
2762         return 0;
2763 }
2764
2765 static void brcmf_init_escan(struct brcmf_cfg80211_info *cfg)
2766 {
2767         brcmf_fweh_register(cfg->pub, BRCMF_E_ESCAN_RESULT,
2768                             brcmf_cfg80211_escan_handler);
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);
2776 }
2777
2778 static __always_inline void brcmf_delay(u32 ms)
2779 {
2780         if (ms < 1000 / HZ) {
2781                 cond_resched();
2782                 mdelay(ms);
2783         } else {
2784                 msleep(ms);
2785         }
2786 }
2787
2788 static s32 brcmf_cfg80211_resume(struct wiphy *wiphy)
2789 {
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
2794         brcmf_dbg(TRACE, "Enter\n");
2795
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         }
2803         return 0;
2804 }
2805
2806 static 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
2832 static s32 brcmf_cfg80211_suspend(struct wiphy *wiphy,
2833                                   struct cfg80211_wowlan *wowl)
2834 {
2835         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2836         struct net_device *ndev = cfg_to_ndev(cfg);
2837         struct brcmf_if *ifp = netdev_priv(ndev);
2838         struct brcmf_cfg80211_vif *vif;
2839
2840         brcmf_dbg(TRACE, "Enter\n");
2841
2842         /* if the primary net_device is not READY there is nothing
2843          * we can do but pray resume goes smoothly.
2844          */
2845         if (!check_vif_up(ifp->vif))
2846                 goto exit;
2847
2848         /* end any scanning */
2849         if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
2850                 brcmf_abort_scanning(cfg);
2851
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         }
2875
2876 exit:
2877         brcmf_dbg(TRACE, "Exit\n");
2878         /* clear any scanning activity */
2879         cfg->scan_status = 0;
2880         return 0;
2881 }
2882
2883 static __used s32
2884 brcmf_update_pmklist(struct net_device *ndev,
2885                      struct brcmf_cfg80211_pmk_list *pmk_list, s32 err)
2886 {
2887         int i, j;
2888         int pmkid_len;
2889
2890         pmkid_len = le32_to_cpu(pmk_list->pmkids.npmkid);
2891
2892         brcmf_dbg(CONN, "No of elements %d\n", pmkid_len);
2893         for (i = 0; i < pmkid_len; i++) {
2894                 brcmf_dbg(CONN, "PMKID[%d]: %pM =\n", i,
2895                           &pmk_list->pmkids.pmkid[i].BSSID);
2896                 for (j = 0; j < WLAN_PMKID_LEN; j++)
2897                         brcmf_dbg(CONN, "%02x\n",
2898                                   pmk_list->pmkids.pmkid[i].PMKID[j]);
2899         }
2900
2901         if (!err)
2902                 brcmf_fil_iovar_data_set(netdev_priv(ndev), "pmkid_info",
2903                                          (char *)pmk_list, sizeof(*pmk_list));
2904
2905         return err;
2906 }
2907
2908 static s32
2909 brcmf_cfg80211_set_pmksa(struct wiphy *wiphy, struct net_device *ndev,
2910                          struct cfg80211_pmksa *pmksa)
2911 {
2912         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2913         struct brcmf_if *ifp = netdev_priv(ndev);
2914         struct pmkid_list *pmkids = &cfg->pmk_list->pmkids;
2915         s32 err = 0;
2916         int i;
2917         int pmkid_len;
2918
2919         brcmf_dbg(TRACE, "Enter\n");
2920         if (!check_vif_up(ifp->vif))
2921                 return -EIO;
2922
2923         pmkid_len = le32_to_cpu(pmkids->npmkid);
2924         for (i = 0; i < pmkid_len; i++)
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);
2930                 if (i == pmkid_len) {
2931                         pmkid_len++;
2932                         pmkids->npmkid = cpu_to_le32(pmkid_len);
2933                 }
2934         } else
2935                 err = -EINVAL;
2936
2937         brcmf_dbg(CONN, "set_pmksa,IW_PMKSA_ADD - PMKID: %pM =\n",
2938                   pmkids->pmkid[pmkid_len].BSSID);
2939         for (i = 0; i < WLAN_PMKID_LEN; i++)
2940                 brcmf_dbg(CONN, "%02x\n", pmkids->pmkid[pmkid_len].PMKID[i]);
2941
2942         err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
2943
2944         brcmf_dbg(TRACE, "Exit\n");
2945         return err;
2946 }
2947
2948 static s32
2949 brcmf_cfg80211_del_pmksa(struct wiphy *wiphy, struct net_device *ndev,
2950                       struct cfg80211_pmksa *pmksa)
2951 {
2952         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2953         struct brcmf_if *ifp = netdev_priv(ndev);
2954         struct pmkid_list pmkid;
2955         s32 err = 0;
2956         int i, pmkid_len;
2957
2958         brcmf_dbg(TRACE, "Enter\n");
2959         if (!check_vif_up(ifp->vif))
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
2965         brcmf_dbg(CONN, "del_pmksa,IW_PMKSA_REMOVE - PMKID: %pM =\n",
2966                   &pmkid.pmkid[0].BSSID);
2967         for (i = 0; i < WLAN_PMKID_LEN; i++)
2968                 brcmf_dbg(CONN, "%02x\n", pmkid.pmkid[0].PMKID[i]);
2969
2970         pmkid_len = le32_to_cpu(cfg->pmk_list->pmkids.npmkid);
2971         for (i = 0; i < pmkid_len; i++)
2972                 if (!memcmp
2973                     (pmksa->bssid, &cfg->pmk_list->pmkids.pmkid[i].BSSID,
2974                      ETH_ALEN))
2975                         break;
2976
2977         if ((pmkid_len > 0)
2978             && (i < pmkid_len)) {
2979                 memset(&cfg->pmk_list->pmkids.pmkid[i], 0,
2980                        sizeof(struct pmkid));
2981                 for (; i < (pmkid_len - 1); i++) {
2982                         memcpy(&cfg->pmk_list->pmkids.pmkid[i].BSSID,
2983                                &cfg->pmk_list->pmkids.pmkid[i + 1].BSSID,
2984                                ETH_ALEN);
2985                         memcpy(&cfg->pmk_list->pmkids.pmkid[i].PMKID,
2986                                &cfg->pmk_list->pmkids.pmkid[i + 1].PMKID,
2987                                WLAN_PMKID_LEN);
2988                 }
2989                 cfg->pmk_list->pmkids.npmkid = cpu_to_le32(pmkid_len - 1);
2990         } else
2991                 err = -EINVAL;
2992
2993         err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
2994
2995         brcmf_dbg(TRACE, "Exit\n");
2996         return err;
2997
2998 }
2999
3000 static s32
3001 brcmf_cfg80211_flush_pmksa(struct wiphy *wiphy, struct net_device *ndev)
3002 {
3003         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3004         struct brcmf_if *ifp = netdev_priv(ndev);
3005         s32 err = 0;
3006
3007         brcmf_dbg(TRACE, "Enter\n");
3008         if (!check_vif_up(ifp->vif))
3009                 return -EIO;
3010
3011         memset(cfg->pmk_list, 0, sizeof(*cfg->pmk_list));
3012         err = brcmf_update_pmklist(ndev, cfg->pmk_list, err);
3013
3014         brcmf_dbg(TRACE, "Exit\n");
3015         return err;
3016
3017 }
3018
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  */
3027 static s32
3028 brcmf_notify_sched_scan_results(struct brcmf_if *ifp,
3029                                 const struct brcmf_event_msg *e, void *data)
3030 {
3031         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
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;
3036         struct wiphy *wiphy = cfg_to_wiphy(cfg);
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
3044         brcmf_dbg(SCAN, "Enter\n");
3045
3046         if (e->event_code == BRCMF_E_PFN_NET_LOST) {
3047                 brcmf_dbg(SCAN, "PFN NET LOST event. Do Nothing\n");
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);
3060         brcmf_dbg(SCAN, "PFN NET FOUND event. count: %d\n", result_count);
3061         if (result_count > 0) {
3062                 int i;
3063
3064                 request = kzalloc(sizeof(*request), GFP_KERNEL);
3065                 ssid = kcalloc(result_count, sizeof(*ssid), GFP_KERNEL);
3066                 channel = kcalloc(result_count, sizeof(*channel), GFP_KERNEL);
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) {
3079                                 brcmf_err("Invalid netinfo ptr. index: %d\n",
3080                                           i);
3081                                 err = -EINVAL;
3082                                 goto out_err;
3083                         }
3084
3085                         brcmf_dbg(SCAN, "SSID:%s Channel:%d\n",
3086                                   netinfo->SSID, netinfo->channel);
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
3109                 if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
3110                         /* Abort any on-going scan */
3111                         brcmf_abort_scanning(cfg);
3112                 }
3113
3114                 set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
3115                 cfg->escan_info.run = brcmf_run_escan;
3116                 err = brcmf_do_escan(cfg, wiphy, ifp, request);
3117                 if (err) {
3118                         clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
3119                         goto out_err;
3120                 }
3121                 cfg->sched_escan = true;
3122                 cfg->scan_request = request;
3123         } else {
3124                 brcmf_err("FALSE PNO Event. (pfn_count == 0)\n");
3125                 goto out_err;
3126         }
3127
3128         kfree(ssid);
3129         kfree(channel);
3130         kfree(request);
3131         return 0;
3132
3133 out_err:
3134         kfree(ssid);
3135         kfree(channel);
3136         kfree(request);
3137         cfg80211_sched_scan_stopped(wiphy);
3138         return err;
3139 }
3140
3141 static int brcmf_dev_pno_clean(struct net_device *ndev)
3142 {
3143         int ret;
3144
3145         /* Disable pfn */
3146         ret = brcmf_fil_iovar_int_set(netdev_priv(ndev), "pfn", 0);
3147         if (ret == 0) {
3148                 /* clear pfn */
3149                 ret = brcmf_fil_iovar_data_set(netdev_priv(ndev), "pfnclear",
3150                                                NULL, 0);
3151         }
3152         if (ret < 0)
3153                 brcmf_err("failed code %d\n", ret);
3154
3155         return ret;
3156 }
3157
3158 static int brcmf_dev_pno_config(struct net_device *ndev)
3159 {
3160         struct brcmf_pno_param_le pfn_param;
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
3173         return brcmf_fil_iovar_data_set(netdev_priv(ndev), "pfn_set",
3174                                         &pfn_param, sizeof(pfn_param));
3175 }
3176
3177 static int
3178 brcmf_cfg80211_sched_scan_start(struct wiphy *wiphy,
3179                                 struct net_device *ndev,
3180                                 struct cfg80211_sched_scan_request *request)
3181 {
3182         struct brcmf_if *ifp = netdev_priv(ndev);
3183         struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
3184         struct brcmf_pno_net_param_le pfn;
3185         int i;
3186         int ret = 0;
3187
3188         brcmf_dbg(SCAN, "Enter n_match_sets:%d n_ssids:%d\n",
3189                   request->n_match_sets, request->n_ssids);
3190         if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
3191                 brcmf_err("Scanning already: status (%lu)\n", cfg->scan_status);
3192                 return -EAGAIN;
3193         }
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         }
3199
3200         if (!request->n_ssids || !request->n_match_sets) {
3201                 brcmf_dbg(SCAN, "Invalid sched scan req!! n_ssids:%d\n",
3202                           request->n_ssids);
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 */
3209                         brcmf_dbg(SCAN, ">>> Active scan req for ssid (%s)\n",
3210                                   request->ssids[i].ssid);
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) {
3223                         brcmf_err("failed error=%d\n", ret);
3224                         return ret;
3225                 }
3226
3227                 /* configure pno */
3228                 ret = brcmf_dev_pno_config(ndev);
3229                 if (ret < 0) {
3230                         brcmf_err("PNO setup failed!! ret=%d\n", ret);
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) {
3243                                 brcmf_err("skip broadcast ssid\n");
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);
3253                         ret = brcmf_fil_iovar_data_set(ifp, "pfn_add", &pfn,
3254                                                        sizeof(pfn));
3255                         brcmf_dbg(SCAN, ">>> PNO filter %s for ssid (%s)\n",
3256                                   ret == 0 ? "set" : "failed", ssid->ssid);
3257                 }
3258                 /* Enable the PNO */
3259                 if (brcmf_fil_iovar_int_set(ifp, "pfn", 1) < 0) {
3260                         brcmf_err("PNO enable failed!! ret=%d\n", ret);
3261                         return -EINVAL;
3262                 }
3263         } else {
3264                 return -EINVAL;
3265         }
3266
3267         return 0;
3268 }
3269
3270 static int brcmf_cfg80211_sched_scan_stop(struct wiphy *wiphy,
3271                                           struct net_device *ndev)
3272 {
3273         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3274
3275         brcmf_dbg(SCAN, "enter\n");
3276         brcmf_dev_pno_clean(ndev);
3277         if (cfg->sched_escan)
3278                 brcmf_notify_escan_complete(cfg, netdev_priv(ndev), true, true);
3279         return 0;
3280 }
3281
3282 static s32 brcmf_configure_opensecurity(struct brcmf_if *ifp)
3283 {
3284         s32 err;
3285
3286         /* set auth */
3287         err = brcmf_fil_bsscfg_int_set(ifp, "auth", 0);
3288         if (err < 0) {
3289                 brcmf_err("auth error %d\n", err);
3290                 return err;
3291         }
3292         /* set wsec */
3293         err = brcmf_fil_bsscfg_int_set(ifp, "wsec", 0);
3294         if (err < 0) {
3295                 brcmf_err("wsec error %d\n", err);
3296                 return err;
3297         }
3298         /* set upper-layer auth */
3299         err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", WPA_AUTH_NONE);
3300         if (err < 0) {
3301                 brcmf_err("wpa_auth error %d\n", err);
3302                 return err;
3303         }
3304
3305         return 0;
3306 }
3307
3308 static 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
3316 static s32
3317 brcmf_configure_wpaie(struct net_device *ndev,
3318                       const struct brcmf_vs_tlv *wpa_ie,
3319                       bool is_rsn_ie)
3320 {
3321         struct brcmf_if *ifp = netdev_priv(ndev);
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
3336         brcmf_dbg(TRACE, "Enter\n");
3337         if (wpa_ie == NULL)
3338                 goto exit;
3339
3340         len = wpa_ie->len + TLV_HDR_LEN;
3341         data = (u8 *)wpa_ie;
3342         offset = TLV_HDR_LEN;
3343         if (!is_rsn_ie)
3344                 offset += VS_IE_FIXED_HDR_LEN;
3345         else
3346                 offset += WPA_IE_VERSION_LEN;
3347
3348         /* check for multicast cipher suite */
3349         if (offset + WPA_IE_MIN_OUI_LEN > len) {
3350                 err = -EINVAL;
3351                 brcmf_err("no multicast cipher suite\n");
3352                 goto exit;
3353         }
3354
3355         if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
3356                 err = -EINVAL;
3357                 brcmf_err("ivalid OUI\n");
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;
3379                 brcmf_err("Invalid multi cast cipher info\n");
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;
3390                 brcmf_err("no unicast cipher suite\n");
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;
3396                         brcmf_err("ivalid OUI\n");
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:
3414                         brcmf_err("Ivalid unicast security info\n");
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;
3424                 brcmf_err("no auth key mgmt suite\n");
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;
3430                         brcmf_err("ivalid OUI\n");
3431                         goto exit;
3432                 }
3433                 offset += TLV_OUI_LEN;
3434                 switch (data[offset]) {
3435                 case RSN_AKM_NONE:
3436                         brcmf_dbg(TRACE, "RSN_AKM_NONE\n");
3437                         wpa_auth |= WPA_AUTH_NONE;
3438                         break;
3439                 case RSN_AKM_UNSPECIFIED:
3440                         brcmf_dbg(TRACE, "RSN_AKM_UNSPECIFIED\n");
3441                         is_rsn_ie ? (wpa_auth |= WPA2_AUTH_UNSPECIFIED) :
3442                                     (wpa_auth |= WPA_AUTH_UNSPECIFIED);
3443                         break;
3444                 case RSN_AKM_PSK:
3445                         brcmf_dbg(TRACE, "RSN_AKM_PSK\n");
3446                         is_rsn_ie ? (wpa_auth |= WPA2_AUTH_PSK) :
3447                                     (wpa_auth |= WPA_AUTH_PSK);
3448                         break;
3449                 default:
3450                         brcmf_err("Ivalid key mgmt info\n");
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 */
3463                 err = brcmf_fil_bsscfg_int_set(ifp, "wme_bss_disable",
3464                                                wme_bss_disable);
3465                 if (err < 0) {
3466                         brcmf_err("wme_bss_disable error %d\n", err);
3467                         goto exit;
3468                 }
3469         }
3470         /* FOR WPS , set SES_OW_ENABLED */
3471         wsec = (pval | gval | SES_OW_ENABLED);
3472
3473         /* set auth */
3474         err = brcmf_fil_bsscfg_int_set(ifp, "auth", auth);
3475         if (err < 0) {
3476                 brcmf_err("auth error %d\n", err);
3477                 goto exit;
3478         }
3479         /* set wsec */
3480         err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
3481         if (err < 0) {
3482                 brcmf_err("wsec error %d\n", err);
3483                 goto exit;
3484         }
3485         /* set upper-layer auth */
3486         err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_auth);
3487         if (err < 0) {
3488                 brcmf_err("wpa_auth error %d\n", err);
3489                 goto exit;
3490         }
3491
3492 exit:
3493         return err;
3494 }
3495
3496 static s32
3497 brcmf_parse_vndr_ies(const u8 *vndr_ie_buf, u32 vndr_ie_len,
3498                      struct parsed_vndr_ies *vndr_ies)
3499 {
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)) {
3515                         brcmf_err("invalid vndr ie. length is too small %d\n",
3516                                   vndrie->len);
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))) {
3523                         brcmf_dbg(TRACE, "Found WPA/WME oui. Do not add it\n");
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
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);
3541
3542                 if (vndr_ies->count >= VNDR_IE_PARSE_LIMIT)
3543                         break;
3544 next:
3545                 remaining_len -= (ie->len + TLV_HDR_LEN);
3546                 if (remaining_len <= TLV_HDR_LEN)
3547                         ie = NULL;
3548                 else
3549                         ie = (struct brcmf_tlv *)(((u8 *)ie) + ie->len +
3550                                 TLV_HDR_LEN);
3551         }
3552         return 0;
3553 }
3554
3555 static u32
3556 brcmf_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
3576 s32 brcmf_vif_set_mgmt_ie(struct brcmf_cfg80211_vif *vif, s32 pktflag,
3577                           const u8 *vndr_ie_buf, u32 vndr_ie_len)
3578 {
3579         struct brcmf_if *ifp;
3580         struct vif_saved_ie *saved_ie;
3581         s32 err = 0;
3582         u8  *iovar_ie_buf;
3583         u8  *curr_ie_buf;
3584         u8  *mgmt_ie_buf = NULL;
3585         int mgmt_ie_buf_len;
3586         u32 *mgmt_ie_len;
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;
3595         int remained_buf_len;
3596
3597         if (!vif)
3598                 return -ENODEV;
3599         ifp = vif->ifp;
3600         saved_ie = &vif->saved_ie;
3601
3602         brcmf_dbg(TRACE, "bssidx %d, pktflag : 0x%02X\n", ifp->bssidx, pktflag);
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;
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;
3632         }
3633
3634         if (vndr_ie_len > mgmt_ie_buf_len) {
3635                 err = -ENOMEM;
3636                 brcmf_err("extra IE size too big\n");
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
3652         if (mgmt_ie_buf && *mgmt_ie_len) {
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)) {
3656                         brcmf_dbg(TRACE, "Previous mgmt IE equals to current IE\n");
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
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]);
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
3694                         /* verify remained buf size before copy data */
3695                         if (remained_buf_len < (vndrie_info->vndrie.len +
3696                                                         VNDR_IE_VSIE_OFFSET)) {
3697                                 brcmf_err("no space in mgmt_ie_buf: len left %d",
3698                                           remained_buf_len);
3699                                 break;
3700                         }
3701                         remained_buf_len -= (vndrie_info->ie_len +
3702                                              VNDR_IE_VSIE_OFFSET);
3703
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]);
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");
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) {
3726                 err  = brcmf_fil_bsscfg_data_set(ifp, "vndr_ie", iovar_ie_buf,
3727                                                  total_ie_buf_len);
3728                 if (err)
3729                         brcmf_err("vndr ie set error : %d\n", err);
3730         }
3731
3732 exit:
3733         kfree(iovar_ie_buf);
3734         return err;
3735 }
3736
3737 s32 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
3753 static s32
3754 brcmf_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
3780 static s32
3781 brcmf_cfg80211_start_ap(struct wiphy *wiphy, struct net_device *ndev,
3782                         struct cfg80211_ap_settings *settings)
3783 {
3784         s32 ie_offset;
3785         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3786         struct brcmf_if *ifp = netdev_priv(ndev);
3787         const struct brcmf_tlv *ssid_ie;
3788         struct brcmf_ssid_le ssid_le;
3789         s32 err = -EPERM;
3790         const struct brcmf_tlv *rsn_ie;
3791         const struct brcmf_vs_tlv *wpa_ie;
3792         struct brcmf_join_params join_params;
3793         enum nl80211_iftype dev_role;
3794         struct brcmf_fil_bss_enable_le bss_enable;
3795         u16 chanspec;
3796
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,
3800                   settings->beacon_interval, settings->dtim_period);
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);
3804
3805         dev_role = ifp->vif->wdev.iftype;
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);
3819                 brcmf_dbg(TRACE, "SSID is (%s) in Head\n", ssid_le.SSID);
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
3825         brcmf_set_mpc(ifp, 0);
3826         brcmf_configure_arp_offload(ifp, false);
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
3836         if ((wpa_ie != NULL || rsn_ie != NULL)) {
3837                 brcmf_dbg(TRACE, "WPA(2) IE is found\n");
3838                 if (wpa_ie != NULL) {
3839                         /* WPA IE */
3840                         err = brcmf_configure_wpaie(ndev, wpa_ie, false);
3841                         if (err < 0)
3842                                 goto exit;
3843                 } else {
3844                         /* RSN IE */
3845                         err = brcmf_configure_wpaie(ndev,
3846                                 (struct brcmf_vs_tlv *)rsn_ie, true);
3847                         if (err < 0)
3848                                 goto exit;
3849                 }
3850         } else {
3851                 brcmf_dbg(TRACE, "No WPA(2) IEs found\n");
3852                 brcmf_configure_opensecurity(ifp);
3853         }
3854
3855         brcmf_config_ap_mgmt_ie(ifp->vif, &settings->beacon);
3856
3857         chanspec = chandef_to_chanspec(&cfg->d11inf, &settings->chandef);
3858         err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
3859         if (err < 0) {
3860                 brcmf_err("Set Channel failed: chspec=%d, %d\n", chanspec, err);
3861                 goto exit;
3862         }
3863
3864         if (settings->beacon_interval) {
3865                 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD,
3866                                             settings->beacon_interval);
3867                 if (err < 0) {
3868                         brcmf_err("Beacon Interval Set Error, %d\n", err);
3869                         goto exit;
3870                 }
3871         }
3872         if (settings->dtim_period) {
3873                 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_DTIMPRD,
3874                                             settings->dtim_period);
3875                 if (err < 0) {
3876                         brcmf_err("DTIM Interval Set Error, %d\n", err);
3877                         goto exit;
3878                 }
3879         }
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                 }
3887                 brcmf_fil_iovar_int_set(ifp, "apsta", 0);
3888         }
3889
3890         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 1);
3891         if (err < 0) {
3892                 brcmf_err("SET INFRA error %d\n", err);
3893                 goto exit;
3894         }
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         }
3936         clear_bit(BRCMF_VIF_STATUS_AP_CREATING, &ifp->vif->sme_state);
3937         set_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
3938
3939 exit:
3940         if (err) {
3941                 brcmf_set_mpc(ifp, 1);
3942                 brcmf_configure_arp_offload(ifp, true);
3943         }
3944         return err;
3945 }
3946
3947 static int brcmf_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *ndev)
3948 {
3949         struct brcmf_if *ifp = netdev_priv(ndev);
3950         s32 err;
3951         struct brcmf_fil_bss_enable_le bss_enable;
3952         struct brcmf_join_params join_params;
3953
3954         brcmf_dbg(TRACE, "Enter\n");
3955
3956         if (ifp->vif->wdev.iftype == NL80211_IFTYPE_AP) {
3957                 /* Due to most likely deauths outstanding we sleep */
3958                 /* first to make sure they get processed by fw. */
3959                 msleep(400);
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);
3966                 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
3967                 if (err < 0)
3968                         brcmf_err("BRCMF_C_UP error %d\n", err);
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);
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);
3982         }
3983         brcmf_set_mpc(ifp, 1);
3984         brcmf_configure_arp_offload(ifp, true);
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
3988         return err;
3989 }
3990
3991 static s32
3992 brcmf_cfg80211_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
3993                              struct cfg80211_beacon_data *info)
3994 {
3995         struct brcmf_if *ifp = netdev_priv(ndev);
3996         s32 err;
3997
3998         brcmf_dbg(TRACE, "Enter\n");
3999
4000         err = brcmf_config_ap_mgmt_ie(ifp->vif, info);
4001
4002         return err;
4003 }
4004
4005 static int
4006 brcmf_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev,
4007                            const u8 *mac)
4008 {
4009         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4010         struct brcmf_scb_val_le scbval;
4011         struct brcmf_if *ifp = netdev_priv(ndev);
4012         s32 err;
4013
4014         if (!mac)
4015                 return -EFAULT;
4016
4017         brcmf_dbg(TRACE, "Enter %pM\n", mac);
4018
4019         if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
4020                 ifp = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
4021         if (!check_vif_up(ifp->vif))
4022                 return -EIO;
4023
4024         memcpy(&scbval.ea, mac, ETH_ALEN);
4025         scbval.val = cpu_to_le32(WLAN_REASON_DEAUTH_LEAVING);
4026         err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCB_DEAUTHENTICATE_FOR_REASON,
4027                                      &scbval, sizeof(scbval));
4028         if (err)
4029                 brcmf_err("SCB_DEAUTHENTICATE_FOR_REASON failed %d\n", err);
4030
4031         brcmf_dbg(TRACE, "Exit\n");
4032         return err;
4033 }
4034
4035
4036 static void
4037 brcmf_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
4038                                    struct wireless_dev *wdev,
4039                                    u16 frame_type, bool reg)
4040 {
4041         struct brcmf_cfg80211_vif *vif;
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;
4047         vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4048         if (reg)
4049                 vif->mgmt_rx_reg |= BIT(mgmt_type);
4050         else
4051                 vif->mgmt_rx_reg &= ~BIT(mgmt_type);
4052 }
4053
4054
4055 static int
4056 brcmf_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
4057                        struct cfg80211_mgmt_tx_params *params, u64 *cookie)
4058 {
4059         struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4060         struct ieee80211_channel *chan = params->chan;
4061         const u8 *buf = params->buf;
4062         size_t len = params->len;
4063         const struct ieee80211_mgmt *mgmt;
4064         struct brcmf_cfg80211_vif *vif;
4065         s32 err = 0;
4066         s32 ie_offset;
4067         s32 ie_len;
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;
4072         u32 freq;
4073
4074         brcmf_dbg(TRACE, "Enter\n");
4075
4076         *cookie = 0;
4077
4078         mgmt = (const struct ieee80211_mgmt *)buf;
4079
4080         if (!ieee80211_is_mgmt(mgmt->frame_control)) {
4081                 brcmf_err("Driver only allows MGMT packet type\n");
4082                 return -EPERM;
4083         }
4084
4085         vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
4086
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;
4102                 if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif)
4103                         vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
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);
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);
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);
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",
4140                           *cookie, le16_to_cpu(action_frame->len), freq);
4141
4142                 ack = brcmf_p2p_send_action_frame(cfg, cfg_to_ndev(cfg),
4143                                                   af_params);
4144
4145                 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, ack,
4146                                         GFP_KERNEL);
4147                 kfree(af_params);
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);
4151         }
4152
4153 exit:
4154         return err;
4155 }
4156
4157
4158 static int
4159 brcmf_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);
4176 exit:
4177         return err;
4178 }
4179
4180 static 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
4201 static 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
4213 static s32
4214 brcmf_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
4234 static 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
4255 static int brcmf_cfg80211_tdls_oper(struct wiphy *wiphy,
4256                                     struct net_device *ndev, const u8 *peer,
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
4281 static struct cfg80211_ops wl_cfg80211_ops = {
4282         .add_virtual_intf = brcmf_cfg80211_add_iface,
4283         .del_virtual_intf = brcmf_cfg80211_del_iface,
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,
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,
4304         .flush_pmksa = brcmf_cfg80211_flush_pmksa,
4305         .start_ap = brcmf_cfg80211_start_ap,
4306         .stop_ap = brcmf_cfg80211_stop_ap,
4307         .change_beacon = brcmf_cfg80211_change_beacon,
4308         .del_station = brcmf_cfg80211_del_station,
4309         .sched_scan_start = brcmf_cfg80211_sched_scan_start,
4310         .sched_scan_stop = brcmf_cfg80211_sched_scan_stop,
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,
4315         .start_p2p_device = brcmf_p2p_start_device,
4316         .stop_p2p_device = brcmf_p2p_stop_device,
4317         .crit_proto_start = brcmf_cfg80211_crit_proto_start,
4318         .crit_proto_stop = brcmf_cfg80211_crit_proto_stop,
4319         .tdls_oper = brcmf_cfg80211_tdls_oper,
4320 };
4321
4322 struct brcmf_cfg80211_vif *brcmf_alloc_vif(struct brcmf_cfg80211_info *cfg,
4323                                            enum nl80211_iftype type,
4324                                            bool pm_block)
4325 {
4326         struct brcmf_cfg80211_vif *vif;
4327
4328         brcmf_dbg(TRACE, "allocating virtual interface (size=%zu)\n",
4329                   sizeof(*vif));
4330         vif = kzalloc(sizeof(*vif), GFP_KERNEL);
4331         if (!vif)
4332                 return ERR_PTR(-ENOMEM);
4333
4334         vif->wdev.wiphy = cfg->wiphy;
4335         vif->wdev.iftype = type;
4336
4337         vif->pm_block = pm_block;
4338         vif->roam_off = -1;
4339
4340         brcmf_init_prof(&vif->profile);
4341
4342         list_add_tail(&vif->list, &cfg->vif_list);
4343         return vif;
4344 }
4345
4346 void brcmf_free_vif(struct brcmf_cfg80211_vif *vif)
4347 {
4348         list_del(&vif->list);
4349         kfree(vif);
4350 }
4351
4352 void 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
4364 static bool brcmf_is_linkup(const struct brcmf_event_msg *e)
4365 {
4366         u32 event = e->event_code;
4367         u32 status = e->status;
4368
4369         if (event == BRCMF_E_SET_SSID && status == BRCMF_E_STATUS_SUCCESS) {
4370                 brcmf_dbg(CONN, "Processing set ssid\n");
4371                 return true;
4372         }
4373
4374         return false;
4375 }
4376
4377 static bool brcmf_is_linkdown(const struct brcmf_event_msg *e)
4378 {
4379         u32 event = e->event_code;
4380         u16 flags = e->flags;
4381
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)))) {
4385                 brcmf_dbg(CONN, "Processing link down\n");
4386                 return true;
4387         }
4388         return false;
4389 }
4390
4391 static bool brcmf_is_nonetwork(struct brcmf_cfg80211_info *cfg,
4392                                const struct brcmf_event_msg *e)
4393 {
4394         u32 event = e->event_code;
4395         u32 status = e->status;
4396
4397         if (event == BRCMF_E_LINK && status == BRCMF_E_STATUS_NO_NETWORKS) {
4398                 brcmf_dbg(CONN, "Processing Link %s & no network found\n",
4399                           e->flags & BRCMF_EVENT_MSG_LINK ? "up" : "down");
4400                 return true;
4401         }
4402
4403         if (event == BRCMF_E_SET_SSID && status != BRCMF_E_STATUS_SUCCESS) {
4404                 brcmf_dbg(CONN, "Processing connecting & no network found\n");
4405                 return true;
4406         }
4407
4408         return false;
4409 }
4410
4411 static void brcmf_clear_assoc_ies(struct brcmf_cfg80211_info *cfg)
4412 {
4413         struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
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
4423 static s32 brcmf_get_assoc_ies(struct brcmf_cfg80211_info *cfg,
4424                                struct brcmf_if *ifp)
4425 {
4426         struct brcmf_cfg80211_assoc_ielen_le *assoc_info;
4427         struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
4428         u32 req_len;
4429         u32 resp_len;
4430         s32 err = 0;
4431
4432         brcmf_clear_assoc_ies(cfg);
4433
4434         err = brcmf_fil_iovar_data_get(ifp, "assoc_info",
4435                                        cfg->extra_buf, WL_ASSOC_INFO_MAX);
4436         if (err) {
4437                 brcmf_err("could not get assoc info (%d)\n", err);
4438                 return err;
4439         }
4440         assoc_info =
4441                 (struct brcmf_cfg80211_assoc_ielen_le *)cfg->extra_buf;
4442         req_len = le32_to_cpu(assoc_info->req_len);
4443         resp_len = le32_to_cpu(assoc_info->resp_len);
4444         if (req_len) {
4445                 err = brcmf_fil_iovar_data_get(ifp, "assoc_req_ies",
4446                                                cfg->extra_buf,
4447                                                WL_ASSOC_INFO_MAX);
4448                 if (err) {
4449                         brcmf_err("could not get assoc req (%d)\n", err);
4450                         return err;
4451                 }
4452                 conn_info->req_ie_len = req_len;
4453                 conn_info->req_ie =
4454                     kmemdup(cfg->extra_buf, conn_info->req_ie_len,
4455                             GFP_KERNEL);
4456         } else {
4457                 conn_info->req_ie_len = 0;
4458                 conn_info->req_ie = NULL;
4459         }
4460         if (resp_len) {
4461                 err = brcmf_fil_iovar_data_get(ifp, "assoc_resp_ies",
4462                                                cfg->extra_buf,
4463                                                WL_ASSOC_INFO_MAX);
4464                 if (err) {
4465                         brcmf_err("could not get assoc resp (%d)\n", err);
4466                         return err;
4467                 }
4468                 conn_info->resp_ie_len = resp_len;
4469                 conn_info->resp_ie =
4470                     kmemdup(cfg->extra_buf, conn_info->resp_ie_len,
4471                             GFP_KERNEL);
4472         } else {
4473                 conn_info->resp_ie_len = 0;
4474                 conn_info->resp_ie = NULL;
4475         }
4476         brcmf_dbg(CONN, "req len (%d) resp len (%d)\n",
4477                   conn_info->req_ie_len, conn_info->resp_ie_len);
4478
4479         return err;
4480 }
4481
4482 static s32
4483 brcmf_bss_roaming_done(struct brcmf_cfg80211_info *cfg,
4484                        struct net_device *ndev,
4485                        const struct brcmf_event_msg *e)
4486 {
4487         struct brcmf_if *ifp = netdev_priv(ndev);
4488         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
4489         struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
4490         struct wiphy *wiphy = cfg_to_wiphy(cfg);
4491         struct ieee80211_channel *notify_channel = NULL;
4492         struct ieee80211_supported_band *band;
4493         struct brcmf_bss_info_le *bi;
4494         struct brcmu_chan ch;
4495         u32 freq;
4496         s32 err = 0;
4497         u8 *buf;
4498
4499         brcmf_dbg(TRACE, "Enter\n");
4500
4501         brcmf_get_assoc_ies(cfg, ifp);
4502         memcpy(profile->bssid, e->addr, ETH_ALEN);
4503         brcmf_update_bss_info(cfg, ifp);
4504
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);
4513         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
4514                                      buf, WL_BSS_INFO_MAX);
4515
4516         if (err)
4517                 goto done;
4518
4519         bi = (struct brcmf_bss_info_le *)(buf + 4);
4520         ch.chspec = le16_to_cpu(bi->chanspec);
4521         cfg->d11inf.decchspec(&ch);
4522
4523         if (ch.band == BRCMU_CHAN_BAND_2G)
4524                 band = wiphy->bands[IEEE80211_BAND_2GHZ];
4525         else
4526                 band = wiphy->bands[IEEE80211_BAND_5GHZ];
4527
4528         freq = ieee80211_channel_to_frequency(ch.chnum, band->band);
4529         notify_channel = ieee80211_get_channel(wiphy, freq);
4530
4531 done:
4532         kfree(buf);
4533         cfg80211_roamed(ndev, notify_channel, (u8 *)profile->bssid,
4534                         conn_info->req_ie, conn_info->req_ie_len,
4535                         conn_info->resp_ie, conn_info->resp_ie_len, GFP_KERNEL);
4536         brcmf_dbg(CONN, "Report roaming result\n");
4537
4538         set_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
4539         brcmf_dbg(TRACE, "Exit\n");
4540         return err;
4541 }
4542
4543 static s32
4544 brcmf_bss_connect_done(struct brcmf_cfg80211_info *cfg,
4545                        struct net_device *ndev, const struct brcmf_event_msg *e,
4546                        bool completed)
4547 {
4548         struct brcmf_if *ifp = netdev_priv(ndev);
4549         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
4550         struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
4551
4552         brcmf_dbg(TRACE, "Enter\n");
4553
4554         if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4555                                &ifp->vif->sme_state)) {
4556                 if (completed) {
4557                         brcmf_get_assoc_ies(cfg, ifp);
4558                         memcpy(profile->bssid, e->addr, ETH_ALEN);
4559                         brcmf_update_bss_info(cfg, ifp);
4560                         set_bit(BRCMF_VIF_STATUS_CONNECTED,
4561                                 &ifp->vif->sme_state);
4562                 }
4563                 cfg80211_connect_result(ndev,
4564                                         (u8 *)profile->bssid,
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);
4572                 brcmf_dbg(CONN, "Report connect result - connection %s\n",
4573                           completed ? "succeeded" : "failed");
4574         }
4575         brcmf_dbg(TRACE, "Exit\n");
4576         return 0;
4577 }
4578
4579 static s32
4580 brcmf_notify_connect_status_ap(struct brcmf_cfg80211_info *cfg,
4581                                struct net_device *ndev,
4582                                const struct brcmf_event_msg *e, void *data)
4583 {
4584         static int generation;
4585         u32 event = e->event_code;
4586         u32 reason = e->reason;
4587         struct station_info sinfo;
4588
4589         brcmf_dbg(CONN, "event %d, reason %d\n", event, reason);
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         }
4596
4597         if (((event == BRCMF_E_ASSOC_IND) || (event == BRCMF_E_REASSOC_IND)) &&
4598             (reason == BRCMF_E_STATUS_SUCCESS)) {
4599                 memset(&sinfo, 0, sizeof(sinfo));
4600                 sinfo.filled = STATION_INFO_ASSOC_REQ_IES;
4601                 if (!data) {
4602                         brcmf_err("No IEs present in ASSOC/REASSOC_IND");
4603                         return -EINVAL;
4604                 }
4605                 sinfo.assoc_req_ies = data;
4606                 sinfo.assoc_req_ies_len = e->datalen;
4607                 generation++;
4608                 sinfo.generation = generation;
4609                 cfg80211_new_sta(ndev, e->addr, &sinfo, GFP_KERNEL);
4610         } else if ((event == BRCMF_E_DISASSOC_IND) ||
4611                    (event == BRCMF_E_DEAUTH_IND) ||
4612                    (event == BRCMF_E_DEAUTH)) {
4613                 cfg80211_del_sta(ndev, e->addr, GFP_KERNEL);
4614         }
4615         return 0;
4616 }
4617
4618 static s32
4619 brcmf_notify_connect_status(struct brcmf_if *ifp,
4620                             const struct brcmf_event_msg *e, void *data)
4621 {
4622         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
4623         struct net_device *ndev = ifp->ndev;
4624         struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
4625         struct ieee80211_channel *chan;
4626         s32 err = 0;
4627
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
4635         if (brcmf_is_apmode(ifp->vif)) {
4636                 err = brcmf_notify_connect_status_ap(cfg, ndev, e, data);
4637         } else if (brcmf_is_linkup(e)) {
4638                 brcmf_dbg(CONN, "Linkup\n");
4639                 if (brcmf_is_ibssmode(ifp->vif)) {
4640                         chan = ieee80211_get_channel(cfg->wiphy, cfg->channel);
4641                         memcpy(profile->bssid, e->addr, ETH_ALEN);
4642                         wl_inform_ibss(cfg, ndev, e->addr);
4643                         cfg80211_ibss_joined(ndev, e->addr, chan, GFP_KERNEL);
4644                         clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4645                                   &ifp->vif->sme_state);
4646                         set_bit(BRCMF_VIF_STATUS_CONNECTED,
4647                                 &ifp->vif->sme_state);
4648                 } else
4649                         brcmf_bss_connect_done(cfg, ndev, e, true);
4650         } else if (brcmf_is_linkdown(e)) {
4651                 brcmf_dbg(CONN, "Linkdown\n");
4652                 if (!brcmf_is_ibssmode(ifp->vif)) {
4653                         brcmf_bss_connect_done(cfg, ndev, e, false);
4654                 }
4655                 brcmf_link_down(ifp->vif);
4656                 brcmf_init_prof(ndev_to_prof(ndev));
4657                 if (ndev != cfg_to_ndev(cfg))
4658                         complete(&cfg->vif_disabled);
4659         } else if (brcmf_is_nonetwork(cfg, e)) {
4660                 if (brcmf_is_ibssmode(ifp->vif))
4661                         clear_bit(BRCMF_VIF_STATUS_CONNECTING,
4662                                   &ifp->vif->sme_state);
4663                 else
4664                         brcmf_bss_connect_done(cfg, ndev, e, false);
4665         }
4666
4667         return err;
4668 }
4669
4670 static s32
4671 brcmf_notify_roaming_status(struct brcmf_if *ifp,
4672                             const struct brcmf_event_msg *e, void *data)
4673 {
4674         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
4675         u32 event = e->event_code;
4676         u32 status = e->status;
4677
4678         if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) {
4679                 if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state))
4680                         brcmf_bss_roaming_done(cfg, ifp->ndev, e);
4681                 else
4682                         brcmf_bss_connect_done(cfg, ifp->ndev, e, true);
4683         }
4684
4685         return 0;
4686 }
4687
4688 static s32
4689 brcmf_notify_mic_status(struct brcmf_if *ifp,
4690                         const struct brcmf_event_msg *e, void *data)
4691 {
4692         u16 flags = e->flags;
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
4700         cfg80211_michael_mic_failure(ifp->ndev, (u8 *)&e->addr, key_type, -1,
4701                                      NULL, GFP_KERNEL);
4702
4703         return 0;
4704 }
4705
4706 static 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
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 */
4725                 if (!cfg->vif_event.vif) {
4726                         mutex_unlock(&event->vif_event_lock);
4727                         return -EBADF;
4728                 }
4729
4730                 ifp->vif = vif;
4731                 vif->ifp = ifp;
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                 }
4737                 mutex_unlock(&event->vif_event_lock);
4738                 wake_up(&event->vif_wq);
4739                 return 0;
4740
4741         case BRCMF_E_IF_DEL:
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
4748         case BRCMF_E_IF_CHANGE:
4749                 mutex_unlock(&event->vif_event_lock);
4750                 wake_up(&event->vif_wq);
4751                 return 0;
4752
4753         default:
4754                 mutex_unlock(&event->vif_event_lock);
4755                 break;
4756         }
4757         return -EINVAL;
4758 }
4759
4760 static void brcmf_init_conf(struct brcmf_cfg80211_conf *conf)
4761 {
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
4769 static void brcmf_register_event_handlers(struct brcmf_cfg80211_info *cfg)
4770 {
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);
4791         brcmf_fweh_register(cfg->pub, BRCMF_E_IF,
4792                             brcmf_notify_vif_event);
4793         brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_PROBEREQ_MSG,
4794                             brcmf_p2p_notify_rx_mgmt_p2p_probereq);
4795         brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_DISC_LISTEN_COMPLETE,
4796                             brcmf_p2p_notify_listen_complete);
4797         brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_RX,
4798                             brcmf_p2p_notify_action_frame_rx);
4799         brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_COMPLETE,
4800                             brcmf_p2p_notify_action_tx_complete);
4801         brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE,
4802                             brcmf_p2p_notify_action_tx_complete);
4803 }
4804
4805 static void brcmf_deinit_priv_mem(struct brcmf_cfg80211_info *cfg)
4806 {
4807         kfree(cfg->conf);
4808         cfg->conf = NULL;
4809         kfree(cfg->escan_ioctl_buf);
4810         cfg->escan_ioctl_buf = NULL;
4811         kfree(cfg->extra_buf);
4812         cfg->extra_buf = NULL;
4813         kfree(cfg->pmk_list);
4814         cfg->pmk_list = NULL;
4815 }
4816
4817 static s32 brcmf_init_priv_mem(struct brcmf_cfg80211_info *cfg)
4818 {
4819         cfg->conf = kzalloc(sizeof(*cfg->conf), GFP_KERNEL);
4820         if (!cfg->conf)
4821                 goto init_priv_mem_out;
4822         cfg->escan_ioctl_buf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
4823         if (!cfg->escan_ioctl_buf)
4824                 goto init_priv_mem_out;
4825         cfg->extra_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
4826         if (!cfg->extra_buf)
4827                 goto init_priv_mem_out;
4828         cfg->pmk_list = kzalloc(sizeof(*cfg->pmk_list), GFP_KERNEL);
4829         if (!cfg->pmk_list)
4830                 goto init_priv_mem_out;
4831
4832         return 0;
4833
4834 init_priv_mem_out:
4835         brcmf_deinit_priv_mem(cfg);
4836
4837         return -ENOMEM;
4838 }
4839
4840 static s32 wl_init_priv(struct brcmf_cfg80211_info *cfg)
4841 {
4842         s32 err = 0;
4843
4844         cfg->scan_request = NULL;
4845         cfg->pwr_save = true;
4846         cfg->active_scan = true;        /* we do active scan per default */
4847         cfg->dongle_up = false;         /* dongle is not up yet */
4848         err = brcmf_init_priv_mem(cfg);
4849         if (err)
4850                 return err;
4851         brcmf_register_event_handlers(cfg);
4852         mutex_init(&cfg->usr_sync);
4853         brcmf_init_escan(cfg);
4854         brcmf_init_conf(cfg->conf);
4855         init_completion(&cfg->vif_disabled);
4856         return err;
4857 }
4858
4859 static void wl_deinit_priv(struct brcmf_cfg80211_info *cfg)
4860 {
4861         cfg->dongle_up = false; /* dongle down */
4862         brcmf_abort_scanning(cfg);
4863         brcmf_deinit_priv_mem(cfg);
4864 }
4865
4866 static void init_vif_event(struct brcmf_cfg80211_vif_event *event)
4867 {
4868         init_waitqueue_head(&event->vif_wq);
4869         mutex_init(&event->vif_event_lock);
4870 }
4871
4872 static s32
4873 brcmf_dongle_roam(struct brcmf_if *ifp, u32 bcn_timeout)
4874 {
4875         s32 err = 0;
4876         __le32 roamtrigger[2];
4877         __le32 roam_delta[2];
4878
4879         /*
4880          * Setup timeout if Beacons are lost and roam is
4881          * off to report link down
4882          */
4883         if (brcmf_roamoff) {
4884                 err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout", bcn_timeout);
4885                 if (err) {
4886                         brcmf_err("bcn_timeout error (%d)\n", err);
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          */
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));
4898         if (err) {
4899                 brcmf_err("roam_off error (%d)\n", err);
4900                 goto dongle_rom_out;
4901         }
4902
4903         roamtrigger[0] = cpu_to_le32(WL_ROAM_TRIGGER_LEVEL);
4904         roamtrigger[1] = cpu_to_le32(BRCM_BAND_ALL);
4905         err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_TRIGGER,
4906                                      (void *)roamtrigger, sizeof(roamtrigger));
4907         if (err) {
4908                 brcmf_err("WLC_SET_ROAM_TRIGGER error (%d)\n", err);
4909                 goto dongle_rom_out;
4910         }
4911
4912         roam_delta[0] = cpu_to_le32(WL_ROAM_DELTA);
4913         roam_delta[1] = cpu_to_le32(BRCM_BAND_ALL);
4914         err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_DELTA,
4915                                      (void *)roam_delta, sizeof(roam_delta));
4916         if (err) {
4917                 brcmf_err("WLC_SET_ROAM_DELTA error (%d)\n", err);
4918                 goto dongle_rom_out;
4919         }
4920
4921 dongle_rom_out:
4922         return err;
4923 }
4924
4925 static s32
4926 brcmf_dongle_scantime(struct brcmf_if *ifp, s32 scan_assoc_time,
4927                       s32 scan_unassoc_time, s32 scan_passive_time)
4928 {
4929         s32 err = 0;
4930
4931         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
4932                                     scan_assoc_time);
4933         if (err) {
4934                 if (err == -EOPNOTSUPP)
4935                         brcmf_dbg(INFO, "Scan assoc time is not supported\n");
4936                 else
4937                         brcmf_err("Scan assoc time error (%d)\n", err);
4938                 goto dongle_scantime_out;
4939         }
4940         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
4941                                     scan_unassoc_time);
4942         if (err) {
4943                 if (err == -EOPNOTSUPP)
4944                         brcmf_dbg(INFO, "Scan unassoc time is not supported\n");
4945                 else
4946                         brcmf_err("Scan unassoc time error (%d)\n", err);
4947                 goto dongle_scantime_out;
4948         }
4949
4950         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_PASSIVE_TIME,
4951                                     scan_passive_time);
4952         if (err) {
4953                 if (err == -EOPNOTSUPP)
4954                         brcmf_dbg(INFO, "Scan passive time is not supported\n");
4955                 else
4956                         brcmf_err("Scan passive time error (%d)\n", err);
4957                 goto dongle_scantime_out;
4958         }
4959
4960 dongle_scantime_out:
4961         return err;
4962 }
4963
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  */
4968 static 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
4976         for (i = 0; i < total; i++) {
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
4998 static void brcmf_update_bw40_channel_flag(struct ieee80211_channel *channel,
4999                                            struct brcmu_chan *ch)
5000 {
5001         u32 ht40_flag;
5002
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
5019 static int brcmf_construct_chaninfo(struct brcmf_cfg80211_info *cfg,
5020                                     u32 bw_cap[])
5021 {
5022         struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5023         struct ieee80211_supported_band *band;
5024         struct ieee80211_channel *channel;
5025         struct wiphy *wiphy;
5026         struct brcmf_chanspec_list *list;
5027         struct brcmu_chan ch;
5028         int err;
5029         u8 *pbuf;
5030         u32 i, j;
5031         u32 total;
5032         u32 chaninfo;
5033         u32 chcnt[2] = { 0, 0 };
5034         u32 index;
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);
5047                 goto fail_pbuf;
5048         }
5049
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         }
5086
5087         total = le32_to_cpu(list->count);
5088         for (i = 0; i < total; i++) {
5089                 ch.chspec = (u16)le32_to_cpu(list->element[i]);
5090                 cfg->d11inf.decchspec(&ch);
5091
5092                 if (ch.band == BRCMU_CHAN_BAND_2G) {
5093                         band = wiphy->bands[IEEE80211_BAND_2GHZ];
5094                 } else if (ch.band == BRCMU_CHAN_BAND_5G) {
5095                         band = wiphy->bands[IEEE80211_BAND_5GHZ];
5096                 } else {
5097                         brcmf_err("Invalid channel Spec. 0x%x.\n", ch.chspec);
5098                         continue;
5099                 }
5100                 if (!(bw_cap[band->band] & WLC_BW_40MHZ_BIT) &&
5101                     ch.bw == BRCMU_CHAN_BW_40)
5102                         continue;
5103                 if (!(bw_cap[band->band] & WLC_BW_80MHZ_BIT) &&
5104                     ch.bw == BRCMU_CHAN_BW_80)
5105                         continue;
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;
5112                                 break;
5113                         }
5114                 }
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.
5130                          */
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;
5146                         }
5147                 }
5148                 if (index == band->n_channels)
5149                         band->n_channels++;
5150         }
5151         kfree(pbuf);
5152         return 0;
5153
5154 fail_band2g:
5155         kfree(wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
5156         kfree(wiphy->bands[IEEE80211_BAND_2GHZ]);
5157         wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
5158 fail_pbuf:
5159         kfree(pbuf);
5160         return err;
5161 }
5162
5163 static int brcmf_enable_bw40_2g(struct brcmf_cfg80211_info *cfg)
5164 {
5165         struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5166         struct ieee80211_supported_band *band;
5167         struct brcmf_fil_bwcap_le band_bwcap;
5168         struct brcmf_chanspec_list *list;
5169         u8 *pbuf;
5170         u32 val;
5171         int err;
5172         struct brcmu_chan ch;
5173         u32 num_chan;
5174         int i, j;
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         }
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;
5201                 ch.sb = BRCMU_CHAN_SB_NONE;
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                 }
5235                 kfree(pbuf);
5236         }
5237         return err;
5238 }
5239
5240 static 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 }
5280
5281 static 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
5297 static __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
5308 static 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
5330 static int brcmf_setup_wiphybands(struct wiphy *wiphy)
5331 {
5332         struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
5333         struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
5334         u32 nmode = 0;
5335         u32 vhtmode = 0;
5336         u32 bw_cap[2] = { WLC_BW_20MHZ_BIT, WLC_BW_20MHZ_BIT };
5337         u32 rxchain;
5338         u32 nchain;
5339         int err;
5340         s32 i;
5341         struct ieee80211_supported_band *band;
5342
5343         (void)brcmf_fil_iovar_int_get(ifp, "vhtmode", &vhtmode);
5344         err = brcmf_fil_iovar_int_get(ifp, "nmode", &nmode);
5345         if (err) {
5346                 brcmf_err("nmode error (%d)\n", err);
5347         } else {
5348                 brcmf_get_bwcap(ifp, bw_cap);
5349         }
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]);
5353
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
5364         err = brcmf_construct_chaninfo(cfg, bw_cap);
5365         if (err) {
5366                 brcmf_err("brcmf_construct_chaninfo failed (%d)\n", err);
5367                 return err;
5368         }
5369
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)
5374                         continue;
5375
5376                 if (nmode)
5377                         brcmf_update_ht_cap(band, bw_cap, nchain);
5378                 if (vhtmode)
5379                         brcmf_update_vht_cap(band, bw_cap, nchain);
5380         }
5381
5382         return 0;
5383 }
5384
5385 static 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 };
5402 static 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
5411 static const struct ieee80211_txrx_stypes
5412 brcmf_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
5440 static 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
5449
5450 #ifdef CONFIG_PM
5451 static const struct wiphy_wowlan_support brcmf_wowlan_support = {
5452         .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT,
5453 };
5454 #endif
5455
5456 static 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
5464 static int brcmf_setup_wiphy(struct wiphy *wiphy, struct brcmf_if *ifp)
5465 {
5466         struct ieee80211_iface_combination ifc_combo;
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 */
5477         ifc_combo = brcmf_iface_combos[0];
5478         if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MCHAN))
5479                 ifc_combo.num_different_channels = 2;
5480         wiphy->iface_combinations = kmemdup(&ifc_combo,
5481                                             sizeof(ifc_combo),
5482                                             GFP_KERNEL);
5483         wiphy->n_iface_combinations = ARRAY_SIZE(brcmf_iface_combos);
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);
5496
5497         /* vendor commands/events support */
5498         wiphy->vendor_commands = brcmf_vendor_cmds;
5499         wiphy->n_vendor_commands = BRCMF_VNDR_CMDS_LAST - 1;
5500
5501         if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL))
5502                 brcmf_wiphy_wowl_params(wiphy);
5503
5504         return brcmf_setup_wiphybands(wiphy);
5505 }
5506
5507 static s32 brcmf_config_dongle(struct brcmf_cfg80211_info *cfg)
5508 {
5509         struct net_device *ndev;
5510         struct wireless_dev *wdev;
5511         struct brcmf_if *ifp;
5512         s32 power_mode;
5513         s32 err = 0;
5514
5515         if (cfg->dongle_up)
5516                 return err;
5517
5518         ndev = cfg_to_ndev(cfg);
5519         wdev = ndev->ieee80211_ptr;
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);
5524
5525         brcmf_dongle_scantime(ifp, WL_SCAN_CHANNEL_TIME,
5526                               WL_SCAN_UNASSOC_TIME, WL_SCAN_PASSIVE_TIME);
5527
5528         power_mode = cfg->pwr_save ? PM_FAST : PM_OFF;
5529         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, power_mode);
5530         if (err)
5531                 goto default_conf_out;
5532         brcmf_dbg(INFO, "power save set to %s\n",
5533                   (power_mode ? "enabled" : "disabled"));
5534
5535         err = brcmf_dongle_roam(ifp, WL_BEACON_TIMEOUT);
5536         if (err)
5537                 goto default_conf_out;
5538         err = brcmf_cfg80211_change_iface(wdev->wiphy, ndev, wdev->iftype,
5539                                           NULL, NULL);
5540         if (err)
5541                 goto default_conf_out;
5542
5543         brcmf_configure_arp_offload(ifp, true);
5544
5545         cfg->dongle_up = true;
5546 default_conf_out:
5547
5548         return err;
5549
5550 }
5551
5552 static s32 __brcmf_cfg80211_up(struct brcmf_if *ifp)
5553 {
5554         set_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
5555
5556         return brcmf_config_dongle(ifp->drvr->config);
5557 }
5558
5559 static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp)
5560 {
5561         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5562
5563         /*
5564          * While going down, if associated with AP disassociate
5565          * from AP to save power
5566          */
5567         if (check_vif_up(ifp->vif)) {
5568                 brcmf_link_down(ifp->vif);
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
5577         brcmf_abort_scanning(cfg);
5578         clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
5579
5580         return 0;
5581 }
5582
5583 s32 brcmf_cfg80211_up(struct net_device *ndev)
5584 {
5585         struct brcmf_if *ifp = netdev_priv(ndev);
5586         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5587         s32 err = 0;
5588
5589         mutex_lock(&cfg->usr_sync);
5590         err = __brcmf_cfg80211_up(ifp);
5591         mutex_unlock(&cfg->usr_sync);
5592
5593         return err;
5594 }
5595
5596 s32 brcmf_cfg80211_down(struct net_device *ndev)
5597 {
5598         struct brcmf_if *ifp = netdev_priv(ndev);
5599         struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5600         s32 err = 0;
5601
5602         mutex_lock(&cfg->usr_sync);
5603         err = __brcmf_cfg80211_down(ifp);
5604         mutex_unlock(&cfg->usr_sync);
5605
5606         return err;
5607 }
5608
5609 enum 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
5616 bool brcmf_get_vif_state_any(struct brcmf_cfg80211_info *cfg, unsigned long state)
5617 {
5618         struct brcmf_cfg80211_vif *vif;
5619
5620         list_for_each_entry(vif, &cfg->vif_list, list) {
5621                 if (test_bit(state, &vif->sme_state))
5622                         return true;
5623         }
5624         return false;
5625 }
5626
5627 static 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
5638 void 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
5649 bool 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 }
5660 int 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
5669 static 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
5683 struct 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;
5693         u16 *cap = NULL;
5694
5695         if (!ndev) {
5696                 brcmf_err("ndev is invalid\n");
5697                 return NULL;
5698         }
5699
5700         ifp = netdev_priv(ndev);
5701         wiphy = wiphy_new(&wl_cfg80211_ops, sizeof(struct brcmf_cfg80211_info));
5702         if (!wiphy) {
5703                 brcmf_err("Could not allocate wiphy device\n");
5704                 return NULL;
5705         }
5706         set_wiphy_dev(wiphy, busdev);
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);
5715         if (IS_ERR(vif))
5716                 goto wiphy_out;
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);
5726                 brcmf_free_vif(vif);
5727                 goto wiphy_out;
5728         }
5729         ifp->vif = vif;
5730
5731         /* determine d11 io type before wiphy setup */
5732         err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_VERSION, &io_type);
5733         if (err) {
5734                 brcmf_err("Failed to get D11 version (%d)\n", err);
5735                 goto priv_out;
5736         }
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;
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          */
5765         if (cap && (*cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)) {
5766                 err = brcmf_enable_bw40_2g(cfg);
5767                 if (!err)
5768                         err = brcmf_fil_iovar_int_set(ifp, "obss_coex",
5769                                                       BRCMF_OBSS_COEX_AUTO);
5770                 else
5771                         *cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5772         }
5773
5774         err = brcmf_p2p_attach(cfg);
5775         if (err) {
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;
5784         }
5785
5786         err = brcmf_fil_iovar_int_set(ifp, "tdls_enable", 1);
5787         if (err) {
5788                 brcmf_dbg(INFO, "TDLS not enabled (%d)\n", err);
5789                 wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_TDLS;
5790         } else {
5791                 brcmf_fweh_register(cfg->pub, BRCMF_E_TDLS_PEER_EVENT,
5792                                     brcmf_notify_tdls_peer_event);
5793         }
5794
5795         return cfg;
5796
5797 wiphy_unreg_out:
5798         wiphy_unregister(cfg->wiphy);
5799 priv_out:
5800         wl_deinit_priv(cfg);
5801         brcmf_free_vif(vif);
5802 wiphy_out:
5803         brcmf_free_wiphy(wiphy);
5804         return NULL;
5805 }
5806
5807 void 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);
5815         brcmf_p2p_detach(&cfg->p2p);
5816         wl_deinit_priv(cfg);
5817         brcmf_free_wiphy(cfg->wiphy);
5818 }