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