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