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