1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3 
4 #define _RTW_WLAN_UTIL_C_
5 
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/wifi.h"
9 
10 static unsigned char ARTHEROS_OUI1[] = {0x00, 0x03, 0x7f};
11 static unsigned char ARTHEROS_OUI2[] = {0x00, 0x13, 0x74};
12 
13 static unsigned char BROADCOM_OUI1[] = {0x00, 0x10, 0x18};
14 static unsigned char BROADCOM_OUI2[] = {0x00, 0x0a, 0xf7};
15 
16 static unsigned char CISCO_OUI[] = {0x00, 0x40, 0x96};
17 static unsigned char MARVELL_OUI[] = {0x00, 0x50, 0x43};
18 static unsigned char RALINK_OUI[] = {0x00, 0x0c, 0x43};
19 static unsigned char REALTEK_OUI[] = {0x00, 0xe0, 0x4c};
20 static unsigned char AIRGOCAP_OUI[] = {0x00, 0x0a, 0xf5};
21 static unsigned char EPIGRAM_OUI[] = {0x00, 0x90, 0x4c};
22 
23 unsigned char REALTEK_96B_IE[] = {0x00, 0xe0, 0x4c, 0x02, 0x01, 0x20};
24 
25 #define R2T_PHY_DELAY	(0)
26 
27 /* define WAIT_FOR_BCN_TO_M	(3000) */
28 #define WAIT_FOR_BCN_TO_MIN	(6000)
29 #define WAIT_FOR_BCN_TO_MAX	(20000)
30 
31 static u8 rtw_basic_rate_cck[4] = {
32 	IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK, IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK,
33 	IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK, IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK
34 };
35 
36 static u8 rtw_basic_rate_ofdm[3] = {
37 	IEEE80211_OFDM_RATE_6MB | IEEE80211_BASIC_RATE_MASK, IEEE80211_OFDM_RATE_12MB | IEEE80211_BASIC_RATE_MASK,
38 	IEEE80211_OFDM_RATE_24MB | IEEE80211_BASIC_RATE_MASK
39 };
40 
41 static u8 rtw_basic_rate_mix[7] = {
42 	IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK, IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK,
43 	IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK, IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK,
44 	IEEE80211_OFDM_RATE_6MB | IEEE80211_BASIC_RATE_MASK, IEEE80211_OFDM_RATE_12MB | IEEE80211_BASIC_RATE_MASK,
45 	IEEE80211_OFDM_RATE_24MB | IEEE80211_BASIC_RATE_MASK
46 };
47 
cckrates_included(unsigned char * rate,int ratelen)48 bool cckrates_included(unsigned char *rate, int ratelen)
49 {
50 	int	i;
51 
52 	for (i = 0; i < ratelen; i++) {
53 		if  ((((rate[i]) & 0x7f) == 2)	|| (((rate[i]) & 0x7f) == 4) ||
54 		     (((rate[i]) & 0x7f) == 11)  || (((rate[i]) & 0x7f) == 22))
55 			return true;
56 	}
57 	return false;
58 }
59 
cckratesonly_included(unsigned char * rate,int ratelen)60 bool cckratesonly_included(unsigned char *rate, int ratelen)
61 {
62 	int	i;
63 
64 	for (i = 0; i < ratelen; i++) {
65 		if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
66 		    (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
67 			return false;
68 	}
69 
70 	return true;
71 }
72 
networktype_to_raid(unsigned char network_type)73 unsigned char networktype_to_raid(unsigned char network_type)
74 {
75 	unsigned char raid;
76 
77 	switch (network_type) {
78 	case WIRELESS_11B:
79 		raid = RATR_INX_WIRELESS_B;
80 		break;
81 	case WIRELESS_11G:
82 		raid = RATR_INX_WIRELESS_G;
83 		break;
84 	case WIRELESS_11BG:
85 		raid = RATR_INX_WIRELESS_GB;
86 		break;
87 	case WIRELESS_11_24N:
88 		raid = RATR_INX_WIRELESS_N;
89 		break;
90 	case WIRELESS_11G_24N:
91 		raid = RATR_INX_WIRELESS_NG;
92 		break;
93 	case WIRELESS_11BG_24N:
94 		raid = RATR_INX_WIRELESS_NGB;
95 		break;
96 	default:
97 		raid = RATR_INX_WIRELESS_GB;
98 		break;
99 	}
100 	return raid;
101 }
102 
judge_network_type(struct adapter * padapter,unsigned char * rate,int ratelen)103 u8 judge_network_type(struct adapter *padapter, unsigned char *rate, int ratelen)
104 {
105 	u8 network_type = 0;
106 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
107 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
108 
109 	if (pmlmeext->cur_channel > 14) {
110 		network_type |= WIRELESS_INVALID;
111 	} else {
112 		if (pmlmeinfo->HT_enable)
113 			network_type = WIRELESS_11_24N;
114 
115 		if (cckratesonly_included(rate, ratelen))
116 			network_type |= WIRELESS_11B;
117 		else if (cckrates_included(rate, ratelen))
118 			network_type |= WIRELESS_11BG;
119 		else
120 			network_type |= WIRELESS_11G;
121 	}
122 	return	network_type;
123 }
124 
ratetbl_val_2wifirate(unsigned char rate)125 static unsigned char ratetbl_val_2wifirate(unsigned char rate)
126 {
127 	unsigned char val = 0;
128 
129 	switch (rate & 0x7f) {
130 	case 0:
131 		val = IEEE80211_CCK_RATE_1MB;
132 		break;
133 	case 1:
134 		val = IEEE80211_CCK_RATE_2MB;
135 		break;
136 	case 2:
137 		val = IEEE80211_CCK_RATE_5MB;
138 		break;
139 	case 3:
140 		val = IEEE80211_CCK_RATE_11MB;
141 		break;
142 	case 4:
143 		val = IEEE80211_OFDM_RATE_6MB;
144 		break;
145 	case 5:
146 		val = IEEE80211_OFDM_RATE_9MB;
147 		break;
148 	case 6:
149 		val = IEEE80211_OFDM_RATE_12MB;
150 		break;
151 	case 7:
152 		val = IEEE80211_OFDM_RATE_18MB;
153 		break;
154 	case 8:
155 		val = IEEE80211_OFDM_RATE_24MB;
156 		break;
157 	case 9:
158 		val = IEEE80211_OFDM_RATE_36MB;
159 		break;
160 	case 10:
161 		val = IEEE80211_OFDM_RATE_48MB;
162 		break;
163 	case 11:
164 		val = IEEE80211_OFDM_RATE_54MB;
165 		break;
166 	}
167 	return val;
168 }
169 
is_basicrate(struct adapter * padapter,unsigned char rate)170 static bool is_basicrate(struct adapter *padapter, unsigned char rate)
171 {
172 	int i;
173 	unsigned char val;
174 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
175 
176 	for (i = 0; i < NumRates; i++) {
177 		val = pmlmeext->basicrate[i];
178 
179 		if ((val != 0xff) && (val != 0xfe)) {
180 			if (rate == ratetbl_val_2wifirate(val))
181 				return true;
182 		}
183 	}
184 	return false;
185 }
186 
ratetbl2rateset(struct adapter * padapter,unsigned char * rateset)187 static unsigned int ratetbl2rateset(struct adapter *padapter, unsigned char *rateset)
188 {
189 	int i;
190 	unsigned char rate;
191 	unsigned int	len = 0;
192 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
193 
194 	for (i = 0; i < NumRates; i++) {
195 		rate = pmlmeext->datarate[i];
196 
197 		switch (rate) {
198 		case 0xff:
199 			return len;
200 		case 0xfe:
201 			continue;
202 		default:
203 			rate = ratetbl_val_2wifirate(rate);
204 
205 			if (is_basicrate(padapter, rate))
206 				rate |= IEEE80211_BASIC_RATE_MASK;
207 
208 			rateset[len] = rate;
209 			len++;
210 			break;
211 		}
212 	}
213 	return len;
214 }
215 
get_rate_set(struct adapter * padapter,unsigned char * pbssrate,int * bssrate_len)216 void get_rate_set(struct adapter *padapter, unsigned char *pbssrate, int *bssrate_len)
217 {
218 	unsigned char supportedrates[NumRates];
219 
220 	memset(supportedrates, 0, NumRates);
221 	*bssrate_len = ratetbl2rateset(padapter, supportedrates);
222 	memcpy(pbssrate, supportedrates, *bssrate_len);
223 }
224 
UpdateBrateTbl(struct adapter * Adapter,u8 * mbrate)225 void UpdateBrateTbl(struct adapter *Adapter, u8 *mbrate)
226 {
227 	u8	i;
228 	u8	rate;
229 
230 	/*  1M, 2M, 5.5M, 11M, 6M, 12M, 24M are mandatory. */
231 	for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
232 		rate = mbrate[i] & 0x7f;
233 		switch (rate) {
234 		case IEEE80211_CCK_RATE_1MB:
235 		case IEEE80211_CCK_RATE_2MB:
236 		case IEEE80211_CCK_RATE_5MB:
237 		case IEEE80211_CCK_RATE_11MB:
238 		case IEEE80211_OFDM_RATE_6MB:
239 		case IEEE80211_OFDM_RATE_12MB:
240 		case IEEE80211_OFDM_RATE_24MB:
241 			mbrate[i] |= IEEE80211_BASIC_RATE_MASK;
242 			break;
243 		}
244 	}
245 }
246 
UpdateBrateTblForSoftAP(u8 * bssrateset,u32 bssratelen)247 void UpdateBrateTblForSoftAP(u8 *bssrateset, u32 bssratelen)
248 {
249 	u8	i;
250 	u8	rate;
251 
252 	for (i = 0; i < bssratelen; i++) {
253 		rate = bssrateset[i] & 0x7f;
254 		switch (rate) {
255 		case IEEE80211_CCK_RATE_1MB:
256 		case IEEE80211_CCK_RATE_2MB:
257 		case IEEE80211_CCK_RATE_5MB:
258 		case IEEE80211_CCK_RATE_11MB:
259 			bssrateset[i] |= IEEE80211_BASIC_RATE_MASK;
260 			break;
261 		}
262 	}
263 }
264 
Save_DM_Func_Flag(struct adapter * padapter)265 void Save_DM_Func_Flag(struct adapter *padapter)
266 {
267 	u8	saveflag = true;
268 
269 	SetHwReg8188EU(padapter, HW_VAR_DM_FUNC_OP, (u8 *)(&saveflag));
270 }
271 
Restore_DM_Func_Flag(struct adapter * padapter)272 void Restore_DM_Func_Flag(struct adapter *padapter)
273 {
274 	u8	saveflag = false;
275 
276 	SetHwReg8188EU(padapter, HW_VAR_DM_FUNC_OP, (u8 *)(&saveflag));
277 }
278 
Set_MSR(struct adapter * padapter,u8 type)279 void Set_MSR(struct adapter *padapter, u8 type)
280 {
281 	u8 val8;
282 	int res;
283 
284 	res = rtw_read8(padapter, MSR, &val8);
285 	if (res)
286 		return;
287 
288 	val8 &= 0x0c;
289 	val8 |= type;
290 	rtw_write8(padapter, MSR, val8);
291 }
292 
rtw_get_oper_ch(struct adapter * adapter)293 inline u8 rtw_get_oper_ch(struct adapter *adapter)
294 {
295 	return adapter->mlmeextpriv.oper_channel;
296 }
297 
rtw_set_oper_ch(struct adapter * adapter,u8 ch)298 inline void rtw_set_oper_ch(struct adapter *adapter, u8 ch)
299 {
300 	adapter->mlmeextpriv.oper_channel = ch;
301 }
302 
rtw_set_oper_bw(struct adapter * adapter,u8 bw)303 inline void rtw_set_oper_bw(struct adapter *adapter, u8 bw)
304 {
305 	adapter->mlmeextpriv.oper_bwmode = bw;
306 }
307 
rtw_set_oper_choffset(struct adapter * adapter,u8 offset)308 inline void rtw_set_oper_choffset(struct adapter *adapter, u8 offset)
309 {
310 	adapter->mlmeextpriv.oper_ch_offset = offset;
311 }
312 
SelectChannel(struct adapter * padapter,unsigned char channel)313 void SelectChannel(struct adapter *padapter, unsigned char channel)
314 {
315 	/* saved channel info */
316 	rtw_set_oper_ch(padapter, channel);
317 	PHY_SwChnl8188E(padapter, channel);
318 }
319 
SetBWMode(struct adapter * padapter,unsigned short bwmode,unsigned char channel_offset)320 void SetBWMode(struct adapter *padapter, unsigned short bwmode,
321 	       unsigned char channel_offset)
322 {
323 	/* saved bw info */
324 	rtw_set_oper_bw(padapter, bwmode);
325 	rtw_set_oper_choffset(padapter, channel_offset);
326 
327 	PHY_SetBWMode8188E(padapter, (enum ht_channel_width)bwmode, channel_offset);
328 }
329 
set_channel_bwmode(struct adapter * padapter,unsigned char channel,unsigned char channel_offset,unsigned short bwmode)330 void set_channel_bwmode(struct adapter *padapter, unsigned char channel, unsigned char channel_offset, unsigned short bwmode)
331 {
332 	u8 center_ch;
333 
334 	if ((bwmode == HT_CHANNEL_WIDTH_20) ||
335 	    (channel_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)) {
336 		/* SelectChannel(padapter, channel); */
337 		center_ch = channel;
338 	} else {
339 		/* switch to the proper channel */
340 		if (channel_offset == HAL_PRIME_CHNL_OFFSET_LOWER) {
341 			/* SelectChannel(padapter, channel + 2); */
342 			center_ch = channel + 2;
343 		} else {
344 			/* SelectChannel(padapter, channel - 2); */
345 			center_ch = channel - 2;
346 		}
347 	}
348 
349 	/* set Channel */
350 	/* saved channel/bw info */
351 	rtw_set_oper_ch(padapter, channel);
352 	rtw_set_oper_bw(padapter, bwmode);
353 	rtw_set_oper_choffset(padapter, channel_offset);
354 
355 	PHY_SwChnl8188E(padapter, center_ch); /*  set center channel */
356 	SetBWMode(padapter, bwmode, channel_offset);
357 }
358 
get_my_bssid(struct wlan_bssid_ex * pnetwork)359 __inline u8 *get_my_bssid(struct wlan_bssid_ex *pnetwork)
360 {
361 	return pnetwork->MacAddress;
362 }
363 
get_beacon_interval(struct wlan_bssid_ex * bss)364 u16 get_beacon_interval(struct wlan_bssid_ex *bss)
365 {
366 	__le16 val;
367 	memcpy((unsigned char *)&val, rtw_get_beacon_interval_from_ie(bss->IEs), 2);
368 
369 	return le16_to_cpu(val);
370 }
371 
is_client_associated_to_ap(struct adapter * padapter)372 int is_client_associated_to_ap(struct adapter *padapter)
373 {
374 	struct mlme_ext_priv	*pmlmeext;
375 	struct mlme_ext_info	*pmlmeinfo;
376 
377 	if (!padapter)
378 		return _FAIL;
379 
380 	pmlmeext = &padapter->mlmeextpriv;
381 	pmlmeinfo = &pmlmeext->mlmext_info;
382 
383 	if ((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && ((pmlmeinfo->state & 0x03) == WIFI_FW_STATION_STATE))
384 		return true;
385 	else
386 		return _FAIL;
387 }
388 
is_client_associated_to_ibss(struct adapter * padapter)389 int is_client_associated_to_ibss(struct adapter *padapter)
390 {
391 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
392 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
393 
394 	if ((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && ((pmlmeinfo->state & 0x03) == WIFI_FW_ADHOC_STATE))
395 		return true;
396 	else
397 		return _FAIL;
398 }
399 
is_IBSS_empty(struct adapter * padapter)400 int is_IBSS_empty(struct adapter *padapter)
401 {
402 	unsigned int i;
403 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
404 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
405 
406 	for (i = IBSS_START_MAC_ID; i < NUM_STA; i++) {
407 		if (pmlmeinfo->FW_sta_info[i].status == 1)
408 			return _FAIL;
409 	}
410 	return true;
411 }
412 
decide_wait_for_beacon_timeout(unsigned int bcn_interval)413 unsigned int decide_wait_for_beacon_timeout(unsigned int bcn_interval)
414 {
415 	if ((bcn_interval << 2) < WAIT_FOR_BCN_TO_MIN)
416 		return WAIT_FOR_BCN_TO_MIN;
417 	else if ((bcn_interval << 2) > WAIT_FOR_BCN_TO_MAX)
418 		return WAIT_FOR_BCN_TO_MAX;
419 	else
420 		return bcn_interval << 2;
421 }
422 
invalidate_cam_all(struct adapter * padapter)423 void invalidate_cam_all(struct adapter *padapter)
424 {
425 	rtw_write32(padapter, RWCAM, BIT(31) | BIT(30));
426 }
427 
write_cam(struct adapter * padapter,u8 entry,u16 ctrl,u8 * mac,u8 * key)428 void write_cam(struct adapter *padapter, u8 entry, u16 ctrl, u8 *mac, u8 *key)
429 {
430 	unsigned int	i, val, addr;
431 	int j;
432 	u32	cam_val[2];
433 
434 	addr = entry << 3;
435 
436 	for (j = 5; j >= 0; j--) {
437 		switch (j) {
438 		case 0:
439 			val = (ctrl | (mac[0] << 16) | (mac[1] << 24));
440 			break;
441 		case 1:
442 			val = (mac[2] | (mac[3] << 8) | (mac[4] << 16) | (mac[5] << 24));
443 			break;
444 		default:
445 			i = (j - 2) << 2;
446 			val = (key[i] | (key[i + 1] << 8) | (key[i + 2] << 16) | (key[i + 3] << 24));
447 			break;
448 		}
449 
450 		cam_val[0] = val;
451 		cam_val[1] = addr + (unsigned int)j;
452 
453 		rtw_write32(padapter, WCAMI, cam_val[0]);
454 		rtw_write32(padapter, RWCAM, CAM_POLLINIG | CAM_WRITE | cam_val[1]);
455 	}
456 }
457 
clear_cam_entry(struct adapter * padapter,u8 entry)458 void clear_cam_entry(struct adapter *padapter, u8 entry)
459 {
460 	unsigned char null_sta[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
461 	unsigned char null_key[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
462 				    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
463 
464 	write_cam(padapter, entry, 0, null_sta, null_key);
465 }
466 
allocate_fw_sta_entry(struct adapter * padapter)467 int allocate_fw_sta_entry(struct adapter *padapter)
468 {
469 	unsigned int mac_id;
470 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
471 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
472 
473 	for (mac_id = IBSS_START_MAC_ID; mac_id < NUM_STA; mac_id++) {
474 		if (pmlmeinfo->FW_sta_info[mac_id].status == 0) {
475 			pmlmeinfo->FW_sta_info[mac_id].status = 1;
476 			pmlmeinfo->FW_sta_info[mac_id].retry = 0;
477 			break;
478 		}
479 	}
480 
481 	return mac_id;
482 }
483 
flush_all_cam_entry(struct adapter * padapter)484 void flush_all_cam_entry(struct adapter *padapter)
485 {
486 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
487 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
488 
489 	rtw_write32(padapter, RWCAM, BIT(31) | BIT(30));
490 
491 	memset((u8 *)(pmlmeinfo->FW_sta_info), 0, sizeof(pmlmeinfo->FW_sta_info));
492 }
493 
WMM_param_handler(struct adapter * padapter,struct ndis_802_11_var_ie * pIE)494 int WMM_param_handler(struct adapter *padapter, struct ndis_802_11_var_ie *pIE)
495 {
496 	/* struct registry_priv	*pregpriv = &padapter->registrypriv; */
497 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
498 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
499 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
500 
501 	if (pmlmepriv->qospriv.qos_option == 0) {
502 		pmlmeinfo->WMM_enable = 0;
503 		return _FAIL;
504 	}
505 
506 	pmlmeinfo->WMM_enable = 1;
507 	memcpy(&pmlmeinfo->WMM_param, pIE->data + 6, sizeof(struct WMM_para_element));
508 	return true;
509 }
510 
set_acm_ctrl(struct adapter * adapter,u8 acm_mask)511 static void set_acm_ctrl(struct adapter *adapter, u8 acm_mask)
512 {
513 	u8 acmctrl;
514 	int res = rtw_read8(adapter, REG_ACMHWCTRL, &acmctrl);
515 
516 	if (res)
517 		return;
518 
519 	if (acm_mask > 1)
520 		acmctrl = acmctrl | 0x1;
521 
522 	if (acm_mask & BIT(3))
523 		acmctrl |= ACMHW_VOQEN;
524 	else
525 		acmctrl &= (~ACMHW_VOQEN);
526 
527 	if (acm_mask & BIT(2))
528 		acmctrl |= ACMHW_VIQEN;
529 	else
530 		acmctrl &= (~ACMHW_VIQEN);
531 
532 	if (acm_mask & BIT(1))
533 		acmctrl |= ACMHW_BEQEN;
534 	else
535 		acmctrl &= (~ACMHW_BEQEN);
536 
537 	rtw_write8(adapter, REG_ACMHWCTRL, acmctrl);
538 }
539 
WMMOnAssocRsp(struct adapter * padapter)540 void WMMOnAssocRsp(struct adapter *padapter)
541 {
542 	u8	ACI, ACM, AIFS, ECWMin, ECWMax, aSifsTime;
543 	u8	acm_mask;
544 	u16	TXOP;
545 	u32	acParm, i;
546 	u32	edca[4], inx[4];
547 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
548 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
549 	struct xmit_priv		*pxmitpriv = &padapter->xmitpriv;
550 	struct registry_priv	*pregpriv = &padapter->registrypriv;
551 	struct hal_data_8188e *haldata = &padapter->haldata;
552 
553 	if (pmlmeinfo->WMM_enable == 0) {
554 		padapter->mlmepriv.acm_mask = 0;
555 		return;
556 	}
557 
558 	acm_mask = 0;
559 
560 	if (pmlmeext->cur_wireless_mode == WIRELESS_11B)
561 		aSifsTime = 10;
562 	else
563 		aSifsTime = 16;
564 
565 	for (i = 0; i < 4; i++) {
566 		ACI = (pmlmeinfo->WMM_param.ac_param[i].ACI_AIFSN >> 5) & 0x03;
567 		ACM = (pmlmeinfo->WMM_param.ac_param[i].ACI_AIFSN >> 4) & 0x01;
568 
569 		/* AIFS = AIFSN * slot time + SIFS - r2t phy delay */
570 		AIFS = (pmlmeinfo->WMM_param.ac_param[i].ACI_AIFSN & 0x0f) * pmlmeinfo->slotTime + aSifsTime;
571 
572 		ECWMin = (pmlmeinfo->WMM_param.ac_param[i].CW & 0x0f);
573 		ECWMax = (pmlmeinfo->WMM_param.ac_param[i].CW & 0xf0) >> 4;
574 		TXOP = le16_to_cpu(pmlmeinfo->WMM_param.ac_param[i].TXOP_limit);
575 
576 		acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
577 
578 		switch (ACI) {
579 		case 0x0:
580 			haldata->AcParam_BE = acParm;
581 			rtw_write32(padapter, REG_EDCA_BE_PARAM, acParm);
582 			acm_mask |= (ACM ? BIT(1) : 0);
583 			edca[XMIT_BE_QUEUE] = acParm;
584 			break;
585 		case 0x1:
586 			rtw_write32(padapter, REG_EDCA_BK_PARAM, acParm);
587 			edca[XMIT_BK_QUEUE] = acParm;
588 			break;
589 		case 0x2:
590 			rtw_write32(padapter, REG_EDCA_VI_PARAM, acParm);
591 			acm_mask |= (ACM ? BIT(2) : 0);
592 			edca[XMIT_VI_QUEUE] = acParm;
593 			break;
594 		case 0x3:
595 			rtw_write32(padapter, REG_EDCA_VO_PARAM, acParm);
596 			acm_mask |= (ACM ? BIT(3) : 0);
597 			edca[XMIT_VO_QUEUE] = acParm;
598 			break;
599 		}
600 	}
601 
602 	if (padapter->registrypriv.acm_method == 1)
603 		set_acm_ctrl(padapter, acm_mask);
604 	else
605 		padapter->mlmepriv.acm_mask = acm_mask;
606 
607 	inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
608 
609 	if (pregpriv->wifi_spec == 1) {
610 		u32 j, change_inx = false;
611 
612 		/* entry indx: 0->vo, 1->vi, 2->be, 3->bk. */
613 		for (i = 0; i < 4; i++) {
614 			for (j = i + 1; j < 4; j++) {
615 				/* compare CW and AIFS */
616 				if ((edca[j] & 0xFFFF) < (edca[i] & 0xFFFF)) {
617 					change_inx = true;
618 				} else if ((edca[j] & 0xFFFF) == (edca[i] & 0xFFFF)) {
619 					/* compare TXOP */
620 					if ((edca[j] >> 16) > (edca[i] >> 16))
621 						change_inx = true;
622 				}
623 
624 				if (change_inx) {
625 					swap(edca[i], edca[j]);
626 					swap(inx[i], inx[j]);
627 
628 					change_inx = false;
629 				}
630 			}
631 		}
632 	}
633 
634 	for (i = 0; i < 4; i++)
635 		pxmitpriv->wmm_para_seq[i] = inx[i];
636 }
637 
bwmode_update_check(struct adapter * padapter,struct ndis_802_11_var_ie * pIE)638 static void bwmode_update_check(struct adapter *padapter, struct ndis_802_11_var_ie *pIE)
639 {
640 	unsigned char	 new_bwmode;
641 	unsigned char  new_ch_offset;
642 	struct HT_info_element	 *pHT_info;
643 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
644 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
645 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
646 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
647 	struct ht_priv			*phtpriv = &pmlmepriv->htpriv;
648 
649 	if (!pIE)
650 		return;
651 
652 	if (!phtpriv)
653 		return;
654 
655 	if (pIE->Length > sizeof(struct HT_info_element))
656 		return;
657 
658 	pHT_info = (struct HT_info_element *)pIE->data;
659 
660 	if ((pHT_info->infos[0] & BIT(2)) && pregistrypriv->cbw40_enable) {
661 		new_bwmode = HT_CHANNEL_WIDTH_40;
662 
663 		switch (pHT_info->infos[0] & 0x3) {
664 		case 1:
665 			new_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
666 			break;
667 		case 3:
668 			new_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
669 			break;
670 		default:
671 			new_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
672 			break;
673 		}
674 	} else {
675 		new_bwmode = HT_CHANNEL_WIDTH_20;
676 		new_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
677 	}
678 
679 	if ((new_bwmode != pmlmeext->cur_bwmode) ||
680 	    (new_ch_offset != pmlmeext->cur_ch_offset)) {
681 		pmlmeinfo->bwmode_updated = true;
682 
683 		pmlmeext->cur_bwmode = new_bwmode;
684 		pmlmeext->cur_ch_offset = new_ch_offset;
685 
686 		/* update HT info also */
687 		HT_info_handler(padapter, pIE);
688 	} else {
689 		pmlmeinfo->bwmode_updated = false;
690 	}
691 
692 	if (pmlmeinfo->bwmode_updated) {
693 		struct sta_info *psta;
694 		struct wlan_bssid_ex	*cur_network = &pmlmeinfo->network;
695 		struct sta_priv	*pstapriv = &padapter->stapriv;
696 
697 		/* set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode); */
698 
699 		/* update ap's stainfo */
700 		psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress);
701 		if (psta) {
702 			struct ht_priv	*phtpriv_sta = &psta->htpriv;
703 
704 			if (phtpriv_sta->ht_option) {
705 				/*  bwmode */
706 				phtpriv_sta->bwmode = pmlmeext->cur_bwmode;
707 				phtpriv_sta->ch_offset = pmlmeext->cur_ch_offset;
708 			} else {
709 				phtpriv_sta->bwmode = HT_CHANNEL_WIDTH_20;
710 				phtpriv_sta->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
711 			}
712 		}
713 	}
714 }
715 
HT_caps_handler(struct adapter * padapter,struct ndis_802_11_var_ie * pIE)716 void HT_caps_handler(struct adapter *padapter, struct ndis_802_11_var_ie *pIE)
717 {
718 	unsigned int	i;
719 	u8	max_AMPDU_len, min_MPDU_spacing;
720 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
721 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
722 	struct mlme_priv		*pmlmepriv = &padapter->mlmepriv;
723 	struct ht_priv			*phtpriv = &pmlmepriv->htpriv;
724 
725 	if (!pIE)
726 		return;
727 
728 	if (!phtpriv->ht_option)
729 		return;
730 
731 	pmlmeinfo->HT_caps_enable = 1;
732 
733 	for (i = 0; i < (pIE->Length); i++) {
734 		if (i != 2) {
735 			/* 	Got the endian issue here. */
736 			pmlmeinfo->HT_caps.u.HT_cap[i] &= (pIE->data[i]);
737 		} else {
738 			/* modify from  fw by Thomas 2010/11/17 */
739 			max_AMPDU_len = min(pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x3,
740 					    pIE->data[i] & 0x3);
741 
742 			min_MPDU_spacing = max(pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c,
743 					       pIE->data[i] & 0x1c);
744 
745 			pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para = max_AMPDU_len | min_MPDU_spacing;
746 		}
747 	}
748 
749 	/* update the MCS rates */
750 	for (i = 0; i < 16; i++)
751 		pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate[i] &= MCS_rate_1R[i];
752 }
753 
HT_info_handler(struct adapter * padapter,struct ndis_802_11_var_ie * pIE)754 void HT_info_handler(struct adapter *padapter, struct ndis_802_11_var_ie *pIE)
755 {
756 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
757 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
758 	struct mlme_priv		*pmlmepriv = &padapter->mlmepriv;
759 	struct ht_priv			*phtpriv = &pmlmepriv->htpriv;
760 
761 	if (!pIE)
762 		return;
763 
764 	if (!phtpriv->ht_option)
765 		return;
766 
767 	if (pIE->Length > sizeof(struct HT_info_element))
768 		return;
769 
770 	pmlmeinfo->HT_info_enable = 1;
771 	memcpy(&pmlmeinfo->HT_info, pIE->data, pIE->Length);
772 }
773 
set_min_ampdu_spacing(struct adapter * adapter,u8 spacing)774 static void set_min_ampdu_spacing(struct adapter *adapter, u8 spacing)
775 {
776 	u8 sec_spacing;
777 	int res;
778 
779 	if (spacing <= 7) {
780 		switch (adapter->securitypriv.dot11PrivacyAlgrthm) {
781 		case _NO_PRIVACY_:
782 		case _AES_:
783 			sec_spacing = 0;
784 			break;
785 		case _WEP40_:
786 		case _WEP104_:
787 		case _TKIP_:
788 		case _TKIP_WTMIC_:
789 			sec_spacing = 6;
790 			break;
791 		default:
792 			sec_spacing = 7;
793 			break;
794 		}
795 
796 		if (spacing < sec_spacing)
797 			spacing = sec_spacing;
798 
799 		res = rtw_read8(adapter, REG_AMPDU_MIN_SPACE, &sec_spacing);
800 		if (res)
801 			return;
802 
803 		rtw_write8(adapter, REG_AMPDU_MIN_SPACE,
804 			   (sec_spacing & 0xf8) | spacing);
805 	}
806 }
807 
HTOnAssocRsp(struct adapter * padapter)808 void HTOnAssocRsp(struct adapter *padapter)
809 {
810 	unsigned char		max_AMPDU_len;
811 	unsigned char		min_MPDU_spacing;
812 	/* struct registry_priv	 *pregpriv = &padapter->registrypriv; */
813 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
814 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
815 
816 	if ((pmlmeinfo->HT_info_enable) && (pmlmeinfo->HT_caps_enable)) {
817 		pmlmeinfo->HT_enable = 1;
818 	} else {
819 		pmlmeinfo->HT_enable = 0;
820 		return;
821 	}
822 
823 	/* handle A-MPDU parameter field */
824 	/*
825 		AMPDU_para [1:0]:Max AMPDU Len => 0:8k , 1:16k, 2:32k, 3:64k
826 		AMPDU_para [4:2]:Min MPDU Start Spacing
827 	*/
828 	max_AMPDU_len = pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x03;
829 
830 	min_MPDU_spacing = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c) >> 2;
831 
832 	set_min_ampdu_spacing(padapter, min_MPDU_spacing);
833 
834 	SetHwReg8188EU(padapter, HW_VAR_AMPDU_FACTOR, (u8 *)(&max_AMPDU_len));
835 }
836 
ERP_IE_handler(struct adapter * padapter,struct ndis_802_11_var_ie * pIE)837 void ERP_IE_handler(struct adapter *padapter, struct ndis_802_11_var_ie *pIE)
838 {
839 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
840 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
841 
842 	if (pIE->Length > 1)
843 		return;
844 
845 	pmlmeinfo->ERP_enable = 1;
846 	memcpy(&pmlmeinfo->ERP_IE, pIE->data, pIE->Length);
847 }
848 
VCS_update(struct adapter * padapter,struct sta_info * psta)849 void VCS_update(struct adapter *padapter, struct sta_info *psta)
850 {
851 	struct registry_priv	 *pregpriv = &padapter->registrypriv;
852 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
853 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
854 
855 	switch (pregpriv->vrtl_carrier_sense) { /* 0:off 1:on 2:auto */
856 	case 0: /* off */
857 		psta->rtsen = 0;
858 		psta->cts2self = 0;
859 		break;
860 	case 1: /* on */
861 		if (pregpriv->vcs_type == 1) { /* 1:RTS/CTS 2:CTS to self */
862 			psta->rtsen = 1;
863 			psta->cts2self = 0;
864 		} else {
865 			psta->rtsen = 0;
866 			psta->cts2self = 1;
867 		}
868 		break;
869 	case 2: /* auto */
870 	default:
871 		if ((pmlmeinfo->ERP_enable) && (pmlmeinfo->ERP_IE & BIT(1))) {
872 			if (pregpriv->vcs_type == 1) {
873 				psta->rtsen = 1;
874 				psta->cts2self = 0;
875 			} else {
876 				psta->rtsen = 0;
877 				psta->cts2self = 1;
878 			}
879 		} else {
880 			psta->rtsen = 0;
881 			psta->cts2self = 0;
882 		}
883 		break;
884 	}
885 }
886 
rtw_check_bcn_info(struct adapter * Adapter,u8 * pframe,u32 packet_len)887 int rtw_check_bcn_info(struct adapter  *Adapter, u8 *pframe, u32 packet_len)
888 {
889 	unsigned int		len;
890 	unsigned char		*p;
891 	unsigned short	val16, subtype;
892 	struct wlan_network *cur_network = &Adapter->mlmepriv.cur_network;
893 	/* u8 wpa_ie[255], rsn_ie[255]; */
894 	u16 wpa_len = 0, rsn_len = 0;
895 	u8 encryp_protocol = 0;
896 	struct wlan_bssid_ex *bssid;
897 	int group_cipher = 0, pairwise_cipher = 0, is_8021x = 0;
898 	unsigned char *pbuf;
899 	u32 wpa_ielen = 0;
900 	u8 *pbssid = GetAddr3Ptr(pframe);
901 	u32 hidden_ssid = 0;
902 	struct HT_info_element *pht_info = NULL;
903 	struct ieee80211_ht_cap *pht_cap = NULL;
904 	u32 bcn_channel;
905 	unsigned short	ht_cap_info;
906 	unsigned char	ht_info_infos_0;
907 
908 	if (!is_client_associated_to_ap(Adapter))
909 		return true;
910 
911 	len = packet_len - sizeof(struct ieee80211_hdr_3addr);
912 
913 	if (len > MAX_IE_SZ)
914 		return _FAIL;
915 
916 	if (memcmp(cur_network->network.MacAddress, pbssid, 6))
917 		return true;
918 
919 	bssid = kzalloc(sizeof(struct wlan_bssid_ex), GFP_ATOMIC);
920 	if (!bssid)
921 		return _FAIL;
922 
923 	subtype = GetFrameSubType(pframe) >> 4;
924 
925 	if (subtype == WIFI_BEACON)
926 		bssid->Reserved[0] = 1;
927 
928 	bssid->Length = sizeof(struct wlan_bssid_ex) - MAX_IE_SZ + len;
929 
930 	/* below is to copy the information element */
931 	bssid->IELength = len;
932 	memcpy(bssid->IEs, (pframe + sizeof(struct ieee80211_hdr_3addr)), bssid->IELength);
933 
934 	/* check bw and channel offset */
935 	/* parsing HT_CAP_IE */
936 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
937 	if (p && len > 0) {
938 		pht_cap = (struct ieee80211_ht_cap *)(p + 2);
939 		ht_cap_info = le16_to_cpu(pht_cap->cap_info);
940 	} else {
941 		ht_cap_info = 0;
942 	}
943 	/* parsing HT_INFO_IE */
944 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
945 	if (p && len > 0) {
946 			pht_info = (struct HT_info_element *)(p + 2);
947 			ht_info_infos_0 = pht_info->infos[0];
948 	} else {
949 			ht_info_infos_0 = 0;
950 	}
951 	if (ht_cap_info != cur_network->BcnInfo.ht_cap_info ||
952 	    ((ht_info_infos_0 & 0x03) != (cur_network->BcnInfo.ht_info_infos_0 & 0x03))) {
953 			/* bcn_info_update */
954 			cur_network->BcnInfo.ht_cap_info = ht_cap_info;
955 			cur_network->BcnInfo.ht_info_infos_0 = ht_info_infos_0;
956 			/* to do : need to check that whether modify related register of BB or not */
957 			/* goto _mismatch; */
958 	}
959 
960 	/* Checking for channel */
961 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _DSSET_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
962 	if (p) {
963 			bcn_channel = *(p + 2);
964 	} else {/* In 5G, some ap do not have DSSET IE checking HT info for channel */
965 			p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
966 			if (pht_info)
967 				bcn_channel = pht_info->primary_channel;
968 			else /* we don't find channel IE, so don't check it */
969 				bcn_channel = Adapter->mlmeextpriv.cur_channel;
970 	}
971 	if (bcn_channel != Adapter->mlmeextpriv.cur_channel)
972 		goto _mismatch;
973 
974 	/* checking SSID */
975 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _SSID_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
976 	if (!p)
977 		hidden_ssid = true;
978 	else
979 		hidden_ssid = false;
980 
981 	if (p && (!hidden_ssid && (*(p + 1)))) {
982 		memcpy(bssid->Ssid.Ssid, (p + 2), *(p + 1));
983 		bssid->Ssid.SsidLength = *(p + 1);
984 	} else {
985 		bssid->Ssid.SsidLength = 0;
986 		bssid->Ssid.Ssid[0] = '\0';
987 	}
988 
989 	if (memcmp(bssid->Ssid.Ssid, cur_network->network.Ssid.Ssid, 32) ||
990 	    bssid->Ssid.SsidLength != cur_network->network.Ssid.SsidLength) {
991 		/* not hidden ssid */
992 		if (bssid->Ssid.Ssid[0] != '\0' && bssid->Ssid.SsidLength != 0)
993 			goto _mismatch;
994 	}
995 
996 	/* check encryption info */
997 	val16 = rtw_get_capability((struct wlan_bssid_ex *)bssid);
998 
999 	if (val16 & BIT(4))
1000 		bssid->Privacy = 1;
1001 	else
1002 		bssid->Privacy = 0;
1003 
1004 	if (cur_network->network.Privacy != bssid->Privacy)
1005 		goto _mismatch;
1006 
1007 	rtw_get_sec_ie(bssid->IEs, bssid->IELength, NULL, &rsn_len, NULL, &wpa_len);
1008 
1009 	if (rsn_len > 0) {
1010 		encryp_protocol = ENCRYP_PROTOCOL_WPA2;
1011 	} else if (wpa_len > 0) {
1012 		encryp_protocol = ENCRYP_PROTOCOL_WPA;
1013 	} else {
1014 		if (bssid->Privacy)
1015 			encryp_protocol = ENCRYP_PROTOCOL_WEP;
1016 	}
1017 
1018 	if (cur_network->BcnInfo.encryp_protocol != encryp_protocol)
1019 		goto _mismatch;
1020 
1021 	if (encryp_protocol == ENCRYP_PROTOCOL_WPA || encryp_protocol == ENCRYP_PROTOCOL_WPA2) {
1022 		pbuf = rtw_get_wpa_ie(&bssid->IEs[12], &wpa_ielen, bssid->IELength - 12);
1023 		if (pbuf && (wpa_ielen > 0)) {
1024 			rtw_parse_wpa_ie(pbuf, wpa_ielen + 2, &group_cipher, &pairwise_cipher, &is_8021x);
1025 		} else {
1026 			pbuf = rtw_get_wpa2_ie(&bssid->IEs[12], &wpa_ielen, bssid->IELength - 12);
1027 
1028 			if (pbuf && (wpa_ielen > 0))
1029 				rtw_parse_wpa2_ie(pbuf, wpa_ielen + 2, &group_cipher, &pairwise_cipher, &is_8021x);
1030 		}
1031 
1032 		if (pairwise_cipher != cur_network->BcnInfo.pairwise_cipher ||
1033 		    group_cipher != cur_network->BcnInfo.group_cipher)
1034 			goto _mismatch;
1035 
1036 		if (is_8021x != cur_network->BcnInfo.is_8021x)
1037 			goto _mismatch;
1038 	}
1039 
1040 	kfree(bssid);
1041 
1042 	return _SUCCESS;
1043 
1044 _mismatch:
1045 	kfree(bssid);
1046 
1047 	return _FAIL;
1048 }
1049 
update_beacon_info(struct adapter * padapter,u8 * pframe,uint pkt_len,struct sta_info * psta)1050 void update_beacon_info(struct adapter *padapter, u8 *pframe, uint pkt_len, struct sta_info *psta)
1051 {
1052 	unsigned int i;
1053 	unsigned int len;
1054 	struct ndis_802_11_var_ie *pIE;
1055 
1056 	len = pkt_len - (_BEACON_IE_OFFSET_ + WLAN_HDR_A3_LEN);
1057 
1058 	for (i = 0; i < len;) {
1059 		pIE = (struct ndis_802_11_var_ie *)(pframe + (_BEACON_IE_OFFSET_ + WLAN_HDR_A3_LEN) + i);
1060 
1061 		switch (pIE->ElementID) {
1062 		case _HT_EXTRA_INFO_IE_:	/* HT info */
1063 			/* HT_info_handler(padapter, pIE); */
1064 			bwmode_update_check(padapter, pIE);
1065 			break;
1066 		case _ERPINFO_IE_:
1067 			ERP_IE_handler(padapter, pIE);
1068 			VCS_update(padapter, psta);
1069 			break;
1070 		default:
1071 			break;
1072 		}
1073 
1074 		i += (pIE->Length + 2);
1075 	}
1076 }
1077 
is_ap_in_tkip(struct adapter * padapter)1078 bool is_ap_in_tkip(struct adapter *padapter)
1079 {
1080 	u32 i;
1081 	struct ndis_802_11_var_ie *pIE;
1082 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1083 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1084 	struct wlan_bssid_ex		*cur_network = &pmlmeinfo->network;
1085 
1086 	if (rtw_get_capability((struct wlan_bssid_ex *)cur_network) & WLAN_CAPABILITY_PRIVACY) {
1087 		for (i = sizeof(struct ndis_802_11_fixed_ie); i < pmlmeinfo->network.IELength;) {
1088 			pIE = (struct ndis_802_11_var_ie *)(pmlmeinfo->network.IEs + i);
1089 
1090 			switch (pIE->ElementID) {
1091 			case _VENDOR_SPECIFIC_IE_:
1092 				if ((!memcmp(pIE->data, RTW_WPA_OUI, 4)) && (!memcmp((pIE->data + 12), WPA_TKIP_CIPHER, 4)))
1093 					return true;
1094 				break;
1095 			case _RSN_IE_2_:
1096 				if (!memcmp((pIE->data + 8), RSN_TKIP_CIPHER, 4))
1097 					return true;
1098 				break;
1099 			default:
1100 				break;
1101 			}
1102 
1103 			i += (pIE->Length + 2);
1104 		}
1105 		return false;
1106 	} else {
1107 		return false;
1108 	}
1109 }
1110 
wifirate2_ratetbl_inx(unsigned char rate)1111 int wifirate2_ratetbl_inx(unsigned char rate)
1112 {
1113 	int	inx = 0;
1114 	rate = rate & 0x7f;
1115 
1116 	switch (rate) {
1117 	case 54 * 2:
1118 		inx = 11;
1119 		break;
1120 	case 48 * 2:
1121 		inx = 10;
1122 		break;
1123 	case 36 * 2:
1124 		inx = 9;
1125 		break;
1126 	case 24 * 2:
1127 		inx = 8;
1128 		break;
1129 	case 18 * 2:
1130 		inx = 7;
1131 		break;
1132 	case 12 * 2:
1133 		inx = 6;
1134 		break;
1135 	case 9 * 2:
1136 		inx = 5;
1137 		break;
1138 	case 6 * 2:
1139 		inx = 4;
1140 		break;
1141 	case 11 * 2:
1142 		inx = 3;
1143 		break;
1144 	case 11:
1145 		inx = 2;
1146 		break;
1147 	case 2 * 2:
1148 		inx = 1;
1149 		break;
1150 	case 1 * 2:
1151 		inx = 0;
1152 		break;
1153 	}
1154 	return inx;
1155 }
1156 
update_basic_rate(unsigned char * ptn,unsigned int ptn_sz)1157 unsigned int update_basic_rate(unsigned char *ptn, unsigned int ptn_sz)
1158 {
1159 	unsigned int i, num_of_rate;
1160 	unsigned int mask = 0;
1161 
1162 	num_of_rate = (ptn_sz > NumRates) ? NumRates : ptn_sz;
1163 
1164 	for (i = 0; i < num_of_rate; i++) {
1165 		if ((*(ptn + i)) & 0x80)
1166 			mask |= 0x1 << wifirate2_ratetbl_inx(*(ptn + i));
1167 	}
1168 	return mask;
1169 }
1170 
update_supported_rate(unsigned char * ptn,unsigned int ptn_sz)1171 unsigned int update_supported_rate(unsigned char *ptn, unsigned int ptn_sz)
1172 {
1173 	unsigned int i, num_of_rate;
1174 	unsigned int mask = 0;
1175 
1176 	num_of_rate = (ptn_sz > NumRates) ? NumRates : ptn_sz;
1177 
1178 	for (i = 0; i < num_of_rate; i++)
1179 		mask |= 0x1 << wifirate2_ratetbl_inx(*(ptn + i));
1180 	return mask;
1181 }
1182 
update_MSC_rate(struct HT_caps_element * pHT_caps)1183 unsigned int update_MSC_rate(struct HT_caps_element *pHT_caps)
1184 {
1185 	unsigned int mask = 0;
1186 
1187 	mask = ((pHT_caps->u.HT_cap_element.MCS_rate[0] << 12) | (pHT_caps->u.HT_cap_element.MCS_rate[1] << 20));
1188 
1189 	return mask;
1190 }
1191 
support_short_GI(struct adapter * padapter,struct HT_caps_element * pHT_caps)1192 int support_short_GI(struct adapter *padapter, struct HT_caps_element *pHT_caps)
1193 {
1194 	unsigned char					bit_offset;
1195 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
1196 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1197 
1198 	if (!(pmlmeinfo->HT_enable))
1199 		return _FAIL;
1200 
1201 	if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_RALINK)
1202 		return _FAIL;
1203 
1204 	bit_offset = (pmlmeext->cur_bwmode & HT_CHANNEL_WIDTH_40) ? 6 : 5;
1205 
1206 	if (__le16_to_cpu(pHT_caps->u.HT_cap_element.HT_caps_info) & (0x1 << bit_offset))
1207 		return _SUCCESS;
1208 	else
1209 		return _FAIL;
1210 }
1211 
get_highest_rate_idx(u32 mask)1212 unsigned char get_highest_rate_idx(u32 mask)
1213 {
1214 	int i;
1215 	unsigned char rate_idx = 0;
1216 
1217 	for (i = 27; i >= 0; i--) {
1218 		if (mask & BIT(i)) {
1219 			rate_idx = i;
1220 			break;
1221 		}
1222 	}
1223 	return rate_idx;
1224 }
1225 
Update_RA_Entry(struct adapter * padapter,u32 mac_id)1226 void Update_RA_Entry(struct adapter *padapter, u32 mac_id)
1227 {
1228 	rtw_hal_update_ra_mask(padapter, mac_id, 0);
1229 }
1230 
enable_rate_adaptive(struct adapter * padapter,u32 mac_id)1231 static void enable_rate_adaptive(struct adapter *padapter, u32 mac_id)
1232 {
1233 	Update_RA_Entry(padapter, mac_id);
1234 }
1235 
set_sta_rate(struct adapter * padapter,struct sta_info * psta)1236 void set_sta_rate(struct adapter *padapter, struct sta_info *psta)
1237 {
1238 	/* rate adaptive */
1239 	enable_rate_adaptive(padapter, psta->mac_id);
1240 }
1241 
1242 /*  Update RRSR and Rate for USERATE */
update_tx_basic_rate(struct adapter * padapter,u8 wirelessmode)1243 void update_tx_basic_rate(struct adapter *padapter, u8 wirelessmode)
1244 {
1245 	unsigned char supported_rates[NDIS_802_11_LENGTH_RATES_EX];
1246 	struct wifidirect_info *pwdinfo = &padapter->wdinfo;
1247 
1248 	/* 	Added by Albert 2011/03/22 */
1249 	/* 	In the P2P mode, the driver should not support the b mode. */
1250 	/* 	So, the Tx packet shouldn't use the CCK rate */
1251 	if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE))
1252 		return;
1253 	memset(supported_rates, 0, NDIS_802_11_LENGTH_RATES_EX);
1254 
1255 	if ((wirelessmode & WIRELESS_11B) && (wirelessmode == WIRELESS_11B))
1256 		memcpy(supported_rates, rtw_basic_rate_cck, 4);
1257 	else if (wirelessmode & WIRELESS_11B)
1258 		memcpy(supported_rates, rtw_basic_rate_mix, 7);
1259 	else
1260 		memcpy(supported_rates, rtw_basic_rate_ofdm, 3);
1261 
1262 	if (wirelessmode & WIRELESS_11B)
1263 		update_mgnt_tx_rate(padapter, IEEE80211_CCK_RATE_1MB);
1264 	else
1265 		update_mgnt_tx_rate(padapter, IEEE80211_OFDM_RATE_6MB);
1266 
1267 	SetHwReg8188EU(padapter, HW_VAR_BASIC_RATE, supported_rates);
1268 }
1269 
check_assoc_AP(u8 * pframe,uint len)1270 unsigned char check_assoc_AP(u8 *pframe, uint len)
1271 {
1272 	unsigned int i;
1273 	struct ndis_802_11_var_ie *pIE;
1274 	u8	epigram_vendor_flag;
1275 	u8	ralink_vendor_flag;
1276 	epigram_vendor_flag = 0;
1277 	ralink_vendor_flag = 0;
1278 
1279 	for (i = sizeof(struct ndis_802_11_fixed_ie); i < len;) {
1280 		pIE = (struct ndis_802_11_var_ie *)(pframe + i);
1281 
1282 		switch (pIE->ElementID) {
1283 		case _VENDOR_SPECIFIC_IE_:
1284 			if ((!memcmp(pIE->data, ARTHEROS_OUI1, 3)) ||
1285 			    (!memcmp(pIE->data, ARTHEROS_OUI2, 3))) {
1286 				return HT_IOT_PEER_ATHEROS;
1287 			} else if ((!memcmp(pIE->data, BROADCOM_OUI1, 3)) ||
1288 				   (!memcmp(pIE->data, BROADCOM_OUI2, 3))) {
1289 				return HT_IOT_PEER_BROADCOM;
1290 			} else if (!memcmp(pIE->data, MARVELL_OUI, 3)) {
1291 				return HT_IOT_PEER_MARVELL;
1292 			} else if (!memcmp(pIE->data, RALINK_OUI, 3)) {
1293 				if (!ralink_vendor_flag) {
1294 					ralink_vendor_flag = 1;
1295 				} else {
1296 					return HT_IOT_PEER_RALINK;
1297 				}
1298 			} else if (!memcmp(pIE->data, CISCO_OUI, 3)) {
1299 				return HT_IOT_PEER_CISCO;
1300 			} else if (!memcmp(pIE->data, REALTEK_OUI, 3)) {
1301 				return HT_IOT_PEER_REALTEK;
1302 			} else if (!memcmp(pIE->data, AIRGOCAP_OUI, 3)) {
1303 				return HT_IOT_PEER_AIRGO;
1304 			} else if (!memcmp(pIE->data, EPIGRAM_OUI, 3)) {
1305 				epigram_vendor_flag = 1;
1306 				if (ralink_vendor_flag)
1307 					return HT_IOT_PEER_TENDA;
1308 			} else {
1309 				break;
1310 			}
1311 			break;
1312 
1313 		default:
1314 			break;
1315 		}
1316 		i += (pIE->Length + 2);
1317 	}
1318 
1319 	if (ralink_vendor_flag && !epigram_vendor_flag)
1320 		return HT_IOT_PEER_RALINK;
1321 	else if (ralink_vendor_flag && epigram_vendor_flag)
1322 		return HT_IOT_PEER_TENDA;
1323 	else
1324 		return HT_IOT_PEER_UNKNOWN;
1325 }
1326 
update_IOT_info(struct adapter * padapter)1327 void update_IOT_info(struct adapter *padapter)
1328 {
1329 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
1330 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1331 
1332 	switch (pmlmeinfo->assoc_AP_vendor) {
1333 	case HT_IOT_PEER_MARVELL:
1334 		pmlmeinfo->turboMode_cts2self = 1;
1335 		pmlmeinfo->turboMode_rtsen = 0;
1336 		break;
1337 	case HT_IOT_PEER_RALINK:
1338 		pmlmeinfo->turboMode_cts2self = 0;
1339 		pmlmeinfo->turboMode_rtsen = 1;
1340 		break;
1341 	case HT_IOT_PEER_REALTEK:
1342 		/* rtw_write16(padapter, 0x4cc, 0xffff); */
1343 		/* rtw_write16(padapter, 0x546, 0x01c0); */
1344 		break;
1345 	default:
1346 		pmlmeinfo->turboMode_cts2self = 0;
1347 		pmlmeinfo->turboMode_rtsen = 1;
1348 		break;
1349 	}
1350 }
1351 
set_ack_preamble(struct adapter * adapter,bool short_preamble)1352 static void set_ack_preamble(struct adapter *adapter, bool short_preamble)
1353 {
1354 	struct hal_data_8188e *haldata = &adapter->haldata;
1355 	u8 val8;
1356 
1357 	/*  Joseph marked out for Netgear 3500 TKIP channel 7 issue.(Temporarily) */
1358 	val8 = haldata->nCur40MhzPrimeSC << 5;
1359 	if (short_preamble)
1360 		val8 |= 0x80;
1361 
1362 	rtw_write8(adapter, REG_RRSR + 2, val8);
1363 };
1364 
update_capinfo(struct adapter * Adapter,u16 updateCap)1365 void update_capinfo(struct adapter *Adapter, u16 updateCap)
1366 {
1367 	struct mlme_ext_priv	*pmlmeext = &Adapter->mlmeextpriv;
1368 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1369 
1370 	/*  Check preamble mode, 2005.01.06, by rcnjko. */
1371 	/*  Mark to update preamble value forever, 2008.03.18 by lanhsin */
1372 
1373 	if (updateCap & cShortPreamble) { /*  Short Preamble */
1374 		if (pmlmeinfo->preamble_mode != PREAMBLE_SHORT) { /*  PREAMBLE_LONG or PREAMBLE_AUTO */
1375 			pmlmeinfo->preamble_mode = PREAMBLE_SHORT;
1376 			set_ack_preamble(Adapter, true);
1377 		}
1378 	} else { /*  Long Preamble */
1379 		if (pmlmeinfo->preamble_mode != PREAMBLE_LONG) {  /*  PREAMBLE_SHORT or PREAMBLE_AUTO */
1380 			pmlmeinfo->preamble_mode = PREAMBLE_LONG;
1381 			set_ack_preamble(Adapter, false);
1382 		}
1383 	}
1384 
1385 	if (updateCap & cIBSS) {
1386 		/* Filen: See 802.11-2007 p.91 */
1387 		pmlmeinfo->slotTime = NON_SHORT_SLOT_TIME;
1388 	} else { /* Filen: See 802.11-2007 p.90 */
1389 		if (pmlmeext->cur_wireless_mode & (WIRELESS_11G | WIRELESS_11_24N)) {
1390 			if (updateCap & cShortSlotTime) { /*  Short Slot Time */
1391 				if (pmlmeinfo->slotTime != SHORT_SLOT_TIME)
1392 					pmlmeinfo->slotTime = SHORT_SLOT_TIME;
1393 			} else { /*  Long Slot Time */
1394 				if (pmlmeinfo->slotTime != NON_SHORT_SLOT_TIME)
1395 					pmlmeinfo->slotTime = NON_SHORT_SLOT_TIME;
1396 			}
1397 		} else {
1398 			/* B Mode */
1399 			pmlmeinfo->slotTime = NON_SHORT_SLOT_TIME;
1400 		}
1401 	}
1402 
1403 	SetHwReg8188EU(Adapter, HW_VAR_SLOT_TIME, &pmlmeinfo->slotTime);
1404 }
1405 
update_wireless_mode(struct adapter * padapter)1406 void update_wireless_mode(struct adapter *padapter)
1407 {
1408 	int ratelen, network_type = 0;
1409 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
1410 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1411 	struct wlan_bssid_ex	*cur_network = &pmlmeinfo->network;
1412 	unsigned char		*rate = cur_network->SupportedRates;
1413 
1414 	ratelen = rtw_get_rateset_len(cur_network->SupportedRates);
1415 
1416 	if ((pmlmeinfo->HT_info_enable) && (pmlmeinfo->HT_caps_enable))
1417 		pmlmeinfo->HT_enable = 1;
1418 
1419 	if (pmlmeext->cur_channel > 14) {
1420 		network_type |= WIRELESS_INVALID;
1421 	} else {
1422 		if (pmlmeinfo->HT_enable)
1423 			network_type = WIRELESS_11_24N;
1424 
1425 		if (cckratesonly_included(rate, ratelen))
1426 			network_type |= WIRELESS_11B;
1427 		else if (cckrates_included(rate, ratelen))
1428 			network_type |= WIRELESS_11BG;
1429 		else
1430 			network_type |= WIRELESS_11G;
1431 	}
1432 
1433 	pmlmeext->cur_wireless_mode = network_type & padapter->registrypriv.wireless_mode;
1434 
1435 	/* RESP_SIFS for CCK */
1436 	rtw_write8(padapter, REG_R2T_SIFS, 0x08);
1437 	rtw_write8(padapter, REG_R2T_SIFS + 1, 0x08);
1438 	/* RESP_SIFS for OFDM */
1439 	rtw_write8(padapter, REG_T2T_SIFS, 0x0a);
1440 	rtw_write8(padapter, REG_T2T_SIFS + 1, 0x0a);
1441 
1442 	if (pmlmeext->cur_wireless_mode & WIRELESS_11B)
1443 		update_mgnt_tx_rate(padapter, IEEE80211_CCK_RATE_1MB);
1444 	 else
1445 		update_mgnt_tx_rate(padapter, IEEE80211_OFDM_RATE_6MB);
1446 }
1447 
update_bmc_sta_support_rate(struct adapter * padapter,u32 mac_id)1448 void update_bmc_sta_support_rate(struct adapter *padapter, u32 mac_id)
1449 {
1450 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
1451 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1452 
1453 	if (pmlmeext->cur_wireless_mode & WIRELESS_11B) {
1454 		/*  Only B, B/G, and B/G/N AP could use CCK rate */
1455 		memcpy((pmlmeinfo->FW_sta_info[mac_id].SupportedRates), rtw_basic_rate_cck, 4);
1456 	} else {
1457 		memcpy((pmlmeinfo->FW_sta_info[mac_id].SupportedRates), rtw_basic_rate_ofdm, 3);
1458 	}
1459 }
1460 
update_sta_support_rate(struct adapter * padapter,u8 * pvar_ie,uint var_ie_len,int cam_idx)1461 int update_sta_support_rate(struct adapter *padapter, u8 *pvar_ie, uint var_ie_len, int cam_idx)
1462 {
1463 	unsigned int	ie_len;
1464 	struct ndis_802_11_var_ie *pIE;
1465 	int	supportRateNum = 0;
1466 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
1467 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1468 
1469 	pIE = (struct ndis_802_11_var_ie *)rtw_get_ie(pvar_ie, _SUPPORTEDRATES_IE_, &ie_len, var_ie_len);
1470 	if (!pIE)
1471 		return _FAIL;
1472 
1473 	memcpy(pmlmeinfo->FW_sta_info[cam_idx].SupportedRates, pIE->data, ie_len);
1474 	supportRateNum = ie_len;
1475 
1476 	pIE = (struct ndis_802_11_var_ie *)rtw_get_ie(pvar_ie, _EXT_SUPPORTEDRATES_IE_, &ie_len, var_ie_len);
1477 	if (pIE)
1478 		memcpy((pmlmeinfo->FW_sta_info[cam_idx].SupportedRates + supportRateNum), pIE->data, ie_len);
1479 
1480 	return _SUCCESS;
1481 }
1482 
update_TSF(struct mlme_ext_priv * pmlmeext,u8 * pframe,uint len)1483 void update_TSF(struct mlme_ext_priv *pmlmeext, u8 *pframe, uint len)
1484 {
1485 	u8 *pIE;
1486 	__le32 *pbuf;
1487 
1488 	pIE = pframe + sizeof(struct ieee80211_hdr_3addr);
1489 	pbuf = (__le32 *)pIE;
1490 
1491 	pmlmeext->TSFValue = le32_to_cpu(*(pbuf + 1));
1492 
1493 	pmlmeext->TSFValue = pmlmeext->TSFValue << 32;
1494 
1495 	pmlmeext->TSFValue |= le32_to_cpu(*pbuf);
1496 }
1497 
correct_TSF(struct adapter * padapter,struct mlme_ext_priv * pmlmeext)1498 void correct_TSF(struct adapter *padapter, struct mlme_ext_priv *pmlmeext)
1499 {
1500 	SetHwReg8188EU(padapter, HW_VAR_CORRECT_TSF, NULL);
1501 }
1502 
beacon_timing_control(struct adapter * padapter)1503 void beacon_timing_control(struct adapter *padapter)
1504 {
1505 	SetBeaconRelatedRegisters8188EUsb(padapter);
1506 }
1507 
1508 static struct adapter *pbuddy_padapter;
1509 
rtw_handle_dualmac(struct adapter * adapter,bool init)1510 int rtw_handle_dualmac(struct adapter *adapter, bool init)
1511 {
1512 	int status = _SUCCESS;
1513 
1514 	if (init) {
1515 		if (!pbuddy_padapter) {
1516 			pbuddy_padapter = adapter;
1517 		} else {
1518 			adapter->pbuddy_adapter = pbuddy_padapter;
1519 			pbuddy_padapter->pbuddy_adapter = adapter;
1520 			/*  clear global value */
1521 			pbuddy_padapter = NULL;
1522 		}
1523 	} else {
1524 		pbuddy_padapter = NULL;
1525 	}
1526 	return status;
1527 }
1528