OSDN Git Service

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