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