GNU Linux-libre 4.9.332-gnu1
[releases.git] / drivers / net / wireless / marvell / mwifiex / cfg80211.c
1 /*
2  * Marvell Wireless LAN device driver: CFG80211
3  *
4  * Copyright (C) 2011-2014, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "cfg80211.h"
21 #include "main.h"
22 #include "11n.h"
23 #include "wmm.h"
24
25 static char *reg_alpha2;
26 module_param(reg_alpha2, charp, 0);
27
28 static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
29         {
30                 .max = 3, .types = BIT(NL80211_IFTYPE_STATION) |
31                                    BIT(NL80211_IFTYPE_P2P_GO) |
32                                    BIT(NL80211_IFTYPE_P2P_CLIENT) |
33                                    BIT(NL80211_IFTYPE_AP),
34         },
35 };
36
37 static const struct ieee80211_iface_combination
38 mwifiex_iface_comb_ap_sta = {
39         .limits = mwifiex_ap_sta_limits,
40         .num_different_channels = 1,
41         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
42         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
43         .beacon_int_infra_match = true,
44         .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
45                                 BIT(NL80211_CHAN_WIDTH_20) |
46                                 BIT(NL80211_CHAN_WIDTH_40),
47 };
48
49 static const struct ieee80211_iface_combination
50 mwifiex_iface_comb_ap_sta_vht = {
51         .limits = mwifiex_ap_sta_limits,
52         .num_different_channels = 1,
53         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
54         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
55         .beacon_int_infra_match = true,
56         .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
57                                 BIT(NL80211_CHAN_WIDTH_20) |
58                                 BIT(NL80211_CHAN_WIDTH_40) |
59                                 BIT(NL80211_CHAN_WIDTH_80),
60 };
61
62 static const struct
63 ieee80211_iface_combination mwifiex_iface_comb_ap_sta_drcs = {
64         .limits = mwifiex_ap_sta_limits,
65         .num_different_channels = 2,
66         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
67         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
68         .beacon_int_infra_match = true,
69 };
70
71 /*
72  * This function maps the nl802.11 channel type into driver channel type.
73  *
74  * The mapping is as follows -
75  *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
76  *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
77  *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
78  *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
79  *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
80  */
81 u8 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
82 {
83         switch (chan_type) {
84         case NL80211_CHAN_NO_HT:
85         case NL80211_CHAN_HT20:
86                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
87         case NL80211_CHAN_HT40PLUS:
88                 return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
89         case NL80211_CHAN_HT40MINUS:
90                 return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
91         default:
92                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
93         }
94 }
95
96 /* This function maps IEEE HT secondary channel type to NL80211 channel type
97  */
98 u8 mwifiex_sec_chan_offset_to_chan_type(u8 second_chan_offset)
99 {
100         switch (second_chan_offset) {
101         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
102                 return NL80211_CHAN_HT20;
103         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
104                 return NL80211_CHAN_HT40PLUS;
105         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
106                 return NL80211_CHAN_HT40MINUS;
107         default:
108                 return NL80211_CHAN_HT20;
109         }
110 }
111
112 /*
113  * This function checks whether WEP is set.
114  */
115 static int
116 mwifiex_is_alg_wep(u32 cipher)
117 {
118         switch (cipher) {
119         case WLAN_CIPHER_SUITE_WEP40:
120         case WLAN_CIPHER_SUITE_WEP104:
121                 return 1;
122         default:
123                 break;
124         }
125
126         return 0;
127 }
128
129 /*
130  * This function retrieves the private structure from kernel wiphy structure.
131  */
132 static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
133 {
134         return (void *) (*(unsigned long *) wiphy_priv(wiphy));
135 }
136
137 /*
138  * CFG802.11 operation handler to delete a network key.
139  */
140 static int
141 mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
142                          u8 key_index, bool pairwise, const u8 *mac_addr)
143 {
144         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
145         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
146         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
147
148         if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
149                 mwifiex_dbg(priv->adapter, ERROR, "deleting the crypto keys\n");
150                 return -EFAULT;
151         }
152
153         mwifiex_dbg(priv->adapter, INFO, "info: crypto keys deleted\n");
154         return 0;
155 }
156
157 /*
158  * This function forms an skb for management frame.
159  */
160 static int
161 mwifiex_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
162 {
163         u8 addr[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
164         u16 pkt_len;
165         u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
166
167         pkt_len = len + ETH_ALEN;
168
169         skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN +
170                     MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
171         memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
172
173         memcpy(skb_push(skb, sizeof(tx_control)),
174                &tx_control, sizeof(tx_control));
175
176         memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
177
178         /* Add packet data and address4 */
179         memcpy(skb_put(skb, sizeof(struct ieee80211_hdr_3addr)), buf,
180                sizeof(struct ieee80211_hdr_3addr));
181         memcpy(skb_put(skb, ETH_ALEN), addr, ETH_ALEN);
182         memcpy(skb_put(skb, len - sizeof(struct ieee80211_hdr_3addr)),
183                buf + sizeof(struct ieee80211_hdr_3addr),
184                len - sizeof(struct ieee80211_hdr_3addr));
185
186         skb->priority = LOW_PRIO_TID;
187         __net_timestamp(skb);
188
189         return 0;
190 }
191
192 /*
193  * CFG802.11 operation handler to transmit a management frame.
194  */
195 static int
196 mwifiex_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
197                          struct cfg80211_mgmt_tx_params *params, u64 *cookie)
198 {
199         const u8 *buf = params->buf;
200         size_t len = params->len;
201         struct sk_buff *skb;
202         u16 pkt_len;
203         const struct ieee80211_mgmt *mgmt;
204         struct mwifiex_txinfo *tx_info;
205         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
206
207         if (!buf || !len) {
208                 mwifiex_dbg(priv->adapter, ERROR, "invalid buffer and length\n");
209                 return -EFAULT;
210         }
211
212         mgmt = (const struct ieee80211_mgmt *)buf;
213         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA &&
214             ieee80211_is_probe_resp(mgmt->frame_control)) {
215                 /* Since we support offload probe resp, we need to skip probe
216                  * resp in AP or GO mode */
217                 mwifiex_dbg(priv->adapter, INFO,
218                             "info: skip to send probe resp in AP or GO mode\n");
219                 return 0;
220         }
221
222         pkt_len = len + ETH_ALEN;
223         skb = dev_alloc_skb(MWIFIEX_MIN_DATA_HEADER_LEN +
224                             MWIFIEX_MGMT_FRAME_HEADER_SIZE +
225                             pkt_len + sizeof(pkt_len));
226
227         if (!skb) {
228                 mwifiex_dbg(priv->adapter, ERROR,
229                             "allocate skb failed for management frame\n");
230                 return -ENOMEM;
231         }
232
233         tx_info = MWIFIEX_SKB_TXCB(skb);
234         memset(tx_info, 0, sizeof(*tx_info));
235         tx_info->bss_num = priv->bss_num;
236         tx_info->bss_type = priv->bss_type;
237         tx_info->pkt_len = pkt_len;
238
239         mwifiex_form_mgmt_frame(skb, buf, len);
240         *cookie = prandom_u32() | 1;
241
242         if (ieee80211_is_action(mgmt->frame_control))
243                 skb = mwifiex_clone_skb_for_tx_status(priv,
244                                                       skb,
245                                 MWIFIEX_BUF_FLAG_ACTION_TX_STATUS, cookie);
246         else
247                 cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
248                                         GFP_ATOMIC);
249
250         mwifiex_queue_tx_pkt(priv, skb);
251
252         mwifiex_dbg(priv->adapter, INFO, "info: management frame transmitted\n");
253         return 0;
254 }
255
256 /*
257  * CFG802.11 operation handler to register a mgmt frame.
258  */
259 static void
260 mwifiex_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
261                                      struct wireless_dev *wdev,
262                                      u16 frame_type, bool reg)
263 {
264         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
265         u32 mask;
266
267         if (reg)
268                 mask = priv->mgmt_frame_mask | BIT(frame_type >> 4);
269         else
270                 mask = priv->mgmt_frame_mask & ~BIT(frame_type >> 4);
271
272         if (mask != priv->mgmt_frame_mask) {
273                 priv->mgmt_frame_mask = mask;
274                 mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
275                                  HostCmd_ACT_GEN_SET, 0,
276                                  &priv->mgmt_frame_mask, false);
277                 mwifiex_dbg(priv->adapter, INFO, "info: mgmt frame registered\n");
278         }
279 }
280
281 /*
282  * CFG802.11 operation handler to remain on channel.
283  */
284 static int
285 mwifiex_cfg80211_remain_on_channel(struct wiphy *wiphy,
286                                    struct wireless_dev *wdev,
287                                    struct ieee80211_channel *chan,
288                                    unsigned int duration, u64 *cookie)
289 {
290         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
291         int ret;
292
293         if (!chan || !cookie) {
294                 mwifiex_dbg(priv->adapter, ERROR, "Invalid parameter for ROC\n");
295                 return -EINVAL;
296         }
297
298         if (priv->roc_cfg.cookie) {
299                 mwifiex_dbg(priv->adapter, INFO,
300                             "info: ongoing ROC, cookie = 0x%llx\n",
301                             priv->roc_cfg.cookie);
302                 return -EBUSY;
303         }
304
305         ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_SET, chan,
306                                          duration);
307
308         if (!ret) {
309                 *cookie = prandom_u32() | 1;
310                 priv->roc_cfg.cookie = *cookie;
311                 priv->roc_cfg.chan = *chan;
312
313                 cfg80211_ready_on_channel(wdev, *cookie, chan,
314                                           duration, GFP_ATOMIC);
315
316                 mwifiex_dbg(priv->adapter, INFO,
317                             "info: ROC, cookie = 0x%llx\n", *cookie);
318         }
319
320         return ret;
321 }
322
323 /*
324  * CFG802.11 operation handler to cancel remain on channel.
325  */
326 static int
327 mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
328                                           struct wireless_dev *wdev, u64 cookie)
329 {
330         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
331         int ret;
332
333         if (cookie != priv->roc_cfg.cookie)
334                 return -ENOENT;
335
336         ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_REMOVE,
337                                          &priv->roc_cfg.chan, 0);
338
339         if (!ret) {
340                 cfg80211_remain_on_channel_expired(wdev, cookie,
341                                                    &priv->roc_cfg.chan,
342                                                    GFP_ATOMIC);
343
344                 memset(&priv->roc_cfg, 0, sizeof(struct mwifiex_roc_cfg));
345
346                 mwifiex_dbg(priv->adapter, INFO,
347                             "info: cancel ROC, cookie = 0x%llx\n", cookie);
348         }
349
350         return ret;
351 }
352
353 /*
354  * CFG802.11 operation handler to set Tx power.
355  */
356 static int
357 mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
358                               struct wireless_dev *wdev,
359                               enum nl80211_tx_power_setting type,
360                               int mbm)
361 {
362         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
363         struct mwifiex_private *priv;
364         struct mwifiex_power_cfg power_cfg;
365         int dbm = MBM_TO_DBM(mbm);
366
367         switch (type) {
368         case NL80211_TX_POWER_FIXED:
369                 power_cfg.is_power_auto = 0;
370                 power_cfg.is_power_fixed = 1;
371                 power_cfg.power_level = dbm;
372                 break;
373         case NL80211_TX_POWER_LIMITED:
374                 power_cfg.is_power_auto = 0;
375                 power_cfg.is_power_fixed = 0;
376                 power_cfg.power_level = dbm;
377                 break;
378         case NL80211_TX_POWER_AUTOMATIC:
379                 power_cfg.is_power_auto = 1;
380                 break;
381         }
382
383         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
384
385         return mwifiex_set_tx_power(priv, &power_cfg);
386 }
387
388 /*
389  * CFG802.11 operation handler to get Tx power.
390  */
391 static int
392 mwifiex_cfg80211_get_tx_power(struct wiphy *wiphy,
393                               struct wireless_dev *wdev,
394                               int *dbm)
395 {
396         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
397         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
398                                                         MWIFIEX_BSS_ROLE_ANY);
399         int ret = mwifiex_send_cmd(priv, HostCmd_CMD_RF_TX_PWR,
400                                    HostCmd_ACT_GEN_GET, 0, NULL, true);
401
402         if (ret < 0)
403                 return ret;
404
405         /* tx_power_level is set in HostCmd_CMD_RF_TX_PWR command handler */
406         *dbm = priv->tx_power_level;
407
408         return 0;
409 }
410
411 /*
412  * CFG802.11 operation handler to set Power Save option.
413  *
414  * The timeout value, if provided, is currently ignored.
415  */
416 static int
417 mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
418                                 struct net_device *dev,
419                                 bool enabled, int timeout)
420 {
421         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
422         u32 ps_mode;
423
424         if (timeout)
425                 mwifiex_dbg(priv->adapter, INFO,
426                             "info: ignore timeout value for IEEE Power Save\n");
427
428         ps_mode = enabled;
429
430         return mwifiex_drv_set_power(priv, &ps_mode);
431 }
432
433 /*
434  * CFG802.11 operation handler to set the default network key.
435  */
436 static int
437 mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
438                                  u8 key_index, bool unicast,
439                                  bool multicast)
440 {
441         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
442
443         /* Return if WEP key not configured */
444         if (!priv->sec_info.wep_enabled)
445                 return 0;
446
447         if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
448                 priv->wep_key_curr_index = key_index;
449         } else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
450                                       NULL, 0)) {
451                 mwifiex_dbg(priv->adapter, ERROR, "set default Tx key index\n");
452                 return -EFAULT;
453         }
454
455         return 0;
456 }
457
458 /*
459  * CFG802.11 operation handler to add a network key.
460  */
461 static int
462 mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
463                          u8 key_index, bool pairwise, const u8 *mac_addr,
464                          struct key_params *params)
465 {
466         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
467         struct mwifiex_wep_key *wep_key;
468         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
469         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
470
471         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
472             (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
473              params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
474                 if (params->key && params->key_len) {
475                         wep_key = &priv->wep_key[key_index];
476                         memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
477                         memcpy(wep_key->key_material, params->key,
478                                params->key_len);
479                         wep_key->key_index = key_index;
480                         wep_key->key_length = params->key_len;
481                         priv->sec_info.wep_enabled = 1;
482                 }
483                 return 0;
484         }
485
486         if (mwifiex_set_encode(priv, params, params->key, params->key_len,
487                                key_index, peer_mac, 0)) {
488                 mwifiex_dbg(priv->adapter, ERROR, "crypto keys added\n");
489                 return -EFAULT;
490         }
491
492         return 0;
493 }
494
495 /*
496  * CFG802.11 operation handler to set default mgmt key.
497  */
498 static int
499 mwifiex_cfg80211_set_default_mgmt_key(struct wiphy *wiphy,
500                                       struct net_device *netdev,
501                                       u8 key_index)
502 {
503         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
504         struct mwifiex_ds_encrypt_key encrypt_key;
505
506         wiphy_dbg(wiphy, "set default mgmt key, key index=%d\n", key_index);
507
508         memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
509         encrypt_key.key_len = WLAN_KEY_LEN_CCMP;
510         encrypt_key.key_index = key_index;
511         encrypt_key.is_igtk_def_key = true;
512         eth_broadcast_addr(encrypt_key.mac_addr);
513
514         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
515                                 HostCmd_ACT_GEN_SET, true, &encrypt_key, true);
516 }
517
518 /*
519  * This function sends domain information to the firmware.
520  *
521  * The following information are passed to the firmware -
522  *      - Country codes
523  *      - Sub bands (first channel, number of channels, maximum Tx power)
524  */
525 int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
526 {
527         u8 no_of_triplet = 0;
528         struct ieee80211_country_ie_triplet *t;
529         u8 no_of_parsed_chan = 0;
530         u8 first_chan = 0, next_chan = 0, max_pwr = 0;
531         u8 i, flag = 0;
532         enum nl80211_band band;
533         struct ieee80211_supported_band *sband;
534         struct ieee80211_channel *ch;
535         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
536         struct mwifiex_private *priv;
537         struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
538
539         /* Set country code */
540         domain_info->country_code[0] = adapter->country_code[0];
541         domain_info->country_code[1] = adapter->country_code[1];
542         domain_info->country_code[2] = ' ';
543
544         band = mwifiex_band_to_radio_type(adapter->config_bands);
545         if (!wiphy->bands[band]) {
546                 mwifiex_dbg(adapter, ERROR,
547                             "11D: setting domain info in FW\n");
548                 return -1;
549         }
550
551         sband = wiphy->bands[band];
552
553         for (i = 0; i < sband->n_channels ; i++) {
554                 ch = &sband->channels[i];
555                 if (ch->flags & IEEE80211_CHAN_DISABLED)
556                         continue;
557
558                 if (!flag) {
559                         flag = 1;
560                         first_chan = (u32) ch->hw_value;
561                         next_chan = first_chan;
562                         max_pwr = ch->max_power;
563                         no_of_parsed_chan = 1;
564                         continue;
565                 }
566
567                 if (ch->hw_value == next_chan + 1 &&
568                     ch->max_power == max_pwr) {
569                         next_chan++;
570                         no_of_parsed_chan++;
571                 } else {
572                         t = &domain_info->triplet[no_of_triplet];
573                         t->chans.first_channel = first_chan;
574                         t->chans.num_channels = no_of_parsed_chan;
575                         t->chans.max_power = max_pwr;
576                         no_of_triplet++;
577                         first_chan = (u32) ch->hw_value;
578                         next_chan = first_chan;
579                         max_pwr = ch->max_power;
580                         no_of_parsed_chan = 1;
581                 }
582         }
583
584         if (flag) {
585                 t = &domain_info->triplet[no_of_triplet];
586                 t->chans.first_channel = first_chan;
587                 t->chans.num_channels = no_of_parsed_chan;
588                 t->chans.max_power = max_pwr;
589                 no_of_triplet++;
590         }
591
592         domain_info->no_of_triplet = no_of_triplet;
593
594         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
595
596         if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
597                              HostCmd_ACT_GEN_SET, 0, NULL, false)) {
598                 mwifiex_dbg(adapter, INFO,
599                             "11D: setting domain info in FW\n");
600                 return -1;
601         }
602
603         return 0;
604 }
605
606 /*
607  * CFG802.11 regulatory domain callback function.
608  *
609  * This function is called when the regulatory domain is changed due to the
610  * following reasons -
611  *      - Set by driver
612  *      - Set by system core
613  *      - Set by user
614  *      - Set bt Country IE
615  */
616 static void mwifiex_reg_notifier(struct wiphy *wiphy,
617                                  struct regulatory_request *request)
618 {
619         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
620         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
621                                                         MWIFIEX_BSS_ROLE_ANY);
622         mwifiex_dbg(adapter, INFO,
623                     "info: cfg80211 regulatory domain callback for %c%c\n",
624                     request->alpha2[0], request->alpha2[1]);
625
626         switch (request->initiator) {
627         case NL80211_REGDOM_SET_BY_DRIVER:
628         case NL80211_REGDOM_SET_BY_CORE:
629         case NL80211_REGDOM_SET_BY_USER:
630         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
631                 break;
632         default:
633                 mwifiex_dbg(adapter, ERROR,
634                             "unknown regdom initiator: %d\n",
635                             request->initiator);
636                 return;
637         }
638
639         /* Don't send world or same regdom info to firmware */
640         if (strncmp(request->alpha2, "00", 2) &&
641             strncmp(request->alpha2, adapter->country_code,
642                     sizeof(request->alpha2))) {
643                 memcpy(adapter->country_code, request->alpha2,
644                        sizeof(request->alpha2));
645                 mwifiex_send_domain_info_cmd_fw(wiphy);
646                 mwifiex_dnld_txpwr_table(priv);
647         }
648 }
649
650 /*
651  * This function sets the fragmentation threshold.
652  *
653  * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
654  * and MWIFIEX_FRAG_MAX_VALUE.
655  */
656 static int
657 mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
658 {
659         if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
660             frag_thr > MWIFIEX_FRAG_MAX_VALUE)
661                 frag_thr = MWIFIEX_FRAG_MAX_VALUE;
662
663         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
664                                 HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
665                                 &frag_thr, true);
666 }
667
668 /*
669  * This function sets the RTS threshold.
670
671  * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
672  * and MWIFIEX_RTS_MAX_VALUE.
673  */
674 static int
675 mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
676 {
677         if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
678                 rts_thr = MWIFIEX_RTS_MAX_VALUE;
679
680         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
681                                 HostCmd_ACT_GEN_SET, RTS_THRESH_I,
682                                 &rts_thr, true);
683 }
684
685 /*
686  * CFG802.11 operation handler to set wiphy parameters.
687  *
688  * This function can be used to set the RTS threshold and the
689  * Fragmentation threshold of the driver.
690  */
691 static int
692 mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
693 {
694         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
695         struct mwifiex_private *priv;
696         struct mwifiex_uap_bss_param *bss_cfg;
697         int ret;
698
699         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
700
701         switch (priv->bss_role) {
702         case MWIFIEX_BSS_ROLE_UAP:
703                 if (priv->bss_started) {
704                         mwifiex_dbg(adapter, ERROR,
705                                     "cannot change wiphy params when bss started");
706                         return -EINVAL;
707                 }
708
709                 bss_cfg = kzalloc(sizeof(*bss_cfg), GFP_KERNEL);
710                 if (!bss_cfg)
711                         return -ENOMEM;
712
713                 mwifiex_set_sys_config_invalid_data(bss_cfg);
714
715                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
716                         bss_cfg->rts_threshold = wiphy->rts_threshold;
717                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
718                         bss_cfg->frag_threshold = wiphy->frag_threshold;
719                 if (changed & WIPHY_PARAM_RETRY_LONG)
720                         bss_cfg->retry_limit = wiphy->retry_long;
721
722                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
723                                        HostCmd_ACT_GEN_SET,
724                                        UAP_BSS_PARAMS_I, bss_cfg,
725                                        false);
726
727                 kfree(bss_cfg);
728                 if (ret) {
729                         mwifiex_dbg(adapter, ERROR,
730                                     "Failed to set wiphy phy params\n");
731                         return ret;
732                 }
733                 break;
734
735         case MWIFIEX_BSS_ROLE_STA:
736                 if (priv->media_connected) {
737                         mwifiex_dbg(adapter, ERROR,
738                                     "cannot change wiphy params when connected");
739                         return -EINVAL;
740                 }
741                 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
742                         ret = mwifiex_set_rts(priv,
743                                               wiphy->rts_threshold);
744                         if (ret)
745                                 return ret;
746                 }
747                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
748                         ret = mwifiex_set_frag(priv,
749                                                wiphy->frag_threshold);
750                         if (ret)
751                                 return ret;
752                 }
753                 break;
754         }
755
756         return 0;
757 }
758
759 static int
760 mwifiex_cfg80211_deinit_p2p(struct mwifiex_private *priv)
761 {
762         u16 mode = P2P_MODE_DISABLE;
763
764         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
765                              HostCmd_ACT_GEN_SET, 0, &mode, true))
766                 return -1;
767
768         return 0;
769 }
770
771 /*
772  * This function initializes the functionalities for P2P client.
773  * The P2P client initialization sequence is:
774  * disable -> device -> client
775  */
776 static int
777 mwifiex_cfg80211_init_p2p_client(struct mwifiex_private *priv)
778 {
779         u16 mode;
780
781         if (mwifiex_cfg80211_deinit_p2p(priv))
782                 return -1;
783
784         mode = P2P_MODE_DEVICE;
785         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
786                              HostCmd_ACT_GEN_SET, 0, &mode, true))
787                 return -1;
788
789         mode = P2P_MODE_CLIENT;
790         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
791                              HostCmd_ACT_GEN_SET, 0, &mode, true))
792                 return -1;
793
794         return 0;
795 }
796
797 /*
798  * This function initializes the functionalities for P2P GO.
799  * The P2P GO initialization sequence is:
800  * disable -> device -> GO
801  */
802 static int
803 mwifiex_cfg80211_init_p2p_go(struct mwifiex_private *priv)
804 {
805         u16 mode;
806
807         if (mwifiex_cfg80211_deinit_p2p(priv))
808                 return -1;
809
810         mode = P2P_MODE_DEVICE;
811         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
812                              HostCmd_ACT_GEN_SET, 0, &mode, true))
813                 return -1;
814
815         mode = P2P_MODE_GO;
816         if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
817                              HostCmd_ACT_GEN_SET, 0, &mode, true))
818                 return -1;
819
820         return 0;
821 }
822
823 static int mwifiex_deinit_priv_params(struct mwifiex_private *priv)
824 {
825         struct mwifiex_adapter *adapter = priv->adapter;
826         unsigned long flags;
827
828         priv->mgmt_frame_mask = 0;
829         if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
830                              HostCmd_ACT_GEN_SET, 0,
831                              &priv->mgmt_frame_mask, false)) {
832                 mwifiex_dbg(adapter, ERROR,
833                             "could not unregister mgmt frame rx\n");
834                 return -1;
835         }
836
837         mwifiex_deauthenticate(priv, NULL);
838
839         spin_lock_irqsave(&adapter->main_proc_lock, flags);
840         adapter->main_locked = true;
841         if (adapter->mwifiex_processing) {
842                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
843                 flush_workqueue(adapter->workqueue);
844         } else {
845                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
846         }
847
848         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
849         adapter->rx_locked = true;
850         if (adapter->rx_processing) {
851                 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
852                 flush_workqueue(adapter->rx_workqueue);
853         } else {
854         spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
855         }
856
857         mwifiex_free_priv(priv);
858         priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
859         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
860         priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
861
862         return 0;
863 }
864
865 static int
866 mwifiex_init_new_priv_params(struct mwifiex_private *priv,
867                              struct net_device *dev,
868                              enum nl80211_iftype type)
869 {
870         struct mwifiex_adapter *adapter = priv->adapter;
871         unsigned long flags;
872
873         mwifiex_init_priv(priv);
874
875         priv->bss_mode = type;
876         priv->wdev.iftype = type;
877
878         mwifiex_init_priv_params(priv, priv->netdev);
879         priv->bss_started = 0;
880
881         switch (type) {
882         case NL80211_IFTYPE_STATION:
883         case NL80211_IFTYPE_ADHOC:
884                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
885                          MWIFIEX_BSS_TYPE_STA);
886                 priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
887                 priv->bss_type = MWIFIEX_BSS_TYPE_STA;
888                 break;
889         case NL80211_IFTYPE_P2P_CLIENT:
890                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
891                          MWIFIEX_BSS_TYPE_P2P);
892                 priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
893                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
894                 break;
895         case NL80211_IFTYPE_P2P_GO:
896                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
897                          MWIFIEX_BSS_TYPE_P2P);
898                 priv->bss_role =  MWIFIEX_BSS_ROLE_UAP;
899                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
900                 break;
901         case NL80211_IFTYPE_AP:
902                 priv->bss_num = mwifiex_get_unused_bss_num(adapter,
903                          MWIFIEX_BSS_TYPE_UAP);
904                 priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
905                 priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
906                 break;
907         default:
908                 mwifiex_dbg(adapter, ERROR,
909                             "%s: changing to %d not supported\n",
910                             dev->name, type);
911                 return -EOPNOTSUPP;
912         }
913
914         spin_lock_irqsave(&adapter->main_proc_lock, flags);
915         adapter->main_locked = false;
916         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
917
918         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
919         adapter->rx_locked = false;
920         spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
921
922         return 0;
923 }
924
925 static int
926 mwifiex_change_vif_to_p2p(struct net_device *dev,
927                           enum nl80211_iftype curr_iftype,
928                           enum nl80211_iftype type, u32 *flags,
929                           struct vif_params *params)
930 {
931         struct mwifiex_private *priv;
932         struct mwifiex_adapter *adapter;
933
934         priv = mwifiex_netdev_get_priv(dev);
935
936         if (!priv)
937                 return -1;
938
939         adapter = priv->adapter;
940
941         if (adapter->curr_iface_comb.p2p_intf ==
942             adapter->iface_limit.p2p_intf) {
943                 mwifiex_dbg(adapter, ERROR,
944                             "cannot create multiple P2P ifaces\n");
945                 return -1;
946         }
947
948         mwifiex_dbg(adapter, INFO,
949                     "%s: changing role to p2p\n", dev->name);
950
951         if (mwifiex_deinit_priv_params(priv))
952                 return -1;
953         if (mwifiex_init_new_priv_params(priv, dev, type))
954                 return -1;
955
956         switch (type) {
957         case NL80211_IFTYPE_P2P_CLIENT:
958                 if (mwifiex_cfg80211_init_p2p_client(priv))
959                         return -EFAULT;
960                 break;
961         case NL80211_IFTYPE_P2P_GO:
962                 if (mwifiex_cfg80211_init_p2p_go(priv))
963                         return -EFAULT;
964                 break;
965         default:
966                 mwifiex_dbg(adapter, ERROR,
967                             "%s: changing to %d not supported\n",
968                             dev->name, type);
969                 return -EOPNOTSUPP;
970         }
971
972         if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
973                              HostCmd_ACT_GEN_SET, 0, NULL, true))
974                 return -1;
975
976         if (mwifiex_sta_init_cmd(priv, false, false))
977                 return -1;
978
979         switch (curr_iftype) {
980         case NL80211_IFTYPE_STATION:
981         case NL80211_IFTYPE_ADHOC:
982                 adapter->curr_iface_comb.sta_intf--;
983                 break;
984         case NL80211_IFTYPE_AP:
985                 adapter->curr_iface_comb.uap_intf--;
986                 break;
987         default:
988                 break;
989         }
990
991         adapter->curr_iface_comb.p2p_intf++;
992         dev->ieee80211_ptr->iftype = type;
993
994         return 0;
995 }
996
997 static int
998 mwifiex_change_vif_to_sta_adhoc(struct net_device *dev,
999                                 enum nl80211_iftype curr_iftype,
1000                                 enum nl80211_iftype type, u32 *flags,
1001                                 struct vif_params *params)
1002 {
1003         struct mwifiex_private *priv;
1004         struct mwifiex_adapter *adapter;
1005
1006         priv = mwifiex_netdev_get_priv(dev);
1007
1008         if (!priv)
1009                 return -1;
1010
1011         adapter = priv->adapter;
1012
1013         if ((curr_iftype != NL80211_IFTYPE_P2P_CLIENT &&
1014              curr_iftype != NL80211_IFTYPE_P2P_GO) &&
1015             (adapter->curr_iface_comb.sta_intf ==
1016              adapter->iface_limit.sta_intf)) {
1017                 mwifiex_dbg(adapter, ERROR,
1018                             "cannot create multiple station/adhoc ifaces\n");
1019                 return -1;
1020         }
1021
1022         if (type == NL80211_IFTYPE_STATION)
1023                 mwifiex_dbg(adapter, INFO,
1024                             "%s: changing role to station\n", dev->name);
1025         else
1026                 mwifiex_dbg(adapter, INFO,
1027                             "%s: changing role to adhoc\n", dev->name);
1028
1029         if (mwifiex_deinit_priv_params(priv))
1030                 return -1;
1031         if (mwifiex_init_new_priv_params(priv, dev, type))
1032                 return -1;
1033         if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1034                              HostCmd_ACT_GEN_SET, 0, NULL, true))
1035                 return -1;
1036         if (mwifiex_sta_init_cmd(priv, false, false))
1037                 return -1;
1038
1039         switch (curr_iftype) {
1040         case NL80211_IFTYPE_P2P_CLIENT:
1041         case NL80211_IFTYPE_P2P_GO:
1042                 adapter->curr_iface_comb.p2p_intf--;
1043                 break;
1044         case NL80211_IFTYPE_AP:
1045                 adapter->curr_iface_comb.uap_intf--;
1046                 break;
1047         default:
1048                 break;
1049         }
1050
1051         adapter->curr_iface_comb.sta_intf++;
1052         dev->ieee80211_ptr->iftype = type;
1053         return 0;
1054 }
1055
1056 static int
1057 mwifiex_change_vif_to_ap(struct net_device *dev,
1058                          enum nl80211_iftype curr_iftype,
1059                          enum nl80211_iftype type, u32 *flags,
1060                          struct vif_params *params)
1061 {
1062         struct mwifiex_private *priv;
1063         struct mwifiex_adapter *adapter;
1064
1065         priv = mwifiex_netdev_get_priv(dev);
1066
1067         if (!priv)
1068                 return -1;
1069
1070         adapter = priv->adapter;
1071
1072         if (adapter->curr_iface_comb.uap_intf ==
1073             adapter->iface_limit.uap_intf) {
1074                 mwifiex_dbg(adapter, ERROR,
1075                             "cannot create multiple AP ifaces\n");
1076                 return -1;
1077         }
1078
1079         mwifiex_dbg(adapter, INFO,
1080                     "%s: changing role to AP\n", dev->name);
1081
1082         if (mwifiex_deinit_priv_params(priv))
1083                 return -1;
1084         if (mwifiex_init_new_priv_params(priv, dev, type))
1085                 return -1;
1086         if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1087                              HostCmd_ACT_GEN_SET, 0, NULL, true))
1088                 return -1;
1089         if (mwifiex_sta_init_cmd(priv, false, false))
1090                 return -1;
1091
1092         switch (curr_iftype) {
1093         case NL80211_IFTYPE_P2P_CLIENT:
1094         case NL80211_IFTYPE_P2P_GO:
1095                 adapter->curr_iface_comb.p2p_intf--;
1096                 break;
1097         case NL80211_IFTYPE_STATION:
1098         case NL80211_IFTYPE_ADHOC:
1099                 adapter->curr_iface_comb.sta_intf--;
1100                 break;
1101         default:
1102                 break;
1103         }
1104
1105         adapter->curr_iface_comb.uap_intf++;
1106         dev->ieee80211_ptr->iftype = type;
1107         return 0;
1108 }
1109 /*
1110  * CFG802.11 operation handler to change interface type.
1111  */
1112 static int
1113 mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
1114                                      struct net_device *dev,
1115                                      enum nl80211_iftype type, u32 *flags,
1116                                      struct vif_params *params)
1117 {
1118         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1119         enum nl80211_iftype curr_iftype = dev->ieee80211_ptr->iftype;
1120
1121         if (priv->scan_request) {
1122                 mwifiex_dbg(priv->adapter, ERROR,
1123                             "change virtual interface: scan in process\n");
1124                 return -EBUSY;
1125         }
1126
1127         switch (curr_iftype) {
1128         case NL80211_IFTYPE_ADHOC:
1129                 switch (type) {
1130                 case NL80211_IFTYPE_STATION:
1131                         priv->bss_mode = type;
1132                         priv->sec_info.authentication_mode =
1133                                                    NL80211_AUTHTYPE_OPEN_SYSTEM;
1134                         dev->ieee80211_ptr->iftype = type;
1135                         mwifiex_deauthenticate(priv, NULL);
1136                         return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1137                                                 HostCmd_ACT_GEN_SET, 0, NULL,
1138                                                 true);
1139                 case NL80211_IFTYPE_P2P_CLIENT:
1140                 case NL80211_IFTYPE_P2P_GO:
1141                         return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1142                                                          type, flags, params);
1143                 case NL80211_IFTYPE_AP:
1144                         return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1145                                                         flags, params);
1146                 case NL80211_IFTYPE_UNSPECIFIED:
1147                         mwifiex_dbg(priv->adapter, INFO,
1148                                     "%s: kept type as IBSS\n", dev->name);
1149                 case NL80211_IFTYPE_ADHOC:      /* This shouldn't happen */
1150                         return 0;
1151                 default:
1152                         mwifiex_dbg(priv->adapter, ERROR,
1153                                     "%s: changing to %d not supported\n",
1154                                     dev->name, type);
1155                         return -EOPNOTSUPP;
1156                 }
1157                 break;
1158         case NL80211_IFTYPE_STATION:
1159                 switch (type) {
1160                 case NL80211_IFTYPE_ADHOC:
1161                         priv->bss_mode = type;
1162                         priv->sec_info.authentication_mode =
1163                                                    NL80211_AUTHTYPE_OPEN_SYSTEM;
1164                         dev->ieee80211_ptr->iftype = type;
1165                         mwifiex_deauthenticate(priv, NULL);
1166                         return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1167                                                 HostCmd_ACT_GEN_SET, 0, NULL,
1168                                                 true);
1169                 case NL80211_IFTYPE_P2P_CLIENT:
1170                 case NL80211_IFTYPE_P2P_GO:
1171                         return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1172                                                          type, flags, params);
1173                 case NL80211_IFTYPE_AP:
1174                         return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1175                                                         flags, params);
1176                 case NL80211_IFTYPE_UNSPECIFIED:
1177                         mwifiex_dbg(priv->adapter, INFO,
1178                                     "%s: kept type as STA\n", dev->name);
1179                 case NL80211_IFTYPE_STATION:    /* This shouldn't happen */
1180                         return 0;
1181                 default:
1182                         mwifiex_dbg(priv->adapter, ERROR,
1183                                     "%s: changing to %d not supported\n",
1184                                     dev->name, type);
1185                         return -EOPNOTSUPP;
1186                 }
1187                 break;
1188         case NL80211_IFTYPE_AP:
1189                 switch (type) {
1190                 case NL80211_IFTYPE_ADHOC:
1191                 case NL80211_IFTYPE_STATION:
1192                         return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1193                                                                type, flags,
1194                                                                params);
1195                         break;
1196                 case NL80211_IFTYPE_P2P_CLIENT:
1197                 case NL80211_IFTYPE_P2P_GO:
1198                         return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1199                                                          type, flags, params);
1200                 case NL80211_IFTYPE_UNSPECIFIED:
1201                         mwifiex_dbg(priv->adapter, INFO,
1202                                     "%s: kept type as AP\n", dev->name);
1203                 case NL80211_IFTYPE_AP:         /* This shouldn't happen */
1204                         return 0;
1205                 default:
1206                         mwifiex_dbg(priv->adapter, ERROR,
1207                                     "%s: changing to %d not supported\n",
1208                                     dev->name, type);
1209                         return -EOPNOTSUPP;
1210                 }
1211                 break;
1212         case NL80211_IFTYPE_P2P_CLIENT:
1213         case NL80211_IFTYPE_P2P_GO:
1214                 switch (type) {
1215                 case NL80211_IFTYPE_STATION:
1216                         if (mwifiex_cfg80211_deinit_p2p(priv))
1217                                 return -EFAULT;
1218                         priv->adapter->curr_iface_comb.p2p_intf--;
1219                         priv->adapter->curr_iface_comb.sta_intf++;
1220                         dev->ieee80211_ptr->iftype = type;
1221                         if (mwifiex_deinit_priv_params(priv))
1222                                 return -1;
1223                         if (mwifiex_init_new_priv_params(priv, dev, type))
1224                                 return -1;
1225                         if (mwifiex_sta_init_cmd(priv, false, false))
1226                                 return -1;
1227                         break;
1228                 case NL80211_IFTYPE_ADHOC:
1229                         if (mwifiex_cfg80211_deinit_p2p(priv))
1230                                 return -EFAULT;
1231                         return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1232                                                                type, flags,
1233                                                                params);
1234                         break;
1235                 case NL80211_IFTYPE_AP:
1236                         if (mwifiex_cfg80211_deinit_p2p(priv))
1237                                 return -EFAULT;
1238                         return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1239                                                         flags, params);
1240                 case NL80211_IFTYPE_UNSPECIFIED:
1241                         mwifiex_dbg(priv->adapter, INFO,
1242                                     "%s: kept type as P2P\n", dev->name);
1243                 case NL80211_IFTYPE_P2P_CLIENT:
1244                 case NL80211_IFTYPE_P2P_GO:
1245                         return 0;
1246                 default:
1247                         mwifiex_dbg(priv->adapter, ERROR,
1248                                     "%s: changing to %d not supported\n",
1249                                     dev->name, type);
1250                         return -EOPNOTSUPP;
1251                 }
1252                 break;
1253         default:
1254                 mwifiex_dbg(priv->adapter, ERROR,
1255                             "%s: unknown iftype: %d\n",
1256                             dev->name, dev->ieee80211_ptr->iftype);
1257                 return -EOPNOTSUPP;
1258         }
1259
1260
1261         return 0;
1262 }
1263
1264 static void
1265 mwifiex_parse_htinfo(struct mwifiex_private *priv, u8 tx_htinfo,
1266                      struct rate_info *rate)
1267 {
1268         struct mwifiex_adapter *adapter = priv->adapter;
1269
1270         if (adapter->is_hw_11ac_capable) {
1271                 /* bit[1-0]: 00=LG 01=HT 10=VHT */
1272                 if (tx_htinfo & BIT(0)) {
1273                         /* HT */
1274                         rate->mcs = priv->tx_rate;
1275                         rate->flags |= RATE_INFO_FLAGS_MCS;
1276                 }
1277                 if (tx_htinfo & BIT(1)) {
1278                         /* VHT */
1279                         rate->mcs = priv->tx_rate & 0x0F;
1280                         rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
1281                 }
1282
1283                 if (tx_htinfo & (BIT(1) | BIT(0))) {
1284                         /* HT or VHT */
1285                         switch (tx_htinfo & (BIT(3) | BIT(2))) {
1286                         case 0:
1287                                 rate->bw = RATE_INFO_BW_20;
1288                                 break;
1289                         case (BIT(2)):
1290                                 rate->bw = RATE_INFO_BW_40;
1291                                 break;
1292                         case (BIT(3)):
1293                                 rate->bw = RATE_INFO_BW_80;
1294                                 break;
1295                         case (BIT(3) | BIT(2)):
1296                                 rate->bw = RATE_INFO_BW_160;
1297                                 break;
1298                         }
1299
1300                         if (tx_htinfo & BIT(4))
1301                                 rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1302
1303                         if ((priv->tx_rate >> 4) == 1)
1304                                 rate->nss = 2;
1305                         else
1306                                 rate->nss = 1;
1307                 }
1308         } else {
1309                 /*
1310                  * Bit 0 in tx_htinfo indicates that current Tx rate
1311                  * is 11n rate. Valid MCS index values for us are 0 to 15.
1312                  */
1313                 if ((tx_htinfo & BIT(0)) && (priv->tx_rate < 16)) {
1314                         rate->mcs = priv->tx_rate;
1315                         rate->flags |= RATE_INFO_FLAGS_MCS;
1316                         rate->bw = RATE_INFO_BW_20;
1317                         if (tx_htinfo & BIT(1))
1318                                 rate->bw = RATE_INFO_BW_40;
1319                         if (tx_htinfo & BIT(2))
1320                                 rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1321                 }
1322         }
1323 }
1324
1325 /*
1326  * This function dumps the station information on a buffer.
1327  *
1328  * The following information are shown -
1329  *      - Total bytes transmitted
1330  *      - Total bytes received
1331  *      - Total packets transmitted
1332  *      - Total packets received
1333  *      - Signal quality level
1334  *      - Transmission rate
1335  */
1336 static int
1337 mwifiex_dump_station_info(struct mwifiex_private *priv,
1338                           struct mwifiex_sta_node *node,
1339                           struct station_info *sinfo)
1340 {
1341         u32 rate;
1342
1343         sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) | BIT(NL80211_STA_INFO_TX_BYTES) |
1344                         BIT(NL80211_STA_INFO_RX_PACKETS) | BIT(NL80211_STA_INFO_TX_PACKETS) |
1345                         BIT(NL80211_STA_INFO_TX_BITRATE) |
1346                         BIT(NL80211_STA_INFO_SIGNAL) | BIT(NL80211_STA_INFO_SIGNAL_AVG);
1347
1348         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1349                 if (!node)
1350                         return -ENOENT;
1351
1352                 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1353                                 BIT(NL80211_STA_INFO_TX_FAILED);
1354                 sinfo->inactive_time =
1355                         jiffies_to_msecs(jiffies - node->stats.last_rx);
1356
1357                 sinfo->signal = node->stats.rssi;
1358                 sinfo->signal_avg = node->stats.rssi;
1359                 sinfo->rx_bytes = node->stats.rx_bytes;
1360                 sinfo->tx_bytes = node->stats.tx_bytes;
1361                 sinfo->rx_packets = node->stats.rx_packets;
1362                 sinfo->tx_packets = node->stats.tx_packets;
1363                 sinfo->tx_failed = node->stats.tx_failed;
1364
1365                 mwifiex_parse_htinfo(priv, node->stats.last_tx_htinfo,
1366                                      &sinfo->txrate);
1367                 sinfo->txrate.legacy = node->stats.last_tx_rate * 5;
1368
1369                 return 0;
1370         }
1371
1372         /* Get signal information from the firmware */
1373         if (mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
1374                              HostCmd_ACT_GEN_GET, 0, NULL, true)) {
1375                 mwifiex_dbg(priv->adapter, ERROR,
1376                             "failed to get signal information\n");
1377                 return -EFAULT;
1378         }
1379
1380         if (mwifiex_drv_get_data_rate(priv, &rate)) {
1381                 mwifiex_dbg(priv->adapter, ERROR,
1382                             "getting data rate error\n");
1383                 return -EFAULT;
1384         }
1385
1386         /* Get DTIM period information from firmware */
1387         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
1388                          HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
1389                          &priv->dtim_period, true);
1390
1391         mwifiex_parse_htinfo(priv, priv->tx_htinfo, &sinfo->txrate);
1392
1393         sinfo->signal_avg = priv->bcn_rssi_avg;
1394         sinfo->rx_bytes = priv->stats.rx_bytes;
1395         sinfo->tx_bytes = priv->stats.tx_bytes;
1396         sinfo->rx_packets = priv->stats.rx_packets;
1397         sinfo->tx_packets = priv->stats.tx_packets;
1398         sinfo->signal = priv->bcn_rssi_avg;
1399         /* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
1400         sinfo->txrate.legacy = rate * 5;
1401
1402         if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1403                 sinfo->filled |= BIT(NL80211_STA_INFO_BSS_PARAM);
1404                 sinfo->bss_param.flags = 0;
1405                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1406                                                 WLAN_CAPABILITY_SHORT_PREAMBLE)
1407                         sinfo->bss_param.flags |=
1408                                         BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1409                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1410                                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
1411                         sinfo->bss_param.flags |=
1412                                         BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1413                 sinfo->bss_param.dtim_period = priv->dtim_period;
1414                 sinfo->bss_param.beacon_interval =
1415                         priv->curr_bss_params.bss_descriptor.beacon_period;
1416         }
1417
1418         return 0;
1419 }
1420
1421 /*
1422  * CFG802.11 operation handler to get station information.
1423  *
1424  * This function only works in connected mode, and dumps the
1425  * requested station information, if available.
1426  */
1427 static int
1428 mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1429                              const u8 *mac, struct station_info *sinfo)
1430 {
1431         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1432
1433         if (!priv->media_connected)
1434                 return -ENOENT;
1435         if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1436                 return -ENOENT;
1437
1438         return mwifiex_dump_station_info(priv, NULL, sinfo);
1439 }
1440
1441 /*
1442  * CFG802.11 operation handler to dump station information.
1443  */
1444 static int
1445 mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
1446                               int idx, u8 *mac, struct station_info *sinfo)
1447 {
1448         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1449         struct mwifiex_sta_node *node;
1450         int i;
1451
1452         if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1453             priv->media_connected && idx == 0) {
1454                 ether_addr_copy(mac, priv->cfg_bssid);
1455                 return mwifiex_dump_station_info(priv, NULL, sinfo);
1456         } else if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1457                 mwifiex_send_cmd(priv, HOST_CMD_APCMD_STA_LIST,
1458                                  HostCmd_ACT_GEN_GET, 0, NULL, true);
1459
1460                 i = 0;
1461                 list_for_each_entry(node, &priv->sta_list, list) {
1462                         if (i++ != idx)
1463                                 continue;
1464                         ether_addr_copy(mac, node->mac_addr);
1465                         return mwifiex_dump_station_info(priv, node, sinfo);
1466                 }
1467         }
1468
1469         return -ENOENT;
1470 }
1471
1472 static int
1473 mwifiex_cfg80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1474                              int idx, struct survey_info *survey)
1475 {
1476         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1477         struct mwifiex_chan_stats *pchan_stats = priv->adapter->chan_stats;
1478         enum nl80211_band band;
1479
1480         mwifiex_dbg(priv->adapter, DUMP, "dump_survey idx=%d\n", idx);
1481
1482         memset(survey, 0, sizeof(struct survey_info));
1483
1484         if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1485             priv->media_connected && idx == 0) {
1486                         u8 curr_bss_band = priv->curr_bss_params.band;
1487                         u32 chan = priv->curr_bss_params.bss_descriptor.channel;
1488
1489                         band = mwifiex_band_to_radio_type(curr_bss_band);
1490                         survey->channel = ieee80211_get_channel(wiphy,
1491                                 ieee80211_channel_to_frequency(chan, band));
1492
1493                         if (priv->bcn_nf_last) {
1494                                 survey->filled = SURVEY_INFO_NOISE_DBM;
1495                                 survey->noise = priv->bcn_nf_last;
1496                         }
1497                         return 0;
1498         }
1499
1500         if (idx >= priv->adapter->num_in_chan_stats)
1501                 return -ENOENT;
1502
1503         if (!pchan_stats[idx].cca_scan_dur)
1504                 return 0;
1505
1506         band = pchan_stats[idx].bandcfg;
1507         survey->channel = ieee80211_get_channel(wiphy,
1508             ieee80211_channel_to_frequency(pchan_stats[idx].chan_num, band));
1509         survey->filled = SURVEY_INFO_NOISE_DBM |
1510                          SURVEY_INFO_TIME |
1511                          SURVEY_INFO_TIME_BUSY;
1512         survey->noise = pchan_stats[idx].noise;
1513         survey->time = pchan_stats[idx].cca_scan_dur;
1514         survey->time_busy = pchan_stats[idx].cca_busy_dur;
1515
1516         return 0;
1517 }
1518
1519 /* Supported rates to be advertised to the cfg80211 */
1520 static struct ieee80211_rate mwifiex_rates[] = {
1521         {.bitrate = 10, .hw_value = 2, },
1522         {.bitrate = 20, .hw_value = 4, },
1523         {.bitrate = 55, .hw_value = 11, },
1524         {.bitrate = 110, .hw_value = 22, },
1525         {.bitrate = 60, .hw_value = 12, },
1526         {.bitrate = 90, .hw_value = 18, },
1527         {.bitrate = 120, .hw_value = 24, },
1528         {.bitrate = 180, .hw_value = 36, },
1529         {.bitrate = 240, .hw_value = 48, },
1530         {.bitrate = 360, .hw_value = 72, },
1531         {.bitrate = 480, .hw_value = 96, },
1532         {.bitrate = 540, .hw_value = 108, },
1533 };
1534
1535 /* Channel definitions to be advertised to cfg80211 */
1536 static struct ieee80211_channel mwifiex_channels_2ghz[] = {
1537         {.center_freq = 2412, .hw_value = 1, },
1538         {.center_freq = 2417, .hw_value = 2, },
1539         {.center_freq = 2422, .hw_value = 3, },
1540         {.center_freq = 2427, .hw_value = 4, },
1541         {.center_freq = 2432, .hw_value = 5, },
1542         {.center_freq = 2437, .hw_value = 6, },
1543         {.center_freq = 2442, .hw_value = 7, },
1544         {.center_freq = 2447, .hw_value = 8, },
1545         {.center_freq = 2452, .hw_value = 9, },
1546         {.center_freq = 2457, .hw_value = 10, },
1547         {.center_freq = 2462, .hw_value = 11, },
1548         {.center_freq = 2467, .hw_value = 12, },
1549         {.center_freq = 2472, .hw_value = 13, },
1550         {.center_freq = 2484, .hw_value = 14, },
1551 };
1552
1553 static struct ieee80211_supported_band mwifiex_band_2ghz = {
1554         .channels = mwifiex_channels_2ghz,
1555         .n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
1556         .bitrates = mwifiex_rates,
1557         .n_bitrates = ARRAY_SIZE(mwifiex_rates),
1558 };
1559
1560 static struct ieee80211_channel mwifiex_channels_5ghz[] = {
1561         {.center_freq = 5040, .hw_value = 8, },
1562         {.center_freq = 5060, .hw_value = 12, },
1563         {.center_freq = 5080, .hw_value = 16, },
1564         {.center_freq = 5170, .hw_value = 34, },
1565         {.center_freq = 5190, .hw_value = 38, },
1566         {.center_freq = 5210, .hw_value = 42, },
1567         {.center_freq = 5230, .hw_value = 46, },
1568         {.center_freq = 5180, .hw_value = 36, },
1569         {.center_freq = 5200, .hw_value = 40, },
1570         {.center_freq = 5220, .hw_value = 44, },
1571         {.center_freq = 5240, .hw_value = 48, },
1572         {.center_freq = 5260, .hw_value = 52, },
1573         {.center_freq = 5280, .hw_value = 56, },
1574         {.center_freq = 5300, .hw_value = 60, },
1575         {.center_freq = 5320, .hw_value = 64, },
1576         {.center_freq = 5500, .hw_value = 100, },
1577         {.center_freq = 5520, .hw_value = 104, },
1578         {.center_freq = 5540, .hw_value = 108, },
1579         {.center_freq = 5560, .hw_value = 112, },
1580         {.center_freq = 5580, .hw_value = 116, },
1581         {.center_freq = 5600, .hw_value = 120, },
1582         {.center_freq = 5620, .hw_value = 124, },
1583         {.center_freq = 5640, .hw_value = 128, },
1584         {.center_freq = 5660, .hw_value = 132, },
1585         {.center_freq = 5680, .hw_value = 136, },
1586         {.center_freq = 5700, .hw_value = 140, },
1587         {.center_freq = 5745, .hw_value = 149, },
1588         {.center_freq = 5765, .hw_value = 153, },
1589         {.center_freq = 5785, .hw_value = 157, },
1590         {.center_freq = 5805, .hw_value = 161, },
1591         {.center_freq = 5825, .hw_value = 165, },
1592 };
1593
1594 static struct ieee80211_supported_band mwifiex_band_5ghz = {
1595         .channels = mwifiex_channels_5ghz,
1596         .n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
1597         .bitrates = mwifiex_rates + 4,
1598         .n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
1599 };
1600
1601
1602 /* Supported crypto cipher suits to be advertised to cfg80211 */
1603 static const u32 mwifiex_cipher_suites[] = {
1604         WLAN_CIPHER_SUITE_WEP40,
1605         WLAN_CIPHER_SUITE_WEP104,
1606         WLAN_CIPHER_SUITE_TKIP,
1607         WLAN_CIPHER_SUITE_CCMP,
1608         WLAN_CIPHER_SUITE_SMS4,
1609         WLAN_CIPHER_SUITE_AES_CMAC,
1610 };
1611
1612 /* Supported mgmt frame types to be advertised to cfg80211 */
1613 static const struct ieee80211_txrx_stypes
1614 mwifiex_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1615         [NL80211_IFTYPE_STATION] = {
1616                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1617                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1618                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1619                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1620         },
1621         [NL80211_IFTYPE_AP] = {
1622                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1623                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1624                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1625                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1626         },
1627         [NL80211_IFTYPE_P2P_CLIENT] = {
1628                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1629                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1630                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1631                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1632         },
1633         [NL80211_IFTYPE_P2P_GO] = {
1634                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1635                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1636                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1637                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1638         },
1639 };
1640
1641 /*
1642  * CFG802.11 operation handler for setting bit rates.
1643  *
1644  * Function configures data rates to firmware using bitrate mask
1645  * provided by cfg80211.
1646  */
1647 static int mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1648                                 struct net_device *dev,
1649                                 const u8 *peer,
1650                                 const struct cfg80211_bitrate_mask *mask)
1651 {
1652         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1653         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1654         enum nl80211_band band;
1655         struct mwifiex_adapter *adapter = priv->adapter;
1656
1657         if (!priv->media_connected) {
1658                 mwifiex_dbg(adapter, ERROR,
1659                             "Can not set Tx data rate in disconnected state\n");
1660                 return -EINVAL;
1661         }
1662
1663         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1664
1665         memset(bitmap_rates, 0, sizeof(bitmap_rates));
1666
1667         /* Fill HR/DSSS rates. */
1668         if (band == NL80211_BAND_2GHZ)
1669                 bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1670
1671         /* Fill OFDM rates */
1672         if (band == NL80211_BAND_2GHZ)
1673                 bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1674         else
1675                 bitmap_rates[1] = mask->control[band].legacy;
1676
1677         /* Fill HT MCS rates */
1678         bitmap_rates[2] = mask->control[band].ht_mcs[0];
1679         if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1680                 bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1681
1682        /* Fill VHT MCS rates */
1683         if (adapter->fw_api_ver == MWIFIEX_FW_V15) {
1684                 bitmap_rates[10] = mask->control[band].vht_mcs[0];
1685                 if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1686                         bitmap_rates[11] = mask->control[band].vht_mcs[1];
1687         }
1688
1689         return mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1690                                 HostCmd_ACT_GEN_SET, 0, bitmap_rates, true);
1691 }
1692
1693 /*
1694  * CFG802.11 operation handler for connection quality monitoring.
1695  *
1696  * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
1697  * events to FW.
1698  */
1699 static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1700                                                 struct net_device *dev,
1701                                                 s32 rssi_thold, u32 rssi_hyst)
1702 {
1703         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1704         struct mwifiex_ds_misc_subsc_evt subsc_evt;
1705
1706         priv->cqm_rssi_thold = rssi_thold;
1707         priv->cqm_rssi_hyst = rssi_hyst;
1708
1709         memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
1710         subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1711
1712         /* Subscribe/unsubscribe low and high rssi events */
1713         if (rssi_thold && rssi_hyst) {
1714                 subsc_evt.action = HostCmd_ACT_BITWISE_SET;
1715                 subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1716                 subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1717                 subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1718                 subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1719                 return mwifiex_send_cmd(priv,
1720                                         HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1721                                         0, 0, &subsc_evt, true);
1722         } else {
1723                 subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
1724                 return mwifiex_send_cmd(priv,
1725                                         HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1726                                         0, 0, &subsc_evt, true);
1727         }
1728
1729         return 0;
1730 }
1731
1732 /* cfg80211 operation handler for change_beacon.
1733  * Function retrieves and sets modified management IEs to FW.
1734  */
1735 static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
1736                                           struct net_device *dev,
1737                                           struct cfg80211_beacon_data *data)
1738 {
1739         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1740         struct mwifiex_adapter *adapter = priv->adapter;
1741
1742         mwifiex_cancel_scan(adapter);
1743
1744         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
1745                 mwifiex_dbg(priv->adapter, ERROR,
1746                             "%s: bss_type mismatched\n", __func__);
1747                 return -EINVAL;
1748         }
1749
1750         if (!priv->bss_started) {
1751                 mwifiex_dbg(priv->adapter, ERROR,
1752                             "%s: bss not started\n", __func__);
1753                 return -EINVAL;
1754         }
1755
1756         if (mwifiex_set_mgmt_ies(priv, data)) {
1757                 mwifiex_dbg(priv->adapter, ERROR,
1758                             "%s: setting mgmt ies failed\n", __func__);
1759                 return -EFAULT;
1760         }
1761
1762         return 0;
1763 }
1764
1765 /* cfg80211 operation handler for del_station.
1766  * Function deauthenticates station which value is provided in mac parameter.
1767  * If mac is NULL/broadcast, all stations in associated station list are
1768  * deauthenticated. If bss is not started or there are no stations in
1769  * associated stations list, no action is taken.
1770  */
1771 static int
1772 mwifiex_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1773                              struct station_del_parameters *params)
1774 {
1775         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1776         struct mwifiex_sta_node *sta_node;
1777         u8 deauth_mac[ETH_ALEN];
1778         unsigned long flags;
1779
1780         if (!priv->bss_started && priv->wdev.cac_started) {
1781                 mwifiex_dbg(priv->adapter, INFO, "%s: abort CAC!\n", __func__);
1782                 mwifiex_abort_cac(priv);
1783         }
1784
1785         if (list_empty(&priv->sta_list) || !priv->bss_started)
1786                 return 0;
1787
1788         if (!params->mac || is_broadcast_ether_addr(params->mac))
1789                 return 0;
1790
1791         mwifiex_dbg(priv->adapter, INFO, "%s: mac address %pM\n",
1792                     __func__, params->mac);
1793
1794         eth_zero_addr(deauth_mac);
1795
1796         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
1797         sta_node = mwifiex_get_sta_entry(priv, params->mac);
1798         if (sta_node)
1799                 ether_addr_copy(deauth_mac, params->mac);
1800         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
1801
1802         if (is_valid_ether_addr(deauth_mac)) {
1803                 if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_STA_DEAUTH,
1804                                      HostCmd_ACT_GEN_SET, 0,
1805                                      deauth_mac, true))
1806                         return -1;
1807         }
1808
1809         return 0;
1810 }
1811
1812 static int
1813 mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1814 {
1815         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1816         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1817                                                         MWIFIEX_BSS_ROLE_ANY);
1818         struct mwifiex_ds_ant_cfg ant_cfg;
1819
1820         if (!tx_ant || !rx_ant)
1821                 return -EOPNOTSUPP;
1822
1823         if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1824                 /* Not a MIMO chip. User should provide specific antenna number
1825                  * for Tx/Rx path or enable all antennas for diversity
1826                  */
1827                 if (tx_ant != rx_ant)
1828                         return -EOPNOTSUPP;
1829
1830                 if ((tx_ant & (tx_ant - 1)) &&
1831                     (tx_ant != BIT(adapter->number_of_antenna) - 1))
1832                         return -EOPNOTSUPP;
1833
1834                 if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1835                     (priv->adapter->number_of_antenna > 1)) {
1836                         tx_ant = RF_ANTENNA_AUTO;
1837                         rx_ant = RF_ANTENNA_AUTO;
1838                 }
1839         } else {
1840                 struct ieee80211_sta_ht_cap *ht_info;
1841                 int rx_mcs_supp;
1842                 enum nl80211_band band;
1843
1844                 if ((tx_ant == 0x1 && rx_ant == 0x1)) {
1845                         adapter->user_dev_mcs_support = HT_STREAM_1X1;
1846                         if (adapter->is_hw_11ac_capable)
1847                                 adapter->usr_dot_11ac_mcs_support =
1848                                                 MWIFIEX_11AC_MCS_MAP_1X1;
1849                 } else {
1850                         adapter->user_dev_mcs_support = HT_STREAM_2X2;
1851                         if (adapter->is_hw_11ac_capable)
1852                                 adapter->usr_dot_11ac_mcs_support =
1853                                                 MWIFIEX_11AC_MCS_MAP_2X2;
1854                 }
1855
1856                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1857                         if (!adapter->wiphy->bands[band])
1858                                 continue;
1859
1860                         ht_info = &adapter->wiphy->bands[band]->ht_cap;
1861                         rx_mcs_supp =
1862                                 GET_RXMCSSUPP(adapter->user_dev_mcs_support);
1863                         memset(&ht_info->mcs, 0, adapter->number_of_antenna);
1864                         memset(&ht_info->mcs, 0xff, rx_mcs_supp);
1865                 }
1866         }
1867
1868         ant_cfg.tx_ant = tx_ant;
1869         ant_cfg.rx_ant = rx_ant;
1870
1871         return mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1872                                 HostCmd_ACT_GEN_SET, 0, &ant_cfg, true);
1873 }
1874
1875 static int
1876 mwifiex_cfg80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
1877 {
1878         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1879         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1880                                                         MWIFIEX_BSS_ROLE_ANY);
1881         mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1882                          HostCmd_ACT_GEN_GET, 0, NULL, true);
1883
1884         *tx_ant = priv->tx_ant;
1885         *rx_ant = priv->rx_ant;
1886
1887         return 0;
1888 }
1889
1890 /* cfg80211 operation handler for stop ap.
1891  * Function stops BSS running at uAP interface.
1892  */
1893 static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1894 {
1895         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1896
1897         mwifiex_abort_cac(priv);
1898
1899         if (mwifiex_del_mgmt_ies(priv))
1900                 mwifiex_dbg(priv->adapter, ERROR,
1901                             "Failed to delete mgmt IEs!\n");
1902
1903         priv->ap_11n_enabled = 0;
1904         memset(&priv->bss_cfg, 0, sizeof(priv->bss_cfg));
1905
1906         if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
1907                              HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1908                 mwifiex_dbg(priv->adapter, ERROR,
1909                             "Failed to stop the BSS\n");
1910                 return -1;
1911         }
1912
1913         if (mwifiex_send_cmd(priv, HOST_CMD_APCMD_SYS_RESET,
1914                              HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1915                 mwifiex_dbg(priv->adapter, ERROR,
1916                             "Failed to reset BSS\n");
1917                 return -1;
1918         }
1919
1920         if (netif_carrier_ok(priv->netdev))
1921                 netif_carrier_off(priv->netdev);
1922         mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
1923
1924         return 0;
1925 }
1926
1927 /* cfg80211 operation handler for start_ap.
1928  * Function sets beacon period, DTIM period, SSID and security into
1929  * AP config structure.
1930  * AP is configured with these settings and BSS is started.
1931  */
1932 static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
1933                                      struct net_device *dev,
1934                                      struct cfg80211_ap_settings *params)
1935 {
1936         struct mwifiex_uap_bss_param *bss_cfg;
1937         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1938
1939         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
1940                 return -1;
1941
1942         bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
1943         if (!bss_cfg)
1944                 return -ENOMEM;
1945
1946         mwifiex_set_sys_config_invalid_data(bss_cfg);
1947
1948         if (params->beacon_interval)
1949                 bss_cfg->beacon_period = params->beacon_interval;
1950         if (params->dtim_period)
1951                 bss_cfg->dtim_period = params->dtim_period;
1952
1953         if (params->ssid && params->ssid_len) {
1954                 memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
1955                 bss_cfg->ssid.ssid_len = params->ssid_len;
1956         }
1957         if (params->inactivity_timeout > 0) {
1958                 /* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
1959                 bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
1960                 bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
1961         }
1962
1963         switch (params->hidden_ssid) {
1964         case NL80211_HIDDEN_SSID_NOT_IN_USE:
1965                 bss_cfg->bcast_ssid_ctl = 1;
1966                 break;
1967         case NL80211_HIDDEN_SSID_ZERO_LEN:
1968                 bss_cfg->bcast_ssid_ctl = 0;
1969                 break;
1970         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1971                 /* firmware doesn't support this type of hidden SSID */
1972         default:
1973                 kfree(bss_cfg);
1974                 return -EINVAL;
1975         }
1976
1977         mwifiex_uap_set_channel(priv, bss_cfg, params->chandef);
1978         mwifiex_set_uap_rates(bss_cfg, params);
1979
1980         if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
1981                 mwifiex_dbg(priv->adapter, ERROR,
1982                             "Failed to parse secuirty parameters!\n");
1983                 goto out;
1984         }
1985
1986         mwifiex_set_ht_params(priv, bss_cfg, params);
1987
1988         if (priv->adapter->is_hw_11ac_capable) {
1989                 mwifiex_set_vht_params(priv, bss_cfg, params);
1990                 mwifiex_set_vht_width(priv, params->chandef.width,
1991                                       priv->ap_11ac_enabled);
1992         }
1993
1994         if (priv->ap_11ac_enabled)
1995                 mwifiex_set_11ac_ba_params(priv);
1996         else
1997                 mwifiex_set_ba_params(priv);
1998
1999         mwifiex_set_wmm_params(priv, bss_cfg, params);
2000
2001         if (mwifiex_is_11h_active(priv))
2002                 mwifiex_set_tpc_params(priv, bss_cfg, params);
2003
2004         if (mwifiex_is_11h_active(priv) &&
2005             !cfg80211_chandef_dfs_required(wiphy, &params->chandef,
2006                                            priv->bss_mode)) {
2007                 mwifiex_dbg(priv->adapter, INFO,
2008                             "Disable 11h extensions in FW\n");
2009                 if (mwifiex_11h_activate(priv, false)) {
2010                         mwifiex_dbg(priv->adapter, ERROR,
2011                                     "Failed to disable 11h extensions!!");
2012                         goto out;
2013                 }
2014                 priv->state_11h.is_11h_active = false;
2015         }
2016
2017         if (mwifiex_config_start_uap(priv, bss_cfg)) {
2018                 mwifiex_dbg(priv->adapter, ERROR,
2019                             "Failed to start AP\n");
2020                 goto out;
2021         }
2022
2023         if (mwifiex_set_mgmt_ies(priv, &params->beacon))
2024                 goto out;
2025
2026         if (!netif_carrier_ok(priv->netdev))
2027                 netif_carrier_on(priv->netdev);
2028         mwifiex_wake_up_net_dev_queue(priv->netdev, priv->adapter);
2029
2030         memcpy(&priv->bss_cfg, bss_cfg, sizeof(priv->bss_cfg));
2031         kfree(bss_cfg);
2032         return 0;
2033
2034 out:
2035         kfree(bss_cfg);
2036         return -1;
2037 }
2038
2039 /*
2040  * CFG802.11 operation handler for disconnection request.
2041  *
2042  * This function does not work when there is already a disconnection
2043  * procedure going on.
2044  */
2045 static int
2046 mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
2047                             u16 reason_code)
2048 {
2049         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2050
2051         if (!mwifiex_stop_bg_scan(priv))
2052                 cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy);
2053
2054         if (mwifiex_deauthenticate(priv, NULL))
2055                 return -EFAULT;
2056
2057         eth_zero_addr(priv->cfg_bssid);
2058         priv->hs2_enabled = false;
2059
2060         return 0;
2061 }
2062
2063 /*
2064  * This function informs the CFG802.11 subsystem of a new IBSS.
2065  *
2066  * The following information are sent to the CFG802.11 subsystem
2067  * to register the new IBSS. If we do not register the new IBSS,
2068  * a kernel panic will result.
2069  *      - SSID
2070  *      - SSID length
2071  *      - BSSID
2072  *      - Channel
2073  */
2074 static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
2075 {
2076         struct ieee80211_channel *chan;
2077         struct mwifiex_bss_info bss_info;
2078         struct cfg80211_bss *bss;
2079         int ie_len;
2080         u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
2081         enum nl80211_band band;
2082
2083         if (mwifiex_get_bss_info(priv, &bss_info))
2084                 return -1;
2085
2086         ie_buf[0] = WLAN_EID_SSID;
2087         ie_buf[1] = bss_info.ssid.ssid_len;
2088
2089         memcpy(&ie_buf[sizeof(struct ieee_types_header)],
2090                &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
2091         ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
2092
2093         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
2094         chan = __ieee80211_get_channel(priv->wdev.wiphy,
2095                         ieee80211_channel_to_frequency(bss_info.bss_chan,
2096                                                        band));
2097
2098         bss = cfg80211_inform_bss(priv->wdev.wiphy, chan,
2099                                   CFG80211_BSS_FTYPE_UNKNOWN,
2100                                   bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
2101                                   0, ie_buf, ie_len, 0, GFP_KERNEL);
2102         if (bss) {
2103                 cfg80211_put_bss(priv->wdev.wiphy, bss);
2104                 ether_addr_copy(priv->cfg_bssid, bss_info.bssid);
2105         }
2106
2107         return 0;
2108 }
2109
2110 /*
2111  * This function connects with a BSS.
2112  *
2113  * This function handles both Infra and Ad-Hoc modes. It also performs
2114  * validity checking on the provided parameters, disconnects from the
2115  * current BSS (if any), sets up the association/scan parameters,
2116  * including security settings, and performs specific SSID scan before
2117  * trying to connect.
2118  *
2119  * For Infra mode, the function returns failure if the specified SSID
2120  * is not found in scan table. However, for Ad-Hoc mode, it can create
2121  * the IBSS if it does not exist. On successful completion in either case,
2122  * the function notifies the CFG802.11 subsystem of the new BSS connection.
2123  */
2124 static int
2125 mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len,
2126                        const u8 *ssid, const u8 *bssid, int mode,
2127                        struct ieee80211_channel *channel,
2128                        struct cfg80211_connect_params *sme, bool privacy)
2129 {
2130         struct cfg80211_ssid req_ssid;
2131         int ret, auth_type = 0;
2132         struct cfg80211_bss *bss = NULL;
2133         u8 is_scanning_required = 0;
2134
2135         memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
2136
2137         req_ssid.ssid_len = ssid_len;
2138         if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2139                 mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2140                 return -EINVAL;
2141         }
2142
2143         memcpy(req_ssid.ssid, ssid, ssid_len);
2144         if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
2145                 mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2146                 return -EINVAL;
2147         }
2148
2149         /* As this is new association, clear locally stored
2150          * keys and security related flags */
2151         priv->sec_info.wpa_enabled = false;
2152         priv->sec_info.wpa2_enabled = false;
2153         priv->wep_key_curr_index = 0;
2154         priv->sec_info.encryption_mode = 0;
2155         priv->sec_info.is_authtype_auto = 0;
2156         ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
2157
2158         if (mode == NL80211_IFTYPE_ADHOC) {
2159                 /* "privacy" is set only for ad-hoc mode */
2160                 if (privacy) {
2161                         /*
2162                          * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
2163                          * the firmware can find a matching network from the
2164                          * scan. The cfg80211 does not give us the encryption
2165                          * mode at this stage so just setting it to WEP here.
2166                          */
2167                         priv->sec_info.encryption_mode =
2168                                         WLAN_CIPHER_SUITE_WEP104;
2169                         priv->sec_info.authentication_mode =
2170                                         NL80211_AUTHTYPE_OPEN_SYSTEM;
2171                 }
2172
2173                 goto done;
2174         }
2175
2176         /* Now handle infra mode. "sme" is valid for infra mode only */
2177         if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
2178                 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2179                 priv->sec_info.is_authtype_auto = 1;
2180         } else {
2181                 auth_type = sme->auth_type;
2182         }
2183
2184         if (sme->crypto.n_ciphers_pairwise) {
2185                 priv->sec_info.encryption_mode =
2186                                                 sme->crypto.ciphers_pairwise[0];
2187                 priv->sec_info.authentication_mode = auth_type;
2188         }
2189
2190         if (sme->crypto.cipher_group) {
2191                 priv->sec_info.encryption_mode = sme->crypto.cipher_group;
2192                 priv->sec_info.authentication_mode = auth_type;
2193         }
2194         if (sme->ie)
2195                 ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
2196
2197         if (sme->key) {
2198                 if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
2199                         mwifiex_dbg(priv->adapter, INFO,
2200                                     "info: setting wep encryption\t"
2201                                     "with key len %d\n", sme->key_len);
2202                         priv->wep_key_curr_index = sme->key_idx;
2203                         ret = mwifiex_set_encode(priv, NULL, sme->key,
2204                                                  sme->key_len, sme->key_idx,
2205                                                  NULL, 0);
2206                 }
2207         }
2208 done:
2209         /*
2210          * Scan entries are valid for some time (15 sec). So we can save one
2211          * active scan time if we just try cfg80211_get_bss first. If it fails
2212          * then request scan and cfg80211_get_bss() again for final output.
2213          */
2214         while (1) {
2215                 if (is_scanning_required) {
2216                         /* Do specific SSID scanning */
2217                         if (mwifiex_request_scan(priv, &req_ssid)) {
2218                                 mwifiex_dbg(priv->adapter, ERROR, "scan error\n");
2219                                 return -EFAULT;
2220                         }
2221                 }
2222
2223                 /* Find the BSS we want using available scan results */
2224                 if (mode == NL80211_IFTYPE_ADHOC)
2225                         bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2226                                                bssid, ssid, ssid_len,
2227                                                IEEE80211_BSS_TYPE_IBSS,
2228                                                IEEE80211_PRIVACY_ANY);
2229                 else
2230                         bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2231                                                bssid, ssid, ssid_len,
2232                                                IEEE80211_BSS_TYPE_ESS,
2233                                                IEEE80211_PRIVACY_ANY);
2234
2235                 if (!bss) {
2236                         if (is_scanning_required) {
2237                                 mwifiex_dbg(priv->adapter, WARN,
2238                                             "assoc: requested bss not found in scan results\n");
2239                                 break;
2240                         }
2241                         is_scanning_required = 1;
2242                 } else {
2243                         mwifiex_dbg(priv->adapter, MSG,
2244                                     "info: trying to associate to '%.*s' bssid %pM\n",
2245                                     req_ssid.ssid_len, (char *)req_ssid.ssid,
2246                                     bss->bssid);
2247                         memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
2248                         break;
2249                 }
2250         }
2251
2252         ret = mwifiex_bss_start(priv, bss, &req_ssid);
2253         if (ret)
2254                 return ret;
2255
2256         if (mode == NL80211_IFTYPE_ADHOC) {
2257                 /* Inform the BSS information to kernel, otherwise
2258                  * kernel will give a panic after successful assoc */
2259                 if (mwifiex_cfg80211_inform_ibss_bss(priv))
2260                         return -EFAULT;
2261         }
2262
2263         return ret;
2264 }
2265
2266 /*
2267  * CFG802.11 operation handler for association request.
2268  *
2269  * This function does not work when the current mode is set to Ad-Hoc, or
2270  * when there is already an association procedure going on. The given BSS
2271  * information is used to associate.
2272  */
2273 static int
2274 mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
2275                          struct cfg80211_connect_params *sme)
2276 {
2277         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2278         struct mwifiex_adapter *adapter = priv->adapter;
2279         int ret;
2280
2281         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) {
2282                 mwifiex_dbg(adapter, ERROR,
2283                             "%s: reject infra assoc request in non-STA role\n",
2284                             dev->name);
2285                 return -EINVAL;
2286         }
2287
2288         if (priv->wdev.current_bss) {
2289                 mwifiex_dbg(adapter, ERROR,
2290                             "%s: already connected\n", dev->name);
2291                 return -EALREADY;
2292         }
2293
2294         if (priv->scan_block)
2295                 priv->scan_block = false;
2296
2297         if (adapter->surprise_removed || adapter->is_cmd_timedout) {
2298                 mwifiex_dbg(adapter, ERROR,
2299                             "%s: Ignore connection.\t"
2300                             "Card removed or FW in bad state\n",
2301                             dev->name);
2302                 return -EFAULT;
2303         }
2304
2305         mwifiex_dbg(adapter, INFO,
2306                     "info: Trying to associate to %.*s and bssid %pM\n",
2307                     (int)sme->ssid_len, (char *)sme->ssid, sme->bssid);
2308
2309         if (!mwifiex_stop_bg_scan(priv))
2310                 cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy);
2311
2312         ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
2313                                      priv->bss_mode, sme->channel, sme, 0);
2314         if (!ret) {
2315                 cfg80211_connect_result(priv->netdev, priv->cfg_bssid, NULL, 0,
2316                                         NULL, 0, WLAN_STATUS_SUCCESS,
2317                                         GFP_KERNEL);
2318                 mwifiex_dbg(priv->adapter, MSG,
2319                             "info: associated to bssid %pM successfully\n",
2320                             priv->cfg_bssid);
2321                 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
2322                     priv->adapter->auto_tdls &&
2323                     priv->bss_type == MWIFIEX_BSS_TYPE_STA)
2324                         mwifiex_setup_auto_tdls_timer(priv);
2325         } else {
2326                 mwifiex_dbg(priv->adapter, ERROR,
2327                             "info: association to bssid %pM failed\n",
2328                             priv->cfg_bssid);
2329                 eth_zero_addr(priv->cfg_bssid);
2330
2331                 if (ret > 0)
2332                         cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2333                                                 NULL, 0, NULL, 0, ret,
2334                                                 GFP_KERNEL);
2335                 else
2336                         cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2337                                                 NULL, 0, NULL, 0,
2338                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
2339                                                 GFP_KERNEL);
2340         }
2341
2342         return 0;
2343 }
2344
2345 /*
2346  * This function sets following parameters for ibss network.
2347  *  -  channel
2348  *  -  start band
2349  *  -  11n flag
2350  *  -  secondary channel offset
2351  */
2352 static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
2353                                    struct cfg80211_ibss_params *params)
2354 {
2355         struct mwifiex_adapter *adapter = priv->adapter;
2356         int index = 0, i;
2357         u8 config_bands = 0;
2358
2359         if (params->chandef.chan->band == NL80211_BAND_2GHZ) {
2360                 if (!params->basic_rates) {
2361                         config_bands = BAND_B | BAND_G;
2362                 } else {
2363                         for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
2364                                 /*
2365                                  * Rates below 6 Mbps in the table are CCK
2366                                  * rates; 802.11b and from 6 they are OFDM;
2367                                  * 802.11G
2368                                  */
2369                                 if (mwifiex_rates[i].bitrate == 60) {
2370                                         index = 1 << i;
2371                                         break;
2372                                 }
2373                         }
2374
2375                         if (params->basic_rates < index) {
2376                                 config_bands = BAND_B;
2377                         } else {
2378                                 config_bands = BAND_G;
2379                                 if (params->basic_rates % index)
2380                                         config_bands |= BAND_B;
2381                         }
2382                 }
2383
2384                 if (cfg80211_get_chandef_type(&params->chandef) !=
2385                                                 NL80211_CHAN_NO_HT)
2386                         config_bands |= BAND_G | BAND_GN;
2387         } else {
2388                 if (cfg80211_get_chandef_type(&params->chandef) ==
2389                                                 NL80211_CHAN_NO_HT)
2390                         config_bands = BAND_A;
2391                 else
2392                         config_bands = BAND_AN | BAND_A;
2393         }
2394
2395         if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
2396                 adapter->config_bands = config_bands;
2397                 adapter->adhoc_start_band = config_bands;
2398
2399                 if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
2400                         adapter->adhoc_11n_enabled = true;
2401                 else
2402                         adapter->adhoc_11n_enabled = false;
2403         }
2404
2405         adapter->sec_chan_offset =
2406                 mwifiex_chan_type_to_sec_chan_offset(
2407                         cfg80211_get_chandef_type(&params->chandef));
2408         priv->adhoc_channel = ieee80211_frequency_to_channel(
2409                                 params->chandef.chan->center_freq);
2410
2411         mwifiex_dbg(adapter, INFO,
2412                     "info: set ibss band %d, chan %d, chan offset %d\n",
2413                     config_bands, priv->adhoc_channel,
2414                     adapter->sec_chan_offset);
2415
2416         return 0;
2417 }
2418
2419 /*
2420  * CFG802.11 operation handler to join an IBSS.
2421  *
2422  * This function does not work in any mode other than Ad-Hoc, or if
2423  * a join operation is already in progress.
2424  */
2425 static int
2426 mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2427                            struct cfg80211_ibss_params *params)
2428 {
2429         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2430         int ret = 0;
2431
2432         if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
2433                 mwifiex_dbg(priv->adapter, ERROR,
2434                             "request to join ibss received\t"
2435                             "when station is not in ibss mode\n");
2436                 goto done;
2437         }
2438
2439         mwifiex_dbg(priv->adapter, MSG,
2440                     "info: trying to join to %.*s and bssid %pM\n",
2441                     params->ssid_len, (char *)params->ssid, params->bssid);
2442
2443         mwifiex_set_ibss_params(priv, params);
2444
2445         ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
2446                                      params->bssid, priv->bss_mode,
2447                                      params->chandef.chan, NULL,
2448                                      params->privacy);
2449 done:
2450         if (!ret) {
2451                 cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid,
2452                                      params->chandef.chan, GFP_KERNEL);
2453                 mwifiex_dbg(priv->adapter, MSG,
2454                             "info: joined/created adhoc network with bssid\t"
2455                             "%pM successfully\n", priv->cfg_bssid);
2456         } else {
2457                 mwifiex_dbg(priv->adapter, ERROR,
2458                             "info: failed creating/joining adhoc network\n");
2459         }
2460
2461         return ret;
2462 }
2463
2464 /*
2465  * CFG802.11 operation handler to leave an IBSS.
2466  *
2467  * This function does not work if a leave operation is
2468  * already in progress.
2469  */
2470 static int
2471 mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2472 {
2473         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2474
2475         mwifiex_dbg(priv->adapter, MSG, "info: disconnecting from essid %pM\n",
2476                     priv->cfg_bssid);
2477         if (mwifiex_deauthenticate(priv, NULL))
2478                 return -EFAULT;
2479
2480         eth_zero_addr(priv->cfg_bssid);
2481
2482         return 0;
2483 }
2484
2485 /*
2486  * CFG802.11 operation handler for scan request.
2487  *
2488  * This function issues a scan request to the firmware based upon
2489  * the user specified scan configuration. On successful completion,
2490  * it also informs the results.
2491  */
2492 static int
2493 mwifiex_cfg80211_scan(struct wiphy *wiphy,
2494                       struct cfg80211_scan_request *request)
2495 {
2496         struct net_device *dev = request->wdev->netdev;
2497         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2498         int i, offset, ret;
2499         struct ieee80211_channel *chan;
2500         struct ieee_types_header *ie;
2501         struct mwifiex_user_scan_cfg *user_scan_cfg;
2502
2503         mwifiex_dbg(priv->adapter, CMD,
2504                     "info: received scan request on %s\n", dev->name);
2505
2506         /* Block scan request if scan operation or scan cleanup when interface
2507          * is disabled is in process
2508          */
2509         if (priv->scan_request || priv->scan_aborting) {
2510                 mwifiex_dbg(priv->adapter, WARN,
2511                             "cmd: Scan already in process..\n");
2512                 return -EBUSY;
2513         }
2514
2515         if (!priv->wdev.current_bss && priv->scan_block)
2516                 priv->scan_block = false;
2517
2518         if (!mwifiex_stop_bg_scan(priv))
2519                 cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy);
2520
2521         user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
2522         if (!user_scan_cfg)
2523                 return -ENOMEM;
2524
2525         priv->scan_request = request;
2526
2527         if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2528                 ether_addr_copy(priv->random_mac, request->mac_addr);
2529                 for (i = 0; i < ETH_ALEN; i++) {
2530                         priv->random_mac[i] &= request->mac_addr_mask[i];
2531                         priv->random_mac[i] |= get_random_int() &
2532                                                ~(request->mac_addr_mask[i]);
2533                 }
2534                 ether_addr_copy(user_scan_cfg->random_mac, priv->random_mac);
2535         } else {
2536                 eth_zero_addr(priv->random_mac);
2537         }
2538
2539         user_scan_cfg->num_ssids = request->n_ssids;
2540         user_scan_cfg->ssid_list = request->ssids;
2541
2542         if (request->ie && request->ie_len) {
2543                 offset = 0;
2544                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2545                         if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2546                                 continue;
2547                         priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
2548                         ie = (struct ieee_types_header *)(request->ie + offset);
2549                         memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2550                         offset += sizeof(*ie) + ie->len;
2551
2552                         if (offset >= request->ie_len)
2553                                 break;
2554                 }
2555         }
2556
2557         for (i = 0; i < min_t(u32, request->n_channels,
2558                               MWIFIEX_USER_SCAN_CHAN_MAX); i++) {
2559                 chan = request->channels[i];
2560                 user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
2561                 user_scan_cfg->chan_list[i].radio_type = chan->band;
2562
2563                 if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2564                         user_scan_cfg->chan_list[i].scan_type =
2565                                                 MWIFIEX_SCAN_TYPE_PASSIVE;
2566                 else
2567                         user_scan_cfg->chan_list[i].scan_type =
2568                                                 MWIFIEX_SCAN_TYPE_ACTIVE;
2569
2570                 user_scan_cfg->chan_list[i].scan_time = 0;
2571         }
2572
2573         if (priv->adapter->scan_chan_gap_enabled &&
2574             mwifiex_is_any_intf_active(priv))
2575                 user_scan_cfg->scan_chan_gap =
2576                                               priv->adapter->scan_chan_gap_time;
2577
2578         ret = mwifiex_scan_networks(priv, user_scan_cfg);
2579         kfree(user_scan_cfg);
2580         if (ret) {
2581                 mwifiex_dbg(priv->adapter, ERROR,
2582                             "scan failed: %d\n", ret);
2583                 priv->scan_aborting = false;
2584                 priv->scan_request = NULL;
2585                 return ret;
2586         }
2587
2588         if (request->ie && request->ie_len) {
2589                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2590                         if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
2591                                 priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
2592                                 memset(&priv->vs_ie[i].ie, 0,
2593                                        MWIFIEX_MAX_VSIE_LEN);
2594                         }
2595                 }
2596         }
2597         return 0;
2598 }
2599
2600 /* CFG802.11 operation handler for sched_scan_start.
2601  *
2602  * This function issues a bgscan config request to the firmware based upon
2603  * the user specified sched_scan configuration. On successful completion,
2604  * firmware will generate BGSCAN_REPORT event, driver should issue bgscan
2605  * query command to get sched_scan results from firmware.
2606  */
2607 static int
2608 mwifiex_cfg80211_sched_scan_start(struct wiphy *wiphy,
2609                                   struct net_device *dev,
2610                                   struct cfg80211_sched_scan_request *request)
2611 {
2612         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2613         int i, offset;
2614         struct ieee80211_channel *chan;
2615         struct mwifiex_bg_scan_cfg *bgscan_cfg;
2616         struct ieee_types_header *ie;
2617
2618         if (!request || (!request->n_ssids && !request->n_match_sets)) {
2619                 wiphy_err(wiphy, "%s : Invalid Sched_scan parameters",
2620                           __func__);
2621                 return -EINVAL;
2622         }
2623
2624         wiphy_info(wiphy, "sched_scan start : n_ssids=%d n_match_sets=%d ",
2625                    request->n_ssids, request->n_match_sets);
2626         wiphy_info(wiphy, "n_channels=%d interval=%d ie_len=%d\n",
2627                    request->n_channels, request->scan_plans->interval,
2628                    (int)request->ie_len);
2629
2630         bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
2631         if (!bgscan_cfg)
2632                 return -ENOMEM;
2633
2634         if (priv->scan_request || priv->scan_aborting)
2635                 bgscan_cfg->start_later = true;
2636
2637         bgscan_cfg->num_ssids = request->n_match_sets;
2638         bgscan_cfg->ssid_list = request->match_sets;
2639
2640         if (request->ie && request->ie_len) {
2641                 offset = 0;
2642                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2643                         if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2644                                 continue;
2645                         priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_BGSCAN;
2646                         ie = (struct ieee_types_header *)(request->ie + offset);
2647                         memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2648                         offset += sizeof(*ie) + ie->len;
2649
2650                         if (offset >= request->ie_len)
2651                                 break;
2652                 }
2653         }
2654
2655         for (i = 0; i < min_t(u32, request->n_channels,
2656                               MWIFIEX_BG_SCAN_CHAN_MAX); i++) {
2657                 chan = request->channels[i];
2658                 bgscan_cfg->chan_list[i].chan_number = chan->hw_value;
2659                 bgscan_cfg->chan_list[i].radio_type = chan->band;
2660
2661                 if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2662                         bgscan_cfg->chan_list[i].scan_type =
2663                                                 MWIFIEX_SCAN_TYPE_PASSIVE;
2664                 else
2665                         bgscan_cfg->chan_list[i].scan_type =
2666                                                 MWIFIEX_SCAN_TYPE_ACTIVE;
2667
2668                 bgscan_cfg->chan_list[i].scan_time = 0;
2669         }
2670
2671         bgscan_cfg->chan_per_scan = min_t(u32, request->n_channels,
2672                                           MWIFIEX_BG_SCAN_CHAN_MAX);
2673
2674         /* Use at least 15 second for per scan cycle */
2675         bgscan_cfg->scan_interval = (request->scan_plans->interval >
2676                                      MWIFIEX_BGSCAN_INTERVAL) ?
2677                                 request->scan_plans->interval :
2678                                 MWIFIEX_BGSCAN_INTERVAL;
2679
2680         bgscan_cfg->repeat_count = MWIFIEX_BGSCAN_REPEAT_COUNT;
2681         bgscan_cfg->report_condition = MWIFIEX_BGSCAN_SSID_MATCH |
2682                                 MWIFIEX_BGSCAN_WAIT_ALL_CHAN_DONE;
2683         bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
2684         bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
2685         bgscan_cfg->enable = true;
2686         if (request->min_rssi_thold != NL80211_SCAN_RSSI_THOLD_OFF) {
2687                 bgscan_cfg->report_condition |= MWIFIEX_BGSCAN_SSID_RSSI_MATCH;
2688                 bgscan_cfg->rssi_threshold = request->min_rssi_thold;
2689         }
2690
2691         if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
2692                              HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
2693                 kfree(bgscan_cfg);
2694                 return -EFAULT;
2695         }
2696
2697         priv->sched_scanning = true;
2698
2699         kfree(bgscan_cfg);
2700         return 0;
2701 }
2702
2703 /* CFG802.11 operation handler for sched_scan_stop.
2704  *
2705  * This function issues a bgscan config command to disable
2706  * previous bgscan configuration in the firmware
2707  */
2708 static int mwifiex_cfg80211_sched_scan_stop(struct wiphy *wiphy,
2709                                             struct net_device *dev)
2710 {
2711         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2712
2713         wiphy_info(wiphy, "sched scan stop!");
2714         mwifiex_stop_bg_scan(priv);
2715
2716         return 0;
2717 }
2718
2719 static void mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap *vht_info,
2720                                    struct mwifiex_private *priv)
2721 {
2722         struct mwifiex_adapter *adapter = priv->adapter;
2723
2724         vht_info->vht_supported = true;
2725
2726         vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2727         /* Update MCS support for VHT */
2728         vht_info->vht_mcs.rx_mcs_map = cpu_to_le16(
2729                                 adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2730         vht_info->vht_mcs.rx_highest = 0;
2731         vht_info->vht_mcs.tx_mcs_map = cpu_to_le16(
2732                                 adapter->hw_dot_11ac_mcs_support >> 16);
2733         vht_info->vht_mcs.tx_highest = 0;
2734 }
2735
2736 /*
2737  * This function sets up the CFG802.11 specific HT capability fields
2738  * with default values.
2739  *
2740  * The following default values are set -
2741  *      - HT Supported = True
2742  *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
2743  *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
2744  *      - HT Capabilities supported by firmware
2745  *      - MCS information, Rx mask = 0xff
2746  *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
2747  */
2748 static void
2749 mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
2750                       struct mwifiex_private *priv)
2751 {
2752         int rx_mcs_supp;
2753         struct ieee80211_mcs_info mcs_set;
2754         u8 *mcs = (u8 *)&mcs_set;
2755         struct mwifiex_adapter *adapter = priv->adapter;
2756
2757         ht_info->ht_supported = true;
2758         ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2759         ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2760
2761         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2762
2763         /* Fill HT capability information */
2764         if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2765                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2766         else
2767                 ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2768
2769         if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2770                 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2771         else
2772                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2773
2774         if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2775                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2776         else
2777                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2778
2779         if (adapter->user_dev_mcs_support == HT_STREAM_2X2)
2780                 ht_info->cap |= 2 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2781         else
2782                 ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2783
2784         if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2785                 ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2786         else
2787                 ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2788
2789         if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2790                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2791         else
2792                 ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2793
2794         if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2795                 ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2796         else
2797                 ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2798
2799         if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2800                 ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2801         else
2802                 ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2803
2804         ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2805         ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2806
2807         rx_mcs_supp = GET_RXMCSSUPP(adapter->user_dev_mcs_support);
2808         /* Set MCS for 1x1/2x2 */
2809         memset(mcs, 0xff, rx_mcs_supp);
2810         /* Clear all the other values */
2811         memset(&mcs[rx_mcs_supp], 0,
2812                sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2813         if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2814             ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2815                 /* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2816                 SETHT_MCS32(mcs_set.rx_mask);
2817
2818         memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2819
2820         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2821 }
2822
2823 /*
2824  *  create a new virtual interface with the given name and name assign type
2825  */
2826 struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
2827                                               const char *name,
2828                                               unsigned char name_assign_type,
2829                                               enum nl80211_iftype type,
2830                                               u32 *flags,
2831                                               struct vif_params *params)
2832 {
2833         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2834         struct mwifiex_private *priv;
2835         struct net_device *dev;
2836         void *mdev_priv;
2837         int ret;
2838
2839         if (!adapter)
2840                 return ERR_PTR(-EFAULT);
2841
2842         switch (type) {
2843         case NL80211_IFTYPE_UNSPECIFIED:
2844         case NL80211_IFTYPE_STATION:
2845         case NL80211_IFTYPE_ADHOC:
2846                 if (adapter->curr_iface_comb.sta_intf ==
2847                     adapter->iface_limit.sta_intf) {
2848                         mwifiex_dbg(adapter, ERROR,
2849                                     "cannot create multiple sta/adhoc ifaces\n");
2850                         return ERR_PTR(-EINVAL);
2851                 }
2852
2853                 priv = mwifiex_get_unused_priv_by_bss_type(
2854                                                 adapter, MWIFIEX_BSS_TYPE_STA);
2855                 if (!priv) {
2856                         mwifiex_dbg(adapter, ERROR,
2857                                     "could not get free private struct\n");
2858                         return ERR_PTR(-EFAULT);
2859                 }
2860
2861                 priv->wdev.wiphy = wiphy;
2862                 priv->wdev.iftype = NL80211_IFTYPE_STATION;
2863
2864                 if (type == NL80211_IFTYPE_UNSPECIFIED)
2865                         priv->bss_mode = NL80211_IFTYPE_STATION;
2866                 else
2867                         priv->bss_mode = type;
2868
2869                 priv->bss_type = MWIFIEX_BSS_TYPE_STA;
2870                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2871                 priv->bss_priority = 0;
2872                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2873
2874                 break;
2875         case NL80211_IFTYPE_AP:
2876                 if (adapter->curr_iface_comb.uap_intf ==
2877                     adapter->iface_limit.uap_intf) {
2878                         mwifiex_dbg(adapter, ERROR,
2879                                     "cannot create multiple AP ifaces\n");
2880                         return ERR_PTR(-EINVAL);
2881                 }
2882
2883                 priv = mwifiex_get_unused_priv_by_bss_type(
2884                                                 adapter, MWIFIEX_BSS_TYPE_UAP);
2885                 if (!priv) {
2886                         mwifiex_dbg(adapter, ERROR,
2887                                     "could not get free private struct\n");
2888                         return ERR_PTR(-EFAULT);
2889                 }
2890
2891                 priv->wdev.wiphy = wiphy;
2892                 priv->wdev.iftype = NL80211_IFTYPE_AP;
2893
2894                 priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
2895                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2896                 priv->bss_priority = 0;
2897                 priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
2898                 priv->bss_started = 0;
2899                 priv->bss_mode = type;
2900
2901                 break;
2902         case NL80211_IFTYPE_P2P_CLIENT:
2903                 if (adapter->curr_iface_comb.p2p_intf ==
2904                     adapter->iface_limit.p2p_intf) {
2905                         mwifiex_dbg(adapter, ERROR,
2906                                     "cannot create multiple P2P ifaces\n");
2907                         return ERR_PTR(-EINVAL);
2908                 }
2909
2910                 priv = mwifiex_get_unused_priv_by_bss_type(
2911                                                 adapter, MWIFIEX_BSS_TYPE_P2P);
2912                 if (!priv) {
2913                         mwifiex_dbg(adapter, ERROR,
2914                                     "could not get free private struct\n");
2915                         return ERR_PTR(-EFAULT);
2916                 }
2917
2918                 priv->wdev.wiphy = wiphy;
2919                 /* At start-up, wpa_supplicant tries to change the interface
2920                  * to NL80211_IFTYPE_STATION if it is not managed mode.
2921                  */
2922                 priv->wdev.iftype = NL80211_IFTYPE_P2P_CLIENT;
2923                 priv->bss_mode = NL80211_IFTYPE_P2P_CLIENT;
2924
2925                 /* Setting bss_type to P2P tells firmware that this interface
2926                  * is receiving P2P peers found during find phase and doing
2927                  * action frame handshake.
2928                  */
2929                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
2930
2931                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2932                 priv->bss_priority = MWIFIEX_BSS_ROLE_STA;
2933                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2934                 priv->bss_started = 0;
2935
2936                 if (mwifiex_cfg80211_init_p2p_client(priv)) {
2937                         memset(&priv->wdev, 0, sizeof(priv->wdev));
2938                         priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2939                         return ERR_PTR(-EFAULT);
2940                 }
2941
2942                 break;
2943         default:
2944                 mwifiex_dbg(adapter, ERROR, "type not supported\n");
2945                 return ERR_PTR(-EINVAL);
2946         }
2947
2948         dev = alloc_netdev_mqs(sizeof(struct mwifiex_private *), name,
2949                                name_assign_type, ether_setup,
2950                                IEEE80211_NUM_ACS, 1);
2951         if (!dev) {
2952                 mwifiex_dbg(adapter, ERROR,
2953                             "no memory available for netdevice\n");
2954                 memset(&priv->wdev, 0, sizeof(priv->wdev));
2955                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2956                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2957                 return ERR_PTR(-ENOMEM);
2958         }
2959
2960         mwifiex_init_priv_params(priv, dev);
2961         priv->netdev = dev;
2962
2963         ret = mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
2964                                HostCmd_ACT_GEN_SET, 0, NULL, true);
2965         if (ret)
2966                 return ERR_PTR(ret);
2967
2968         ret = mwifiex_sta_init_cmd(priv, false, false);
2969         if (ret)
2970                 return ERR_PTR(ret);
2971
2972         mwifiex_setup_ht_caps(&wiphy->bands[NL80211_BAND_2GHZ]->ht_cap, priv);
2973         if (adapter->is_hw_11ac_capable)
2974                 mwifiex_setup_vht_caps(
2975                         &wiphy->bands[NL80211_BAND_2GHZ]->vht_cap, priv);
2976
2977         if (adapter->config_bands & BAND_A)
2978                 mwifiex_setup_ht_caps(
2979                         &wiphy->bands[NL80211_BAND_5GHZ]->ht_cap, priv);
2980
2981         if ((adapter->config_bands & BAND_A) && adapter->is_hw_11ac_capable)
2982                 mwifiex_setup_vht_caps(
2983                         &wiphy->bands[NL80211_BAND_5GHZ]->vht_cap, priv);
2984
2985         dev_net_set(dev, wiphy_net(wiphy));
2986         dev->ieee80211_ptr = &priv->wdev;
2987         dev->ieee80211_ptr->iftype = priv->bss_mode;
2988         memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2989         SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
2990
2991         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
2992         dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
2993         dev->hard_header_len += MWIFIEX_MIN_DATA_HEADER_LEN;
2994         dev->ethtool_ops = &mwifiex_ethtool_ops;
2995
2996         mdev_priv = netdev_priv(dev);
2997         *((unsigned long *) mdev_priv) = (unsigned long) priv;
2998
2999         SET_NETDEV_DEV(dev, adapter->dev);
3000
3001         /* Register network device */
3002         if (register_netdevice(dev)) {
3003                 mwifiex_dbg(adapter, ERROR,
3004                             "cannot register virtual network device\n");
3005                 free_netdev(dev);
3006                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3007                 priv->netdev = NULL;
3008                 memset(&priv->wdev, 0, sizeof(priv->wdev));
3009                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3010                 return ERR_PTR(-EFAULT);
3011         }
3012
3013         priv->dfs_cac_workqueue = alloc_workqueue("MWIFIEX_DFS_CAC%s",
3014                                                   WQ_HIGHPRI |
3015                                                   WQ_MEM_RECLAIM |
3016                                                   WQ_UNBOUND, 1, name);
3017         if (!priv->dfs_cac_workqueue) {
3018                 mwifiex_dbg(adapter, ERROR,
3019                             "cannot register virtual network device\n");
3020                 free_netdev(dev);
3021                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3022                 priv->netdev = NULL;
3023                 memset(&priv->wdev, 0, sizeof(priv->wdev));
3024                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3025                 return ERR_PTR(-ENOMEM);
3026         }
3027
3028         INIT_DELAYED_WORK(&priv->dfs_cac_work, mwifiex_dfs_cac_work_queue);
3029
3030         priv->dfs_chan_sw_workqueue = alloc_workqueue("MWIFIEX_DFS_CHSW%s",
3031                                                       WQ_HIGHPRI | WQ_UNBOUND |
3032                                                       WQ_MEM_RECLAIM, 1, name);
3033         if (!priv->dfs_chan_sw_workqueue) {
3034                 mwifiex_dbg(adapter, ERROR,
3035                             "cannot register virtual network device\n");
3036                 free_netdev(dev);
3037                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3038                 priv->netdev = NULL;
3039                 memset(&priv->wdev, 0, sizeof(priv->wdev));
3040                 priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3041                 return ERR_PTR(-ENOMEM);
3042         }
3043
3044         INIT_DELAYED_WORK(&priv->dfs_chan_sw_work,
3045                           mwifiex_dfs_chan_sw_work_queue);
3046
3047         sema_init(&priv->async_sem, 1);
3048
3049         mwifiex_dbg(adapter, INFO,
3050                     "info: %s: Marvell 802.11 Adapter\n", dev->name);
3051
3052 #ifdef CONFIG_DEBUG_FS
3053         mwifiex_dev_debugfs_init(priv);
3054 #endif
3055
3056         switch (type) {
3057         case NL80211_IFTYPE_UNSPECIFIED:
3058         case NL80211_IFTYPE_STATION:
3059         case NL80211_IFTYPE_ADHOC:
3060                 adapter->curr_iface_comb.sta_intf++;
3061                 break;
3062         case NL80211_IFTYPE_AP:
3063                 adapter->curr_iface_comb.uap_intf++;
3064                 break;
3065         case NL80211_IFTYPE_P2P_CLIENT:
3066                 adapter->curr_iface_comb.p2p_intf++;
3067                 break;
3068         default:
3069                 mwifiex_dbg(adapter, ERROR, "type not supported\n");
3070                 return ERR_PTR(-EINVAL);
3071         }
3072
3073         return &priv->wdev;
3074 }
3075 EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
3076
3077 /*
3078  * del_virtual_intf: remove the virtual interface determined by dev
3079  */
3080 int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
3081 {
3082         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3083         struct mwifiex_adapter *adapter = priv->adapter;
3084         struct sk_buff *skb, *tmp;
3085
3086 #ifdef CONFIG_DEBUG_FS
3087         mwifiex_dev_debugfs_remove(priv);
3088 #endif
3089
3090         if (priv->sched_scanning)
3091                 priv->sched_scanning = false;
3092
3093         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
3094
3095         skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
3096                 skb_unlink(skb, &priv->bypass_txq);
3097                 mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
3098         }
3099
3100         if (netif_carrier_ok(priv->netdev))
3101                 netif_carrier_off(priv->netdev);
3102
3103         if (wdev->netdev->reg_state == NETREG_REGISTERED)
3104                 unregister_netdevice(wdev->netdev);
3105
3106         if (priv->dfs_cac_workqueue) {
3107                 flush_workqueue(priv->dfs_cac_workqueue);
3108                 destroy_workqueue(priv->dfs_cac_workqueue);
3109                 priv->dfs_cac_workqueue = NULL;
3110         }
3111
3112         if (priv->dfs_chan_sw_workqueue) {
3113                 flush_workqueue(priv->dfs_chan_sw_workqueue);
3114                 destroy_workqueue(priv->dfs_chan_sw_workqueue);
3115                 priv->dfs_chan_sw_workqueue = NULL;
3116         }
3117         /* Clear the priv in adapter */
3118         priv->netdev->ieee80211_ptr = NULL;
3119         priv->netdev = NULL;
3120         priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3121
3122         priv->media_connected = false;
3123
3124         switch (priv->bss_mode) {
3125         case NL80211_IFTYPE_UNSPECIFIED:
3126         case NL80211_IFTYPE_STATION:
3127         case NL80211_IFTYPE_ADHOC:
3128                 adapter->curr_iface_comb.sta_intf--;
3129                 break;
3130         case NL80211_IFTYPE_AP:
3131                 adapter->curr_iface_comb.uap_intf--;
3132                 break;
3133         case NL80211_IFTYPE_P2P_CLIENT:
3134         case NL80211_IFTYPE_P2P_GO:
3135                 adapter->curr_iface_comb.p2p_intf--;
3136                 break;
3137         default:
3138                 mwifiex_dbg(adapter, ERROR,
3139                             "del_virtual_intf: type not supported\n");
3140                 break;
3141         }
3142
3143         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3144
3145         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
3146             GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
3147                 kfree(priv->hist_data);
3148
3149         return 0;
3150 }
3151 EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
3152
3153 static bool
3154 mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
3155                              u8 max_byte_seq)
3156 {
3157         int j, k, valid_byte_cnt = 0;
3158         bool dont_care_byte = false;
3159
3160         for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
3161                 for (k = 0; k < 8; k++) {
3162                         if (pat->mask[j] & 1 << k) {
3163                                 memcpy(byte_seq + valid_byte_cnt,
3164                                        &pat->pattern[j * 8 + k], 1);
3165                                 valid_byte_cnt++;
3166                                 if (dont_care_byte)
3167                                         return false;
3168                         } else {
3169                                 if (valid_byte_cnt)
3170                                         dont_care_byte = true;
3171                         }
3172
3173                         /* wildcard bytes record as the offset
3174                          * before the valid byte
3175                          */
3176                         if (!valid_byte_cnt && !dont_care_byte)
3177                                 pat->pkt_offset++;
3178
3179                         if (valid_byte_cnt > max_byte_seq)
3180                                 return false;
3181                 }
3182         }
3183
3184         byte_seq[max_byte_seq] = valid_byte_cnt;
3185
3186         return true;
3187 }
3188
3189 #ifdef CONFIG_PM
3190 static void mwifiex_set_auto_arp_mef_entry(struct mwifiex_private *priv,
3191                                            struct mwifiex_mef_entry *mef_entry)
3192 {
3193         int i, filt_num = 0, num_ipv4 = 0;
3194         struct in_device *in_dev;
3195         struct in_ifaddr *ifa;
3196         __be32 ips[MWIFIEX_MAX_SUPPORTED_IPADDR];
3197         struct mwifiex_adapter *adapter = priv->adapter;
3198
3199         mef_entry->mode = MEF_MODE_HOST_SLEEP;
3200         mef_entry->action = MEF_ACTION_AUTO_ARP;
3201
3202         /* Enable ARP offload feature */
3203         memset(ips, 0, sizeof(ips));
3204         for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
3205                 if (adapter->priv[i]->netdev) {
3206                         in_dev = __in_dev_get_rtnl(adapter->priv[i]->netdev);
3207                         if (!in_dev)
3208                                 continue;
3209                         ifa = in_dev->ifa_list;
3210                         if (!ifa || !ifa->ifa_local)
3211                                 continue;
3212                         ips[i] = ifa->ifa_local;
3213                         num_ipv4++;
3214                 }
3215         }
3216
3217         for (i = 0; i < num_ipv4; i++) {
3218                 if (!ips[i])
3219                         continue;
3220                 mef_entry->filter[filt_num].repeat = 1;
3221                 memcpy(mef_entry->filter[filt_num].byte_seq,
3222                        (u8 *)&ips[i], sizeof(ips[i]));
3223                 mef_entry->filter[filt_num].
3224                         byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3225                         sizeof(ips[i]);
3226                 mef_entry->filter[filt_num].offset = 46;
3227                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3228                 if (filt_num) {
3229                         mef_entry->filter[filt_num].filt_action =
3230                                 TYPE_OR;
3231                 }
3232                 filt_num++;
3233         }
3234
3235         mef_entry->filter[filt_num].repeat = 1;
3236         mef_entry->filter[filt_num].byte_seq[0] = 0x08;
3237         mef_entry->filter[filt_num].byte_seq[1] = 0x06;
3238         mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] = 2;
3239         mef_entry->filter[filt_num].offset = 20;
3240         mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3241         mef_entry->filter[filt_num].filt_action = TYPE_AND;
3242 }
3243
3244 static int mwifiex_set_wowlan_mef_entry(struct mwifiex_private *priv,
3245                                         struct mwifiex_ds_mef_cfg *mef_cfg,
3246                                         struct mwifiex_mef_entry *mef_entry,
3247                                         struct cfg80211_wowlan *wowlan)
3248 {
3249         int i, filt_num = 0, ret = 0;
3250         bool first_pat = true;
3251         u8 byte_seq[MWIFIEX_MEF_MAX_BYTESEQ + 1];
3252         const u8 ipv4_mc_mac[] = {0x33, 0x33};
3253         const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3254
3255         mef_entry->mode = MEF_MODE_HOST_SLEEP;
3256         mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
3257
3258         for (i = 0; i < wowlan->n_patterns; i++) {
3259                 memset(byte_seq, 0, sizeof(byte_seq));
3260                 if (!mwifiex_is_pattern_supported(&wowlan->patterns[i],
3261                                         byte_seq,
3262                                         MWIFIEX_MEF_MAX_BYTESEQ)) {
3263                         mwifiex_dbg(priv->adapter, ERROR,
3264                                     "Pattern not supported\n");
3265                         return -EOPNOTSUPP;
3266                 }
3267
3268                 if (!wowlan->patterns[i].pkt_offset) {
3269                         if (!(byte_seq[0] & 0x01) &&
3270                             (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 1)) {
3271                                 mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3272                                 continue;
3273                         } else if (is_broadcast_ether_addr(byte_seq)) {
3274                                 mef_cfg->criteria |= MWIFIEX_CRITERIA_BROADCAST;
3275                                 continue;
3276                         } else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3277                                     (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 2)) ||
3278                                    (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3279                                     (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 3))) {
3280                                 mef_cfg->criteria |= MWIFIEX_CRITERIA_MULTICAST;
3281                                 continue;
3282                         }
3283                 }
3284                 mef_entry->filter[filt_num].repeat = 1;
3285                 mef_entry->filter[filt_num].offset =
3286                         wowlan->patterns[i].pkt_offset;
3287                 memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
3288                                 sizeof(byte_seq));
3289                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3290
3291                 if (first_pat) {
3292                         first_pat = false;
3293                         mwifiex_dbg(priv->adapter, INFO, "Wake on patterns\n");
3294                 } else {
3295                         mef_entry->filter[filt_num].filt_action = TYPE_AND;
3296                 }
3297
3298                 filt_num++;
3299         }
3300
3301         if (wowlan->magic_pkt) {
3302                 mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3303                 mef_entry->filter[filt_num].repeat = 16;
3304                 memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3305                                 ETH_ALEN);
3306                 mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3307                         ETH_ALEN;
3308                 mef_entry->filter[filt_num].offset = 28;
3309                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3310                 if (filt_num)
3311                         mef_entry->filter[filt_num].filt_action = TYPE_OR;
3312
3313                 filt_num++;
3314                 mef_entry->filter[filt_num].repeat = 16;
3315                 memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3316                                 ETH_ALEN);
3317                 mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3318                         ETH_ALEN;
3319                 mef_entry->filter[filt_num].offset = 56;
3320                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3321                 mef_entry->filter[filt_num].filt_action = TYPE_OR;
3322                 mwifiex_dbg(priv->adapter, INFO, "Wake on magic packet\n");
3323         }
3324         return ret;
3325 }
3326
3327 static int mwifiex_set_mef_filter(struct mwifiex_private *priv,
3328                                   struct cfg80211_wowlan *wowlan)
3329 {
3330         int ret = 0, num_entries = 1;
3331         struct mwifiex_ds_mef_cfg mef_cfg;
3332         struct mwifiex_mef_entry *mef_entry;
3333
3334         if (wowlan->n_patterns || wowlan->magic_pkt)
3335                 num_entries++;
3336
3337         mef_entry = kcalloc(num_entries, sizeof(*mef_entry), GFP_KERNEL);
3338         if (!mef_entry)
3339                 return -ENOMEM;
3340
3341         memset(&mef_cfg, 0, sizeof(mef_cfg));
3342         mef_cfg.criteria |= MWIFIEX_CRITERIA_BROADCAST |
3343                 MWIFIEX_CRITERIA_UNICAST;
3344         mef_cfg.num_entries = num_entries;
3345         mef_cfg.mef_entry = mef_entry;
3346
3347         mwifiex_set_auto_arp_mef_entry(priv, &mef_entry[0]);
3348
3349         if (wowlan->n_patterns || wowlan->magic_pkt) {
3350                 ret = mwifiex_set_wowlan_mef_entry(priv, &mef_cfg,
3351                                                    &mef_entry[1], wowlan);
3352                 if (ret)
3353                         goto err;
3354         }
3355
3356         if (!mef_cfg.criteria)
3357                 mef_cfg.criteria = MWIFIEX_CRITERIA_BROADCAST |
3358                         MWIFIEX_CRITERIA_UNICAST |
3359                         MWIFIEX_CRITERIA_MULTICAST;
3360
3361         ret = mwifiex_send_cmd(priv, HostCmd_CMD_MEF_CFG,
3362                         HostCmd_ACT_GEN_SET, 0,
3363                         &mef_cfg, true);
3364
3365 err:
3366         kfree(mef_entry);
3367         return ret;
3368 }
3369
3370 static int mwifiex_cfg80211_suspend(struct wiphy *wiphy,
3371                                     struct cfg80211_wowlan *wowlan)
3372 {
3373         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3374         struct mwifiex_ds_hs_cfg hs_cfg;
3375         int i, ret = 0, retry_num = 10;
3376         struct mwifiex_private *priv;
3377         struct mwifiex_private *sta_priv =
3378                         mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3379
3380         sta_priv->scan_aborting = true;
3381         for (i = 0; i < adapter->priv_num; i++) {
3382                 priv = adapter->priv[i];
3383                 mwifiex_abort_cac(priv);
3384         }
3385
3386         mwifiex_cancel_all_pending_cmd(adapter);
3387
3388         for (i = 0; i < adapter->priv_num; i++) {
3389                 priv = adapter->priv[i];
3390                 if (priv && priv->netdev) {
3391                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
3392                         if (netif_carrier_ok(priv->netdev))
3393                                 netif_carrier_off(priv->netdev);
3394                 }
3395         }
3396
3397         for (i = 0; i < retry_num; i++) {
3398                 if (!mwifiex_wmm_lists_empty(adapter) ||
3399                     !mwifiex_bypass_txlist_empty(adapter) ||
3400                     !skb_queue_empty(&adapter->tx_data_q))
3401                         usleep_range(10000, 15000);
3402                 else
3403                         break;
3404         }
3405
3406         if (!wowlan) {
3407                 mwifiex_dbg(adapter, ERROR,
3408                             "None of the WOWLAN triggers enabled\n");
3409                 ret = 0;
3410                 goto done;
3411         }
3412
3413         if (!sta_priv->media_connected && !wowlan->nd_config) {
3414                 mwifiex_dbg(adapter, ERROR,
3415                             "Can not configure WOWLAN in disconnected state\n");
3416                 ret = 0;
3417                 goto done;
3418         }
3419
3420         ret = mwifiex_set_mef_filter(sta_priv, wowlan);
3421         if (ret) {
3422                 mwifiex_dbg(adapter, ERROR, "Failed to set MEF filter\n");
3423                 goto done;
3424         }
3425
3426         memset(&hs_cfg, 0, sizeof(hs_cfg));
3427         hs_cfg.conditions = le32_to_cpu(adapter->hs_cfg.conditions);
3428
3429         if (wowlan->nd_config) {
3430                 mwifiex_dbg(adapter, INFO, "Wake on net detect\n");
3431                 hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3432                 mwifiex_cfg80211_sched_scan_start(wiphy, sta_priv->netdev,
3433                                                   wowlan->nd_config);
3434         }
3435
3436         if (wowlan->disconnect) {
3437                 hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3438                 mwifiex_dbg(sta_priv->adapter, INFO, "Wake on device disconnect\n");
3439         }
3440
3441         hs_cfg.is_invoke_hostcmd = false;
3442         hs_cfg.gpio = adapter->hs_cfg.gpio;
3443         hs_cfg.gap = adapter->hs_cfg.gap;
3444         ret = mwifiex_set_hs_params(sta_priv, HostCmd_ACT_GEN_SET,
3445                                     MWIFIEX_SYNC_CMD, &hs_cfg);
3446         if (ret)
3447                 mwifiex_dbg(adapter, ERROR, "Failed to set HS params\n");
3448
3449 done:
3450         sta_priv->scan_aborting = false;
3451         return ret;
3452 }
3453
3454 static int mwifiex_cfg80211_resume(struct wiphy *wiphy)
3455 {
3456         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3457         struct mwifiex_private *priv;
3458         struct mwifiex_ds_wakeup_reason wakeup_reason;
3459         struct cfg80211_wowlan_wakeup wakeup_report;
3460         int i;
3461         bool report_wakeup_reason = true;
3462
3463         for (i = 0; i < adapter->priv_num; i++) {
3464                 priv = adapter->priv[i];
3465                 if (priv && priv->netdev) {
3466                         if (!netif_carrier_ok(priv->netdev))
3467                                 netif_carrier_on(priv->netdev);
3468                         mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
3469                 }
3470         }
3471
3472         if (!wiphy->wowlan_config)
3473                 goto done;
3474
3475         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3476         mwifiex_get_wakeup_reason(priv, HostCmd_ACT_GEN_GET, MWIFIEX_SYNC_CMD,
3477                                   &wakeup_reason);
3478         memset(&wakeup_report, 0, sizeof(struct cfg80211_wowlan_wakeup));
3479
3480         wakeup_report.pattern_idx = -1;
3481
3482         switch (wakeup_reason.hs_wakeup_reason) {
3483         case NO_HSWAKEUP_REASON:
3484                 break;
3485         case BCAST_DATA_MATCHED:
3486                 break;
3487         case MCAST_DATA_MATCHED:
3488                 break;
3489         case UCAST_DATA_MATCHED:
3490                 break;
3491         case MASKTABLE_EVENT_MATCHED:
3492                 break;
3493         case NON_MASKABLE_EVENT_MATCHED:
3494                 if (wiphy->wowlan_config->disconnect)
3495                         wakeup_report.disconnect = true;
3496                 if (wiphy->wowlan_config->nd_config)
3497                         wakeup_report.net_detect = adapter->nd_info;
3498                 break;
3499         case NON_MASKABLE_CONDITION_MATCHED:
3500                 break;
3501         case MAGIC_PATTERN_MATCHED:
3502                 if (wiphy->wowlan_config->magic_pkt)
3503                         wakeup_report.magic_pkt = true;
3504                 if (wiphy->wowlan_config->n_patterns)
3505                         wakeup_report.pattern_idx = 1;
3506                 break;
3507         case GTK_REKEY_FAILURE:
3508                 if (wiphy->wowlan_config->gtk_rekey_failure)
3509                         wakeup_report.gtk_rekey_failure = true;
3510                 break;
3511         default:
3512                 report_wakeup_reason = false;
3513                 break;
3514         }
3515
3516         if (report_wakeup_reason)
3517                 cfg80211_report_wowlan_wakeup(&priv->wdev, &wakeup_report,
3518                                               GFP_KERNEL);
3519
3520 done:
3521         if (adapter->nd_info) {
3522                 for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
3523                         kfree(adapter->nd_info->matches[i]);
3524                 kfree(adapter->nd_info);
3525                 adapter->nd_info = NULL;
3526         }
3527
3528         return 0;
3529 }
3530
3531 static void mwifiex_cfg80211_set_wakeup(struct wiphy *wiphy,
3532                                        bool enabled)
3533 {
3534         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3535
3536         device_set_wakeup_enable(adapter->dev, enabled);
3537 }
3538
3539 static int mwifiex_set_rekey_data(struct wiphy *wiphy, struct net_device *dev,
3540                                   struct cfg80211_gtk_rekey_data *data)
3541 {
3542         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3543
3544         return mwifiex_send_cmd(priv, HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG,
3545                                 HostCmd_ACT_GEN_SET, 0, data, true);
3546 }
3547
3548 #endif
3549
3550 static int mwifiex_get_coalesce_pkt_type(u8 *byte_seq)
3551 {
3552         const u8 ipv4_mc_mac[] = {0x33, 0x33};
3553         const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3554         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff};
3555
3556         if ((byte_seq[0] & 0x01) &&
3557             (byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 1))
3558                 return PACKET_TYPE_UNICAST;
3559         else if (!memcmp(byte_seq, bc_mac, 4))
3560                 return PACKET_TYPE_BROADCAST;
3561         else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3562                   byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 2) ||
3563                  (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3564                   byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 3))
3565                 return PACKET_TYPE_MULTICAST;
3566
3567         return 0;
3568 }
3569
3570 static int
3571 mwifiex_fill_coalesce_rule_info(struct mwifiex_private *priv,
3572                                 struct cfg80211_coalesce_rules *crule,
3573                                 struct mwifiex_coalesce_rule *mrule)
3574 {
3575         u8 byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ + 1];
3576         struct filt_field_param *param;
3577         int i;
3578
3579         mrule->max_coalescing_delay = crule->delay;
3580
3581         param = mrule->params;
3582
3583         for (i = 0; i < crule->n_patterns; i++) {
3584                 memset(byte_seq, 0, sizeof(byte_seq));
3585                 if (!mwifiex_is_pattern_supported(&crule->patterns[i],
3586                                                   byte_seq,
3587                                                 MWIFIEX_COALESCE_MAX_BYTESEQ)) {
3588                         mwifiex_dbg(priv->adapter, ERROR,
3589                                     "Pattern not supported\n");
3590                         return -EOPNOTSUPP;
3591                 }
3592
3593                 if (!crule->patterns[i].pkt_offset) {
3594                         u8 pkt_type;
3595
3596                         pkt_type = mwifiex_get_coalesce_pkt_type(byte_seq);
3597                         if (pkt_type && mrule->pkt_type) {
3598                                 mwifiex_dbg(priv->adapter, ERROR,
3599                                             "Multiple packet types not allowed\n");
3600                                 return -EOPNOTSUPP;
3601                         } else if (pkt_type) {
3602                                 mrule->pkt_type = pkt_type;
3603                                 continue;
3604                         }
3605                 }
3606
3607                 if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
3608                         param->operation = RECV_FILTER_MATCH_TYPE_EQ;
3609                 else
3610                         param->operation = RECV_FILTER_MATCH_TYPE_NE;
3611
3612                 param->operand_len = byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ];
3613                 memcpy(param->operand_byte_stream, byte_seq,
3614                        param->operand_len);
3615                 param->offset = crule->patterns[i].pkt_offset;
3616                 param++;
3617
3618                 mrule->num_of_fields++;
3619         }
3620
3621         if (!mrule->pkt_type) {
3622                 mwifiex_dbg(priv->adapter, ERROR,
3623                             "Packet type can not be determined\n");
3624                 return -EOPNOTSUPP;
3625         }
3626
3627         return 0;
3628 }
3629
3630 static int mwifiex_cfg80211_set_coalesce(struct wiphy *wiphy,
3631                                          struct cfg80211_coalesce *coalesce)
3632 {
3633         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3634         int i, ret;
3635         struct mwifiex_ds_coalesce_cfg coalesce_cfg;
3636         struct mwifiex_private *priv =
3637                         mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3638
3639         memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
3640         if (!coalesce) {
3641                 mwifiex_dbg(adapter, WARN,
3642                             "Disable coalesce and reset all previous rules\n");
3643                 return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3644                                         HostCmd_ACT_GEN_SET, 0,
3645                                         &coalesce_cfg, true);
3646         }
3647
3648         coalesce_cfg.num_of_rules = coalesce->n_rules;
3649         for (i = 0; i < coalesce->n_rules; i++) {
3650                 ret = mwifiex_fill_coalesce_rule_info(priv, &coalesce->rules[i],
3651                                                       &coalesce_cfg.rule[i]);
3652                 if (ret) {
3653                         mwifiex_dbg(adapter, ERROR,
3654                                     "Recheck the patterns provided for rule %d\n",
3655                                 i + 1);
3656                         return ret;
3657                 }
3658         }
3659
3660         return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3661                                 HostCmd_ACT_GEN_SET, 0, &coalesce_cfg, true);
3662 }
3663
3664 /* cfg80211 ops handler for tdls_mgmt.
3665  * Function prepares TDLS action frame packets and forwards them to FW
3666  */
3667 static int
3668 mwifiex_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3669                            const u8 *peer, u8 action_code, u8 dialog_token,
3670                            u16 status_code, u32 peer_capability,
3671                            bool initiator, const u8 *extra_ies,
3672                            size_t extra_ies_len)
3673 {
3674         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3675         int ret;
3676
3677         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3678                 return -ENOTSUPP;
3679
3680         /* make sure we are in station mode and connected */
3681         if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3682                 return -ENOTSUPP;
3683
3684         switch (action_code) {
3685         case WLAN_TDLS_SETUP_REQUEST:
3686                 mwifiex_dbg(priv->adapter, MSG,
3687                             "Send TDLS Setup Request to %pM status_code=%d\n",
3688                             peer, status_code);
3689                 mwifiex_add_auto_tdls_peer(priv, peer);
3690                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3691                                                    dialog_token, status_code,
3692                                                    extra_ies, extra_ies_len);
3693                 break;
3694         case WLAN_TDLS_SETUP_RESPONSE:
3695                 mwifiex_add_auto_tdls_peer(priv, peer);
3696                 mwifiex_dbg(priv->adapter, MSG,
3697                             "Send TDLS Setup Response to %pM status_code=%d\n",
3698                             peer, status_code);
3699                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3700                                                    dialog_token, status_code,
3701                                                    extra_ies, extra_ies_len);
3702                 break;
3703         case WLAN_TDLS_SETUP_CONFIRM:
3704                 mwifiex_dbg(priv->adapter, MSG,
3705                             "Send TDLS Confirm to %pM status_code=%d\n", peer,
3706                             status_code);
3707                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3708                                                    dialog_token, status_code,
3709                                                    extra_ies, extra_ies_len);
3710                 break;
3711         case WLAN_TDLS_TEARDOWN:
3712                 mwifiex_dbg(priv->adapter, MSG,
3713                             "Send TDLS Tear down to %pM\n", peer);
3714                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3715                                                    dialog_token, status_code,
3716                                                    extra_ies, extra_ies_len);
3717                 break;
3718         case WLAN_TDLS_DISCOVERY_REQUEST:
3719                 mwifiex_dbg(priv->adapter, MSG,
3720                             "Send TDLS Discovery Request to %pM\n", peer);
3721                 ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3722                                                    dialog_token, status_code,
3723                                                    extra_ies, extra_ies_len);
3724                 break;
3725         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3726                 mwifiex_dbg(priv->adapter, MSG,
3727                             "Send TDLS Discovery Response to %pM\n", peer);
3728                 ret = mwifiex_send_tdls_action_frame(priv, peer, action_code,
3729                                                    dialog_token, status_code,
3730                                                    extra_ies, extra_ies_len);
3731                 break;
3732         default:
3733                 mwifiex_dbg(priv->adapter, ERROR,
3734                             "Unknown TDLS mgmt/action frame %pM\n", peer);
3735                 ret = -EINVAL;
3736                 break;
3737         }
3738
3739         return ret;
3740 }
3741
3742 static int
3743 mwifiex_cfg80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3744                            const u8 *peer, enum nl80211_tdls_operation action)
3745 {
3746         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3747
3748         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
3749             !(wiphy->flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3750                 return -ENOTSUPP;
3751
3752         /* make sure we are in station mode and connected */
3753         if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3754                 return -ENOTSUPP;
3755
3756         mwifiex_dbg(priv->adapter, MSG,
3757                     "TDLS peer=%pM, oper=%d\n", peer, action);
3758
3759         switch (action) {
3760         case NL80211_TDLS_ENABLE_LINK:
3761                 action = MWIFIEX_TDLS_ENABLE_LINK;
3762                 break;
3763         case NL80211_TDLS_DISABLE_LINK:
3764                 action = MWIFIEX_TDLS_DISABLE_LINK;
3765                 break;
3766         case NL80211_TDLS_TEARDOWN:
3767                 /* shouldn't happen!*/
3768                 mwifiex_dbg(priv->adapter, ERROR,
3769                             "tdls_oper: teardown from driver not supported\n");
3770                 return -EINVAL;
3771         case NL80211_TDLS_SETUP:
3772                 /* shouldn't happen!*/
3773                 mwifiex_dbg(priv->adapter, ERROR,
3774                             "tdls_oper: setup from driver not supported\n");
3775                 return -EINVAL;
3776         case NL80211_TDLS_DISCOVERY_REQ:
3777                 /* shouldn't happen!*/
3778                 mwifiex_dbg(priv->adapter, ERROR,
3779                             "tdls_oper: discovery from driver not supported\n");
3780                 return -EINVAL;
3781         default:
3782                 mwifiex_dbg(priv->adapter, ERROR,
3783                             "tdls_oper: operation not supported\n");
3784                 return -ENOTSUPP;
3785         }
3786
3787         return mwifiex_tdls_oper(priv, peer, action);
3788 }
3789
3790 static int
3791 mwifiex_cfg80211_tdls_chan_switch(struct wiphy *wiphy, struct net_device *dev,
3792                                   const u8 *addr, u8 oper_class,
3793                                   struct cfg80211_chan_def *chandef)
3794 {
3795         struct mwifiex_sta_node *sta_ptr;
3796         unsigned long flags;
3797         u16 chan;
3798         u8 second_chan_offset, band;
3799         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3800
3801         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
3802         sta_ptr = mwifiex_get_sta_entry(priv, addr);
3803         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
3804
3805         if (!sta_ptr) {
3806                 wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3807                           __func__, addr);
3808                 return -ENOENT;
3809         }
3810
3811         if (!(sta_ptr->tdls_cap.extcap.ext_capab[3] &
3812               WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)) {
3813                 wiphy_err(wiphy, "%pM do not support tdls cs\n", addr);
3814                 return -ENOENT;
3815         }
3816
3817         if (sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3818             sta_ptr->tdls_status == TDLS_IN_OFF_CHAN) {
3819                 wiphy_err(wiphy, "channel switch is running, abort request\n");
3820                 return -EALREADY;
3821         }
3822
3823         chan = chandef->chan->hw_value;
3824         second_chan_offset = mwifiex_get_sec_chan_offset(chan);
3825         band = chandef->chan->band;
3826         mwifiex_start_tdls_cs(priv, addr, chan, second_chan_offset, band);
3827
3828         return 0;
3829 }
3830
3831 static void
3832 mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy *wiphy,
3833                                          struct net_device *dev,
3834                                          const u8 *addr)
3835 {
3836         struct mwifiex_sta_node *sta_ptr;
3837         unsigned long flags;
3838         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3839
3840         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
3841         sta_ptr = mwifiex_get_sta_entry(priv, addr);
3842         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
3843
3844         if (!sta_ptr) {
3845                 wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3846                           __func__, addr);
3847         } else if (!(sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3848                      sta_ptr->tdls_status == TDLS_IN_BASE_CHAN ||
3849                      sta_ptr->tdls_status == TDLS_IN_OFF_CHAN)) {
3850                 wiphy_err(wiphy, "tdls chan switch not initialize by %pM\n",
3851                           addr);
3852         } else
3853                 mwifiex_stop_tdls_cs(priv, addr);
3854 }
3855
3856 static int
3857 mwifiex_cfg80211_add_station(struct wiphy *wiphy, struct net_device *dev,
3858                              const u8 *mac, struct station_parameters *params)
3859 {
3860         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3861
3862         if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3863                 return -ENOTSUPP;
3864
3865         /* make sure we are in station mode and connected */
3866         if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
3867                 return -ENOTSUPP;
3868
3869         return mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CREATE_LINK);
3870 }
3871
3872 static int
3873 mwifiex_cfg80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3874                                 struct cfg80211_csa_settings *params)
3875 {
3876         struct ieee_types_header *chsw_ie;
3877         struct ieee80211_channel_sw_ie *channel_sw;
3878         int chsw_msec;
3879         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3880
3881         if (priv->adapter->scan_processing) {
3882                 mwifiex_dbg(priv->adapter, ERROR,
3883                             "radar detection: scan in process...\n");
3884                 return -EBUSY;
3885         }
3886
3887         if (priv->wdev.cac_started)
3888                 return -EBUSY;
3889
3890         if (cfg80211_chandef_identical(&params->chandef,
3891                                        &priv->dfs_chandef))
3892                 return -EINVAL;
3893
3894         chsw_ie = (void *)cfg80211_find_ie(WLAN_EID_CHANNEL_SWITCH,
3895                                            params->beacon_csa.tail,
3896                                            params->beacon_csa.tail_len);
3897         if (!chsw_ie) {
3898                 mwifiex_dbg(priv->adapter, ERROR,
3899                             "Could not parse channel switch announcement IE\n");
3900                 return -EINVAL;
3901         }
3902
3903         channel_sw = (void *)(chsw_ie + 1);
3904         if (channel_sw->mode) {
3905                 if (netif_carrier_ok(priv->netdev))
3906                         netif_carrier_off(priv->netdev);
3907                 mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
3908         }
3909
3910         if (mwifiex_del_mgmt_ies(priv))
3911                 mwifiex_dbg(priv->adapter, ERROR,
3912                             "Failed to delete mgmt IEs!\n");
3913
3914         if (mwifiex_set_mgmt_ies(priv, &params->beacon_csa)) {
3915                 mwifiex_dbg(priv->adapter, ERROR,
3916                             "%s: setting mgmt ies failed\n", __func__);
3917                 return -EFAULT;
3918         }
3919
3920         memcpy(&priv->dfs_chandef, &params->chandef, sizeof(priv->dfs_chandef));
3921         memcpy(&priv->beacon_after, &params->beacon_after,
3922                sizeof(priv->beacon_after));
3923
3924         chsw_msec = max(channel_sw->count * priv->bss_cfg.beacon_period, 100);
3925         queue_delayed_work(priv->dfs_chan_sw_workqueue, &priv->dfs_chan_sw_work,
3926                            msecs_to_jiffies(chsw_msec));
3927         return 0;
3928 }
3929
3930 static int mwifiex_cfg80211_get_channel(struct wiphy *wiphy,
3931                                         struct wireless_dev *wdev,
3932                                         struct cfg80211_chan_def *chandef)
3933 {
3934         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3935         struct mwifiex_bssdescriptor *curr_bss;
3936         struct ieee80211_channel *chan;
3937         u8 second_chan_offset;
3938         enum nl80211_channel_type chan_type;
3939         enum nl80211_band band;
3940         int freq;
3941         int ret = -ENODATA;
3942
3943         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
3944             cfg80211_chandef_valid(&priv->bss_chandef)) {
3945                 *chandef = priv->bss_chandef;
3946                 ret = 0;
3947         } else if (priv->media_connected) {
3948                 curr_bss = &priv->curr_bss_params.bss_descriptor;
3949                 band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
3950                 freq = ieee80211_channel_to_frequency(curr_bss->channel, band);
3951                 chan = ieee80211_get_channel(wiphy, freq);
3952
3953                 if (priv->ht_param_present) {
3954                         second_chan_offset = priv->assoc_resp_ht_param &
3955                                         IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
3956                         chan_type = mwifiex_sec_chan_offset_to_chan_type
3957                                                         (second_chan_offset);
3958                         cfg80211_chandef_create(chandef, chan, chan_type);
3959                 } else {
3960                         cfg80211_chandef_create(chandef, chan,
3961                                                 NL80211_CHAN_NO_HT);
3962                 }
3963                 ret = 0;
3964         }
3965
3966         return ret;
3967 }
3968
3969 #ifdef CONFIG_NL80211_TESTMODE
3970
3971 enum mwifiex_tm_attr {
3972         __MWIFIEX_TM_ATTR_INVALID       = 0,
3973         MWIFIEX_TM_ATTR_CMD             = 1,
3974         MWIFIEX_TM_ATTR_DATA            = 2,
3975
3976         /* keep last */
3977         __MWIFIEX_TM_ATTR_AFTER_LAST,
3978         MWIFIEX_TM_ATTR_MAX             = __MWIFIEX_TM_ATTR_AFTER_LAST - 1,
3979 };
3980
3981 static const struct nla_policy mwifiex_tm_policy[MWIFIEX_TM_ATTR_MAX + 1] = {
3982         [MWIFIEX_TM_ATTR_CMD]           = { .type = NLA_U32 },
3983         [MWIFIEX_TM_ATTR_DATA]          = { .type = NLA_BINARY,
3984                                             .len = MWIFIEX_SIZE_OF_CMD_BUFFER },
3985 };
3986
3987 enum mwifiex_tm_command {
3988         MWIFIEX_TM_CMD_HOSTCMD  = 0,
3989 };
3990
3991 static int mwifiex_tm_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
3992                           void *data, int len)
3993 {
3994         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3995         struct mwifiex_ds_misc_cmd *hostcmd;
3996         struct nlattr *tb[MWIFIEX_TM_ATTR_MAX + 1];
3997         struct mwifiex_adapter *adapter;
3998         struct sk_buff *skb;
3999         int err;
4000
4001         if (!priv)
4002                 return -EINVAL;
4003         adapter = priv->adapter;
4004
4005         err = nla_parse(tb, MWIFIEX_TM_ATTR_MAX, data, len,
4006                         mwifiex_tm_policy);
4007         if (err)
4008                 return err;
4009
4010         if (!tb[MWIFIEX_TM_ATTR_CMD])
4011                 return -EINVAL;
4012
4013         switch (nla_get_u32(tb[MWIFIEX_TM_ATTR_CMD])) {
4014         case MWIFIEX_TM_CMD_HOSTCMD:
4015                 if (!tb[MWIFIEX_TM_ATTR_DATA])
4016                         return -EINVAL;
4017
4018                 hostcmd = kzalloc(sizeof(*hostcmd), GFP_KERNEL);
4019                 if (!hostcmd)
4020                         return -ENOMEM;
4021
4022                 hostcmd->len = nla_len(tb[MWIFIEX_TM_ATTR_DATA]);
4023                 memcpy(hostcmd->cmd, nla_data(tb[MWIFIEX_TM_ATTR_DATA]),
4024                        hostcmd->len);
4025
4026                 if (mwifiex_send_cmd(priv, 0, 0, 0, hostcmd, true)) {
4027                         dev_err(priv->adapter->dev, "Failed to process hostcmd\n");
4028                         kfree(hostcmd);
4029                         return -EFAULT;
4030                 }
4031
4032                 /* process hostcmd response*/
4033                 skb = cfg80211_testmode_alloc_reply_skb(wiphy, hostcmd->len);
4034                 if (!skb) {
4035                         kfree(hostcmd);
4036                         return -ENOMEM;
4037                 }
4038                 err = nla_put(skb, MWIFIEX_TM_ATTR_DATA,
4039                               hostcmd->len, hostcmd->cmd);
4040                 if (err) {
4041                         kfree(hostcmd);
4042                         kfree_skb(skb);
4043                         return -EMSGSIZE;
4044                 }
4045
4046                 err = cfg80211_testmode_reply(skb);
4047                 kfree(hostcmd);
4048                 return err;
4049         default:
4050                 return -EOPNOTSUPP;
4051         }
4052 }
4053 #endif
4054
4055 static int
4056 mwifiex_cfg80211_start_radar_detection(struct wiphy *wiphy,
4057                                        struct net_device *dev,
4058                                        struct cfg80211_chan_def *chandef,
4059                                        u32 cac_time_ms)
4060 {
4061         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4062         struct mwifiex_radar_params radar_params;
4063
4064         if (priv->adapter->scan_processing) {
4065                 mwifiex_dbg(priv->adapter, ERROR,
4066                             "radar detection: scan already in process...\n");
4067                 return -EBUSY;
4068         }
4069
4070         if (!mwifiex_is_11h_active(priv)) {
4071                 mwifiex_dbg(priv->adapter, INFO,
4072                             "Enable 11h extensions in FW\n");
4073                 if (mwifiex_11h_activate(priv, true)) {
4074                         mwifiex_dbg(priv->adapter, ERROR,
4075                                     "Failed to activate 11h extensions!!");
4076                         return -1;
4077                 }
4078                 priv->state_11h.is_11h_active = true;
4079         }
4080
4081         memset(&radar_params, 0, sizeof(struct mwifiex_radar_params));
4082         radar_params.chandef = chandef;
4083         radar_params.cac_time_ms = cac_time_ms;
4084
4085         memcpy(&priv->dfs_chandef, chandef, sizeof(priv->dfs_chandef));
4086
4087         if (mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
4088                              HostCmd_ACT_GEN_SET, 0, &radar_params, true))
4089                 return -1;
4090
4091         queue_delayed_work(priv->dfs_cac_workqueue, &priv->dfs_cac_work,
4092                            msecs_to_jiffies(cac_time_ms));
4093         return 0;
4094 }
4095
4096 static int
4097 mwifiex_cfg80211_change_station(struct wiphy *wiphy, struct net_device *dev,
4098                                 const u8 *mac,
4099                                 struct station_parameters *params)
4100 {
4101         int ret;
4102         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4103
4104         /* we support change_station handler only for TDLS peers*/
4105         if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4106                 return -ENOTSUPP;
4107
4108         /* make sure we are in station mode and connected */
4109         if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
4110                 return -ENOTSUPP;
4111
4112         priv->sta_params = params;
4113
4114         ret = mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CONFIG_LINK);
4115         priv->sta_params = NULL;
4116
4117         return ret;
4118 }
4119
4120 /* station cfg80211 operations */
4121 static struct cfg80211_ops mwifiex_cfg80211_ops = {
4122         .add_virtual_intf = mwifiex_add_virtual_intf,
4123         .del_virtual_intf = mwifiex_del_virtual_intf,
4124         .change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
4125         .scan = mwifiex_cfg80211_scan,
4126         .connect = mwifiex_cfg80211_connect,
4127         .disconnect = mwifiex_cfg80211_disconnect,
4128         .get_station = mwifiex_cfg80211_get_station,
4129         .dump_station = mwifiex_cfg80211_dump_station,
4130         .dump_survey = mwifiex_cfg80211_dump_survey,
4131         .set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
4132         .join_ibss = mwifiex_cfg80211_join_ibss,
4133         .leave_ibss = mwifiex_cfg80211_leave_ibss,
4134         .add_key = mwifiex_cfg80211_add_key,
4135         .del_key = mwifiex_cfg80211_del_key,
4136         .set_default_mgmt_key = mwifiex_cfg80211_set_default_mgmt_key,
4137         .mgmt_tx = mwifiex_cfg80211_mgmt_tx,
4138         .mgmt_frame_register = mwifiex_cfg80211_mgmt_frame_register,
4139         .remain_on_channel = mwifiex_cfg80211_remain_on_channel,
4140         .cancel_remain_on_channel = mwifiex_cfg80211_cancel_remain_on_channel,
4141         .set_default_key = mwifiex_cfg80211_set_default_key,
4142         .set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
4143         .set_tx_power = mwifiex_cfg80211_set_tx_power,
4144         .get_tx_power = mwifiex_cfg80211_get_tx_power,
4145         .set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
4146         .start_ap = mwifiex_cfg80211_start_ap,
4147         .stop_ap = mwifiex_cfg80211_stop_ap,
4148         .change_beacon = mwifiex_cfg80211_change_beacon,
4149         .set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
4150         .set_antenna = mwifiex_cfg80211_set_antenna,
4151         .get_antenna = mwifiex_cfg80211_get_antenna,
4152         .del_station = mwifiex_cfg80211_del_station,
4153         .sched_scan_start = mwifiex_cfg80211_sched_scan_start,
4154         .sched_scan_stop = mwifiex_cfg80211_sched_scan_stop,
4155 #ifdef CONFIG_PM
4156         .suspend = mwifiex_cfg80211_suspend,
4157         .resume = mwifiex_cfg80211_resume,
4158         .set_wakeup = mwifiex_cfg80211_set_wakeup,
4159         .set_rekey_data = mwifiex_set_rekey_data,
4160 #endif
4161         .set_coalesce = mwifiex_cfg80211_set_coalesce,
4162         .tdls_mgmt = mwifiex_cfg80211_tdls_mgmt,
4163         .tdls_oper = mwifiex_cfg80211_tdls_oper,
4164         .tdls_channel_switch = mwifiex_cfg80211_tdls_chan_switch,
4165         .tdls_cancel_channel_switch = mwifiex_cfg80211_tdls_cancel_chan_switch,
4166         .add_station = mwifiex_cfg80211_add_station,
4167         .change_station = mwifiex_cfg80211_change_station,
4168         CFG80211_TESTMODE_CMD(mwifiex_tm_cmd)
4169         .get_channel = mwifiex_cfg80211_get_channel,
4170         .start_radar_detection = mwifiex_cfg80211_start_radar_detection,
4171         .channel_switch = mwifiex_cfg80211_channel_switch,
4172 };
4173
4174 #ifdef CONFIG_PM
4175 static const struct wiphy_wowlan_support mwifiex_wowlan_support = {
4176         .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
4177                 WIPHY_WOWLAN_NET_DETECT | WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
4178                 WIPHY_WOWLAN_GTK_REKEY_FAILURE,
4179         .n_patterns = MWIFIEX_MEF_MAX_FILTERS,
4180         .pattern_min_len = 1,
4181         .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4182         .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4183         .max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
4184 };
4185 #endif
4186
4187 static bool mwifiex_is_valid_alpha2(const char *alpha2)
4188 {
4189         if (!alpha2 || strlen(alpha2) != 2)
4190                 return false;
4191
4192         if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
4193                 return true;
4194
4195         return false;
4196 }
4197
4198 static const struct wiphy_coalesce_support mwifiex_coalesce_support = {
4199         .n_rules = MWIFIEX_COALESCE_MAX_RULES,
4200         .max_delay = MWIFIEX_MAX_COALESCING_DELAY,
4201         .n_patterns = MWIFIEX_COALESCE_MAX_FILTERS,
4202         .pattern_min_len = 1,
4203         .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4204         .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4205 };
4206
4207 int mwifiex_init_channel_scan_gap(struct mwifiex_adapter *adapter)
4208 {
4209         u32 n_channels_bg, n_channels_a = 0;
4210
4211         n_channels_bg = mwifiex_band_2ghz.n_channels;
4212
4213         if (adapter->config_bands & BAND_A)
4214                 n_channels_a = mwifiex_band_5ghz.n_channels;
4215
4216         adapter->num_in_chan_stats = n_channels_bg + n_channels_a;
4217         adapter->chan_stats = vmalloc(sizeof(*adapter->chan_stats) *
4218                                       adapter->num_in_chan_stats);
4219
4220         if (!adapter->chan_stats)
4221                 return -ENOMEM;
4222
4223         return 0;
4224 }
4225
4226 /*
4227  * This function registers the device with CFG802.11 subsystem.
4228  *
4229  * The function creates the wireless device/wiphy, populates it with
4230  * default parameters and handler function pointers, and finally
4231  * registers the device.
4232  */
4233
4234 int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
4235 {
4236         int ret;
4237         void *wdev_priv;
4238         struct wiphy *wiphy;
4239         struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
4240         u8 *country_code;
4241         u32 thr, retry;
4242
4243         /* create a new wiphy for use with cfg80211 */
4244         wiphy = wiphy_new(&mwifiex_cfg80211_ops,
4245                           sizeof(struct mwifiex_adapter *));
4246         if (!wiphy) {
4247                 mwifiex_dbg(adapter, ERROR,
4248                             "%s: creating new wiphy\n", __func__);
4249                 return -ENOMEM;
4250         }
4251         wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4252         wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4253         wiphy->mgmt_stypes = mwifiex_mgmt_stypes;
4254         wiphy->max_remain_on_channel_duration = 5000;
4255         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4256                                  BIT(NL80211_IFTYPE_ADHOC) |
4257                                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
4258                                  BIT(NL80211_IFTYPE_P2P_GO) |
4259                                  BIT(NL80211_IFTYPE_AP);
4260
4261         wiphy->bands[NL80211_BAND_2GHZ] = &mwifiex_band_2ghz;
4262         if (adapter->config_bands & BAND_A)
4263                 wiphy->bands[NL80211_BAND_5GHZ] = &mwifiex_band_5ghz;
4264         else
4265                 wiphy->bands[NL80211_BAND_5GHZ] = NULL;
4266
4267         if (adapter->drcs_enabled && ISSUPP_DRCS_ENABLED(adapter->fw_cap_info))
4268                 wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_drcs;
4269         else if (adapter->is_hw_11ac_capable)
4270                 wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_vht;
4271         else
4272                 wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
4273         wiphy->n_iface_combinations = 1;
4274
4275         /* Initialize cipher suits */
4276         wiphy->cipher_suites = mwifiex_cipher_suites;
4277         wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
4278
4279         if (adapter->regd) {
4280                 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
4281                                            REGULATORY_DISABLE_BEACON_HINTS |
4282                                            REGULATORY_COUNTRY_IE_IGNORE;
4283                 wiphy_apply_custom_regulatory(wiphy, adapter->regd);
4284         }
4285
4286         ether_addr_copy(wiphy->perm_addr, adapter->perm_addr);
4287         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
4288         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
4289                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
4290                         WIPHY_FLAG_AP_UAPSD |
4291                         WIPHY_FLAG_SUPPORTS_SCHED_SCAN |
4292                         WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
4293                         WIPHY_FLAG_HAS_CHANNEL_SWITCH |
4294                         WIPHY_FLAG_PS_ON_BY_DEFAULT;
4295
4296         if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4297                 wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
4298                                 WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
4299
4300 #ifdef CONFIG_PM
4301         wiphy->wowlan = &mwifiex_wowlan_support;
4302 #endif
4303
4304         wiphy->coalesce = &mwifiex_coalesce_support;
4305
4306         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
4307                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
4308                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
4309
4310         wiphy->max_sched_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4311         wiphy->max_sched_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4312         wiphy->max_match_sets = MWIFIEX_MAX_SSID_LIST_LENGTH;
4313
4314         wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
4315         wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
4316
4317         wiphy->features |= NL80211_FEATURE_HT_IBSS |
4318                            NL80211_FEATURE_INACTIVITY_TIMER |
4319                            NL80211_FEATURE_LOW_PRIORITY_SCAN |
4320                            NL80211_FEATURE_NEED_OBSS_SCAN |
4321                            NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
4322                            NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
4323                            NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
4324
4325         if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4326                 wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
4327
4328         if (adapter->fw_api_ver == MWIFIEX_FW_V15)
4329                 wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
4330
4331         /* Reserve space for mwifiex specific private data for BSS */
4332         wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
4333
4334         wiphy->reg_notifier = mwifiex_reg_notifier;
4335
4336         /* Set struct mwifiex_adapter pointer in wiphy_priv */
4337         wdev_priv = wiphy_priv(wiphy);
4338         *(unsigned long *)wdev_priv = (unsigned long)adapter;
4339
4340         set_wiphy_dev(wiphy, priv->adapter->dev);
4341
4342         ret = wiphy_register(wiphy);
4343         if (ret < 0) {
4344                 mwifiex_dbg(adapter, ERROR,
4345                             "%s: wiphy_register failed: %d\n", __func__, ret);
4346                 wiphy_free(wiphy);
4347                 return ret;
4348         }
4349
4350         if (!adapter->regd) {
4351                 if (reg_alpha2 && mwifiex_is_valid_alpha2(reg_alpha2)) {
4352                         mwifiex_dbg(adapter, INFO,
4353                                     "driver hint alpha2: %2.2s\n", reg_alpha2);
4354                         regulatory_hint(wiphy, reg_alpha2);
4355                 } else {
4356                         if (adapter->region_code == 0x00) {
4357                                 mwifiex_dbg(adapter, WARN,
4358                                             "Ignore world regulatory domain\n");
4359                         } else {
4360                                 wiphy->regulatory_flags |=
4361                                         REGULATORY_DISABLE_BEACON_HINTS |
4362                                         REGULATORY_COUNTRY_IE_IGNORE;
4363                                 country_code =
4364                                         mwifiex_11d_code_2_region(
4365                                                 adapter->region_code);
4366                                 if (country_code &&
4367                                     regulatory_hint(wiphy, country_code))
4368                                         mwifiex_dbg(priv->adapter, ERROR,
4369                                                     "regulatory_hint() failed\n");
4370                         }
4371                 }
4372         }
4373
4374         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4375                          HostCmd_ACT_GEN_GET, FRAG_THRESH_I, &thr, true);
4376         wiphy->frag_threshold = thr;
4377         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4378                          HostCmd_ACT_GEN_GET, RTS_THRESH_I, &thr, true);
4379         wiphy->rts_threshold = thr;
4380         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4381                          HostCmd_ACT_GEN_GET, SHORT_RETRY_LIM_I, &retry, true);
4382         wiphy->retry_short = (u8) retry;
4383         mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4384                          HostCmd_ACT_GEN_GET, LONG_RETRY_LIM_I, &retry, true);
4385         wiphy->retry_long = (u8) retry;
4386
4387         adapter->wiphy = wiphy;
4388         return ret;
4389 }