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