1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 #define _RTW_WLAN_UTIL_C_
16 
17 #include <drv_types.h>
18 #include <rtw_debug.h>
19 #include <hal_com_h2c.h>
20 
21 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
22 #include <linux/inetdevice.h>
23 #endif
24 
25 static unsigned char ARTHEROS_OUI1[] = {0x00, 0x03, 0x7f};
26 static unsigned char ARTHEROS_OUI2[] = {0x00, 0x13, 0x74};
27 
28 static unsigned char BROADCOM_OUI1[] = {0x00, 0x10, 0x18};
29 static unsigned char BROADCOM_OUI2[] = {0x00, 0x0a, 0xf7};
30 static unsigned char BROADCOM_OUI3[] = {0x00, 0x05, 0xb5};
31 
32 static unsigned char CISCO_OUI[] = {0x00, 0x40, 0x96};
33 static unsigned char MARVELL_OUI[] = {0x00, 0x50, 0x43};
34 static unsigned char RALINK_OUI[] = {0x00, 0x0c, 0x43};
35 static unsigned char REALTEK_OUI[] = {0x00, 0xe0, 0x4c};
36 static unsigned char AIRGOCAP_OUI[] = {0x00, 0x0a, 0xf5};
37 static unsigned char RSN_TKIP_CIPHER[4] = {0x00, 0x0f, 0xac, 0x02};
38 static unsigned char WPA_TKIP_CIPHER[4] = {0x00, 0x50, 0xf2, 0x02};
39 
40 extern unsigned char RTW_WPA_OUI[];
41 extern unsigned char WPA_TKIP_CIPHER[4];
42 
43 #define R2T_PHY_DELAY	(0)
44 
45 /* define WAIT_FOR_BCN_TO_MIN	(3000) */
46 #define WAIT_FOR_BCN_TO_MIN	(6000)
47 #define WAIT_FOR_BCN_TO_MAX	(20000)
48 
49 #define DISCONNECT_BY_CHK_BCN_FAIL_OBSERV_PERIOD_IN_MS 1000
50 #define DISCONNECT_BY_CHK_BCN_FAIL_THRESHOLD 3
51 
52 static u8 rtw_basic_rate_cck[4] = {
53 	IEEE80211_CCK_RATE_1MB|IEEE80211_BASIC_RATE_MASK, IEEE80211_CCK_RATE_2MB|IEEE80211_BASIC_RATE_MASK,
54 	IEEE80211_CCK_RATE_5MB|IEEE80211_BASIC_RATE_MASK, IEEE80211_CCK_RATE_11MB|IEEE80211_BASIC_RATE_MASK
55 };
56 
57 static u8 rtw_basic_rate_ofdm[3] = {
58 	IEEE80211_OFDM_RATE_6MB|IEEE80211_BASIC_RATE_MASK, IEEE80211_OFDM_RATE_12MB|IEEE80211_BASIC_RATE_MASK,
59 	IEEE80211_OFDM_RATE_24MB|IEEE80211_BASIC_RATE_MASK
60 };
61 
62 int cckrates_included(unsigned char *rate, int ratelen)
63 {
64 	int	i;
65 
66 	for (i = 0; i < ratelen; i++) {
67 		if  ((((rate[i]) & 0x7f) == 2)	|| (((rate[i]) & 0x7f) == 4) ||
68 		     (((rate[i]) & 0x7f) == 11)  || (((rate[i]) & 0x7f) == 22))
69 			return true;
70 	}
71 
72 	return false;
73 
74 }
75 
76 int cckratesonly_included(unsigned char *rate, int ratelen)
77 {
78 	int	i;
79 
80 	for (i = 0; i < ratelen; i++) {
81 		if  ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
82 		     (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
83 			return false;
84 	}
85 
86 	return true;
87 }
88 
89 u8 networktype_to_raid_ex(struct adapter *adapter, struct sta_info *psta)
90 {
91 	u8 raid, cur_rf_type, rf_type = RF_1T1R;
92 
93 	rtw_hal_get_hwreg(adapter, HW_VAR_RF_TYPE, (u8 *)(&cur_rf_type));
94 
95 	if (cur_rf_type == RF_1T1R) {
96 		rf_type = RF_1T1R;
97 	} else if (IsSupportedVHT(psta->wireless_mode)) {
98 		if (psta->ra_mask & 0xffc00000)
99 			rf_type = RF_2T2R;
100 	} else if (IsSupportedHT(psta->wireless_mode)) {
101 		if (psta->ra_mask & 0xfff00000)
102 			rf_type = RF_2T2R;
103 	}
104 
105 	switch (psta->wireless_mode) {
106 	case WIRELESS_11B:
107 		raid = RATEID_IDX_B;
108 		break;
109 	case WIRELESS_11A:
110 	case WIRELESS_11G:
111 		raid = RATEID_IDX_G;
112 		break;
113 	case WIRELESS_11BG:
114 		raid = RATEID_IDX_BG;
115 		break;
116 	case WIRELESS_11_24N:
117 	case WIRELESS_11_5N:
118 	case WIRELESS_11A_5N:
119 	case WIRELESS_11G_24N:
120 		if (rf_type == RF_2T2R)
121 			raid = RATEID_IDX_GN_N2SS;
122 		else
123 			raid = RATEID_IDX_GN_N1SS;
124 		break;
125 	case WIRELESS_11B_24N:
126 	case WIRELESS_11BG_24N:
127 		if (psta->bw_mode == CHANNEL_WIDTH_20) {
128 			if (rf_type == RF_2T2R)
129 				raid = RATEID_IDX_BGN_20M_2SS_BN;
130 			else
131 				raid = RATEID_IDX_BGN_20M_1SS_BN;
132 		} else {
133 			if (rf_type == RF_2T2R)
134 				raid = RATEID_IDX_BGN_40M_2SS;
135 			else
136 				raid = RATEID_IDX_BGN_40M_1SS;
137 		}
138 		break;
139 	default:
140 		raid = RATEID_IDX_BGN_40M_2SS;
141 		break;
142 
143 	}
144 	return raid;
145 
146 }
147 
148 unsigned char ratetbl_val_2wifirate(unsigned char rate);
149 unsigned char ratetbl_val_2wifirate(unsigned char rate)
150 {
151 	unsigned char val = 0;
152 
153 	switch (rate & 0x7f) {
154 	case 0:
155 		val = IEEE80211_CCK_RATE_1MB;
156 		break;
157 
158 	case 1:
159 		val = IEEE80211_CCK_RATE_2MB;
160 		break;
161 
162 	case 2:
163 		val = IEEE80211_CCK_RATE_5MB;
164 		break;
165 
166 	case 3:
167 		val = IEEE80211_CCK_RATE_11MB;
168 		break;
169 
170 	case 4:
171 		val = IEEE80211_OFDM_RATE_6MB;
172 		break;
173 
174 	case 5:
175 		val = IEEE80211_OFDM_RATE_9MB;
176 		break;
177 
178 	case 6:
179 		val = IEEE80211_OFDM_RATE_12MB;
180 		break;
181 
182 	case 7:
183 		val = IEEE80211_OFDM_RATE_18MB;
184 		break;
185 
186 	case 8:
187 		val = IEEE80211_OFDM_RATE_24MB;
188 		break;
189 
190 	case 9:
191 		val = IEEE80211_OFDM_RATE_36MB;
192 		break;
193 
194 	case 10:
195 		val = IEEE80211_OFDM_RATE_48MB;
196 		break;
197 
198 	case 11:
199 		val = IEEE80211_OFDM_RATE_54MB;
200 		break;
201 
202 	}
203 
204 	return val;
205 
206 }
207 
208 int is_basicrate(struct adapter *padapter, unsigned char rate);
209 int is_basicrate(struct adapter *padapter, unsigned char rate)
210 {
211 	int i;
212 	unsigned char val;
213 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
214 
215 	for (i = 0; i < NumRates; i++) {
216 		val = pmlmeext->basicrate[i];
217 
218 		if ((val != 0xff) && (val != 0xfe))
219 			if (rate == ratetbl_val_2wifirate(val))
220 				return true;
221 	}
222 
223 	return false;
224 }
225 
226 unsigned int ratetbl2rateset(struct adapter *padapter, unsigned char *rateset);
227 unsigned int ratetbl2rateset(struct adapter *padapter, unsigned char *rateset)
228 {
229 	int i;
230 	unsigned char rate;
231 	unsigned int	len = 0;
232 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
233 
234 	for (i = 0; i < NumRates; i++) {
235 		rate = pmlmeext->datarate[i];
236 
237 		switch (rate) {
238 		case 0xff:
239 			return len;
240 
241 		case 0xfe:
242 			continue;
243 
244 		default:
245 			rate = ratetbl_val_2wifirate(rate);
246 
247 			if (is_basicrate(padapter, rate) == true)
248 				rate |= IEEE80211_BASIC_RATE_MASK;
249 
250 			rateset[len] = rate;
251 			len++;
252 			break;
253 		}
254 	}
255 	return len;
256 }
257 
258 void get_rate_set(struct adapter *padapter, unsigned char *pbssrate, int *bssrate_len)
259 {
260 	unsigned char supportedrates[NumRates];
261 
262 	memset(supportedrates, 0, NumRates);
263 	*bssrate_len = ratetbl2rateset(padapter, supportedrates);
264 	memcpy(pbssrate, supportedrates, *bssrate_len);
265 }
266 
267 void set_mcs_rate_by_mask(u8 *mcs_set, u32 mask)
268 {
269 	u8 mcs_rate_1r = (u8)(mask&0xff);
270 	u8 mcs_rate_2r = (u8)((mask>>8)&0xff);
271 	u8 mcs_rate_3r = (u8)((mask>>16)&0xff);
272 	u8 mcs_rate_4r = (u8)((mask>>24)&0xff);
273 
274 	mcs_set[0] &= mcs_rate_1r;
275 	mcs_set[1] &= mcs_rate_2r;
276 	mcs_set[2] &= mcs_rate_3r;
277 	mcs_set[3] &= mcs_rate_4r;
278 }
279 
280 void UpdateBrateTbl(struct adapter *Adapter, u8 *mBratesOS)
281 {
282 	u8 i;
283 	u8 rate;
284 
285 	/*  1M, 2M, 5.5M, 11M, 6M, 12M, 24M are mandatory. */
286 	for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
287 		rate = mBratesOS[i] & 0x7f;
288 		switch (rate) {
289 		case IEEE80211_CCK_RATE_1MB:
290 		case IEEE80211_CCK_RATE_2MB:
291 		case IEEE80211_CCK_RATE_5MB:
292 		case IEEE80211_CCK_RATE_11MB:
293 		case IEEE80211_OFDM_RATE_6MB:
294 		case IEEE80211_OFDM_RATE_12MB:
295 		case IEEE80211_OFDM_RATE_24MB:
296 			mBratesOS[i] |= IEEE80211_BASIC_RATE_MASK;
297 			break;
298 		}
299 	}
300 
301 }
302 
303 void UpdateBrateTblForSoftAP(u8 *bssrateset, u32 bssratelen)
304 {
305 	u8 i;
306 	u8 rate;
307 
308 	for (i = 0; i < bssratelen; i++) {
309 		rate = bssrateset[i] & 0x7f;
310 		switch (rate) {
311 		case IEEE80211_CCK_RATE_1MB:
312 		case IEEE80211_CCK_RATE_2MB:
313 		case IEEE80211_CCK_RATE_5MB:
314 		case IEEE80211_CCK_RATE_11MB:
315 			bssrateset[i] |= IEEE80211_BASIC_RATE_MASK;
316 			break;
317 		}
318 	}
319 
320 }
321 
322 void Save_DM_Func_Flag(struct adapter *padapter)
323 {
324 	u8 bSaveFlag = true;
325 	rtw_hal_set_hwreg(padapter, HW_VAR_DM_FUNC_OP, (u8 *)(&bSaveFlag));
326 }
327 
328 void Restore_DM_Func_Flag(struct adapter *padapter)
329 {
330 	u8 bSaveFlag = false;
331 	rtw_hal_set_hwreg(padapter, HW_VAR_DM_FUNC_OP, (u8 *)(&bSaveFlag));
332 }
333 
334 void Switch_DM_Func(struct adapter *padapter, u32 mode, u8 enable)
335 {
336 	if (enable == true)
337 		rtw_hal_set_hwreg(padapter, HW_VAR_DM_FUNC_SET, (u8 *)(&mode));
338 	else
339 		rtw_hal_set_hwreg(padapter, HW_VAR_DM_FUNC_CLR, (u8 *)(&mode));
340 }
341 
342 static void Set_NETYPE0_MSR(struct adapter *padapter, u8 type)
343 {
344 	rtw_hal_set_hwreg(padapter, HW_VAR_MEDIA_STATUS, (u8 *)(&type));
345 }
346 
347 void Set_MSR(struct adapter *padapter, u8 type)
348 {
349 	Set_NETYPE0_MSR(padapter, type);
350 }
351 
352 inline u8 rtw_get_oper_ch(struct adapter *adapter)
353 {
354 	return adapter_to_dvobj(adapter)->oper_channel;
355 }
356 
357 inline void rtw_set_oper_ch(struct adapter *adapter, u8 ch)
358 {
359 #ifdef DBG_CH_SWITCH
360 	const int len = 128;
361 	char msg[128] = {0};
362 	int cnt = 0;
363 	int i = 0;
364 #endif  /* DBG_CH_SWITCH */
365 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
366 
367 	if (dvobj->oper_channel != ch) {
368 		dvobj->on_oper_ch_time = jiffies;
369 
370 #ifdef DBG_CH_SWITCH
371 		cnt += snprintf(msg+cnt, len-cnt, "switch to ch %3u", ch);
372 
373 		for (i = 0; i < dvobj->iface_nums; i++) {
374 			struct adapter *iface = dvobj->padapters[i];
375 			cnt += snprintf(msg+cnt, len-cnt, " ["ADPT_FMT":", ADPT_ARG(iface));
376 			if (iface->mlmeextpriv.cur_channel == ch)
377 				cnt += snprintf(msg+cnt, len-cnt, "C");
378 			else
379 				cnt += snprintf(msg+cnt, len-cnt, "_");
380 			if (iface->wdinfo.listen_channel == ch && !rtw_p2p_chk_state(&iface->wdinfo, P2P_STATE_NONE))
381 				cnt += snprintf(msg+cnt, len-cnt, "L");
382 			else
383 				cnt += snprintf(msg+cnt, len-cnt, "_");
384 			cnt += snprintf(msg+cnt, len-cnt, "]");
385 		}
386 
387 		DBG_871X(FUNC_ADPT_FMT" %s\n", FUNC_ADPT_ARG(adapter), msg);
388 #endif /* DBG_CH_SWITCH */
389 	}
390 
391 	dvobj->oper_channel = ch;
392 }
393 
394 inline u8 rtw_get_oper_bw(struct adapter *adapter)
395 {
396 	return adapter_to_dvobj(adapter)->oper_bwmode;
397 }
398 
399 inline void rtw_set_oper_bw(struct adapter *adapter, u8 bw)
400 {
401 	adapter_to_dvobj(adapter)->oper_bwmode = bw;
402 }
403 
404 inline u8 rtw_get_oper_choffset(struct adapter *adapter)
405 {
406 	return adapter_to_dvobj(adapter)->oper_ch_offset;
407 }
408 
409 inline void rtw_set_oper_choffset(struct adapter *adapter, u8 offset)
410 {
411 	adapter_to_dvobj(adapter)->oper_ch_offset = offset;
412 }
413 
414 u8 rtw_get_center_ch(u8 channel, u8 chnl_bw, u8 chnl_offset)
415 {
416 	u8 center_ch = channel;
417 
418 	if (chnl_bw == CHANNEL_WIDTH_80) {
419 		if ((channel == 36) || (channel == 40) || (channel == 44) || (channel == 48))
420 			center_ch = 42;
421 		if ((channel == 52) || (channel == 56) || (channel == 60) || (channel == 64))
422 			center_ch = 58;
423 		if ((channel == 100) || (channel == 104) || (channel == 108) || (channel == 112))
424 			center_ch = 106;
425 		if ((channel == 116) || (channel == 120) || (channel == 124) || (channel == 128))
426 			center_ch = 122;
427 		if ((channel == 132) || (channel == 136) || (channel == 140) || (channel == 144))
428 			center_ch = 138;
429 		if ((channel == 149) || (channel == 153) || (channel == 157) || (channel == 161))
430 			center_ch = 155;
431 		else if (channel <= 14)
432 			center_ch = 7;
433 	} else if (chnl_bw == CHANNEL_WIDTH_40) {
434 		if (chnl_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
435 			center_ch = channel + 2;
436 		else
437 			center_ch = channel - 2;
438 	}
439 
440 	return center_ch;
441 }
442 
443 inline unsigned long rtw_get_on_cur_ch_time(struct adapter *adapter)
444 {
445 	if (adapter->mlmeextpriv.cur_channel == adapter_to_dvobj(adapter)->oper_channel)
446 		return adapter_to_dvobj(adapter)->on_oper_ch_time;
447 	else
448 		return 0;
449 }
450 
451 void SelectChannel(struct adapter *padapter, unsigned char channel)
452 {
453 	if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->setch_mutex)))
454 		return;
455 
456 	/* saved channel info */
457 	rtw_set_oper_ch(padapter, channel);
458 
459 	rtw_hal_set_chan(padapter, channel);
460 
461 	mutex_unlock(&(adapter_to_dvobj(padapter)->setch_mutex));
462 }
463 
464 void set_channel_bwmode(struct adapter *padapter, unsigned char channel, unsigned char channel_offset, unsigned short bwmode)
465 {
466 	u8 center_ch, chnl_offset80 = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
467 
468 	if (padapter->bNotifyChannelChange)
469 		DBG_871X("[%s] ch = %d, offset = %d, bwmode = %d\n", __func__, channel, channel_offset, bwmode);
470 
471 	center_ch = rtw_get_center_ch(channel, bwmode, channel_offset);
472 
473 	if (bwmode == CHANNEL_WIDTH_80) {
474 		if (center_ch > channel)
475 			chnl_offset80 = HAL_PRIME_CHNL_OFFSET_LOWER;
476 		else if (center_ch < channel)
477 			chnl_offset80 = HAL_PRIME_CHNL_OFFSET_UPPER;
478 		else
479 			chnl_offset80 = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
480 	}
481 
482 	/* set Channel */
483 	if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->setch_mutex)))
484 		return;
485 
486 	/* saved channel/bw info */
487 	rtw_set_oper_ch(padapter, channel);
488 	rtw_set_oper_bw(padapter, bwmode);
489 	rtw_set_oper_choffset(padapter, channel_offset);
490 
491 	rtw_hal_set_chnl_bw(padapter, center_ch, bwmode, channel_offset, chnl_offset80); /*  set center channel */
492 
493 	mutex_unlock(&(adapter_to_dvobj(padapter)->setch_mutex));
494 }
495 
496 __inline u8 *get_my_bssid(struct wlan_bssid_ex *pnetwork)
497 {
498 	return pnetwork->MacAddress;
499 }
500 
501 u16 get_beacon_interval(struct wlan_bssid_ex *bss)
502 {
503 	__le16 val;
504 	memcpy((unsigned char *)&val, rtw_get_beacon_interval_from_ie(bss->IEs), 2);
505 
506 	return le16_to_cpu(val);
507 
508 }
509 
510 int is_client_associated_to_ap(struct adapter *padapter)
511 {
512 	struct mlme_ext_priv *pmlmeext;
513 	struct mlme_ext_info *pmlmeinfo;
514 
515 	if (!padapter)
516 		return _FAIL;
517 
518 	pmlmeext = &padapter->mlmeextpriv;
519 	pmlmeinfo = &(pmlmeext->mlmext_info);
520 
521 	if ((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE))
522 		return true;
523 	else
524 		return _FAIL;
525 }
526 
527 int is_client_associated_to_ibss(struct adapter *padapter)
528 {
529 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
530 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
531 
532 	if ((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE))
533 		return true;
534 	else
535 		return _FAIL;
536 }
537 
538 int is_IBSS_empty(struct adapter *padapter)
539 {
540 	unsigned int i;
541 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
542 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
543 
544 	for (i = IBSS_START_MAC_ID; i < NUM_STA; i++) {
545 		if (pmlmeinfo->FW_sta_info[i].status == 1)
546 			return _FAIL;
547 	}
548 
549 	return true;
550 
551 }
552 
553 unsigned int decide_wait_for_beacon_timeout(unsigned int bcn_interval)
554 {
555 	if ((bcn_interval << 2) < WAIT_FOR_BCN_TO_MIN)
556 		return WAIT_FOR_BCN_TO_MIN;
557 	else if ((bcn_interval << 2) > WAIT_FOR_BCN_TO_MAX)
558 		return WAIT_FOR_BCN_TO_MAX;
559 	else
560 		return ((bcn_interval << 2));
561 }
562 
563 void invalidate_cam_all(struct adapter *padapter)
564 {
565 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
566 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
567 
568 	rtw_hal_set_hwreg(padapter, HW_VAR_CAM_INVALID_ALL, NULL);
569 
570 	spin_lock_bh(&cam_ctl->lock);
571 	cam_ctl->bitmap = 0;
572 	memset(dvobj->cam_cache, 0, sizeof(struct cam_entry_cache)*TOTAL_CAM_ENTRY);
573 	spin_unlock_bh(&cam_ctl->lock);
574 }
575 
576 static u32 _ReadCAM(struct adapter *padapter, u32 addr)
577 {
578 	u32 count = 0, cmd;
579 	cmd = CAM_POLLINIG | addr;
580 	rtw_write32(padapter, RWCAM, cmd);
581 
582 	do {
583 		if (0 == (rtw_read32(padapter, REG_CAMCMD) & CAM_POLLINIG))
584 			break;
585 	} while (count++ < 100);
586 
587 	return rtw_read32(padapter, REG_CAMREAD);
588 }
589 void read_cam(struct adapter *padapter, u8 entry, u8 *get_key)
590 {
591 	u32 j, addr, cmd;
592 	addr = entry << 3;
593 
594 	/* DBG_8192C("********* DUMP CAM Entry_#%02d***************\n", entry); */
595 	for (j = 0; j < 6; j++) {
596 		cmd = _ReadCAM(padapter, addr+j);
597 		/* DBG_8192C("offset:0x%02x => 0x%08x\n", addr+j, cmd); */
598 		if (j > 1) /* get key from cam */
599 			memcpy(get_key+(j-2)*4, &cmd, 4);
600 	}
601 	/* DBG_8192C("*********************************\n"); */
602 }
603 
604 void _write_cam(struct adapter *padapter, u8 entry, u16 ctrl, u8 *mac, u8 *key)
605 {
606 	unsigned int i, val, addr;
607 	int j;
608 	u32 cam_val[2];
609 
610 	addr = entry << 3;
611 
612 	for (j = 5; j >= 0; j--) {
613 		switch (j) {
614 		case 0:
615 			val = (ctrl | (mac[0] << 16) | (mac[1] << 24));
616 			break;
617 		case 1:
618 			val = (mac[2] | (mac[3] << 8) | (mac[4] << 16) | (mac[5] << 24));
619 			break;
620 		default:
621 			i = (j - 2) << 2;
622 			val = (key[i] | (key[i+1] << 8) | (key[i+2] << 16) | (key[i+3] << 24));
623 			break;
624 		}
625 
626 		cam_val[0] = val;
627 		cam_val[1] = addr + (unsigned int)j;
628 
629 		rtw_hal_set_hwreg(padapter, HW_VAR_CAM_WRITE, (u8 *)cam_val);
630 	}
631 }
632 
633 void _clear_cam_entry(struct adapter *padapter, u8 entry)
634 {
635 	unsigned char null_sta[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
636 	unsigned char null_key[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
637 
638 	_write_cam(padapter, entry, 0, null_sta, null_key);
639 }
640 
641 inline void write_cam(struct adapter *adapter, u8 id, u16 ctrl, u8 *mac, u8 *key)
642 {
643 	_write_cam(adapter, id, ctrl, mac, key);
644 	write_cam_cache(adapter, id, ctrl, mac, key);
645 }
646 
647 inline void clear_cam_entry(struct adapter *adapter, u8 id)
648 {
649 	_clear_cam_entry(adapter, id);
650 	clear_cam_cache(adapter, id);
651 }
652 
653 inline void write_cam_from_cache(struct adapter *adapter, u8 id)
654 {
655 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
656 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
657 	struct cam_entry_cache cache;
658 
659 	spin_lock_bh(&cam_ctl->lock);
660 	memcpy(&cache, &dvobj->cam_cache[id], sizeof(struct cam_entry_cache));
661 	spin_unlock_bh(&cam_ctl->lock);
662 
663 	_write_cam(adapter, id, cache.ctrl, cache.mac, cache.key);
664 }
665 
666 void write_cam_cache(struct adapter *adapter, u8 id, u16 ctrl, u8 *mac, u8 *key)
667 {
668 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
669 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
670 
671 	spin_lock_bh(&cam_ctl->lock);
672 
673 	dvobj->cam_cache[id].ctrl = ctrl;
674 	memcpy(dvobj->cam_cache[id].mac, mac, ETH_ALEN);
675 	memcpy(dvobj->cam_cache[id].key, key, 16);
676 
677 	spin_unlock_bh(&cam_ctl->lock);
678 }
679 
680 void clear_cam_cache(struct adapter *adapter, u8 id)
681 {
682 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
683 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
684 
685 	spin_lock_bh(&cam_ctl->lock);
686 
687 	memset(&(dvobj->cam_cache[id]), 0, sizeof(struct cam_entry_cache));
688 
689 	spin_unlock_bh(&cam_ctl->lock);
690 }
691 
692 static bool _rtw_camid_is_gk(struct adapter *adapter, u8 cam_id)
693 {
694 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
695 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
696 	bool ret = false;
697 
698 	if (cam_id >= TOTAL_CAM_ENTRY)
699 		goto exit;
700 
701 	if (!(cam_ctl->bitmap & BIT(cam_id)))
702 		goto exit;
703 
704 	ret = (dvobj->cam_cache[cam_id].ctrl&BIT6)?true:false;
705 
706 exit:
707 	return ret;
708 }
709 
710 static s16 _rtw_camid_search(struct adapter *adapter, u8 *addr, s16 kid)
711 {
712 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
713 	int i;
714 	s16 cam_id = -1;
715 
716 	for (i = 0; i < TOTAL_CAM_ENTRY; i++) {
717 		if (addr && memcmp(dvobj->cam_cache[i].mac, addr, ETH_ALEN))
718 			continue;
719 		if (kid >= 0 && kid != (dvobj->cam_cache[i].ctrl&0x03))
720 			continue;
721 
722 		cam_id = i;
723 		break;
724 	}
725 
726 	if (addr)
727 		DBG_871X(FUNC_ADPT_FMT" addr:"MAC_FMT" kid:%d, return cam_id:%d\n"
728 			 , FUNC_ADPT_ARG(adapter), MAC_ARG(addr), kid, cam_id);
729 	else
730 		DBG_871X(FUNC_ADPT_FMT" addr:%p kid:%d, return cam_id:%d\n"
731 			 , FUNC_ADPT_ARG(adapter), addr, kid, cam_id);
732 
733 	return cam_id;
734 }
735 
736 s16 rtw_camid_search(struct adapter *adapter, u8 *addr, s16 kid)
737 {
738 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
739 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
740 	s16 cam_id = -1;
741 
742 	spin_lock_bh(&cam_ctl->lock);
743 	cam_id = _rtw_camid_search(adapter, addr, kid);
744 	spin_unlock_bh(&cam_ctl->lock);
745 
746 	return cam_id;
747 }
748 
749 s16 rtw_camid_alloc(struct adapter *adapter, struct sta_info *sta, u8 kid)
750 {
751 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
752 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
753 	s16 cam_id = -1;
754 	struct mlme_ext_info *mlmeinfo;
755 
756 	spin_lock_bh(&cam_ctl->lock);
757 
758 	mlmeinfo = &adapter->mlmeextpriv.mlmext_info;
759 
760 	if ((((mlmeinfo->state&0x03) == WIFI_FW_AP_STATE) || ((mlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE))
761 		&& !sta) {
762 		/* AP/Ad-hoc mode group key: static alloction to default key by key ID */
763 		if (kid > 3) {
764 			DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" group key with invalid key id:%u\n"
765 				, FUNC_ADPT_ARG(adapter), kid);
766 			rtw_warn_on(1);
767 			goto bitmap_handle;
768 		}
769 
770 		cam_id = kid;
771 	} else {
772 		int i;
773 		u8 *addr = sta?sta->hwaddr:NULL;
774 
775 		if (!sta) {
776 			if (!(mlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)) {
777 				/* bypass STA mode group key setting before connected(ex:WEP) because bssid is not ready */
778 				goto bitmap_handle;
779 			}
780 
781 			addr = get_bssid(&adapter->mlmepriv);
782 		}
783 
784 		i = _rtw_camid_search(adapter, addr, kid);
785 		if (i >= 0) {
786 			/* Fix issue that pairwise and group key have same key id. Pairwise key first, group key can overwirte group only(ex: rekey) */
787 			if (sta || _rtw_camid_is_gk(adapter, i) == true)
788 				cam_id = i;
789 			else
790 				DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" group key id:%u the same key id as pairwise key\n"
791 					, FUNC_ADPT_ARG(adapter), kid);
792 			goto bitmap_handle;
793 		}
794 
795 		for (i = 4; i < TOTAL_CAM_ENTRY; i++)
796 			if (!(cam_ctl->bitmap & BIT(i)))
797 				break;
798 
799 		if (i == TOTAL_CAM_ENTRY) {
800 			if (sta)
801 				DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" pairwise key with "MAC_FMT" id:%u no room\n"
802 				, FUNC_ADPT_ARG(adapter), MAC_ARG(sta->hwaddr), kid);
803 			else
804 				DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" group key id:%u no room\n"
805 				, FUNC_ADPT_ARG(adapter), kid);
806 			rtw_warn_on(1);
807 			goto bitmap_handle;
808 		}
809 
810 		cam_id = i;
811 	}
812 
813 bitmap_handle:
814 	if (cam_id >= 0 && cam_id < 32)
815 		cam_ctl->bitmap |= BIT(cam_id);
816 
817 	spin_unlock_bh(&cam_ctl->lock);
818 
819 	return cam_id;
820 }
821 
822 void rtw_camid_free(struct adapter *adapter, u8 cam_id)
823 {
824 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
825 	struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl;
826 
827 	spin_lock_bh(&cam_ctl->lock);
828 
829 	if (cam_id < TOTAL_CAM_ENTRY)
830 		cam_ctl->bitmap &= ~(BIT(cam_id));
831 
832 	spin_unlock_bh(&cam_ctl->lock);
833 }
834 
835 int allocate_fw_sta_entry(struct adapter *padapter)
836 {
837 	unsigned int mac_id;
838 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
839 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
840 
841 	for (mac_id = IBSS_START_MAC_ID; mac_id < NUM_STA; mac_id++) {
842 		if (pmlmeinfo->FW_sta_info[mac_id].status == 0) {
843 			pmlmeinfo->FW_sta_info[mac_id].status = 1;
844 			pmlmeinfo->FW_sta_info[mac_id].retry = 0;
845 			break;
846 		}
847 	}
848 
849 	return mac_id;
850 }
851 
852 void flush_all_cam_entry(struct adapter *padapter)
853 {
854 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
855 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
856 
857 	invalidate_cam_all(padapter);
858 	/* clear default key related key search setting */
859 	rtw_hal_set_hwreg(padapter, HW_VAR_SEC_DK_CFG, (u8 *)false);
860 
861 	memset((u8 *)(pmlmeinfo->FW_sta_info), 0, sizeof(pmlmeinfo->FW_sta_info));
862 
863 }
864 
865 int WMM_param_handler(struct adapter *padapter, struct ndis_80211_var_ie *pIE)
866 {
867 	/* struct registry_priv *pregpriv = &padapter->registrypriv; */
868 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
869 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
870 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
871 
872 	if (pmlmepriv->qospriv.qos_option == 0) {
873 		pmlmeinfo->WMM_enable = 0;
874 		return false;
875 	}
876 
877 	if (!memcmp(&(pmlmeinfo->WMM_param), (pIE->data + 6), sizeof(struct WMM_para_element)))
878 		return false;
879 	else
880 		memcpy(&(pmlmeinfo->WMM_param), (pIE->data + 6), sizeof(struct WMM_para_element));
881 
882 	pmlmeinfo->WMM_enable = 1;
883 	return true;
884 }
885 
886 void WMMOnAssocRsp(struct adapter *padapter)
887 {
888 	u8 ACI, ACM, AIFS, ECWMin, ECWMax, aSifsTime;
889 	u8 acm_mask;
890 	u16 TXOP;
891 	u32 acParm, i;
892 	u32 edca[4], inx[4];
893 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
894 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
895 	struct xmit_priv 	*pxmitpriv = &padapter->xmitpriv;
896 	struct registry_priv *pregpriv = &padapter->registrypriv;
897 
898 	acm_mask = 0;
899 
900 	if (pmlmeext->cur_wireless_mode & WIRELESS_11_24N)
901 		aSifsTime = 16;
902 	else
903 		aSifsTime = 10;
904 
905 	if (pmlmeinfo->WMM_enable == 0) {
906 		padapter->mlmepriv.acm_mask = 0;
907 
908 		AIFS = aSifsTime + (2 * pmlmeinfo->slotTime);
909 
910 		if (pmlmeext->cur_wireless_mode & (WIRELESS_11G | WIRELESS_11A)) {
911 			ECWMin = 4;
912 			ECWMax = 10;
913 		} else if (pmlmeext->cur_wireless_mode & WIRELESS_11B) {
914 			ECWMin = 5;
915 			ECWMax = 10;
916 		} else {
917 			ECWMin = 4;
918 			ECWMax = 10;
919 		}
920 
921 		TXOP = 0;
922 		acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
923 		rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&acParm));
924 		rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BK, (u8 *)(&acParm));
925 		rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VI, (u8 *)(&acParm));
926 
927 		ECWMin = 2;
928 		ECWMax = 3;
929 		TXOP = 0x2f;
930 		acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
931 		rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VO, (u8 *)(&acParm));
932 	} else{
933 		edca[0] = edca[1] = edca[2] = edca[3] = 0;
934 
935 		for (i = 0; i < 4; i++) {
936 			ACI = (pmlmeinfo->WMM_param.ac_param[i].ACI_AIFSN >> 5) & 0x03;
937 			ACM = (pmlmeinfo->WMM_param.ac_param[i].ACI_AIFSN >> 4) & 0x01;
938 
939 			/* AIFS = AIFSN * slot time + SIFS - r2t phy delay */
940 			AIFS = (pmlmeinfo->WMM_param.ac_param[i].ACI_AIFSN & 0x0f) * pmlmeinfo->slotTime + aSifsTime;
941 
942 			ECWMin = (pmlmeinfo->WMM_param.ac_param[i].CW & 0x0f);
943 			ECWMax = (pmlmeinfo->WMM_param.ac_param[i].CW & 0xf0) >> 4;
944 			TXOP = le16_to_cpu(pmlmeinfo->WMM_param.ac_param[i].TXOP_limit);
945 
946 			acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
947 
948 			switch (ACI) {
949 			case 0x0:
950 				rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&acParm));
951 				acm_mask |= (ACM ? BIT(1):0);
952 				edca[XMIT_BE_QUEUE] = acParm;
953 				break;
954 
955 			case 0x1:
956 				rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BK, (u8 *)(&acParm));
957 				/* acm_mask |= (ACM? BIT(0):0); */
958 				edca[XMIT_BK_QUEUE] = acParm;
959 				break;
960 
961 			case 0x2:
962 				rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VI, (u8 *)(&acParm));
963 				acm_mask |= (ACM ? BIT(2):0);
964 				edca[XMIT_VI_QUEUE] = acParm;
965 				break;
966 
967 			case 0x3:
968 				rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VO, (u8 *)(&acParm));
969 				acm_mask |= (ACM ? BIT(3):0);
970 				edca[XMIT_VO_QUEUE] = acParm;
971 				break;
972 			}
973 
974 			DBG_871X("WMM(%x): %x, %x\n", ACI, ACM, acParm);
975 		}
976 
977 		if (padapter->registrypriv.acm_method == 1)
978 			rtw_hal_set_hwreg(padapter, HW_VAR_ACM_CTRL, (u8 *)(&acm_mask));
979 		else
980 			padapter->mlmepriv.acm_mask = acm_mask;
981 
982 		inx[0] = 0; inx[1] = 1; inx[2] = 2; inx[3] = 3;
983 
984 		if (pregpriv->wifi_spec == 1) {
985 			u32 j, tmp, change_inx = false;
986 
987 			/* entry indx: 0->vo, 1->vi, 2->be, 3->bk. */
988 			for (i = 0; i < 4; i++) {
989 				for (j = i+1; j < 4; j++) {
990 					/* compare CW and AIFS */
991 					if ((edca[j] & 0xFFFF) < (edca[i] & 0xFFFF)) {
992 						change_inx = true;
993 					} else if ((edca[j] & 0xFFFF) == (edca[i] & 0xFFFF)) {
994 						/* compare TXOP */
995 						if ((edca[j] >> 16) > (edca[i] >> 16))
996 							change_inx = true;
997 					}
998 
999 					if (change_inx) {
1000 						tmp = edca[i];
1001 						edca[i] = edca[j];
1002 						edca[j] = tmp;
1003 
1004 						tmp = inx[i];
1005 						inx[i] = inx[j];
1006 						inx[j] = tmp;
1007 
1008 						change_inx = false;
1009 					}
1010 				}
1011 			}
1012 		}
1013 
1014 		for (i = 0; i < 4; i++) {
1015 			pxmitpriv->wmm_para_seq[i] = inx[i];
1016 			DBG_871X("wmm_para_seq(%d): %d\n", i, pxmitpriv->wmm_para_seq[i]);
1017 		}
1018 	}
1019 }
1020 
1021 static void bwmode_update_check(struct adapter *padapter, struct ndis_80211_var_ie *pIE)
1022 {
1023 	unsigned char  new_bwmode;
1024 	unsigned char  new_ch_offset;
1025 	struct HT_info_element	 *pHT_info;
1026 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1027 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1028 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1029 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
1030 	struct ht_priv 		*phtpriv = &pmlmepriv->htpriv;
1031 	u8 cbw40_enable = 0;
1032 
1033 	if (!pIE)
1034 		return;
1035 
1036 	if (phtpriv->ht_option == false)
1037 		return;
1038 
1039 	if (pmlmeext->cur_bwmode >= CHANNEL_WIDTH_80)
1040 		return;
1041 
1042 	if (pIE->Length > sizeof(struct HT_info_element))
1043 		return;
1044 
1045 	pHT_info = (struct HT_info_element *)pIE->data;
1046 
1047 	if (pmlmeext->cur_channel > 14) {
1048 		if ((pregistrypriv->bw_mode & 0xf0) > 0)
1049 			cbw40_enable = 1;
1050 	} else
1051 		if ((pregistrypriv->bw_mode & 0x0f) > 0)
1052 			cbw40_enable = 1;
1053 
1054 	if ((pHT_info->infos[0] & BIT(2)) && cbw40_enable) {
1055 		new_bwmode = CHANNEL_WIDTH_40;
1056 
1057 		switch (pHT_info->infos[0] & 0x3) {
1058 		case 1:
1059 			new_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
1060 			break;
1061 
1062 		case 3:
1063 			new_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
1064 			break;
1065 
1066 		default:
1067 			new_bwmode = CHANNEL_WIDTH_20;
1068 			new_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
1069 			break;
1070 		}
1071 	} else{
1072 		new_bwmode = CHANNEL_WIDTH_20;
1073 		new_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
1074 	}
1075 
1076 
1077 	if ((new_bwmode != pmlmeext->cur_bwmode) || (new_ch_offset != pmlmeext->cur_ch_offset)) {
1078 		pmlmeinfo->bwmode_updated = true;
1079 
1080 		pmlmeext->cur_bwmode = new_bwmode;
1081 		pmlmeext->cur_ch_offset = new_ch_offset;
1082 
1083 		/* update HT info also */
1084 		HT_info_handler(padapter, pIE);
1085 	} else
1086 		pmlmeinfo->bwmode_updated = false;
1087 
1088 
1089 	if (true == pmlmeinfo->bwmode_updated) {
1090 		struct sta_info *psta;
1091 		struct wlan_bssid_ex	*cur_network = &(pmlmeinfo->network);
1092 		struct sta_priv *pstapriv = &padapter->stapriv;
1093 
1094 		/* set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode); */
1095 
1096 
1097 		/* update ap's stainfo */
1098 		psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress);
1099 		if (psta) {
1100 			struct ht_priv *phtpriv_sta = &psta->htpriv;
1101 
1102 			if (phtpriv_sta->ht_option) {
1103 				/*  bwmode */
1104 				psta->bw_mode = pmlmeext->cur_bwmode;
1105 				phtpriv_sta->ch_offset = pmlmeext->cur_ch_offset;
1106 			} else{
1107 				psta->bw_mode = CHANNEL_WIDTH_20;
1108 				phtpriv_sta->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
1109 			}
1110 
1111 			rtw_dm_ra_mask_wk_cmd(padapter, (u8 *)psta);
1112 		}
1113 	}
1114 }
1115 
1116 void HT_caps_handler(struct adapter *padapter, struct ndis_80211_var_ie *pIE)
1117 {
1118 	unsigned int	i;
1119 	u8 rf_type;
1120 	u8 max_AMPDU_len, min_MPDU_spacing;
1121 	u8 cur_ldpc_cap = 0, cur_stbc_cap = 0;
1122 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1123 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1124 	struct mlme_priv 	*pmlmepriv = &padapter->mlmepriv;
1125 	struct ht_priv 		*phtpriv = &pmlmepriv->htpriv;
1126 
1127 	if (pIE == NULL)
1128 		return;
1129 
1130 	if (phtpriv->ht_option == false)
1131 		return;
1132 
1133 	pmlmeinfo->HT_caps_enable = 1;
1134 
1135 	for (i = 0; i < (pIE->Length); i++) {
1136 		if (i != 2) {
1137 			/* 	Commented by Albert 2010/07/12 */
1138 			/* 	Got the endian issue here. */
1139 			pmlmeinfo->HT_caps.u.HT_cap[i] &= (pIE->data[i]);
1140 		} else{
1141 			/* modify from  fw by Thomas 2010/11/17 */
1142 			if ((pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x3) > (pIE->data[i] & 0x3))
1143 				max_AMPDU_len = (pIE->data[i] & 0x3);
1144 			else
1145 				max_AMPDU_len = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x3);
1146 
1147 			if ((pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c) > (pIE->data[i] & 0x1c))
1148 				min_MPDU_spacing = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c);
1149 			else
1150 				min_MPDU_spacing = (pIE->data[i] & 0x1c);
1151 
1152 			pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para = max_AMPDU_len | min_MPDU_spacing;
1153 		}
1154 	}
1155 	rtw_hal_get_hwreg(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
1156 
1157 	/* update the MCS set */
1158 	for (i = 0; i < 16; i++)
1159 		pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate[i] &= pmlmeext->default_supported_mcs_set[i];
1160 
1161 	/* update the MCS rates */
1162 	switch (rf_type) {
1163 	case RF_1T1R:
1164 	case RF_1T2R:
1165 		set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_1R);
1166 		break;
1167 	case RF_2T2R:
1168 	default:
1169 #ifdef CONFIG_DISABLE_MCS13TO15
1170 		if (pmlmeext->cur_bwmode == CHANNEL_WIDTH_40 && pregistrypriv->wifi_spec != 1)
1171 			set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_2R_13TO15_OFF);
1172 		else
1173 			set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_2R);
1174 #else /* CONFIG_DISABLE_MCS13TO15 */
1175 		set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_2R);
1176 #endif /* CONFIG_DISABLE_MCS13TO15 */
1177 	}
1178 
1179 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1180 		/*  Config STBC setting */
1181 		if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX) && GET_HT_CAPABILITY_ELE_TX_STBC(pIE->data)) {
1182 			SET_FLAG(cur_stbc_cap, STBC_HT_ENABLE_TX);
1183 			DBG_871X("Enable HT Tx STBC !\n");
1184 		}
1185 		phtpriv->stbc_cap = cur_stbc_cap;
1186 	} else {
1187 		/*  Config LDPC Coding Capability */
1188 		if (TEST_FLAG(phtpriv->ldpc_cap, LDPC_HT_ENABLE_TX) && GET_HT_CAPABILITY_ELE_LDPC_CAP(pIE->data)) {
1189 			SET_FLAG(cur_ldpc_cap, (LDPC_HT_ENABLE_TX | LDPC_HT_CAP_TX));
1190 			DBG_871X("Enable HT Tx LDPC!\n");
1191 		}
1192 		phtpriv->ldpc_cap = cur_ldpc_cap;
1193 
1194 		/*  Config STBC setting */
1195 		if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX) && GET_HT_CAPABILITY_ELE_RX_STBC(pIE->data)) {
1196 			SET_FLAG(cur_stbc_cap, (STBC_HT_ENABLE_TX | STBC_HT_CAP_TX));
1197 			DBG_871X("Enable HT Tx STBC!\n");
1198 		}
1199 		phtpriv->stbc_cap = cur_stbc_cap;
1200 	}
1201 }
1202 
1203 void HT_info_handler(struct adapter *padapter, struct ndis_80211_var_ie *pIE)
1204 {
1205 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1206 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1207 	struct mlme_priv 	*pmlmepriv = &padapter->mlmepriv;
1208 	struct ht_priv 		*phtpriv = &pmlmepriv->htpriv;
1209 
1210 	if (pIE == NULL)
1211 		return;
1212 
1213 	if (phtpriv->ht_option == false)
1214 		return;
1215 
1216 
1217 	if (pIE->Length > sizeof(struct HT_info_element))
1218 		return;
1219 
1220 	pmlmeinfo->HT_info_enable = 1;
1221 	memcpy(&(pmlmeinfo->HT_info), pIE->data, pIE->Length);
1222 
1223 	return;
1224 }
1225 
1226 void HTOnAssocRsp(struct adapter *padapter)
1227 {
1228 	unsigned char 	max_AMPDU_len;
1229 	unsigned char 	min_MPDU_spacing;
1230 	/* struct registry_priv  *pregpriv = &padapter->registrypriv; */
1231 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1232 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1233 
1234 	DBG_871X("%s\n", __func__);
1235 
1236 	if ((pmlmeinfo->HT_info_enable) && (pmlmeinfo->HT_caps_enable)) {
1237 		pmlmeinfo->HT_enable = 1;
1238 	} else{
1239 		pmlmeinfo->HT_enable = 0;
1240 		/* set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode); */
1241 		return;
1242 	}
1243 
1244 	/* handle A-MPDU parameter field */
1245 	/*
1246 		AMPDU_para [1:0]:Max AMPDU Len => 0:8k , 1:16k, 2:32k, 3:64k
1247 		AMPDU_para [4:2]:Min MPDU Start Spacing
1248 	*/
1249 	max_AMPDU_len = pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x03;
1250 
1251 	min_MPDU_spacing = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c) >> 2;
1252 
1253 	rtw_hal_set_hwreg(padapter, HW_VAR_AMPDU_MIN_SPACE, (u8 *)(&min_MPDU_spacing));
1254 
1255 	rtw_hal_set_hwreg(padapter, HW_VAR_AMPDU_FACTOR, (u8 *)(&max_AMPDU_len));
1256 }
1257 
1258 void ERP_IE_handler(struct adapter *padapter, struct ndis_80211_var_ie *pIE)
1259 {
1260 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1261 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1262 
1263 	if (pIE->Length > 1)
1264 		return;
1265 
1266 	pmlmeinfo->ERP_enable = 1;
1267 	memcpy(&(pmlmeinfo->ERP_IE), pIE->data, pIE->Length);
1268 }
1269 
1270 void VCS_update(struct adapter *padapter, struct sta_info *psta)
1271 {
1272 	struct registry_priv  *pregpriv = &padapter->registrypriv;
1273 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1274 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1275 
1276 	switch (pregpriv->vrtl_carrier_sense) {/* 0:off 1:on 2:auto */
1277 	case 0: /* off */
1278 		psta->rtsen = 0;
1279 		psta->cts2self = 0;
1280 		break;
1281 
1282 	case 1: /* on */
1283 		if (pregpriv->vcs_type == 1) { /* 1:RTS/CTS 2:CTS to self */
1284 			psta->rtsen = 1;
1285 			psta->cts2self = 0;
1286 		} else{
1287 			psta->rtsen = 0;
1288 			psta->cts2self = 1;
1289 		}
1290 		break;
1291 
1292 	case 2: /* auto */
1293 	default:
1294 		if ((pmlmeinfo->ERP_enable) && (pmlmeinfo->ERP_IE & BIT(1))) {
1295 			if (pregpriv->vcs_type == 1) {
1296 				psta->rtsen = 1;
1297 				psta->cts2self = 0;
1298 			} else{
1299 				psta->rtsen = 0;
1300 				psta->cts2self = 1;
1301 			}
1302 		} else{
1303 			psta->rtsen = 0;
1304 			psta->cts2self = 0;
1305 		}
1306 		break;
1307 	}
1308 }
1309 
1310 void update_ldpc_stbc_cap(struct sta_info *psta)
1311 {
1312 	if (psta->htpriv.ht_option) {
1313 		if (TEST_FLAG(psta->htpriv.ldpc_cap, LDPC_HT_ENABLE_TX))
1314 			psta->ldpc = 1;
1315 
1316 		if (TEST_FLAG(psta->htpriv.stbc_cap, STBC_HT_ENABLE_TX))
1317 			psta->stbc = 1;
1318 	} else {
1319 		psta->ldpc = 0;
1320 		psta->stbc = 0;
1321 	}
1322 }
1323 
1324 int rtw_check_bcn_info(struct adapter *Adapter, u8 *pframe, u32 packet_len)
1325 {
1326 	unsigned int		len;
1327 	unsigned char 	*p;
1328 	unsigned short	val16, subtype;
1329 	struct wlan_network *cur_network = &(Adapter->mlmepriv.cur_network);
1330 	/* u8 wpa_ie[255], rsn_ie[255]; */
1331 	u16 wpa_len = 0, rsn_len = 0;
1332 	u8 encryp_protocol = 0;
1333 	struct wlan_bssid_ex *bssid;
1334 	int group_cipher = 0, pairwise_cipher = 0, is_8021x = 0;
1335 	unsigned char *pbuf;
1336 	u32 wpa_ielen = 0;
1337 	u8 *pbssid = GetAddr3Ptr(pframe);
1338 	u32 hidden_ssid = 0;
1339 	struct HT_info_element *pht_info = NULL;
1340 	struct rtw_ieee80211_ht_cap *pht_cap = NULL;
1341 	u32 bcn_channel;
1342 	unsigned short	ht_cap_info;
1343 	unsigned char ht_info_infos_0;
1344 	struct mlme_priv *pmlmepriv = &Adapter->mlmepriv;
1345 
1346 	if (is_client_associated_to_ap(Adapter) == false)
1347 		return true;
1348 
1349 	len = packet_len - sizeof(struct ieee80211_hdr_3addr);
1350 
1351 	if (len > MAX_IE_SZ) {
1352 		DBG_871X("%s IE too long for survey event\n", __func__);
1353 		return _FAIL;
1354 	}
1355 
1356 	if (memcmp(cur_network->network.MacAddress, pbssid, 6)) {
1357 		DBG_871X("Oops: rtw_check_network_encrypt linked but recv other bssid bcn\n" MAC_FMT MAC_FMT,
1358 				MAC_ARG(pbssid), MAC_ARG(cur_network->network.MacAddress));
1359 		return true;
1360 	}
1361 
1362 	bssid = (struct wlan_bssid_ex *)rtw_zmalloc(sizeof(struct wlan_bssid_ex));
1363 	if (bssid == NULL) {
1364 		DBG_871X("%s rtw_zmalloc fail !!!\n", __func__);
1365 		return true;
1366 	}
1367 
1368 	if ((pmlmepriv->timeBcnInfoChkStart != 0) && (jiffies_to_msecs(jiffies - pmlmepriv->timeBcnInfoChkStart) > DISCONNECT_BY_CHK_BCN_FAIL_OBSERV_PERIOD_IN_MS)) {
1369 		pmlmepriv->timeBcnInfoChkStart = 0;
1370 		pmlmepriv->NumOfBcnInfoChkFail = 0;
1371 	}
1372 
1373 	subtype = GetFrameSubType(pframe) >> 4;
1374 
1375 	if (subtype == WIFI_BEACON)
1376 		bssid->Reserved[0] = 1;
1377 
1378 	bssid->Length = sizeof(struct wlan_bssid_ex) - MAX_IE_SZ + len;
1379 
1380 	/* below is to copy the information element */
1381 	bssid->IELength = len;
1382 	memcpy(bssid->IEs, (pframe + sizeof(struct ieee80211_hdr_3addr)), bssid->IELength);
1383 
1384 	/* check bw and channel offset */
1385 	/* parsing HT_CAP_IE */
1386 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
1387 	if (p && len > 0) {
1388 			pht_cap = (struct rtw_ieee80211_ht_cap *)(p + 2);
1389 			ht_cap_info = le16_to_cpu(pht_cap->cap_info);
1390 	} else {
1391 			ht_cap_info = 0;
1392 	}
1393 	/* parsing HT_INFO_IE */
1394 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
1395 	if (p && len > 0) {
1396 			pht_info = (struct HT_info_element *)(p + 2);
1397 			ht_info_infos_0 = pht_info->infos[0];
1398 	} else {
1399 			ht_info_infos_0 = 0;
1400 	}
1401 	if (ht_cap_info != cur_network->BcnInfo.ht_cap_info ||
1402 		((ht_info_infos_0&0x03) != (cur_network->BcnInfo.ht_info_infos_0&0x03))) {
1403 			DBG_871X("%s bcn now: ht_cap_info:%x ht_info_infos_0:%x\n", __func__,
1404 							ht_cap_info, ht_info_infos_0);
1405 			DBG_871X("%s bcn link: ht_cap_info:%x ht_info_infos_0:%x\n", __func__,
1406 							cur_network->BcnInfo.ht_cap_info, cur_network->BcnInfo.ht_info_infos_0);
1407 			DBG_871X("%s bw mode change\n", __func__);
1408 			{
1409 				/* bcn_info_update */
1410 				cur_network->BcnInfo.ht_cap_info = ht_cap_info;
1411 				cur_network->BcnInfo.ht_info_infos_0 = ht_info_infos_0;
1412 				/* to do : need to check that whether modify related register of BB or not */
1413 			}
1414 			/* goto _mismatch; */
1415 	}
1416 
1417 	/* Checking for channel */
1418 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _DSSET_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
1419 	if (p) {
1420 			bcn_channel = *(p + 2);
1421 	} else {/* In 5G, some ap do not have DSSET IE checking HT info for channel */
1422 			rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
1423 			if (pht_info) {
1424 					bcn_channel = pht_info->primary_channel;
1425 			} else { /* we don't find channel IE, so don't check it */
1426 					/* DBG_871X("Oops: %s we don't find channel IE, so don't check it\n", __func__); */
1427 					bcn_channel = Adapter->mlmeextpriv.cur_channel;
1428 			}
1429 	}
1430 	if (bcn_channel != Adapter->mlmeextpriv.cur_channel) {
1431 			DBG_871X("%s beacon channel:%d cur channel:%d disconnect\n", __func__,
1432 						   bcn_channel, Adapter->mlmeextpriv.cur_channel);
1433 			goto _mismatch;
1434 	}
1435 
1436 	/* checking SSID */
1437 	p = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _SSID_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
1438 	if (p == NULL) {
1439 		DBG_871X("%s marc: cannot find SSID for survey event\n", __func__);
1440 		hidden_ssid = true;
1441 	} else {
1442 		hidden_ssid = false;
1443 	}
1444 
1445 	if ((NULL != p) && (false == hidden_ssid && (*(p + 1)))) {
1446 		memcpy(bssid->Ssid.Ssid, (p + 2), *(p + 1));
1447 		bssid->Ssid.SsidLength = *(p + 1);
1448 	} else {
1449 		bssid->Ssid.SsidLength = 0;
1450 		bssid->Ssid.Ssid[0] = '\0';
1451 	}
1452 
1453 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s bssid.Ssid.Ssid:%s bssid.Ssid.SsidLength:%d "
1454 				"cur_network->network.Ssid.Ssid:%s len:%d\n", __func__, bssid->Ssid.Ssid,
1455 				bssid->Ssid.SsidLength, cur_network->network.Ssid.Ssid,
1456 				cur_network->network.Ssid.SsidLength));
1457 
1458 	if (memcmp(bssid->Ssid.Ssid, cur_network->network.Ssid.Ssid, 32) ||
1459 			bssid->Ssid.SsidLength != cur_network->network.Ssid.SsidLength) {
1460 		if (bssid->Ssid.Ssid[0] != '\0' && bssid->Ssid.SsidLength != 0) { /* not hidden ssid */
1461 			DBG_871X("%s(), SSID is not match\n", __func__);
1462 			goto _mismatch;
1463 		}
1464 	}
1465 
1466 	/* check encryption info */
1467 	val16 = rtw_get_capability((struct wlan_bssid_ex *)bssid);
1468 
1469 	if (val16 & BIT(4))
1470 		bssid->Privacy = 1;
1471 	else
1472 		bssid->Privacy = 0;
1473 
1474 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
1475 			("%s(): cur_network->network.Privacy is %d, bssid.Privacy is %d\n",
1476 			 __func__, cur_network->network.Privacy, bssid->Privacy));
1477 	if (cur_network->network.Privacy != bssid->Privacy) {
1478 		DBG_871X("%s(), privacy is not match\n", __func__);
1479 		goto _mismatch;
1480 	}
1481 
1482 	rtw_get_sec_ie(bssid->IEs, bssid->IELength, NULL, &rsn_len, NULL, &wpa_len);
1483 
1484 	if (rsn_len > 0) {
1485 		encryp_protocol = ENCRYP_PROTOCOL_WPA2;
1486 	} else if (wpa_len > 0) {
1487 		encryp_protocol = ENCRYP_PROTOCOL_WPA;
1488 	} else {
1489 		if (bssid->Privacy)
1490 			encryp_protocol = ENCRYP_PROTOCOL_WEP;
1491 	}
1492 
1493 	if (cur_network->BcnInfo.encryp_protocol != encryp_protocol) {
1494 		DBG_871X("%s(): enctyp is not match\n", __func__);
1495 		goto _mismatch;
1496 	}
1497 
1498 	if (encryp_protocol == ENCRYP_PROTOCOL_WPA || encryp_protocol == ENCRYP_PROTOCOL_WPA2) {
1499 		pbuf = rtw_get_wpa_ie(&bssid->IEs[12], &wpa_ielen, bssid->IELength-12);
1500 		if (pbuf && (wpa_ielen > 0)) {
1501 			if (_SUCCESS == rtw_parse_wpa_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is_8021x)) {
1502 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
1503 						("%s pnetwork->pairwise_cipher: %d, group_cipher is %d, is_8021x is %d\n", __func__,
1504 						 pairwise_cipher, group_cipher, is_8021x));
1505 			}
1506 		} else {
1507 			pbuf = rtw_get_wpa2_ie(&bssid->IEs[12], &wpa_ielen, bssid->IELength-12);
1508 
1509 			if (pbuf && (wpa_ielen > 0)) {
1510 				if (_SUCCESS == rtw_parse_wpa2_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is_8021x)) {
1511 					RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
1512 							("%s pnetwork->pairwise_cipher: %d, pnetwork->group_cipher is %d, is_802x is %d\n",
1513 							 __func__, pairwise_cipher, group_cipher, is_8021x));
1514 				}
1515 			}
1516 		}
1517 
1518 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1519 				("%s cur_network->group_cipher is %d: %d\n", __func__, cur_network->BcnInfo.group_cipher, group_cipher));
1520 		if (pairwise_cipher != cur_network->BcnInfo.pairwise_cipher || group_cipher != cur_network->BcnInfo.group_cipher) {
1521 			DBG_871X("%s pairwise_cipher(%x:%x) or group_cipher(%x:%x) is not match\n", __func__,
1522 					pairwise_cipher, cur_network->BcnInfo.pairwise_cipher,
1523 					group_cipher, cur_network->BcnInfo.group_cipher);
1524 			goto _mismatch;
1525 		}
1526 
1527 		if (is_8021x != cur_network->BcnInfo.is_8021x) {
1528 			DBG_871X("%s authentication is not match\n", __func__);
1529 			goto _mismatch;
1530 		}
1531 	}
1532 
1533 	kfree((u8 *)bssid);
1534 	return _SUCCESS;
1535 
1536 _mismatch:
1537 	kfree((u8 *)bssid);
1538 
1539 	if (pmlmepriv->NumOfBcnInfoChkFail == 0)
1540 		pmlmepriv->timeBcnInfoChkStart = jiffies;
1541 
1542 	pmlmepriv->NumOfBcnInfoChkFail++;
1543 	DBG_871X("%s by "ADPT_FMT" - NumOfChkFail = %d (SeqNum of this Beacon frame = %d).\n", __func__, ADPT_ARG(Adapter), pmlmepriv->NumOfBcnInfoChkFail, GetSequence(pframe));
1544 
1545 	if ((pmlmepriv->timeBcnInfoChkStart != 0) && (jiffies_to_msecs(jiffies - pmlmepriv->timeBcnInfoChkStart) <= DISCONNECT_BY_CHK_BCN_FAIL_OBSERV_PERIOD_IN_MS)
1546 		&& (pmlmepriv->NumOfBcnInfoChkFail >= DISCONNECT_BY_CHK_BCN_FAIL_THRESHOLD)) {
1547 		DBG_871X("%s by "ADPT_FMT" - NumOfChkFail = %d >= threshold : %d (in %d ms), return FAIL.\n", __func__, ADPT_ARG(Adapter), pmlmepriv->NumOfBcnInfoChkFail,
1548 			DISCONNECT_BY_CHK_BCN_FAIL_THRESHOLD, jiffies_to_msecs(jiffies - pmlmepriv->timeBcnInfoChkStart));
1549 		pmlmepriv->timeBcnInfoChkStart = 0;
1550 		pmlmepriv->NumOfBcnInfoChkFail = 0;
1551 		return _FAIL;
1552 	}
1553 
1554 	return _SUCCESS;
1555 }
1556 
1557 void update_beacon_info(struct adapter *padapter, u8 *pframe, uint pkt_len, struct sta_info *psta)
1558 {
1559 	unsigned int i;
1560 	unsigned int len;
1561 	struct ndis_80211_var_ie *pIE;
1562 
1563 	len = pkt_len - (_BEACON_IE_OFFSET_ + WLAN_HDR_A3_LEN);
1564 
1565 	for (i = 0; i < len;) {
1566 		pIE = (struct ndis_80211_var_ie *)(pframe + (_BEACON_IE_OFFSET_ + WLAN_HDR_A3_LEN) + i);
1567 
1568 		switch (pIE->ElementID) {
1569 		case _VENDOR_SPECIFIC_IE_:
1570 			/* to update WMM paramter set while receiving beacon */
1571 			if (!memcmp(pIE->data, WMM_PARA_OUI, 6) && pIE->Length == WLAN_WMM_LEN)	/* WMM */
1572 				if (WMM_param_handler(padapter, pIE))
1573 					report_wmm_edca_update(padapter);
1574 
1575 			break;
1576 
1577 		case _HT_EXTRA_INFO_IE_:	/* HT info */
1578 			/* HT_info_handler(padapter, pIE); */
1579 			bwmode_update_check(padapter, pIE);
1580 			break;
1581 
1582 		case _ERPINFO_IE_:
1583 			ERP_IE_handler(padapter, pIE);
1584 			VCS_update(padapter, psta);
1585 			break;
1586 
1587 		default:
1588 			break;
1589 		}
1590 
1591 		i += (pIE->Length + 2);
1592 	}
1593 }
1594 
1595 unsigned int is_ap_in_tkip(struct adapter *padapter)
1596 {
1597 	u32 i;
1598 	struct ndis_80211_var_ie *pIE;
1599 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1600 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1601 	struct wlan_bssid_ex		*cur_network = &(pmlmeinfo->network);
1602 
1603 	if (rtw_get_capability((struct wlan_bssid_ex *)cur_network) & WLAN_CAPABILITY_PRIVACY) {
1604 		for (i = sizeof(struct ndis_802_11_fix_ie); i < pmlmeinfo->network.IELength;) {
1605 			pIE = (struct ndis_80211_var_ie *)(pmlmeinfo->network.IEs + i);
1606 
1607 			switch (pIE->ElementID) {
1608 			case _VENDOR_SPECIFIC_IE_:
1609 				if ((!memcmp(pIE->data, RTW_WPA_OUI, 4)) && (!memcmp((pIE->data + 12), WPA_TKIP_CIPHER, 4)))
1610 					return true;
1611 
1612 				break;
1613 
1614 			case _RSN_IE_2_:
1615 				if (!memcmp((pIE->data + 8), RSN_TKIP_CIPHER, 4))
1616 					return true;
1617 
1618 			default:
1619 				break;
1620 			}
1621 
1622 			i += (pIE->Length + 2);
1623 		}
1624 
1625 		return false;
1626 	} else
1627 		return false;
1628 
1629 }
1630 
1631 int support_short_GI(struct adapter *padapter, struct HT_caps_element *pHT_caps, u8 bwmode)
1632 {
1633 	unsigned char 				bit_offset;
1634 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1635 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1636 
1637 	if (!(pmlmeinfo->HT_enable))
1638 		return _FAIL;
1639 
1640 	bit_offset = (bwmode & CHANNEL_WIDTH_40) ? 6 : 5;
1641 
1642 	if (le16_to_cpu(pHT_caps->u.HT_cap_element.HT_caps_info) & (0x1 << bit_offset))
1643 		return _SUCCESS;
1644 	else
1645 		return _FAIL;
1646 }
1647 
1648 unsigned char get_highest_rate_idx(u32 mask)
1649 {
1650 	int i;
1651 	unsigned char rate_idx = 0;
1652 
1653 	for (i = 31; i >= 0; i--) {
1654 		if (mask & BIT(i)) {
1655 			rate_idx = i;
1656 			break;
1657 		}
1658 	}
1659 
1660 	return rate_idx;
1661 }
1662 
1663 void Update_RA_Entry(struct adapter *padapter, struct sta_info *psta)
1664 {
1665 	rtw_hal_update_ra_mask(psta, 0);
1666 }
1667 
1668 void enable_rate_adaptive(struct adapter *padapter, struct sta_info *psta);
1669 void enable_rate_adaptive(struct adapter *padapter, struct sta_info *psta)
1670 {
1671 	Update_RA_Entry(padapter, psta);
1672 }
1673 
1674 void set_sta_rate(struct adapter *padapter, struct sta_info *psta)
1675 {
1676 	/* rate adaptive */
1677 	enable_rate_adaptive(padapter, psta);
1678 }
1679 
1680 unsigned char check_assoc_AP(u8 *pframe, uint len)
1681 {
1682 	unsigned int	i;
1683 	struct ndis_80211_var_ie *pIE;
1684 
1685 	for (i = sizeof(struct ndis_802_11_fix_ie); i < len;) {
1686 		pIE = (struct ndis_80211_var_ie *)(pframe + i);
1687 
1688 		switch (pIE->ElementID) {
1689 		case _VENDOR_SPECIFIC_IE_:
1690 			if ((!memcmp(pIE->data, ARTHEROS_OUI1, 3)) || (!memcmp(pIE->data, ARTHEROS_OUI2, 3))) {
1691 				DBG_871X("link to Artheros AP\n");
1692 				return HT_IOT_PEER_ATHEROS;
1693 			} else if ((!memcmp(pIE->data, BROADCOM_OUI1, 3))
1694 						|| (!memcmp(pIE->data, BROADCOM_OUI2, 3))
1695 						|| (!memcmp(pIE->data, BROADCOM_OUI3, 3))) {
1696 				DBG_871X("link to Broadcom AP\n");
1697 				return HT_IOT_PEER_BROADCOM;
1698 			} else if (!memcmp(pIE->data, MARVELL_OUI, 3)) {
1699 				DBG_871X("link to Marvell AP\n");
1700 				return HT_IOT_PEER_MARVELL;
1701 			} else if (!memcmp(pIE->data, RALINK_OUI, 3)) {
1702 				DBG_871X("link to Ralink AP\n");
1703 				return HT_IOT_PEER_RALINK;
1704 			} else if (!memcmp(pIE->data, CISCO_OUI, 3)) {
1705 				DBG_871X("link to Cisco AP\n");
1706 				return HT_IOT_PEER_CISCO;
1707 			} else if (!memcmp(pIE->data, REALTEK_OUI, 3)) {
1708 				u32 Vender = HT_IOT_PEER_REALTEK;
1709 
1710 				if (pIE->Length >= 5) {
1711 					if (pIE->data[4] == 1)
1712 						/* if (pIE->data[5] & RT_HT_CAP_USE_LONG_PREAMBLE) */
1713 						/* 	bssDesc->BssHT.RT2RT_HT_Mode |= RT_HT_CAP_USE_LONG_PREAMBLE; */
1714 						if (pIE->data[5] & RT_HT_CAP_USE_92SE)
1715 							/* bssDesc->BssHT.RT2RT_HT_Mode |= RT_HT_CAP_USE_92SE; */
1716 							Vender = HT_IOT_PEER_REALTEK_92SE;
1717 
1718 					if (pIE->data[5] & RT_HT_CAP_USE_SOFTAP)
1719 						Vender = HT_IOT_PEER_REALTEK_SOFTAP;
1720 
1721 					if (pIE->data[4] == 2) {
1722 						if (pIE->data[6] & RT_HT_CAP_USE_JAGUAR_BCUT) {
1723 							Vender = HT_IOT_PEER_REALTEK_JAGUAR_BCUTAP;
1724 							DBG_871X("link to Realtek JAGUAR_BCUTAP\n");
1725 						}
1726 						if (pIE->data[6] & RT_HT_CAP_USE_JAGUAR_CCUT) {
1727 							Vender = HT_IOT_PEER_REALTEK_JAGUAR_CCUTAP;
1728 							DBG_871X("link to Realtek JAGUAR_CCUTAP\n");
1729 						}
1730 					}
1731 				}
1732 
1733 				DBG_871X("link to Realtek AP\n");
1734 				return Vender;
1735 			} else if (!memcmp(pIE->data, AIRGOCAP_OUI, 3)) {
1736 				DBG_871X("link to Airgo Cap\n");
1737 				return HT_IOT_PEER_AIRGO;
1738 			} else
1739 				break;
1740 
1741 		default:
1742 			break;
1743 		}
1744 
1745 		i += (pIE->Length + 2);
1746 	}
1747 
1748 	DBG_871X("link to new AP\n");
1749 	return HT_IOT_PEER_UNKNOWN;
1750 }
1751 
1752 void update_IOT_info(struct adapter *padapter)
1753 {
1754 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1755 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1756 
1757 	switch (pmlmeinfo->assoc_AP_vendor) {
1758 	case HT_IOT_PEER_MARVELL:
1759 		pmlmeinfo->turboMode_cts2self = 1;
1760 		pmlmeinfo->turboMode_rtsen = 0;
1761 		break;
1762 
1763 	case HT_IOT_PEER_RALINK:
1764 		pmlmeinfo->turboMode_cts2self = 0;
1765 		pmlmeinfo->turboMode_rtsen = 1;
1766 		/* disable high power */
1767 		Switch_DM_Func(padapter, (~DYNAMIC_BB_DYNAMIC_TXPWR), false);
1768 		break;
1769 	case HT_IOT_PEER_REALTEK:
1770 		/* rtw_write16(padapter, 0x4cc, 0xffff); */
1771 		/* rtw_write16(padapter, 0x546, 0x01c0); */
1772 		/* disable high power */
1773 		Switch_DM_Func(padapter, (~DYNAMIC_BB_DYNAMIC_TXPWR), false);
1774 		break;
1775 	default:
1776 		pmlmeinfo->turboMode_cts2self = 0;
1777 		pmlmeinfo->turboMode_rtsen = 1;
1778 		break;
1779 	}
1780 
1781 }
1782 
1783 void update_capinfo(struct adapter *Adapter, u16 updateCap)
1784 {
1785 	struct mlme_ext_priv *pmlmeext = &Adapter->mlmeextpriv;
1786 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1787 	bool		ShortPreamble;
1788 
1789 	/*  Check preamble mode, 2005.01.06, by rcnjko. */
1790 	/*  Mark to update preamble value forever, 2008.03.18 by lanhsin */
1791 	/* if (pMgntInfo->RegPreambleMode == PREAMBLE_AUTO) */
1792 	{
1793 
1794 		if (updateCap & cShortPreamble) {
1795 			/*  Short Preamble */
1796 			if (pmlmeinfo->preamble_mode != PREAMBLE_SHORT) { /*  PREAMBLE_LONG or PREAMBLE_AUTO */
1797 				ShortPreamble = true;
1798 				pmlmeinfo->preamble_mode = PREAMBLE_SHORT;
1799 				rtw_hal_set_hwreg(Adapter, HW_VAR_ACK_PREAMBLE, (u8 *)&ShortPreamble);
1800 			}
1801 		} else{
1802 			/*  Long Preamble */
1803 			if (pmlmeinfo->preamble_mode != PREAMBLE_LONG) { /*  PREAMBLE_SHORT or PREAMBLE_AUTO */
1804 				ShortPreamble = false;
1805 				pmlmeinfo->preamble_mode = PREAMBLE_LONG;
1806 				rtw_hal_set_hwreg(Adapter, HW_VAR_ACK_PREAMBLE, (u8 *)&ShortPreamble);
1807 			}
1808 		}
1809 	}
1810 
1811 	if (updateCap & cIBSS)
1812 		/* Filen: See 802.11-2007 p.91 */
1813 		pmlmeinfo->slotTime = NON_SHORT_SLOT_TIME;
1814 	else {
1815 		/* Filen: See 802.11-2007 p.90 */
1816 		if (pmlmeext->cur_wireless_mode & (WIRELESS_11_24N | WIRELESS_11A | WIRELESS_11_5N | WIRELESS_11AC))
1817 			pmlmeinfo->slotTime = SHORT_SLOT_TIME;
1818 		else if (pmlmeext->cur_wireless_mode & (WIRELESS_11G)) {
1819 			if ((updateCap & cShortSlotTime) /* && (!(pMgntInfo->pHTInfo->RT2RT_HT_Mode & RT_HT_CAP_USE_LONG_PREAMBLE)) */)
1820 				/*  Short Slot Time */
1821 				pmlmeinfo->slotTime = SHORT_SLOT_TIME;
1822 			else
1823 				/*  Long Slot Time */
1824 				pmlmeinfo->slotTime = NON_SHORT_SLOT_TIME;
1825 		} else
1826 			/* B Mode */
1827 			pmlmeinfo->slotTime = NON_SHORT_SLOT_TIME;
1828 	}
1829 
1830 	rtw_hal_set_hwreg(Adapter, HW_VAR_SLOT_TIME, &pmlmeinfo->slotTime);
1831 
1832 }
1833 
1834 void update_wireless_mode(struct adapter *padapter)
1835 {
1836 	int ratelen, network_type = 0;
1837 	u32 SIFS_Timer;
1838 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1839 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1840 	struct wlan_bssid_ex		*cur_network = &(pmlmeinfo->network);
1841 	unsigned char 		*rate = cur_network->SupportedRates;
1842 
1843 	ratelen = rtw_get_rateset_len(cur_network->SupportedRates);
1844 
1845 	if ((pmlmeinfo->HT_info_enable) && (pmlmeinfo->HT_caps_enable))
1846 		pmlmeinfo->HT_enable = 1;
1847 
1848 	if (pmlmeext->cur_channel > 14) {
1849 		if (pmlmeinfo->VHT_enable)
1850 			network_type = WIRELESS_11AC;
1851 		else if (pmlmeinfo->HT_enable)
1852 			network_type = WIRELESS_11_5N;
1853 
1854 		network_type |= WIRELESS_11A;
1855 	} else{
1856 		if (pmlmeinfo->VHT_enable)
1857 			network_type = WIRELESS_11AC;
1858 		else if (pmlmeinfo->HT_enable)
1859 			network_type = WIRELESS_11_24N;
1860 
1861 		if ((cckratesonly_included(rate, ratelen)) == true)
1862 			network_type |= WIRELESS_11B;
1863 		else if ((cckrates_included(rate, ratelen)) == true)
1864 			network_type |= WIRELESS_11BG;
1865 		else
1866 			network_type |= WIRELESS_11G;
1867 	}
1868 
1869 	pmlmeext->cur_wireless_mode = network_type & padapter->registrypriv.wireless_mode;
1870 
1871 	SIFS_Timer = 0x0a0a0808; /* 0x0808 -> for CCK, 0x0a0a -> for OFDM */
1872 													/* change this value if having IOT issues. */
1873 
1874 	padapter->HalFunc.SetHwRegHandler(padapter, HW_VAR_RESP_SIFS,  (u8 *)&SIFS_Timer);
1875 
1876 	padapter->HalFunc.SetHwRegHandler(padapter, HW_VAR_WIRELESS_MODE,  (u8 *)&(pmlmeext->cur_wireless_mode));
1877 
1878 	if (pmlmeext->cur_wireless_mode & WIRELESS_11B)
1879 		update_mgnt_tx_rate(padapter, IEEE80211_CCK_RATE_1MB);
1880 	 else
1881 		update_mgnt_tx_rate(padapter, IEEE80211_OFDM_RATE_6MB);
1882 }
1883 
1884 void update_sta_basic_rate(struct sta_info *psta, u8 wireless_mode)
1885 {
1886 	if (IsSupportedTxCCK(wireless_mode)) {
1887 		/*  Only B, B/G, and B/G/N AP could use CCK rate */
1888 		memcpy(psta->bssrateset, rtw_basic_rate_cck, 4);
1889 		psta->bssratelen = 4;
1890 	} else{
1891 		memcpy(psta->bssrateset, rtw_basic_rate_ofdm, 3);
1892 		psta->bssratelen = 3;
1893 	}
1894 }
1895 
1896 int update_sta_support_rate(struct adapter *padapter, u8 *pvar_ie, uint var_ie_len, int cam_idx)
1897 {
1898 	unsigned int	ie_len;
1899 	struct ndis_80211_var_ie *pIE;
1900 	int	supportRateNum = 0;
1901 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1902 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1903 
1904 	pIE = (struct ndis_80211_var_ie *)rtw_get_ie(pvar_ie, _SUPPORTEDRATES_IE_, &ie_len, var_ie_len);
1905 	if (pIE == NULL)
1906 		return _FAIL;
1907 
1908 	memcpy(pmlmeinfo->FW_sta_info[cam_idx].SupportedRates, pIE->data, ie_len);
1909 	supportRateNum = ie_len;
1910 
1911 	pIE = (struct ndis_80211_var_ie *)rtw_get_ie(pvar_ie, _EXT_SUPPORTEDRATES_IE_, &ie_len, var_ie_len);
1912 	if (pIE)
1913 		memcpy((pmlmeinfo->FW_sta_info[cam_idx].SupportedRates + supportRateNum), pIE->data, ie_len);
1914 
1915 	return _SUCCESS;
1916 
1917 }
1918 
1919 void process_addba_req(struct adapter *padapter, u8 *paddba_req, u8 *addr)
1920 {
1921 	struct sta_info *psta;
1922 	u16 tid, start_seq, param;
1923 	struct recv_reorder_ctrl *preorder_ctrl;
1924 	struct sta_priv *pstapriv = &padapter->stapriv;
1925 	struct ADDBA_request *preq = (struct ADDBA_request *)paddba_req;
1926 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1927 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1928 
1929 	psta = rtw_get_stainfo(pstapriv, addr);
1930 
1931 	if (psta) {
1932 		start_seq = le16_to_cpu(preq->BA_starting_seqctrl) >> 4;
1933 
1934 		param = le16_to_cpu(preq->BA_para_set);
1935 		tid = (param>>2)&0x0f;
1936 
1937 		preorder_ctrl = &psta->recvreorder_ctrl[tid];
1938 
1939 		#ifdef CONFIG_UPDATE_INDICATE_SEQ_WHILE_PROCESS_ADDBA_REQ
1940 		preorder_ctrl->indicate_seq = start_seq;
1941 		#ifdef DBG_RX_SEQ
1942 		DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d, start_seq: %d\n", __func__, __LINE__,
1943 			preorder_ctrl->indicate_seq, start_seq);
1944 		#endif
1945 		#else
1946 		preorder_ctrl->indicate_seq = 0xffff;
1947 		#endif
1948 
1949 		preorder_ctrl->enable = (pmlmeinfo->bAcceptAddbaReq == true) ? true : false;
1950 	}
1951 
1952 }
1953 
1954 void update_TSF(struct mlme_ext_priv *pmlmeext, u8 *pframe, uint len)
1955 {
1956 	u8 *pIE;
1957 	__le32 *pbuf;
1958 
1959 	pIE = pframe + sizeof(struct ieee80211_hdr_3addr);
1960 	pbuf = (__le32 *)pIE;
1961 
1962 	pmlmeext->TSFValue = le32_to_cpu(*(pbuf+1));
1963 
1964 	pmlmeext->TSFValue = pmlmeext->TSFValue << 32;
1965 
1966 	pmlmeext->TSFValue |= le32_to_cpu(*pbuf);
1967 }
1968 
1969 void correct_TSF(struct adapter *padapter, struct mlme_ext_priv *pmlmeext)
1970 {
1971 	rtw_hal_set_hwreg(padapter, HW_VAR_CORRECT_TSF, NULL);
1972 }
1973 
1974 void adaptive_early_32k(struct mlme_ext_priv *pmlmeext, u8 *pframe, uint len)
1975 {
1976 	int i;
1977 	u8 *pIE;
1978 	__le32 *pbuf;
1979 	u64 tsf = 0;
1980 	u32 delay_ms;
1981 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1982 
1983 
1984 	pmlmeext->bcn_cnt++;
1985 
1986 	pIE = pframe + sizeof(struct ieee80211_hdr_3addr);
1987 	pbuf = (__le32 *)pIE;
1988 
1989 	tsf = le32_to_cpu(*(pbuf+1));
1990 	tsf = tsf << 32;
1991 	tsf |= le32_to_cpu(*pbuf);
1992 
1993 	/* DBG_871X("%s(): tsf_upper = 0x%08x, tsf_lower = 0x%08x\n", __func__, (u32)(tsf>>32), (u32)tsf); */
1994 
1995 	/* delay = (timestamp mod 1024*100)/1000 (unit: ms) */
1996 	/* delay_ms = do_div(tsf, (pmlmeinfo->bcn_interval*1024))/1000; */
1997 	delay_ms = rtw_modular64(tsf, (pmlmeinfo->bcn_interval*1024));
1998 	delay_ms = delay_ms/1000;
1999 
2000 	if (delay_ms >= 8)
2001 		pmlmeext->bcn_delay_cnt[8]++;
2002 		/* pmlmeext->bcn_delay_ratio[8] = (pmlmeext->bcn_delay_cnt[8] * 100) /pmlmeext->bcn_cnt; */
2003 	else
2004 		pmlmeext->bcn_delay_cnt[delay_ms]++;
2005 		/* pmlmeext->bcn_delay_ratio[delay_ms] = (pmlmeext->bcn_delay_cnt[delay_ms] * 100) /pmlmeext->bcn_cnt; */
2006 
2007 /*
2008 	DBG_871X("%s(): (a)bcn_cnt = %d\n", __func__, pmlmeext->bcn_cnt);
2009 
2010 
2011 	for (i = 0; i<9; i++)
2012 	{
2013 		DBG_871X("%s():bcn_delay_cnt[%d]=%d,  bcn_delay_ratio[%d]=%d\n", __func__, i,
2014 			pmlmeext->bcn_delay_cnt[i] , i, pmlmeext->bcn_delay_ratio[i]);
2015 	}
2016 */
2017 
2018 	/* dump for  adaptive_early_32k */
2019 	if (pmlmeext->bcn_cnt > 100 && (pmlmeext->adaptive_tsf_done == true)) {
2020 		u8 ratio_20_delay, ratio_80_delay;
2021 		u8 DrvBcnEarly, DrvBcnTimeOut;
2022 
2023 		ratio_20_delay = 0;
2024 		ratio_80_delay = 0;
2025 		DrvBcnEarly = 0xff;
2026 		DrvBcnTimeOut = 0xff;
2027 
2028 		DBG_871X("%s(): bcn_cnt = %d\n", __func__, pmlmeext->bcn_cnt);
2029 
2030 		for (i = 0; i < 9; i++) {
2031 			pmlmeext->bcn_delay_ratio[i] = (pmlmeext->bcn_delay_cnt[i] * 100) / pmlmeext->bcn_cnt;
2032 
2033 
2034 			DBG_871X("%s():bcn_delay_cnt[%d]=%d,  bcn_delay_ratio[%d]=%d\n", __func__, i,
2035 				pmlmeext->bcn_delay_cnt[i], i, pmlmeext->bcn_delay_ratio[i]);
2036 
2037 			ratio_20_delay += pmlmeext->bcn_delay_ratio[i];
2038 			ratio_80_delay += pmlmeext->bcn_delay_ratio[i];
2039 
2040 			if (ratio_20_delay > 20 && DrvBcnEarly == 0xff) {
2041 				DrvBcnEarly = i;
2042 				DBG_871X("%s(): DrvBcnEarly = %d\n", __func__, DrvBcnEarly);
2043 			}
2044 
2045 			if (ratio_80_delay > 80 && DrvBcnTimeOut == 0xff) {
2046 				DrvBcnTimeOut = i;
2047 				DBG_871X("%s(): DrvBcnTimeOut = %d\n", __func__, DrvBcnTimeOut);
2048 			}
2049 
2050 			/* reset adaptive_early_32k cnt */
2051 			pmlmeext->bcn_delay_cnt[i] = 0;
2052 			pmlmeext->bcn_delay_ratio[i] = 0;
2053 		}
2054 
2055 		pmlmeext->DrvBcnEarly = DrvBcnEarly;
2056 		pmlmeext->DrvBcnTimeOut = DrvBcnTimeOut;
2057 
2058 		pmlmeext->bcn_cnt = 0;
2059 	}
2060 
2061 }
2062 
2063 
2064 void beacon_timing_control(struct adapter *padapter)
2065 {
2066 	rtw_hal_bcn_related_reg_setting(padapter);
2067 }
2068 
2069 void rtw_alloc_macid(struct adapter *padapter, struct sta_info *psta)
2070 {
2071 	int i;
2072 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2073 	struct dvobj_priv *pdvobj = adapter_to_dvobj(padapter);
2074 
2075 
2076 	if (!memcmp(psta->hwaddr, bc_addr, ETH_ALEN))
2077 		return;
2078 
2079 	if (!memcmp(psta->hwaddr, myid(&padapter->eeprompriv), ETH_ALEN)) {
2080 		psta->mac_id = NUM_STA;
2081 		return;
2082 	}
2083 
2084 	spin_lock_bh(&pdvobj->lock);
2085 	for (i = 0; i < NUM_STA; i++) {
2086 		if (pdvobj->macid[i] == false) {
2087 			pdvobj->macid[i]  = true;
2088 			break;
2089 		}
2090 	}
2091 	spin_unlock_bh(&pdvobj->lock);
2092 
2093 	if (i > (NUM_STA-1)) {
2094 		psta->mac_id = NUM_STA;
2095 		DBG_871X("  no room for more MACIDs\n");
2096 	} else{
2097 		psta->mac_id = i;
2098 		DBG_871X("%s = %d\n", __func__, psta->mac_id);
2099 	}
2100 
2101 }
2102 
2103 void rtw_release_macid(struct adapter *padapter, struct sta_info *psta)
2104 {
2105 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2106 	struct dvobj_priv *pdvobj = adapter_to_dvobj(padapter);
2107 
2108 
2109 	if (!memcmp(psta->hwaddr, bc_addr, ETH_ALEN))
2110 		return;
2111 
2112 	if (!memcmp(psta->hwaddr, myid(&padapter->eeprompriv), ETH_ALEN))
2113 		return;
2114 
2115 	spin_lock_bh(&pdvobj->lock);
2116 	if (psta->mac_id < NUM_STA && psta->mac_id != 1) {
2117 		if (pdvobj->macid[psta->mac_id] == true) {
2118 			DBG_871X("%s = %d\n", __func__, psta->mac_id);
2119 			pdvobj->macid[psta->mac_id] = false;
2120 			psta->mac_id = NUM_STA;
2121 		}
2122 
2123 	}
2124 	spin_unlock_bh(&pdvobj->lock);
2125 
2126 }
2127 /* For 8188E RA */
2128 u8 rtw_search_max_mac_id(struct adapter *padapter)
2129 {
2130 	u8 max_mac_id = 0;
2131 	struct dvobj_priv *pdvobj = adapter_to_dvobj(padapter);
2132 	int i;
2133 	spin_lock_bh(&pdvobj->lock);
2134 	for (i = (NUM_STA-1); i >= 0 ; i--) {
2135 		if (pdvobj->macid[i] == true)
2136 			break;
2137 	}
2138 	max_mac_id = i;
2139 	spin_unlock_bh(&pdvobj->lock);
2140 
2141 	return max_mac_id;
2142 
2143 }
2144 
2145 struct adapter *dvobj_get_port0_adapter(struct dvobj_priv *dvobj)
2146 {
2147 	if (get_iface_type(dvobj->padapters[i]) != IFACE_PORT0)
2148 		return NULL;
2149 
2150 	return dvobj->padapters;
2151 }
2152 
2153 #ifdef CONFIG_GPIO_API
2154 int rtw_get_gpio(struct net_device *netdev, int gpio_num)
2155 {
2156 	u8 value;
2157 	u8 direction;
2158 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(netdev);
2159 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter);
2160 
2161 	rtw_ps_deny(adapter, PS_DENY_IOCTL);
2162 
2163 	DBG_871X("rf_pwrstate = 0x%02x\n", pwrpriv->rf_pwrstate);
2164 	LeaveAllPowerSaveModeDirect(adapter);
2165 
2166 	/* Read GPIO Direction */
2167 	direction = (rtw_read8(adapter, REG_GPIO_PIN_CTRL + 2) & BIT(gpio_num)) >> gpio_num;
2168 
2169 	/* According the direction to read register value */
2170 	if (direction)
2171 		value = (rtw_read8(adapter, REG_GPIO_PIN_CTRL + 1) & BIT(gpio_num)) >> gpio_num;
2172 	else
2173 		value = (rtw_read8(adapter, REG_GPIO_PIN_CTRL) & BIT(gpio_num)) >> gpio_num;
2174 
2175 	rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL);
2176 	DBG_871X("%s direction =%d value =%d\n", __func__, direction, value);
2177 
2178 	return value;
2179 }
2180 EXPORT_SYMBOL(rtw_get_gpio);
2181 
2182 int  rtw_set_gpio_output_value(struct net_device *netdev, int gpio_num, bool isHigh)
2183 {
2184 	u8 direction = 0;
2185 	u8 res = -1;
2186 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(netdev);
2187 
2188 	/* Check GPIO is 4~7 */
2189 	if (gpio_num > 7 || gpio_num < 4) {
2190 		DBG_871X("%s The gpio number does not included 4~7.\n", __func__);
2191 		return -1;
2192 	}
2193 
2194 	rtw_ps_deny(adapter, PS_DENY_IOCTL);
2195 
2196 	LeaveAllPowerSaveModeDirect(adapter);
2197 
2198 	/* Read GPIO direction */
2199 	direction = (rtw_read8(adapter, REG_GPIO_PIN_CTRL + 2) & BIT(gpio_num)) >> gpio_num;
2200 
2201 	/* If GPIO is output direction, setting value. */
2202 	if (direction) {
2203 		if (isHigh)
2204 			rtw_write8(adapter, REG_GPIO_PIN_CTRL + 1, rtw_read8(adapter, REG_GPIO_PIN_CTRL + 1) | BIT(gpio_num));
2205 		else
2206 			rtw_write8(adapter, REG_GPIO_PIN_CTRL + 1, rtw_read8(adapter, REG_GPIO_PIN_CTRL + 1) & ~BIT(gpio_num));
2207 
2208 		DBG_871X("%s Set gpio %x[%d]=%d\n", __func__, REG_GPIO_PIN_CTRL+1, gpio_num, isHigh);
2209 		res = 0;
2210 	} else{
2211 		DBG_871X("%s The gpio is input, not be set!\n", __func__);
2212 		res = -1;
2213 	}
2214 
2215 	rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL);
2216 	return res;
2217 }
2218 EXPORT_SYMBOL(rtw_set_gpio_output_value);
2219 
2220 int rtw_config_gpio(struct net_device *netdev, int gpio_num, bool isOutput)
2221 {
2222 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(netdev);
2223 
2224 	if (gpio_num > 7 || gpio_num < 4) {
2225 		DBG_871X("%s The gpio number does not included 4~7.\n", __func__);
2226 		return -1;
2227 	}
2228 
2229 	DBG_871X("%s gpio_num =%d direction =%d\n", __func__, gpio_num, isOutput);
2230 
2231 	rtw_ps_deny(adapter, PS_DENY_IOCTL);
2232 
2233 	LeaveAllPowerSaveModeDirect(adapter);
2234 
2235 	if (isOutput)
2236 		rtw_write8(adapter, REG_GPIO_PIN_CTRL + 2, rtw_read8(adapter, REG_GPIO_PIN_CTRL + 2) | BIT(gpio_num));
2237 	else
2238 		rtw_write8(adapter, REG_GPIO_PIN_CTRL + 2, rtw_read8(adapter, REG_GPIO_PIN_CTRL + 2) & ~BIT(gpio_num));
2239 
2240 	rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL);
2241 
2242 	return 0;
2243 }
2244 EXPORT_SYMBOL(rtw_config_gpio);
2245 #endif
2246 
2247 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
2248 void rtw_get_current_ip_address(struct adapter *padapter, u8 *pcurrentip)
2249 {
2250 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2251 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2252 	struct in_device *my_ip_ptr = padapter->pnetdev->ip_ptr;
2253 	u8 ipaddress[4];
2254 
2255 	if ((pmlmeinfo->state & WIFI_FW_LINKING_STATE) ||
2256 			pmlmeinfo->state & WIFI_FW_AP_STATE) {
2257 		if (my_ip_ptr != NULL) {
2258 			struct in_ifaddr *my_ifa_list = my_ip_ptr->ifa_list;
2259 			if (my_ifa_list != NULL) {
2260 				ipaddress[0] = my_ifa_list->ifa_address & 0xFF;
2261 				ipaddress[1] = (my_ifa_list->ifa_address >> 8) & 0xFF;
2262 				ipaddress[2] = (my_ifa_list->ifa_address >> 16) & 0xFF;
2263 				ipaddress[3] = my_ifa_list->ifa_address >> 24;
2264 				DBG_871X("%s: %d.%d.%d.%d ==========\n", __func__,
2265 						ipaddress[0], ipaddress[1], ipaddress[2], ipaddress[3]);
2266 				memcpy(pcurrentip, ipaddress, 4);
2267 			}
2268 		}
2269 	}
2270 }
2271 #endif
2272 #ifdef CONFIG_WOWLAN
2273 void rtw_get_sec_iv(struct adapter *padapter, u8 *pcur_dot11txpn, u8 *StaAddr)
2274 {
2275 	struct sta_info 	*psta;
2276 	struct security_priv *psecpriv = &padapter->securitypriv;
2277 
2278 	memset(pcur_dot11txpn, 0, 8);
2279 	if (NULL == StaAddr)
2280 		return;
2281 	psta = rtw_get_stainfo(&padapter->stapriv, StaAddr);
2282 	DBG_871X("%s(): StaAddr: %02x %02x %02x %02x %02x %02x\n",
2283 		__func__, StaAddr[0], StaAddr[1], StaAddr[2],
2284 		StaAddr[3], StaAddr[4], StaAddr[5]);
2285 
2286 	if (psta) {
2287 		if (psecpriv->dot11PrivacyAlgrthm != _NO_PRIVACY_ && psta->dot11txpn.val > 0)
2288 			psta->dot11txpn.val--;
2289 		AES_IV(pcur_dot11txpn, psta->dot11txpn, 0);
2290 
2291 		DBG_871X("%s(): CurrentIV: %02x %02x %02x %02x %02x %02x %02x %02x\n"
2292 		, __func__, pcur_dot11txpn[0], pcur_dot11txpn[1],
2293 		pcur_dot11txpn[2], pcur_dot11txpn[3], pcur_dot11txpn[4],
2294 		pcur_dot11txpn[5], pcur_dot11txpn[6], pcur_dot11txpn[7]);
2295 	}
2296 }
2297 void rtw_set_sec_pn(struct adapter *padapter)
2298 {
2299 		struct sta_info         *psta;
2300 		struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2301 		struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2302 		struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
2303 		struct security_priv *psecpriv = &padapter->securitypriv;
2304 
2305 		psta = rtw_get_stainfo(&padapter->stapriv,
2306 		get_my_bssid(&pmlmeinfo->network));
2307 
2308 		if (psta) {
2309 			if (pwrpriv->wowlan_fw_iv > psta->dot11txpn.val) {
2310 				if (psecpriv->dot11PrivacyAlgrthm != _NO_PRIVACY_)
2311 					psta->dot11txpn.val = pwrpriv->wowlan_fw_iv + 2;
2312 			} else {
2313 				DBG_871X("%s(): FW IV is smaller than driver\n", __func__);
2314 				psta->dot11txpn.val += 2;
2315 			}
2316 			DBG_871X("%s: dot11txpn: 0x%016llx\n", __func__, psta->dot11txpn.val);
2317 		}
2318 }
2319 #endif /* CONFIG_WOWLAN */
2320 
2321 #ifdef CONFIG_PNO_SUPPORT
2322 #define	CSCAN_TLV_TYPE_SSID_IE	'S'
2323 #define CIPHER_IE "key_mgmt ="
2324 #define CIPHER_NONE "NONE"
2325 #define CIPHER_WPA_PSK "WPA-PSK"
2326 #define CIPHER_WPA_EAP "WPA-EAP IEEE8021X"
2327 
2328 #endif /* CONFIG_PNO_SUPPORT */
2329