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