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