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