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