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