GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / rtl8723bs / os_dep / ioctl_cfg80211.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7
8 #include <linux/etherdevice.h>
9 #include <drv_types.h>
10 #include <rtw_debug.h>
11 #include <linux/jiffies.h>
12
13 #include <rtw_wifi_regd.h>
14
15 #define RTW_MAX_MGMT_TX_CNT (8)
16
17 #define RTW_SCAN_IE_LEN_MAX      2304
18 #define RTW_MAX_REMAIN_ON_CHANNEL_DURATION 5000 /* ms */
19 #define RTW_MAX_NUM_PMKIDS 4
20
21 static const u32 rtw_cipher_suites[] = {
22         WLAN_CIPHER_SUITE_WEP40,
23         WLAN_CIPHER_SUITE_WEP104,
24         WLAN_CIPHER_SUITE_TKIP,
25         WLAN_CIPHER_SUITE_CCMP,
26         WLAN_CIPHER_SUITE_AES_CMAC,
27 };
28
29 #define RATETAB_ENT(_rate, _rateid, _flags) \
30         {                                                               \
31                 .bitrate        = (_rate),                              \
32                 .hw_value       = (_rateid),                            \
33                 .flags          = (_flags),                             \
34         }
35
36 #define CHAN2G(_channel, _freq, _flags) {                       \
37         .band                   = NL80211_BAND_2GHZ,            \
38         .center_freq            = (_freq),                      \
39         .hw_value               = (_channel),                   \
40         .flags                  = (_flags),                     \
41         .max_antenna_gain       = 0,                            \
42         .max_power              = 30,                           \
43 }
44
45 /* if wowlan is not supported, kernel generate a disconnect at each suspend
46  * cf: /net/wireless/sysfs.c, so register a stub wowlan.
47  * Moreover wowlan has to be enabled via a the nl80211_set_wowlan callback.
48  * (from user space, e.g. iw phy0 wowlan enable)
49  */
50 static __maybe_unused const struct wiphy_wowlan_support wowlan_stub = {
51         .flags = WIPHY_WOWLAN_ANY,
52         .n_patterns = 0,
53         .pattern_max_len = 0,
54         .pattern_min_len = 0,
55         .max_pkt_offset = 0,
56 };
57
58 static struct ieee80211_rate rtw_rates[] = {
59         RATETAB_ENT(10,  0x1,   0),
60         RATETAB_ENT(20,  0x2,   0),
61         RATETAB_ENT(55,  0x4,   0),
62         RATETAB_ENT(110, 0x8,   0),
63         RATETAB_ENT(60,  0x10,  0),
64         RATETAB_ENT(90,  0x20,  0),
65         RATETAB_ENT(120, 0x40,  0),
66         RATETAB_ENT(180, 0x80,  0),
67         RATETAB_ENT(240, 0x100, 0),
68         RATETAB_ENT(360, 0x200, 0),
69         RATETAB_ENT(480, 0x400, 0),
70         RATETAB_ENT(540, 0x800, 0),
71 };
72
73 #define rtw_g_rates             (rtw_rates + 0)
74 #define RTW_G_RATES_NUM 12
75
76 #define RTW_2G_CHANNELS_NUM 14
77
78 static struct ieee80211_channel rtw_2ghz_channels[] = {
79         CHAN2G(1, 2412, 0),
80         CHAN2G(2, 2417, 0),
81         CHAN2G(3, 2422, 0),
82         CHAN2G(4, 2427, 0),
83         CHAN2G(5, 2432, 0),
84         CHAN2G(6, 2437, 0),
85         CHAN2G(7, 2442, 0),
86         CHAN2G(8, 2447, 0),
87         CHAN2G(9, 2452, 0),
88         CHAN2G(10, 2457, 0),
89         CHAN2G(11, 2462, 0),
90         CHAN2G(12, 2467, 0),
91         CHAN2G(13, 2472, 0),
92         CHAN2G(14, 2484, 0),
93 };
94
95 static void rtw_2g_channels_init(struct ieee80211_channel *channels)
96 {
97         memcpy((void *)channels, (void *)rtw_2ghz_channels,
98                 sizeof(struct ieee80211_channel)*RTW_2G_CHANNELS_NUM
99         );
100 }
101
102 static void rtw_2g_rates_init(struct ieee80211_rate *rates)
103 {
104         memcpy(rates, rtw_g_rates,
105                 sizeof(struct ieee80211_rate)*RTW_G_RATES_NUM
106         );
107 }
108
109 static struct ieee80211_supported_band *rtw_spt_band_alloc(
110         enum nl80211_band band
111         )
112 {
113         struct ieee80211_supported_band *spt_band = NULL;
114         int n_channels, n_bitrates;
115
116         if (band == NL80211_BAND_2GHZ) {
117                 n_channels = RTW_2G_CHANNELS_NUM;
118                 n_bitrates = RTW_G_RATES_NUM;
119         } else {
120                 goto exit;
121         }
122
123         spt_band = rtw_zmalloc(sizeof(struct ieee80211_supported_band) +
124                                sizeof(struct ieee80211_channel) * n_channels +
125                                sizeof(struct ieee80211_rate) * n_bitrates);
126         if (!spt_band)
127                 goto exit;
128
129         spt_band->channels = (struct ieee80211_channel *)(((u8 *)spt_band)+sizeof(struct ieee80211_supported_band));
130         spt_band->bitrates = (struct ieee80211_rate *)(((u8 *)spt_band->channels)+sizeof(struct ieee80211_channel)*n_channels);
131         spt_band->band = band;
132         spt_band->n_channels = n_channels;
133         spt_band->n_bitrates = n_bitrates;
134
135         if (band == NL80211_BAND_2GHZ) {
136                 rtw_2g_channels_init(spt_band->channels);
137                 rtw_2g_rates_init(spt_band->bitrates);
138         }
139
140         /* spt_band.ht_cap */
141
142 exit:
143
144         return spt_band;
145 }
146
147 static const struct ieee80211_txrx_stypes
148 rtw_cfg80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
149         [NL80211_IFTYPE_ADHOC] = {
150                 .tx = 0xffff,
151                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4)
152         },
153         [NL80211_IFTYPE_STATION] = {
154                 .tx = 0xffff,
155                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
156                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
157         },
158         [NL80211_IFTYPE_AP] = {
159                 .tx = 0xffff,
160                 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
161                 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
162                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
163                 BIT(IEEE80211_STYPE_DISASSOC >> 4) |
164                 BIT(IEEE80211_STYPE_AUTH >> 4) |
165                 BIT(IEEE80211_STYPE_DEAUTH >> 4) |
166                 BIT(IEEE80211_STYPE_ACTION >> 4)
167         },
168         [NL80211_IFTYPE_AP_VLAN] = {
169                 /* copy AP */
170                 .tx = 0xffff,
171                 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
172                 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
173                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
174                 BIT(IEEE80211_STYPE_DISASSOC >> 4) |
175                 BIT(IEEE80211_STYPE_AUTH >> 4) |
176                 BIT(IEEE80211_STYPE_DEAUTH >> 4) |
177                 BIT(IEEE80211_STYPE_ACTION >> 4)
178         },
179         [NL80211_IFTYPE_P2P_CLIENT] = {
180                 .tx = 0xffff,
181                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
182                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
183         },
184         [NL80211_IFTYPE_P2P_GO] = {
185                 .tx = 0xffff,
186                 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
187                 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
188                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
189                 BIT(IEEE80211_STYPE_DISASSOC >> 4) |
190                 BIT(IEEE80211_STYPE_AUTH >> 4) |
191                 BIT(IEEE80211_STYPE_DEAUTH >> 4) |
192                 BIT(IEEE80211_STYPE_ACTION >> 4)
193         },
194 };
195
196 static int rtw_ieee80211_channel_to_frequency(int chan, int band)
197 {
198         if (band == NL80211_BAND_2GHZ) {
199                 if (chan == 14)
200                         return 2484;
201              else if (chan < 14)
202                         return 2407 + chan * 5;
203         }
204
205         return 0; /* not supported */
206 }
207
208 #define MAX_BSSINFO_LEN 1000
209 struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wlan_network *pnetwork)
210 {
211         struct ieee80211_channel *notify_channel;
212         struct cfg80211_bss *bss = NULL;
213         /* struct ieee80211_supported_band *band; */
214         u16 channel;
215         u32 freq;
216         u64 notify_timestamp;
217         s32 notify_signal;
218         u8 *buf = NULL, *pbuf;
219         size_t len, bssinf_len = 0;
220         struct ieee80211_hdr *pwlanhdr;
221         __le16 *fctrl;
222
223         struct wireless_dev *wdev = padapter->rtw_wdev;
224         struct wiphy *wiphy = wdev->wiphy;
225         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
226
227         bssinf_len = pnetwork->network.ie_length + sizeof(struct ieee80211_hdr_3addr);
228         if (bssinf_len > MAX_BSSINFO_LEN)
229                 goto exit;
230
231         {
232                 u16 wapi_len = 0;
233
234                 if (rtw_get_wapi_ie(pnetwork->network.ies, pnetwork->network.ie_length, NULL, &wapi_len) > 0) {
235                         if (wapi_len > 0)
236                                 goto exit;
237                 }
238         }
239
240         /* To reduce PBC Overlap rate */
241         /* spin_lock_bh(&pwdev_priv->scan_req_lock); */
242         if (adapter_wdev_data(padapter)->scan_request) {
243                 u8 *psr = NULL, sr = 0;
244                 struct ndis_802_11_ssid *pssid = &pnetwork->network.ssid;
245                 struct cfg80211_scan_request *request = adapter_wdev_data(padapter)->scan_request;
246                 struct cfg80211_ssid *ssids = request->ssids;
247                 u32 wpsielen = 0;
248                 u8 *wpsie = NULL;
249
250                 wpsie = rtw_get_wps_ie(pnetwork->network.ies+_FIXED_IE_LENGTH_, pnetwork->network.ie_length-_FIXED_IE_LENGTH_, NULL, &wpsielen);
251
252                 if (wpsie && wpsielen > 0)
253                         psr = rtw_get_wps_attr_content(wpsie,  wpsielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
254
255                 if (sr != 0) {
256                         /* it means under processing WPS */
257                         if (request->n_ssids == 1 && request->n_channels == 1) {
258                                 if (ssids[0].ssid_len != 0 &&
259                                     (pssid->ssid_length != ssids[0].ssid_len ||
260                                      memcmp(pssid->ssid, ssids[0].ssid, ssids[0].ssid_len))) {
261                                         if (psr)
262                                                 *psr = 0; /* clear sr */
263                                 }
264                         }
265                 }
266         }
267         /* spin_unlock_bh(&pwdev_priv->scan_req_lock); */
268
269
270         channel = pnetwork->network.configuration.ds_config;
271         freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
272
273         notify_channel = ieee80211_get_channel(wiphy, freq);
274
275         notify_timestamp = ktime_to_us(ktime_get_boottime());
276
277         /* We've set wiphy's signal_type as CFG80211_SIGNAL_TYPE_MBM: signal strength in mBm (100*dBm) */
278         if (check_fwstate(pmlmepriv, _FW_LINKED) == true &&
279                 is_same_network(&pmlmepriv->cur_network.network, &pnetwork->network, 0)) {
280                 notify_signal = 100*translate_percentage_to_dbm(padapter->recvpriv.signal_strength);/* dbm */
281         } else {
282                 notify_signal = 100*translate_percentage_to_dbm(pnetwork->network.phy_info.signal_strength);/* dbm */
283         }
284
285         buf = kzalloc(MAX_BSSINFO_LEN, GFP_ATOMIC);
286         if (!buf)
287                 goto exit;
288         pbuf = buf;
289
290         pwlanhdr = (struct ieee80211_hdr *)pbuf;
291         fctrl = &(pwlanhdr->frame_control);
292         *(fctrl) = 0;
293
294         SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
295         /* pmlmeext->mgnt_seq++; */
296
297         if (pnetwork->network.reserved[0] == 1) { /*  WIFI_BEACON */
298                 eth_broadcast_addr(pwlanhdr->addr1);
299                 SetFrameSubType(pbuf, WIFI_BEACON);
300         } else {
301                 memcpy(pwlanhdr->addr1, myid(&(padapter->eeprompriv)), ETH_ALEN);
302                 SetFrameSubType(pbuf, WIFI_PROBERSP);
303         }
304
305         memcpy(pwlanhdr->addr2, pnetwork->network.mac_address, ETH_ALEN);
306         memcpy(pwlanhdr->addr3, pnetwork->network.mac_address, ETH_ALEN);
307
308
309         pbuf += sizeof(struct ieee80211_hdr_3addr);
310         len = sizeof(struct ieee80211_hdr_3addr);
311
312         memcpy(pbuf, pnetwork->network.ies, pnetwork->network.ie_length);
313         len += pnetwork->network.ie_length;
314
315         *((__le64 *)pbuf) = cpu_to_le64(notify_timestamp);
316
317         bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)buf,
318                 len, notify_signal, GFP_ATOMIC);
319
320         if (unlikely(!bss))
321                 goto exit;
322
323         cfg80211_put_bss(wiphy, bss);
324         kfree(buf);
325
326 exit:
327         return bss;
328
329 }
330
331 /*
332         Check the given bss is valid by kernel API cfg80211_get_bss()
333         @padapter : the given adapter
334
335         return true if bss is valid,  false for not found.
336 */
337 int rtw_cfg80211_check_bss(struct adapter *padapter)
338 {
339         struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
340         struct cfg80211_bss *bss = NULL;
341         struct ieee80211_channel *notify_channel = NULL;
342         u32 freq;
343
344         if (!(pnetwork) || !(padapter->rtw_wdev))
345                 return false;
346
347         freq = rtw_ieee80211_channel_to_frequency(pnetwork->configuration.ds_config, NL80211_BAND_2GHZ);
348
349         notify_channel = ieee80211_get_channel(padapter->rtw_wdev->wiphy, freq);
350         bss = cfg80211_get_bss(padapter->rtw_wdev->wiphy, notify_channel,
351                         pnetwork->mac_address, pnetwork->ssid.ssid,
352                         pnetwork->ssid.ssid_length,
353                         WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
354
355         cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss);
356
357         return  (bss != NULL);
358 }
359
360 void rtw_cfg80211_ibss_indicate_connect(struct adapter *padapter)
361 {
362         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
363         struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
364         struct wireless_dev *pwdev = padapter->rtw_wdev;
365         struct wiphy *wiphy = pwdev->wiphy;
366         int freq = (int)cur_network->network.configuration.ds_config;
367         struct ieee80211_channel *chan;
368
369         if (pwdev->iftype != NL80211_IFTYPE_ADHOC) {
370                 return;
371         }
372
373         if (!rtw_cfg80211_check_bss(padapter)) {
374                 struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
375                 struct wlan_network *scanned = pmlmepriv->cur_network_scanned;
376
377                 if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) {
378
379                         memcpy(&cur_network->network, pnetwork, sizeof(struct wlan_bssid_ex));
380                         rtw_cfg80211_inform_bss(padapter, cur_network);
381                 } else {
382                         if (!scanned) {
383                                 rtw_warn_on(1);
384                                 return;
385                         }
386                         if (!memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid))
387                                 && !memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS))
388                         )
389                                 rtw_cfg80211_inform_bss(padapter, scanned);
390                         else
391                                 rtw_warn_on(1);
392                 }
393
394                 if (!rtw_cfg80211_check_bss(padapter))
395                         netdev_dbg(padapter->pnetdev,
396                                    FUNC_ADPT_FMT " BSS not found !!\n",
397                                    FUNC_ADPT_ARG(padapter));
398         }
399         /* notify cfg80211 that device joined an IBSS */
400         chan = ieee80211_get_channel(wiphy, freq);
401         cfg80211_ibss_joined(padapter->pnetdev, cur_network->network.mac_address, chan, GFP_ATOMIC);
402 }
403
404 void rtw_cfg80211_indicate_connect(struct adapter *padapter)
405 {
406         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
407         struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
408         struct wireless_dev *pwdev = padapter->rtw_wdev;
409
410         if (pwdev->iftype != NL80211_IFTYPE_STATION
411                 && pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT
412         ) {
413                 return;
414         }
415
416         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
417                 return;
418
419         {
420                 struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
421                 struct wlan_network *scanned = pmlmepriv->cur_network_scanned;
422
423                 if (!scanned) {
424                         rtw_warn_on(1);
425                         goto check_bss;
426                 }
427
428                 if (!memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS))
429                         && !memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid))
430                 )
431                         rtw_cfg80211_inform_bss(padapter, scanned);
432                 else
433                         rtw_warn_on(1);
434         }
435
436 check_bss:
437         if (!rtw_cfg80211_check_bss(padapter))
438                 netdev_dbg(padapter->pnetdev,
439                            FUNC_ADPT_FMT " BSS not found !!\n",
440                            FUNC_ADPT_ARG(padapter));
441
442         if (rtw_to_roam(padapter) > 0) {
443                 struct wiphy *wiphy = pwdev->wiphy;
444                 struct ieee80211_channel *notify_channel;
445                 u32 freq;
446                 u16 channel = cur_network->network.configuration.ds_config;
447                 struct cfg80211_roam_info roam_info = {};
448
449                 freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
450
451                 notify_channel = ieee80211_get_channel(wiphy, freq);
452
453                 roam_info.channel = notify_channel;
454                 roam_info.bssid = cur_network->network.mac_address;
455                 roam_info.req_ie =
456                         pmlmepriv->assoc_req+sizeof(struct ieee80211_hdr_3addr)+2;
457                 roam_info.req_ie_len =
458                         pmlmepriv->assoc_req_len-sizeof(struct ieee80211_hdr_3addr)-2;
459                 roam_info.resp_ie =
460                         pmlmepriv->assoc_rsp+sizeof(struct ieee80211_hdr_3addr)+6;
461                 roam_info.resp_ie_len =
462                         pmlmepriv->assoc_rsp_len-sizeof(struct ieee80211_hdr_3addr)-6;
463                 cfg80211_roamed(padapter->pnetdev, &roam_info, GFP_ATOMIC);
464         } else {
465                 cfg80211_connect_result(padapter->pnetdev, cur_network->network.mac_address
466                         , pmlmepriv->assoc_req+sizeof(struct ieee80211_hdr_3addr)+2
467                         , pmlmepriv->assoc_req_len-sizeof(struct ieee80211_hdr_3addr)-2
468                         , pmlmepriv->assoc_rsp+sizeof(struct ieee80211_hdr_3addr)+6
469                         , pmlmepriv->assoc_rsp_len-sizeof(struct ieee80211_hdr_3addr)-6
470                         , WLAN_STATUS_SUCCESS, GFP_ATOMIC);
471         }
472 }
473
474 void rtw_cfg80211_indicate_disconnect(struct adapter *padapter)
475 {
476         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
477         struct wireless_dev *pwdev = padapter->rtw_wdev;
478
479         if (pwdev->iftype != NL80211_IFTYPE_STATION
480                 && pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT
481         ) {
482                 return;
483         }
484
485         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
486                 return;
487
488         if (!padapter->mlmepriv.not_indic_disco) {
489                 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
490                         cfg80211_disconnected(padapter->pnetdev, 0,
491                                               NULL, 0, true, GFP_ATOMIC);
492                 } else {
493                         cfg80211_connect_result(padapter->pnetdev, NULL, NULL, 0, NULL, 0,
494                                 WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_ATOMIC/*GFP_KERNEL*/);
495                 }
496         }
497 }
498
499
500 static int rtw_cfg80211_ap_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len)
501 {
502         int ret = 0;
503         u32 wep_key_idx, wep_key_len;
504         struct sta_info *psta = NULL, *pbcmc_sta = NULL;
505         struct adapter *padapter = rtw_netdev_priv(dev);
506         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
507         struct security_priv *psecuritypriv =  &(padapter->securitypriv);
508         struct sta_priv *pstapriv = &padapter->stapriv;
509         char *grpkey = padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey;
510         char *txkey = padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey;
511         char *rxkey = padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey;
512
513         param->u.crypt.err = 0;
514         param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
515
516         if (param_len !=  sizeof(struct ieee_param) + param->u.crypt.key_len) {
517                 ret =  -EINVAL;
518                 goto exit;
519         }
520
521         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
522             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
523             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
524                 if (param->u.crypt.idx >= WEP_KEYS) {
525                         ret = -EINVAL;
526                         goto exit;
527                 }
528         } else {
529                 psta = rtw_get_stainfo(pstapriv, param->sta_addr);
530                 if (!psta)
531                         /* ret = -EINVAL; */
532                         goto exit;
533         }
534
535         if (strcmp(param->u.crypt.alg, "none") == 0 && !psta)
536                 goto exit;
537
538         if (strcmp(param->u.crypt.alg, "WEP") == 0 && !psta) {
539                 wep_key_idx = param->u.crypt.idx;
540                 wep_key_len = param->u.crypt.key_len;
541
542                 if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) {
543                         ret = -EINVAL;
544                         goto exit;
545                 }
546
547                 if (wep_key_len > 0) {
548                         wep_key_len = wep_key_len <= 5 ? 5 : 13;
549                 }
550
551                 if (psecuritypriv->bWepDefaultKeyIdxSet == 0) {
552                         /* wep default key has not been set, so use this key index as default key. */
553
554                         psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
555                         psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
556                         psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
557                         psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
558
559                         if (wep_key_len == 13) {
560                                 psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
561                                 psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
562                         }
563
564                         psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx;
565                 }
566
567                 memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len);
568
569                 psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len;
570
571                 rtw_ap_set_wep_key(padapter, param->u.crypt.key, wep_key_len, wep_key_idx, 1);
572
573                 goto exit;
574
575         }
576
577         /* group key */
578         if (!psta && check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
579                 /* group key */
580                 if (param->u.crypt.set_tx == 0) {
581                         if (strcmp(param->u.crypt.alg, "WEP") == 0) {
582                                 memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
583
584                                 psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
585                                 if (param->u.crypt.key_len == 13) {
586                                                 psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
587                                 }
588
589                         } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
590                                 psecuritypriv->dot118021XGrpPrivacy = _TKIP_;
591
592                                 memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
593
594                                 /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */
595                                 /* set mic key */
596                                 memcpy(txkey, &(param->u.crypt.key[16]), 8);
597                                 memcpy(rxkey, &(param->u.crypt.key[24]), 8);
598
599                                 psecuritypriv->busetkipkey = true;
600
601                         } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
602                                 psecuritypriv->dot118021XGrpPrivacy = _AES_;
603
604                                 memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
605                         } else {
606                                 psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
607                         }
608
609                         psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx;
610
611                         psecuritypriv->binstallGrpkey = true;
612
613                         psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/*  */
614
615                         rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx);
616
617                         pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
618                         if (pbcmc_sta) {
619                                 pbcmc_sta->ieee8021x_blocked = false;
620                                 pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */
621                         }
622
623                 }
624
625                 goto exit;
626
627         }
628
629         if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X && psta) /*  psk/802_1x */
630         {
631                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
632                 {
633                         if (param->u.crypt.set_tx == 1) /* pairwise key */
634                         {
635                                 memcpy(psta->dot118021x_UncstKey.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
636
637                                 if (strcmp(param->u.crypt.alg, "WEP") == 0)
638                                 {
639                                         psta->dot118021XPrivacy = _WEP40_;
640                                         if (param->u.crypt.key_len == 13)
641                                         {
642                                                 psta->dot118021XPrivacy = _WEP104_;
643                                         }
644                                 }
645                                 else if (strcmp(param->u.crypt.alg, "TKIP") == 0)
646                                 {
647                                         psta->dot118021XPrivacy = _TKIP_;
648
649                                         /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */
650                                         /* set mic key */
651                                         memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8);
652                                         memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8);
653
654                                         psecuritypriv->busetkipkey = true;
655
656                                 }
657                                 else if (strcmp(param->u.crypt.alg, "CCMP") == 0)
658                                 {
659
660                                         psta->dot118021XPrivacy = _AES_;
661                                 }
662                                 else
663                                 {
664                                         psta->dot118021XPrivacy = _NO_PRIVACY_;
665                                 }
666
667                                 rtw_ap_set_pairwise_key(padapter, psta);
668
669                                 psta->ieee8021x_blocked = false;
670
671                                 psta->bpairwise_key_installed = true;
672
673                         }
674                         else/* group key??? */
675                         {
676                                 if (strcmp(param->u.crypt.alg, "WEP") == 0)
677                                 {
678                                         memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
679
680                                         psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
681                                         if (param->u.crypt.key_len == 13)
682                                         {
683                                                 psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
684                                         }
685                                 }
686                                 else if (strcmp(param->u.crypt.alg, "TKIP") == 0)
687                                 {
688                                         psecuritypriv->dot118021XGrpPrivacy = _TKIP_;
689
690                                         memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
691
692                                         /* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */
693                                         /* set mic key */
694                                         memcpy(txkey, &(param->u.crypt.key[16]), 8);
695                                         memcpy(rxkey, &(param->u.crypt.key[24]), 8);
696
697                                         psecuritypriv->busetkipkey = true;
698
699                                 }
700                                 else if (strcmp(param->u.crypt.alg, "CCMP") == 0)
701                                 {
702                                         psecuritypriv->dot118021XGrpPrivacy = _AES_;
703
704                                         memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
705                                 }
706                                 else
707                                 {
708                                         psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
709                                 }
710
711                                 psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx;
712
713                                 psecuritypriv->binstallGrpkey = true;
714
715                                 psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/*  */
716
717                                 rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx);
718
719                                 pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
720                                 if (pbcmc_sta)
721                                 {
722                                         pbcmc_sta->ieee8021x_blocked = false;
723                                         pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */
724                                 }
725
726                         }
727
728                 }
729
730         }
731
732 exit:
733
734         return ret;
735
736 }
737
738 static int rtw_cfg80211_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len)
739 {
740         int ret = 0;
741         u32 wep_key_idx, wep_key_len;
742         struct adapter *padapter = rtw_netdev_priv(dev);
743         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
744         struct security_priv *psecuritypriv = &padapter->securitypriv;
745
746         param->u.crypt.err = 0;
747         param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
748
749         if (param_len < (u32) ((u8 *) param->u.crypt.key - (u8 *) param) + param->u.crypt.key_len)
750         {
751                 ret =  -EINVAL;
752                 goto exit;
753         }
754
755         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
756             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
757             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff)
758         {
759                 if (param->u.crypt.idx >= WEP_KEYS
760                         || param->u.crypt.idx >= BIP_MAX_KEYID
761                 )
762                 {
763                         ret = -EINVAL;
764                         goto exit;
765                 }
766         } else {
767                 {
768                 ret = -EINVAL;
769                 goto exit;
770         }
771         }
772
773         if (strcmp(param->u.crypt.alg, "WEP") == 0)
774         {
775                 wep_key_idx = param->u.crypt.idx;
776                 wep_key_len = param->u.crypt.key_len;
777
778                 if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0))
779                 {
780                         ret = -EINVAL;
781                         goto exit;
782                 }
783
784                 if (psecuritypriv->bWepDefaultKeyIdxSet == 0)
785                 {
786                         /* wep default key has not been set, so use this key index as default key. */
787
788                         wep_key_len = wep_key_len <= 5 ? 5 : 13;
789
790                         psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
791                         psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
792                         psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
793
794                         if (wep_key_len == 13)
795                         {
796                                 psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
797                                 psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
798                         }
799
800                         psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx;
801                 }
802
803                 memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len);
804
805                 psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len;
806
807                 rtw_set_key(padapter, psecuritypriv, wep_key_idx, 0, true);
808
809                 goto exit;
810         }
811
812         if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) /*  802_1x */
813         {
814                 struct sta_info *psta, *pbcmc_sta;
815                 struct sta_priv *pstapriv = &padapter->stapriv;
816
817                 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE) == true) /* sta mode */
818                 {
819                         psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv));
820                         if (psta) {
821                                 /* Jeff: don't disable ieee8021x_blocked while clearing key */
822                                 if (strcmp(param->u.crypt.alg, "none") != 0)
823                                         psta->ieee8021x_blocked = false;
824
825
826                                 if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) ||
827                                                 (padapter->securitypriv.ndisencryptstatus ==  Ndis802_11Encryption3Enabled))
828                                 {
829                                         psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
830                                 }
831
832                                 if (param->u.crypt.set_tx == 1)/* pairwise key */
833                                 {
834
835                                         memcpy(psta->dot118021x_UncstKey.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
836
837                                         if (strcmp(param->u.crypt.alg, "TKIP") == 0)/* set mic key */
838                                         {
839                                                 /* DEBUG_ERR(("\nset key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len)); */
840                                                 memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8);
841                                                 memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8);
842
843                                                 padapter->securitypriv.busetkipkey = false;
844                                                 /* _set_timer(&padapter->securitypriv.tkip_timer, 50); */
845                                         }
846
847                                         rtw_setstakey_cmd(padapter, psta, true, true);
848                                 }
849                                 else/* group key */
850                                 {
851                                         if (strcmp(param->u.crypt.alg, "TKIP") == 0 || strcmp(param->u.crypt.alg, "CCMP") == 0)
852                                         {
853                                                 memcpy(padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
854                                                 memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8);
855                                                 memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8);
856                                                 padapter->securitypriv.binstallGrpkey = true;
857
858                                                 padapter->securitypriv.dot118021XGrpKeyid = param->u.crypt.idx;
859                                                 rtw_set_key(padapter, &padapter->securitypriv, param->u.crypt.idx, 1, true);
860                                         }
861                                         else if (strcmp(param->u.crypt.alg, "BIP") == 0)
862                                         {
863                                                 /* save the IGTK key, length 16 bytes */
864                                                 memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
865                                                 /*
866                                                 for (no = 0;no<16;no++)
867                                                         printk(" %02x ", padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey[no]);
868                                                 */
869                                                 padapter->securitypriv.dot11wBIPKeyid = param->u.crypt.idx;
870                                                 padapter->securitypriv.binstallBIPkey = true;
871                                         }
872                                 }
873                         }
874
875                         pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
876                         if (!pbcmc_sta)
877                         {
878                                 /* DEBUG_ERR(("Set OID_802_11_ADD_KEY: bcmc stainfo is null\n")); */
879                         }
880                         else
881                         {
882                                 /* Jeff: don't disable ieee8021x_blocked while clearing key */
883                                 if (strcmp(param->u.crypt.alg, "none") != 0)
884                                         pbcmc_sta->ieee8021x_blocked = false;
885
886                                 if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) ||
887                                                 (padapter->securitypriv.ndisencryptstatus ==  Ndis802_11Encryption3Enabled))
888                                 {
889                                         pbcmc_sta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
890                                 }
891                         }
892                 }
893                 else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) /* adhoc mode */
894                 {
895                 }
896         }
897
898 exit:
899
900         return ret;
901 }
902
903 static int cfg80211_rtw_add_key(struct wiphy *wiphy, struct net_device *ndev,
904                                 u8 key_index, bool pairwise, const u8 *mac_addr,
905                                 struct key_params *params)
906 {
907         char *alg_name;
908         u32 param_len;
909         struct ieee_param *param = NULL;
910         int ret = 0;
911         struct adapter *padapter = rtw_netdev_priv(ndev);
912         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
913
914         param_len = sizeof(struct ieee_param) + params->key_len;
915         param = rtw_malloc(param_len);
916         if (!param)
917                 return -1;
918
919         memset(param, 0, param_len);
920
921         param->cmd = IEEE_CMD_SET_ENCRYPTION;
922         eth_broadcast_addr(param->sta_addr);
923
924         switch (params->cipher) {
925         case IW_AUTH_CIPHER_NONE:
926                 /* todo: remove key */
927                 /* remove = 1; */
928                 alg_name = "none";
929                 break;
930         case WLAN_CIPHER_SUITE_WEP40:
931         case WLAN_CIPHER_SUITE_WEP104:
932                 alg_name = "WEP";
933                 break;
934         case WLAN_CIPHER_SUITE_TKIP:
935                 alg_name = "TKIP";
936                 break;
937         case WLAN_CIPHER_SUITE_CCMP:
938                 alg_name = "CCMP";
939                 break;
940         case WLAN_CIPHER_SUITE_AES_CMAC:
941                 alg_name = "BIP";
942                 break;
943         default:
944                 ret = -ENOTSUPP;
945                 goto addkey_end;
946         }
947
948         strncpy((char *)param->u.crypt.alg, alg_name, IEEE_CRYPT_ALG_NAME_LEN);
949
950
951         if (!mac_addr || is_broadcast_ether_addr(mac_addr))
952         {
953                 param->u.crypt.set_tx = 0; /* for wpa/wpa2 group key */
954         } else {
955                 param->u.crypt.set_tx = 1; /* for wpa/wpa2 pairwise key */
956         }
957
958         param->u.crypt.idx = key_index;
959
960         if (params->seq_len && params->seq)
961         {
962                 memcpy(param->u.crypt.seq, (u8 *)params->seq, params->seq_len);
963         }
964
965         if (params->key_len && params->key)
966         {
967                 param->u.crypt.key_len = params->key_len;
968                 memcpy(param->u.crypt.key, (u8 *)params->key, params->key_len);
969         }
970
971         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)
972         {
973                 ret =  rtw_cfg80211_set_encryption(ndev, param, param_len);
974         }
975         else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
976         {
977                 if (mac_addr)
978                         memcpy(param->sta_addr, (void *)mac_addr, ETH_ALEN);
979
980                 ret = rtw_cfg80211_ap_set_encryption(ndev, param, param_len);
981         }
982         else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true
983                 || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true)
984         {
985                 ret =  rtw_cfg80211_set_encryption(ndev, param, param_len);
986         }
987
988 addkey_end:
989         kfree(param);
990
991         return ret;
992
993 }
994
995 static int cfg80211_rtw_get_key(struct wiphy *wiphy, struct net_device *ndev,
996                                 u8 key_index, bool pairwise, const u8 *mac_addr,
997                                 void *cookie,
998                                 void (*callback)(void *cookie,
999                                                  struct key_params*))
1000 {
1001         return 0;
1002 }
1003
1004 static int cfg80211_rtw_del_key(struct wiphy *wiphy, struct net_device *ndev,
1005                                 u8 key_index, bool pairwise, const u8 *mac_addr)
1006 {
1007         struct adapter *padapter = rtw_netdev_priv(ndev);
1008         struct security_priv *psecuritypriv = &padapter->securitypriv;
1009
1010         if (key_index == psecuritypriv->dot11PrivacyKeyIndex)
1011         {
1012                 /* clear the flag of wep default key set. */
1013                 psecuritypriv->bWepDefaultKeyIdxSet = 0;
1014         }
1015
1016         return 0;
1017 }
1018
1019 static int cfg80211_rtw_set_default_key(struct wiphy *wiphy,
1020         struct net_device *ndev, u8 key_index
1021         , bool unicast, bool multicast
1022         )
1023 {
1024         struct adapter *padapter = rtw_netdev_priv(ndev);
1025         struct security_priv *psecuritypriv = &padapter->securitypriv;
1026
1027         if ((key_index < WEP_KEYS) && ((psecuritypriv->dot11PrivacyAlgrthm == _WEP40_) || (psecuritypriv->dot11PrivacyAlgrthm == _WEP104_))) /* set wep default key */
1028         {
1029                 psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
1030
1031                 psecuritypriv->dot11PrivacyKeyIndex = key_index;
1032
1033                 psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
1034                 psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
1035                 if (psecuritypriv->dot11DefKeylen[key_index] == 13)
1036                 {
1037                         psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
1038                         psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
1039                 }
1040
1041                 psecuritypriv->bWepDefaultKeyIdxSet = 1; /* set the flag to represent that wep default key has been set */
1042         }
1043
1044         return 0;
1045
1046 }
1047
1048 static int cfg80211_rtw_get_station(struct wiphy *wiphy,
1049                                     struct net_device *ndev,
1050                                 const u8 *mac,
1051                                 struct station_info *sinfo)
1052 {
1053         int ret = 0;
1054         struct adapter *padapter = rtw_netdev_priv(ndev);
1055         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1056         struct sta_info *psta = NULL;
1057         struct sta_priv *pstapriv = &padapter->stapriv;
1058
1059         sinfo->filled = 0;
1060
1061         if (!mac) {
1062                 ret = -ENOENT;
1063                 goto exit;
1064         }
1065
1066         psta = rtw_get_stainfo(pstapriv, (u8 *)mac);
1067         if (!psta) {
1068                 ret = -ENOENT;
1069                 goto exit;
1070         }
1071
1072         /* for infra./P2PClient mode */
1073         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
1074                 && check_fwstate(pmlmepriv, _FW_LINKED)
1075         )
1076         {
1077                 struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
1078
1079                 if (memcmp((u8 *)mac, cur_network->network.mac_address, ETH_ALEN)) {
1080                         ret = -ENOENT;
1081                         goto exit;
1082                 }
1083
1084                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1085                 sinfo->signal = translate_percentage_to_dbm(padapter->recvpriv.signal_strength);
1086
1087                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1088                 sinfo->txrate.legacy = rtw_get_cur_max_rate(padapter);
1089
1090                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1091                 sinfo->rx_packets = sta_rx_data_pkts(psta);
1092
1093                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1094                 sinfo->tx_packets = psta->sta_stats.tx_pkts;
1095                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1096         }
1097
1098         /* for Ad-Hoc/AP mode */
1099         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)
1100  || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)
1101  || check_fwstate(pmlmepriv, WIFI_AP_STATE))
1102                 && check_fwstate(pmlmepriv, _FW_LINKED)
1103         )
1104         {
1105                 /* TODO: should acquire station info... */
1106         }
1107
1108 exit:
1109         return ret;
1110 }
1111
1112 static int cfg80211_rtw_change_iface(struct wiphy *wiphy,
1113                                      struct net_device *ndev,
1114                                      enum nl80211_iftype type,
1115                                      struct vif_params *params)
1116 {
1117         enum nl80211_iftype old_type;
1118         enum ndis_802_11_network_infrastructure networkType;
1119         struct adapter *padapter = rtw_netdev_priv(ndev);
1120         struct wireless_dev *rtw_wdev = padapter->rtw_wdev;
1121         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1122         int ret = 0;
1123
1124         if (adapter_to_dvobj(padapter)->processing_dev_remove == true)
1125         {
1126                 ret = -EPERM;
1127                 goto exit;
1128         }
1129
1130         {
1131                 if (netdev_open(ndev) != 0) {
1132                         ret = -EPERM;
1133                         goto exit;
1134                 }
1135         }
1136
1137         if (_FAIL == rtw_pwr_wakeup(padapter)) {
1138                 ret = -EPERM;
1139                 goto exit;
1140         }
1141
1142         old_type = rtw_wdev->iftype;
1143
1144         if (old_type != type)
1145         {
1146                 pmlmeext->action_public_rxseq = 0xffff;
1147                 pmlmeext->action_public_dialog_token = 0xff;
1148         }
1149
1150         switch (type) {
1151         case NL80211_IFTYPE_ADHOC:
1152                 networkType = Ndis802_11IBSS;
1153                 break;
1154         case NL80211_IFTYPE_STATION:
1155                 networkType = Ndis802_11Infrastructure;
1156                 break;
1157         case NL80211_IFTYPE_AP:
1158                 networkType = Ndis802_11APMode;
1159                 break;
1160         default:
1161                 ret = -EOPNOTSUPP;
1162                 goto exit;
1163         }
1164
1165         rtw_wdev->iftype = type;
1166
1167         if (rtw_set_802_11_infrastructure_mode(padapter, networkType) == false)
1168         {
1169                 rtw_wdev->iftype = old_type;
1170                 ret = -EPERM;
1171                 goto exit;
1172         }
1173
1174         rtw_setopmode_cmd(padapter, networkType, true);
1175
1176 exit:
1177
1178         return ret;
1179 }
1180
1181 void rtw_cfg80211_indicate_scan_done(struct adapter *adapter, bool aborted)
1182 {
1183         struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter);
1184         struct cfg80211_scan_info info = {
1185                 .aborted = aborted
1186         };
1187
1188         spin_lock_bh(&pwdev_priv->scan_req_lock);
1189         if (pwdev_priv->scan_request) {
1190                 /* avoid WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); */
1191                 if (pwdev_priv->scan_request->wiphy == pwdev_priv->rtw_wdev->wiphy)
1192                         cfg80211_scan_done(pwdev_priv->scan_request, &info);
1193
1194                 pwdev_priv->scan_request = NULL;
1195         }
1196         spin_unlock_bh(&pwdev_priv->scan_req_lock);
1197 }
1198
1199 void rtw_cfg80211_unlink_bss(struct adapter *padapter, struct wlan_network *pnetwork)
1200 {
1201         struct wireless_dev *pwdev = padapter->rtw_wdev;
1202         struct wiphy *wiphy = pwdev->wiphy;
1203         struct cfg80211_bss *bss = NULL;
1204         struct wlan_bssid_ex *select_network = &pnetwork->network;
1205
1206         bss = cfg80211_get_bss(wiphy, NULL/*notify_channel*/,
1207                 select_network->mac_address, select_network->ssid.ssid,
1208                 select_network->ssid.ssid_length, 0/*WLAN_CAPABILITY_ESS*/,
1209                 0/*WLAN_CAPABILITY_ESS*/);
1210
1211         if (bss) {
1212                 cfg80211_unlink_bss(wiphy, bss);
1213                 cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss);
1214         }
1215 }
1216
1217 void rtw_cfg80211_surveydone_event_callback(struct adapter *padapter)
1218 {
1219         struct list_head                                        *plist, *phead;
1220         struct  mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1221         struct __queue *queue   = &(pmlmepriv->scanned_queue);
1222         struct  wlan_network    *pnetwork = NULL;
1223
1224         spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1225
1226         phead = get_list_head(queue);
1227         list_for_each(plist, phead)
1228         {
1229                 pnetwork = list_entry(plist, struct wlan_network, list);
1230
1231                 /* report network only if the current channel set contains the channel to which this network belongs */
1232                 if (rtw_ch_set_search_ch(padapter->mlmeextpriv.channel_set, pnetwork->network.configuration.ds_config) >= 0
1233                         && true == rtw_validate_ssid(&(pnetwork->network.ssid))
1234                 )
1235                 {
1236                         /* ev =translate_scan(padapter, a, pnetwork, ev, stop); */
1237                         rtw_cfg80211_inform_bss(padapter, pnetwork);
1238                 }
1239
1240         }
1241
1242         spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1243 }
1244
1245 static int rtw_cfg80211_set_probe_req_wpsp2pie(struct adapter *padapter, char *buf, int len)
1246 {
1247         int ret = 0;
1248         uint wps_ielen = 0;
1249         u8 *wps_ie;
1250         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1251
1252         if (len > 0)
1253         {
1254                 wps_ie = rtw_get_wps_ie(buf, len, NULL, &wps_ielen);
1255                 if (wps_ie)
1256                 {
1257                         if (pmlmepriv->wps_probe_req_ie)
1258                         {
1259                                 pmlmepriv->wps_probe_req_ie_len = 0;
1260                                 kfree(pmlmepriv->wps_probe_req_ie);
1261                                 pmlmepriv->wps_probe_req_ie = NULL;
1262                         }
1263
1264                         pmlmepriv->wps_probe_req_ie = rtw_malloc(wps_ielen);
1265                         if (!pmlmepriv->wps_probe_req_ie)
1266                                 return -EINVAL;
1267
1268                         memcpy(pmlmepriv->wps_probe_req_ie, wps_ie, wps_ielen);
1269                         pmlmepriv->wps_probe_req_ie_len = wps_ielen;
1270                 }
1271         }
1272
1273         return ret;
1274
1275 }
1276
1277 static int cfg80211_rtw_scan(struct wiphy *wiphy
1278         , struct cfg80211_scan_request *request)
1279 {
1280         struct net_device *ndev = wdev_to_ndev(request->wdev);
1281         int i;
1282         u8 _status = false;
1283         int ret = 0;
1284         struct ndis_802_11_ssid *ssid = NULL;
1285         struct rtw_ieee80211_channel ch[RTW_CHANNEL_SCAN_AMOUNT];
1286         u8 survey_times = 3;
1287         u8 survey_times_for_one_ch = 6;
1288         struct cfg80211_ssid *ssids = request->ssids;
1289         int j = 0;
1290         bool need_indicate_scan_done = false;
1291
1292         struct adapter *padapter;
1293         struct rtw_wdev_priv *pwdev_priv;
1294         struct mlme_priv *pmlmepriv;
1295
1296         if (!ndev) {
1297                 ret = -EINVAL;
1298                 goto exit;
1299         }
1300
1301         padapter = rtw_netdev_priv(ndev);
1302         pwdev_priv = adapter_wdev_data(padapter);
1303         pmlmepriv = &padapter->mlmepriv;
1304 /* endif */
1305
1306         spin_lock_bh(&pwdev_priv->scan_req_lock);
1307         pwdev_priv->scan_request = request;
1308         spin_unlock_bh(&pwdev_priv->scan_req_lock);
1309
1310         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
1311         {
1312                 if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS|_FW_UNDER_SURVEY|_FW_UNDER_LINKING) == true)
1313                 {
1314                         need_indicate_scan_done = true;
1315                         goto check_need_indicate_scan_done;
1316                 }
1317         }
1318
1319         rtw_ps_deny(padapter, PS_DENY_SCAN);
1320         if (_FAIL == rtw_pwr_wakeup(padapter)) {
1321                 need_indicate_scan_done = true;
1322                 goto check_need_indicate_scan_done;
1323         }
1324
1325         if (request->ie && request->ie_len > 0)
1326                 rtw_cfg80211_set_probe_req_wpsp2pie(padapter, (u8 *)request->ie, request->ie_len);
1327
1328         if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true) {
1329                 need_indicate_scan_done = true;
1330                 goto check_need_indicate_scan_done;
1331         } else if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
1332                 ret = -EBUSY;
1333                 goto check_need_indicate_scan_done;
1334         }
1335
1336         if (pmlmepriv->LinkDetectInfo.bBusyTraffic == true)
1337         {
1338                 static unsigned long lastscantime = 0;
1339                 unsigned long passtime;
1340
1341                 passtime = jiffies_to_msecs(jiffies - lastscantime);
1342                 lastscantime = jiffies;
1343                 if (passtime > 12000)
1344                 {
1345                         need_indicate_scan_done = true;
1346                         goto check_need_indicate_scan_done;
1347                 }
1348         }
1349
1350         if (rtw_is_scan_deny(padapter)) {
1351                 need_indicate_scan_done = true;
1352                 goto check_need_indicate_scan_done;
1353         }
1354
1355         ssid = kzalloc(RTW_SSID_SCAN_AMOUNT * sizeof(struct ndis_802_11_ssid),
1356                        GFP_KERNEL);
1357         if (!ssid) {
1358                 ret = -ENOMEM;
1359                 goto check_need_indicate_scan_done;
1360         }
1361
1362         /* parsing request ssids, n_ssids */
1363         for (i = 0; i < request->n_ssids && i < RTW_SSID_SCAN_AMOUNT; i++) {
1364                 memcpy(ssid[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
1365                 ssid[i].ssid_length = ssids[i].ssid_len;
1366         }
1367
1368         /* parsing channels, n_channels */
1369         memset(ch, 0, sizeof(struct rtw_ieee80211_channel)*RTW_CHANNEL_SCAN_AMOUNT);
1370         for (i = 0; i < request->n_channels && i < RTW_CHANNEL_SCAN_AMOUNT; i++) {
1371                 ch[i].hw_value = request->channels[i]->hw_value;
1372                 ch[i].flags = request->channels[i]->flags;
1373         }
1374
1375         spin_lock_bh(&pmlmepriv->lock);
1376         if (request->n_channels == 1) {
1377                 for (i = 1; i < survey_times_for_one_ch; i++)
1378                         memcpy(&ch[i], &ch[0], sizeof(struct rtw_ieee80211_channel));
1379                 _status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, ch, survey_times_for_one_ch);
1380         } else if (request->n_channels <= 4) {
1381                 for (j = request->n_channels - 1; j >= 0; j--)
1382                         for (i = 0; i < survey_times; i++)
1383                 {
1384                         memcpy(&ch[j*survey_times+i], &ch[j], sizeof(struct rtw_ieee80211_channel));
1385                 }
1386                 _status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, ch, survey_times * request->n_channels);
1387         } else {
1388                 _status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, NULL, 0);
1389         }
1390         spin_unlock_bh(&pmlmepriv->lock);
1391
1392
1393         if (_status == false)
1394         {
1395                 ret = -1;
1396         }
1397
1398 check_need_indicate_scan_done:
1399         kfree(ssid);
1400         if (need_indicate_scan_done)
1401         {
1402                 rtw_cfg80211_surveydone_event_callback(padapter);
1403                 rtw_cfg80211_indicate_scan_done(padapter, false);
1404         }
1405
1406         rtw_ps_deny_cancel(padapter, PS_DENY_SCAN);
1407
1408 exit:
1409         return ret;
1410
1411 }
1412
1413 static int cfg80211_rtw_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1414 {
1415         return 0;
1416 }
1417
1418 static int rtw_cfg80211_set_wpa_version(struct security_priv *psecuritypriv, u32 wpa_version)
1419 {
1420         if (!wpa_version) {
1421                 psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1422                 return 0;
1423         }
1424
1425
1426         if (wpa_version & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2))
1427         {
1428                 psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPAPSK;
1429         }
1430
1431         return 0;
1432
1433 }
1434
1435 static int rtw_cfg80211_set_auth_type(struct security_priv *psecuritypriv,
1436                              enum nl80211_auth_type sme_auth_type)
1437 {
1438         switch (sme_auth_type) {
1439         case NL80211_AUTHTYPE_AUTOMATIC:
1440
1441                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
1442
1443                 break;
1444         case NL80211_AUTHTYPE_OPEN_SYSTEM:
1445
1446                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1447
1448                 if (psecuritypriv->ndisauthtype > Ndis802_11AuthModeWPA)
1449                         psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1450
1451                 break;
1452         case NL80211_AUTHTYPE_SHARED_KEY:
1453
1454                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Shared;
1455
1456                 psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
1457
1458
1459                 break;
1460         default:
1461                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1462                 /* return -ENOTSUPP; */
1463         }
1464
1465         return 0;
1466
1467 }
1468
1469 static int rtw_cfg80211_set_cipher(struct security_priv *psecuritypriv, u32 cipher, bool ucast)
1470 {
1471         u32 ndisencryptstatus = Ndis802_11EncryptionDisabled;
1472
1473         u32 *profile_cipher = ucast ? &psecuritypriv->dot11PrivacyAlgrthm :
1474                 &psecuritypriv->dot118021XGrpPrivacy;
1475
1476
1477         if (!cipher) {
1478                 *profile_cipher = _NO_PRIVACY_;
1479                 psecuritypriv->ndisencryptstatus = ndisencryptstatus;
1480                 return 0;
1481         }
1482
1483         switch (cipher) {
1484         case IW_AUTH_CIPHER_NONE:
1485                 *profile_cipher = _NO_PRIVACY_;
1486                 ndisencryptstatus = Ndis802_11EncryptionDisabled;
1487                 break;
1488         case WLAN_CIPHER_SUITE_WEP40:
1489                 *profile_cipher = _WEP40_;
1490                 ndisencryptstatus = Ndis802_11Encryption1Enabled;
1491                 break;
1492         case WLAN_CIPHER_SUITE_WEP104:
1493                 *profile_cipher = _WEP104_;
1494                 ndisencryptstatus = Ndis802_11Encryption1Enabled;
1495                 break;
1496         case WLAN_CIPHER_SUITE_TKIP:
1497                 *profile_cipher = _TKIP_;
1498                 ndisencryptstatus = Ndis802_11Encryption2Enabled;
1499                 break;
1500         case WLAN_CIPHER_SUITE_CCMP:
1501                 *profile_cipher = _AES_;
1502                 ndisencryptstatus = Ndis802_11Encryption3Enabled;
1503                 break;
1504         default:
1505                 return -ENOTSUPP;
1506         }
1507
1508         if (ucast) {
1509                 psecuritypriv->ndisencryptstatus = ndisencryptstatus;
1510
1511                 /* if (psecuritypriv->dot11PrivacyAlgrthm >= _AES_) */
1512                 /*      psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPA2PSK; */
1513         }
1514
1515         return 0;
1516 }
1517
1518 static int rtw_cfg80211_set_key_mgt(struct security_priv *psecuritypriv, u32 key_mgt)
1519 {
1520         if (key_mgt == WLAN_AKM_SUITE_8021X)
1521                 /* auth_type = UMAC_AUTH_TYPE_8021X; */
1522                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1523         else if (key_mgt == WLAN_AKM_SUITE_PSK) {
1524                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1525         }
1526
1527         return 0;
1528 }
1529
1530 static int rtw_cfg80211_set_wpa_ie(struct adapter *padapter, u8 *pie, size_t ielen)
1531 {
1532         u8 *buf = NULL;
1533         int group_cipher = 0, pairwise_cipher = 0;
1534         int ret = 0;
1535         int wpa_ielen = 0;
1536         int wpa2_ielen = 0;
1537         u8 *pwpa, *pwpa2;
1538         u8 null_addr[] = {0, 0, 0, 0, 0, 0};
1539
1540         if (!pie || !ielen) {
1541                 /* Treat this as normal case, but need to clear WIFI_UNDER_WPS */
1542                 _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1543                 goto exit;
1544         }
1545
1546         if (ielen > MAX_WPA_IE_LEN+MAX_WPS_IE_LEN+MAX_P2P_IE_LEN) {
1547                 ret = -EINVAL;
1548                 goto exit;
1549         }
1550
1551         buf = rtw_zmalloc(ielen);
1552         if (!buf) {
1553                 ret =  -ENOMEM;
1554                 goto exit;
1555         }
1556
1557         memcpy(buf, pie, ielen);
1558
1559         if (ielen < RSN_HEADER_LEN) {
1560                 ret  = -1;
1561                 goto exit;
1562         }
1563
1564         pwpa = rtw_get_wpa_ie(buf, &wpa_ielen, ielen);
1565         if (pwpa && wpa_ielen > 0) {
1566                 if (rtw_parse_wpa_ie(pwpa, wpa_ielen+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1567                         padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1568                         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK;
1569                         memcpy(padapter->securitypriv.supplicant_ie, &pwpa[0], wpa_ielen+2);
1570                 }
1571         }
1572
1573         pwpa2 = rtw_get_wpa2_ie(buf, &wpa2_ielen, ielen);
1574         if (pwpa2 && wpa2_ielen > 0) {
1575                 if (rtw_parse_wpa2_ie(pwpa2, wpa2_ielen+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1576                         padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1577                         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK;
1578                         memcpy(padapter->securitypriv.supplicant_ie, &pwpa2[0], wpa2_ielen+2);
1579                 }
1580         }
1581
1582         if (group_cipher == 0)
1583                 group_cipher = WPA_CIPHER_NONE;
1584
1585         if (pairwise_cipher == 0)
1586                 pairwise_cipher = WPA_CIPHER_NONE;
1587
1588         switch (group_cipher)
1589         {
1590                 case WPA_CIPHER_NONE:
1591                         padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1592                         padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1593                         break;
1594                 case WPA_CIPHER_WEP40:
1595                         padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_;
1596                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1597                         break;
1598                 case WPA_CIPHER_TKIP:
1599                         padapter->securitypriv.dot118021XGrpPrivacy = _TKIP_;
1600                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
1601                         break;
1602                 case WPA_CIPHER_CCMP:
1603                         padapter->securitypriv.dot118021XGrpPrivacy = _AES_;
1604                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
1605                         break;
1606                 case WPA_CIPHER_WEP104:
1607                         padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
1608                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1609                         break;
1610         }
1611
1612         switch (pairwise_cipher)
1613         {
1614                 case WPA_CIPHER_NONE:
1615                         padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1616                         padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1617                         break;
1618                 case WPA_CIPHER_WEP40:
1619                         padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
1620                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1621                         break;
1622                 case WPA_CIPHER_TKIP:
1623                         padapter->securitypriv.dot11PrivacyAlgrthm = _TKIP_;
1624                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
1625                         break;
1626                 case WPA_CIPHER_CCMP:
1627                         padapter->securitypriv.dot11PrivacyAlgrthm = _AES_;
1628                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
1629                         break;
1630                 case WPA_CIPHER_WEP104:
1631                         padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1632                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1633                         break;
1634         }
1635
1636         {/* handle wps_ie */
1637                 uint wps_ielen;
1638                 u8 *wps_ie;
1639
1640                 wps_ie = rtw_get_wps_ie(buf, ielen, NULL, &wps_ielen);
1641                 if (wps_ie && wps_ielen > 0) {
1642                         padapter->securitypriv.wps_ie_len = wps_ielen < MAX_WPS_IE_LEN ? wps_ielen : MAX_WPS_IE_LEN;
1643                         memcpy(padapter->securitypriv.wps_ie, wps_ie, padapter->securitypriv.wps_ie_len);
1644                         set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS);
1645                 } else {
1646                         _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1647                 }
1648         }
1649
1650         /* TKIP and AES disallow multicast packets until installing group key */
1651         if (padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_
1652                 || padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_WTMIC_
1653                 || padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
1654                 /* WPS open need to enable multicast */
1655                 /*  check_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS) == true) */
1656                 rtw_hal_set_hwreg(padapter, HW_VAR_OFF_RCR_AM, null_addr);
1657
1658 exit:
1659         kfree(buf);
1660         if (ret)
1661                 _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1662         return ret;
1663 }
1664
1665 static int cfg80211_rtw_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
1666                                   struct cfg80211_ibss_params *params)
1667 {
1668         struct adapter *padapter = rtw_netdev_priv(ndev);
1669         struct ndis_802_11_ssid ndis_ssid;
1670         struct security_priv *psecuritypriv = &padapter->securitypriv;
1671         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1672         int ret = 0;
1673
1674         if (_FAIL == rtw_pwr_wakeup(padapter)) {
1675                 ret = -EPERM;
1676                 goto exit;
1677         }
1678
1679         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1680                 ret = -EPERM;
1681                 goto exit;
1682         }
1683
1684         if (!params->ssid || !params->ssid_len) {
1685                 ret = -EINVAL;
1686                 goto exit;
1687         }
1688
1689         if (params->ssid_len > IW_ESSID_MAX_SIZE) {
1690
1691                 ret = -E2BIG;
1692                 goto exit;
1693         }
1694
1695         memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1696         ndis_ssid.ssid_length = params->ssid_len;
1697         memcpy(ndis_ssid.ssid, (u8 *)params->ssid, params->ssid_len);
1698
1699         psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled;
1700         psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
1701         psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
1702         psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1703         psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1704
1705         ret = rtw_cfg80211_set_auth_type(psecuritypriv, NL80211_AUTHTYPE_OPEN_SYSTEM);
1706         rtw_set_802_11_authentication_mode(padapter, psecuritypriv->ndisauthtype);
1707
1708         if (rtw_set_802_11_ssid(padapter, &ndis_ssid) == false) {
1709                 ret = -1;
1710                 goto exit;
1711         }
1712
1713 exit:
1714         return ret;
1715 }
1716
1717 static int cfg80211_rtw_leave_ibss(struct wiphy *wiphy, struct net_device *ndev)
1718 {
1719         struct adapter *padapter = rtw_netdev_priv(ndev);
1720         struct wireless_dev *rtw_wdev = padapter->rtw_wdev;
1721         enum nl80211_iftype old_type;
1722         int ret = 0;
1723
1724         old_type = rtw_wdev->iftype;
1725
1726         rtw_set_to_roam(padapter, 0);
1727
1728         if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
1729                 rtw_scan_abort(padapter);
1730                 LeaveAllPowerSaveMode(padapter);
1731
1732                 rtw_wdev->iftype = NL80211_IFTYPE_STATION;
1733
1734                 if (rtw_set_802_11_infrastructure_mode(padapter, Ndis802_11Infrastructure) == false)
1735                 {
1736                         rtw_wdev->iftype = old_type;
1737                         ret = -EPERM;
1738                         goto leave_ibss;
1739                 }
1740                 rtw_setopmode_cmd(padapter, Ndis802_11Infrastructure, true);
1741         }
1742
1743 leave_ibss:
1744         return ret;
1745 }
1746
1747 static int cfg80211_rtw_connect(struct wiphy *wiphy, struct net_device *ndev,
1748                                  struct cfg80211_connect_params *sme)
1749 {
1750         int ret = 0;
1751         enum ndis_802_11_authentication_mode authmode;
1752         struct ndis_802_11_ssid ndis_ssid;
1753         struct adapter *padapter = rtw_netdev_priv(ndev);
1754         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1755         struct security_priv *psecuritypriv = &padapter->securitypriv;
1756
1757         padapter->mlmepriv.not_indic_disco = true;
1758
1759
1760         if (adapter_wdev_data(padapter)->block == true) {
1761                 ret = -EBUSY;
1762                 goto exit;
1763         }
1764
1765         rtw_ps_deny(padapter, PS_DENY_JOIN);
1766         if (_FAIL == rtw_pwr_wakeup(padapter)) {
1767                 ret = -EPERM;
1768                 goto exit;
1769         }
1770
1771         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1772                 ret = -EPERM;
1773                 goto exit;
1774         }
1775
1776         if (!sme->ssid || !sme->ssid_len) {
1777                 ret = -EINVAL;
1778                 goto exit;
1779         }
1780
1781         if (sme->ssid_len > IW_ESSID_MAX_SIZE) {
1782
1783                 ret = -E2BIG;
1784                 goto exit;
1785         }
1786
1787         memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1788         ndis_ssid.ssid_length = sme->ssid_len;
1789         memcpy(ndis_ssid.ssid, (u8 *)sme->ssid, sme->ssid_len);
1790
1791         if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
1792                 ret = -EBUSY;
1793                 goto exit;
1794         }
1795         if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true) {
1796                 rtw_scan_abort(padapter);
1797         }
1798
1799         psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled;
1800         psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
1801         psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
1802         psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1803         psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1804
1805         ret = rtw_cfg80211_set_wpa_version(psecuritypriv, sme->crypto.wpa_versions);
1806         if (ret < 0)
1807                 goto exit;
1808
1809         ret = rtw_cfg80211_set_auth_type(psecuritypriv, sme->auth_type);
1810
1811         if (ret < 0)
1812                 goto exit;
1813
1814         ret = rtw_cfg80211_set_wpa_ie(padapter, (u8 *)sme->ie, sme->ie_len);
1815         if (ret < 0)
1816                 goto exit;
1817
1818         if (sme->crypto.n_ciphers_pairwise) {
1819                 ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.ciphers_pairwise[0], true);
1820                 if (ret < 0)
1821                         goto exit;
1822         }
1823
1824         /* For WEP Shared auth */
1825         if ((psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Shared ||
1826             psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Auto) && sme->key) {
1827                 u32 wep_key_idx, wep_key_len, wep_total_len;
1828                 struct ndis_802_11_wep   *pwep = NULL;
1829
1830                 wep_key_idx = sme->key_idx;
1831                 wep_key_len = sme->key_len;
1832
1833                 if (sme->key_idx > WEP_KEYS) {
1834                         ret = -EINVAL;
1835                         goto exit;
1836                 }
1837
1838                 if (wep_key_len > 0) {
1839                         wep_key_len = wep_key_len <= 5 ? 5 : 13;
1840                         wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
1841                         pwep = rtw_malloc(wep_total_len);
1842                         if (!pwep) {
1843                                 ret = -ENOMEM;
1844                                 goto exit;
1845                         }
1846
1847                         memset(pwep, 0, wep_total_len);
1848
1849                         pwep->key_length = wep_key_len;
1850                         pwep->length = wep_total_len;
1851
1852                         if (wep_key_len == 13) {
1853                                 padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1854                                 padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
1855                         }
1856                 } else {
1857                         ret = -EINVAL;
1858                         goto exit;
1859                 }
1860
1861                 pwep->key_index = wep_key_idx;
1862                 pwep->key_index |= 0x80000000;
1863
1864                 memcpy(pwep->key_material,  (void *)sme->key, pwep->key_length);
1865
1866                 if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL)
1867                         ret = -EOPNOTSUPP;
1868
1869                 kfree(pwep);
1870
1871                 if (ret < 0)
1872                         goto exit;
1873         }
1874
1875         ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.cipher_group, false);
1876         if (ret < 0)
1877                 return ret;
1878
1879         if (sme->crypto.n_akm_suites) {
1880                 ret = rtw_cfg80211_set_key_mgt(psecuritypriv, sme->crypto.akm_suites[0]);
1881                 if (ret < 0)
1882                         goto exit;
1883         }
1884
1885         authmode = psecuritypriv->ndisauthtype;
1886         rtw_set_802_11_authentication_mode(padapter, authmode);
1887
1888         /* rtw_set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */
1889
1890         if (rtw_set_802_11_connect(padapter, (u8 *)sme->bssid, &ndis_ssid) == false) {
1891                 ret = -1;
1892                 goto exit;
1893         }
1894
1895 exit:
1896
1897         rtw_ps_deny_cancel(padapter, PS_DENY_JOIN);
1898
1899         padapter->mlmepriv.not_indic_disco = false;
1900
1901         return ret;
1902 }
1903
1904 static int cfg80211_rtw_disconnect(struct wiphy *wiphy, struct net_device *ndev,
1905                                    u16 reason_code)
1906 {
1907         struct adapter *padapter = rtw_netdev_priv(ndev);
1908
1909         rtw_set_to_roam(padapter, 0);
1910
1911         rtw_scan_abort(padapter);
1912         LeaveAllPowerSaveMode(padapter);
1913         rtw_disassoc_cmd(padapter, 500, false);
1914
1915         rtw_indicate_disconnect(padapter);
1916
1917         rtw_free_assoc_resources(padapter, 1);
1918         rtw_pwr_wakeup(padapter);
1919
1920         return 0;
1921 }
1922
1923 static int cfg80211_rtw_set_txpower(struct wiphy *wiphy,
1924         struct wireless_dev *wdev,
1925         enum nl80211_tx_power_setting type, int mbm)
1926 {
1927         return 0;
1928 }
1929
1930 static int cfg80211_rtw_get_txpower(struct wiphy *wiphy,
1931         struct wireless_dev *wdev,
1932         int *dbm)
1933 {
1934         *dbm = (12);
1935
1936         return 0;
1937 }
1938
1939 inline bool rtw_cfg80211_pwr_mgmt(struct adapter *adapter)
1940 {
1941         struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(adapter);
1942         return rtw_wdev_priv->power_mgmt;
1943 }
1944
1945 static int cfg80211_rtw_set_power_mgmt(struct wiphy *wiphy,
1946                                        struct net_device *ndev,
1947                                        bool enabled, int timeout)
1948 {
1949         struct adapter *padapter = rtw_netdev_priv(ndev);
1950         struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(padapter);
1951
1952         rtw_wdev_priv->power_mgmt = enabled;
1953
1954         if (!enabled)
1955                 LPS_Leave(padapter, "CFG80211_PWRMGMT");
1956
1957         return 0;
1958 }
1959
1960 static int cfg80211_rtw_set_pmksa(struct wiphy *wiphy,
1961                                   struct net_device *ndev,
1962                                   struct cfg80211_pmksa *pmksa)
1963 {
1964         u8 index, blInserted = false;
1965         struct adapter *padapter = rtw_netdev_priv(ndev);
1966         struct security_priv *psecuritypriv = &padapter->securitypriv;
1967         u8 strZeroMacAddress[ETH_ALEN] = { 0x00 };
1968
1969         if (!memcmp((u8 *)pmksa->bssid, strZeroMacAddress, ETH_ALEN))
1970                 return -EINVAL;
1971
1972         blInserted = false;
1973
1974         /* overwrite PMKID */
1975         for (index = 0 ; index < NUM_PMKID_CACHE; index++) {
1976                 if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) {
1977
1978                         memcpy(psecuritypriv->PMKIDList[index].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN);
1979                         psecuritypriv->PMKIDList[index].bUsed = true;
1980                         psecuritypriv->PMKIDIndex = index+1;
1981                         blInserted = true;
1982                         break;
1983                 }
1984         }
1985
1986         if (!blInserted) {
1987
1988                 memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, (u8 *)pmksa->bssid, ETH_ALEN);
1989                 memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN);
1990
1991                 psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = true;
1992                 psecuritypriv->PMKIDIndex++;
1993                 if (psecuritypriv->PMKIDIndex == 16)
1994                         psecuritypriv->PMKIDIndex = 0;
1995         }
1996
1997         return 0;
1998 }
1999
2000 static int cfg80211_rtw_del_pmksa(struct wiphy *wiphy,
2001                                   struct net_device *ndev,
2002                                   struct cfg80211_pmksa *pmksa)
2003 {
2004         u8 index, bMatched = false;
2005         struct adapter *padapter = rtw_netdev_priv(ndev);
2006         struct security_priv *psecuritypriv = &padapter->securitypriv;
2007
2008         for (index = 0 ; index < NUM_PMKID_CACHE; index++) {
2009                 if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) {
2010                         /*
2011                          * BSSID is matched, the same AP => Remove this PMKID information
2012                          * and reset it.
2013                          */
2014                         eth_zero_addr(psecuritypriv->PMKIDList[index].Bssid);
2015                         memset(psecuritypriv->PMKIDList[index].PMKID, 0x00, WLAN_PMKID_LEN);
2016                         psecuritypriv->PMKIDList[index].bUsed = false;
2017                         bMatched = true;
2018                         break;
2019                 }
2020         }
2021
2022         if (!bMatched)
2023                 return -EINVAL;
2024
2025         return 0;
2026 }
2027
2028 static int cfg80211_rtw_flush_pmksa(struct wiphy *wiphy,
2029                                     struct net_device *ndev)
2030 {
2031         struct adapter *padapter = rtw_netdev_priv(ndev);
2032         struct security_priv *psecuritypriv = &padapter->securitypriv;
2033
2034         memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(struct rt_pmkid_list) * NUM_PMKID_CACHE);
2035         psecuritypriv->PMKIDIndex = 0;
2036
2037         return 0;
2038 }
2039
2040 void rtw_cfg80211_indicate_sta_assoc(struct adapter *padapter, u8 *pmgmt_frame, uint frame_len)
2041 {
2042         struct net_device *ndev = padapter->pnetdev;
2043
2044         {
2045                 struct station_info sinfo = {};
2046                 u8 ie_offset;
2047                 if (GetFrameSubType(pmgmt_frame) == WIFI_ASSOCREQ)
2048                         ie_offset = _ASOCREQ_IE_OFFSET_;
2049                 else /*  WIFI_REASSOCREQ */
2050                         ie_offset = _REASOCREQ_IE_OFFSET_;
2051
2052                 sinfo.filled = 0;
2053                 sinfo.assoc_req_ies = pmgmt_frame + WLAN_HDR_A3_LEN + ie_offset;
2054                 sinfo.assoc_req_ies_len = frame_len - WLAN_HDR_A3_LEN - ie_offset;
2055                 cfg80211_new_sta(ndev, GetAddr2Ptr(pmgmt_frame), &sinfo, GFP_ATOMIC);
2056         }
2057 }
2058
2059 void rtw_cfg80211_indicate_sta_disassoc(struct adapter *padapter, unsigned char *da, unsigned short reason)
2060 {
2061         struct net_device *ndev = padapter->pnetdev;
2062
2063         cfg80211_del_sta(ndev, da, GFP_ATOMIC);
2064 }
2065
2066 static u8 rtw_get_chan_type(struct adapter *adapter)
2067 {
2068         struct mlme_ext_priv *mlme_ext = &adapter->mlmeextpriv;
2069
2070         switch (mlme_ext->cur_bwmode) {
2071         case CHANNEL_WIDTH_20:
2072                 if (is_supported_ht(adapter->registrypriv.wireless_mode))
2073                         return NL80211_CHAN_HT20;
2074                 else
2075                         return NL80211_CHAN_NO_HT;
2076         case CHANNEL_WIDTH_40:
2077                 if (mlme_ext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
2078                         return NL80211_CHAN_HT40PLUS;
2079                 else
2080                         return NL80211_CHAN_HT40MINUS;
2081         default:
2082                 return NL80211_CHAN_HT20;
2083         }
2084
2085         return NL80211_CHAN_HT20;
2086 }
2087
2088 static int cfg80211_rtw_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
2089                                     struct cfg80211_chan_def *chandef)
2090 {
2091         struct adapter *adapter = wiphy_to_adapter(wiphy);
2092         struct registry_priv *registrypriv = &adapter->registrypriv;
2093         enum nl80211_channel_type chan_type;
2094         struct ieee80211_channel *chan = NULL;
2095         int channel;
2096         int freq;
2097
2098         if (!adapter->rtw_wdev)
2099                 return -ENODEV;
2100
2101         channel = rtw_get_oper_ch(adapter);
2102         if (!channel)
2103                 return -ENODATA;
2104
2105         freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
2106
2107         chan = ieee80211_get_channel(adapter->rtw_wdev->wiphy, freq);
2108
2109         if (registrypriv->ht_enable) {
2110                 chan_type = rtw_get_chan_type(adapter);
2111                 cfg80211_chandef_create(chandef, chan, chan_type);
2112         } else {
2113                 cfg80211_chandef_create(chandef, chan, NL80211_CHAN_NO_HT);
2114         }
2115
2116         return 0;
2117 }
2118
2119 static netdev_tx_t rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff *skb, struct net_device *ndev)
2120 {
2121         int rtap_len;
2122         int qos_len = 0;
2123         int dot11_hdr_len = 24;
2124         int snap_len = 6;
2125         unsigned char *pdata;
2126         u16 frame_control;
2127         unsigned char src_mac_addr[6];
2128         unsigned char dst_mac_addr[6];
2129         struct ieee80211_hdr *dot11_hdr;
2130         struct ieee80211_radiotap_header *rtap_hdr;
2131         struct adapter *padapter = rtw_netdev_priv(ndev);
2132
2133         if (!skb)
2134                 goto fail;
2135
2136         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2137                 goto fail;
2138
2139         rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
2140         if (unlikely(rtap_hdr->it_version))
2141                 goto fail;
2142
2143         rtap_len = ieee80211_get_radiotap_len(skb->data);
2144         if (unlikely(skb->len < rtap_len))
2145                 goto fail;
2146
2147         if (rtap_len != 14)
2148                 goto fail;
2149
2150         /* Skip the ratio tap header */
2151         skb_pull(skb, rtap_len);
2152
2153         dot11_hdr = (struct ieee80211_hdr *)skb->data;
2154         frame_control = le16_to_cpu(dot11_hdr->frame_control);
2155         /* Check if the QoS bit is set */
2156         if ((frame_control & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
2157                 /* Check if this ia a Wireless Distribution System (WDS) frame
2158                  * which has 4 MAC addresses
2159                  */
2160                 if (frame_control & 0x0080)
2161                         qos_len = 2;
2162                 if ((frame_control & 0x0300) == 0x0300)
2163                         dot11_hdr_len += 6;
2164
2165                 memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
2166                 memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
2167
2168                 /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
2169                  * two MAC addresses
2170                  */
2171                 skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
2172                 pdata = (unsigned char *)skb->data;
2173                 memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
2174                 memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
2175
2176                 /* Use the real net device to transmit the packet */
2177                 return _rtw_xmit_entry(skb, padapter->pnetdev);
2178
2179         } else if ((frame_control & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
2180                    (IEEE80211_FTYPE_MGMT|IEEE80211_STYPE_ACTION)) {
2181                 /* only for action frames */
2182                 struct xmit_frame               *pmgntframe;
2183                 struct pkt_attrib       *pattrib;
2184                 unsigned char *pframe;
2185                 /* u8 category, action, OUI_Subtype, dialogToken = 0; */
2186                 /* unsigned char *frame_body; */
2187                 struct ieee80211_hdr *pwlanhdr;
2188                 struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2189                 struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2190                 u8 *buf = skb->data;
2191                 u32 len = skb->len;
2192                 u8 category, action;
2193
2194                 if (rtw_action_frame_parse(buf, len, &category, &action) == false)
2195                         goto fail;
2196
2197                 /* starting alloc mgmt frame to dump it */
2198                 pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2199                 if (!pmgntframe)
2200                         goto fail;
2201
2202                 /* update attribute */
2203                 pattrib = &pmgntframe->attrib;
2204                 update_mgntframe_attrib(padapter, pattrib);
2205                 pattrib->retry_ctrl = false;
2206
2207                 memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2208
2209                 pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2210
2211                 memcpy(pframe, (void *)buf, len);
2212                 pattrib->pktlen = len;
2213
2214                 pwlanhdr = (struct ieee80211_hdr *)pframe;
2215                 /* update seq number */
2216                 pmlmeext->mgnt_seq = GetSequence(pwlanhdr);
2217                 pattrib->seqnum = pmlmeext->mgnt_seq;
2218                 pmlmeext->mgnt_seq++;
2219
2220
2221                 pattrib->last_txcmdsz = pattrib->pktlen;
2222
2223                 dump_mgntframe(padapter, pmgntframe);
2224
2225         }
2226
2227 fail:
2228
2229         dev_kfree_skb_any(skb);
2230
2231         return NETDEV_TX_OK;
2232
2233 }
2234
2235
2236
2237 static const struct net_device_ops rtw_cfg80211_monitor_if_ops = {
2238         .ndo_start_xmit = rtw_cfg80211_monitor_if_xmit_entry,
2239 };
2240
2241 static int rtw_cfg80211_add_monitor_if(struct adapter *padapter, char *name, struct net_device **ndev)
2242 {
2243         int ret = 0;
2244         struct net_device *mon_ndev = NULL;
2245         struct wireless_dev *mon_wdev = NULL;
2246         struct rtw_netdev_priv_indicator *pnpi;
2247         struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter);
2248
2249         if (!name) {
2250                 ret = -EINVAL;
2251                 goto out;
2252         }
2253
2254         if (pwdev_priv->pmon_ndev) {
2255                 ret = -EBUSY;
2256                 goto out;
2257         }
2258
2259         mon_ndev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
2260         if (!mon_ndev) {
2261                 ret = -ENOMEM;
2262                 goto out;
2263         }
2264
2265         mon_ndev->type = ARPHRD_IEEE80211_RADIOTAP;
2266         strncpy(mon_ndev->name, name, IFNAMSIZ);
2267         mon_ndev->name[IFNAMSIZ - 1] = 0;
2268         mon_ndev->needs_free_netdev = true;
2269         mon_ndev->priv_destructor = rtw_ndev_destructor;
2270
2271         mon_ndev->netdev_ops = &rtw_cfg80211_monitor_if_ops;
2272
2273         pnpi = netdev_priv(mon_ndev);
2274         pnpi->priv = padapter;
2275         pnpi->sizeof_priv = sizeof(struct adapter);
2276
2277         /*  wdev */
2278         mon_wdev = rtw_zmalloc(sizeof(struct wireless_dev));
2279         if (!mon_wdev) {
2280                 ret = -ENOMEM;
2281                 goto out;
2282         }
2283
2284         mon_wdev->wiphy = padapter->rtw_wdev->wiphy;
2285         mon_wdev->netdev = mon_ndev;
2286         mon_wdev->iftype = NL80211_IFTYPE_MONITOR;
2287         mon_ndev->ieee80211_ptr = mon_wdev;
2288
2289         ret = cfg80211_register_netdevice(mon_ndev);
2290         if (ret) {
2291                 goto out;
2292         }
2293
2294         *ndev = pwdev_priv->pmon_ndev = mon_ndev;
2295         memcpy(pwdev_priv->ifname_mon, name, IFNAMSIZ+1);
2296
2297 out:
2298         if (ret && mon_wdev) {
2299                 kfree(mon_wdev);
2300                 mon_wdev = NULL;
2301         }
2302
2303         if (ret && mon_ndev) {
2304                 free_netdev(mon_ndev);
2305                 *ndev = mon_ndev = NULL;
2306         }
2307
2308         return ret;
2309 }
2310
2311 static struct wireless_dev *
2312         cfg80211_rtw_add_virtual_intf(
2313                 struct wiphy *wiphy,
2314                 const char *name,
2315                 unsigned char name_assign_type,
2316                 enum nl80211_iftype type, struct vif_params *params)
2317 {
2318         int ret = 0;
2319         struct net_device *ndev = NULL;
2320         struct adapter *padapter = wiphy_to_adapter(wiphy);
2321
2322         switch (type) {
2323         case NL80211_IFTYPE_ADHOC:
2324         case NL80211_IFTYPE_AP_VLAN:
2325         case NL80211_IFTYPE_WDS:
2326         case NL80211_IFTYPE_MESH_POINT:
2327                 ret = -ENODEV;
2328                 break;
2329         case NL80211_IFTYPE_MONITOR:
2330                 ret = rtw_cfg80211_add_monitor_if(padapter, (char *)name, &ndev);
2331                 break;
2332         case NL80211_IFTYPE_P2P_CLIENT:
2333         case NL80211_IFTYPE_STATION:
2334                 ret = -ENODEV;
2335                 break;
2336         case NL80211_IFTYPE_P2P_GO:
2337         case NL80211_IFTYPE_AP:
2338                 ret = -ENODEV;
2339                 break;
2340         default:
2341                 ret = -ENODEV;
2342                 break;
2343         }
2344
2345         return ndev ? ndev->ieee80211_ptr : ERR_PTR(ret);
2346 }
2347
2348 static int cfg80211_rtw_del_virtual_intf(struct wiphy *wiphy,
2349         struct wireless_dev *wdev
2350 )
2351 {
2352         struct net_device *ndev = wdev_to_ndev(wdev);
2353         int ret = 0;
2354         struct adapter *adapter;
2355         struct rtw_wdev_priv *pwdev_priv;
2356
2357         if (!ndev) {
2358                 ret = -EINVAL;
2359                 goto exit;
2360         }
2361
2362         adapter = rtw_netdev_priv(ndev);
2363         pwdev_priv = adapter_wdev_data(adapter);
2364
2365         cfg80211_unregister_netdevice(ndev);
2366
2367         if (ndev == pwdev_priv->pmon_ndev) {
2368                 pwdev_priv->pmon_ndev = NULL;
2369                 pwdev_priv->ifname_mon[0] = '\0';
2370         }
2371
2372 exit:
2373         return ret;
2374 }
2375
2376 static int rtw_add_beacon(struct adapter *adapter, const u8 *head, size_t head_len, const u8 *tail, size_t tail_len)
2377 {
2378         int ret = 0;
2379         u8 *pbuf = NULL;
2380         uint len, wps_ielen = 0;
2381         struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
2382
2383         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true)
2384                 return -EINVAL;
2385
2386         if (head_len < 24)
2387                 return -EINVAL;
2388
2389         pbuf = rtw_zmalloc(head_len+tail_len);
2390         if (!pbuf)
2391                 return -ENOMEM;
2392
2393         memcpy(pbuf, (void *)head+24, head_len-24);/*  24 =beacon header len. */
2394         memcpy(pbuf+head_len-24, (void *)tail, tail_len);
2395
2396         len = head_len+tail_len-24;
2397
2398         /* check wps ie if inclued */
2399         rtw_get_wps_ie(pbuf + _FIXED_IE_LENGTH_, len - _FIXED_IE_LENGTH_, NULL, &wps_ielen);
2400
2401         /* pbss_network->ies will not include p2p_ie, wfd ie */
2402         rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, P2P_OUI, 4);
2403         rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, WFD_OUI, 4);
2404
2405         if (rtw_check_beacon_data(adapter, pbuf,  len) == _SUCCESS) {
2406                 ret = 0;
2407         } else {
2408                 ret = -EINVAL;
2409         }
2410
2411
2412         kfree(pbuf);
2413
2414         return ret;
2415 }
2416
2417 static int cfg80211_rtw_start_ap(struct wiphy *wiphy, struct net_device *ndev,
2418                                                                 struct cfg80211_ap_settings *settings)
2419 {
2420         int ret = 0;
2421         struct adapter *adapter = rtw_netdev_priv(ndev);
2422
2423         ret = rtw_add_beacon(adapter, settings->beacon.head, settings->beacon.head_len,
2424                 settings->beacon.tail, settings->beacon.tail_len);
2425
2426         adapter->mlmeextpriv.mlmext_info.hidden_ssid_mode = settings->hidden_ssid;
2427
2428         if (settings->ssid && settings->ssid_len) {
2429                 struct wlan_bssid_ex *pbss_network = &adapter->mlmepriv.cur_network.network;
2430                 struct wlan_bssid_ex *pbss_network_ext = &adapter->mlmeextpriv.mlmext_info.network;
2431
2432                 memcpy(pbss_network->ssid.ssid, (void *)settings->ssid, settings->ssid_len);
2433                 pbss_network->ssid.ssid_length = settings->ssid_len;
2434                 memcpy(pbss_network_ext->ssid.ssid, (void *)settings->ssid, settings->ssid_len);
2435                 pbss_network_ext->ssid.ssid_length = settings->ssid_len;
2436         }
2437
2438         return ret;
2439 }
2440
2441 static int cfg80211_rtw_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
2442                                 struct cfg80211_beacon_data *info)
2443 {
2444         struct adapter *adapter = rtw_netdev_priv(ndev);
2445
2446         return rtw_add_beacon(adapter, info->head, info->head_len, info->tail, info->tail_len);
2447 }
2448
2449 static int cfg80211_rtw_stop_ap(struct wiphy *wiphy, struct net_device *ndev)
2450 {
2451         return 0;
2452 }
2453
2454 static int      cfg80211_rtw_add_station(struct wiphy *wiphy, struct net_device *ndev,
2455                                 const u8 *mac,
2456                         struct station_parameters *params)
2457 {
2458         return 0;
2459 }
2460
2461 static int cfg80211_rtw_del_station(struct wiphy *wiphy, struct net_device *ndev,
2462                                     struct station_del_parameters *params)
2463 {
2464         int ret = 0;
2465         struct list_head *phead, *plist, *tmp;
2466         u8 updated = false;
2467         struct sta_info *psta = NULL;
2468         struct adapter *padapter = rtw_netdev_priv(ndev);
2469         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2470         struct sta_priv *pstapriv = &padapter->stapriv;
2471         const u8 *mac = params->mac;
2472
2473         if (check_fwstate(pmlmepriv, (_FW_LINKED | WIFI_AP_STATE)) != true)
2474                 return -EINVAL;
2475
2476         if (!mac) {
2477                 flush_all_cam_entry(padapter);  /* clear CAM */
2478
2479                 rtw_sta_flush(padapter);
2480
2481                 return 0;
2482         }
2483
2484         if (mac[0] == 0xff && mac[1] == 0xff &&
2485             mac[2] == 0xff && mac[3] == 0xff &&
2486             mac[4] == 0xff && mac[5] == 0xff) {
2487                 return -EINVAL;
2488         }
2489
2490
2491         spin_lock_bh(&pstapriv->asoc_list_lock);
2492
2493         phead = &pstapriv->asoc_list;
2494         /* check asoc_queue */
2495         list_for_each_safe(plist, tmp, phead) {
2496                 psta = list_entry(plist, struct sta_info, asoc_list);
2497
2498                 if (!memcmp((u8 *)mac, psta->hwaddr, ETH_ALEN)) {
2499                         if (psta->dot8021xalg != 1 || psta->bpairwise_key_installed) {
2500                                 list_del_init(&psta->asoc_list);
2501                                 pstapriv->asoc_list_cnt--;
2502
2503                                 updated = ap_free_sta(padapter, psta, true, WLAN_REASON_DEAUTH_LEAVING);
2504
2505                                 psta = NULL;
2506
2507                                 break;
2508                         }
2509
2510                 }
2511
2512         }
2513
2514         spin_unlock_bh(&pstapriv->asoc_list_lock);
2515
2516         associated_clients_update(padapter, updated);
2517
2518         return ret;
2519
2520 }
2521
2522 static int cfg80211_rtw_change_station(struct wiphy *wiphy, struct net_device *ndev,
2523                                   const u8 *mac, struct station_parameters *params)
2524 {
2525         return 0;
2526 }
2527
2528 static struct sta_info *rtw_sta_info_get_by_idx(const int idx, struct sta_priv *pstapriv)
2529
2530 {
2531         struct list_head        *phead, *plist;
2532         struct sta_info *psta = NULL;
2533         int i = 0;
2534
2535         phead = &pstapriv->asoc_list;
2536         plist = get_next(phead);
2537
2538         /* check asoc_queue */
2539         while (phead != plist) {
2540                 if (idx == i)
2541                         psta = container_of(plist, struct sta_info, asoc_list);
2542                 plist = get_next(plist);
2543                 i++;
2544         }
2545         return psta;
2546 }
2547
2548 static int      cfg80211_rtw_dump_station(struct wiphy *wiphy, struct net_device *ndev,
2549                                int idx, u8 *mac, struct station_info *sinfo)
2550 {
2551
2552         int ret = 0;
2553         struct adapter *padapter = rtw_netdev_priv(ndev);
2554         struct sta_info *psta = NULL;
2555         struct sta_priv *pstapriv = &padapter->stapriv;
2556
2557         spin_lock_bh(&pstapriv->asoc_list_lock);
2558         psta = rtw_sta_info_get_by_idx(idx, pstapriv);
2559         spin_unlock_bh(&pstapriv->asoc_list_lock);
2560         if (NULL == psta) {
2561                 ret = -ENOENT;
2562                 goto exit;
2563         }
2564         memcpy(mac, psta->hwaddr, ETH_ALEN);
2565         sinfo->filled = BIT_ULL(NL80211_STA_INFO_SIGNAL);
2566         sinfo->signal = psta->rssi;
2567
2568 exit:
2569         return ret;
2570 }
2571
2572 static int      cfg80211_rtw_change_bss(struct wiphy *wiphy, struct net_device *ndev,
2573                               struct bss_parameters *params)
2574 {
2575         return 0;
2576 }
2577
2578 void rtw_cfg80211_rx_action(struct adapter *adapter, u8 *frame, uint frame_len, const char *msg)
2579 {
2580         s32 freq;
2581         int channel;
2582         u8 category, action;
2583
2584         channel = rtw_get_oper_ch(adapter);
2585
2586         rtw_action_frame_parse(frame, frame_len, &category, &action);
2587
2588         freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
2589
2590         rtw_cfg80211_rx_mgmt(adapter, freq, 0, frame, frame_len, GFP_ATOMIC);
2591 }
2592
2593 static int _cfg80211_rtw_mgmt_tx(struct adapter *padapter, u8 tx_ch, const u8 *buf, size_t len)
2594 {
2595         struct xmit_frame       *pmgntframe;
2596         struct pkt_attrib       *pattrib;
2597         unsigned char *pframe;
2598         int ret = _FAIL;
2599         bool __maybe_unused ack = true;
2600         struct ieee80211_hdr *pwlanhdr;
2601         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2602         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2603
2604         rtw_set_scan_deny(padapter, 1000);
2605
2606         rtw_scan_abort(padapter);
2607         if (tx_ch != rtw_get_oper_ch(padapter)) {
2608                 if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED))
2609                         pmlmeext->cur_channel = tx_ch;
2610                 set_channel_bwmode(padapter, tx_ch, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
2611         }
2612
2613         /* starting alloc mgmt frame to dump it */
2614         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2615         if (!pmgntframe) {
2616                 /* ret = -ENOMEM; */
2617                 ret = _FAIL;
2618                 goto exit;
2619         }
2620
2621         /* update attribute */
2622         pattrib = &pmgntframe->attrib;
2623         update_mgntframe_attrib(padapter, pattrib);
2624         pattrib->retry_ctrl = false;
2625
2626         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2627
2628         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2629
2630         memcpy(pframe, (void *)buf, len);
2631         pattrib->pktlen = len;
2632
2633         pwlanhdr = (struct ieee80211_hdr *)pframe;
2634         /* update seq number */
2635         pmlmeext->mgnt_seq = GetSequence(pwlanhdr);
2636         pattrib->seqnum = pmlmeext->mgnt_seq;
2637         pmlmeext->mgnt_seq++;
2638
2639         pattrib->last_txcmdsz = pattrib->pktlen;
2640
2641         if (dump_mgntframe_and_wait_ack(padapter, pmgntframe) != _SUCCESS) {
2642                 ack = false;
2643                 ret = _FAIL;
2644
2645         } else {
2646                 msleep(50);
2647
2648                 ret = _SUCCESS;
2649         }
2650
2651 exit:
2652
2653         return ret;
2654
2655 }
2656
2657 static int cfg80211_rtw_mgmt_tx(struct wiphy *wiphy,
2658         struct wireless_dev *wdev,
2659         struct cfg80211_mgmt_tx_params *params,
2660         u64 *cookie)
2661 {
2662         struct net_device *ndev = wdev_to_ndev(wdev);
2663         struct ieee80211_channel *chan = params->chan;
2664         const u8 *buf = params->buf;
2665         size_t len = params->len;
2666         int ret = 0;
2667         int tx_ret;
2668         u32 dump_limit = RTW_MAX_MGMT_TX_CNT;
2669         u32 dump_cnt = 0;
2670         bool ack = true;
2671         u8 tx_ch = (u8)ieee80211_frequency_to_channel(chan->center_freq);
2672         u8 category, action;
2673         int type = (-1);
2674         struct adapter *padapter;
2675         struct rtw_wdev_priv *pwdev_priv;
2676
2677         if (!ndev) {
2678                 ret = -EINVAL;
2679                 goto exit;
2680         }
2681
2682         padapter = rtw_netdev_priv(ndev);
2683         pwdev_priv = adapter_wdev_data(padapter);
2684
2685         /* cookie generation */
2686         *cookie = (unsigned long) buf;
2687
2688         /* indicate ack before issue frame to avoid racing with rsp frame */
2689         rtw_cfg80211_mgmt_tx_status(padapter, *cookie, buf, len, ack, GFP_KERNEL);
2690
2691         if (rtw_action_frame_parse(buf, len, &category, &action) == false)
2692                 goto exit;
2693
2694         rtw_ps_deny(padapter, PS_DENY_MGNT_TX);
2695         if (_FAIL == rtw_pwr_wakeup(padapter)) {
2696                 ret = -EFAULT;
2697                 goto cancel_ps_deny;
2698         }
2699
2700         do {
2701                 dump_cnt++;
2702                 tx_ret = _cfg80211_rtw_mgmt_tx(padapter, tx_ch, buf, len);
2703         } while (dump_cnt < dump_limit && tx_ret != _SUCCESS);
2704
2705         switch (type) {
2706         case P2P_GO_NEGO_CONF:
2707                 rtw_clear_scan_deny(padapter);
2708                 break;
2709         case P2P_INVIT_RESP:
2710                 if (pwdev_priv->invit_info.flags & BIT(0) && pwdev_priv->invit_info.status == 0) {
2711                         rtw_set_scan_deny(padapter, 5000);
2712                         rtw_pwr_wakeup_ex(padapter, 5000);
2713                         rtw_clear_scan_deny(padapter);
2714                 }
2715                 break;
2716         }
2717
2718 cancel_ps_deny:
2719         rtw_ps_deny_cancel(padapter, PS_DENY_MGNT_TX);
2720 exit:
2721         return ret;
2722 }
2723
2724 static void rtw_cfg80211_init_ht_capab(struct ieee80211_sta_ht_cap *ht_cap, enum nl80211_band band)
2725 {
2726
2727 #define MAX_BIT_RATE_40MHZ_MCS15        300     /* Mbps */
2728 #define MAX_BIT_RATE_40MHZ_MCS7         150     /* Mbps */
2729
2730         ht_cap->ht_supported = true;
2731
2732         ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2733                                         IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20 |
2734                                         IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
2735
2736         /*
2737          *Maximum length of AMPDU that the STA can receive.
2738          *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
2739          */
2740         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2741
2742         /*Minimum MPDU start spacing , */
2743         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
2744
2745         ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2746
2747         /*
2748          *hw->wiphy->bands[NL80211_BAND_2GHZ]
2749          *base on ant_num
2750          *rx_mask: RX mask
2751          *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
2752          *if rx_ant =2 rx_mask[1]= 0xff;==>MCS8-MCS15
2753          *if rx_ant >=3 rx_mask[2]= 0xff;
2754          *if BW_40 rx_mask[4]= 0x01;
2755          *highest supported RX rate
2756          */
2757         ht_cap->mcs.rx_mask[0] = 0xFF;
2758         ht_cap->mcs.rx_mask[1] = 0x00;
2759         ht_cap->mcs.rx_mask[4] = 0x01;
2760
2761         ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
2762 }
2763
2764 void rtw_cfg80211_init_wiphy(struct adapter *padapter)
2765 {
2766         struct ieee80211_supported_band *bands;
2767         struct wireless_dev *pwdev = padapter->rtw_wdev;
2768         struct wiphy *wiphy = pwdev->wiphy;
2769
2770         {
2771                 bands = wiphy->bands[NL80211_BAND_2GHZ];
2772                 if (bands)
2773                         rtw_cfg80211_init_ht_capab(&bands->ht_cap, NL80211_BAND_2GHZ);
2774         }
2775
2776         /* copy mac_addr to wiphy */
2777         memcpy(wiphy->perm_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
2778
2779 }
2780
2781 static void rtw_cfg80211_preinit_wiphy(struct adapter *padapter, struct wiphy *wiphy)
2782 {
2783
2784         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2785
2786         wiphy->max_scan_ssids = RTW_SSID_SCAN_AMOUNT;
2787         wiphy->max_scan_ie_len = RTW_SCAN_IE_LEN_MAX;
2788         wiphy->max_num_pmkids = RTW_MAX_NUM_PMKIDS;
2789
2790         wiphy->max_remain_on_channel_duration = RTW_MAX_REMAIN_ON_CHANNEL_DURATION;
2791
2792         wiphy->interface_modes =        BIT(NL80211_IFTYPE_STATION)
2793                                                                 | BIT(NL80211_IFTYPE_ADHOC)
2794                                                                 | BIT(NL80211_IFTYPE_AP)
2795                                                                 | BIT(NL80211_IFTYPE_MONITOR)
2796                                                                 ;
2797
2798         wiphy->mgmt_stypes = rtw_cfg80211_default_mgmt_stypes;
2799
2800         wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
2801
2802         wiphy->cipher_suites = rtw_cipher_suites;
2803         wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites);
2804
2805         /* if (padapter->registrypriv.wireless_mode & WIRELESS_11G) */
2806         wiphy->bands[NL80211_BAND_2GHZ] = rtw_spt_band_alloc(NL80211_BAND_2GHZ);
2807
2808         wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
2809         wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX | WIPHY_FLAG_HAVE_AP_SME;
2810
2811 #if defined(CONFIG_PM)
2812         wiphy->max_sched_scan_reqs = 1;
2813 #endif
2814
2815 #if defined(CONFIG_PM)
2816         wiphy->wowlan = &wowlan_stub;
2817 #endif
2818
2819         if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE)
2820                 wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
2821         else
2822                 wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2823 }
2824
2825 static struct cfg80211_ops rtw_cfg80211_ops = {
2826         .change_virtual_intf = cfg80211_rtw_change_iface,
2827         .add_key = cfg80211_rtw_add_key,
2828         .get_key = cfg80211_rtw_get_key,
2829         .del_key = cfg80211_rtw_del_key,
2830         .set_default_key = cfg80211_rtw_set_default_key,
2831         .get_station = cfg80211_rtw_get_station,
2832         .scan = cfg80211_rtw_scan,
2833         .set_wiphy_params = cfg80211_rtw_set_wiphy_params,
2834         .connect = cfg80211_rtw_connect,
2835         .disconnect = cfg80211_rtw_disconnect,
2836         .join_ibss = cfg80211_rtw_join_ibss,
2837         .leave_ibss = cfg80211_rtw_leave_ibss,
2838         .set_tx_power = cfg80211_rtw_set_txpower,
2839         .get_tx_power = cfg80211_rtw_get_txpower,
2840         .set_power_mgmt = cfg80211_rtw_set_power_mgmt,
2841         .set_pmksa = cfg80211_rtw_set_pmksa,
2842         .del_pmksa = cfg80211_rtw_del_pmksa,
2843         .flush_pmksa = cfg80211_rtw_flush_pmksa,
2844         .get_channel = cfg80211_rtw_get_channel,
2845         .add_virtual_intf = cfg80211_rtw_add_virtual_intf,
2846         .del_virtual_intf = cfg80211_rtw_del_virtual_intf,
2847
2848         .start_ap = cfg80211_rtw_start_ap,
2849         .change_beacon = cfg80211_rtw_change_beacon,
2850         .stop_ap = cfg80211_rtw_stop_ap,
2851
2852         .add_station = cfg80211_rtw_add_station,
2853         .del_station = cfg80211_rtw_del_station,
2854         .change_station = cfg80211_rtw_change_station,
2855         .dump_station = cfg80211_rtw_dump_station,
2856         .change_bss = cfg80211_rtw_change_bss,
2857
2858         .mgmt_tx = cfg80211_rtw_mgmt_tx,
2859 };
2860
2861 int rtw_wdev_alloc(struct adapter *padapter, struct device *dev)
2862 {
2863         int ret = 0;
2864         struct wiphy *wiphy;
2865         struct wireless_dev *wdev;
2866         struct rtw_wdev_priv *pwdev_priv;
2867         struct net_device *pnetdev = padapter->pnetdev;
2868
2869         /* wiphy */
2870         wiphy = wiphy_new(&rtw_cfg80211_ops, sizeof(struct adapter *));
2871         if (!wiphy) {
2872                 ret = -ENOMEM;
2873                 goto exit;
2874         }
2875         set_wiphy_dev(wiphy, dev);
2876         *((struct adapter **)wiphy_priv(wiphy)) = padapter;
2877         rtw_cfg80211_preinit_wiphy(padapter, wiphy);
2878
2879         /* init regulary domain */
2880         rtw_regd_init(wiphy, rtw_reg_notifier);
2881
2882         ret = wiphy_register(wiphy);
2883         if (ret < 0)
2884                 goto free_wiphy;
2885
2886         /*  wdev */
2887         wdev = rtw_zmalloc(sizeof(struct wireless_dev));
2888         if (!wdev) {
2889                 ret = -ENOMEM;
2890                 goto unregister_wiphy;
2891         }
2892         wdev->wiphy = wiphy;
2893         wdev->netdev = pnetdev;
2894
2895         wdev->iftype = NL80211_IFTYPE_STATION; /*  will be init in rtw_hal_init() */
2896                                                /*  Must sync with _rtw_init_mlme_priv() */
2897                                            /*  pmlmepriv->fw_state = WIFI_STATION_STATE */
2898         padapter->rtw_wdev = wdev;
2899         pnetdev->ieee80211_ptr = wdev;
2900
2901         /* init pwdev_priv */
2902         pwdev_priv = adapter_wdev_data(padapter);
2903         pwdev_priv->rtw_wdev = wdev;
2904         pwdev_priv->pmon_ndev = NULL;
2905         pwdev_priv->ifname_mon[0] = '\0';
2906         pwdev_priv->padapter = padapter;
2907         pwdev_priv->scan_request = NULL;
2908         spin_lock_init(&pwdev_priv->scan_req_lock);
2909
2910         pwdev_priv->p2p_enabled = false;
2911         pwdev_priv->provdisc_req_issued = false;
2912         rtw_wdev_invit_info_init(&pwdev_priv->invit_info);
2913         rtw_wdev_nego_info_init(&pwdev_priv->nego_info);
2914
2915         pwdev_priv->bandroid_scan = false;
2916
2917         if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE)
2918                 pwdev_priv->power_mgmt = true;
2919         else
2920                 pwdev_priv->power_mgmt = false;
2921
2922         return ret;
2923
2924 unregister_wiphy:
2925         wiphy_unregister(wiphy);
2926  free_wiphy:
2927         wiphy_free(wiphy);
2928 exit:
2929         return ret;
2930
2931 }
2932
2933 void rtw_wdev_free(struct wireless_dev *wdev)
2934 {
2935         if (!wdev)
2936                 return;
2937
2938         kfree(wdev->wiphy->bands[NL80211_BAND_2GHZ]);
2939
2940         wiphy_free(wdev->wiphy);
2941
2942         kfree(wdev);
2943 }
2944
2945 void rtw_wdev_unregister(struct wireless_dev *wdev)
2946 {
2947         struct net_device *ndev;
2948         struct adapter *adapter;
2949         struct rtw_wdev_priv *pwdev_priv;
2950
2951         if (!wdev)
2952                 return;
2953         ndev = wdev_to_ndev(wdev);
2954         if (!ndev)
2955                 return;
2956
2957         adapter = rtw_netdev_priv(ndev);
2958         pwdev_priv = adapter_wdev_data(adapter);
2959
2960         rtw_cfg80211_indicate_scan_done(adapter, true);
2961
2962         if (pwdev_priv->pmon_ndev)
2963                 unregister_netdev(pwdev_priv->pmon_ndev);
2964
2965         wiphy_unregister(wdev->wiphy);
2966 }