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