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