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