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