mwifiex: download per country tx power table to firmware
[linux-drm-fsl-dcu.git] / drivers / net / wireless / mwifiex / cfg80211.c
1 /*
2  * Marvell Wireless LAN device driver: CFG80211
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "cfg80211.h"
21 #include "main.h"
22
23 static char *reg_alpha2;
24 module_param(reg_alpha2, charp, 0);
25
26 static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
27         {
28                 .max = 2, .types = BIT(NL80211_IFTYPE_STATION) |
29                                    BIT(NL80211_IFTYPE_P2P_GO) |
30                                    BIT(NL80211_IFTYPE_P2P_CLIENT),
31         },
32         {
33                 .max = 1, .types = BIT(NL80211_IFTYPE_AP),
34         },
35 };
36
37 static const struct ieee80211_iface_combination mwifiex_iface_comb_ap_sta = {
38         .limits = mwifiex_ap_sta_limits,
39         .num_different_channels = 1,
40         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
41         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
42         .beacon_int_infra_match = true,
43 };
44
45 static const struct ieee80211_regdomain mwifiex_world_regdom_custom = {
46         .n_reg_rules = 7,
47         .alpha2 =  "99",
48         .reg_rules = {
49                 /* Channel 1 - 11 */
50                 REG_RULE(2412-10, 2462+10, 40, 3, 20, 0),
51                 /* Channel 12 - 13 */
52                 REG_RULE(2467-10, 2472+10, 20, 3, 20,
53                          NL80211_RRF_NO_IR),
54                 /* Channel 14 */
55                 REG_RULE(2484-10, 2484+10, 20, 3, 20,
56                          NL80211_RRF_NO_IR |
57                          NL80211_RRF_NO_OFDM),
58                 /* Channel 36 - 48 */
59                 REG_RULE(5180-10, 5240+10, 40, 3, 20,
60                          NL80211_RRF_NO_IR),
61                 /* Channel 149 - 165 */
62                 REG_RULE(5745-10, 5825+10, 40, 3, 20,
63                          NL80211_RRF_NO_IR),
64                 /* Channel 52 - 64 */
65                 REG_RULE(5260-10, 5320+10, 40, 3, 30,
66                          NL80211_RRF_NO_IR |
67                          NL80211_RRF_DFS),
68                 /* Channel 100 - 140 */
69                 REG_RULE(5500-10, 5700+10, 40, 3, 30,
70                          NL80211_RRF_NO_IR |
71                          NL80211_RRF_DFS),
72         }
73 };
74
75 /*
76  * This function maps the nl802.11 channel type into driver channel type.
77  *
78  * The mapping is as follows -
79  *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
80  *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
81  *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
82  *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
83  *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
84  */
85 u8 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
86 {
87         switch (chan_type) {
88         case NL80211_CHAN_NO_HT:
89         case NL80211_CHAN_HT20:
90                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
91         case NL80211_CHAN_HT40PLUS:
92                 return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
93         case NL80211_CHAN_HT40MINUS:
94                 return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
95         default:
96                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
97         }
98 }
99
100 /*
101  * This function checks whether WEP is set.
102  */
103 static int
104 mwifiex_is_alg_wep(u32 cipher)
105 {
106         switch (cipher) {
107         case WLAN_CIPHER_SUITE_WEP40:
108         case WLAN_CIPHER_SUITE_WEP104:
109                 return 1;
110         default:
111                 break;
112         }
113
114         return 0;
115 }
116
117 /*
118  * This function retrieves the private structure from kernel wiphy structure.
119  */
120 static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
121 {
122         return (void *) (*(unsigned long *) wiphy_priv(wiphy));
123 }
124
125 /*
126  * CFG802.11 operation handler to delete a network key.
127  */
128 static int
129 mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
130                          u8 key_index, bool pairwise, const u8 *mac_addr)
131 {
132         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
133         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
134         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
135
136         if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
137                 wiphy_err(wiphy, "deleting the crypto keys\n");
138                 return -EFAULT;
139         }
140
141         wiphy_dbg(wiphy, "info: crypto keys deleted\n");
142         return 0;
143 }
144
145 /*
146  * This function forms an skb for management frame.
147  */
148 static int
149 mwifiex_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
150 {
151         u8 addr[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
152         u16 pkt_len;
153         u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
154         struct timeval tv;
155
156         pkt_len = len + ETH_ALEN;
157
158         skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN +
159                     MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
160         memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
161
162         memcpy(skb_push(skb, sizeof(tx_control)),
163                &tx_control, sizeof(tx_control));
164
165         memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
166
167         /* Add packet data and address4 */
168         memcpy(skb_put(skb, sizeof(struct ieee80211_hdr_3addr)), buf,
169                sizeof(struct ieee80211_hdr_3addr));
170         memcpy(skb_put(skb, ETH_ALEN), addr, ETH_ALEN);
171         memcpy(skb_put(skb, len - sizeof(struct ieee80211_hdr_3addr)),
172                buf + sizeof(struct ieee80211_hdr_3addr),
173                len - sizeof(struct ieee80211_hdr_3addr));
174
175         skb->priority = LOW_PRIO_TID;
176         do_gettimeofday(&tv);
177         skb->tstamp = timeval_to_ktime(tv);
178
179         return 0;
180 }
181
182 /*
183  * CFG802.11 operation handler to transmit a management frame.
184  */
185 static int
186 mwifiex_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
187                          struct cfg80211_mgmt_tx_params *params, u64 *cookie)
188 {
189         const u8 *buf = params->buf;
190         size_t len = params->len;
191         struct sk_buff *skb;
192         u16 pkt_len;
193         const struct ieee80211_mgmt *mgmt;
194         struct mwifiex_txinfo *tx_info;
195         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
196
197         if (!buf || !len) {
198                 wiphy_err(wiphy, "invalid buffer and length\n");
199                 return -EFAULT;
200         }
201
202         mgmt = (const struct ieee80211_mgmt *)buf;
203         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA &&
204             ieee80211_is_probe_resp(mgmt->frame_control)) {
205                 /* Since we support offload probe resp, we need to skip probe
206                  * resp in AP or GO mode */
207                 wiphy_dbg(wiphy,
208                           "info: skip to send probe resp in AP or GO mode\n");
209                 return 0;
210         }
211
212         pkt_len = len + ETH_ALEN;
213         skb = dev_alloc_skb(MWIFIEX_MIN_DATA_HEADER_LEN +
214                             MWIFIEX_MGMT_FRAME_HEADER_SIZE +
215                             pkt_len + sizeof(pkt_len));
216
217         if (!skb) {
218                 wiphy_err(wiphy, "allocate skb failed for management frame\n");
219                 return -ENOMEM;
220         }
221
222         tx_info = MWIFIEX_SKB_TXCB(skb);
223         tx_info->bss_num = priv->bss_num;
224         tx_info->bss_type = priv->bss_type;
225         tx_info->pkt_len = pkt_len;
226
227         mwifiex_form_mgmt_frame(skb, buf, len);
228         mwifiex_queue_tx_pkt(priv, skb);
229
230         *cookie = prandom_u32() | 1;
231         cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true, GFP_ATOMIC);
232
233         wiphy_dbg(wiphy, "info: management frame transmitted\n");
234         return 0;
235 }
236
237 /*
238  * CFG802.11 operation handler to register a mgmt frame.
239  */
240 static void
241 mwifiex_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
242                                      struct wireless_dev *wdev,
243                                      u16 frame_type, bool reg)
244 {
245         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
246         u32 mask;
247
248         if (reg)
249                 mask = priv->mgmt_frame_mask | BIT(frame_type >> 4);
250         else
251                 mask = priv->mgmt_frame_mask & ~BIT(frame_type >> 4);
252
253         if (mask != priv->mgmt_frame_mask) {
254                 priv->mgmt_frame_mask = mask;
255                 mwifiex_send_cmd_async(priv, HostCmd_CMD_MGMT_FRAME_REG,
256                                        HostCmd_ACT_GEN_SET, 0,
257                                        &priv->mgmt_frame_mask);
258                 wiphy_dbg(wiphy, "info: mgmt frame registered\n");
259         }
260 }
261
262 /*
263  * CFG802.11 operation handler to remain on channel.
264  */
265 static int
266 mwifiex_cfg80211_remain_on_channel(struct wiphy *wiphy,
267                                    struct wireless_dev *wdev,
268                                    struct ieee80211_channel *chan,
269                                    unsigned int duration, u64 *cookie)
270 {
271         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
272         int ret;
273
274         if (!chan || !cookie) {
275                 wiphy_err(wiphy, "Invalid parameter for ROC\n");
276                 return -EINVAL;
277         }
278
279         if (priv->roc_cfg.cookie) {
280                 wiphy_dbg(wiphy, "info: ongoing ROC, cookie = 0x%llu\n",
281                           priv->roc_cfg.cookie);
282                 return -EBUSY;
283         }
284
285         ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_SET, chan,
286                                          duration);
287
288         if (!ret) {
289                 *cookie = prandom_u32() | 1;
290                 priv->roc_cfg.cookie = *cookie;
291                 priv->roc_cfg.chan = *chan;
292
293                 cfg80211_ready_on_channel(wdev, *cookie, chan,
294                                           duration, GFP_ATOMIC);
295
296                 wiphy_dbg(wiphy, "info: ROC, cookie = 0x%llx\n", *cookie);
297         }
298
299         return ret;
300 }
301
302 /*
303  * CFG802.11 operation handler to cancel remain on channel.
304  */
305 static int
306 mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
307                                           struct wireless_dev *wdev, u64 cookie)
308 {
309         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
310         int ret;
311
312         if (cookie != priv->roc_cfg.cookie)
313                 return -ENOENT;
314
315         ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_REMOVE,
316                                          &priv->roc_cfg.chan, 0);
317
318         if (!ret) {
319                 cfg80211_remain_on_channel_expired(wdev, cookie,
320                                                    &priv->roc_cfg.chan,
321                                                    GFP_ATOMIC);
322
323                 memset(&priv->roc_cfg, 0, sizeof(struct mwifiex_roc_cfg));
324
325                 wiphy_dbg(wiphy, "info: cancel ROC, cookie = 0x%llx\n", cookie);
326         }
327
328         return ret;
329 }
330
331 /*
332  * CFG802.11 operation handler to set Tx power.
333  */
334 static int
335 mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
336                               struct wireless_dev *wdev,
337                               enum nl80211_tx_power_setting type,
338                               int mbm)
339 {
340         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
341         struct mwifiex_private *priv;
342         struct mwifiex_power_cfg power_cfg;
343         int dbm = MBM_TO_DBM(mbm);
344
345         if (type == NL80211_TX_POWER_FIXED) {
346                 power_cfg.is_power_auto = 0;
347                 power_cfg.power_level = dbm;
348         } else {
349                 power_cfg.is_power_auto = 1;
350         }
351
352         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
353
354         return mwifiex_set_tx_power(priv, &power_cfg);
355 }
356
357 /*
358  * CFG802.11 operation handler to set Power Save option.
359  *
360  * The timeout value, if provided, is currently ignored.
361  */
362 static int
363 mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
364                                 struct net_device *dev,
365                                 bool enabled, int timeout)
366 {
367         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
368         u32 ps_mode;
369
370         if (timeout)
371                 wiphy_dbg(wiphy,
372                           "info: ignore timeout value for IEEE Power Save\n");
373
374         ps_mode = enabled;
375
376         return mwifiex_drv_set_power(priv, &ps_mode);
377 }
378
379 /*
380  * CFG802.11 operation handler to set the default network key.
381  */
382 static int
383 mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
384                                  u8 key_index, bool unicast,
385                                  bool multicast)
386 {
387         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
388
389         /* Return if WEP key not configured */
390         if (!priv->sec_info.wep_enabled)
391                 return 0;
392
393         if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
394                 priv->wep_key_curr_index = key_index;
395         } else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
396                                       NULL, 0)) {
397                 wiphy_err(wiphy, "set default Tx key index\n");
398                 return -EFAULT;
399         }
400
401         return 0;
402 }
403
404 /*
405  * CFG802.11 operation handler to add a network key.
406  */
407 static int
408 mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
409                          u8 key_index, bool pairwise, const u8 *mac_addr,
410                          struct key_params *params)
411 {
412         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
413         struct mwifiex_wep_key *wep_key;
414         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
415         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
416
417         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
418             (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
419              params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
420                 if (params->key && params->key_len) {
421                         wep_key = &priv->wep_key[key_index];
422                         memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
423                         memcpy(wep_key->key_material, params->key,
424                                params->key_len);
425                         wep_key->key_index = key_index;
426                         wep_key->key_length = params->key_len;
427                         priv->sec_info.wep_enabled = 1;
428                 }
429                 return 0;
430         }
431
432         if (mwifiex_set_encode(priv, params, params->key, params->key_len,
433                                key_index, peer_mac, 0)) {
434                 wiphy_err(wiphy, "crypto keys added\n");
435                 return -EFAULT;
436         }
437
438         return 0;
439 }
440
441 /*
442  * This function sends domain information to the firmware.
443  *
444  * The following information are passed to the firmware -
445  *      - Country codes
446  *      - Sub bands (first channel, number of channels, maximum Tx power)
447  */
448 static int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
449 {
450         u8 no_of_triplet = 0;
451         struct ieee80211_country_ie_triplet *t;
452         u8 no_of_parsed_chan = 0;
453         u8 first_chan = 0, next_chan = 0, max_pwr = 0;
454         u8 i, flag = 0;
455         enum ieee80211_band band;
456         struct ieee80211_supported_band *sband;
457         struct ieee80211_channel *ch;
458         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
459         struct mwifiex_private *priv;
460         struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
461
462         /* Set country code */
463         domain_info->country_code[0] = adapter->country_code[0];
464         domain_info->country_code[1] = adapter->country_code[1];
465         domain_info->country_code[2] = ' ';
466
467         band = mwifiex_band_to_radio_type(adapter->config_bands);
468         if (!wiphy->bands[band]) {
469                 wiphy_err(wiphy, "11D: setting domain info in FW\n");
470                 return -1;
471         }
472
473         sband = wiphy->bands[band];
474
475         for (i = 0; i < sband->n_channels ; i++) {
476                 ch = &sband->channels[i];
477                 if (ch->flags & IEEE80211_CHAN_DISABLED)
478                         continue;
479
480                 if (!flag) {
481                         flag = 1;
482                         first_chan = (u32) ch->hw_value;
483                         next_chan = first_chan;
484                         max_pwr = ch->max_power;
485                         no_of_parsed_chan = 1;
486                         continue;
487                 }
488
489                 if (ch->hw_value == next_chan + 1 &&
490                     ch->max_power == max_pwr) {
491                         next_chan++;
492                         no_of_parsed_chan++;
493                 } else {
494                         t = &domain_info->triplet[no_of_triplet];
495                         t->chans.first_channel = first_chan;
496                         t->chans.num_channels = no_of_parsed_chan;
497                         t->chans.max_power = max_pwr;
498                         no_of_triplet++;
499                         first_chan = (u32) ch->hw_value;
500                         next_chan = first_chan;
501                         max_pwr = ch->max_power;
502                         no_of_parsed_chan = 1;
503                 }
504         }
505
506         if (flag) {
507                 t = &domain_info->triplet[no_of_triplet];
508                 t->chans.first_channel = first_chan;
509                 t->chans.num_channels = no_of_parsed_chan;
510                 t->chans.max_power = max_pwr;
511                 no_of_triplet++;
512         }
513
514         domain_info->no_of_triplet = no_of_triplet;
515
516         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
517
518         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
519                                    HostCmd_ACT_GEN_SET, 0, NULL)) {
520                 wiphy_err(wiphy, "11D: setting domain info in FW\n");
521                 return -1;
522         }
523
524         return 0;
525 }
526
527 /*
528  * CFG802.11 regulatory domain callback function.
529  *
530  * This function is called when the regulatory domain is changed due to the
531  * following reasons -
532  *      - Set by driver
533  *      - Set by system core
534  *      - Set by user
535  *      - Set bt Country IE
536  */
537 static void mwifiex_reg_notifier(struct wiphy *wiphy,
538                                  struct regulatory_request *request)
539 {
540         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
541         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
542                                                         MWIFIEX_BSS_ROLE_ANY);
543
544         wiphy_dbg(wiphy, "info: cfg80211 regulatory domain callback for %c%c\n",
545                   request->alpha2[0], request->alpha2[1]);
546
547         switch (request->initiator) {
548         case NL80211_REGDOM_SET_BY_DRIVER:
549         case NL80211_REGDOM_SET_BY_CORE:
550         case NL80211_REGDOM_SET_BY_USER:
551         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
552                 break;
553         default:
554                 wiphy_err(wiphy, "unknown regdom initiator: %d\n",
555                           request->initiator);
556                 return;
557         }
558
559         /* Don't send world or same regdom info to firmware */
560         if (strncmp(request->alpha2, "00", 2) &&
561             strncmp(request->alpha2, adapter->country_code,
562                     sizeof(request->alpha2))) {
563                 memcpy(adapter->country_code, request->alpha2,
564                        sizeof(request->alpha2));
565                 mwifiex_send_domain_info_cmd_fw(wiphy);
566
567                 if (adapter->dt_node) {
568                         char txpwr[] = {"marvell,00_txpwrlimit"};
569
570                         memcpy(&txpwr[8], adapter->country_code, 2);
571                         mwifiex_dnld_dt_cfgdata(priv, adapter->dt_node,
572                                                 txpwr);
573                 }
574         }
575 }
576
577 /*
578  * This function sets the fragmentation threshold.
579  *
580  * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
581  * and MWIFIEX_FRAG_MAX_VALUE.
582  */
583 static int
584 mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
585 {
586         if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
587             frag_thr > MWIFIEX_FRAG_MAX_VALUE)
588                 frag_thr = MWIFIEX_FRAG_MAX_VALUE;
589
590         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_SNMP_MIB,
591                                      HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
592                                      &frag_thr);
593 }
594
595 /*
596  * This function sets the RTS threshold.
597
598  * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
599  * and MWIFIEX_RTS_MAX_VALUE.
600  */
601 static int
602 mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
603 {
604         if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
605                 rts_thr = MWIFIEX_RTS_MAX_VALUE;
606
607         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_SNMP_MIB,
608                                     HostCmd_ACT_GEN_SET, RTS_THRESH_I,
609                                     &rts_thr);
610 }
611
612 /*
613  * CFG802.11 operation handler to set wiphy parameters.
614  *
615  * This function can be used to set the RTS threshold and the
616  * Fragmentation threshold of the driver.
617  */
618 static int
619 mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
620 {
621         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
622         struct mwifiex_private *priv;
623         struct mwifiex_uap_bss_param *bss_cfg;
624         int ret, bss_started, i;
625
626         for (i = 0; i < adapter->priv_num; i++) {
627                 priv = adapter->priv[i];
628
629                 switch (priv->bss_role) {
630                 case MWIFIEX_BSS_ROLE_UAP:
631                         bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param),
632                                           GFP_KERNEL);
633                         if (!bss_cfg)
634                                 return -ENOMEM;
635
636                         mwifiex_set_sys_config_invalid_data(bss_cfg);
637
638                         if (changed & WIPHY_PARAM_RTS_THRESHOLD)
639                                 bss_cfg->rts_threshold = wiphy->rts_threshold;
640                         if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
641                                 bss_cfg->frag_threshold = wiphy->frag_threshold;
642                         if (changed & WIPHY_PARAM_RETRY_LONG)
643                                 bss_cfg->retry_limit = wiphy->retry_long;
644
645                         bss_started = priv->bss_started;
646
647                         ret = mwifiex_send_cmd_sync(priv,
648                                                     HostCmd_CMD_UAP_BSS_STOP,
649                                                     HostCmd_ACT_GEN_SET, 0,
650                                                     NULL);
651                         if (ret) {
652                                 wiphy_err(wiphy, "Failed to stop the BSS\n");
653                                 kfree(bss_cfg);
654                                 return ret;
655                         }
656
657                         ret = mwifiex_send_cmd_async(priv,
658                                                      HostCmd_CMD_UAP_SYS_CONFIG,
659                                                      HostCmd_ACT_GEN_SET,
660                                                      UAP_BSS_PARAMS_I, bss_cfg);
661
662                         kfree(bss_cfg);
663
664                         if (ret) {
665                                 wiphy_err(wiphy, "Failed to set bss config\n");
666                                 return ret;
667                         }
668
669                         if (!bss_started)
670                                 break;
671
672                         ret = mwifiex_send_cmd_async(priv,
673                                                      HostCmd_CMD_UAP_BSS_START,
674                                                      HostCmd_ACT_GEN_SET, 0,
675                                                      NULL);
676                         if (ret) {
677                                 wiphy_err(wiphy, "Failed to start BSS\n");
678                                 return ret;
679                         }
680
681                         break;
682                 case MWIFIEX_BSS_ROLE_STA:
683                         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
684                                 ret = mwifiex_set_rts(priv,
685                                                       wiphy->rts_threshold);
686                                 if (ret)
687                                         return ret;
688                         }
689                         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
690                                 ret = mwifiex_set_frag(priv,
691                                                        wiphy->frag_threshold);
692                                 if (ret)
693                                         return ret;
694                         }
695                         break;
696                 }
697         }
698
699         return 0;
700 }
701
702 static int
703 mwifiex_cfg80211_deinit_p2p(struct mwifiex_private *priv)
704 {
705         u16 mode = P2P_MODE_DISABLE;
706
707         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA)
708                 mwifiex_set_bss_role(priv, MWIFIEX_BSS_ROLE_STA);
709
710         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_P2P_MODE_CFG,
711                                   HostCmd_ACT_GEN_SET, 0, &mode))
712                 return -1;
713
714         return 0;
715 }
716
717 /*
718  * This function initializes the functionalities for P2P client.
719  * The P2P client initialization sequence is:
720  * disable -> device -> client
721  */
722 static int
723 mwifiex_cfg80211_init_p2p_client(struct mwifiex_private *priv)
724 {
725         u16 mode;
726
727         if (mwifiex_cfg80211_deinit_p2p(priv))
728                 return -1;
729
730         mode = P2P_MODE_DEVICE;
731         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_P2P_MODE_CFG,
732                                   HostCmd_ACT_GEN_SET, 0, &mode))
733                 return -1;
734
735         mode = P2P_MODE_CLIENT;
736         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_P2P_MODE_CFG,
737                                   HostCmd_ACT_GEN_SET, 0, &mode))
738                 return -1;
739
740         return 0;
741 }
742
743 /*
744  * This function initializes the functionalities for P2P GO.
745  * The P2P GO initialization sequence is:
746  * disable -> device -> GO
747  */
748 static int
749 mwifiex_cfg80211_init_p2p_go(struct mwifiex_private *priv)
750 {
751         u16 mode;
752
753         if (mwifiex_cfg80211_deinit_p2p(priv))
754                 return -1;
755
756         mode = P2P_MODE_DEVICE;
757         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_P2P_MODE_CFG,
758                                   HostCmd_ACT_GEN_SET, 0, &mode))
759                 return -1;
760
761         mode = P2P_MODE_GO;
762         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_P2P_MODE_CFG,
763                                   HostCmd_ACT_GEN_SET, 0, &mode))
764                 return -1;
765
766         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
767                 mwifiex_set_bss_role(priv, MWIFIEX_BSS_ROLE_UAP);
768
769         return 0;
770 }
771
772 /*
773  * CFG802.11 operation handler to change interface type.
774  */
775 static int
776 mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
777                                      struct net_device *dev,
778                                      enum nl80211_iftype type, u32 *flags,
779                                      struct vif_params *params)
780 {
781         int ret;
782         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
783
784         switch (dev->ieee80211_ptr->iftype) {
785         case NL80211_IFTYPE_ADHOC:
786                 switch (type) {
787                 case NL80211_IFTYPE_STATION:
788                         break;
789                 case NL80211_IFTYPE_UNSPECIFIED:
790                         wiphy_warn(wiphy, "%s: kept type as IBSS\n", dev->name);
791                 case NL80211_IFTYPE_ADHOC:      /* This shouldn't happen */
792                         return 0;
793                 case NL80211_IFTYPE_AP:
794                 default:
795                         wiphy_err(wiphy, "%s: changing to %d not supported\n",
796                                   dev->name, type);
797                         return -EOPNOTSUPP;
798                 }
799                 break;
800         case NL80211_IFTYPE_STATION:
801                 switch (type) {
802                 case NL80211_IFTYPE_ADHOC:
803                         break;
804                 case NL80211_IFTYPE_P2P_CLIENT:
805                         if (mwifiex_cfg80211_init_p2p_client(priv))
806                                 return -EFAULT;
807                         dev->ieee80211_ptr->iftype = type;
808                         return 0;
809                 case NL80211_IFTYPE_P2P_GO:
810                         if (mwifiex_cfg80211_init_p2p_go(priv))
811                                 return -EFAULT;
812                         dev->ieee80211_ptr->iftype = type;
813                         return 0;
814                 case NL80211_IFTYPE_UNSPECIFIED:
815                         wiphy_warn(wiphy, "%s: kept type as STA\n", dev->name);
816                 case NL80211_IFTYPE_STATION:    /* This shouldn't happen */
817                         return 0;
818                 case NL80211_IFTYPE_AP:
819                 default:
820                         wiphy_err(wiphy, "%s: changing to %d not supported\n",
821                                   dev->name, type);
822                         return -EOPNOTSUPP;
823                 }
824                 break;
825         case NL80211_IFTYPE_AP:
826                 switch (type) {
827                 case NL80211_IFTYPE_UNSPECIFIED:
828                         wiphy_warn(wiphy, "%s: kept type as AP\n", dev->name);
829                 case NL80211_IFTYPE_AP:         /* This shouldn't happen */
830                         return 0;
831                 case NL80211_IFTYPE_ADHOC:
832                 case NL80211_IFTYPE_STATION:
833                 default:
834                         wiphy_err(wiphy, "%s: changing to %d not supported\n",
835                                   dev->name, type);
836                         return -EOPNOTSUPP;
837                 }
838                 break;
839         case NL80211_IFTYPE_P2P_CLIENT:
840         case NL80211_IFTYPE_P2P_GO:
841                 switch (type) {
842                 case NL80211_IFTYPE_STATION:
843                         if (mwifiex_cfg80211_deinit_p2p(priv))
844                                 return -EFAULT;
845                         dev->ieee80211_ptr->iftype = type;
846                         return 0;
847                 default:
848                         return -EOPNOTSUPP;
849                 }
850                 break;
851         default:
852                 wiphy_err(wiphy, "%s: unknown iftype: %d\n",
853                           dev->name, dev->ieee80211_ptr->iftype);
854                 return -EOPNOTSUPP;
855         }
856
857         dev->ieee80211_ptr->iftype = type;
858         priv->bss_mode = type;
859         mwifiex_deauthenticate(priv, NULL);
860
861         priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
862
863         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_SET_BSS_MODE,
864                                     HostCmd_ACT_GEN_SET, 0, NULL);
865
866         return ret;
867 }
868
869 static void
870 mwifiex_parse_htinfo(struct mwifiex_private *priv, u8 tx_htinfo,
871                      struct rate_info *rate)
872 {
873         struct mwifiex_adapter *adapter = priv->adapter;
874
875         if (adapter->is_hw_11ac_capable) {
876                 /* bit[1-0]: 00=LG 01=HT 10=VHT */
877                 if (tx_htinfo & BIT(0)) {
878                         /* HT */
879                         rate->mcs = priv->tx_rate;
880                         rate->flags |= RATE_INFO_FLAGS_MCS;
881                 }
882                 if (tx_htinfo & BIT(1)) {
883                         /* VHT */
884                         rate->mcs = priv->tx_rate & 0x0F;
885                         rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
886                 }
887
888                 if (tx_htinfo & (BIT(1) | BIT(0))) {
889                         /* HT or VHT */
890                         switch (tx_htinfo & (BIT(3) | BIT(2))) {
891                         case 0:
892                                 /* This will be 20MHz */
893                                 break;
894                         case (BIT(2)):
895                                 rate->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
896                                 break;
897                         case (BIT(3)):
898                                 rate->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
899                                 break;
900                         case (BIT(3) | BIT(2)):
901                                 rate->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
902                                 break;
903                         }
904
905                         if (tx_htinfo & BIT(4))
906                                 rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
907
908                         if ((priv->tx_rate >> 4) == 1)
909                                 rate->nss = 2;
910                         else
911                                 rate->nss = 1;
912                 }
913         } else {
914                 /*
915                  * Bit 0 in tx_htinfo indicates that current Tx rate
916                  * is 11n rate. Valid MCS index values for us are 0 to 15.
917                  */
918                 if ((tx_htinfo & BIT(0)) && (priv->tx_rate < 16)) {
919                         rate->mcs = priv->tx_rate;
920                         rate->flags |= RATE_INFO_FLAGS_MCS;
921                         if (tx_htinfo & BIT(1))
922                                 rate->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
923                         if (tx_htinfo & BIT(2))
924                                 rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
925                 }
926         }
927 }
928
929 /*
930  * This function dumps the station information on a buffer.
931  *
932  * The following information are shown -
933  *      - Total bytes transmitted
934  *      - Total bytes received
935  *      - Total packets transmitted
936  *      - Total packets received
937  *      - Signal quality level
938  *      - Transmission rate
939  */
940 static int
941 mwifiex_dump_station_info(struct mwifiex_private *priv,
942                           struct station_info *sinfo)
943 {
944         u32 rate;
945
946         sinfo->filled = STATION_INFO_RX_BYTES | STATION_INFO_TX_BYTES |
947                         STATION_INFO_RX_PACKETS | STATION_INFO_TX_PACKETS |
948                         STATION_INFO_TX_BITRATE |
949                         STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
950
951         /* Get signal information from the firmware */
952         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_RSSI_INFO,
953                                   HostCmd_ACT_GEN_GET, 0, NULL)) {
954                 dev_err(priv->adapter->dev, "failed to get signal information\n");
955                 return -EFAULT;
956         }
957
958         if (mwifiex_drv_get_data_rate(priv, &rate)) {
959                 dev_err(priv->adapter->dev, "getting data rate\n");
960                 return -EFAULT;
961         }
962
963         /* Get DTIM period information from firmware */
964         mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_SNMP_MIB,
965                               HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
966                               &priv->dtim_period);
967
968         mwifiex_parse_htinfo(priv, priv->tx_htinfo, &sinfo->txrate);
969
970         sinfo->signal_avg = priv->bcn_rssi_avg;
971         sinfo->rx_bytes = priv->stats.rx_bytes;
972         sinfo->tx_bytes = priv->stats.tx_bytes;
973         sinfo->rx_packets = priv->stats.rx_packets;
974         sinfo->tx_packets = priv->stats.tx_packets;
975         sinfo->signal = priv->bcn_rssi_avg;
976         /* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
977         sinfo->txrate.legacy = rate * 5;
978
979         if (priv->bss_mode == NL80211_IFTYPE_STATION) {
980                 sinfo->filled |= STATION_INFO_BSS_PARAM;
981                 sinfo->bss_param.flags = 0;
982                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
983                                                 WLAN_CAPABILITY_SHORT_PREAMBLE)
984                         sinfo->bss_param.flags |=
985                                         BSS_PARAM_FLAGS_SHORT_PREAMBLE;
986                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
987                                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
988                         sinfo->bss_param.flags |=
989                                         BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
990                 sinfo->bss_param.dtim_period = priv->dtim_period;
991                 sinfo->bss_param.beacon_interval =
992                         priv->curr_bss_params.bss_descriptor.beacon_period;
993         }
994
995         return 0;
996 }
997
998 /*
999  * CFG802.11 operation handler to get station information.
1000  *
1001  * This function only works in connected mode, and dumps the
1002  * requested station information, if available.
1003  */
1004 static int
1005 mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1006                              u8 *mac, struct station_info *sinfo)
1007 {
1008         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1009
1010         if (!priv->media_connected)
1011                 return -ENOENT;
1012         if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1013                 return -ENOENT;
1014
1015         return mwifiex_dump_station_info(priv, sinfo);
1016 }
1017
1018 /*
1019  * CFG802.11 operation handler to dump station information.
1020  */
1021 static int
1022 mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
1023                               int idx, u8 *mac, struct station_info *sinfo)
1024 {
1025         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1026
1027         if (!priv->media_connected || idx)
1028                 return -ENOENT;
1029
1030         memcpy(mac, priv->cfg_bssid, ETH_ALEN);
1031
1032         return mwifiex_dump_station_info(priv, sinfo);
1033 }
1034
1035 /* Supported rates to be advertised to the cfg80211 */
1036 static struct ieee80211_rate mwifiex_rates[] = {
1037         {.bitrate = 10, .hw_value = 2, },
1038         {.bitrate = 20, .hw_value = 4, },
1039         {.bitrate = 55, .hw_value = 11, },
1040         {.bitrate = 110, .hw_value = 22, },
1041         {.bitrate = 60, .hw_value = 12, },
1042         {.bitrate = 90, .hw_value = 18, },
1043         {.bitrate = 120, .hw_value = 24, },
1044         {.bitrate = 180, .hw_value = 36, },
1045         {.bitrate = 240, .hw_value = 48, },
1046         {.bitrate = 360, .hw_value = 72, },
1047         {.bitrate = 480, .hw_value = 96, },
1048         {.bitrate = 540, .hw_value = 108, },
1049 };
1050
1051 /* Channel definitions to be advertised to cfg80211 */
1052 static struct ieee80211_channel mwifiex_channels_2ghz[] = {
1053         {.center_freq = 2412, .hw_value = 1, },
1054         {.center_freq = 2417, .hw_value = 2, },
1055         {.center_freq = 2422, .hw_value = 3, },
1056         {.center_freq = 2427, .hw_value = 4, },
1057         {.center_freq = 2432, .hw_value = 5, },
1058         {.center_freq = 2437, .hw_value = 6, },
1059         {.center_freq = 2442, .hw_value = 7, },
1060         {.center_freq = 2447, .hw_value = 8, },
1061         {.center_freq = 2452, .hw_value = 9, },
1062         {.center_freq = 2457, .hw_value = 10, },
1063         {.center_freq = 2462, .hw_value = 11, },
1064         {.center_freq = 2467, .hw_value = 12, },
1065         {.center_freq = 2472, .hw_value = 13, },
1066         {.center_freq = 2484, .hw_value = 14, },
1067 };
1068
1069 static struct ieee80211_supported_band mwifiex_band_2ghz = {
1070         .channels = mwifiex_channels_2ghz,
1071         .n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
1072         .bitrates = mwifiex_rates,
1073         .n_bitrates = ARRAY_SIZE(mwifiex_rates),
1074 };
1075
1076 static struct ieee80211_channel mwifiex_channels_5ghz[] = {
1077         {.center_freq = 5040, .hw_value = 8, },
1078         {.center_freq = 5060, .hw_value = 12, },
1079         {.center_freq = 5080, .hw_value = 16, },
1080         {.center_freq = 5170, .hw_value = 34, },
1081         {.center_freq = 5190, .hw_value = 38, },
1082         {.center_freq = 5210, .hw_value = 42, },
1083         {.center_freq = 5230, .hw_value = 46, },
1084         {.center_freq = 5180, .hw_value = 36, },
1085         {.center_freq = 5200, .hw_value = 40, },
1086         {.center_freq = 5220, .hw_value = 44, },
1087         {.center_freq = 5240, .hw_value = 48, },
1088         {.center_freq = 5260, .hw_value = 52, },
1089         {.center_freq = 5280, .hw_value = 56, },
1090         {.center_freq = 5300, .hw_value = 60, },
1091         {.center_freq = 5320, .hw_value = 64, },
1092         {.center_freq = 5500, .hw_value = 100, },
1093         {.center_freq = 5520, .hw_value = 104, },
1094         {.center_freq = 5540, .hw_value = 108, },
1095         {.center_freq = 5560, .hw_value = 112, },
1096         {.center_freq = 5580, .hw_value = 116, },
1097         {.center_freq = 5600, .hw_value = 120, },
1098         {.center_freq = 5620, .hw_value = 124, },
1099         {.center_freq = 5640, .hw_value = 128, },
1100         {.center_freq = 5660, .hw_value = 132, },
1101         {.center_freq = 5680, .hw_value = 136, },
1102         {.center_freq = 5700, .hw_value = 140, },
1103         {.center_freq = 5745, .hw_value = 149, },
1104         {.center_freq = 5765, .hw_value = 153, },
1105         {.center_freq = 5785, .hw_value = 157, },
1106         {.center_freq = 5805, .hw_value = 161, },
1107         {.center_freq = 5825, .hw_value = 165, },
1108 };
1109
1110 static struct ieee80211_supported_band mwifiex_band_5ghz = {
1111         .channels = mwifiex_channels_5ghz,
1112         .n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
1113         .bitrates = mwifiex_rates + 4,
1114         .n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
1115 };
1116
1117
1118 /* Supported crypto cipher suits to be advertised to cfg80211 */
1119 static const u32 mwifiex_cipher_suites[] = {
1120         WLAN_CIPHER_SUITE_WEP40,
1121         WLAN_CIPHER_SUITE_WEP104,
1122         WLAN_CIPHER_SUITE_TKIP,
1123         WLAN_CIPHER_SUITE_CCMP,
1124         WLAN_CIPHER_SUITE_AES_CMAC,
1125 };
1126
1127 /* Supported mgmt frame types to be advertised to cfg80211 */
1128 static const struct ieee80211_txrx_stypes
1129 mwifiex_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1130         [NL80211_IFTYPE_STATION] = {
1131                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1132                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1133                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1134                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1135         },
1136         [NL80211_IFTYPE_AP] = {
1137                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1138                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1139                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1140                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1141         },
1142         [NL80211_IFTYPE_P2P_CLIENT] = {
1143                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1144                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1145                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1146                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1147         },
1148         [NL80211_IFTYPE_P2P_GO] = {
1149                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1150                       BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1151                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1152                       BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1153         },
1154 };
1155
1156 /*
1157  * CFG802.11 operation handler for setting bit rates.
1158  *
1159  * Function configures data rates to firmware using bitrate mask
1160  * provided by cfg80211.
1161  */
1162 static int mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1163                                 struct net_device *dev,
1164                                 const u8 *peer,
1165                                 const struct cfg80211_bitrate_mask *mask)
1166 {
1167         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1168         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1169         enum ieee80211_band band;
1170
1171         if (!priv->media_connected) {
1172                 dev_err(priv->adapter->dev,
1173                         "Can not set Tx data rate in disconnected state\n");
1174                 return -EINVAL;
1175         }
1176
1177         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1178
1179         memset(bitmap_rates, 0, sizeof(bitmap_rates));
1180
1181         /* Fill HR/DSSS rates. */
1182         if (band == IEEE80211_BAND_2GHZ)
1183                 bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1184
1185         /* Fill OFDM rates */
1186         if (band == IEEE80211_BAND_2GHZ)
1187                 bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1188         else
1189                 bitmap_rates[1] = mask->control[band].legacy;
1190
1191         /* Fill HT MCS rates */
1192         bitmap_rates[2] = mask->control[band].ht_mcs[0];
1193         if (priv->adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1194                 bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1195
1196         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_TX_RATE_CFG,
1197                                      HostCmd_ACT_GEN_SET, 0, bitmap_rates);
1198 }
1199
1200 /*
1201  * CFG802.11 operation handler for connection quality monitoring.
1202  *
1203  * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
1204  * events to FW.
1205  */
1206 static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1207                                                 struct net_device *dev,
1208                                                 s32 rssi_thold, u32 rssi_hyst)
1209 {
1210         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1211         struct mwifiex_ds_misc_subsc_evt subsc_evt;
1212
1213         priv->cqm_rssi_thold = rssi_thold;
1214         priv->cqm_rssi_hyst = rssi_hyst;
1215
1216         memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
1217         subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1218
1219         /* Subscribe/unsubscribe low and high rssi events */
1220         if (rssi_thold && rssi_hyst) {
1221                 subsc_evt.action = HostCmd_ACT_BITWISE_SET;
1222                 subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1223                 subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1224                 subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1225                 subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1226                 return mwifiex_send_cmd_sync(priv,
1227                                              HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1228                                              0, 0, &subsc_evt);
1229         } else {
1230                 subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
1231                 return mwifiex_send_cmd_sync(priv,
1232                                              HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1233                                              0, 0, &subsc_evt);
1234         }
1235
1236         return 0;
1237 }
1238
1239 /* cfg80211 operation handler for change_beacon.
1240  * Function retrieves and sets modified management IEs to FW.
1241  */
1242 static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
1243                                           struct net_device *dev,
1244                                           struct cfg80211_beacon_data *data)
1245 {
1246         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1247
1248         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
1249                 wiphy_err(wiphy, "%s: bss_type mismatched\n", __func__);
1250                 return -EINVAL;
1251         }
1252
1253         if (!priv->bss_started) {
1254                 wiphy_err(wiphy, "%s: bss not started\n", __func__);
1255                 return -EINVAL;
1256         }
1257
1258         if (mwifiex_set_mgmt_ies(priv, data)) {
1259                 wiphy_err(wiphy, "%s: setting mgmt ies failed\n", __func__);
1260                 return -EFAULT;
1261         }
1262
1263         return 0;
1264 }
1265
1266 /* cfg80211 operation handler for del_station.
1267  * Function deauthenticates station which value is provided in mac parameter.
1268  * If mac is NULL/broadcast, all stations in associated station list are
1269  * deauthenticated. If bss is not started or there are no stations in
1270  * associated stations list, no action is taken.
1271  */
1272 static int
1273 mwifiex_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1274                              u8 *mac)
1275 {
1276         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1277         struct mwifiex_sta_node *sta_node;
1278         unsigned long flags;
1279
1280         if (list_empty(&priv->sta_list) || !priv->bss_started)
1281                 return 0;
1282
1283         if (!mac || is_broadcast_ether_addr(mac)) {
1284                 wiphy_dbg(wiphy, "%s: NULL/broadcast mac address\n", __func__);
1285                 list_for_each_entry(sta_node, &priv->sta_list, list) {
1286                         if (mwifiex_send_cmd_sync(priv,
1287                                                   HostCmd_CMD_UAP_STA_DEAUTH,
1288                                                   HostCmd_ACT_GEN_SET, 0,
1289                                                   sta_node->mac_addr))
1290                                 return -1;
1291                         mwifiex_uap_del_sta_data(priv, sta_node);
1292                 }
1293         } else {
1294                 wiphy_dbg(wiphy, "%s: mac address %pM\n", __func__, mac);
1295                 spin_lock_irqsave(&priv->sta_list_spinlock, flags);
1296                 sta_node = mwifiex_get_sta_entry(priv, mac);
1297                 spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
1298                 if (sta_node) {
1299                         if (mwifiex_send_cmd_sync(priv,
1300                                                   HostCmd_CMD_UAP_STA_DEAUTH,
1301                                                   HostCmd_ACT_GEN_SET, 0,
1302                                                   sta_node->mac_addr))
1303                                 return -1;
1304                         mwifiex_uap_del_sta_data(priv, sta_node);
1305                 }
1306         }
1307
1308         return 0;
1309 }
1310
1311 static int
1312 mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1313 {
1314         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1315         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1316                                                         MWIFIEX_BSS_ROLE_ANY);
1317         struct mwifiex_ds_ant_cfg ant_cfg;
1318
1319         if (!tx_ant || !rx_ant)
1320                 return -EOPNOTSUPP;
1321
1322         if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1323                 /* Not a MIMO chip. User should provide specific antenna number
1324                  * for Tx/Rx path or enable all antennas for diversity
1325                  */
1326                 if (tx_ant != rx_ant)
1327                         return -EOPNOTSUPP;
1328
1329                 if ((tx_ant & (tx_ant - 1)) &&
1330                     (tx_ant != BIT(adapter->number_of_antenna) - 1))
1331                         return -EOPNOTSUPP;
1332
1333                 if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1334                     (priv->adapter->number_of_antenna > 1)) {
1335                         tx_ant = RF_ANTENNA_AUTO;
1336                         rx_ant = RF_ANTENNA_AUTO;
1337                 }
1338         }
1339
1340         ant_cfg.tx_ant = tx_ant;
1341         ant_cfg.rx_ant = rx_ant;
1342
1343         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_RF_ANTENNA,
1344                                      HostCmd_ACT_GEN_SET, 0, &ant_cfg);
1345 }
1346
1347 /* cfg80211 operation handler for stop ap.
1348  * Function stops BSS running at uAP interface.
1349  */
1350 static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1351 {
1352         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1353
1354         if (mwifiex_del_mgmt_ies(priv))
1355                 wiphy_err(wiphy, "Failed to delete mgmt IEs!\n");
1356
1357         priv->ap_11n_enabled = 0;
1358
1359         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_UAP_BSS_STOP,
1360                                   HostCmd_ACT_GEN_SET, 0, NULL)) {
1361                 wiphy_err(wiphy, "Failed to stop the BSS\n");
1362                 return -1;
1363         }
1364
1365         return 0;
1366 }
1367
1368 /* cfg80211 operation handler for start_ap.
1369  * Function sets beacon period, DTIM period, SSID and security into
1370  * AP config structure.
1371  * AP is configured with these settings and BSS is started.
1372  */
1373 static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
1374                                      struct net_device *dev,
1375                                      struct cfg80211_ap_settings *params)
1376 {
1377         struct mwifiex_uap_bss_param *bss_cfg;
1378         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1379         u8 config_bands = 0;
1380
1381         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
1382                 return -1;
1383         if (mwifiex_set_mgmt_ies(priv, &params->beacon))
1384                 return -1;
1385
1386         bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
1387         if (!bss_cfg)
1388                 return -ENOMEM;
1389
1390         mwifiex_set_sys_config_invalid_data(bss_cfg);
1391
1392         if (params->beacon_interval)
1393                 bss_cfg->beacon_period = params->beacon_interval;
1394         if (params->dtim_period)
1395                 bss_cfg->dtim_period = params->dtim_period;
1396
1397         if (params->ssid && params->ssid_len) {
1398                 memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
1399                 bss_cfg->ssid.ssid_len = params->ssid_len;
1400         }
1401
1402         switch (params->hidden_ssid) {
1403         case NL80211_HIDDEN_SSID_NOT_IN_USE:
1404                 bss_cfg->bcast_ssid_ctl = 1;
1405                 break;
1406         case NL80211_HIDDEN_SSID_ZERO_LEN:
1407                 bss_cfg->bcast_ssid_ctl = 0;
1408                 break;
1409         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1410                 /* firmware doesn't support this type of hidden SSID */
1411         default:
1412                 kfree(bss_cfg);
1413                 return -EINVAL;
1414         }
1415
1416         bss_cfg->channel = ieee80211_frequency_to_channel(
1417                                 params->chandef.chan->center_freq);
1418
1419         /* Set appropriate bands */
1420         if (params->chandef.chan->band == IEEE80211_BAND_2GHZ) {
1421                 bss_cfg->band_cfg = BAND_CONFIG_BG;
1422                 config_bands = BAND_B | BAND_G;
1423
1424                 if (params->chandef.width > NL80211_CHAN_WIDTH_20_NOHT)
1425                         config_bands |= BAND_GN;
1426
1427                 if (params->chandef.width > NL80211_CHAN_WIDTH_40)
1428                         config_bands |= BAND_GAC;
1429         } else {
1430                 bss_cfg->band_cfg = BAND_CONFIG_A;
1431                 config_bands = BAND_A;
1432
1433                 if (params->chandef.width > NL80211_CHAN_WIDTH_20_NOHT)
1434                         config_bands |= BAND_AN;
1435
1436                 if (params->chandef.width > NL80211_CHAN_WIDTH_40)
1437                         config_bands |= BAND_AAC;
1438         }
1439
1440         if (!((config_bands | priv->adapter->fw_bands) &
1441               ~priv->adapter->fw_bands))
1442                 priv->adapter->config_bands = config_bands;
1443
1444         mwifiex_set_uap_rates(bss_cfg, params);
1445         mwifiex_send_domain_info_cmd_fw(wiphy);
1446
1447         if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
1448                 kfree(bss_cfg);
1449                 wiphy_err(wiphy, "Failed to parse secuirty parameters!\n");
1450                 return -1;
1451         }
1452
1453         mwifiex_set_ht_params(priv, bss_cfg, params);
1454
1455         if (priv->adapter->is_hw_11ac_capable) {
1456                 mwifiex_set_vht_params(priv, bss_cfg, params);
1457                 mwifiex_set_vht_width(priv, params->chandef.width,
1458                                       priv->ap_11ac_enabled);
1459         }
1460
1461         if (priv->ap_11ac_enabled)
1462                 mwifiex_set_11ac_ba_params(priv);
1463         else
1464                 mwifiex_set_ba_params(priv);
1465
1466         mwifiex_set_wmm_params(priv, bss_cfg, params);
1467
1468         if (params->inactivity_timeout > 0) {
1469                 /* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
1470                 bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
1471                 bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
1472         }
1473
1474         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_UAP_BSS_STOP,
1475                                   HostCmd_ACT_GEN_SET, 0, NULL)) {
1476                 wiphy_err(wiphy, "Failed to stop the BSS\n");
1477                 kfree(bss_cfg);
1478                 return -1;
1479         }
1480
1481         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_UAP_SYS_CONFIG,
1482                                    HostCmd_ACT_GEN_SET,
1483                                    UAP_BSS_PARAMS_I, bss_cfg)) {
1484                 wiphy_err(wiphy, "Failed to set the SSID\n");
1485                 kfree(bss_cfg);
1486                 return -1;
1487         }
1488
1489         kfree(bss_cfg);
1490
1491         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_UAP_BSS_START,
1492                                    HostCmd_ACT_GEN_SET, 0, NULL)) {
1493                 wiphy_err(wiphy, "Failed to start the BSS\n");
1494                 return -1;
1495         }
1496
1497         if (priv->sec_info.wep_enabled)
1498                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
1499         else
1500                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
1501
1502         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_MAC_CONTROL,
1503                                   HostCmd_ACT_GEN_SET, 0,
1504                                   &priv->curr_pkt_filter))
1505                 return -1;
1506
1507         return 0;
1508 }
1509
1510 /*
1511  * CFG802.11 operation handler for disconnection request.
1512  *
1513  * This function does not work when there is already a disconnection
1514  * procedure going on.
1515  */
1516 static int
1517 mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
1518                             u16 reason_code)
1519 {
1520         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1521
1522         if (mwifiex_deauthenticate(priv, NULL))
1523                 return -EFAULT;
1524
1525         wiphy_dbg(wiphy, "info: successfully disconnected from %pM:"
1526                 " reason code %d\n", priv->cfg_bssid, reason_code);
1527
1528         memset(priv->cfg_bssid, 0, ETH_ALEN);
1529         priv->hs2_enabled = false;
1530
1531         return 0;
1532 }
1533
1534 /*
1535  * This function informs the CFG802.11 subsystem of a new IBSS.
1536  *
1537  * The following information are sent to the CFG802.11 subsystem
1538  * to register the new IBSS. If we do not register the new IBSS,
1539  * a kernel panic will result.
1540  *      - SSID
1541  *      - SSID length
1542  *      - BSSID
1543  *      - Channel
1544  */
1545 static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
1546 {
1547         struct ieee80211_channel *chan;
1548         struct mwifiex_bss_info bss_info;
1549         struct cfg80211_bss *bss;
1550         int ie_len;
1551         u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
1552         enum ieee80211_band band;
1553
1554         if (mwifiex_get_bss_info(priv, &bss_info))
1555                 return -1;
1556
1557         ie_buf[0] = WLAN_EID_SSID;
1558         ie_buf[1] = bss_info.ssid.ssid_len;
1559
1560         memcpy(&ie_buf[sizeof(struct ieee_types_header)],
1561                &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
1562         ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
1563
1564         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1565         chan = __ieee80211_get_channel(priv->wdev->wiphy,
1566                         ieee80211_channel_to_frequency(bss_info.bss_chan,
1567                                                        band));
1568
1569         bss = cfg80211_inform_bss(priv->wdev->wiphy, chan,
1570                                   bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
1571                                   0, ie_buf, ie_len, 0, GFP_KERNEL);
1572         cfg80211_put_bss(priv->wdev->wiphy, bss);
1573         memcpy(priv->cfg_bssid, bss_info.bssid, ETH_ALEN);
1574
1575         return 0;
1576 }
1577
1578 /*
1579  * This function connects with a BSS.
1580  *
1581  * This function handles both Infra and Ad-Hoc modes. It also performs
1582  * validity checking on the provided parameters, disconnects from the
1583  * current BSS (if any), sets up the association/scan parameters,
1584  * including security settings, and performs specific SSID scan before
1585  * trying to connect.
1586  *
1587  * For Infra mode, the function returns failure if the specified SSID
1588  * is not found in scan table. However, for Ad-Hoc mode, it can create
1589  * the IBSS if it does not exist. On successful completion in either case,
1590  * the function notifies the CFG802.11 subsystem of the new BSS connection.
1591  */
1592 static int
1593 mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len, u8 *ssid,
1594                        u8 *bssid, int mode, struct ieee80211_channel *channel,
1595                        struct cfg80211_connect_params *sme, bool privacy)
1596 {
1597         struct cfg80211_ssid req_ssid;
1598         int ret, auth_type = 0;
1599         struct cfg80211_bss *bss = NULL;
1600         u8 is_scanning_required = 0;
1601
1602         memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
1603
1604         req_ssid.ssid_len = ssid_len;
1605         if (ssid_len > IEEE80211_MAX_SSID_LEN) {
1606                 dev_err(priv->adapter->dev, "invalid SSID - aborting\n");
1607                 return -EINVAL;
1608         }
1609
1610         memcpy(req_ssid.ssid, ssid, ssid_len);
1611         if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
1612                 dev_err(priv->adapter->dev, "invalid SSID - aborting\n");
1613                 return -EINVAL;
1614         }
1615
1616         /* disconnect before try to associate */
1617         mwifiex_deauthenticate(priv, NULL);
1618
1619         /* As this is new association, clear locally stored
1620          * keys and security related flags */
1621         priv->sec_info.wpa_enabled = false;
1622         priv->sec_info.wpa2_enabled = false;
1623         priv->wep_key_curr_index = 0;
1624         priv->sec_info.encryption_mode = 0;
1625         priv->sec_info.is_authtype_auto = 0;
1626         ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
1627
1628         if (mode == NL80211_IFTYPE_ADHOC) {
1629                 /* "privacy" is set only for ad-hoc mode */
1630                 if (privacy) {
1631                         /*
1632                          * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
1633                          * the firmware can find a matching network from the
1634                          * scan. The cfg80211 does not give us the encryption
1635                          * mode at this stage so just setting it to WEP here.
1636                          */
1637                         priv->sec_info.encryption_mode =
1638                                         WLAN_CIPHER_SUITE_WEP104;
1639                         priv->sec_info.authentication_mode =
1640                                         NL80211_AUTHTYPE_OPEN_SYSTEM;
1641                 }
1642
1643                 goto done;
1644         }
1645
1646         /* Now handle infra mode. "sme" is valid for infra mode only */
1647         if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
1648                 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1649                 priv->sec_info.is_authtype_auto = 1;
1650         } else {
1651                 auth_type = sme->auth_type;
1652         }
1653
1654         if (sme->crypto.n_ciphers_pairwise) {
1655                 priv->sec_info.encryption_mode =
1656                                                 sme->crypto.ciphers_pairwise[0];
1657                 priv->sec_info.authentication_mode = auth_type;
1658         }
1659
1660         if (sme->crypto.cipher_group) {
1661                 priv->sec_info.encryption_mode = sme->crypto.cipher_group;
1662                 priv->sec_info.authentication_mode = auth_type;
1663         }
1664         if (sme->ie)
1665                 ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
1666
1667         if (sme->key) {
1668                 if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
1669                         dev_dbg(priv->adapter->dev,
1670                                 "info: setting wep encryption"
1671                                 " with key len %d\n", sme->key_len);
1672                         priv->wep_key_curr_index = sme->key_idx;
1673                         ret = mwifiex_set_encode(priv, NULL, sme->key,
1674                                                  sme->key_len, sme->key_idx,
1675                                                  NULL, 0);
1676                 }
1677         }
1678 done:
1679         /*
1680          * Scan entries are valid for some time (15 sec). So we can save one
1681          * active scan time if we just try cfg80211_get_bss first. If it fails
1682          * then request scan and cfg80211_get_bss() again for final output.
1683          */
1684         while (1) {
1685                 if (is_scanning_required) {
1686                         /* Do specific SSID scanning */
1687                         if (mwifiex_request_scan(priv, &req_ssid)) {
1688                                 dev_err(priv->adapter->dev, "scan error\n");
1689                                 return -EFAULT;
1690                         }
1691                 }
1692
1693                 /* Find the BSS we want using available scan results */
1694                 if (mode == NL80211_IFTYPE_ADHOC)
1695                         bss = cfg80211_get_bss(priv->wdev->wiphy, channel,
1696                                                bssid, ssid, ssid_len,
1697                                                WLAN_CAPABILITY_IBSS,
1698                                                WLAN_CAPABILITY_IBSS);
1699                 else
1700                         bss = cfg80211_get_bss(priv->wdev->wiphy, channel,
1701                                                bssid, ssid, ssid_len,
1702                                                WLAN_CAPABILITY_ESS,
1703                                                WLAN_CAPABILITY_ESS);
1704
1705                 if (!bss) {
1706                         if (is_scanning_required) {
1707                                 dev_warn(priv->adapter->dev,
1708                                          "assoc: requested bss not found in scan results\n");
1709                                 break;
1710                         }
1711                         is_scanning_required = 1;
1712                 } else {
1713                         dev_dbg(priv->adapter->dev,
1714                                 "info: trying to associate to '%s' bssid %pM\n",
1715                                 (char *) req_ssid.ssid, bss->bssid);
1716                         memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
1717                         break;
1718                 }
1719         }
1720
1721         ret = mwifiex_bss_start(priv, bss, &req_ssid);
1722         if (ret)
1723                 return ret;
1724
1725         if (mode == NL80211_IFTYPE_ADHOC) {
1726                 /* Inform the BSS information to kernel, otherwise
1727                  * kernel will give a panic after successful assoc */
1728                 if (mwifiex_cfg80211_inform_ibss_bss(priv))
1729                         return -EFAULT;
1730         }
1731
1732         return ret;
1733 }
1734
1735 /*
1736  * CFG802.11 operation handler for association request.
1737  *
1738  * This function does not work when the current mode is set to Ad-Hoc, or
1739  * when there is already an association procedure going on. The given BSS
1740  * information is used to associate.
1741  */
1742 static int
1743 mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
1744                          struct cfg80211_connect_params *sme)
1745 {
1746         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1747         int ret;
1748
1749         if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) {
1750                 wiphy_err(wiphy,
1751                           "%s: reject infra assoc request in non-STA role\n",
1752                           dev->name);
1753                 return -EINVAL;
1754         }
1755
1756         wiphy_dbg(wiphy, "info: Trying to associate to %s and bssid %pM\n",
1757                   (char *) sme->ssid, sme->bssid);
1758
1759         ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
1760                                      priv->bss_mode, sme->channel, sme, 0);
1761         if (!ret) {
1762                 cfg80211_connect_result(priv->netdev, priv->cfg_bssid, NULL, 0,
1763                                         NULL, 0, WLAN_STATUS_SUCCESS,
1764                                         GFP_KERNEL);
1765                 dev_dbg(priv->adapter->dev,
1766                         "info: associated to bssid %pM successfully\n",
1767                         priv->cfg_bssid);
1768         } else {
1769                 dev_dbg(priv->adapter->dev,
1770                         "info: association to bssid %pM failed\n",
1771                         priv->cfg_bssid);
1772                 memset(priv->cfg_bssid, 0, ETH_ALEN);
1773
1774                 if (ret > 0)
1775                         cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
1776                                                 NULL, 0, NULL, 0, ret,
1777                                                 GFP_KERNEL);
1778                 else
1779                         cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
1780                                                 NULL, 0, NULL, 0,
1781                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
1782                                                 GFP_KERNEL);
1783         }
1784
1785         return 0;
1786 }
1787
1788 /*
1789  * This function sets following parameters for ibss network.
1790  *  -  channel
1791  *  -  start band
1792  *  -  11n flag
1793  *  -  secondary channel offset
1794  */
1795 static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
1796                                    struct cfg80211_ibss_params *params)
1797 {
1798         struct wiphy *wiphy = priv->wdev->wiphy;
1799         struct mwifiex_adapter *adapter = priv->adapter;
1800         int index = 0, i;
1801         u8 config_bands = 0;
1802
1803         if (params->chandef.chan->band == IEEE80211_BAND_2GHZ) {
1804                 if (!params->basic_rates) {
1805                         config_bands = BAND_B | BAND_G;
1806                 } else {
1807                         for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
1808                                 /*
1809                                  * Rates below 6 Mbps in the table are CCK
1810                                  * rates; 802.11b and from 6 they are OFDM;
1811                                  * 802.11G
1812                                  */
1813                                 if (mwifiex_rates[i].bitrate == 60) {
1814                                         index = 1 << i;
1815                                         break;
1816                                 }
1817                         }
1818
1819                         if (params->basic_rates < index) {
1820                                 config_bands = BAND_B;
1821                         } else {
1822                                 config_bands = BAND_G;
1823                                 if (params->basic_rates % index)
1824                                         config_bands |= BAND_B;
1825                         }
1826                 }
1827
1828                 if (cfg80211_get_chandef_type(&params->chandef) !=
1829                                                 NL80211_CHAN_NO_HT)
1830                         config_bands |= BAND_G | BAND_GN;
1831         } else {
1832                 if (cfg80211_get_chandef_type(&params->chandef) ==
1833                                                 NL80211_CHAN_NO_HT)
1834                         config_bands = BAND_A;
1835                 else
1836                         config_bands = BAND_AN | BAND_A;
1837         }
1838
1839         if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
1840                 adapter->config_bands = config_bands;
1841                 adapter->adhoc_start_band = config_bands;
1842
1843                 if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
1844                         adapter->adhoc_11n_enabled = true;
1845                 else
1846                         adapter->adhoc_11n_enabled = false;
1847         }
1848
1849         adapter->sec_chan_offset =
1850                 mwifiex_chan_type_to_sec_chan_offset(
1851                         cfg80211_get_chandef_type(&params->chandef));
1852         priv->adhoc_channel = ieee80211_frequency_to_channel(
1853                                 params->chandef.chan->center_freq);
1854
1855         wiphy_dbg(wiphy, "info: set ibss band %d, chan %d, chan offset %d\n",
1856                   config_bands, priv->adhoc_channel, adapter->sec_chan_offset);
1857
1858         return 0;
1859 }
1860
1861 /*
1862  * CFG802.11 operation handler to join an IBSS.
1863  *
1864  * This function does not work in any mode other than Ad-Hoc, or if
1865  * a join operation is already in progress.
1866  */
1867 static int
1868 mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1869                            struct cfg80211_ibss_params *params)
1870 {
1871         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1872         int ret = 0;
1873
1874         if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
1875                 wiphy_err(wiphy, "request to join ibss received "
1876                                 "when station is not in ibss mode\n");
1877                 goto done;
1878         }
1879
1880         wiphy_dbg(wiphy, "info: trying to join to %s and bssid %pM\n",
1881                   (char *) params->ssid, params->bssid);
1882
1883         mwifiex_set_ibss_params(priv, params);
1884
1885         ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
1886                                      params->bssid, priv->bss_mode,
1887                                      params->chandef.chan, NULL,
1888                                      params->privacy);
1889 done:
1890         if (!ret) {
1891                 cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid, GFP_KERNEL);
1892                 dev_dbg(priv->adapter->dev,
1893                         "info: joined/created adhoc network with bssid"
1894                         " %pM successfully\n", priv->cfg_bssid);
1895         } else {
1896                 dev_dbg(priv->adapter->dev,
1897                         "info: failed creating/joining adhoc network\n");
1898         }
1899
1900         return ret;
1901 }
1902
1903 /*
1904  * CFG802.11 operation handler to leave an IBSS.
1905  *
1906  * This function does not work if a leave operation is
1907  * already in progress.
1908  */
1909 static int
1910 mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1911 {
1912         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1913
1914         wiphy_dbg(wiphy, "info: disconnecting from essid %pM\n",
1915                   priv->cfg_bssid);
1916         if (mwifiex_deauthenticate(priv, NULL))
1917                 return -EFAULT;
1918
1919         memset(priv->cfg_bssid, 0, ETH_ALEN);
1920
1921         return 0;
1922 }
1923
1924 /*
1925  * CFG802.11 operation handler for scan request.
1926  *
1927  * This function issues a scan request to the firmware based upon
1928  * the user specified scan configuration. On successfull completion,
1929  * it also informs the results.
1930  */
1931 static int
1932 mwifiex_cfg80211_scan(struct wiphy *wiphy,
1933                       struct cfg80211_scan_request *request)
1934 {
1935         struct net_device *dev = request->wdev->netdev;
1936         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1937         int i, offset, ret;
1938         struct ieee80211_channel *chan;
1939         struct ieee_types_header *ie;
1940         struct mwifiex_user_scan_cfg *user_scan_cfg;
1941
1942         wiphy_dbg(wiphy, "info: received scan request on %s\n", dev->name);
1943
1944         if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
1945             atomic_read(&priv->wmm.tx_pkts_queued) >=
1946             MWIFIEX_MIN_TX_PENDING_TO_CANCEL_SCAN) {
1947                 dev_dbg(priv->adapter->dev, "scan rejected due to traffic\n");
1948                 return -EBUSY;
1949         }
1950
1951         /* Block scan request if scan operation or scan cleanup when interface
1952          * is disabled is in process
1953          */
1954         if (priv->scan_request || priv->scan_aborting) {
1955                 dev_err(priv->adapter->dev, "cmd: Scan already in process..\n");
1956                 return -EBUSY;
1957         }
1958
1959         user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
1960         if (!user_scan_cfg)
1961                 return -ENOMEM;
1962
1963         priv->scan_request = request;
1964
1965         user_scan_cfg->num_ssids = request->n_ssids;
1966         user_scan_cfg->ssid_list = request->ssids;
1967
1968         if (request->ie && request->ie_len) {
1969                 offset = 0;
1970                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
1971                         if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
1972                                 continue;
1973                         priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
1974                         ie = (struct ieee_types_header *)(request->ie + offset);
1975                         memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
1976                         offset += sizeof(*ie) + ie->len;
1977
1978                         if (offset >= request->ie_len)
1979                                 break;
1980                 }
1981         }
1982
1983         for (i = 0; i < min_t(u32, request->n_channels,
1984                               MWIFIEX_USER_SCAN_CHAN_MAX); i++) {
1985                 chan = request->channels[i];
1986                 user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
1987                 user_scan_cfg->chan_list[i].radio_type = chan->band;
1988
1989                 if (chan->flags & IEEE80211_CHAN_NO_IR)
1990                         user_scan_cfg->chan_list[i].scan_type =
1991                                                 MWIFIEX_SCAN_TYPE_PASSIVE;
1992                 else
1993                         user_scan_cfg->chan_list[i].scan_type =
1994                                                 MWIFIEX_SCAN_TYPE_ACTIVE;
1995
1996                 user_scan_cfg->chan_list[i].scan_time = 0;
1997         }
1998
1999         ret = mwifiex_scan_networks(priv, user_scan_cfg);
2000         kfree(user_scan_cfg);
2001         if (ret) {
2002                 dev_err(priv->adapter->dev, "scan failed: %d\n", ret);
2003                 priv->scan_aborting = false;
2004                 priv->scan_request = NULL;
2005                 return ret;
2006         }
2007
2008         if (request->ie && request->ie_len) {
2009                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2010                         if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
2011                                 priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
2012                                 memset(&priv->vs_ie[i].ie, 0,
2013                                        MWIFIEX_MAX_VSIE_LEN);
2014                         }
2015                 }
2016         }
2017         return 0;
2018 }
2019
2020 static void mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap *vht_info,
2021                                    struct mwifiex_private *priv)
2022 {
2023         struct mwifiex_adapter *adapter = priv->adapter;
2024
2025         vht_info->vht_supported = true;
2026
2027         vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2028         /* Update MCS support for VHT */
2029         vht_info->vht_mcs.rx_mcs_map = cpu_to_le16(
2030                                 adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2031         vht_info->vht_mcs.rx_highest = 0;
2032         vht_info->vht_mcs.tx_mcs_map = cpu_to_le16(
2033                                 adapter->hw_dot_11ac_mcs_support >> 16);
2034         vht_info->vht_mcs.tx_highest = 0;
2035 }
2036
2037 /*
2038  * This function sets up the CFG802.11 specific HT capability fields
2039  * with default values.
2040  *
2041  * The following default values are set -
2042  *      - HT Supported = True
2043  *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
2044  *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
2045  *      - HT Capabilities supported by firmware
2046  *      - MCS information, Rx mask = 0xff
2047  *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
2048  */
2049 static void
2050 mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
2051                       struct mwifiex_private *priv)
2052 {
2053         int rx_mcs_supp;
2054         struct ieee80211_mcs_info mcs_set;
2055         u8 *mcs = (u8 *)&mcs_set;
2056         struct mwifiex_adapter *adapter = priv->adapter;
2057
2058         ht_info->ht_supported = true;
2059         ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2060         ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2061
2062         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2063
2064         /* Fill HT capability information */
2065         if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2066                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2067         else
2068                 ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2069
2070         if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2071                 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2072         else
2073                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2074
2075         if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2076                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2077         else
2078                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2079
2080         if (ISSUPP_RXSTBC(adapter->hw_dot_11n_dev_cap))
2081                 ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2082         else
2083                 ht_info->cap &= ~(3 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
2084
2085         if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2086                 ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2087         else
2088                 ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2089
2090         if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2091                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2092         else
2093                 ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2094
2095         if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2096                 ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2097         else
2098                 ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2099
2100         if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2101                 ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2102         else
2103                 ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2104
2105         ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2106         ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2107
2108         rx_mcs_supp = GET_RXMCSSUPP(adapter->hw_dev_mcs_support);
2109         /* Set MCS for 1x1 */
2110         memset(mcs, 0xff, rx_mcs_supp);
2111         /* Clear all the other values */
2112         memset(&mcs[rx_mcs_supp], 0,
2113                sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2114         if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2115             ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2116                 /* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2117                 SETHT_MCS32(mcs_set.rx_mask);
2118
2119         memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2120
2121         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2122 }
2123
2124 /*
2125  *  create a new virtual interface with the given name
2126  */
2127 struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
2128                                               const char *name,
2129                                               enum nl80211_iftype type,
2130                                               u32 *flags,
2131                                               struct vif_params *params)
2132 {
2133         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2134         struct mwifiex_private *priv;
2135         struct net_device *dev;
2136         void *mdev_priv;
2137         struct wireless_dev *wdev;
2138
2139         if (!adapter)
2140                 return ERR_PTR(-EFAULT);
2141
2142         switch (type) {
2143         case NL80211_IFTYPE_UNSPECIFIED:
2144         case NL80211_IFTYPE_STATION:
2145         case NL80211_IFTYPE_ADHOC:
2146                 priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
2147                 if (priv->bss_mode) {
2148                         wiphy_err(wiphy,
2149                                   "cannot create multiple sta/adhoc ifaces\n");
2150                         return ERR_PTR(-EINVAL);
2151                 }
2152
2153                 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2154                 if (!wdev)
2155                         return ERR_PTR(-ENOMEM);
2156
2157                 wdev->wiphy = wiphy;
2158                 priv->wdev = wdev;
2159                 wdev->iftype = NL80211_IFTYPE_STATION;
2160
2161                 if (type == NL80211_IFTYPE_UNSPECIFIED)
2162                         priv->bss_mode = NL80211_IFTYPE_STATION;
2163                 else
2164                         priv->bss_mode = type;
2165
2166                 priv->bss_type = MWIFIEX_BSS_TYPE_STA;
2167                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2168                 priv->bss_priority = 0;
2169                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2170                 priv->bss_num = 0;
2171
2172                 break;
2173         case NL80211_IFTYPE_AP:
2174                 priv = adapter->priv[MWIFIEX_BSS_TYPE_UAP];
2175
2176                 if (priv->bss_mode) {
2177                         wiphy_err(wiphy, "Can't create multiple AP interfaces");
2178                         return ERR_PTR(-EINVAL);
2179                 }
2180
2181                 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2182                 if (!wdev)
2183                         return ERR_PTR(-ENOMEM);
2184
2185                 priv->wdev = wdev;
2186                 wdev->wiphy = wiphy;
2187                 wdev->iftype = NL80211_IFTYPE_AP;
2188
2189                 priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
2190                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2191                 priv->bss_priority = 0;
2192                 priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
2193                 priv->bss_started = 0;
2194                 priv->bss_num = 0;
2195                 priv->bss_mode = type;
2196
2197                 break;
2198         case NL80211_IFTYPE_P2P_CLIENT:
2199                 priv = adapter->priv[MWIFIEX_BSS_TYPE_P2P];
2200
2201                 if (priv->bss_mode) {
2202                         wiphy_err(wiphy, "Can't create multiple P2P ifaces");
2203                         return ERR_PTR(-EINVAL);
2204                 }
2205
2206                 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2207                 if (!wdev)
2208                         return ERR_PTR(-ENOMEM);
2209
2210                 priv->wdev = wdev;
2211                 wdev->wiphy = wiphy;
2212
2213                 /* At start-up, wpa_supplicant tries to change the interface
2214                  * to NL80211_IFTYPE_STATION if it is not managed mode.
2215                  */
2216                 wdev->iftype = NL80211_IFTYPE_P2P_CLIENT;
2217                 priv->bss_mode = NL80211_IFTYPE_P2P_CLIENT;
2218
2219                 /* Setting bss_type to P2P tells firmware that this interface
2220                  * is receiving P2P peers found during find phase and doing
2221                  * action frame handshake.
2222                  */
2223                 priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
2224
2225                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2226                 priv->bss_priority = MWIFIEX_BSS_ROLE_STA;
2227                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2228                 priv->bss_started = 0;
2229                 priv->bss_num = 0;
2230
2231                 if (mwifiex_cfg80211_init_p2p_client(priv)) {
2232                         wdev = ERR_PTR(-EFAULT);
2233                         goto done;
2234                 }
2235
2236                 break;
2237         default:
2238                 wiphy_err(wiphy, "type not supported\n");
2239                 return ERR_PTR(-EINVAL);
2240         }
2241
2242         dev = alloc_netdev_mqs(sizeof(struct mwifiex_private *), name,
2243                                ether_setup, IEEE80211_NUM_ACS, 1);
2244         if (!dev) {
2245                 wiphy_err(wiphy, "no memory available for netdevice\n");
2246                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2247                 wdev = ERR_PTR(-ENOMEM);
2248                 goto done;
2249         }
2250
2251         mwifiex_init_priv_params(priv, dev);
2252         priv->netdev = dev;
2253
2254         mwifiex_setup_ht_caps(&wiphy->bands[IEEE80211_BAND_2GHZ]->ht_cap, priv);
2255         if (adapter->is_hw_11ac_capable)
2256                 mwifiex_setup_vht_caps(
2257                         &wiphy->bands[IEEE80211_BAND_2GHZ]->vht_cap, priv);
2258
2259         if (adapter->config_bands & BAND_A)
2260                 mwifiex_setup_ht_caps(
2261                         &wiphy->bands[IEEE80211_BAND_5GHZ]->ht_cap, priv);
2262
2263         if ((adapter->config_bands & BAND_A) && adapter->is_hw_11ac_capable)
2264                 mwifiex_setup_vht_caps(
2265                         &wiphy->bands[IEEE80211_BAND_5GHZ]->vht_cap, priv);
2266
2267         dev_net_set(dev, wiphy_net(wiphy));
2268         dev->ieee80211_ptr = priv->wdev;
2269         dev->ieee80211_ptr->iftype = priv->bss_mode;
2270         memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2271         SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
2272
2273         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
2274         dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
2275         dev->hard_header_len += MWIFIEX_MIN_DATA_HEADER_LEN;
2276         dev->ethtool_ops = &mwifiex_ethtool_ops;
2277
2278         mdev_priv = netdev_priv(dev);
2279         *((unsigned long *) mdev_priv) = (unsigned long) priv;
2280
2281         SET_NETDEV_DEV(dev, adapter->dev);
2282
2283         /* Register network device */
2284         if (register_netdevice(dev)) {
2285                 wiphy_err(wiphy, "cannot register virtual network device\n");
2286                 free_netdev(dev);
2287                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2288                 priv->netdev = NULL;
2289                 wdev = ERR_PTR(-EFAULT);
2290                 goto done;
2291         }
2292
2293         sema_init(&priv->async_sem, 1);
2294
2295         dev_dbg(adapter->dev, "info: %s: Marvell 802.11 Adapter\n", dev->name);
2296
2297 #ifdef CONFIG_DEBUG_FS
2298         mwifiex_dev_debugfs_init(priv);
2299 #endif
2300
2301 done:
2302         if (IS_ERR(wdev)) {
2303                 kfree(priv->wdev);
2304                 priv->wdev = NULL;
2305         }
2306
2307         return wdev;
2308 }
2309 EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
2310
2311 /*
2312  * del_virtual_intf: remove the virtual interface determined by dev
2313  */
2314 int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
2315 {
2316         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
2317
2318 #ifdef CONFIG_DEBUG_FS
2319         mwifiex_dev_debugfs_remove(priv);
2320 #endif
2321
2322         mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
2323
2324         if (netif_carrier_ok(priv->netdev))
2325                 netif_carrier_off(priv->netdev);
2326
2327         if (wdev->netdev->reg_state == NETREG_REGISTERED)
2328                 unregister_netdevice(wdev->netdev);
2329
2330         /* Clear the priv in adapter */
2331         priv->netdev->ieee80211_ptr = NULL;
2332         priv->netdev = NULL;
2333         kfree(wdev);
2334         priv->wdev = NULL;
2335
2336         priv->media_connected = false;
2337
2338         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2339
2340         return 0;
2341 }
2342 EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
2343
2344 static bool
2345 mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
2346                              u8 max_byte_seq)
2347 {
2348         int j, k, valid_byte_cnt = 0;
2349         bool dont_care_byte = false;
2350
2351         for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
2352                 for (k = 0; k < 8; k++) {
2353                         if (pat->mask[j] & 1 << k) {
2354                                 memcpy(byte_seq + valid_byte_cnt,
2355                                        &pat->pattern[j * 8 + k], 1);
2356                                 valid_byte_cnt++;
2357                                 if (dont_care_byte)
2358                                         return false;
2359                         } else {
2360                                 if (valid_byte_cnt)
2361                                         dont_care_byte = true;
2362                         }
2363
2364                         if (valid_byte_cnt > max_byte_seq)
2365                                 return false;
2366                 }
2367         }
2368
2369         byte_seq[max_byte_seq] = valid_byte_cnt;
2370
2371         return true;
2372 }
2373
2374 #ifdef CONFIG_PM
2375 static int mwifiex_cfg80211_suspend(struct wiphy *wiphy,
2376                                     struct cfg80211_wowlan *wowlan)
2377 {
2378         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2379         struct mwifiex_ds_mef_cfg mef_cfg;
2380         struct mwifiex_mef_entry *mef_entry;
2381         int i, filt_num = 0, ret;
2382         bool first_pat = true;
2383         u8 byte_seq[MWIFIEX_MEF_MAX_BYTESEQ + 1];
2384         const u8 ipv4_mc_mac[] = {0x33, 0x33};
2385         const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
2386         struct mwifiex_private *priv =
2387                         mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
2388
2389         if (!wowlan) {
2390                 dev_warn(adapter->dev, "None of the WOWLAN triggers enabled\n");
2391                 return 0;
2392         }
2393
2394         if (!priv->media_connected) {
2395                 dev_warn(adapter->dev,
2396                          "Can not configure WOWLAN in disconnected state\n");
2397                 return 0;
2398         }
2399
2400         mef_entry = kzalloc(sizeof(*mef_entry), GFP_KERNEL);
2401         if (!mef_entry)
2402                 return -ENOMEM;
2403
2404         memset(&mef_cfg, 0, sizeof(mef_cfg));
2405         mef_cfg.num_entries = 1;
2406         mef_cfg.mef_entry = mef_entry;
2407         mef_entry->mode = MEF_MODE_HOST_SLEEP;
2408         mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
2409
2410         for (i = 0; i < wowlan->n_patterns; i++) {
2411                 memset(byte_seq, 0, sizeof(byte_seq));
2412                 if (!mwifiex_is_pattern_supported(&wowlan->patterns[i],
2413                                                   byte_seq,
2414                                                   MWIFIEX_MEF_MAX_BYTESEQ)) {
2415                         wiphy_err(wiphy, "Pattern not supported\n");
2416                         kfree(mef_entry);
2417                         return -EOPNOTSUPP;
2418                 }
2419
2420                 if (!wowlan->patterns[i].pkt_offset) {
2421                         if (!(byte_seq[0] & 0x01) &&
2422                             (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 1)) {
2423                                 mef_cfg.criteria |= MWIFIEX_CRITERIA_UNICAST;
2424                                 continue;
2425                         } else if (is_broadcast_ether_addr(byte_seq)) {
2426                                 mef_cfg.criteria |= MWIFIEX_CRITERIA_BROADCAST;
2427                                 continue;
2428                         } else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
2429                                     (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 2)) ||
2430                                    (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
2431                                     (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 3))) {
2432                                 mef_cfg.criteria |= MWIFIEX_CRITERIA_MULTICAST;
2433                                 continue;
2434                         }
2435                 }
2436
2437                 mef_entry->filter[filt_num].repeat = 1;
2438                 mef_entry->filter[filt_num].offset =
2439                                                 wowlan->patterns[i].pkt_offset;
2440                 memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
2441                        sizeof(byte_seq));
2442                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2443
2444                 if (first_pat)
2445                         first_pat = false;
2446                 else
2447                         mef_entry->filter[filt_num].filt_action = TYPE_AND;
2448
2449                 filt_num++;
2450         }
2451
2452         if (wowlan->magic_pkt) {
2453                 mef_cfg.criteria |= MWIFIEX_CRITERIA_UNICAST;
2454                 mef_entry->filter[filt_num].repeat = 16;
2455                 memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
2456                        ETH_ALEN);
2457                 mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
2458                                                                 ETH_ALEN;
2459                 mef_entry->filter[filt_num].offset = 14;
2460                 mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2461                 if (filt_num)
2462                         mef_entry->filter[filt_num].filt_action = TYPE_OR;
2463         }
2464
2465         if (!mef_cfg.criteria)
2466                 mef_cfg.criteria = MWIFIEX_CRITERIA_BROADCAST |
2467                                    MWIFIEX_CRITERIA_UNICAST |
2468                                    MWIFIEX_CRITERIA_MULTICAST;
2469
2470         ret =  mwifiex_send_cmd_sync(priv, HostCmd_CMD_MEF_CFG,
2471                                      HostCmd_ACT_GEN_SET, 0,
2472                                      &mef_cfg);
2473
2474         kfree(mef_entry);
2475         return ret;
2476 }
2477
2478 static int mwifiex_cfg80211_resume(struct wiphy *wiphy)
2479 {
2480         return 0;
2481 }
2482
2483 static void mwifiex_cfg80211_set_wakeup(struct wiphy *wiphy,
2484                                        bool enabled)
2485 {
2486         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2487
2488         device_set_wakeup_enable(adapter->dev, enabled);
2489 }
2490 #endif
2491
2492 static int mwifiex_get_coalesce_pkt_type(u8 *byte_seq)
2493 {
2494         const u8 ipv4_mc_mac[] = {0x33, 0x33};
2495         const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
2496         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff};
2497
2498         if ((byte_seq[0] & 0x01) &&
2499             (byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 1))
2500                 return PACKET_TYPE_UNICAST;
2501         else if (!memcmp(byte_seq, bc_mac, 4))
2502                 return PACKET_TYPE_BROADCAST;
2503         else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
2504                   byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 2) ||
2505                  (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
2506                   byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 3))
2507                 return PACKET_TYPE_MULTICAST;
2508
2509         return 0;
2510 }
2511
2512 static int
2513 mwifiex_fill_coalesce_rule_info(struct mwifiex_private *priv,
2514                                 struct cfg80211_coalesce_rules *crule,
2515                                 struct mwifiex_coalesce_rule *mrule)
2516 {
2517         u8 byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ + 1];
2518         struct filt_field_param *param;
2519         int i;
2520
2521         mrule->max_coalescing_delay = crule->delay;
2522
2523         param = mrule->params;
2524
2525         for (i = 0; i < crule->n_patterns; i++) {
2526                 memset(byte_seq, 0, sizeof(byte_seq));
2527                 if (!mwifiex_is_pattern_supported(&crule->patterns[i],
2528                                                   byte_seq,
2529                                                 MWIFIEX_COALESCE_MAX_BYTESEQ)) {
2530                         dev_err(priv->adapter->dev, "Pattern not supported\n");
2531                         return -EOPNOTSUPP;
2532                 }
2533
2534                 if (!crule->patterns[i].pkt_offset) {
2535                         u8 pkt_type;
2536
2537                         pkt_type = mwifiex_get_coalesce_pkt_type(byte_seq);
2538                         if (pkt_type && mrule->pkt_type) {
2539                                 dev_err(priv->adapter->dev,
2540                                         "Multiple packet types not allowed\n");
2541                                 return -EOPNOTSUPP;
2542                         } else if (pkt_type) {
2543                                 mrule->pkt_type = pkt_type;
2544                                 continue;
2545                         }
2546                 }
2547
2548                 if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
2549                         param->operation = RECV_FILTER_MATCH_TYPE_EQ;
2550                 else
2551                         param->operation = RECV_FILTER_MATCH_TYPE_NE;
2552
2553                 param->operand_len = byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ];
2554                 memcpy(param->operand_byte_stream, byte_seq,
2555                        param->operand_len);
2556                 param->offset = crule->patterns[i].pkt_offset;
2557                 param++;
2558
2559                 mrule->num_of_fields++;
2560         }
2561
2562         if (!mrule->pkt_type) {
2563                 dev_err(priv->adapter->dev,
2564                         "Packet type can not be determined\n");
2565                 return -EOPNOTSUPP;
2566         }
2567
2568         return 0;
2569 }
2570
2571 static int mwifiex_cfg80211_set_coalesce(struct wiphy *wiphy,
2572                                          struct cfg80211_coalesce *coalesce)
2573 {
2574         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2575         int i, ret;
2576         struct mwifiex_ds_coalesce_cfg coalesce_cfg;
2577         struct mwifiex_private *priv =
2578                         mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
2579
2580         memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
2581         if (!coalesce) {
2582                 dev_dbg(adapter->dev,
2583                         "Disable coalesce and reset all previous rules\n");
2584                 return mwifiex_send_cmd_sync(priv, HostCmd_CMD_COALESCE_CFG,
2585                                              HostCmd_ACT_GEN_SET, 0,
2586                                              &coalesce_cfg);
2587         }
2588
2589         coalesce_cfg.num_of_rules = coalesce->n_rules;
2590         for (i = 0; i < coalesce->n_rules; i++) {
2591                 ret = mwifiex_fill_coalesce_rule_info(priv, &coalesce->rules[i],
2592                                                       &coalesce_cfg.rule[i]);
2593                 if (ret) {
2594                         dev_err(priv->adapter->dev,
2595                                 "Recheck the patterns provided for rule %d\n",
2596                                 i + 1);
2597                         return ret;
2598                 }
2599         }
2600
2601         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_COALESCE_CFG,
2602                                      HostCmd_ACT_GEN_SET, 0, &coalesce_cfg);
2603 }
2604
2605 /* station cfg80211 operations */
2606 static struct cfg80211_ops mwifiex_cfg80211_ops = {
2607         .add_virtual_intf = mwifiex_add_virtual_intf,
2608         .del_virtual_intf = mwifiex_del_virtual_intf,
2609         .change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
2610         .scan = mwifiex_cfg80211_scan,
2611         .connect = mwifiex_cfg80211_connect,
2612         .disconnect = mwifiex_cfg80211_disconnect,
2613         .get_station = mwifiex_cfg80211_get_station,
2614         .dump_station = mwifiex_cfg80211_dump_station,
2615         .set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
2616         .join_ibss = mwifiex_cfg80211_join_ibss,
2617         .leave_ibss = mwifiex_cfg80211_leave_ibss,
2618         .add_key = mwifiex_cfg80211_add_key,
2619         .del_key = mwifiex_cfg80211_del_key,
2620         .mgmt_tx = mwifiex_cfg80211_mgmt_tx,
2621         .mgmt_frame_register = mwifiex_cfg80211_mgmt_frame_register,
2622         .remain_on_channel = mwifiex_cfg80211_remain_on_channel,
2623         .cancel_remain_on_channel = mwifiex_cfg80211_cancel_remain_on_channel,
2624         .set_default_key = mwifiex_cfg80211_set_default_key,
2625         .set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
2626         .set_tx_power = mwifiex_cfg80211_set_tx_power,
2627         .set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
2628         .start_ap = mwifiex_cfg80211_start_ap,
2629         .stop_ap = mwifiex_cfg80211_stop_ap,
2630         .change_beacon = mwifiex_cfg80211_change_beacon,
2631         .set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
2632         .set_antenna = mwifiex_cfg80211_set_antenna,
2633         .del_station = mwifiex_cfg80211_del_station,
2634 #ifdef CONFIG_PM
2635         .suspend = mwifiex_cfg80211_suspend,
2636         .resume = mwifiex_cfg80211_resume,
2637         .set_wakeup = mwifiex_cfg80211_set_wakeup,
2638 #endif
2639         .set_coalesce = mwifiex_cfg80211_set_coalesce,
2640 };
2641
2642 #ifdef CONFIG_PM
2643 static const struct wiphy_wowlan_support mwifiex_wowlan_support = {
2644         .flags = WIPHY_WOWLAN_MAGIC_PKT,
2645         .n_patterns = MWIFIEX_MEF_MAX_FILTERS,
2646         .pattern_min_len = 1,
2647         .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
2648         .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
2649 };
2650 #endif
2651
2652 static bool mwifiex_is_valid_alpha2(const char *alpha2)
2653 {
2654         if (!alpha2 || strlen(alpha2) != 2)
2655                 return false;
2656
2657         if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
2658                 return true;
2659
2660         return false;
2661 }
2662
2663 static const struct wiphy_coalesce_support mwifiex_coalesce_support = {
2664         .n_rules = MWIFIEX_COALESCE_MAX_RULES,
2665         .max_delay = MWIFIEX_MAX_COALESCING_DELAY,
2666         .n_patterns = MWIFIEX_COALESCE_MAX_FILTERS,
2667         .pattern_min_len = 1,
2668         .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
2669         .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
2670 };
2671
2672 /*
2673  * This function registers the device with CFG802.11 subsystem.
2674  *
2675  * The function creates the wireless device/wiphy, populates it with
2676  * default parameters and handler function pointers, and finally
2677  * registers the device.
2678  */
2679
2680 int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
2681 {
2682         int ret;
2683         void *wdev_priv;
2684         struct wiphy *wiphy;
2685         struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
2686         u8 *country_code;
2687
2688         /* create a new wiphy for use with cfg80211 */
2689         wiphy = wiphy_new(&mwifiex_cfg80211_ops,
2690                           sizeof(struct mwifiex_adapter *));
2691         if (!wiphy) {
2692                 dev_err(adapter->dev, "%s: creating new wiphy\n", __func__);
2693                 return -ENOMEM;
2694         }
2695         wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
2696         wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
2697         wiphy->mgmt_stypes = mwifiex_mgmt_stypes;
2698         wiphy->max_remain_on_channel_duration = 5000;
2699         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2700                                  BIT(NL80211_IFTYPE_ADHOC) |
2701                                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
2702                                  BIT(NL80211_IFTYPE_P2P_GO) |
2703                                  BIT(NL80211_IFTYPE_AP);
2704
2705         wiphy->bands[IEEE80211_BAND_2GHZ] = &mwifiex_band_2ghz;
2706         if (adapter->config_bands & BAND_A)
2707                 wiphy->bands[IEEE80211_BAND_5GHZ] = &mwifiex_band_5ghz;
2708         else
2709                 wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
2710
2711         wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
2712         wiphy->n_iface_combinations = 1;
2713
2714         /* Initialize cipher suits */
2715         wiphy->cipher_suites = mwifiex_cipher_suites;
2716         wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
2717
2718         memcpy(wiphy->perm_addr, priv->curr_addr, ETH_ALEN);
2719         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2720         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
2721                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
2722                         WIPHY_FLAG_AP_UAPSD |
2723                         WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
2724         wiphy->regulatory_flags |=
2725                         REGULATORY_CUSTOM_REG |
2726                         REGULATORY_STRICT_REG;
2727
2728         wiphy_apply_custom_regulatory(wiphy, &mwifiex_world_regdom_custom);
2729
2730 #ifdef CONFIG_PM
2731         wiphy->wowlan = &mwifiex_wowlan_support;
2732 #endif
2733
2734         wiphy->coalesce = &mwifiex_coalesce_support;
2735
2736         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
2737                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
2738                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
2739
2740         wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
2741         wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
2742
2743         wiphy->features |= NL80211_FEATURE_HT_IBSS |
2744                            NL80211_FEATURE_INACTIVITY_TIMER |
2745                            NL80211_FEATURE_LOW_PRIORITY_SCAN;
2746
2747         /* Reserve space for mwifiex specific private data for BSS */
2748         wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
2749
2750         wiphy->reg_notifier = mwifiex_reg_notifier;
2751
2752         /* Set struct mwifiex_adapter pointer in wiphy_priv */
2753         wdev_priv = wiphy_priv(wiphy);
2754         *(unsigned long *)wdev_priv = (unsigned long)adapter;
2755
2756         set_wiphy_dev(wiphy, priv->adapter->dev);
2757
2758         ret = wiphy_register(wiphy);
2759         if (ret < 0) {
2760                 dev_err(adapter->dev,
2761                         "%s: wiphy_register failed: %d\n", __func__, ret);
2762                 wiphy_free(wiphy);
2763                 return ret;
2764         }
2765
2766         if (reg_alpha2 && mwifiex_is_valid_alpha2(reg_alpha2)) {
2767                 wiphy_info(wiphy, "driver hint alpha2: %2.2s\n", reg_alpha2);
2768                 regulatory_hint(wiphy, reg_alpha2);
2769         } else {
2770                 country_code = mwifiex_11d_code_2_region(adapter->region_code);
2771                 if (country_code)
2772                         wiphy_info(wiphy, "ignoring F/W country code %2.2s\n",
2773                                    country_code);
2774         }
2775
2776         adapter->wiphy = wiphy;
2777         return ret;
2778 }