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