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