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